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.ArrayList;
020: import java.util.Collections;
021: import java.util.List;
022: import java.util.concurrent.locks.Lock;
023: import java.util.concurrent.locks.ReentrantLock;
024:
025: import net.refractions.udig.internal.ui.operations.OperationCategory;
026: import net.refractions.udig.project.ui.internal.ProjectUIPlugin;
027: import net.refractions.udig.ui.PlatformGIS;
028: import net.refractions.udig.ui.graphics.Glyph;
029: import net.refractions.udig.ui.operations.ILazyOpListener;
030: import net.refractions.udig.ui.operations.OpFilter;
031:
032: import org.eclipse.core.commands.IHandler;
033: import org.eclipse.jface.resource.ImageDescriptor;
034: import org.eclipse.jface.resource.ImageRegistry;
035: import org.eclipse.swt.graphics.Image;
036:
037: /**
038: * <p>
039: * Responsibilities:
040: * <ul>
041: * <li> Maintain that only ModalItem is active at a time. </li>
042: * <li> Mark contained ContributionItems as selected or not depending on whether the current
043: * ModalItem is active. </li>
044: * <li> Contains a set of contributions. </li>
045: * </ul>
046: *
047: * @author jeichar
048: * @since 0.9.0
049: */
050: public abstract class ModalItem implements ILazyOpListener {
051:
052: private static ImageRegistry IMAGES = ProjectUIPlugin.getDefault()
053: .getImageRegistry();
054:
055: // protected static final Cursor defaultCursor = PlatformUI.getWorkbench().getDisplay()
056: // .getSystemCursor(SWT.CURSOR_ARROW);
057:
058: private List<CurrentContributionItem> contributions = new ArrayList<CurrentContributionItem>();
059:
060: protected String[] commandIds;
061: protected String handlerType;
062: protected ImageDescriptor imageDescriptor;
063: protected String name;
064: protected String toolTipText;
065: protected String id;
066: protected OpFilter enablement;
067: protected List<OperationCategory> operationCategories;
068: protected boolean isEnabled = true;
069:
070: /**
071: * Gets the image descriptor of the item.
072: *
073: * @return the image descripor of the item.
074: */
075: public ImageDescriptor getImageDescriptor() {
076: return imageDescriptor;
077: }
078:
079: /**
080: * Marks each contribution item as selected.
081: *
082: * @param checked the selected value of each contribution.
083: */
084: public void setChecked(boolean checked) {
085: List<CurrentContributionItem> toRemove = new ArrayList<CurrentContributionItem>();
086: for (CurrentContributionItem item : contributions) {
087: if (item.isDisposed()) {
088: toRemove.add(item);
089: } else
090: item.setSelection(checked, this );
091: }
092: contributions.removeAll(toRemove);
093: }
094:
095: /**
096: * @see net.refractions.udig.project.ui.tool.ActionTool#run()
097: */
098: public void run() {
099: if (runModeless() && isEnabled)
100: return;
101:
102: ModalItem activeModalItem = getActiveItem();
103: if (activeModalItem != null)
104: activeModalItem.setActive(false);
105:
106: setActive(true);
107: setActiveItem(this );
108:
109: }
110:
111: /**
112: * Gets the default item.
113: *
114: * @return the default item.
115: */
116: protected abstract ModalItem getDefaultItem();
117:
118: /**
119: * Returns the currently active item or null if no currently active tool.
120: *
121: * @return the currently active item or null if no currently active tool.
122: */
123: protected abstract ModalItem getActiveItem();
124:
125: /**
126: * Sets the currently active item.
127: */
128: protected abstract void setActiveItem(ModalItem item);
129:
130: /**
131: * If the current Item is modeless then runModelss runs the item and return true.
132: *
133: * @return true if the item is modeless.
134: */
135: protected abstract boolean runModeless();
136:
137: /**
138: * disposes of any resources held by the item.
139: */
140: public abstract void dispose();
141:
142: /**
143: * Returns true if the item is disposed
144: *
145: * @return Returns true if the item is disposed
146: */
147: public abstract boolean isDisposed();
148:
149: /**
150: * Activates the current item. The activeTool item field does not need to be set in this method.
151: */
152: protected abstract void setActive(boolean active);
153:
154: /**
155: * Returns the list of contributions controlled by this item.
156: */
157: public List<CurrentContributionItem> getContributions() {
158: return Collections.unmodifiableList(contributions);
159: }
160:
161: public boolean addContribution(CurrentContributionItem contribution) {
162: contribution.setEnabled(isEnabled());
163: return contributions.add(contribution);
164: }
165:
166: public CurrentContributionItem removeContribution(int index) {
167: return contributions.remove(index);
168: }
169:
170: public boolean removeContribution(
171: CurrentContributionItem contribution) {
172: return contributions.remove(contribution);
173: }
174:
175: public void clearContributions() {
176: contributions.clear();
177: }
178:
179: /**
180: * Returns an instance of a command handler for the current item.
181: *
182: * @param commandId the id of the command to get a handler for.
183: * @return an instance of a command handler for the current item.
184: */
185: public abstract IHandler getHandler(String commandId);
186:
187: /**
188: * Returns the list desired commands
189: *
190: * @return the list of desired commands
191: */
192: public String[] getCommandIds() {
193:
194: String[] c = new String[commandIds.length];
195: System.arraycopy(commandIds, 0, c, 0, c.length);
196: return c;
197: }
198:
199: /**
200: * ID of item
201: *
202: * @return the id
203: */
204: public String getId() {
205: return id;
206: }
207:
208: /**
209: * sets the id of the item
210: *
211: * @param id the new id.
212: */
213: public void setId(String id) {
214: this .id = id;
215: }
216:
217: /**
218: * gets the name of the item.
219: *
220: * @return the name of the item.
221: */
222: public String getName() {
223: return name;
224: }
225:
226: /**
227: * Sets the name of the item
228: *
229: * @param name the new name
230: */
231: public void setName(String name) {
232: this .name = name;
233: }
234:
235: /**
236: * Gets the tooltip of the item
237: *
238: * @return the tooltip of the item.
239: */
240: public String getToolTipText() {
241: return toolTipText;
242: }
243:
244: /**
245: * sets the tooltip of the item
246: *
247: * @param toolTipText the new tooltip
248: */
249: public void setToolTipText(String toolTipText) {
250: this .toolTipText = toolTipText;
251: }
252:
253: /**
254: * Sets the images descriptor of the item.
255: *
256: * @param imageDescriptor the new image descriptor.
257: */
258: public void setImageDescriptor(ImageDescriptor imageDescriptor) {
259: this .imageDescriptor = imageDescriptor;
260: IMAGES.remove(getId());
261: }
262:
263: /**
264: * Gets the icon image of the tool
265: *
266: * @return the icon image of the tool.
267: */
268: public Image getImage() {
269: if (IMAGES.get(getId()) == null
270: || IMAGES.get(getId()).isDisposed()) {
271: IMAGES.remove(getId());
272: IMAGES.put(getId(), getImageDescriptor());
273: }
274:
275: return IMAGES.get(getId());
276: }
277:
278: /**
279: * Returns the "pushed" look of an active icon.
280: *
281: * @return the "pushed" look of an active icon.
282: */
283: public Image getActiveImage() {
284: if (getImageDescriptor() == null) {
285: return null;
286: }
287: if (IMAGES.get(getId() + "pushed") == null || IMAGES.get(getId() + "pushed").isDisposed()) { //$NON-NLS-1$ //$NON-NLS-2$
288: IMAGES
289: .put(
290: getId() + "pushed", Glyph.push(getImageDescriptor())); //$NON-NLS-1$
291: }
292:
293: return IMAGES.get(getId() + "pushed"); //$NON-NLS-1$
294: }
295:
296: public OpFilter getEnablesFor() {
297: if (enablement == null)
298: return OpFilter.TRUE;
299: return enablement;
300: }
301:
302: /**
303: * Returns whether the item is enabled.
304: *
305: * @return
306: */
307: public boolean isEnabled() {
308: return isEnabled;
309: }
310:
311: public void setEnabled(final boolean isEnabled) {
312: PlatformGIS.syncInDisplayThread(new Runnable() {
313: public void run() {
314: internalSetEnabled(isEnabled);
315: }
316: });
317: }
318:
319: private Lock enabledLock = new ReentrantLock();
320:
321: protected void internalSetEnabled(boolean isEnabled2) {
322: enabledLock.lock();
323: try {
324: this .isEnabled = isEnabled2;
325: for (CurrentContributionItem contrib : getContributions()) {
326: contrib.setEnabled(isEnabled2);
327: }
328: } finally {
329: enabledLock.unlock();
330: }
331: }
332:
333: public List<OperationCategory> getOperationCategories() {
334: if (operationCategories == null) {
335: return Collections.emptyList();
336: }
337: return Collections.unmodifiableList(operationCategories);
338: }
339: }
|