svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app.document.table.app / org.gvsig.app.document.table.app.mainplugin / src / main / java / org / gvsig / app / project / documents / table / gui / CreateNewAttributePanel.java @ 42533
History | View | Annotate | Download (15.8 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.app.project.documents.table.gui; |
25 |
|
26 |
import java.awt.BorderLayout; |
27 |
import java.awt.GridLayout; |
28 |
import java.awt.LayoutManager; |
29 |
import java.awt.event.ActionListener; |
30 |
import java.awt.event.KeyEvent; |
31 |
import java.awt.event.KeyListener; |
32 |
import java.text.ParseException; |
33 |
|
34 |
import javax.swing.JComboBox; |
35 |
import javax.swing.JLabel; |
36 |
import javax.swing.JOptionPane; |
37 |
import javax.swing.JPanel; |
38 |
import javax.swing.JTextField; |
39 |
|
40 |
import org.gvsig.andami.PluginServices; |
41 |
import org.gvsig.andami.messages.NotificationManager; |
42 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
43 |
import org.gvsig.andami.ui.mdiManager.WindowInfo; |
44 |
import org.gvsig.fmap.dal.DataTypes; |
45 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
46 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
47 |
import org.gvsig.gui.beans.AcceptCancelPanel; |
48 |
import org.gvsig.i18n.Messages; |
49 |
import org.gvsig.tools.ToolsLocator; |
50 |
import org.gvsig.tools.dataTypes.DataTypesManager; |
51 |
|
52 |
/**
|
53 |
* To create new FeatureAttributeDescriptor from the interface.
|
54 |
*
|
55 |
* @author Vicente Caballero Navarro
|
56 |
*
|
57 |
*/
|
58 |
public class CreateNewAttributePanel extends JPanel implements IWindow { |
59 |
|
60 |
/**
|
61 |
*
|
62 |
*/
|
63 |
private static final long serialVersionUID = 6447641307779709964L; |
64 |
private static final String DEFAULT_FIELD_LENGTH = "50"; |
65 |
private JLabel jLblFieldName = null; |
66 |
private JTextField jTxtFieldName = null; |
67 |
private JLabel jLblFieldType = null; |
68 |
private JComboBox jCboFieldType = null; |
69 |
private JLabel jLblFieldLength = null; |
70 |
private JTextField jTxtFieldLength = null; |
71 |
private JLabel jLblFieldPrecision = null; |
72 |
private JTextField jTxtFieldPrecision = null; |
73 |
private JLabel jLblDefaultValue = null; |
74 |
private JTextField jTxtDefaultValue = null; |
75 |
private WindowInfo viewInfo;
|
76 |
private JPanel jPanel = null; |
77 |
private AcceptCancelPanel jPanelOkCancel = null; |
78 |
private JPanel jPnlFields = null; |
79 |
private int maxAttributeNameSize=-1; |
80 |
private KeyListener checkInt = new KeyListener() { |
81 |
|
82 |
public void keyPressed(KeyEvent e) { |
83 |
// do nothing
|
84 |
} |
85 |
|
86 |
public void keyReleased(KeyEvent e) { |
87 |
JTextField component = (JTextField) e.getComponent(); |
88 |
|
89 |
try {
|
90 |
component.setText(String.valueOf(Integer.parseInt(component |
91 |
.getText()))); |
92 |
|
93 |
} catch (Exception ex) { |
94 |
String text = component.getText();
|
95 |
text = |
96 |
(text.length() <= 1) ? "0" : text.substring(0, |
97 |
text.length() - 1);
|
98 |
component.setText(text); |
99 |
} |
100 |
} |
101 |
|
102 |
public void keyTyped(KeyEvent e) { |
103 |
// do nothing
|
104 |
} |
105 |
}; |
106 |
private String[] currentFieldNames; |
107 |
|
108 |
public CreateNewAttributePanel() {
|
109 |
super();
|
110 |
initialize(); |
111 |
} |
112 |
|
113 |
public CreateNewAttributePanel(boolean isDoubleBuffered) { |
114 |
super(isDoubleBuffered);
|
115 |
initialize(); |
116 |
} |
117 |
|
118 |
public CreateNewAttributePanel(LayoutManager layout) { |
119 |
super(layout);
|
120 |
initialize(); |
121 |
} |
122 |
|
123 |
public CreateNewAttributePanel(LayoutManager layout, |
124 |
boolean isDoubleBuffered) {
|
125 |
super(layout, isDoubleBuffered);
|
126 |
initialize(); |
127 |
} |
128 |
|
129 |
public WindowInfo getWindowInfo() {
|
130 |
if (viewInfo == null) { |
131 |
viewInfo = new WindowInfo(WindowInfo.MODALDIALOG);
|
132 |
viewInfo.setWidth(this.getWidth() + 8); |
133 |
viewInfo.setHeight(this.getHeight());
|
134 |
viewInfo.setTitle(PluginServices.getText(this,
|
135 |
"new_field_properties"));
|
136 |
} |
137 |
return viewInfo;
|
138 |
} |
139 |
|
140 |
/**
|
141 |
* This method initializes this
|
142 |
*
|
143 |
* @return void
|
144 |
*/
|
145 |
private void initialize() { |
146 |
this.setLayout(new BorderLayout()); |
147 |
this.setSize(300, 210); |
148 |
this.setPreferredSize(new java.awt.Dimension(300, 210)); |
149 |
this.add(getJPanel(), java.awt.BorderLayout.CENTER);
|
150 |
this.add(getJPanelOkCancel(), java.awt.BorderLayout.SOUTH);
|
151 |
} |
152 |
|
153 |
/**
|
154 |
* This method initializes jTxtFieldName
|
155 |
*
|
156 |
* @return javax.swing.JTextField
|
157 |
*/
|
158 |
private JTextField getJTxtFieldName() { |
159 |
if (jTxtFieldName == null) { |
160 |
jTxtFieldName = new JTextField(); |
161 |
jTxtFieldName.setBounds(new java.awt.Rectangle(147, 15, 138, 22)); |
162 |
} |
163 |
return jTxtFieldName;
|
164 |
} |
165 |
|
166 |
/**
|
167 |
* This method initializes jCboFieldType
|
168 |
*
|
169 |
* @return javax.swing.JComboBox
|
170 |
*/
|
171 |
private JComboBox getJCboFieldType() { |
172 |
DataTypesManager manager = ToolsLocator.getDataTypesManager(); |
173 |
if (jCboFieldType == null) { |
174 |
jCboFieldType = new JComboBox(); |
175 |
jCboFieldType.setBounds(new java.awt.Rectangle(147, 52, 138, 22)); |
176 |
jCboFieldType.addItem(manager.getTypeName(DataTypes.BOOLEAN)); |
177 |
jCboFieldType.addItem(manager.getTypeName(DataTypes.DATE)); |
178 |
jCboFieldType.addItem(manager.getTypeName(DataTypes.INT)); |
179 |
jCboFieldType.addItem(manager.getTypeName(DataTypes.DOUBLE)); |
180 |
jCboFieldType.addItem(manager.getTypeName(DataTypes.STRING)); |
181 |
|
182 |
jCboFieldType.setSelectedIndex(4);
|
183 |
jCboFieldType |
184 |
.addActionListener(new java.awt.event.ActionListener() {
|
185 |
|
186 |
public void actionPerformed(java.awt.event.ActionEvent e) { |
187 |
DataTypesManager manager = |
188 |
ToolsLocator.getDataTypesManager(); |
189 |
String strType =
|
190 |
(String) getJCboFieldType().getModel()
|
191 |
.getSelectedItem(); |
192 |
if (strType == manager.getTypeName(DataTypes.DOUBLE)) {
|
193 |
getJTxtFieldPrecision().setEnabled(true);
|
194 |
if (getJTxtFieldPrecision().getText().equals("")) { |
195 |
getJTxtFieldPrecision().setText("3");
|
196 |
} else {
|
197 |
try {
|
198 |
Integer.parseInt(getJTxtFieldPrecision()
|
199 |
.getText()); |
200 |
} catch (NumberFormatException e1) { |
201 |
getJTxtFieldPrecision().setText("3");
|
202 |
} |
203 |
} |
204 |
} else {
|
205 |
getJTxtFieldPrecision().setEnabled(false);
|
206 |
} |
207 |
if (strType == manager.getTypeName(DataTypes.BOOLEAN)) {
|
208 |
getJTxtFieldLength().setText("0");
|
209 |
getJTxtFieldLength().setEnabled(false);
|
210 |
} else {
|
211 |
getJTxtFieldLength().setEnabled(true);
|
212 |
} |
213 |
|
214 |
} |
215 |
}); |
216 |
|
217 |
} |
218 |
return jCboFieldType;
|
219 |
} |
220 |
|
221 |
/**
|
222 |
* This method initializes jTxtFieldLength
|
223 |
*
|
224 |
* @return javax.swing.JTextField
|
225 |
*/
|
226 |
private JTextField getJTxtFieldLength() { |
227 |
if (jTxtFieldLength == null) { |
228 |
jTxtFieldLength = new JTextField(); |
229 |
jTxtFieldLength.setBounds(new java.awt.Rectangle(147, 89, 138, 22)); |
230 |
jTxtFieldLength.setText(DEFAULT_FIELD_LENGTH); |
231 |
jTxtFieldLength.addKeyListener(checkInt); |
232 |
} |
233 |
return jTxtFieldLength;
|
234 |
} |
235 |
|
236 |
/**
|
237 |
* This method initializes jTxtFieldPrecision
|
238 |
*
|
239 |
* @return javax.swing.JTextField
|
240 |
*/
|
241 |
private JTextField getJTxtFieldPrecision() { |
242 |
if (jTxtFieldPrecision == null) { |
243 |
jTxtFieldPrecision = new JTextField(); |
244 |
jTxtFieldPrecision.setBounds(new java.awt.Rectangle(147, 126, 138, |
245 |
22));
|
246 |
jTxtFieldPrecision.setEnabled(false);
|
247 |
jTxtFieldPrecision.addKeyListener(checkInt); |
248 |
} |
249 |
return jTxtFieldPrecision;
|
250 |
} |
251 |
|
252 |
/**
|
253 |
* This method initializes jTxtDefaultValue
|
254 |
*
|
255 |
* @return javax.swing.JTextField
|
256 |
*/
|
257 |
private JTextField getJTxtDefaultValue() { |
258 |
if (jTxtDefaultValue == null) { |
259 |
jTxtDefaultValue = new JTextField(); |
260 |
jTxtDefaultValue |
261 |
.setBounds(new java.awt.Rectangle(147, 163, 138, 22)); |
262 |
} |
263 |
return jTxtDefaultValue;
|
264 |
} |
265 |
|
266 |
public EditableFeatureAttributeDescriptor loadFieldDescription(
|
267 |
EditableFeatureType featureType) throws ParseException { |
268 |
String nameAttr = ""; |
269 |
int typeAttr = DataTypes.STRING;
|
270 |
int sizeAttr = 0; |
271 |
int precisionAttr = 0; |
272 |
Object defaultValueAttr = ""; |
273 |
|
274 |
nameAttr = getJTxtFieldName().getText(); |
275 |
|
276 |
if (nameAttr == null || nameAttr.length() == 0) { |
277 |
|
278 |
JOptionPane.showMessageDialog(
|
279 |
this,
|
280 |
Messages.getText("_No_input_name"),
|
281 |
Messages.getText("_Rename_column"),
|
282 |
JOptionPane.ERROR_MESSAGE);
|
283 |
return null; |
284 |
} |
285 |
|
286 |
if (maxAttributeNameSize>0){ |
287 |
if (nameAttr.length()>maxAttributeNameSize){
|
288 |
JOptionPane.showMessageDialog(
|
289 |
this,
|
290 |
Messages.getText("_Name_too_long"),
|
291 |
Messages.getText("_Rename_column"),
|
292 |
JOptionPane.ERROR_MESSAGE);
|
293 |
return null; |
294 |
} |
295 |
} |
296 |
|
297 |
String strType =
|
298 |
(String) getJCboFieldType().getModel().getSelectedItem();
|
299 |
typeAttr = ToolsLocator.getDataTypesManager().getType(strType); |
300 |
try {
|
301 |
int fieldLength = Integer.parseInt(getJTxtFieldLength().getText()); |
302 |
sizeAttr = fieldLength; |
303 |
} catch (Exception e) { |
304 |
throw new ParseException(e.getMessage(), 0); |
305 |
} |
306 |
|
307 |
if (typeAttr == DataTypes.DOUBLE) {
|
308 |
try {
|
309 |
precisionAttr = |
310 |
Integer.parseInt(getJTxtFieldPrecision().getText());
|
311 |
} catch (NumberFormatException e) { |
312 |
precisionAttr = 3;
|
313 |
} |
314 |
} |
315 |
defaultValueAttr = getJTxtDefaultValue().getText(); |
316 |
if (defaultValueAttr.equals("")) { |
317 |
defaultValueAttr = null;
|
318 |
} |
319 |
if (featureType.getIndex(nameAttr) != -1) { |
320 |
NotificationManager.showMessageInfo( |
321 |
PluginServices.getText(this, "field_already_exists"), null); |
322 |
return null; |
323 |
} |
324 |
EditableFeatureAttributeDescriptor ead = |
325 |
featureType.add(nameAttr, typeAttr, sizeAttr); |
326 |
ead.setPrecision(precisionAttr); |
327 |
ead.setDefaultValue(defaultValueAttr); |
328 |
return ead;
|
329 |
} |
330 |
|
331 |
public void setOkAction(ActionListener okAction) { |
332 |
getJPanelOkCancel().setOkButtonActionListener(okAction); |
333 |
|
334 |
} |
335 |
|
336 |
/**
|
337 |
* This method initializes jPanel
|
338 |
*
|
339 |
* @return javax.swing.JPanel
|
340 |
*/
|
341 |
private JPanel getJPanel() { |
342 |
if (jPanel == null) { |
343 |
jPanel = new JPanel(); |
344 |
jPanel.setLayout(null);
|
345 |
|
346 |
jPanel.add(getJPnlFields(), null);
|
347 |
} |
348 |
return jPanel;
|
349 |
} |
350 |
|
351 |
/**
|
352 |
* This method initializes jPanelOkCancel
|
353 |
*
|
354 |
* @return javax.swing.JPanel
|
355 |
*/
|
356 |
private AcceptCancelPanel getJPanelOkCancel() {
|
357 |
if (jPanelOkCancel == null) { |
358 |
jPanelOkCancel = new AcceptCancelPanel();
|
359 |
jPanelOkCancel.setCancelButtonActionListener(new ActionListener() { |
360 |
|
361 |
public void actionPerformed(java.awt.event.ActionEvent e) { |
362 |
PluginServices.getMDIManager().closeWindow( |
363 |
CreateNewAttributePanel.this); |
364 |
} |
365 |
}); |
366 |
jPanelOkCancel.setPreferredSize(new java.awt.Dimension(10, 50)); |
367 |
} |
368 |
return jPanelOkCancel;
|
369 |
} |
370 |
|
371 |
/**
|
372 |
* This method initializes jPnlFields
|
373 |
*
|
374 |
* @return javax.swing.JPanel
|
375 |
*/
|
376 |
private JPanel getJPnlFields() { |
377 |
if (jPnlFields == null) { |
378 |
GridLayout gridLayout = new GridLayout(); |
379 |
gridLayout.setRows(6);
|
380 |
gridLayout.setVgap(3);
|
381 |
gridLayout.setHgap(5);
|
382 |
gridLayout.setColumns(2);
|
383 |
jPnlFields = new JPanel(); |
384 |
jPnlFields.setLayout(gridLayout); |
385 |
jPnlFields.setBounds(new java.awt.Rectangle(5, 12, 290, 142)); |
386 |
jLblDefaultValue = new JLabel(); |
387 |
jLblDefaultValue |
388 |
.setBounds(new java.awt.Rectangle(14, 163, 125, 22)); |
389 |
jLblDefaultValue.setText(PluginServices.getText(this,
|
390 |
"default_value"));
|
391 |
jLblFieldPrecision = new JLabel(); |
392 |
jLblFieldPrecision.setBounds(new java.awt.Rectangle(14, 126, 112, |
393 |
22));
|
394 |
jLblFieldPrecision.setText(PluginServices |
395 |
.getText(this, "precision")); |
396 |
jLblFieldLength = new JLabel(); |
397 |
jLblFieldLength.setBounds(new java.awt.Rectangle(14, 89, 99, 22)); |
398 |
jLblFieldLength.setText(PluginServices |
399 |
.getText(this, "field_length")); |
400 |
jLblFieldType = new JLabel(); |
401 |
jLblFieldType.setBounds(new java.awt.Rectangle(14, 52, 94, 22)); |
402 |
jLblFieldType.setText(PluginServices.getText(this, "field_type")); |
403 |
jLblFieldName = new JLabel(); |
404 |
jLblFieldName.setText(PluginServices.getText(this, "field_name")); |
405 |
jLblFieldName.setBounds(new java.awt.Rectangle(14, 15, 99, 22)); |
406 |
jPnlFields.add(jLblFieldName, null);
|
407 |
jPnlFields.add(getJTxtFieldName(), null);
|
408 |
jPnlFields.add(jLblFieldType, null);
|
409 |
jPnlFields.add(getJCboFieldType(), null);
|
410 |
jPnlFields.add(jLblFieldLength, null);
|
411 |
jPnlFields.add(getJTxtFieldLength(), null);
|
412 |
jPnlFields.add(jLblFieldPrecision, null);
|
413 |
jPnlFields.add(getJTxtFieldPrecision(), null);
|
414 |
jPnlFields.add(jLblDefaultValue, null);
|
415 |
jPnlFields.add(getJTxtDefaultValue(), null);
|
416 |
} |
417 |
return jPnlFields;
|
418 |
} |
419 |
|
420 |
public void setCurrentFieldNames(String[] fieldNames) { |
421 |
currentFieldNames = fieldNames; |
422 |
String newField =
|
423 |
PluginServices.getText(this, "field").replaceAll(" +", "_"); |
424 |
int index = 0; |
425 |
for (int i = 0; i < currentFieldNames.length; i++) { |
426 |
if (currentFieldNames[i].startsWith(newField)) {
|
427 |
try {
|
428 |
index = |
429 |
Integer.parseInt(currentFieldNames[i].replaceAll(
|
430 |
newField, ""));
|
431 |
} catch (Exception e) { /* we don't care */ |
432 |
} |
433 |
} |
434 |
} |
435 |
jTxtFieldName.setText(newField + (++index)); |
436 |
} |
437 |
|
438 |
public Object getWindowProfile() { |
439 |
return WindowInfo.DIALOG_PROFILE;
|
440 |
} |
441 |
|
442 |
public void setMaxAttributeNameSize(int maxAttributeNameSize) { |
443 |
this.maxAttributeNameSize = maxAttributeNameSize;
|
444 |
} |
445 |
|
446 |
} |