001: /*
002: @license.gpl.text@
003: */
004:
005: /*
006: * This code is derived from
007: * JGraphPad, JGraphpadPane.java by Gaudenz Alder
008: */
009: package biz.hammurapi.web.diagrameditor;
010:
011: import java.awt.BorderLayout;
012: import java.beans.PropertyChangeEvent;
013: import java.beans.PropertyChangeListener;
014: import java.lang.reflect.Method;
015: import java.util.Hashtable;
016: import java.util.Iterator;
017: import java.util.Map;
018:
019: import javax.swing.ImageIcon;
020: import javax.swing.JPanel;
021: import javax.swing.SwingUtilities;
022:
023: import org.jgraph.JGraph;
024:
025: import com.jgraph.JGraphEditor;
026: import com.jgraph.editor.JGraphEditorDiagram;
027: import com.jgraph.editor.factory.JGraphEditorDiagramPane;
028: import com.jgraph.pad.util.JGraphpadImageIcon;
029: import com.jgraph.pad.util.JGraphpadMouseAdapter;
030:
031: /**
032: * Main application panel consisting of a menubar and toolbar, two tabbed panes,
033: * one on the left and one on the bottom, a desktop pane in the center, and a
034: * status bar.
035: */
036: public class DiagramEditorAppletPane extends JPanel {
037:
038: /**
039: * Nodename to be used for the desktop popup menu configuration.
040: */
041: public static String NODENAME_DESKTOPPOPUPMENU = "desktoppopupmenu";
042:
043: /**
044: * Nodename to be used for the desktop popup menu configuration.
045: */
046: public static String NODENAME_INTERNALFRAMEPOPUPMENU = "internalframepopupmenu";
047:
048: /**
049: * Defines the key used to identify the navigator split divider location.
050: */
051: public static String KEY_DESKTOPPANE = "desktopPane";
052:
053: /**
054: * Holds the desktop pane.
055: */
056: //protected JDesktopPane desktopPane = new JDesktopPane();
057: private JGraphEditorDiagramPane diagramPane;
058:
059: JGraph getGraph() {
060: return diagramPane.getGraph();
061: }
062:
063: /**
064: * Maps from diagrams to internal frames.
065: */
066: //protected Map internalFrames = new HashMap();
067: /**
068: * References the enclosing editor.
069: */
070: protected JGraphEditor editor;
071:
072: /**
073: * Constructs a new editor pane for the specified enclosing editor.
074: *
075: * @param editor
076: * The editor that contains the pane.
077: * @param newDiagram
078: */
079: public DiagramEditorAppletPane(JGraphEditor editor,
080: JGraphEditorDiagram newDiagram) {
081: setLayout(new BorderLayout());
082: this .editor = editor;
083: diagramPane = editor.getFactory().createDiagramPane(newDiagram);
084: configureDiagramPane(diagramPane, newDiagram);
085: diagramPane.addMouseListener(new JGraphpadMouseAdapter(editor,
086: NODENAME_DESKTOPPOPUPMENU));
087: editor.getSettings().putObject(KEY_DESKTOPPANE, diagramPane);
088: add(diagramPane, BorderLayout.CENTER);
089: SwingUtilities.invokeLater(new Runnable() {
090: public void run() {
091: diagramPane.getGraph().requestFocus();
092: }
093: });
094: }
095:
096: /**
097: * Configures the newly created diagram pane to reflect the properties of
098: * the specified diagram. This also installs a listener to keep the
099: * properties of the diagram up-to-date with the graph for the next creation
100: * after persistence.
101: *
102: * @param diagramPane
103: * The diagram pane to be configured.
104: * @param diagram
105: * The diagram to be configured.
106: */
107: protected void configureDiagramPane(
108: JGraphEditorDiagramPane diagramPane,
109: final JGraphEditorDiagram diagram) {
110:
111: // Listens to JGraph properties
112: diagramPane.getGraph().addPropertyChangeListener(
113: new PropertyChangeListener() {
114:
115: /*
116: * (non-Javadoc)
117: */
118: public void propertyChange(PropertyChangeEvent event) {
119:
120: // Checks if it's an interesting property and stores
121: // the interesting ones back in the diagram.
122: String name = event.getPropertyName();
123: if (name.equals(JGraph.ANTIALIASED_PROPERTY)
124: || name
125: .equals(JGraph.EDITABLE_PROPERTY)
126: || name
127: .equals(JGraph.GRID_COLOR_PROPERTY)
128: || name
129: .equals(JGraph.GRID_SIZE_PROPERTY)
130: || name
131: .equals(JGraph.GRID_VISIBLE_PROPERTY)
132: || name
133: .equals(JGraph.HANDLE_COLOR_PROPERTY)
134: || name
135: .equals(JGraph.HANDLE_SIZE_PROPERTY)
136: || name
137: .equals(JGraph.LOCKED_HANDLE_COLOR_PROPERTY)
138: || name
139: .equals(JGraph.PORTS_VISIBLE_PROPERTY)
140: || name
141: .equals(JGraph.PORTS_SCALED_PROPERTY)
142: || name.equals(JGraph.SCALE_PROPERTY))
143: if (event.getNewValue() == null) {
144: diagram.getProperties().remove(
145: "graph." + name);
146: } else {
147: diagram.getProperties().put(
148: "graph." + name,
149: event.getNewValue());
150: }
151: }
152: });
153:
154: // Listens to diagram pane properties
155: diagramPane
156: .addPropertyChangeListener(new PropertyChangeListener() {
157:
158: /*
159: * (non-Javadoc)
160: */
161: public void propertyChange(PropertyChangeEvent event) {
162:
163: // Checks if it's an interesting property and stores
164: // the interesting ones back in the diagram.
165: String name = event.getPropertyName();
166: if (name
167: .equals(JGraphEditorDiagramPane.PROPERTY_AUTOSCALEPOLICY)
168: || name
169: .equals(JGraphEditorDiagramPane.PROPERTY_BACKGROUNDIMAGE)
170: || name
171: .equals(JGraphEditorDiagramPane.PROPERTY_METRIC)
172: || name
173: .equals(JGraphEditorDiagramPane.PROPERTY_PAGEFORMAT)
174: || name
175: .equals(JGraphEditorDiagramPane.PROPERTY_PAGESCALE)
176: || name
177: .equals(JGraphEditorDiagramPane.PROPERTY_PAGEVISIBLE)
178: || name
179: .equals(JGraphEditorDiagramPane.PROPERTY_RULERSVISIBLE))
180: if (event.getNewValue() == null) {
181: diagram.getProperties().remove(
182: "diagramPane." + name);
183: } else {
184: diagram.getProperties().put(
185: "diagramPane." + name,
186: event.getNewValue());
187: }
188: }
189: });
190:
191: // Tries to set all current properties from the diagram to the graph.
192: // Note: A hashtable contains the object to resolve the prefixes.
193: Map objects = new Hashtable();
194: objects.put("diagramPane", diagramPane);
195: objects.put("graph", diagramPane.getGraph());
196:
197: Iterator it = diagram.getProperties().entrySet().iterator();
198: while (it.hasNext()) {
199: Map.Entry entry = (Map.Entry) it.next();
200: setProperty(objects, String.valueOf(entry.getKey()), entry
201: .getValue());
202: }
203: }
204:
205: /**
206: * Utility method to set the property of an object using reflection.
207: *
208: * @param objects
209: * Maps from prefixes to objects.
210: * @param property
211: * The name of the property to be changed.
212: * @param value
213: * The value of the property to be set.
214: */
215: public static void setProperty(Map objects, String property,
216: Object value) {
217: // Analyze prefix
218: int delim = property.indexOf('.');
219: if (delim > 0) {
220: String prefix = property.substring(0, delim);
221: property = property.substring(delim + 1);
222: Object obj = objects.get(prefix);
223: if (obj != null) {
224: // Does type conversion to basic types
225: Class clazz = value.getClass();
226: if (clazz == Boolean.class)
227: clazz = boolean.class;
228: else if (clazz == Integer.class)
229: clazz = int.class;
230: else if (clazz == Long.class)
231: clazz = long.class;
232: else if (clazz == Float.class)
233: clazz = float.class;
234: else if (clazz == Double.class)
235: clazz = double.class;
236: else if (clazz == JGraphpadImageIcon.class)
237: clazz = ImageIcon.class;
238: String name = String.valueOf(property);
239: name = name.substring(0, 1).toUpperCase()
240: + name.substring(1);
241: try {
242: Method setter = obj.getClass().getMethod(
243: "set" + name, new Class[] { clazz });
244: setter.invoke(obj, new Object[] { value });
245: } catch (Exception e) {
246: // ignore
247: }
248: }
249: }
250: }
251: }
|