Revision 846 org.gvsig.tools/library/trunk/org.gvsig.tools/dynform/src/org/gvsig/tools/dynform/impl/dynformfield/file/JDynFormFieldFile.java
JDynFormFieldFile.java | ||
---|---|---|
3 | 3 |
import java.awt.BorderLayout; |
4 | 4 |
import java.awt.event.ActionEvent; |
5 | 5 |
import java.awt.event.ActionListener; |
6 |
import java.awt.event.FocusEvent; |
|
7 |
import java.awt.event.FocusListener; |
|
6 | 8 |
import java.io.File; |
7 | 9 |
|
8 | 10 |
import javax.swing.JButton; |
11 |
import javax.swing.JFileChooser; |
|
9 | 12 |
import javax.swing.JPanel; |
10 | 13 |
import javax.swing.JTextField; |
14 |
import javax.swing.filechooser.FileFilter; |
|
11 | 15 |
|
12 | 16 |
import org.gvsig.tools.dynform.api.JDynFormField; |
13 | 17 |
import org.gvsig.tools.dynform.impl.dynformfield.AbstractJDynFormField; |
14 | 18 |
import org.gvsig.tools.dynobject.DynObject; |
15 | 19 |
import org.gvsig.tools.service.spi.ServiceManager; |
16 | 20 |
|
17 |
public class JDynFormFieldFile extends AbstractJDynFormField implements JDynFormField { |
|
21 |
public class JDynFormFieldFile extends AbstractJDynFormField implements JDynFormField, FocusListener {
|
|
18 | 22 |
|
19 |
private File value = null; |
|
23 |
private File assignedValue = null; |
|
24 |
private File currentValue = null; |
|
20 | 25 |
private JTextField jtext = null; |
21 | 26 |
private JButton jbutton = null; |
22 |
|
|
27 |
|
|
23 | 28 |
public JDynFormFieldFile(DynObject parameters, |
24 | 29 |
ServiceManager serviceManager) { |
25 | 30 |
super(parameters, serviceManager); |
26 |
this.value = (File) this.getParameterValue();
|
|
31 |
this.assignedValue = (File) this.getParameterValue();
|
|
27 | 32 |
} |
28 | 33 |
|
34 |
public Object getAssignedValue() { |
|
35 |
return this.assignedValue; |
|
36 |
} |
|
37 |
|
|
29 | 38 |
public void initComponent() { |
30 | 39 |
this.contents = new JPanel(); |
31 | 40 |
this.contents.setLayout(new BorderLayout()); |
32 | 41 |
|
33 | 42 |
this.jtext = new JTextField(); |
43 |
this.jtext.addFocusListener(this); |
|
34 | 44 |
this.jtext.setEditable(false); |
35 | 45 |
this.jbutton = new JButton("Browse..."); |
36 | 46 |
this.jbutton.addActionListener(new ActionListener() { |
... | ... | |
42 | 52 |
this.contents.add(jtext,BorderLayout.CENTER); |
43 | 53 |
this.contents.add(jbutton,BorderLayout.LINE_END); |
44 | 54 |
this.contents.setVisible(true); |
45 |
this.setValue(this.value);
|
|
55 |
this.setValue(this.assignedValue);
|
|
46 | 56 |
} |
47 | 57 |
|
48 | 58 |
public void onClickBrowse() { |
49 |
// TODO: implements onClickBrowse in JDynFormFieldFile |
|
50 |
// this.value = value entered by the user |
|
59 |
fireFieldEnterEvent(); |
|
60 |
this.problemIndicator().restore(); |
|
61 |
|
|
62 |
File initialPath = null; |
|
63 |
if( this.currentValue!=null ) { |
|
64 |
initialPath = this.currentValue.getParentFile(); |
|
65 |
} |
|
66 |
File[] x = showOpenFileDialog(this.getLabel(), initialPath); |
|
67 |
if( x==null ) { |
|
68 |
return; |
|
69 |
} |
|
70 |
this.currentValue = x[0]; |
|
71 |
this.jtext.setText(x[0].getPath()); |
|
51 | 72 |
} |
52 | 73 |
|
53 | 74 |
public void setValue(Object value) { |
54 | 75 |
if( value == null ) { |
55 | 76 |
this.jtext.setText(""); |
56 |
return; |
|
77 |
} else { |
|
78 |
if( !(value instanceof File) ) { |
|
79 |
logger.info("setValue invoked with non File value ("+value.toString()+")."); |
|
80 |
return; |
|
81 |
} |
|
82 |
this.jtext.setText(((File)value).getPath()); |
|
57 | 83 |
} |
58 |
if( !(value instanceof File) ) { |
|
59 |
logger.info("setValue invoked with non File value ("+value.toString()+")."); |
|
60 |
return; |
|
61 |
} |
|
62 |
this.value = (File) value; |
|
63 |
this.jtext.setText(this.value.getPath()); |
|
84 |
this.assignedValue = (File) value; |
|
85 |
this.currentValue = this.assignedValue; |
|
64 | 86 |
} |
65 | 87 |
|
66 | 88 |
public Object getValue() { |
67 |
return this.value;
|
|
89 |
return this.currentValue;
|
|
68 | 90 |
} |
69 | 91 |
|
70 | 92 |
public boolean hasValidValue() { |
71 | 93 |
return true; |
72 | 94 |
} |
73 |
} |
|
95 |
|
|
96 |
public File[] showOpenFileDialog(String title, File initialPath) { |
|
97 |
return showChooserDialog(title, JFileChooser.OPEN_DIALOG, JFileChooser.FILES_ONLY, false, initialPath, null, false); |
|
98 |
} |
|
99 |
|
|
100 |
public File[] showChooserDialog( |
|
101 |
final String title, |
|
102 |
final int type, // SAVE_DIALOG / OPEN_DIALOG |
|
103 |
final int selectionMode, // JFileChooser.FILES_ONLY, JFileChooser.DIRECTORIES_ONLY, JFileChooser.FILES_AND_DIRECTORIES |
|
104 |
final boolean multiselection, |
|
105 |
final File initialPath, |
|
106 |
final FileFilter filter, |
|
107 |
final boolean fileHidingEnabled |
|
108 |
) { |
|
109 |
File[] returnValue = null; |
|
110 |
|
|
111 |
JFileChooser fc = new JFileChooser(); |
|
112 |
fc.setDialogTitle(title); |
|
113 |
fc.setDialogType(type); |
|
114 |
fc.setFileSelectionMode(selectionMode); |
|
115 |
fc.setMultiSelectionEnabled(multiselection); |
|
116 |
fc.setCurrentDirectory(initialPath); |
|
117 |
fc.setFileFilter(filter); |
|
118 |
fc.setFileHidingEnabled(fileHidingEnabled); |
|
119 |
int r = JFileChooser.CANCEL_OPTION; |
|
120 |
switch(type) { |
|
121 |
case JFileChooser.SAVE_DIALOG: |
|
122 |
r = fc.showSaveDialog(this.contents); |
|
123 |
break; |
|
124 |
case JFileChooser.OPEN_DIALOG: |
|
125 |
default: |
|
126 |
r = fc.showOpenDialog(this.contents); |
|
127 |
break; |
|
128 |
} |
|
129 |
if( r != JFileChooser.APPROVE_OPTION ) { |
|
130 |
returnValue = null; |
|
131 |
} else { |
|
132 |
if( fc.isMultiSelectionEnabled() ) { |
|
133 |
returnValue = fc.getSelectedFiles(); |
|
134 |
} else { |
|
135 |
returnValue = new File[] { fc.getSelectedFile() }; |
|
136 |
} |
|
137 |
} |
|
138 |
return returnValue; |
|
139 |
} |
|
140 |
|
|
141 |
public void focusGained(FocusEvent arg0) { |
|
142 |
fireFieldEnterEvent(); |
|
143 |
this.problemIndicator().restore(); |
|
144 |
} |
|
145 |
|
|
146 |
public void focusLost(FocusEvent arg0) { |
|
147 |
fireFieldExitEvent(); |
|
148 |
}} |
Also available in: Unified diff