1   /****************************************************************************
2    **
3    ** This file is part of yFiles-2.6. 
4    ** 
5    ** yWorks proprietary/confidential. Use is subject to license terms.
6    **
7    ** Redistribution of this file or of an unauthorized byte-code version
8    ** of this file is strictly forbidden.
9    **
10   ** Copyright (c) 2000-2008 by yWorks GmbH, Vor dem Kreuzberg 28, 
11   ** 72070 Tuebingen, Germany. All rights reserved.
12   **
13   ***************************************************************************/
14  package demo.view.viewmode;
15  
16  import demo.view.DemoBase;
17  import y.base.Node;
18  import y.view.EditMode;
19  import y.view.NodeRealizer;
20  import y.view.PopupMode;
21  import y.view.YLabel;
22  
23  import javax.swing.AbstractAction;
24  import javax.swing.JOptionPane;
25  import javax.swing.JPopupMenu;
26  import java.awt.event.ActionEvent;
27  import java.beans.PropertyChangeEvent;
28  import java.beans.PropertyChangeListener;
29  
30  /**
31   *  Demonstrates how to display context sensitive popup menus
32   *  in the view.
33   * 
34   *  This demo does also show how to write an action that opens
35   *  an inlined text editor in the view to modify the label of a node.  
36   *
37   *  To activate the popup menus right click either on a node or
38   *  the view background. 
39   */
40  public class PopupModeDemo extends DemoBase
41  {
42  
43    protected void registerViewModes() {
44      EditMode editMode = new EditMode();
45      //add a popup child mode to editMode (one that listens to the right mouse click
46      //and pops up context sensitive menues)
47      editMode.setPopupMode( new DemoPopupMode() );
48      view.addViewMode( editMode );
49    }
50  
51    class DemoPopupMode extends PopupMode
52    {
53      /** Popup menu for a hit node */
54      public JPopupMenu getNodePopup(Node v)
55      {
56        JPopupMenu pm = new JPopupMenu();
57        pm.add(new ShowNodeInfo(v));
58        pm.add(new EditLabel(v));
59        return pm;
60      }
61  
62      /** Popup menu for a paper (plain background) hit */
63      public JPopupMenu getPaperPopup(double x, double y)
64      {
65        JPopupMenu pm = new JPopupMenu();
66        pm.add(new Zoom(0.8));
67        pm.add(new Zoom(1.2));
68        pm.add(new FitContent(view));
69        return pm;
70      }
71  
72      /** Popup menu for a paper hit if things are selected */
73      public JPopupMenu getSelectionPopup(double x, double y)
74      {
75        JPopupMenu pm = new JPopupMenu();
76        pm.add(new ZoomArea());
77        return pm;
78      }
79    }
80  
81    /**
82     * Action that displays and information dialog for a node. 
83     */
84    class ShowNodeInfo extends AbstractAction
85    {
86      Node v;
87  
88      ShowNodeInfo(Node v)
89      {
90        super("Node Info");
91        this.v = v;
92      }
93  
94      public void actionPerformed(ActionEvent e)
95      {
96        String vtext = view.getGraph2D().getLabelText(v);
97        JOptionPane.showMessageDialog(view,
98                                      "Label text of node is " +
99                                      view.getGraph2D().getLabelText(v) +
100                                     "\n\n(Guess you knew that already :-)");
101     }
102   }
103 
104   /**
105    * Action that opens a text editor for the label of a node 
106    * <p>
107    * The inlined label editor allows to enter multiple lines of
108    * label text for a node. The "Enter" or "Return" key starts
109    * a new line of text. To terminate the label editor click
110    * the mouse somewhere outside of the label editor box.
111    */
112   class EditLabel extends AbstractAction
113   {
114     Node v;
115 
116     EditLabel(Node v)
117     {
118       super("Edit Label");
119       this.v = v;
120     }
121 
122     public void actionPerformed(ActionEvent e)
123     {
124 
125       final NodeRealizer r = view.getGraph2D().getRealizer(v);
126       final YLabel label = r.getLabel();
127 
128 
129       // optional property change listener, that gets invoked
130       // after the label editor has changed the value of the 
131       // label text. what this listener does is to adapt the size
132       // of the node to the new label text
133       PropertyChangeListener pcl = new PropertyChangeListener()
134         {
135           public void propertyChange(PropertyChangeEvent pce)
136             {
137               r.setSize(label.getWidth()+10,label.getHeight()+10);
138             }
139         };
140 
141       view.openLabelEditor(label,
142                            label.getBox().getX(),
143                            label.getBox().getY(),
144                            pcl,    //optional propertyChangeListener
145                            true    //optional single line mode activated
146                            );
147     }
148   }
149 
150   public static void main(String args[])
151   {
152     initLnF();
153     PopupModeDemo demo = new PopupModeDemo();
154     demo.start("Popup Mode Demo");
155   }
156 }
157 
158 
159       
160