svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.swing / org.gvsig.fmap.dal.swing.impl / src / main / java / org / gvsig / fmap / dal / swing / impl / featuretype / DefaultFeatureTypePanel.java @ 44096
History | View | Annotate | Download (9.35 KB)
1 |
package org.gvsig.fmap.dal.swing.impl.featuretype; |
---|---|
2 |
|
3 |
import org.gvsig.fmap.dal.swing.featuretype.FeatureTypePanel; |
4 |
import java.awt.BorderLayout; |
5 |
import java.awt.Dimension; |
6 |
import java.awt.event.ActionEvent; |
7 |
import java.awt.event.ActionListener; |
8 |
import java.util.Date; |
9 |
import java.util.Objects; |
10 |
import javax.swing.JComponent; |
11 |
import javax.swing.event.ListSelectionEvent; |
12 |
import javax.swing.event.ListSelectionListener; |
13 |
import javax.swing.event.TableModelEvent; |
14 |
import javax.swing.table.AbstractTableModel; |
15 |
import javax.swing.table.TableModel; |
16 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
17 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
18 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
19 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
20 |
import org.gvsig.fmap.dal.feature.FeatureType; |
21 |
import org.gvsig.tools.ToolsLocator; |
22 |
import org.gvsig.tools.dataTypes.DataTypes; |
23 |
import org.gvsig.tools.i18n.I18nManager; |
24 |
|
25 |
/**
|
26 |
*
|
27 |
* @author jjdelcerro
|
28 |
*/
|
29 |
public class DefaultFeatureTypePanel |
30 |
extends FeatureTypePanelView
|
31 |
implements FeatureTypePanel
|
32 |
{ |
33 |
|
34 |
private class FeatureTypeTableModel extends AbstractTableModel { |
35 |
|
36 |
private final FeatureType featureType; |
37 |
private final String[] columnNames = new String[] { |
38 |
"Name",
|
39 |
"Type",
|
40 |
"Size",
|
41 |
"Precision",
|
42 |
"Default value",
|
43 |
"Calculated"
|
44 |
}; |
45 |
private final Class[] columnClasses = new Class[] { |
46 |
String.class,
|
47 |
String.class,
|
48 |
Integer.class,
|
49 |
Integer.class,
|
50 |
String.class,
|
51 |
Boolean.class
|
52 |
}; |
53 |
|
54 |
public FeatureTypeTableModel() {
|
55 |
this.featureType = null; |
56 |
} |
57 |
|
58 |
public FeatureTypeTableModel(FeatureType featureType) {
|
59 |
this.featureType = featureType;
|
60 |
} |
61 |
|
62 |
@Override
|
63 |
public int getRowCount() { |
64 |
if( this.featureType==null ) { |
65 |
return 0; |
66 |
} |
67 |
return this.featureType.size(); |
68 |
} |
69 |
|
70 |
@Override
|
71 |
public int getColumnCount() { |
72 |
if( this.featureType==null ) { |
73 |
return 0; |
74 |
} |
75 |
return this.columnNames.length; |
76 |
} |
77 |
|
78 |
@Override
|
79 |
public String getColumnName(int columnIndex) { |
80 |
if( this.featureType==null ) { |
81 |
return ""; |
82 |
} |
83 |
return this.columnNames[columnIndex]; |
84 |
} |
85 |
|
86 |
@Override
|
87 |
public Class<?> getColumnClass(int columnIndex) { |
88 |
if( this.featureType==null ) { |
89 |
return String.class; |
90 |
} |
91 |
return this.columnClasses[columnIndex]; |
92 |
} |
93 |
|
94 |
@Override
|
95 |
public boolean isCellEditable(int rowIndex, int columnIndex) { |
96 |
return false; |
97 |
} |
98 |
|
99 |
@Override
|
100 |
public Object getValueAt(int rowIndex, int columnIndex) { |
101 |
if( this.featureType==null ) { |
102 |
return ""; |
103 |
} |
104 |
FeatureAttributeDescriptor descriptor = this.featureType.getAttributeDescriptor(rowIndex);
|
105 |
switch(columnIndex) {
|
106 |
case 0: |
107 |
return descriptor.getName();
|
108 |
case 1: |
109 |
return descriptor.getDataTypeName();
|
110 |
case 2: |
111 |
return descriptor.getSize();
|
112 |
case 3: |
113 |
return descriptor.getPrecision();
|
114 |
case 4: |
115 |
return Objects.toString(descriptor.getDefaultValue(),""); |
116 |
case 5: |
117 |
return descriptor.isComputed();
|
118 |
default:
|
119 |
return ""; |
120 |
} |
121 |
} |
122 |
|
123 |
@Override
|
124 |
public void setValueAt(Object aValue, int rowIndex, int columnIndex) { |
125 |
} |
126 |
|
127 |
public void fireUpdateEvent() { |
128 |
this.fireTableChanged(new TableModelEvent(this)); |
129 |
} |
130 |
} |
131 |
|
132 |
private DefaultFeatureTypeAttributePanel descriptorPanel;
|
133 |
private FeatureType featureType;
|
134 |
private boolean editable; |
135 |
private FeatureTypeTableModel tableModel;
|
136 |
private FeatureStore store;
|
137 |
|
138 |
public DefaultFeatureTypePanel() {
|
139 |
this.editable = true; |
140 |
this.initComponents();
|
141 |
} |
142 |
|
143 |
private void initComponents() { |
144 |
this.tableModel = new FeatureTypeTableModel(); |
145 |
this.tblFields.setModel(this.tableModel); |
146 |
this.tblFields.getSelectionModel().addListSelectionListener(new ListSelectionListener() { |
147 |
@Override
|
148 |
public void valueChanged(ListSelectionEvent e) { |
149 |
doFormFieldPut(); |
150 |
} |
151 |
}); |
152 |
this.descriptorPanel = new DefaultFeatureTypeAttributePanel(); |
153 |
this.pnlField.setLayout(new BorderLayout()); |
154 |
this.pnlField.add(this.descriptorPanel, BorderLayout.CENTER); |
155 |
this.descriptorPanel.setEnabled(false); |
156 |
|
157 |
this.btnFormFieldDiscard.addActionListener(new ActionListener() { |
158 |
@Override
|
159 |
public void actionPerformed(ActionEvent e) { |
160 |
doFormFieldPut(); |
161 |
} |
162 |
}); |
163 |
this.btnFormFieldAccept.addActionListener(new ActionListener() { |
164 |
@Override
|
165 |
public void actionPerformed(ActionEvent e) { |
166 |
doFormFieldFetch(); |
167 |
} |
168 |
}); |
169 |
this.btnNew.addActionListener(new ActionListener() { |
170 |
@Override
|
171 |
public void actionPerformed(ActionEvent e) { |
172 |
doNewField(); |
173 |
} |
174 |
}); |
175 |
this.btnDelete.addActionListener(new ActionListener() { |
176 |
@Override
|
177 |
public void actionPerformed(ActionEvent e) { |
178 |
doDeleteField(); |
179 |
} |
180 |
}); |
181 |
|
182 |
this.setPreferredSize(new Dimension(700, 500)); |
183 |
} |
184 |
|
185 |
private void doFormFieldFetch() { |
186 |
int row = this.tblFields.getSelectedRow(); |
187 |
if( row<0 ) { |
188 |
return;
|
189 |
} |
190 |
FeatureAttributeDescriptor descriptor = this.featureType.getAttributeDescriptor(row);
|
191 |
if( descriptor instanceof EditableFeatureAttributeDescriptor ) { |
192 |
this.descriptorPanel.fetch((EditableFeatureAttributeDescriptor) descriptor);
|
193 |
this.tableModel.fireUpdateEvent();
|
194 |
} |
195 |
} |
196 |
|
197 |
private void doFormFieldPut() { |
198 |
int row = this.tblFields.getSelectedRow(); |
199 |
if( row<0 ) { |
200 |
return;
|
201 |
} |
202 |
FeatureAttributeDescriptor descriptor = this.featureType.getAttributeDescriptor(row);
|
203 |
this.descriptorPanel.put(descriptor);
|
204 |
this.descriptorPanel.setEnabled(true); |
205 |
if( descriptor instanceof EditableFeatureAttributeDescriptor ) { |
206 |
this.btnFormFieldAccept.setEnabled(this.editable); |
207 |
this.descriptorPanel.setEditable(this.editable); |
208 |
} else {
|
209 |
this.btnFormFieldAccept.setEnabled(false); |
210 |
this.descriptorPanel.setEditable(false); |
211 |
} |
212 |
} |
213 |
|
214 |
|
215 |
@Override
|
216 |
public JComponent asJComponent() { |
217 |
return this; |
218 |
} |
219 |
|
220 |
public void setEditable(boolean editable) { |
221 |
this.editable = editable;
|
222 |
this.btnFormFieldAccept.setEnabled(editable);
|
223 |
this.btnFormFieldDiscard.setEnabled(editable);
|
224 |
this.btnNew.setEnabled(editable);
|
225 |
this.btnDelete.setEnabled(editable);
|
226 |
this.descriptorPanel.setEnabled(editable);
|
227 |
} |
228 |
|
229 |
@Override
|
230 |
public EditableFeatureType fetch(EditableFeatureType type) {
|
231 |
return type;
|
232 |
} |
233 |
|
234 |
@Override
|
235 |
public void put(FeatureType type) { |
236 |
this.featureType = type;
|
237 |
if( type == null ) { |
238 |
this.store = null; |
239 |
} else {
|
240 |
// Nos quedamos una referencia para evitar que se destruya, ya que
|
241 |
// el featureTyper se guarda solo una WeakReference.
|
242 |
this.store = type.getStore();
|
243 |
} |
244 |
if( !(type instanceof EditableFeatureType) ) { |
245 |
this.setEditable(false); |
246 |
} |
247 |
this.tableModel = new FeatureTypeTableModel(type); |
248 |
this.tblFields.setModel(this.tableModel); |
249 |
if( type.size()>0 ) { |
250 |
this.tblFields.getSelectionModel().setSelectionInterval(0, 0); |
251 |
} |
252 |
} |
253 |
|
254 |
private void doNewField() { |
255 |
EditableFeatureType eft = (EditableFeatureType)this.featureType;
|
256 |
EditableFeatureAttributeDescriptor descriptor = eft.add( |
257 |
this.getNewFieldName(),
|
258 |
DataTypes.STRING |
259 |
); |
260 |
this.tableModel.fireUpdateEvent();
|
261 |
int row = descriptor.getIndex();
|
262 |
this.tblFields.getSelectionModel().addSelectionInterval(row,row);
|
263 |
} |
264 |
|
265 |
private void doDeleteField() { |
266 |
int row = this.tblFields.getSelectedRow(); |
267 |
if( row<0 ) { |
268 |
return;
|
269 |
} |
270 |
FeatureAttributeDescriptor descriptor = this.featureType.getAttributeDescriptor(row);
|
271 |
((EditableFeatureType)this.featureType).remove(row);
|
272 |
this.tableModel.fireUpdateEvent();
|
273 |
if( row >= this.tblFields.getRowCount()) { |
274 |
row = this.tblFields.getRowCount()-1; |
275 |
} |
276 |
this.tblFields.getSelectionModel().addSelectionInterval(row,row);
|
277 |
} |
278 |
|
279 |
private String getNewFieldName() { |
280 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
281 |
String prefix = i18n.getTranslation("_Field"); |
282 |
String fieldName;
|
283 |
for (int i = 1; i < 1000; i++) { |
284 |
fieldName = prefix +"-"+i;
|
285 |
if( this.featureType.get(fieldName)==null ) { |
286 |
return fieldName;
|
287 |
} |
288 |
} |
289 |
fieldName = prefix + "-" + (new Date()).getTime(); |
290 |
return fieldName;
|
291 |
} |
292 |
|
293 |
} |