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.MouseAdapter;
36 import java.awt.event.MouseEvent;
37 import java.awt.event.MouseListener;
38
39 import javax.swing.Icon;
40 import javax.swing.ImageIcon;
41 import javax.swing.JPopupMenu;
42
43 import net.sf.jtreemap.swing.JTreeMap;
44 import net.sf.jtreemap.swing.TreeMapNode;
45
46 /**
47 * PopupMenu which permits to zoom the JTreeMap<BR>
48 * The menuItems are the ancestors and the children of the displayed TreeMapNode
49 * of the JTreeMap
50 *
51 * @author Laurent Dutheil
52 */
53 public class ZoomPopupMenu extends JPopupMenu {
54 private static final long serialVersionUID = 8468224816342601183L;
55
56 /**
57 * Unzoom icon
58 */
59 public static final Icon UNZOOM_ICON = new ImageIcon(ZoomPopupMenu.class.getResource("icons/unzoom.png"));
60
61 /**
62 * Zoom icon
63 */
64 public static final Icon ZOOM_ICON = new ImageIcon(ZoomPopupMenu.class.getResource("icons/zoom.png"));
65
66 private final JTreeMap jTreeMap;
67 private final transient MouseListener mListener;
68 private final boolean showAbout;
69
70 /**
71 * Constructor
72 *
73 * @param jTreeMap
74 * jTreeMap which you want to add a zoom popup menu
75 */
76 public ZoomPopupMenu(final JTreeMap jTreeMap) {
77 this(jTreeMap, false);
78 }
79
80 public ZoomPopupMenu(final JTreeMap jTreeMap, final boolean showAbout) {
81 super();
82 this.jTreeMap = jTreeMap;
83 this.mListener = new HandleClickMouse();
84 this.jTreeMap.addMouseListener(this.mListener);
85 this.showAbout = showAbout;
86 }
87
88 protected class HandleClickMouse extends MouseAdapter {
89 @Override
90 public void mouseClicked(final MouseEvent e) {
91 if (e.getButton() == MouseEvent.BUTTON3
92 || e.getButton() == MouseEvent.BUTTON1 && (e.isAltDown() || e.isControlDown() || e.isMetaDown() || e.isAltGraphDown())) {
93
94 for (int i = ZoomPopupMenu.this.getComponentCount(); i > 0; i--) {
95 ZoomPopupMenu.this.remove(i - 1);
96 }
97
98 final TreeMapNode orig = ZoomPopupMenu.this.jTreeMap.getDisplayedRoot();
99
100 TreeMapNode cursor = orig;
101 // Parents
102 while (cursor.getParent() != null) {
103 final TreeMapNode parent = (TreeMapNode) cursor.getParent();
104 final ZoomAction action = new ZoomAction(jTreeMap, parent, UNZOOM_ICON);
105 ZoomPopupMenu.this.insert(action, 0);
106 cursor = parent;
107 }
108 if (orig.getParent() != null) {
109 // Separator
110 ZoomPopupMenu.this.addSeparator();
111 }
112 handleChildren(e, orig);
113
114 if (showAbout) {
115 // Separator
116 ZoomPopupMenu.this.addSeparator();
117 final AboutAction action = new AboutAction();
118 ZoomPopupMenu.this.add(action);
119 }
120
121 ZoomPopupMenu.this.show(e.getComponent(), e.getX(), e.getY());
122 }
123 }
124
125 private void handleChildren(final MouseEvent e, final TreeMapNode orig) {
126 TreeMapNode cursor;
127 // children
128 cursor = orig;
129 while (cursor.getChild(e.getX(), e.getY()) != null) {
130 final TreeMapNode child = cursor.getChild(e.getX(), e.getY());
131 if (!child.isLeaf()) {
132 final ZoomAction action = new ZoomAction(jTreeMap, child, ZOOM_ICON);
133 ZoomPopupMenu.this.add(action);
134 }
135 cursor = child;
136 }
137 }
138 }
139 }
140 /*
141 * ObjectLab is supporing JTreeMap
142 *
143 * Based in London, we are world leaders in the design and development
144 * of bespoke applications for the securities financing markets.
145 *
146 * <a href="http://www.objectlab.co.uk/open">Click here to learn more about us</a>
147 * ___ _ _ _ _ _
148 * / _ \| |__ (_) ___ ___| |_| | __ _| |__
149 * | | | | '_ \| |/ _ \/ __| __| | / _` | '_ \
150 * | |_| | |_) | | __/ (__| |_| |__| (_| | |_) |
151 * \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
152 * |__/
153 *
154 * www.ObjectLab.co.uk
155 */