View Javadoc
1 /*** 2 * $Header: /cvsroot/ebxmlrr/jaxr/src/com/sun/xml/registry/client/browser/graph/JBMarqueeHandler.java,v 1.14 2003/08/24 22:14:56 farrukh_najmi Exp $ 3 */ 4 package com.sun.xml.registry.client.browser.graph; 5 6 import com.jgraph.JGraph; 7 import com.jgraph.graph.*; 8 import com.jgraph.event.*; 9 10 import javax.xml.registry.*; 11 import javax.xml.registry.infomodel.*; 12 13 import java.util.*; 14 import java.awt.*; 15 import java.awt.event.*; 16 import javax.swing.*; 17 18 import com.sun.xml.registry.client.browser.RegistryBrowser; 19 20 /*** 21 * MarqueeHandler that Connects Vertices and Displays PopupMenus 22 * 23 * @author <a href="mailto:Farrukh.Najmi@Sun.COM">Farrukh S. Najmi</a> 24 */ 25 public class JBMarqueeHandler extends BasicMarqueeHandler { 26 27 JBGraph graph = null; 28 29 // Holds the Start and the Current Point 30 protected Point start, current; 31 32 // Holds the First and the Current Port 33 protected PortView port, firstPort; 34 35 public JBMarqueeHandler(JBGraph graph) { 36 this.graph = graph; 37 } 38 39 // Override to Gain Control (for PopupMenu and ConnectMode) 40 public boolean isForceMarqueeEvent(MouseEvent e) { 41 // If Right Mouse Button we want to Display the PopupMenu 42 if (SwingUtilities.isRightMouseButton(e)) 43 // Return Immediately 44 return true; 45 // Find and Remember Port 46 port = getSourcePortAt(e.getPoint()); 47 // If Port Found and in ConnectMode (=Ports Visible) 48 if (port != null && graph.isPortsVisible()) 49 return true; 50 // Else Call Superclass 51 return super.isForceMarqueeEvent(e); 52 } 53 54 // Display PopupMenu or Remember Start Location and First Port 55 public void mousePressed(final MouseEvent e) { 56 // If Right Mouse Button 57 if (SwingUtilities.isRightMouseButton(e)) { 58 // Scale From Screen to Model 59 Point loc = graph.fromScreen(e.getPoint()); 60 // Find Cell in Model Coordinates 61 Object cell = graph.getFirstCellForLocation(loc.x, loc.y); 62 63 //If DefaultGraphCell then it must be a Collection attribute 64 //Get the JBGraphCell under it. 65 if ((!(cell instanceof com.sun.xml.registry.client.browser.graph.JBGraphCell)) && 66 (cell instanceof DefaultGraphCell)) { 67 JBGraphCell _cell = graph.getJBGraphCellAt((DefaultGraphCell)cell, loc.x, loc.y); 68 69 if (_cell != null) { 70 cell = _cell; 71 } 72 } 73 74 75 // Create PopupMenu for the Cell 76 JPopupMenu menu = createPopupMenu(e.getPoint(), cell); 77 // Display PopupMenu 78 menu.show(graph, e.getX(), e.getY()); 79 80 // Else if in ConnectMode and Remembered Port is Valid 81 } 82 else if (port != null && !e.isConsumed() && graph.isPortsVisible()) { 83 // Remember Start Location 84 start = graph.toScreen(port.getLocation(null)); 85 // Remember First Port 86 firstPort = port; 87 // Consume Event 88 e.consume(); 89 } 90 else 91 // Call Superclass 92 super.mousePressed(e); 93 } 94 95 // Find Port under Mouse and Repaint Connector 96 public void mouseDragged(MouseEvent e) { 97 // If remembered Start Point is Valid 98 if (start != null && !e.isConsumed()) { 99 // Fetch Graphics from Graph 100 Graphics g = graph.getGraphics(); 101 // Xor-Paint the old Connector (Hide old Connector) 102 paintConnector(Color.black, graph.getBackground(), g); 103 // Reset Remembered Port 104 port = getTargetPortAt(e.getPoint()); 105 // If Port was found then Point to Port Location 106 if (port != null) current = graph.toScreen(port.getLocation(null)); 107 // Else If no Port was found then Point to Mouse Location 108 else current = graph.snap(e.getPoint()); 109 // Xor-Paint the new Connector 110 paintConnector(graph.getBackground(), Color.black, g); 111 // Consume Event 112 e.consume(); 113 } 114 // Call Superclass 115 super.mouseDragged(e); 116 } 117 118 public PortView getSourcePortAt(Point point) { 119 // Scale from Screen to Model 120 Point tmp = graph.fromScreen(new Point(point)); 121 // Find a Port View in Model Coordinates and Remember 122 return graph.getPortViewAt(tmp.x, tmp.y); 123 } 124 125 // Find a Cell at point and Return its first Port as a PortView 126 protected PortView getTargetPortAt(Point point) { 127 // Find Cell at point (No scaling needed here) 128 Object cell = graph.getFirstCellForLocation(point.x, point.y); 129 // Loop Children to find PortView 130 for (int i = 0; i < graph.getModel().getChildCount(cell); i++) { 131 // Get Child from Model 132 Object tmp = graph.getModel().getChild(cell, i); 133 // Get View for Child using the Graph's View as a Cell Mapper 134 tmp = graph.getView().getMapping(tmp, false); 135 // If Child View is a Port View and not equal to First Port 136 if (tmp instanceof PortView && tmp != firstPort) 137 // Return as PortView 138 return (PortView) tmp; 139 } 140 // No Port View found 141 return getSourcePortAt(point); 142 } 143 144 // Connect the First Port and the Current Port in the Graph or Repaint 145 public void mouseReleased(MouseEvent e) { 146 // If Valid Event, Current and First Port 147 if (e != null && !e.isConsumed() && port != null && firstPort != null && 148 firstPort != port) { 149 // Then Establish Connection 150 graph.connect((Port) firstPort.getCell(), (Port) port.getCell()); 151 // Consume Event 152 e.consume(); 153 // Else Repaint the Graph 154 } 155 else graph.repaint(); 156 // Reset Global Vars 157 firstPort = port = null; 158 start = current = null; 159 // Call Superclass 160 super.mouseReleased(e); 161 } 162 163 // Show Special Cursor if Over Port 164 public void mouseMoved(MouseEvent e) { 165 // Check Mode and Find Port 166 if (e != null && getSourcePortAt(e.getPoint()) != null && 167 !e.isConsumed() && graph.isPortsVisible()) { 168 // Set Cusor on Graph (Automatically Reset) 169 graph.setCursor(new Cursor(Cursor.HAND_CURSOR)); 170 // Consume Event 171 e.consume(); 172 } 173 // Call Superclass 174 super.mouseReleased(e); 175 } 176 177 // Use Xor-Mode on Graphics to Paint Connector 178 protected void paintConnector(Color fg, Color bg, Graphics g) { 179 // Set Foreground 180 g.setColor(fg); 181 // Set Xor-Mode Color 182 g.setXORMode(bg); 183 // Highlight the Current Port 184 paintPort(graph.getGraphics()); 185 // If Valid First Port, Start and Current Point 186 if (firstPort != null && start != null && current != null) 187 // Then Draw A Line From Start to Current Point 188 g.drawLine(start.x, start.y, current.x, current.y); 189 } 190 191 // Use the Preview Flag to Draw a Highlighted Port 192 protected void paintPort(Graphics g) { 193 // If Current Port is Valid 194 if (port != null) { 195 // If Not Floating Port... 196 boolean o = (GraphConstants.getOffset(port.getAttributes()) != null); 197 // ...Then use Parent's Bounds 198 Rectangle r = (o) ? port.getBounds() : port.getParentView().getBounds(); 199 // Scale from Model to Screen 200 r = graph.toScreen(new Rectangle(r)); 201 // Add Space For the Highlight Border 202 r.setBounds(r.x-3, r.y-3, r.width+6, r.height+6); 203 // Paint Port in Preview (=Highlight) Mode 204 graph.getUI().paintCell(g, port, r, true); 205 } 206 } 207 208 // 209 // PopupMenu 210 // 211 public JPopupMenu createPopupMenu(final Point pt, final Object cell) { 212 JPopupMenu menu = new JPopupMenu(); 213 boolean editable = graph.isEditable(); 214 215 if (cell != null) { 216 217 if (cell instanceof com.sun.xml.registry.client.browser.graph.JBGraphCell) { 218 219 // Edit 220 String editLabel = null; 221 if (editable) { 222 editLabel = "Edit"; 223 } 224 else { 225 editLabel = "Show Details"; 226 } 227 menu.add(new AbstractAction(editLabel) { 228 public void actionPerformed(ActionEvent e) { 229 graph.editCell((JBGraphCell)cell); 230 } 231 }); 232 233 // Show related objects 234 menu.add(new AbstractAction("Show Related Objects") { 235 public void actionPerformed(ActionEvent e) { 236 graph.showRelatedObjects((JBGraphCell)cell); 237 } 238 }); 239 240 // Browse object in a new GraphPanel 241 menu.add(new AbstractAction("Browse as Graph") { 242 public void actionPerformed(ActionEvent e) { 243 graph.browseObject((JBGraphCell)cell); 244 } 245 }); 246 247 // Show audit trail 248 menu.add(new AbstractAction("Show Audit Trail") { 249 public void actionPerformed(ActionEvent e) { 250 graph.showAuditTrail((JBGraphCell)cell); 251 } 252 }); 253 254 if (((JBGraphCell)cell).getRegistryObject() instanceof com.sun.xml.registry.ebxml.infomodel.ExtrinsicObjectImpl) { 255 // Retrieve 256 menu.add(new AbstractAction("Retrieve Item") { 257 public void actionPerformed(ActionEvent e) { 258 graph.retrieveItem((JBGraphCell)cell); 259 } 260 }); 261 } 262 } 263 } 264 265 // Remove 266 if (!graph.isSelectionEmpty()) { 267 menu.addSeparator(); 268 269 if (editable) { 270 menu.add(new AbstractAction("Remove") { 271 public void actionPerformed(ActionEvent e) { 272 graph.remove.actionPerformed(e); 273 } 274 }); 275 } 276 277 if (graph.getSelectionCount() > 1) { 278 // ReLayout 279 menu.add(new AbstractAction("ReLayout") { 280 public void actionPerformed(ActionEvent e) { 281 JBGraph.circleLayout(graph); 282 } 283 }); 284 } 285 } 286 menu.addSeparator(); 287 288 if (editable) { 289 // Insert 290 menu.add(new AbstractAction("Insert") { 291 public void actionPerformed(ActionEvent ev) { 292 graph.insert(pt); 293 } 294 }); 295 296 // Save 297 menu.add(new AbstractAction("Save") { 298 public void actionPerformed(ActionEvent ev) { 299 graph.save(); 300 } 301 }); 302 } 303 304 // Export 305 menu.add(new AbstractAction("Export") { 306 public void actionPerformed(ActionEvent ev) { 307 graph.export(); 308 } 309 }); 310 return menu; 311 } 312 313 }

This page was automatically generated by Maven