| 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.beauti.alignmentviewer; |
| 27 |
|
| 28 |
import javax.swing.*; |
| 29 |
import java.awt.*; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public class AlignmentPane extends JComponent implements Scrollable { |
| 36 |
private final TaxonPane taxonPane; |
| 37 |
private final RulerPane rulerPane; |
| 38 |
|
| 39 |
private AlignmentBuffer alignment = null; |
| 40 |
private int rowCount = 0; |
| 41 |
private int columnCount = 0; |
| 42 |
private int rowHeight = 0; |
| 43 |
private int columnWidth = 0; |
| 44 |
private int ascent = 0; |
| 45 |
|
| 46 |
private int col1 = 0; |
| 47 |
private int col2 = 0; |
| 48 |
private int row1 = 0; |
| 49 |
private int row2 = 0; |
| 50 |
|
| 51 |
private RowDecorator rowDecorator = null; |
| 52 |
private ColumnDecorator columnDecorator = null; |
| 53 |
private CellDecorator cellDecorator = null; |
| 54 |
|
| 55 |
public AlignmentPane(TaxonPane taxonPane, RulerPane rulerPane) { |
| 56 |
this.taxonPane = taxonPane; |
| 57 |
this.rulerPane = rulerPane; |
| 58 |
setFont( new Font("monospaced", Font.PLAIN, 12)); |
| 59 |
} |
| 60 |
|
| 61 |
public void setRowDecorator(RowDecorator rowDecorator) { |
| 62 |
this.rowDecorator = rowDecorator; |
| 63 |
} |
| 64 |
|
| 65 |
public void setColumnDecorator(ColumnDecorator columnDecorator) { |
| 66 |
this.columnDecorator = columnDecorator; |
| 67 |
} |
| 68 |
|
| 69 |
public void setCellDecorator(CellDecorator cellDecorator) { |
| 70 |
this.cellDecorator = cellDecorator; |
| 71 |
} |
| 72 |
|
| 73 |
public void setAlignmentBuffer(AlignmentBuffer alignment) { |
| 74 |
this.alignment = alignment; |
| 75 |
|
| 76 |
if (alignment != null) { |
| 77 |
rowCount = alignment.getSequenceCount(); |
| 78 |
columnCount = alignment.getSiteCount(); |
| 79 |
} else { |
| 80 |
rowCount = 0; |
| 81 |
columnCount = 0; |
| 82 |
|
| 83 |
} |
| 84 |
|
| 85 |
FontMetrics fm = getFontMetrics(getFont()); |
| 86 |
rowHeight = fm.getHeight(); |
| 87 |
columnWidth = fm.charWidth('M'); |
| 88 |
ascent = fm.getAscent(); |
| 89 |
|
| 90 |
if (taxonPane != null) { |
| 91 |
taxonPane.setRowHeight(rowHeight); |
| 92 |
} |
| 93 |
rulerPane.setColumnWidth(columnWidth); |
| 94 |
|
| 95 |
setPreferredSize(new Dimension(columnWidth * columnCount, rowHeight * rowCount)); |
| 96 |
invalidate(); |
| 97 |
} |
| 98 |
|
| 99 |
public Rectangle getVisibleArea() { |
| 100 |
return new Rectangle(col1, row1, col2 - col1 + 1, row2 - row1 + 1); |
| 101 |
} |
| 102 |
|
| 103 |
public void setTopRow(int row) { |
| 104 |
Rectangle rect = getVisibleRect(); |
| 105 |
rect.y = getTop(row); |
| 106 |
scrollRectToVisible(rect); |
| 107 |
} |
| 108 |
|
| 109 |
public void setLeftColumn(int col) { |
| 110 |
Rectangle rect = getVisibleRect(); |
| 111 |
rect.x = getLeft(col); |
| 112 |
scrollRectToVisible(rect); |
| 113 |
} |
| 114 |
|
| 115 |
public void setCentreColumn(int col) { |
| 116 |
Rectangle rect = getVisibleRect(); |
| 117 |
rect.x = Math.max(0, (getLeft(col) + getLeft(col) + columnWidth - rect.width) / 2); |
| 118 |
scrollRectToVisible(rect); |
| 119 |
} |
| 120 |
|
| 121 |
public void setRightColumn(int col) { |
| 122 |
Rectangle rect = getVisibleRect(); |
| 123 |
rect.x = Math.max(0, getLeft(col) + columnWidth - rect.width); |
| 124 |
scrollRectToVisible(rect); |
| 125 |
} |
| 126 |
|
| 127 |
public void paint(Graphics graphics) { |
| 128 |
if (alignment == null) return; |
| 129 |
|
| 130 |
Graphics2D g2 = (Graphics2D)graphics; |
| 131 |
|
| 132 |
String[] stateTable = alignment.getStateTable(); |
| 133 |
|
| 134 |
Rectangle bounds = g2.getClipBounds(); |
| 135 |
col1 = Math.min(getColumn(bounds.x), columnCount - 1); |
| 136 |
col2 = Math.min(getColumn(bounds.x + bounds.width), columnCount - 1); |
| 137 |
row1 = Math.min(getRow(bounds.y), rowCount - 1); |
| 138 |
row2 = Math.min(getRow(bounds.y + bounds.height), rowCount - 1); |
| 139 |
|
| 140 |
int x1 = getLeft(col1); |
| 141 |
int x2 = getLeft(col2 + 1); |
| 142 |
int y1 = getTop(row1); |
| 143 |
int y2 = getTop(row2 + 1); |
| 144 |
|
| 145 |
byte[] seq = new byte[columnCount]; |
| 146 |
|
| 147 |
int y = y1; |
| 148 |
if (rowDecorator != null) { |
| 149 |
for (int row = row1; row <= row2; row++) { |
| 150 |
Paint rowBackground = rowDecorator.getRowBackground(row); |
| 151 |
if (rowBackground != null) { |
| 152 |
g2.setPaint(rowBackground); |
| 153 |
Rectangle rect = new Rectangle(x1, y, x2 - x1, rowHeight); |
| 154 |
g2.fill(rect); |
| 155 |
} |
| 156 |
y += rowHeight; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
int x = x1; |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
y = y1; |
| 174 |
for (int row = row1; row <= row2; row++) { |
| 175 |
alignment.getStates(row, col1, col2, seq); |
| 176 |
|
| 177 |
if (cellDecorator != null) { |
| 178 |
x = x1; |
| 179 |
int i = 0; |
| 180 |
for (int col = col1; col <= col2; col++) { |
| 181 |
|
| 182 |
g2.setPaint(cellDecorator.getCellBackground(row, col, seq[i])); |
| 183 |
Rectangle rect = new Rectangle(x, y, x + columnWidth, y + rowHeight); |
| 184 |
g2.fill(rect); |
| 185 |
|
| 186 |
g2.setPaint(cellDecorator.getCellForeground(row, col, seq[i])); |
| 187 |
g2.drawString(stateTable[seq[i]], x, y + ascent); |
| 188 |
|
| 189 |
x += columnWidth; |
| 190 |
i++; |
| 191 |
} |
| 192 |
} else { |
| 193 |
g2.setPaint(Color.BLACK); |
| 194 |
x = x1; |
| 195 |
int i = 0; |
| 196 |
for (int col = col1; col <= col2; col++) { |
| 197 |
g2.drawString(stateTable[seq[i]], x, y + ascent); |
| 198 |
|
| 199 |
x += columnWidth; |
| 200 |
i++; |
| 201 |
} |
| 202 |
} |
| 203 |
y += rowHeight; |
| 204 |
} |
| 205 |
|
| 206 |
x = x1; |
| 207 |
if (columnDecorator != null) { |
| 208 |
for (int col = col1; col <= col2; col++) { |
| 209 |
Paint columnBackground = columnDecorator.getColumnBackground(col); |
| 210 |
if (columnBackground != null) { |
| 211 |
g2.setPaint(columnBackground); |
| 212 |
Rectangle rect = new Rectangle(x, y1, columnWidth, y2 - y1); |
| 213 |
g2.fill(rect); |
| 214 |
} |
| 215 |
x += columnWidth; |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
} |
| 220 |
|
| 221 |
public int getColumn(int x) { |
| 222 |
return x / columnWidth; |
| 223 |
} |
| 224 |
|
| 225 |
public int getLeft(int column) { |
| 226 |
return column * columnWidth; |
| 227 |
} |
| 228 |
|
| 229 |
public int getColumnWidth() { |
| 230 |
return columnWidth; |
| 231 |
} |
| 232 |
|
| 233 |
public int getColumnCount() { |
| 234 |
return columnCount; |
| 235 |
} |
| 236 |
|
| 237 |
public int getRow(int y) { |
| 238 |
return y / rowHeight; |
| 239 |
} |
| 240 |
|
| 241 |
public int getTop(int row) { |
| 242 |
return row * rowHeight; |
| 243 |
} |
| 244 |
|
| 245 |
public int getRowHeight() { |
| 246 |
return rowHeight; |
| 247 |
} |
| 248 |
|
| 249 |
public int getRowCount() { |
| 250 |
return rowCount; |
| 251 |
} |
| 252 |
|
| 253 |
public Dimension getPreferredSize() { |
| 254 |
return new Dimension(columnWidth * columnCount, rowHeight * rowCount); |
| 255 |
} |
| 256 |
|
| 257 |
public Dimension getPreferredScrollableViewportSize() { |
| 258 |
return getPreferredSize(); |
| 259 |
} |
| 260 |
|
| 261 |
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) { |
| 262 |
if (orientation == SwingConstants.HORIZONTAL) { |
| 263 |
return columnWidth; |
| 264 |
} else { |
| 265 |
return rowHeight; |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) { |
| 270 |
if (orientation == SwingConstants.HORIZONTAL) { |
| 271 |
return columnWidth * 10; |
| 272 |
} else { |
| 273 |
return rowHeight * 10; |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
public boolean getScrollableTracksViewportWidth() { |
| 278 |
return false; |
| 279 |
} |
| 280 |
|
| 281 |
public boolean getScrollableTracksViewportHeight() { |
| 282 |
return false; |
| 283 |
} |
| 284 |
} |