View Javadoc

1   /*
2    * ObjectLab, http://www.objectlab.co.uk/open is supporting JTreeMap.
3    * 
4    * Based in London, we are world leaders in the design and development 
5    * of bespoke applications for the securities financing markets.
6    * 
7    * <a href="http://www.objectlab.co.uk/open">Click here to learn more</a>
8    *           ___  _     _           _   _          _
9    *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
10   *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
11   *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
12   *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
13   *                   |__/
14   *
15   *                     www.ObjectLab.co.uk
16   *
17   * $Id: ColorProvider.java 69 2006-10-24 16:20:20Z benoitx $
18   * 
19   * Copyright 2006 the original author or authors.
20   *
21   * Licensed under the Apache License, Version 2.0 (the "License"); you may not
22   * use this file except in compliance with the License. You may obtain a copy of
23   * the License at
24   *
25   * http://www.apache.org/licenses/LICENSE-2.0
26   *
27   * Unless required by applicable law or agreed to in writing, software
28   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
29   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
30   * License for the specific language governing permissions and limitations under
31   * the License.
32   */
33  package net.sf.jtreemap.swing.provider;
34  
35  import java.awt.event.ActionEvent;
36  import java.awt.event.MouseAdapter;
37  import java.awt.event.MouseEvent;
38  import java.awt.event.MouseListener;
39  
40  import javax.swing.AbstractAction;
41  import javax.swing.Icon;
42  import javax.swing.ImageIcon;
43  import javax.swing.JOptionPane;
44  import javax.swing.JPopupMenu;
45  import javax.swing.tree.TreePath;
46  
47  import net.sf.jtreemap.swing.JTreeMap;
48  import net.sf.jtreemap.swing.TreeMapNode;
49  
50  /**
51   * PopupMenu which permits to zoom the JTreeMap<BR>
52   * The menuItems are the ancestors and the children of the displayed TreeMapNode
53   * of the JTreeMap
54   * 
55   * @author Laurent Dutheil
56   */
57  public class ZoomPopupMenu extends JPopupMenu {
58      private static final long serialVersionUID = 8468224816342601183L;
59  
60      /**
61       * Unzoom icon
62       */
63      public static final Icon UNZOOM_ICON = new ImageIcon(ZoomPopupMenu.class.getResource("icons/unzoom.png"));
64  
65      /**
66       * Zoom icon
67       */
68      public static final Icon ZOOM_ICON = new ImageIcon(ZoomPopupMenu.class.getResource("icons/zoom.png"));
69  
70      private JTreeMap jTreeMap;
71  
72      private transient MouseListener mouseListener;
73      
74      private boolean showAbout;
75  
76      /**
77       * Constructor
78       * 
79       * @param jTreeMap
80       *            jTreeMap which you want to add a zoom popup menu
81       */
82      public ZoomPopupMenu(final JTreeMap jTreeMap) {
83          this(jTreeMap,false);
84      }
85      
86      public ZoomPopupMenu(final JTreeMap jTreeMap, final boolean showAbout) {
87          super();
88          this.jTreeMap = jTreeMap;
89          this.mouseListener = new HandleClickMouse();
90          this.jTreeMap.addMouseListener(this.mouseListener);
91          this.showAbout = showAbout;
92      }
93      
94  
95      protected class HandleClickMouse extends MouseAdapter {
96  
97          /*
98           * (non-Javadoc)
99           * 
100          * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
101          */
102         @Override
103         public void mouseClicked(final MouseEvent e) {
104             if (e.getButton() == MouseEvent.BUTTON3
105                     || (e.getButton() == MouseEvent.BUTTON1 && (e.isAltDown() || e.isControlDown() 
106                             || e.isMetaDown() || e.isAltGraphDown()))) {
107 
108                 for (int i = ZoomPopupMenu.this.getComponentCount(); i > 0; i--) {
109                     ZoomPopupMenu.this.remove(i - 1);
110                 }
111 
112                 final TreeMapNode orig = ZoomPopupMenu.this.jTreeMap.getDisplayedRoot();
113 
114                 TreeMapNode cursor = orig;
115                 // Parents
116                 while (cursor.getParent() != null) {
117                     final TreeMapNode parent = (TreeMapNode) cursor.getParent();
118                     final ZoomAction action = new ZoomAction(parent, UNZOOM_ICON);
119                     ZoomPopupMenu.this.insert(action, 0);
120                     cursor = parent;
121                 }
122                 // Separator
123                 ZoomPopupMenu.this.addSeparator();
124 
125                 // children
126                 cursor = orig;
127                 while (cursor.getChild(e.getX(), e.getY()) != null) {
128                     final TreeMapNode child = cursor.getChild(e.getX(), e.getY());
129                     if (!child.isLeaf()) {
130                         final ZoomAction action = new ZoomAction(child, ZOOM_ICON);
131                         ZoomPopupMenu.this.add(action);
132                     }
133                     cursor = child;
134                 }
135                 
136                 if (showAbout) {
137                     // Separator
138                     ZoomPopupMenu.this.addSeparator();
139                     final AboutAction action = new AboutAction();
140                     ZoomPopupMenu.this.add(action);                    
141                 }
142 
143                 ZoomPopupMenu.this.show(e.getComponent(), e.getX(), e.getY());
144             }
145         }
146 
147     }
148 
149     private class ZoomAction extends AbstractAction {
150         private static final long serialVersionUID = -8559400865920393294L;
151 
152         private TreeMapNode node;
153 
154         /**
155          * Constructor
156          * 
157          * @param node
158          *            where you want to zoom/unzoom
159          * @param icon
160          *            icon corresponding to the operation (zoom or unzoom)
161          */
162         public ZoomAction(final TreeMapNode node, final Icon icon) {
163             super(node.getLabel(), icon);
164             this.node = node;
165         }
166 
167         /*
168          * (non-Javadoc)
169          * 
170          * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
171          */
172         public void actionPerformed(final ActionEvent e) {
173             if (jTreeMap.getTreeView() == null) {
174                 ZoomPopupMenu.this.jTreeMap.zoom(this.node);
175                 ZoomPopupMenu.this.jTreeMap.repaint();
176             } else {
177                 TreePath path =  new TreePath(this.node.getPath());
178                 jTreeMap.getTreeView().setSelectionPath(path);
179                 jTreeMap.getTreeView().scrollPathToVisible(path);
180             }
181             
182 
183         }
184 
185         /*
186          * (non-Javadoc)
187          * 
188          * @see javax.swing.Action#isEnabled()
189          */
190         @Override
191         public boolean isEnabled() {
192             return true;
193         }
194     }
195 
196 
197     private class AboutAction extends AbstractAction {
198         private static final long serialVersionUID = -8559400862920393294L;
199 
200         /**
201          * Constructor
202          * 
203          * @param node
204          *            where you want to zoom/unzoom
205          * @param icon
206          *            icon corresponding to the operation (zoom or unzoom)
207          */
208         public AboutAction() {
209             super("About");
210         }
211 
212         /*
213          * (non-Javadoc)
214          * 
215          * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
216          */
217         public void actionPerformed(final ActionEvent e) {
218             JOptionPane.showMessageDialog(null, "<html>JTreeMap powered by <a href=\"http://www.ObjectLab.co.uk\">ObjectLab.co.uk</a></html>");
219         }
220 
221         /*
222          * (non-Javadoc)
223          * 
224          * @see javax.swing.Action#isEnabled()
225          */
226         @Override
227         public boolean isEnabled() {
228             return true;
229         }
230     }
231 
232 }
233 /*
234  *                 ObjectLab is supporing JTreeMap
235  * 
236  * Based in London, we are world leaders in the design and development 
237  * of bespoke applications for the securities financing markets.
238  * 
239  * <a href="http://www.objectlab.co.uk/open">Click here to learn more about us</a>
240  *           ___  _     _           _   _          _
241  *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
242  *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
243  *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
244  *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
245  *                   |__/
246  *
247  *                     www.ObjectLab.co.uk
248  */