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