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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
/*
* $Id: JGraphSQLQueryPane.java,v 1.2 2007/08/18 10:20:11 david Exp $
*
* Copyright (c) 2001-2005, Gaudenz Alder
*
* See LICENSE file in distribution for licensing details of this source file
*/
package com.jgraph.example.adapter;
import java.awt.BorderLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import org.jgraph.JGraph;
import org.jgraph.graph.ConnectionSet;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.GraphConstants;
import org.jgraph.graph.ParentMap;
/**
* A quick and dirty implementation of a query pane. This allows to enter
* keywords to search for property values of entities. Results may then be
* dragged from the result list to the diagram. If the cells are already in the
* diagram then they should be moved and selected, otherwise a new cell should
* be created and inserted into the graph model.
*/
public class JGraphSQLQueryPane extends JPanel {
public JGraphSQLQueryPane(final JGraphAdapterExample adapter,
final JGraph graph) {
super(new BorderLayout());
final JGraphSQLBusinessModel businessModel = (JGraphSQLBusinessModel) graph
.getModel();
final JTextField input = new JTextField("[Type Query + Press Enter]");
add(input, BorderLayout.NORTH);
final DefaultTreeModel treeModel = new DefaultTreeModel(
new DefaultMutableTreeNode("root"));
final JTree tree = new JTree(treeModel);
tree.setRootVisible(false);
tree.setAutoscrolls(true);
tree.setShowsRootHandles(true);
tree.setEditable(false);
add(new JScrollPane(tree), BorderLayout.CENTER);
// Update the result set on ENTER
input.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
String query = input.getText();
if (businessModel != null) {
// TODO: Move into a method
try {
if (query.startsWith(":")) {
query = query.substring(1);
((JGraphSQLBackend) businessModel.getBackend())
.query(query);
} else if (query.startsWith(">")) {
query = query.substring(1);
((JGraphSQLBackend) businessModel.getBackend())
.update(query);
} else {
DefaultMutableTreeNode root = new DefaultMutableTreeNode(
"root");
treeModel.setRoot(root);
Iterator it = businessModel.findVertices(query,
null).iterator();
while (it.hasNext()) {
Object source = it.next();
DefaultMutableTreeNode node = new DefaultMutableTreeNode(
source);
treeModel.insertNodeInto(node, root,
treeModel.getChildCount(root));
Iterator it2 = businessModel.findEdges("",
null, source, null, false)
.iterator();
while (it2.hasNext()) {
// Create the tree node for the edge
treeModel.insertNodeInto(
new DefaultMutableTreeNode(it2
.next()), node,
treeModel.getChildCount(node));
}
}
tree.expandPath(new TreePath(root));
}
} catch (Exception e1) {
e1.printStackTrace();
}
} else {
println("No backend found.");
}
}
}
});
// React to double clicks
tree.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (tree.isSelectionEmpty())
return;
// Result objects are wrapped in treenodes
DefaultGraphCell cell = (DefaultGraphCell) businessModel
.getValue(tree.getSelectionPath()
.getLastPathComponent());
if (cell != null) {
if (e.getClickCount() == 2) {
if (graph != null
&& graph.getModel() instanceof JGraphAdapterModel) {
JGraphAdapterModel bm = (JGraphAdapterModel) graph
.getModel();
// Check if already in model
Object modelCell = bm.getMapping(cell
.getUserObject());
if (modelCell == null) {
// TODO: Move into a method
Map nested = new Hashtable();
println("Inserting " + cell.getClass());
List newCells = new ArrayList(3);
newCells.add(cell);
// TODO: Parent is only used if already in
// model.
ParentMap pm = new ParentMap();
Object parent = ((JGraphSQLBackend) bm
.getBackend()).getParent(cell
.getUserObject());
if (parent != null) {
Object parentCell = bm.getMapping(parent);
if (parentCell != null)
pm.addEntry(cell, parentCell);
}
// Source and target are used or inserted.
ConnectionSet cs = new ConnectionSet();
if (bm.isEdge(cell)) {
// Create source vertex if required
Object source = (((JGraphSQLBackend) bm
.getBackend()).getSource(cell
.getUserObject()));
if (source != null) {
Object sourceCell = bm
.getMapping(source);
if (sourceCell == null
&& source instanceof JGraphSQLEntity) {
try {
sourceCell = ((JGraphSQLBackend) bm
.getBackend())
.createCell(
bm,
(JGraphSQLEntity) source);
nested
.put(
sourceCell,
adapter
.createCellAttributes(new Point2D.Double(
10,
10)));
} catch (SQLException e1) {
// TODO Auto-generated catch
// block
e1.printStackTrace();
}
newCells.add(sourceCell);
}
cs.connect(cell, bm.getChild(
sourceCell, 0), true);
}
// Create target vertex if required
Object target = (((JGraphSQLBackend) bm
.getBackend()).getTarget(cell
.getUserObject()));
if (target != null) {
Object targetCell = bm
.getMapping(target);
if (targetCell == null
&& target instanceof JGraphSQLEntity) {
try {
targetCell = ((JGraphSQLBackend) bm
.getBackend())
.createCell(
bm,
(JGraphSQLEntity) target);
nested
.put(
targetCell,
adapter
.createCellAttributes(new Point2D.Double(
100,
100)));
} catch (SQLException e1) {
// TODO Auto-generated catch
// block
e1.printStackTrace();
}
newCells.add(targetCell);
}
cs.connect(cell, bm.getChild(
targetCell, 0), false);
}
nested.put(cell, adapter
.createEdgeAttributes());
} else {
nested
.put(
cell,
adapter
.createCellAttributes(new Point2D.Double(
10, 10)));
}
// TODO: For all new cells
GraphConstants.setResize(cell.getAttributes(),
true);
println("nested: " + nested);
// Inserts the double clicked node into the
// model
graph.getGraphLayoutCache().insert(
newCells.toArray(), nested, cs, pm);
} else {
// TODO: Move into a method
println("Updating userobject");
// TODO: Maybe add tcn to entity?
JGraphBusinessObject bo = (JGraphBusinessObject) businessModel
.getValue(modelCell);
JGraphBusinessObject other = (JGraphBusinessObject) cell
.getUserObject();
if (bo != other) {
bo.getProperties().clear();
bo.getProperties().putAll(
other.getProperties());
bm.cellsChanged(new Object[] { cell });
}
if (graph.getGraphLayoutCache().getMapping(
modelCell, false) == null) {
graph.getGraphLayoutCache().setVisible(
new Object[] { modelCell }, null);
} else {
ParentMap pm = new ParentMap();
Object parent = bm
.getMapping(((JGraphSQLBackend) bm
.getBackend())
.getParent(cell
.getUserObject()));
if (parent != null) {
pm.addEntry(modelCell, parent);
graph.getGraphLayoutCache().edit(null,
null, pm, null);
}
// TODO: Update source and target?
}
graph.setSelectionCell(modelCell);
}
}
} else if (cell.getUserObject() instanceof JGraphBusinessObject
&& SwingUtilities.isRightMouseButton(e)) { // eg. right
// click
println("Properties: "
+ ((JGraphBusinessObject) cell.getUserObject())
.getProperties());
}
}
}
});
}
protected static void println(String msg) {
JGraphAdapterExample.println(msg);
}
}
|