/*
* Freeplane - mind map editor
* Copyright (C) 2008 Joerg Mueller, Daniel Polansky, Christian Foltin, Dimitry Polivaev
*
* This file is created by Dimitry Polivaev in 2008.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.freeplane.features.icon;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import org.freeplane.core.extension.IExtension;
import org.freeplane.core.io.ReadManager;
import org.freeplane.core.io.WriteManager;
import org.freeplane.features.filter.FilterController;
import org.freeplane.features.icon.factory.IconStoreFactory;
import org.freeplane.features.map.MapController;
import org.freeplane.features.map.NodeModel;
import org.freeplane.features.mode.CombinedPropertyChain;
import org.freeplane.features.mode.Controller;
import org.freeplane.features.mode.IPropertyHandler;
import org.freeplane.features.mode.ModeController;
import org.freeplane.features.styles.IStyle;
import org.freeplane.features.styles.LogicalStyleController;
import org.freeplane.features.styles.MapStyleModel;
import org.freeplane.features.styles.StyleNode;
/**
* @author Dimitry Polivaev
*/
public class IconController implements IExtension {
final private CombinedPropertyChain<Collection<MindIcon>, NodeModel> iconHandlers;
public static IconController getController() {
final ModeController modeController = Controller.getCurrentModeController();
return getController(modeController);
}
public static IconController getController(ModeController modeController) {
return (IconController) modeController.getExtension(IconController.class);
}
public static void install() {
FilterController.getCurrentFilterController().getConditionFactory().addConditionController(1,
new IconConditionController());
FilterController.getCurrentFilterController().getConditionFactory().addConditionController(5,
new PriorityConditionController());
}
public static void install( final IconController iconController) {
final ModeController modeController = Controller.getCurrentModeController();
modeController.addExtension(IconController.class, iconController);
}
// final private ModeController modeController;
final private Collection<IStateIconProvider> stateIconProviders;
public boolean addStateIconProvider(IStateIconProvider o) {
return stateIconProviders.add(o);
}
public boolean removeStateIconProvider(IStateIconProvider o) {
return stateIconProviders.remove(o);
}
public IconController(final ModeController modeController) {
super();
stateIconProviders = new LinkedList<IStateIconProvider>();
iconHandlers = new CombinedPropertyChain<Collection<MindIcon>, NodeModel>(false);
// this.modeController = modeController;
final MapController mapController = modeController.getMapController();
final ReadManager readManager = mapController.getReadManager();
final WriteManager writeManager = mapController.getWriteManager();
final IconBuilder textBuilder = new IconBuilder(this, IconStoreFactory.create());
textBuilder.registerBy(readManager, writeManager);
addIconGetter(IPropertyHandler.STYLE, new IPropertyHandler<Collection<MindIcon>, NodeModel>() {
public Collection<MindIcon> getProperty(final NodeModel node, final Collection<MindIcon> currentValue) {
final MapStyleModel model = MapStyleModel.getExtension(node.getMap());
final Collection<IStyle> styleKeys = LogicalStyleController.getController(modeController).getStyles(node);
for(IStyle styleKey : styleKeys){
final NodeModel styleNode = model.getStyleNode(styleKey);
if (styleNode == null || node == styleNode && !(styleKey instanceof StyleNode)) {
continue;
}
final List<MindIcon> styleIcons;
styleIcons = styleNode.getIcons();
currentValue.addAll(styleIcons);
}
return currentValue;
}
});
}
public IPropertyHandler<Collection<MindIcon>, NodeModel> addIconGetter(
final Integer key,
final IPropertyHandler<Collection<MindIcon>, NodeModel> getter) {
return iconHandlers.addGetter(key, getter);
}
public IPropertyHandler<Collection<MindIcon>, NodeModel> removeIconGetter(
final Integer key,
final IPropertyHandler<Collection<MindIcon>, NodeModel> getter) {
return iconHandlers.addGetter(key, getter);
}
public Collection<MindIcon> getIcons(final NodeModel node) {
final Collection<MindIcon> icons = iconHandlers.getProperty(node, new LinkedList<MindIcon>());
return icons;
}
public final Collection<UIIcon> getStateIcons(final NodeModel node){
final LinkedList<UIIcon> icons = new LinkedList<UIIcon>();
for(IStateIconProvider provider : stateIconProviders){
final UIIcon stateIcon = provider.getStateIcon(node);
if(stateIcon != null){
icons.add(stateIcon);
final IconRegistry iconRegistry = node.getMap().getIconRegistry();
iconRegistry.addIcon(stateIcon);
}
}
return icons;
}
}