001: /*
002: * uDig - User Friendly Desktop Internet GIS client
003: * http://udig.refractions.net
004: * (C) 2004, Refractions Research Inc.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */
017: package net.refractions.udig.project.ui.internal.tool.display;
018:
019: import java.util.Iterator;
020: import java.util.LinkedList;
021: import java.util.List;
022:
023: import net.refractions.udig.project.ui.internal.Messages;
024: import net.refractions.udig.project.ui.tool.IToolManager;
025:
026: import org.eclipse.core.commands.Command;
027: import org.eclipse.core.commands.IHandler;
028: import org.eclipse.core.runtime.IConfigurationElement;
029: import org.eclipse.jface.resource.ImageDescriptor;
030: import org.eclipse.ui.IActionBars;
031: import org.eclipse.ui.commands.ICommandService;
032: import org.eclipse.ui.plugin.AbstractUIPlugin;
033:
034: /**
035: * Representation of a category, this is a UI construct.
036: *
037: * @author jeichar
038: * @since 0.9.0
039: */
040: public abstract class ToolCategory implements Iterable<ModalItem> {
041: /** The list of items in the category */
042: public List<ModalItem> items = new LinkedList<ModalItem>();
043:
044: /** The tool manager instance */
045: protected IToolManager manager;
046: /** id of the category */
047: protected String id;
048: /** name of the category */
049: protected String name;
050: private String commandId;
051: private ImageDescriptor icon;
052:
053: private boolean handlersSet = false;
054:
055: protected IConfigurationElement element;
056:
057: /**
058: * Construct <code>ToolCategory</code>.
059: *
060: * @param element the configurationelement that declares the category
061: * @param manager the containing manager
062: */
063: protected ToolCategory(IConfigurationElement element,
064: IToolManager manager) {
065: id = element.getAttribute("id"); //$NON-NLS-1$
066: commandId = element.getAttribute("commandId"); //$NON-NLS-1$
067: name = element.getAttribute("name"); //$NON-NLS-1$
068: String iconPath = element.getAttribute("icon"); //$NON-NLS-1$
069: if (iconPath != null)
070: icon = AbstractUIPlugin.imageDescriptorFromPlugin(element
071: .getNamespace(), iconPath);
072: this .element = element;
073: this .manager = manager;
074: }
075:
076: /**
077: * Construct <code>ToolCategory2</code>.
078: *
079: * @param manager
080: */
081: public ToolCategory(IToolManager manager) {
082: this .manager = manager;
083: id = Messages.ToolCategory_other;
084: name = Messages.ToolCategory_other_menu;
085: icon = null;
086: commandId = null;
087: }
088:
089: public void dispose(IActionBars bars) {
090: for (Iterator<ModalItem> iter = items.iterator(); iter
091: .hasNext();) {
092: ModalItem item = iter.next();
093: List<CurrentContributionItem> contributions = item
094: .getContributions();
095: for (CurrentContributionItem item2 : contributions) {
096: bars.getMenuManager().remove(item2);
097: bars.getToolBarManager().remove(item2);
098: bars.getStatusLineManager().remove(item2);
099: }
100: item.clearContributions();
101: }
102: }
103:
104: /**
105: * Add an item to the category
106: *
107: * @param item the new item
108: */
109: public void add(ModalItem item) {
110: items.add(item);
111: }
112:
113: /**
114: * @return an iterator that iterates through the items in the category
115: */
116: public Iterator<ModalItem> iterator() {
117: return items.iterator();
118: }
119:
120: /**
121: * Sets the commandHandler for this category.
122: * @param ids
123: */
124: public void setCommandHandlers(ICommandService service) {
125:
126: if (!handlersSet) {
127: if (commandId != null) {
128: Command command = service.getCommand(commandId);
129: if (command != null && getHandler() != null)
130: command.setHandler(getHandler());
131: }
132: handlersSet = true;
133: }
134: }
135:
136: /**
137: * Gets the command handler for the category.
138: *
139: * @return the command handler for the category.
140: */
141: protected abstract IHandler getHandler();
142:
143: /**
144: * Gets the icon for the category.
145: *
146: * @return the icon for the category.
147: */
148: public ImageDescriptor getIcon() {
149: return icon;
150: }
151:
152: /**
153: * Returns the id of the category
154: *
155: * @return the id of the category
156: */
157: public String getId() {
158: return id;
159: }
160:
161: /**
162: * Returns the name of the category
163: *
164: * @return the name of the category
165: */
166: public String getName() {
167: return name;
168: }
169: }
|