[graphl-cvs] graphl/src/org/mediavirus/graphl GraphlApplication.java GraphlPane.java GraphlPanel.jav
Status: Pre-Alpha
Brought to you by:
flo1
From: Flo L. <fl...@us...> - 2004-08-21 22:31:16
|
Update of /cvsroot/graphl/graphl/src/org/mediavirus/graphl In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv32158/src/org/mediavirus/graphl Modified Files: GraphlApplication.java GraphlPane.java GraphlPanel.java Log Message: * added martin klinke's contributions (File>New and mousewheel zooming) * implemented facet configuration from rdf config file * added configuration data to config.rdf * removed historic JGraphPane, merged with GraphlPane * factored out vocabulary registry mechanism from GraphlPane Index: GraphlPanel.java =================================================================== RCS file: /cvsroot/graphl/graphl/src/org/mediavirus/graphl/GraphlPanel.java,v retrieving revision 1.11 retrieving revision 1.12 diff -C2 -d -r1.11 -r1.12 *** GraphlPanel.java 20 Aug 2004 12:38:52 -0000 1.11 --- GraphlPanel.java 21 Aug 2004 22:31:02 -0000 1.12 *************** *** 13,19 **** import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.Collection; import java.util.Collections; - import java.util.Enumeration; import java.util.Iterator; import java.util.Vector; --- 13,20 ---- import java.awt.event.KeyEvent; import java.awt.event.KeyListener; + import java.awt.event.MouseWheelEvent; + import java.awt.event.MouseWheelListener; import java.util.Collection; import java.util.Collections; import java.util.Iterator; import java.util.Vector; *************** *** 32,36 **** import org.mediavirus.graphl.selection.SelectionListener; import org.mediavirus.graphl.selection.SelectionModel; - import org.mediavirus.graphl.vocabulary.Vocabulary; import javax.swing.JLabel; --- 33,36 ---- *************** *** 44,47 **** --- 44,49 ---- public class GraphlPanel extends JPanel implements GraphListener, KeyListener, SelectionListener { + private static final int WHEEL_ZOOM_FACTOR = 3; + GraphlPane graphPane; GraphlManipulator gui; *************** *** 66,70 **** initialize(); ! layouter = new Layouter(new GraphlLayoutStrategy(graphPane.getGraph(), graphPane.getPainterRegistry())); layouter.start(); } --- 68,72 ---- initialize(); ! layouter = new Layouter(new GraphlLayoutStrategy(graphPane.getGraph(), graphPane.getFacetRegistry())); layouter.start(); } *************** *** 240,245 **** graphPane.getGraph().addGraphListener(this); graphPane.addKeyListener(this); } - return graphPane; } --- 242,267 ---- graphPane.getGraph().addGraphListener(this); graphPane.addKeyListener(this); + graphPane.addMouseWheelListener(new MouseWheelListener () { + public void mouseWheelMoved(MouseWheelEvent e) { + //invert the rotation to create standard scroll-zoom + //behaviour (scroll up=zoom in, scroll down=zoom out) + int wheelRotation = e.getWheelRotation() * -1; + int sliderValue = getZoomSlider().getValue(); + //ignore scrolling in invariant directions at extreme values + if ((wheelRotation < 0 && getZoomSlider().getValue() == getZoomSlider().getMinimum()) + ||(wheelRotation > 0 && getZoomSlider().getValue() == getZoomSlider().getMaximum())) { + return; + } + sliderValue += wheelRotation * e.getScrollAmount() * WHEEL_ZOOM_FACTOR; + if(sliderValue < getZoomSlider().getMinimum()) { + sliderValue = getZoomSlider().getMinimum(); + } + if(sliderValue > getZoomSlider().getMaximum()) { + sliderValue = getZoomSlider().getMaximum(); + } + getZoomSlider().setValue(sliderValue); + } + }); } return graphPane; } *************** *** 252,256 **** nodePainterMenu = new JComboBox(); nodePainterMenu.setFont(new Font(null, Font.PLAIN, 9)); ! Vector painters = graphPane.getPainterRegistry().getAvailableNodePainters(); for (Iterator iter = painters.iterator(); iter.hasNext();) { NodePainter painter = (NodePainter) iter.next(); --- 274,278 ---- nodePainterMenu = new JComboBox(); nodePainterMenu.setFont(new Font(null, Font.PLAIN, 9)); ! Vector painters = graphPane.getFacetRegistry().getAvailableNodePainters(); for (Iterator iter = painters.iterator(); iter.hasNext();) { NodePainter painter = (NodePainter) iter.next(); *************** *** 260,264 **** public void actionPerformed(ActionEvent e) { NodePainter p = (NodePainter)nodePainterMenu.getSelectedItem(); ! graphPane.getPainterRegistry().setDefaultNodePainter((NodePainter)p.clone()); getNodePainterConfigureButton().setEnabled(p.hasVisualController()); graphPane.repaint(); --- 282,286 ---- public void actionPerformed(ActionEvent e) { NodePainter p = (NodePainter)nodePainterMenu.getSelectedItem(); ! graphPane.getFacetRegistry().setDefaultNodePainter((NodePainter)p.clone()); getNodePainterConfigureButton().setEnabled(p.hasVisualController()); graphPane.repaint(); *************** *** 277,281 **** edgePainterMenu = new JComboBox(); edgePainterMenu.setFont(new Font(null, Font.PLAIN, 9)); ! Vector painters = graphPane.getPainterRegistry().getAvailableEdgePainters(); for (Iterator iter = painters.iterator(); iter.hasNext();) { EdgePainter painter = (EdgePainter) iter.next(); --- 299,303 ---- edgePainterMenu = new JComboBox(); edgePainterMenu.setFont(new Font(null, Font.PLAIN, 9)); ! Vector painters = graphPane.getFacetRegistry().getAvailableEdgePainters(); for (Iterator iter = painters.iterator(); iter.hasNext();) { EdgePainter painter = (EdgePainter) iter.next(); *************** *** 286,290 **** public void actionPerformed(ActionEvent e) { EdgePainter p = (EdgePainter)edgePainterMenu.getSelectedItem(); ! graphPane.getPainterRegistry().setDefaultEdgePainter((EdgePainter)p.clone()); getEdgePainterConfigureButton().setEnabled(p.hasVisualController()); graphPane.repaint(); --- 308,312 ---- public void actionPerformed(ActionEvent e) { EdgePainter p = (EdgePainter)edgePainterMenu.getSelectedItem(); ! graphPane.getFacetRegistry().setDefaultEdgePainter((EdgePainter)p.clone()); getEdgePainterConfigureButton().setEnabled(p.hasVisualController()); graphPane.repaint(); *************** *** 373,387 **** } - /** - * @return Returns the registered Vocabularies. - */ - public Enumeration getRegisteredVocabularies() { - return graphPane.getRegisteredVocabularies(); - } - - public void registerVocabulary(Vocabulary v) { - graphPane.registerVocabulary(v); - } - /** * @see de.fzi.wim.guibase.graphview.selection.SelectionListener#nodesAddedToSelection(de.fzi.wim.guibase.graphview.selection.NodeSelectionModel, java.util.Collection) --- 395,398 ---- *************** *** 448,452 **** public void actionPerformed(java.awt.event.ActionEvent e) { ! int result = GenericDialog.showModalDialog(null, graphPane.getPainterRegistry().getDefaultNodePainter().getVisualController(), "Default Node Painter Properties"); } }); --- 459,463 ---- public void actionPerformed(java.awt.event.ActionEvent e) { ! int result = GenericDialog.showModalDialog(null, graphPane.getFacetRegistry().getDefaultNodePainter().getVisualController(), "Default Node Painter Properties"); } }); *************** *** 475,479 **** public void actionPerformed(java.awt.event.ActionEvent e) { ! int result = GenericDialog.showModalDialog(null, graphPane.getPainterRegistry().getDefaultEdgePainter().getVisualController(), "Default Edge Painter Properties"); } }); --- 486,490 ---- public void actionPerformed(java.awt.event.ActionEvent e) { ! int result = GenericDialog.showModalDialog(null, graphPane.getFacetRegistry().getDefaultEdgePainter().getVisualController(), "Default Edge Painter Properties"); } }); Index: GraphlApplication.java =================================================================== RCS file: /cvsroot/graphl/graphl/src/org/mediavirus/graphl/GraphlApplication.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** GraphlApplication.java 27 Jul 2004 13:24:06 -0000 1.5 --- GraphlApplication.java 21 Aug 2004 22:31:02 -0000 1.6 *************** *** 29,32 **** --- 29,33 ---- import org.mediavirus.graphl.gui.GenericDialog; import org.mediavirus.graphl.gui.OpenURLPanel; + import org.mediavirus.graphl.view.GraphFacetRegistry; import org.mediavirus.graphl.vocabulary.DC; import org.mediavirus.graphl.vocabulary.FOAF; *************** *** 47,50 **** --- 48,52 ---- JMenu fileMenu; + JMenuItem newItem; JMenuItem openFileItem; JMenuItem openURLItem; *************** *** 60,63 **** --- 62,67 ---- private boolean doReload; + private RDFGraph settings = new RDFGraph(); + /** * @throws java.awt.HeadlessException *************** *** 90,93 **** --- 94,107 ---- fileMenu.setFont(menuFont); + newItem = new JMenuItem("New"); + newItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, KeyEvent.CTRL_DOWN_MASK)); + newItem.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + doNew(); + } + }); + newItem.setFont(menuFont); + fileMenu.add(newItem); + openFileItem = new JMenuItem("Open File..."); openFileItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, KeyEvent.CTRL_DOWN_MASK)); *************** *** 135,143 **** // TODO (Future): the whole vocab. registration should be defined in RDF files ! mainPanel.registerVocabulary(Graphl.getVocabulary()); ! mainPanel.registerVocabulary(RDF.getVocabulary()); ! mainPanel.registerVocabulary(RDFS.getVocabulary()); ! mainPanel.registerVocabulary(DC.getVocabulary()); ! mainPanel.registerVocabulary(FOAF.getVocabulary()); //mainPanel.graphPane.getPainterRegistry().setEdgePainterForType("http://www.mediavirus.org/graphl#frontLink", new StraightLineEdgePainter(0.7f, LineEdgePainter.DASHED, true, false, 9)); --- 149,157 ---- // TODO (Future): the whole vocab. registration should be defined in RDF files ! mainPanel.graphPane.getVocabularyRegistry().registerVocabulary(Graphl.getVocabulary()); ! mainPanel.graphPane.getVocabularyRegistry().registerVocabulary(RDF.getVocabulary()); ! mainPanel.graphPane.getVocabularyRegistry().registerVocabulary(RDFS.getVocabulary()); ! mainPanel.graphPane.getVocabularyRegistry().registerVocabulary(DC.getVocabulary()); ! mainPanel.graphPane.getVocabularyRegistry().registerVocabulary(FOAF.getVocabulary()); //mainPanel.graphPane.getPainterRegistry().setEdgePainterForType("http://www.mediavirus.org/graphl#frontLink", new StraightLineEdgePainter(0.7f, LineEdgePainter.DASHED, true, false, 9)); *************** *** 207,210 **** --- 221,249 ---- * */ + protected void doNew() { + if (mainPanel.getGraph().isDirty()) { + int choice = JOptionPane.showConfirmDialog(this, "File has been changed. Do you want to save your changes?", "Save Changes?", JOptionPane.YES_NO_CANCEL_OPTION); + if (choice == JOptionPane.CANCEL_OPTION) return; + if (choice == JOptionPane.YES_OPTION) { + boolean result = doSave(); + // if save not succesful, treat as cancel + if (result != true) { + return; + } + } + } + + mainPanel.stopLayout(); + mainPanel.getGraph().clear(); + mainPanel.getGraph().resetDirty(); + + currentFile = null; + setTitle("Graphl - untitled"); + + mainPanel.resumeLayout(); + } + /** + * + */ protected boolean doSaveAs() { JFileChooser chooser = new JFileChooser(); *************** *** 318,335 **** } } public static void main(String[] args) { - System.out.println(RDF.getVocabulary().getNamespace()); - System.out.println(RDFS.getVocabulary().getNamespace()); - GraphlApplication app = new GraphlApplication(); ! app.setSize(800, 600); app.show(); try { app.mainPanel.getGraph().readFromFile(System.getProperty("user.dir") + "/graphs/default.rdf"); ! app.setTitle("Graphl - test.rdf"); } catch (IOException ioex) { --- 357,379 ---- } } + + public void loadSettings(String filename) throws IOException { + settings.clear(); + settings.readFromFile(filename); + + mainPanel.graphPane.setFacetRegistry(new GraphFacetRegistry(settings)); + } public static void main(String[] args) { GraphlApplication app = new GraphlApplication(); ! app.setSize(1024, 768); app.show(); try { + app.loadSettings(System.getProperty("user.dir") + "/config/config.rdf"); app.mainPanel.getGraph().readFromFile(System.getProperty("user.dir") + "/graphs/default.rdf"); ! app.setTitle("Graphl - default.rdf"); } catch (IOException ioex) { Index: GraphlPane.java =================================================================== RCS file: /cvsroot/graphl/graphl/src/org/mediavirus/graphl/GraphlPane.java,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** GraphlPane.java 2 Aug 2004 12:36:49 -0000 1.10 --- GraphlPane.java 21 Aug 2004 22:31:02 -0000 1.11 *************** *** 5,8 **** --- 5,9 ---- package org.mediavirus.graphl; + import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; *************** *** 11,33 **** import java.awt.Rectangle; import java.awt.RenderingHints; import java.awt.geom.AffineTransform; import java.awt.geom.NoninvertibleTransformException; import java.awt.geom.Point2D; import java.util.Collection; ! import java.util.Enumeration; import java.util.HashSet; import java.util.Iterator; import java.util.Set; - import java.util.Vector; ! import org.mediavirus.graphl.graph.*; import org.mediavirus.graphl.graph.rdf.RDFGraph; import org.mediavirus.graphl.interaction.Manipulator; import org.mediavirus.graphl.layout.AbsoluteNodeLayouter; import org.mediavirus.graphl.layout.RepulsionNodeLayouter; import org.mediavirus.graphl.layout.SortedNodeLayouter; - import org.mediavirus.graphl.layout.UnconstrainedNodeLayouter; - import org.mediavirus.graphl.layout.NeutralEdgeLayouter; import org.mediavirus.graphl.layout.SpringEdgeLayouter; import org.mediavirus.graphl.painter.BoxNodePainter; import org.mediavirus.graphl.painter.EdgePainter; --- 12,48 ---- import java.awt.Rectangle; import java.awt.RenderingHints; + import java.awt.event.ComponentEvent; + import java.awt.event.FocusEvent; + import java.awt.event.KeyEvent; + import java.awt.event.MouseEvent; import java.awt.geom.AffineTransform; import java.awt.geom.NoninvertibleTransformException; import java.awt.geom.Point2D; + import java.util.ArrayList; import java.util.Collection; ! import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; + import java.util.List; + import java.util.ListIterator; + import java.util.Map; import java.util.Set; ! import javax.swing.JPanel; ! import javax.swing.SwingUtilities; ! import javax.swing.ToolTipManager; ! ! import org.mediavirus.graphl.graph.Edge; ! import org.mediavirus.graphl.graph.Graph; ! import org.mediavirus.graphl.graph.GraphListener; ! import org.mediavirus.graphl.graph.Node; import org.mediavirus.graphl.graph.rdf.RDFGraph; import org.mediavirus.graphl.interaction.Manipulator; import org.mediavirus.graphl.layout.AbsoluteNodeLayouter; + import org.mediavirus.graphl.layout.NeutralEdgeLayouter; import org.mediavirus.graphl.layout.RepulsionNodeLayouter; import org.mediavirus.graphl.layout.SortedNodeLayouter; import org.mediavirus.graphl.layout.SpringEdgeLayouter; + import org.mediavirus.graphl.layout.UnconstrainedNodeLayouter; import org.mediavirus.graphl.painter.BoxNodePainter; import org.mediavirus.graphl.painter.EdgePainter; *************** *** 38,46 **** import org.mediavirus.graphl.painter.NodePainter; import org.mediavirus.graphl.painter.StraightLineEdgePainter; import org.mediavirus.graphl.view.FacetRegistry; - import org.mediavirus.graphl.view.JGraphPane; import org.mediavirus.graphl.view.SimpleFacetRegistry; ! import org.mediavirus.graphl.vocabulary.*; ! import org.mediavirus.graphl.vocabulary.Vocabulary; /** --- 53,62 ---- import org.mediavirus.graphl.painter.NodePainter; import org.mediavirus.graphl.painter.StraightLineEdgePainter; + import org.mediavirus.graphl.selection.DefaultSelectionModel; + import org.mediavirus.graphl.selection.SelectionModel; import org.mediavirus.graphl.view.FacetRegistry; import org.mediavirus.graphl.view.SimpleFacetRegistry; ! import org.mediavirus.graphl.vocabulary.SimpleVocabularyRegistry; ! import org.mediavirus.graphl.vocabulary.VocabularyRegistry; /** *************** *** 48,57 **** * @author flo */ ! public class GraphlPane extends JGraphPane implements GraphListener, VocabularyRegistry { private Set registeredEdgeTypes = new HashSet(); - Vector registeredVocabularies = new Vector(); - private boolean autoRegisterTypes = true; --- 64,71 ---- * @author flo */ ! public class GraphlPane extends JPanel implements GraphListener { private Set registeredEdgeTypes = new HashSet(); private boolean autoRegisterTypes = true; *************** *** 61,126 **** private Point2D.Double translationPoint; ! FacetRegistry painterRegistry = new SimpleFacetRegistry(); public GraphlPane() { this(new RDFGraph()); } ! public FacetRegistry getPainterRegistry() { ! return painterRegistry; } /** ! * @param graph */ ! public GraphlPane(RDFGraph graph) { ! super(graph); ! ! updateTransform(); ! ! graph.addGraphListener(this); ! ! setupDefaultPainters(); ! } private void setupDefaultPainters() { // register the available painter classes (for user selection) ! painterRegistry.registerEdgePainter(new StraightLineEdgePainter()); ! painterRegistry.registerEdgePainter(new ManhattanEdgePainter()); ! painterRegistry.registerEdgePainter(new InvisibleEdgePainter()); ! painterRegistry.registerNodePainter(new BoxNodePainter()); ! painterRegistry.registerNodePainter(new ImageNodePainter()); ! painterRegistry.registerNodePainter(new InvisibleNodePainter()); ! painterRegistry.registerEdgeLayouter(new SpringEdgeLayouter()); ! painterRegistry.registerEdgeLayouter(new NeutralEdgeLayouter()); ! painterRegistry.registerNodeLayouter(new UnconstrainedNodeLayouter()); ! painterRegistry.registerNodeLayouter(new RepulsionNodeLayouter()); ! painterRegistry.registerNodeLayouter(new AbsoluteNodeLayouter()); ! painterRegistry.registerNodeLayouter(new SortedNodeLayouter()); // set default painters ! painterRegistry.setDefaultEdgePainter(new StraightLineEdgePainter()); ! painterRegistry.setDefaultNodePainter(new BoxNodePainter()); ! painterRegistry.setDefaultEdgeLayouter(new SpringEdgeLayouter()); ! painterRegistry.setDefaultNodeLayouter(new RepulsionNodeLayouter()); ! ! // painterRegistry.setNodePainterForType("http://xmlns.com/foaf/0.1/Person", new BoxNodePainter() { ! // protected Color getBackgroundColor( ! // boolean isHighlighted, ! // boolean isSelected, ! // boolean isDragging) { ! // return Color.green; ! // } ! // }); ! // ! // painterRegistry.setEdgePainterForType("http://www.w3.org/1999/02/22-rdf-syntax-ns#type", new StraightLineEdgePainter(0.7f, LineEdgePainter.DOTTED, false, true, 9)); ! // painterRegistry.setEdgePainterForType("http://www.mediavirus.org/graphl#connectedTo", new StraightLineEdgePainter(0.7f, LineEdgePainter.SOLID, false, false, 9)); - // painterRegistry.setEdgeLayouterForType("http://www.w3.org/1999/02/22-rdf-syntax-ns#type", new NeutralEdgeLayouter()); } --- 75,172 ---- private Point2D.Double translationPoint; ! FacetRegistry facetRegistry = new SimpleFacetRegistry(); ! VocabularyRegistry vocabularyRegistry = new SimpleVocabularyRegistry(); ! ! /** The graph being visualized. */ ! protected Graph graph; ! /** The listener for the graph. */ ! protected GraphListener graphListner; ! /** The map of node positions. */ ! protected Map nodePositions; ! /** The array of registered manipulators in the order they were registered. */ ! protected List manipulators; ! /** The map of rigestered manipulators keyed by their name. */ ! protected Map manipulatorsByName; ! private NodePainter defaultNodePainter; ! ! protected SelectionModel selection; ! ! /** ! * Creates a graph pane. ! * ! * @param graph The graph ! */ ! public GraphlPane(Graph graph) { ! super(null); ! graphListner=new GraphHandler(); ! nodePositions=new HashMap(); ! manipulators=new ArrayList(); ! manipulatorsByName=new HashMap(); ! setGraph(graph); ! enableEvents(MouseEvent.MOUSE_EVENT_MASK | MouseEvent.MOUSE_MOTION_EVENT_MASK | KeyEvent.KEY_EVENT_MASK | FocusEvent.FOCUS_EVENT_MASK | ComponentEvent.COMPONENT_EVENT_MASK); ! setBackground(Color.white); ! ToolTipManager.sharedInstance().registerComponent(this); ! defaultNodePainter = new BoxNodePainter(); + updateTransform(); + + graph.addGraphListener(this); + + setupDefaultPainters(); + } + public GraphlPane() { this(new RDFGraph()); } ! public FacetRegistry getFacetRegistry() { ! return facetRegistry; ! } ! ! public void setFacetRegistry(FacetRegistry registry) { ! this.facetRegistry = registry; } /** ! * @return Returns the vocabularyRegistry. */ ! public VocabularyRegistry getVocabularyRegistry() { ! return vocabularyRegistry; ! } ! /** ! * @param vocabularyRegistry The vocabularyRegistry to set. ! */ ! public void setVocabularyRegistry(VocabularyRegistry vocabularyRegistry) { ! this.vocabularyRegistry = vocabularyRegistry; } + /** + * @param graph + */ private void setupDefaultPainters() { // register the available painter classes (for user selection) ! facetRegistry.registerEdgePainter(new StraightLineEdgePainter()); ! facetRegistry.registerEdgePainter(new ManhattanEdgePainter()); ! facetRegistry.registerEdgePainter(new InvisibleEdgePainter()); ! facetRegistry.registerNodePainter(new BoxNodePainter()); ! facetRegistry.registerNodePainter(new ImageNodePainter()); ! facetRegistry.registerNodePainter(new InvisibleNodePainter()); ! facetRegistry.registerEdgeLayouter(new SpringEdgeLayouter()); ! facetRegistry.registerEdgeLayouter(new NeutralEdgeLayouter()); ! facetRegistry.registerNodeLayouter(new UnconstrainedNodeLayouter()); ! facetRegistry.registerNodeLayouter(new RepulsionNodeLayouter()); ! facetRegistry.registerNodeLayouter(new AbsoluteNodeLayouter()); ! facetRegistry.registerNodeLayouter(new SortedNodeLayouter()); // set default painters ! facetRegistry.setDefaultEdgePainter(new StraightLineEdgePainter()); ! facetRegistry.setDefaultNodePainter(new BoxNodePainter()); ! facetRegistry.setDefaultEdgeLayouter(new SpringEdgeLayouter()); ! facetRegistry.setDefaultNodeLayouter(new RepulsionNodeLayouter()); } *************** *** 147,156 **** } - public void setGraph(RDFGraph graph) { - super.setGraph(graph); - - graph.addGraphListener(this); - } - /** * @see javax.swing.JComponent#paintComponent(java.awt.Graphics) --- 193,196 ---- *************** *** 160,164 **** g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); if (transform != null) g2.setTransform(transform); ! super.paintComponent(g); } --- 200,232 ---- g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); if (transform != null) g2.setTransform(transform); ! ! Rectangle clipRectangle=g.getClipBounds(); ! Color oldColor=g.getColor(); ! g.setColor(getBackground()); ! g.fillRect(clipRectangle.x,clipRectangle.y,clipRectangle.width,clipRectangle.height); ! g.setColor(oldColor); ! if (graph!=null) ! synchronized (graph) { ! Rectangle bounds=new Rectangle(); ! long edgesStart=System.currentTimeMillis(); ! Iterator iterator=graph.getEdges().iterator(); ! while (iterator.hasNext()) { ! Edge edge=(Edge)iterator.next(); ! getEdgeScreenBounds(edge,bounds); ! if (clipRectangle.intersects(bounds)) ! paintEdge(g2,edge); ! } ! long edgesDur=System.currentTimeMillis()-edgesStart; ! long nodesStart=System.currentTimeMillis(); ! iterator=graph.getNodes().iterator(); ! while (iterator.hasNext()) { ! Node node=(Node)iterator.next(); ! getNodeScreenBounds(node,bounds); ! if (clipRectangle.intersects(bounds)) ! paintNode(g2,node); ! } ! for (int i=0;i<manipulators.size();i++) ! ((Manipulator)manipulators.get(i)).paint(g2); ! } } *************** *** 229,233 **** * Overrides @see de.fzi.wim.guibase.graphview.graph.GraphListener#graphContentsChanged(de.fzi.wim.guibase.graphview.graph.Graph) */ ! public void graphContentsChanged(Graph graph) { } /* --- 297,313 ---- * Overrides @see de.fzi.wim.guibase.graphview.graph.GraphListener#graphContentsChanged(de.fzi.wim.guibase.graphview.graph.Graph) */ ! public void graphContentsChanged(Graph graph) { ! Iterator nodes = graph.getNodes().iterator(); ! while (nodes.hasNext()) { ! Node node = (Node) nodes.next(); ! facetRegistry.updateNodeFacets(node); ! } ! ! Iterator edges = graph.getEdges().iterator(); ! while (edges.hasNext()) { ! Edge edge = (Edge) edges.next(); ! facetRegistry.updateEdgeFacets(edge); ! } ! } /* *************** *** 238,243 **** for (Iterator iter = edges.iterator(); iter.hasNext();) { Edge edge = (Edge) iter.next(); ! edge.setCurrentPainter(painterRegistry.getDefaultEdgePainter()); ! edge.setCurrentLayouter(painterRegistry.getDefaultEdgeLayouter()); if (autoRegisterTypes) { registeredEdgeTypes.add(edge.getType()); --- 318,322 ---- for (Iterator iter = edges.iterator(); iter.hasNext();) { Edge edge = (Edge) iter.next(); ! facetRegistry.updateEdgeFacets(edge); if (autoRegisterTypes) { registeredEdgeTypes.add(edge.getType()); *************** *** 248,255 **** for (Iterator iter = nodes.iterator(); iter.hasNext();) { Node node = (Node) iter.next(); ! node.setCurrentPainter(painterRegistry.getDefaultNodePainter()); ! node.setCurrentLayouter(painterRegistry.getDefaultNodeLayouter()); } } } --- 327,338 ---- for (Iterator iter = nodes.iterator(); iter.hasNext();) { Node node = (Node) iter.next(); ! facetRegistry.updateNodeFacets(node); } } + + // we have to update all nodes' facets, because new nodes may have changed an existing node's type + // TODO (2) implement node and edge change notification in GraphListener + // then we can detect type changes properly + graphContentsChanged(graph); } *************** *** 260,273 **** /** - * @return Returns the registered Vocabularies. - */ - public Enumeration getRegisteredVocabularies() { - return registeredVocabularies.elements(); - } - - public void registerVocabulary(Vocabulary v) { - registeredVocabularies.add(v); - } - /** * @return Returns the transform. */ --- 343,346 ---- *************** *** 320,322 **** --- 393,808 ---- } + /** + * Returns a manipulator with given name. + * + * @param name the name of the manpulator + * @return the manipulator with given name (or <code>null</code> if the manipualtor with given name is not registered) + */ + public Manipulator getManipulator(String name) { + return (Manipulator)manipulatorsByName.get(name); + } + /** + * Removes a manipulator with given name. + * + * @param name the name of the manpulator + */ + public void removeManipulator(String name) { + Manipulator manipulator=(Manipulator)manipulatorsByName.remove(name); + if (manipulator!=null) { + manipulators.remove(manipulator); + manipulator.setGraphPane(null); + } + } + + /** + * Returns the current graph. + * + * @return the current graph + */ + public Graph getGraph() { + return graph; + } + /** + * Sets the current graph. + * + * @param graph the new graph + */ + public void setGraph(Graph graph) { + Graph oldGraph = this.graph; + if (this.graph!=null) + this.graph.removeGraphListener(graphListner); + this.graph=graph; + if (this.graph!=null) + this.graph.addGraphListener(graphListner); + nodePositions.clear(); + repaint(); + selection = new DefaultSelectionModel(graph); + firePropertyChange("graph",oldGraph,this.graph); + + graph.addGraphListener(this); + } + + /** + * Paints the edge. + * + * @param g the graphics + * @param edge the edge + */ + protected void paintEdge(Graphics2D g,Edge edge) { + EdgePainter edgePainter = getPainterForEdge(edge); + edgePainter.paintEdge(this, g, edge, selection.isEdgeSelected(edge), false); + } + + /** + * Paints the node. + * + * @param g the graphics + * @param node the node + */ + protected void paintNode(Graphics2D g,Node node) { + NodePainter nodePainter = getPainterForNode(node); + nodePainter.paintNode(this, g, node, selection.isNodeSelected(node), false); + } + + /** + * @return + */ + private NodePainter getDefaultNodePainter() { + return defaultNodePainter; + } + + /** + * Returns the node at given point, or <code>null</code> if there is no such node. + * + * @param point the screen point + * @return the node at given point (or <code>null</code> if there is no such node) + */ + public Node getNodeAtPoint(Point point) { + synchronized (graph) { + List nodes=graph.getNodes(); + ListIterator iterator=nodes.listIterator(nodes.size()); + while (iterator.hasPrevious()) { + Node node=(Node)iterator.previous(); + NodePainter nodePainter=getPainterForNode(node); + if (nodePainter.isInNode(this,node,point)) + return node; + } + return null; + } + } + /** + * Returns the set of nodes in given rectangle. + * + * @param rectangle the rectangle in which the nodes must be located + * @return the set of nodes in the region + */ + public Set getNodesInRectangle(Rectangle rectangle) { + synchronized (graph) { + Set nodesInRectangle=new HashSet(); + Rectangle nodeRectangle=new Rectangle(); + Iterator nodes=graph.getNodes().iterator(); + while (nodes.hasNext()) { + Node node=(Node)nodes.next(); + getNodeScreenBounds(node,nodeRectangle); + if (rectangle.contains(nodeRectangle)) + nodesInRectangle.add(node); + } + return nodesInRectangle; + } + } + /** + * Returns the edge at given point, or <code>null</code> if there is no such edge. + * + * @param point the screen point + * @return the edge at given point (or <code>null</code> if there is no such edge) + */ + public Edge getNearestEdge(Point point) { + Edge nearestEdge=null; + double minDistance=4; + synchronized (graph) { + List edges=graph.getEdges(); + ListIterator iterator=edges.listIterator(edges.size()); + while (iterator.hasPrevious()) { + Edge edge=(Edge)iterator.previous(); + EdgePainter edgePainter=getPainterForEdge(edge); + double distance=edgePainter.screenDistanceFromEdge(this,edge,point); + if (distance<minDistance) { + minDistance=distance; + nearestEdge=edge; + } + } + return nearestEdge; + } + } + /** + * Returns the position of the node on the screen. + * + * @param node the node whose on-screen position is required + * @return the position of the node on screen + */ + public Point getScreenPointForNode(Node node) { + Point point=(Point)nodePositions.get(node); + if (point==null) { + point=new Point(); + graphToScreenPoint(new Point2D.Double(node.getX(),node.getY()),point); + nodePositions.put(node,point); + } + return point; + } + /** + * Updates the map of screen positions of nodes. + */ + protected void updateNodeScreenPositions() { + synchronized (graph) { + Point2D graphPoint=new Point2D.Double(); + Iterator nodes=graph.getNodes().iterator(); + while (nodes.hasNext()) { + Node node=(Node)nodes.next(); + Point point=(Point)nodePositions.get(node); + if (point==null) { + point=new Point(); + nodePositions.put(node,point); + } + graphPoint.setLocation(node.getX(),node.getY()); + graphToScreenPoint(graphPoint,point); + } + } + } + /** + * Returns the screen bounds of given node. + * + * @param node the node for which the bounds must be returned + * @param nodeScreenRectangle the rectangle receiving the node's coordinates + */ + public void getNodeScreenBounds(Node node,Rectangle nodeScreenRectangle) { + NodePainter nodePainter = getPainterForNode(node); + nodePainter.getNodeScreenBounds(this,node,nodeScreenRectangle); + } + /** + * Repaints the given node. + * + * @param node the node that needs to be repainted + */ + public void repaintNode(Node node) { + Rectangle nodeScreenRectangle=new Rectangle(); + getNodeScreenBounds(node,nodeScreenRectangle); + repaint(nodeScreenRectangle); + } + /** + * Returns the screen bounds of given edge. + * + * @param edge the edge for which the bounds must be returned + * @param edgeScreenRectangle the rectangle receiving the edge's coordinates + */ + public void getEdgeScreenBounds(Edge edge,Rectangle edgeScreenRectangle) { + EdgePainter edgePainter=getPainterForEdge(edge); + edgePainter.getEdgeScreenBounds(this,edge,edgeScreenRectangle); + } + /** + * Repaints the given edge. + * + * @param edge the edge that needs to be repainted + */ + public void repaintEdge(Edge edge) { + Rectangle edgeScreenRectangle=new Rectangle(); + getEdgeScreenBounds(edge,edgeScreenRectangle); + edgeScreenRectangle.grow(5,5); + repaint(edgeScreenRectangle); + } + /** + * Returns the text for the tool-tip. + * + * @param event the mouse event + * @return the text for the tool-tip + */ + public String getToolTipText(MouseEvent event) { + Point point = event.getPoint(); + Node node = getNodeAtPoint(point); + if (node != null) { + NodePainter nodePainter = getPainterForNode(node); + String toolTipText = nodePainter.getToolTipText(this,node,point); + if (toolTipText != null) + return toolTipText; + } + return super.getToolTipText(event); + } + /** + * Processes the component event. + * + * @param e the event + */ + protected void processComponentEvent(ComponentEvent e) { + super.processComponentEvent(e); + updateNodeScreenPositions(); + repaint(); + } + /** + * Processes the mouse event. + * + * @param e the event + */ + protected void processMouseEvent(MouseEvent e) { + super.processMouseEvent(e); + switch (e.getID()) { + case MouseEvent.MOUSE_PRESSED: + if (!hasFocus() && isRequestFocusEnabled()) + requestFocus(); + for (int i=0;i<manipulators.size() && !e.isConsumed();i++) + ((Manipulator)manipulators.get(i)).mousePressed(e); + break; + case MouseEvent.MOUSE_RELEASED: + for (int i=0;i<manipulators.size() && !e.isConsumed();i++) + ((Manipulator)manipulators.get(i)).mouseReleased(e); + break; + case MouseEvent.MOUSE_CLICKED: + for (int i=0;i<manipulators.size() && !e.isConsumed();i++) + ((Manipulator)manipulators.get(i)).mouseClicked(e); + break; + case MouseEvent.MOUSE_ENTERED: + for (int i=0;i<manipulators.size() && !e.isConsumed();i++) + ((Manipulator)manipulators.get(i)).mouseEntered(e); + break; + case MouseEvent.MOUSE_EXITED: + for (int i=0;i<manipulators.size() && !e.isConsumed();i++) + ((Manipulator)manipulators.get(i)).mouseExited(e); + break; + } + } + /** + * Processes the mouse motion events. + * + * @param e mouse event + */ + protected void processMouseMotionEvent(MouseEvent e) { + super.processMouseMotionEvent(e); + switch (e.getID()) { + case MouseEvent.MOUSE_MOVED: + for (int i=0;i<manipulators.size() && !e.isConsumed();i++) + ((Manipulator)manipulators.get(i)).mouseMoved(e); + break; + case MouseEvent.MOUSE_DRAGGED: + for (int i=0;i<manipulators.size() && !e.isConsumed();i++) + ((Manipulator)manipulators.get(i)).mouseDragged(e); + break; + } + } + /** + * Processes the key event. + * + * @param e the event + */ + protected void processKeyEvent(KeyEvent e) { + super.processKeyEvent(e); + switch (e.getID()) { + case KeyEvent.KEY_TYPED: + for (int i=0;i<manipulators.size() && !e.isConsumed();i++) + ((Manipulator)manipulators.get(i)).keyTyped(e); + break; + case KeyEvent.KEY_PRESSED: + for (int i=0;i<manipulators.size() && !e.isConsumed();i++) + ((Manipulator)manipulators.get(i)).keyPressed(e); + break; + case KeyEvent.KEY_RELEASED: + for (int i=0;i<manipulators.size() && !e.isConsumed();i++) + ((Manipulator)manipulators.get(i)).keyReleased(e); + break; + } + } + /** + * Processes the focus event. + * + * @param e the event + */ + protected void processFocusEvent(FocusEvent e) { + super.processFocusEvent(e); + switch (e.getID()) { + case FocusEvent.FOCUS_GAINED: + for (int i=0;i<manipulators.size();i++) + ((Manipulator)manipulators.get(i)).focusGained(e); + break; + case FocusEvent.FOCUS_LOST: + for (int i=0;i<manipulators.size();i++) + ((Manipulator)manipulators.get(i)).focusLost(e); + break; + } + } + /** + * Overridden to notify the manipulators that the scroll position has changed. + * + * @param rectangle the rectangle + */ + public void scrollRectToVisible(Rectangle rectangle) { + super.scrollRectToVisible(rectangle); + for (int i=0;i<manipulators.size();i++) + ((Manipulator)manipulators.get(i)).notifyGraphPaneScrolled(); + } + + /** + * The handler of graph events. + */ + protected class GraphHandler implements GraphListener { + public void graphLayoutUpdated(Graph graph) { + if (SwingUtilities.isEventDispatchThread()) { + updateNodeScreenPositions(); + repaint(); + } + else + SwingUtilities.invokeLater(new Runnable() { + public void run() { + updateNodeScreenPositions(); + repaint(); + } + }); + } + public void graphUpdated(Graph graph) { + if (SwingUtilities.isEventDispatchThread()) + repaint(); + else + SwingUtilities.invokeLater(new Runnable() { + public void run() { + repaint(); + } + }); + } + public void graphContentsChanged(Graph graph) { + nodePositions.clear(); + repaint(); + } + public void elementsAdded(Graph graph,Collection nodes,Collection edges) { + repaint(); + } + public void elementsRemoved(Graph graph,Collection nodes,Collection edges) { + if (nodes!=null) { + Iterator iterator=nodes.iterator(); + while (iterator.hasNext()) { + Node node=(Node)iterator.next(); + nodePositions.remove(node); + } + } + repaint(); + } + } + + /** + * @return Returns the selection. + */ + public SelectionModel getSelection() { + + return selection; + } + + /** + * @param edge + * @return + */ + public boolean isSelected(Object o) { + if (o instanceof Edge) { + return (selection.isEdgeSelected((Edge)o)); + } + if (o instanceof Node) { + return (selection.isNodeSelected((Node)o)); + } + return false; + } + } |