001: /*
002: * $Id: JGraphEditorToolbox.java,v 1.5 2006/02/03 17:31:13 gaudenz Exp $
003: * Copyright (c) 2001-2005, Gaudenz Alder
004: *
005: * All rights reserved.
006: *
007: * See LICENSE file for license details. If you are unable to locate
008: * this file please contact info (at) jgraph (dot) com.
009: */
010: package com.jgraph.editor.factory;
011:
012: import java.awt.Graphics;
013: import java.awt.event.MouseEvent;
014:
015: import javax.swing.AbstractButton;
016: import javax.swing.JToolBar;
017:
018: import org.jgraph.JGraph;
019: import org.jgraph.graph.BasicMarqueeHandler;
020: import org.w3c.dom.Node;
021:
022: import com.jgraph.editor.JGraphEditorFactory;
023: import com.jgraph.editor.JGraphEditorTool;
024:
025: /**
026: * A toolbox that contains a set of tools to be installed as marquee handlers in
027: * a graph. Toolboxes are created using the built-in
028: * {@link JGraphEditorFactory#createToolbox(Node)}.
029: */
030: public class JGraphEditorToolbox extends JToolBar {
031:
032: /**
033: * Specifies whether the toolbox should select the default button after a
034: * gesture with another tool if the default button exists. By default, the
035: * default button is assigned to be the first inserted button (see
036: * JGraphEditorFactory.configureToolbox) and the default for this value is
037: * false.
038: */
039: public static boolean SWITCH_BACK = false;
040:
041: /**
042: * Holds the marquee redirector.
043: */
044: protected BasicMarqueeHandler redirector = new MarqueeRedirector();
045:
046: /**
047: * References the previous marquee handler.
048: */
049: protected BasicMarqueeHandler previousMarqueeHandler = null;
050:
051: /**
052: * References the tool that is currently selected, ie the tool that is used
053: * to interact with the graph.
054: */
055: protected JGraphEditorTool selectionTool;
056:
057: /**
058: * References the default tool button which is activated after the use of
059: * any other tool.
060: */
061: protected AbstractButton defaultButton;
062:
063: /**
064: * References the graph that is currently installed, ie the graph that is
065: * used to interact with the selection tool.
066: */
067: protected JGraph graph;
068:
069: /**
070: * Constructs a new toolbox.
071: */
072: public JGraphEditorToolbox() {
073: super ();
074: }
075:
076: /**
077: * Returns the installed graph.
078: *
079: * @return Returns the installed graph.
080: */
081: public JGraph getGraph() {
082: return graph;
083: }
084:
085: /**
086: * Sets the installed graph and restores the marquee handler on the
087: * previously installed graph.
088: *
089: * @param newGraph
090: * The graph to be installed.
091: */
092: public void setGraph(JGraph newGraph) {
093: JGraph oldGraph = getGraph();
094: if (oldGraph != newGraph) {
095: if (oldGraph != null)
096: oldGraph.setMarqueeHandler(previousMarqueeHandler);
097: previousMarqueeHandler = newGraph.getMarqueeHandler();
098: newGraph.setMarqueeHandler(redirector);
099: this .graph = newGraph;
100: }
101: }
102:
103: /**
104: * Sets the default tool, which is activated after single use of any other
105: * tool.
106: *
107: * @param defaultTool
108: * The default tool to be used.
109: */
110: public void setDefaultButton(AbstractButton defaultButton) {
111: this .defaultButton = defaultButton;
112: }
113:
114: /**
115: * Returns the selected tool.
116: *
117: * @return Returns the selected tool.
118: */
119: public JGraphEditorTool getSelectionTool() {
120: return selectionTool;
121: }
122:
123: /**
124: * Sets the selected tool.
125: *
126: * @param selectionTool
127: * The tool to be selected.
128: */
129: public void setSelectionTool(JGraphEditorTool selectionTool) {
130: this .selectionTool = selectionTool;
131: }
132:
133: /**
134: * A class that redirects marquee events to the marquee handler it replaces
135: * or to the selection tool of the enclosing toolbox depending on the return
136: * value of {@link BasicMarqueeHandler#isForceMarqueeEvent(MouseEvent)} of
137: * the {@link JGraphEditorToolbox#previousMarqueeHandler}.
138: */
139: public class MarqueeRedirector extends BasicMarqueeHandler {
140:
141: /**
142: * Indicates whether the initial isForceMarqueeEvent returned true.
143: */
144: protected boolean redirect = false;
145:
146: /**
147: * Returns true if the tool wants to take control of an interaction, ie.
148: * if it handles the sequence of events, or if the previous marquee
149: * handler wants to do so.
150: *
151: * @param event
152: * The object that describes the event.
153: * @return Returns the true if the event is handled.
154: */
155: public boolean isForceMarqueeEvent(MouseEvent event) {
156: if (previousMarqueeHandler != null
157: && previousMarqueeHandler
158: .isForceMarqueeEvent(event)) {
159: redirect = true;
160: return true;
161: } else if (selectionTool != null)
162: return selectionTool.isAlwaysActive()
163: || selectionTool.isForceMarqueeEvent(event);
164: return false;
165: }
166:
167: /**
168: * Overrides the basic marquee handler by redirecting to the previous
169: * marquee handler or selection tool's mousePressed method.
170: *
171: * @param event
172: * The object that describes the event.
173: */
174: public void mousePressed(MouseEvent event) {
175: if (redirect || selectionTool == null)
176: previousMarqueeHandler.mousePressed(event);
177: else
178: selectionTool.mousePressed(event);
179: }
180:
181: /**
182: * Overrides the basic marquee handler by redirecting to the previous
183: * marquee handler or selection tool's mouseDragged method.
184: *
185: * @param event
186: * The object that describes the event.
187: */
188: public void mouseDragged(MouseEvent event) {
189: if (redirect || selectionTool == null)
190: previousMarqueeHandler.mouseDragged(event);
191: else
192: selectionTool.mouseDragged(event);
193: }
194:
195: /**
196: * Overrides the basic marquee handler by redirecting to the previous
197: * marquee handler or selection tool's mouseReleased method.
198: *
199: * @param event
200: * The object that describes the event.
201: */
202: public void mouseReleased(MouseEvent event) {
203: if (redirect || selectionTool == null) {
204: redirect = false;
205: previousMarqueeHandler.mouseReleased(event);
206: } else
207: selectionTool.mouseReleased(event);
208:
209: // Switches back to the default button
210: if (SWITCH_BACK && defaultButton != null
211: && !defaultButton.isSelected()) {
212: defaultButton.setSelected(true);
213: }
214: }
215:
216: /**
217: * Overrides the basic marquee handler to display the selection tool
218: * cursor and redirect to the selection tool.
219: *
220: * @param event
221: * The object that describes the event.
222: */
223: public void mouseMoved(MouseEvent event) {
224: if (selectionTool == null
225: || previousMarqueeHandler != null
226: && previousMarqueeHandler
227: .isForceMarqueeEvent(event))
228: previousMarqueeHandler.mouseMoved(event);
229: else
230: selectionTool.mouseMoved(event);
231: }
232:
233: /**
234: * Overrides the paint method to redirect to the selection tool.
235: *
236: * @param graph
237: * The graph to perform the preview in.
238: * @param g
239: * The graphics object to be used for painting.
240: */
241: public void paint(JGraph graph, Graphics g) {
242: if (redirect || selectionTool == null)
243: previousMarqueeHandler.paint(graph, g);
244: else
245: selectionTool.paint(graph, g);
246: }
247:
248: /**
249: * Overrides the overlay method to redirect to the selection tool.
250: *
251: * @param graph
252: * The graph to perform the preview in.
253: * @param g
254: * The graphics object to be used for painting.
255: * @param clear
256: * Specifies if the canvas should be cleared.
257: */
258: public void overlay(JGraph graph, Graphics g, boolean clear) {
259: if (redirect || selectionTool == null)
260: previousMarqueeHandler.overlay(graph, g, clear);
261: else
262: selectionTool.overlay(graph, g, clear);
263: }
264:
265: }
266:
267: }
|