1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
|
/*
* @(#)EditorGraph.java 3.3 23-APR-04
*
* Copyright (c) 2001-2005, Gaudenz Alder
*
* See LICENSE file in distribution for licensing details of this source file
*/
package com.jgraph.example;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.event.CellEditorListener;
import org.jgraph.JGraph;
import org.jgraph.graph.CellView;
import org.jgraph.graph.GraphCellEditor;
import org.jgraph.graph.GraphModel;
import org.jgraph.plaf.basic.BasicGraphUI;
/**
* An example that demonstrates how to use a JDialog
* as a CellEditor in JGraph.
*
* @version 1.1 23/12/02
* @author Gaudenz Alder
*/
public class EditorGraph extends JGraph {
/**
* Constructs a EditorGraph with a sample model.
*/
public EditorGraph() {
setInvokesStopCellEditing(true);
}
/**
* Constructs a EditorGraph for <code>model</code>.
*/
public EditorGraph(GraphModel model) {
super(model);
}
/**
* Override parent method with custom GraphUI.
*/
public void updateUI() {
setUI(new EditorGraphUI());
invalidate();
}
public static void main(String[] args) {
// Switch off D3D because of Sun XOR painting bug
// See http://www.jgraph.com/forum/viewtopic.php?t=4066
System.setProperty("sun.java2d.d3d", "false");
JFrame frame = new JFrame("EditorGraph");
frame.getContentPane().add(new EditorGraph());
frame.pack();
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
/**
* Definition of the custom GraphUI.
*/
public class EditorGraphUI extends BasicGraphUI {
protected CellEditorListener cellEditorListener;
protected JFrame editDialog = null;
/**
* Create the dialog using the cell's editing component.
*/
protected void createEditDialog(Object cell) {
Dimension editorSize = editingComponent.getPreferredSize();
editDialog = new JFrame("Edit " + graph.convertValueToString(cell));
editDialog.setSize(editorSize.width, editorSize.height);
editDialog.getContentPane().add(editingComponent);
editingComponent.validate();
editDialog.pack();
editDialog.setVisible(true);
// Invokes complete editing when the window is closing
editDialog.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
completeEditing();
}
});
}
/**
* Stops the editing session. If messageStop is true the editor
* is messaged with stopEditing, if messageCancel is true the
* editor is messaged with cancelEditing. If messageGraph is true
* the graphModel is messaged with valueForCellChanged.
*/
protected void completeEditing(
boolean messageStop,
boolean messageCancel,
boolean messageGraph) {
if (stopEditingInCompleteEditing
&& editingComponent != null
&& editDialog != null) {
Object oldCell = editingCell;
GraphCellEditor oldEditor = cellEditor;
Object newValue = oldEditor.getCellEditorValue();
boolean requestFocus =
(graph != null
&& (graph.hasFocus() || editingComponent.hasFocus()));
editingCell = null;
editingComponent = null;
if (messageStop)
oldEditor.stopCellEditing();
else if (messageCancel)
oldEditor.cancelCellEditing();
editDialog.dispose();
if (requestFocus)
graph.requestFocus();
if (messageGraph) {
graphLayoutCache.valueForCellChanged(oldCell, newValue);
}
updateSize();
// Remove Editor Listener
if (oldEditor != null && cellEditorListener != null)
oldEditor.removeCellEditorListener(cellEditorListener);
cellEditor = null;
editDialog = null;
}
}
/**
* Will start editing for cell if there is a cellEditor and
* shouldSelectCell returns true.<p>
* This assumes that cell is valid and visible.
*/
protected boolean startEditing(Object cell, MouseEvent event) {
completeEditing();
if (graph.isCellEditable(cell) && editDialog == null) {
// Create Editing Component **** *****
CellView tmp = graphLayoutCache.getMapping(cell, false);
cellEditor = tmp.getEditor();
editingComponent =
cellEditor.getGraphCellEditorComponent(
graph,
cell,
graph.isCellSelected(cell));
if (cellEditor.isCellEditable(event)) {
editingCell = cell;
// Create Wrapper Dialog **** *****
createEditDialog(cell);
// Add Editor Listener
if (cellEditorListener == null)
cellEditorListener = createCellEditorListener();
if (cellEditor != null && cellEditorListener != null)
cellEditor.addCellEditorListener(cellEditorListener);
if (cellEditor.shouldSelectCell(event)) {
stopEditingInCompleteEditing = false;
try {
graph.setSelectionCell(cell);
} catch (Exception e) {
System.err.println("Editing exception: " + e);
}
stopEditingInCompleteEditing = true;
}
if (event instanceof MouseEvent) {
/* Find the component that will get forwarded all the
mouse events until mouseReleased. */
Point componentPoint =
SwingUtilities.convertPoint(
graph,
new Point(event.getX(), event.getY()),
editingComponent);
/* Create an instance of BasicTreeMouseListener to handle
passing the mouse/motion events to the necessary
component. */
// We really want similiar behavior to getMouseEventTarget,
// but it is package private.
Component activeComponent =
SwingUtilities.getDeepestComponentAt(
editingComponent,
componentPoint.x,
componentPoint.y);
if (activeComponent != null) {
new MouseInputHandler(
graph,
activeComponent,
event);
}
}
return true;
} else
editingComponent = null;
}
return false;
}
}
}
|