View Javadoc

1   /*
2    * ToolVertexEditor.java
3    *
4    * Created on 24. august 2005, 00:21
5    */
6   
7   package com.vikash.firsttool.UI;
8   import org.jgraph.graph.*;
9   import org.jgraph.JGraph;
10  import javax.swing.*;
11  import java.awt.event.*;
12  import javax.swing.text.*;
13  import java.awt.*;
14  import java.awt.geom.*;
15  import java.util.*;
16  
17  /***
18   *
19   * @author  studajb
20   */
21  public class ToolVertexEditor extends DefaultGraphCellEditor {
22      public ToolVertexEditor() {
23          super();
24      }
25      /***
26       * Overriding this in order to set the size of an editor to that of an edited view.
27       */
28      public Component getGraphCellEditorComponent(
29      JGraph graph,
30      Object cell,
31      boolean isSelected) {
32          
33          Component component = super.getGraphCellEditorComponent(graph, cell, isSelected);
34          
35          //set the size of an editor to that of a view
36          CellView view = graph.getGraphLayoutCache().getMapping(cell, false);
37          Rectangle2D tmp = view.getBounds();
38          editingComponent.setBounds((int) tmp.getX(), (int) tmp.getY(),(int) tmp.getWidth(), (int) tmp.getHeight());
39          
40          //I have to set a font here instead of in the RealCellEditor.getGraphCellEditorComponent() because
41          //I don't know what cell is being edited when in the RealCellEditor.getGraphCellEditorComponent().
42          Font font = GraphConstants.getFont(view.getAllAttributes());
43          editingComponent.setFont((font != null) ? font : graph.getFont());
44          
45          return component;
46      }
47      
48      protected GraphCellEditor createGraphCellEditor() {
49          return new ToolVertexEditor.RealCellEditor();
50      }
51      
52      /***
53       * Overriting this so that I could modify an eiditor container.
54       * see http://sourceforge.net/forum/forum.php?thread_id=781479&forum_id=140880
55       */
56      protected Container createContainer() {
57          return new ToolVertexEditor.ModifiedEditorContainer();
58      }
59      public class RealCellEditor extends AbstractCellEditor implements GraphCellEditor {
60          JTextArea editorComponent = new JTextArea();
61          public RealCellEditor() {
62              editorComponent.setBorder(UIManager.getBorder("Tree.editorBorder"));
63              editorComponent.setLineWrap(true);
64              editorComponent.setWrapStyleWord(true);
65              
66              
67              //stop cell editing when ENTER key is pressed
68              editorComponent.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "enter");
69              editorComponent.getActionMap().put("enter", new AbstractAction(){
70                  public void actionPerformed(ActionEvent e) {
71                      stopCellEditing();
72                  }
73              });
74              
75              AbstractAction newLineAction = new AbstractAction() {
76                  
77                  public void actionPerformed(ActionEvent e) {
78                      Document doc = editorComponent.getDocument();
79                      try {
80                          doc.insertString(editorComponent
81                          .getCaretPosition(), " \n", null);
82                      } catch (BadLocationException e1) {
83                          e1.printStackTrace();
84                      }
85                  }
86              };
87              //new line when ctrl enter is pressed
88              editorComponent.getInputMap(JComponent.WHEN_FOCUSED).put(
89              KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,
90              KeyEvent.CTRL_DOWN_MASK), "ctrlEnter");
91              editorComponent.getActionMap().put("ctrlEnter",
92              newLineAction);
93              
94              
95          }
96          
97          
98          
99          public Component getGraphCellEditorComponent(
100         JGraph graph,
101         Object value,
102         boolean isSelected) {
103             editorComponent.setText(value.toString());
104             editorComponent.selectAll();
105             return editorComponent;
106         }
107         
108         public Object getCellEditorValue() {
109             return editorComponent.getText();
110         }
111         
112         public boolean stopCellEditing() {
113             //set the size of a vertex to that of an editor.
114             CellView view = graph.getGraphLayoutCache().getMapping(graph.getEditingCell(), false);
115             Map map = view.getAllAttributes();
116             Rectangle2D cellBounds = GraphConstants.getBounds(map);
117             Rectangle editingBounds = editorComponent.getBounds();
118             GraphConstants.setBounds(map, new Rectangle((int) cellBounds.getX(), (int) cellBounds.getY(), editingBounds.width, editingBounds.height));
119             
120             return super.stopCellEditing();
121         }
122         
123         public boolean shouldSelectCell(EventObject event) {
124             editorComponent.requestFocus();
125             return super.shouldSelectCell(event);
126         }
127     }
128     
129     
130     
131     
132     class ModifiedEditorContainer extends EditorContainer {
133         public void doLayout() {
134             super.doLayout();
135             //substract 2 pixels that were added to the preferred size of the container for the border.
136             Dimension cSize = getSize();
137             Dimension dim = editingComponent.getSize();
138             editingComponent.setSize(dim.width - 2, dim.height);
139             
140             //reset container's size based on a potentially new preferred size of a real editor.
141             setSize(cSize.width, getPreferredSize().height);
142         }
143     }
144 }