-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAttributeTable.java
executable file
·187 lines (164 loc) · 6.7 KB
/
AttributeTable.java
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
//---------------------------------------------------------------------------
// Name: AttributeTable
//
// Description: AttributeTable implements a class to display a table with
// attribute names and values.
//---------------------------------------------------------------------------
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;
public class AttributeTable extends JTable
{
private AttributeModel tableModel; // model for the data in the table
private ShapeFileMapLayer layer; // map layer that is currently displayed
// AttributeModel defines the model to use for the AttributeTable
class AttributeModel extends AbstractTableModel
{
String[] columnNames = {"Attribute", "Value"};
String[] attributeNames; // full list of attribute names
String[] visibleAttributeNames; // list of visible attribute names
String[] attributeValues; // full list of attribute values
String[] visibleAttributeValues;// list of visible attribute values
// method to return the name of the requested column
//--------------------------------------------------
public String getColumnName(int col)
{
return columnNames[col];
}
// method to set the attribute names, along with an indication of
// which ones are currently visible for display
//---------------------------------------------------------------
public void setAttributeNames(String[] names, boolean[] visible)
{
// save the list of names and allocate space for the values
attributeNames = names;
attributeValues = new String[names.length];
// initialize the attribute values to an empty string
for (int i = 0; i < attributeValues.length; i++)
attributeValues[i] = "";
// update the visible names and values arrays
updateVisible(visible);
}
// method to set the attribute values, along with an indication of
// which ones are visible
//----------------------------------------------------------------
public void setAttributeValues(String[] values, boolean[] visible)
{
// save the full list of values
attributeValues = values;
// copy the visible values to the visible values array
int index = 0;
for (int i = 0; i < values.length; i++)
{
if (visible[i])
{
visibleAttributeValues[index] = values[i];
index++;
}
}
// notify the table that things have changed
fireTableDataChanged();
}
// method to update which attributes are visible
//----------------------------------------------
public void updateVisible(boolean[] visible)
{
// count the visible rows
int visibleRows = 0;
for (int i = 0; i < visible.length; i++)
{
if (visible[i])
visibleRows++;
}
// create new visible arrays of the correct size
visibleAttributeNames = new String[visibleRows];
visibleAttributeValues = new String[visibleRows];
// copy the attributes that are visible to the visible arrays
int index = 0;
for (int i = 0; i < visible.length; i++)
{
if (visible[i])
{
visibleAttributeNames[index] = attributeNames[i];
visibleAttributeValues[index] = attributeValues[i];
index++;
}
}
// notify the table that there has been a change
fireTableDataChanged();
}
// method to return the number of rows in the table
//-------------------------------------------------
public int getRowCount()
{
if (visibleAttributeNames != null)
return visibleAttributeNames.length;
else
return 0;
}
// method to return the number of columns in the table
//----------------------------------------------------
public int getColumnCount()
{
return columnNames.length;
}
// method to return the value in a table cell
//-------------------------------------------
public Object getValueAt(int row, int column)
{
if (column == 0)
return visibleAttributeNames[row];
else
return visibleAttributeValues[row];
}
// enabled editing of the cells so that the values can be copied
// from the table. If the user actually edits the contents, it won't
// actually be changed since setValueAt is not implemented.
//-------------------------------------------------------------------
public boolean isCellEditable(int rowIndex, int columnIndex)
{
return true;
}
}
// Constructor for the attribute table
//------------------------------------
public AttributeTable()
{
tableModel = new AttributeModel();
setModel(tableModel);
TableColumn column = getColumnModel().getColumn(0);
column.setPreferredWidth(150);
column.setMinWidth(100);
column.setMaxWidth(150);
column = getColumnModel().getColumn(1);
column.setPreferredWidth(450);
}
// method to set the attribute names to display in the table
//----------------------------------------------------------
public void setAttributeNames(ShapeFileMapLayer layer, String[] names,
boolean[] selected)
{
this.layer = layer;
tableModel.setAttributeNames(names, selected);
}
// method to set the attribute values to display in the table
//-----------------------------------------------------------
public void setAttributeValues(ShapeFileMapLayer layer, String[] values,
boolean[] selected)
{
this.layer = layer;
tableModel.setAttributeValues(values, selected);
}
// method to update which attributes are visible in the table
//-----------------------------------------------------------
public void updateVisible(boolean[] selected)
{
tableModel.updateVisible(selected);
}
// method to return which map layer is currently being displayed
//--------------------------------------------------------------
public ShapeFileMapLayer getCurrentLayer()
{
return layer;
}
}