001: /*
002: * $Id: JGraphEditorAction.java,v 1.4 2007/07/27 14:15:59 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;
011:
012: import java.awt.Component;
013: import java.awt.Frame;
014: import java.awt.KeyboardFocusManager;
015: import java.awt.Window;
016: import java.awt.event.ActionEvent;
017:
018: import javax.swing.AbstractAction;
019: import javax.swing.Action;
020: import javax.swing.JFrame;
021: import javax.swing.SwingUtilities;
022:
023: import org.jgraph.JGraph;
024:
025: import com.jgraph.editor.factory.JGraphEditorDiagramPane;
026: import com.jgraph.editor.factory.JGraphEditorNavigator;
027: import com.jgraph.pad.factory.JGraphpadPane;
028:
029: /**
030: * The base class for all actions in a JGraph editor kit. An action may be
031: * toggleable, in which case the UI factory will create an element that displays
032: * the action's selection state, and listen to changes of this state to update
033: * the element. The enabled state will also be listened to and the UI elements
034: * will be updated accordingly.
035: */
036: public abstract class JGraphEditorAction extends AbstractAction {
037:
038: /**
039: * Bean property name for <code>isSelected</code>
040: */
041: public final static String PROPERTY_ISSELECTED = new String(
042: "isSelected");
043:
044: /**
045: * Bean property name for <code>isVisible</code>
046: */
047: public final static String PROPERTY_ISVISIBLE = new String(
048: "isVisible");
049:
050: /**
051: * Holds the toggleable state.
052: */
053: protected boolean isToggleAction;
054:
055: public void setEnabled(boolean newValue) {
056: super .setEnabled(newValue);
057: }
058:
059: /**
060: * Constructs a new action for the specified name
061: *
062: * @param name
063: * The name of the new action.
064: */
065: public JGraphEditorAction(String name) {
066: this (name, false);
067: }
068:
069: /**
070: * Constructs a new action for the specified <code>name</code> and
071: * <code>isToggleAction</code> state.
072: *
073: * @param name
074: * The name of the new action.
075: * @param isToggleAction
076: * Whether the action is a toggle action.
077: */
078: public JGraphEditorAction(String name, boolean isToggleAction) {
079: super (name);
080: setToggleAction(isToggleAction);
081: }
082:
083: /**
084: * Returns the name.
085: */
086: public String getName() {
087: return String.valueOf(getValue(Action.NAME));
088: }
089:
090: /**
091: * Returns whether the UI elements should display the selection state.
092: *
093: * @return Returns true if the action is toggleable.
094: */
095: public boolean isToggleAction() {
096: return isToggleAction;
097: }
098:
099: /**
100: * Sets whether the UI elements should display the selection state.
101: *
102: * @param isToggleAction
103: * The isToggleAction state to set.
104: */
105: public void setToggleAction(boolean isToggleAction) {
106: this .isToggleAction = isToggleAction;
107: }
108:
109: /**
110: * Returns the selection state.
111: *
112: * @return Returns true if the action is selected.
113: */
114: public boolean isSelected() {
115: Boolean isSelected = (Boolean) getValue(PROPERTY_ISSELECTED);
116: if (isSelected != null)
117: return isSelected.booleanValue();
118: return false;
119: }
120:
121: /**
122: * Sets the selection state. Dispatches a change event.
123: *
124: * @param selected
125: * The selected state to set.
126: *
127: * @see Action#putValue(java.lang.String, java.lang.Object)
128: */
129: public void setSelected(boolean selected) {
130: putValue(PROPERTY_ISSELECTED, new Boolean(selected));
131: }
132:
133: /**
134: * Returns the visible state.
135: *
136: * @return Returns true if the action is visible.
137: */
138: public boolean isVisible() {
139: Boolean isVisible = (Boolean) getValue(PROPERTY_ISVISIBLE);
140: if (isVisible != null)
141: return isVisible.booleanValue();
142: return false;
143: }
144:
145: /**
146: * Sets the visible state. Dispatches a change event.
147: *
148: * @param visible
149: * The visible state to set.
150: *
151: * @see Action#putValue(java.lang.String, java.lang.Object)
152: */
153: public void setVisible(boolean visible) {
154: putValue(PROPERTY_ISVISIBLE, new Boolean(visible));
155: }
156:
157: /**
158: * Shortcut method to {@link JGraphEditorResources#getString(String)}.
159: *
160: * @param key
161: * The key to return the resource string for.
162: */
163: public static String getString(String key) {
164: return JGraphEditorResources.getString(key);
165: }
166:
167: /**
168: * Returns the frame for <code>event</code> if the even source is a
169: * Component or the active frame.
170: *
171: * @param event
172: * The event to get the frame from.
173: * @return Returns the frame for <code>event</code> or the active frame.
174: *
175: * @see #getActiveFrame()
176: */
177: public static Frame getFrame(ActionEvent event) {
178: Window wnd = null;
179: if (event != null && event.getSource() instanceof Component)
180: wnd = SwingUtilities.windowForComponent((Component) event
181: .getSource());
182: Frame frame = (wnd instanceof Frame) ? (Frame) wnd
183: : getActiveFrame();
184: return frame;
185: }
186:
187: /**
188: * Returns the permanent focus owner.
189: *
190: * @return Returns the permanent focus owner.
191: *
192: * @see KeyboardFocusManager#getPermanentFocusOwner()
193: */
194: public static Component getPermanentFocusOwner() {
195: return KeyboardFocusManager.getCurrentKeyboardFocusManager()
196: .getPermanentFocusOwner();
197: }
198:
199: /**
200: * Returns the permanent focus owner or the parent scroll pane of it.
201: *
202: * @return Returns the permanent focus owner or its parent scroll pane.
203: *
204: * @see KeyboardFocusManager#getPermanentFocusOwner()
205: */
206: public static Component getPermanentFocusOwnerOrParent() {
207: Component comp = KeyboardFocusManager
208: .getCurrentKeyboardFocusManager()
209: .getPermanentFocusOwner();
210: Component tmp = JGraphEditorNavigator.getParentScrollPane(comp);
211: if (tmp != null)
212: comp = tmp;
213: return comp;
214: }
215:
216: /**
217: * Returns the permanent focus owner graph.
218: *
219: * @return Returns the permanent focus owner graph.
220: *
221: * @see KeyboardFocusManager#getPermanentFocusOwner()
222: */
223: public static JGraph getPermanentFocusOwnerGraph() {
224: return getParentGraph(KeyboardFocusManager
225: .getCurrentKeyboardFocusManager()
226: .getPermanentFocusOwner());
227: }
228:
229: /**
230: * Returns the diagram for the diagram pane that has the focus.
231: *
232: * @return Returns the focused diagram.
233: */
234: public static JGraphEditorDiagram getPermanentFocusOwnerDiagram() {
235: JGraphEditorDiagramPane diagramPane = getPermanentFocusOwnerDiagramPane();
236: if (diagramPane != null)
237: return diagramPane.getDiagram();
238: return null;
239: }
240:
241: /**
242: * Returns the diagram pane that contains the permanent focus owner.
243: */
244: public static JGraphEditorDiagramPane getPermanentFocusOwnerDiagramPane() {
245: return JGraphEditorDiagramPane
246: .getParentDiagramPane(getPermanentFocusOwner());
247: }
248:
249: /**
250: * Returns the first active frame.
251: *
252: * @return Returns the active frame.
253: *
254: * @see Window#isActive()
255: */
256: public static Frame getActiveFrame() {
257: Frame[] frames = JFrame.getFrames();
258: for (int i = 0; i < frames.length; i++)
259: if (frames[i].isActive())
260: return frames[i];
261: return null;
262: }
263:
264: /**
265: * Returns the JGraphpadPane inside the active frame.
266: *
267: * @return Returns the JGraphpad pane for the given frame.
268: */
269: public static JGraphpadPane getJGraphpadPane() {
270: Frame frame = getActiveFrame();
271:
272: if (frame instanceof JFrame) {
273: return getJGraphpadPane((JFrame) frame);
274: }
275:
276: return null;
277: }
278:
279: /**
280: * Returns the JGraphpadPane inside the given frame.
281: *
282: * @return Returns the JGraphpad pane for the given frame.
283: */
284: public static JGraphpadPane getJGraphpadPane(JFrame frame) {
285: int childCount = frame.getContentPane().getComponentCount();
286:
287: for (int i = 0; i < childCount; i++) {
288: try {
289: Component comp = frame.getContentPane().getComponent(i);
290:
291: if (comp instanceof JGraphpadPane) {
292: return (JGraphpadPane) comp;
293: }
294: } catch (Exception e) {
295: e.printStackTrace();
296: }
297: }
298:
299: return null;
300: }
301:
302: /**
303: * Returns the parent diagram pane of the specified component, or the
304: * component itself if it is a editor diagram pane.
305: *
306: * @return Returns the parent editor diagram pane of <code>component</code>.
307: */
308: public static JGraph getParentGraph(Component component) {
309: while (component != null) {
310: if (component instanceof JGraph)
311: return (JGraph) component;
312: component = component.getParent();
313: }
314: return null;
315: }
316:
317: /**
318: * An interface to manage a set of actions as a single entity.
319: *
320: * @see JGraphEditorKit#addBundle(JGraphEditorAction.Bundle)
321: */
322: public interface Bundle {
323:
324: /**
325: * Returns all actions contained in the bundle.
326: *
327: * @return Returns all actions.
328: */
329: public JGraphEditorAction[] getActions();
330:
331: /**
332: * Updates all action states.
333: */
334: public void update();
335:
336: }
337:
338: }
|