Debian Codegraph @

src/dr/app/gui/tree/JTreeDisplay.java

raw symbols SCIP
1 /*
2 * JTreeDisplay.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.app.gui.tree;
27
28 import dr.evolution.tree.MutableTree;
29 import dr.evolution.tree.MutableTreeListener;
30 import dr.evolution.tree.Tree;
31 import dr.evolution.util.MutableTaxonList;
32 import dr.evolution.util.MutableTaxonListListener;
33 import dr.evolution.util.Taxon;
34 import dr.evolution.util.TaxonList;
35
36 import javax.swing.*;
37 import java.awt.*;
38 import java.awt.event.MouseAdapter;
39 import java.awt.event.MouseEvent;
40 import java.awt.print.PageFormat;
41 import java.awt.print.Printable;
42
43 public class JTreeDisplay extends JComponent implements Printable,
44 MutableTreeListener,
45 MutableTaxonListListener {
46
47 /**
48 *
49 */
50 private static final long serialVersionUID = 4845325190293249506L;
51
52 /** the tree */
53 protected Tree tree = null;
54
55 /** the tree painter */
56 private TreePainter treePainter = null;
57
58 public JTreeDisplay(TreePainter treePainter) {
59 this.treePainter = treePainter;
60 init();
61 }
62
63 /**
64 * @param tree the tree
65 */
66 public JTreeDisplay(TreePainter treePainter, Tree tree) {
67 this.treePainter = treePainter;
68
69 init();
70 setTree(tree);
71 }
72
73 /**
74 * Called by all constructors.
75 */
76 void init() {
77
78 // setBorder(BorderFactory.createLineBorder(Color.black, 1));
79
80 enableEvents(AWTEvent.MOUSE_EVENT_MASK);
81
82 // adds a mouse listener
83 addMouseListener(new MListener());
84 // addMouseMotionListener(new MMListener());
85
86 }
87
88 /**
89 * Set the tree.
90 */
91 public void setTree(Tree tree) {
92 this.tree = tree;
93
94 if (tree != null) {
95 if (tree instanceof MutableTree) {
96 ((MutableTree)tree).addMutableTreeListener(this);
97 }
98
99 if (tree instanceof MutableTaxonList) {
100 ((MutableTaxonList)tree).addMutableTaxonListListener(this);
101 }
102 }
103
104 repaint();
105 }
106
107 /**
108 * Set line style
109 */
110 public void setLineStyle(Stroke lineStroke, Paint linePaint) {
111 treePainter.setLineStyle(lineStroke, linePaint);
112 repaint();
113 }
114
115 /**
116 * Set hilight style
117 */
118 public void setHilightStyle(Stroke hilightStroke, Paint hilightPaint) {
119 treePainter.setHilightStyle(hilightStroke, hilightPaint);
120 repaint();
121 }
122
123 /**
124 * Set label style.
125 */
126 public void setLabelStyle(Font labelFont, Paint labelPaint) {
127 treePainter.setLabelStyle(labelFont, labelPaint);
128 repaint();
129 }
130
131 /**
132 * Set hilight label style.
133 */
134 public void setHilightLabelStyle(Font hilightLabelFont, Paint hilightLabelPaint) {
135 treePainter.setHilightLabelStyle(hilightLabelFont, hilightLabelPaint);
136 repaint();
137 }
138
139 public void paintComponent(Graphics g) {
140
141 if (tree == null) return;
142
143 Dimension size = getSize();
144 Graphics2D g2d = (Graphics2D)g;
145 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
146 treePainter.paintTree(g2d, size, tree);
147
148 }
149
150 //********************************************************************
151 // Printable interface
152 //********************************************************************
153
154 public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
155 if (pageIndex > 0) {
156 return(NO_SUCH_PAGE);
157 } else {
158 Graphics2D g2d = (Graphics2D)g;
159
160 double x0 = pageFormat.getImageableX();
161 double y0 = pageFormat.getImageableY();
162
163 double w0 = pageFormat.getImageableWidth();
164 double h0 = pageFormat.getImageableHeight();
165
166 double w1 = getWidth();
167 double h1 = getHeight();
168
169 double scale;
170
171 if (w0 / w1 < h0 / h1) {
172 scale = w0 / w1;
173 } else {
174 scale = h0 /h1;
175 }
176
177 g2d.translate(x0, y0);
178 g2d.scale(scale, scale);
179
180 // Turn off double buffering
181 paint(g2d);
182 // Turn double buffering back on
183 return(PAGE_EXISTS);
184 }
185 }
186
187 //********************************************************************
188 // MutableTreeListener interface
189 //********************************************************************
190
191 public void treeChanged(Tree tree) { repaint(); }
192
193 //********************************************************************
194 // MutableTaxonListListener interface
195 //********************************************************************
196
197 public void taxonAdded(TaxonList taxonList, Taxon taxon) { repaint(); }
198 public void taxonRemoved(TaxonList taxonList, Taxon taxon) { repaint(); }
199 public void taxaChanged(TaxonList taxonList) { repaint(); }
200
201 /**
202 * Add a plot listener
203 */
204 public void addListener(Listener listener) {
205
206 listeners.add(listener);
207 }
208
209 /**
210 * Tells tree listeners that a node has been clicked.
211 */
212 protected void fireNodeClickedEvent(int node) {
213
214 for (int i=0; i < listeners.size(); i++) {
215 Listener listener = listeners.elementAt(i);
216 listener.nodeClicked(node);
217 }
218 }
219
220 // Listeners
221
222 private final java.util.Vector<Listener> listeners = new java.util.Vector<Listener>();
223
224 public interface Listener {
225
226 public void nodeClicked(int node);
227
228 }
229
230 public class Adaptor implements Listener {
231
232 public void nodeClicked(int node) { }
233
234 }
235
236 public class MListener extends MouseAdapter {
237
238 public void mouseClicked(MouseEvent me) {
239
240 int node = treePainter.findNodeAtPoint(me.getPoint());
241
242 fireNodeClickedEvent(node);
243 }
244 }
245 }