Debian Codegraph @

src/dr/app/bss/RowNumberTable.java

raw symbols SCIP
1 /*
2 * RowNumberTable.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.bss;
27
28 import java.awt.Component;
29 import java.awt.Font;
30 import java.beans.PropertyChangeEvent;
31 import java.beans.PropertyChangeListener;
32
33 import javax.swing.JLabel;
34 import javax.swing.JScrollPane;
35 import javax.swing.JTable;
36 import javax.swing.JViewport;
37 import javax.swing.UIManager;
38 import javax.swing.event.ChangeEvent;
39 import javax.swing.event.ChangeListener;
40 import javax.swing.table.DefaultTableCellRenderer;
41 import javax.swing.table.JTableHeader;
42 import javax.swing.table.TableColumn;
43
44 /*
45 * Use a JTable as a renderer for row numbers of a given main table.
46 * This table must be added to the row header of the scrollpane that
47 * contains the main table.
48 */
49 @SuppressWarnings("serial")
50 public class RowNumberTable extends JTable implements ChangeListener,
51 PropertyChangeListener {
52
53 private JTable main;
54
55 public RowNumberTable(JTable table) {
56 main = table;
57 main.addPropertyChangeListener(this);
58
59 setFocusable(false);
60 setAutoCreateColumnsFromModel(false);
61 setModel(main.getModel());
62 setSelectionModel(main.getSelectionModel());
63
64 TableColumn column = new TableColumn();
65 column.setHeaderValue(" ");
66 addColumn(column);
67 column.setCellRenderer(new RowNumberRenderer());
68
69 getColumnModel().getColumn(0).setPreferredWidth(50);
70 setPreferredScrollableViewportSize(getPreferredSize());
71 }
72
73 @Override
74 public void addNotify() {
75 super.addNotify();
76
77 Component c = getParent();
78
79 // Keep scrolling of the row table in sync with the main table.
80
81 if (c instanceof JViewport) {
82 JViewport viewport = (JViewport) c;
83 viewport.addChangeListener(this);
84 }
85 }
86
87 /*
88 * Delegate method to main table
89 */
90 @Override
91 public int getRowCount() {
92 return main.getRowCount();
93 }
94
95 @Override
96 public int getRowHeight(int row) {
97 return main.getRowHeight(row);
98 }
99
100 /*
101 * This table does not use any data from the main TableModel, so just return
102 * a value based on the row parameter.
103 */
104 @Override
105 public Object getValueAt(int row, int column) {
106 return Integer.toString(row + 1);
107 }
108
109 /*
110 * Don't edit data in the main TableModel by mistake
111 */
112 @Override
113 public boolean isCellEditable(int row, int column) {
114 return false;
115 }
116
117 //
118 // Implement the ChangeListener
119 //
120 public void stateChanged(ChangeEvent e) {
121 // Keep the scrolling of the row table in sync with main table
122
123 JViewport viewport = (JViewport) e.getSource();
124 JScrollPane scrollPane = (JScrollPane) viewport.getParent();
125 scrollPane.getVerticalScrollBar()
126 .setValue(viewport.getViewPosition().y);
127 }
128
129 //
130 // Implement the PropertyChangeListener
131 //
132 public void propertyChange(PropertyChangeEvent e) {
133 // Keep the row table in sync with the main table
134
135 if ("selectionModel".equals(e.getPropertyName())) {
136 setSelectionModel(main.getSelectionModel());
137 }
138
139 if ("model".equals(e.getPropertyName())) {
140 setModel(main.getModel());
141 }
142 }
143
144 /*
145 * Borrow the renderer from JDK1.4.2 table header
146 */
147 private static class RowNumberRenderer extends DefaultTableCellRenderer {
148 public RowNumberRenderer() {
149 setHorizontalAlignment(JLabel.CENTER);
150 }
151
152 public Component getTableCellRendererComponent(JTable table,
153 Object value, boolean isSelected, boolean hasFocus, int row,
154 int column) {
155 if (table != null) {
156 JTableHeader header = table.getTableHeader();
157
158 if (header != null) {
159 setForeground(header.getForeground());
160 setBackground(header.getBackground());
161 setFont(header.getFont());
162 }
163 }
164
165 if (isSelected) {
166 setFont(getFont().deriveFont(Font.BOLD));
167 }
168
169 setText((value == null) ? "" : value.toString());
170 setBorder(UIManager.getBorder("TableHeader.cellBorder"));
171
172 return this;
173 }
174 }
175
176 }// END: class