| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 53 |
protected Tree tree = null; |
| 54 |
|
| 55 |
|
| 56 |
private TreePainter treePainter = null; |
| 57 |
|
| 58 |
public JTreeDisplay(TreePainter treePainter) { |
| 59 |
this.treePainter = treePainter; |
| 60 |
init(); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public JTreeDisplay(TreePainter treePainter, Tree tree) { |
| 67 |
this.treePainter = treePainter; |
| 68 |
|
| 69 |
init(); |
| 70 |
setTree(tree); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
void init() { |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
enableEvents(AWTEvent.MOUSE_EVENT_MASK); |
| 81 |
|
| 82 |
|
| 83 |
addMouseListener(new MListener()); |
| 84 |
|
| 85 |
|
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 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 |
|
| 109 |
|
| 110 |
public void setLineStyle(Stroke lineStroke, Paint linePaint) { |
| 111 |
treePainter.setLineStyle(lineStroke, linePaint); |
| 112 |
repaint(); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public void setHilightStyle(Stroke hilightStroke, Paint hilightPaint) { |
| 119 |
treePainter.setHilightStyle(hilightStroke, hilightPaint); |
| 120 |
repaint(); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
public void setLabelStyle(Font labelFont, Paint labelPaint) { |
| 127 |
treePainter.setLabelStyle(labelFont, labelPaint); |
| 128 |
repaint(); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 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 |
|
| 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 |
|
| 181 |
paint(g2d); |
| 182 |
|
| 183 |
return(PAGE_EXISTS); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
public void treeChanged(Tree tree) { repaint(); } |
| 192 |
|
| 193 |
|
| 194 |
|
| 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 |
|
| 203 |
|
| 204 |
public void addListener(Listener listener) { |
| 205 |
|
| 206 |
listeners.add(listener); |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 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 |
|
| 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 |
} |