TableurPanel.java
5.38 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package osp.utils;
import java.awt.Color;
import java.awt.Dimension;
import java.util.Observable;
import java.util.Observer;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableCellRenderer;
import osp.ui.OSPE_VisuPanelInterface;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class TableurPanel extends JPanel implements Observer {
private static final long serialVersionUID = 1L;
private JTable table;
//private OSPE_NavigatorImageDisplay navigatorDisplay;
private OSPE_VisuPanelInterface visuPanel;
private TableurModel tableurModel;
private int sel, num, id, ref, ra, dec, nbcat, prior, slit;
private int[] colSize=null;
private int idRow=0;
public TableurPanel(TableurModel tableurModel, int[] colSize)
{
this.tableurModel = tableurModel;
this.colSize = colSize;
this.setPreferredSize(new Dimension(800, 100));
this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
table = new JTable(tableurModel);
table.setColumnSelectionAllowed(true);
table.setRowSelectionAllowed(true);
table.setSelectionBackground(Color.GREEN);
/* Reordering columns not allowed */
table.getTableHeader().setReorderingAllowed(false);
attributeNumCol();
for (int i = 0; i < colSize.length; i++)
{
if (colSize[i] != Integer.MAX_VALUE)
{
String columnName = tableurModel.getColumnName(i);
table.getColumn(columnName).setMinWidth(colSize[i]);
table.getColumn(columnName).setMaxWidth(colSize[i]);
table.getColumn(columnName).setPreferredWidth(colSize[i]);
}
}
table.setAutoCreateRowSorter(true);
JScrollPane scroll = new JScrollPane(table);
this.add(scroll);
table.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
/**
* when clic on object array row, get the object selected.
*/
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
int a = table.getSelectedRow();
// int colSlit=4; // slit col num in tableurModel
// int slit=99;
if (a>=0)
{
// try to higliht slit with object -- not in function
// String sl = table.getValueAt(a, colSlit).toString();
// if (!sl.contentEquals("none"))
// {
// Integer.parseInt(sl);
// }
getObject(a);
idRow=a;
table.changeSelection(a, 0, false, false);
}
}
});
}
public void updateByHand() {
table.updateUI();
}
@Override
public void update(Observable arg0, Object arg1) {
updateByHand();
}
public OSPE_VisuPanelInterface getVisuPanel()
{
return visuPanel;
}
public void setVisuPanel(OSPE_VisuPanelInterface vPanel)
{
visuPanel=vPanel;
}
/**
* get the object selected on array and set it on image.
* @param selectedRow : object id.
*/
private void getObject(int rowNb) {
// System.out.println("tabPan rowNb "+rowNb);
visuPanel.setImageAtObjectPosition(rowNb);
}
public void razTableSelect() {
// TODO Auto-generated method stub
table.changeSelection(idRow, 0, true, false);
}
public void selectCorrespondingRow(int object_id) {
table.clearSelection();
for (int row = 0; row < table.getRowCount(); row++) {
if ((Integer) table.getValueAt(row, tableurModel.num) == object_id)
table.changeSelection(row, tableurModel.num, false, false);
}
}
private void attributeNumCol(){
/** Set the value column number **/
num=tableurModel.num;
id=tableurModel.id;
ref=tableurModel.ref;
ra=tableurModel.ra;
dec=tableurModel.dec;
nbcat=tableurModel.nbcat;
prior=tableurModel.prior;
slit=tableurModel.slit;
}
public TableurModel getModel() {
return tableurModel;
}
/**
* @return the table
*/
public JTable getTable() {
return table;
}
public static void main(String[] args) {
String[] colString =
{ "num", "isRef", "from", "RA", "DEC",
"RA (recalculated)", "DEC (recalculated)", "Affected Slit" };
boolean[] colEditable =
{ false, true, false, false, false,
false, false,false };
String[] colType =
{ "int", "boolean", "String", "String", "String",
"String", "String", "String" };
int valMaxCol=Integer.MAX_VALUE;
int[] colSize = { 30, 30,valMaxCol, valMaxCol, valMaxCol, valMaxCol, valMaxCol, 30 };
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new TableurPanel(new TableurModel(colString, colEditable, colType), colSize));
frame.setVisible(true);
frame.pack();
}
}