001: package com.jgraph.pad.graph;
002:
003: import java.awt.Component;
004: import java.awt.Dimension;
005: import java.awt.event.ActionEvent;
006: import java.awt.event.KeyEvent;
007: import java.awt.geom.Rectangle2D;
008: import java.util.EventObject;
009:
010: import javax.swing.AbstractAction;
011: import javax.swing.AbstractCellEditor;
012: import javax.swing.JComponent;
013: import javax.swing.JTree;
014: import javax.swing.KeyStroke;
015:
016: import org.jgraph.JGraph;
017: import org.jgraph.graph.DefaultGraphCellEditor;
018: import org.jgraph.graph.GraphCellEditor;
019:
020: /**
021: * In-place editor for rich text multiline values.
022: */
023: public class JGraphpadRedirectingEditor extends DefaultGraphCellEditor {
024:
025: /**
026: * Constructs a new rich text editor.
027: */
028: public JGraphpadRedirectingEditor() {
029: super ();
030: }
031:
032: /**
033: * Utlitiy editor for rich text values.
034: */
035: class RealCellEditor extends AbstractCellEditor implements
036: GraphCellEditor {
037:
038: /**
039: * Holds the component value if one exists.
040: */
041: Component componentValue = null;
042:
043: /**
044: * Sets of the editor for editing the rich text value.
045: */
046: public Component getGraphCellEditorComponent(JGraph graph,
047: Object value, boolean isSelected) {
048: Rectangle2D cellBounds = graph.getCellBounds(value);
049: value = graph.getModel().getValue(value);
050: if (value instanceof JGraphpadBusinessObject
051: && ((JGraphpadBusinessObject) value).isComponent()) {
052:
053: // Clones the heavyweight to avoid repaint problems on Windows
054: value = ((JGraphpadBusinessObject) value).clone();
055:
056: Dimension maxSize = new Dimension((int) cellBounds
057: .getWidth() - 6, (int) cellBounds.getHeight());
058: componentValue = (Component) ((JGraphpadBusinessObject) value)
059: .getValue();
060: componentValue.invalidate();
061: if (componentValue instanceof JComponent) {
062: JComponent c = (JComponent) componentValue;
063: c.setMaximumSize(maxSize);
064: c.setPreferredSize(maxSize);
065: c.setMinimumSize(maxSize);
066: }
067: if (componentValue instanceof JTree) {
068: JTree tree = (JTree) componentValue;
069: tree.setSelectionInterval(0, 0);
070: }
071:
072: // Stops cell editing when enter is pressed
073: if (componentValue instanceof JComponent) {
074: JComponent comp = (JComponent) componentValue;
075: comp.getInputMap(JComponent.WHEN_FOCUSED)
076: .put(
077: KeyStroke.getKeyStroke(
078: KeyEvent.VK_ENTER, 0),
079: "enter");
080: comp
081: .getInputMap(
082: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
083: .put(
084: KeyStroke.getKeyStroke(
085: KeyEvent.VK_ENTER, 0),
086: "enter");
087: comp.getActionMap().put("enter",
088: new AbstractAction() {
089:
090: public void actionPerformed(
091: ActionEvent e) {
092: stopCellEditing();
093: }
094: });
095: }
096: return componentValue;
097: }
098: return null;
099: }
100:
101: /**
102: * Returns the rich text value to be stored in the user object.
103: */
104: public Object getCellEditorValue() {
105: return componentValue;
106: }
107:
108: /**
109: * If the realEditor will allow editing to stop, the realEditor is
110: * removed and true is returned, otherwise false is returned.
111: */
112: public boolean stopCellEditing() {
113: if (componentValue instanceof JTree)
114: ((JTree) componentValue).clearSelection();
115: return super .stopCellEditing();
116: }
117:
118: /**
119: * Messages cancelCellEditing to the realEditor and removes it from
120: * this instance.
121: */
122: public void cancelCellEditing() {
123: if (componentValue instanceof JTree)
124: ((JTree) componentValue).clearSelection();
125: super .cancelCellEditing();
126: }
127:
128: /**
129: * Transfers the focus to the editing component.
130: */
131: public boolean shouldSelectCell(EventObject event) {
132: componentValue.requestFocus();
133: return super .shouldSelectCell(event);
134: }
135: }
136:
137: /**
138: * Returns a new RealCellEditor.
139: */
140: protected GraphCellEditor createGraphCellEditor() {
141: return new JGraphpadRedirectingEditor.RealCellEditor();
142: }
143:
144: }
|