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.tool;
018:
019: import net.refractions.udig.project.ui.ApplicationGIS;
020: import net.refractions.udig.project.ui.internal.tool.display.ToolProxy;
021:
022: import org.eclipse.jface.action.IStatusLineManager;
023:
024: /**
025: * @author Vitalus
026: *
027: */
028: /**
029: * An abstract super class that modal tools can extend.
030: * <p>
031: * The editor will only maintain one modal tool in the "enabled" state at one time.
032: * </p>
033: *
034: * @author jeichar
035: * @since 0.3
036: * @see AbstractTool
037: * @see ModalTool
038: */
039: public abstract class AbstractModalTool extends AbstractTool implements
040: ModalTool {
041:
042: // String statusBarMessage;
043: // String statusBarErrorMessage;
044:
045: private boolean active;
046:
047: /**
048: * Current ID of the tool cursor.
049: */
050: private String currentCursorID;
051:
052: /**
053: * By default SimpleTool will simply respond to MOUSE.
054: * <p>
055: * To respond to additional stimulus please override your constuctor
056: * to call AbstractModalTool( targets ):<pre><code>
057: * public class MyTool extends AbstractModalTool {
058: * public MyTool(){ // default consturctor called by extention point
059: * super( MOUSE | WHEEL );
060: * }
061: * ...
062: * }
063: * </code></pre>
064: */
065: public AbstractModalTool() {
066: super (MOUSE);
067: }
068:
069: /**
070: * Creates an new instance of AbstractModalTool
071: *
072: * @see AbstractTool#AbstractTool(int)
073: */
074: public AbstractModalTool(int targets) {
075: super (targets);
076: }
077:
078: /**
079: * @see net.refractions.udig.project.ui.tool.ModalTool#setActive(boolean)
080: */
081: public void setActive(boolean active) {
082: this .active = active;
083: setStatusBarMessage(active);
084: if (!active) {
085: deregisterMouseListeners();
086: } else {
087: if (isEnabled())
088: registerMouseListeners();
089: }
090: }
091:
092: /**
093: * (non-Javadoc)
094: * @see net.refractions.udig.project.ui.tool.ModalTool#isActive()
095: */
096: public boolean isActive() {
097: return active;
098: }
099:
100: private void setStatusBarMessage(final boolean active) {
101: getContext().updateUI(new Runnable() {
102: public void run() {
103: if (getContext().getActionBars() == null)
104: return;
105: IStatusLineManager bar = getContext().getActionBars()
106: .getStatusLineManager();
107: if (bar != null) {
108: bar.setMessage(""); //$NON-NLS-1$
109: bar.setErrorMessage(""); //$NON-NLS-1$
110: }
111: }
112: });
113: }
114:
115: /**
116: * @see net.refractions.udig.project.ui.tool.AbstractTool#setContext(net.refractions.udig.project.ui.tool.IToolContext)
117: */
118: public void setContext(IToolContext context) {
119: deregisterMouseListeners();
120: this .context = context;
121: if (isActive() && isEnabled())
122: registerMouseListeners();
123: }
124:
125: /**
126: * (non-Javadoc)
127: * @see net.refractions.udig.project.ui.tool.ModalTool#getCursorID()
128: */
129: public final String getCursorID() {
130: return currentCursorID;
131: }
132:
133: /**
134: * (non-Javadoc)
135: * @see net.refractions.udig.project.ui.tool.ModalTool#setCursorID(java.lang.String)
136: */
137: public final void setCursorID(String id) {
138: this .currentCursorID = id;
139:
140: if (isActive() && getContext() != null
141: && !getContext().getViewportPane().isDisposed()) {
142: getContext().getViewportPane().setCursor(
143: ApplicationGIS.getToolManager().findToolCursor(
144: currentCursorID));
145: }
146: }
147:
148: /**
149: * (non-Javadoc)
150: * @see net.refractions.udig.project.ui.tool.Tool#setEnabled(boolean)
151: */
152: public void setEnabled(boolean enabled) {
153: boolean oldValue = isEnabled();
154:
155: boolean tempNotify = isNotifyListeners();
156: setNotifyListeners(false);
157: super .setEnabled(enabled);
158: setNotifyListeners(tempNotify);
159:
160: if (oldValue != enabled) {
161: IToolContext toolContext = getContext();
162:
163: if (!enabled) {
164: if (toolContext != null) {
165: if (isActive()) {
166: deregisterMouseListeners();
167: }
168: setProperty("latestCursorId", getCursorID()); //$NON-NLS-1$
169: setCursorID(ModalTool.NO_CURSOR);
170: }
171: } else {
172: if (toolContext != null) {
173: if (isActive()) {
174: registerMouseListeners();
175: }
176: String defaultCursorId = (String) getProperty("latestCursorId"); //$NON-NLS-1$
177: setCursorID(defaultCursorId);
178: }
179: }
180:
181: }
182:
183: if (isNotifyListeners() && oldValue != enabled) {
184: ToolLifecycleEvent event = new ToolLifecycleEvent(this,
185: ToolLifecycleEvent.Type.ENABLE, enabled, oldValue);
186: fireEvent(event);
187: }
188:
189: }
190:
191: }
|