gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.swing / org.gvsig.tools.swing.impl / src / main / java / org / gvsig / tools / swing / impl / pickercontroller / FilePickerControllerImpl.java @ 1987
History | View | Annotate | Download (6.55 KB)
1 |
package org.gvsig.tools.swing.impl.pickercontroller; |
---|---|
2 |
|
3 |
import java.awt.BorderLayout; |
4 |
import java.awt.event.ActionEvent; |
5 |
import java.awt.event.ActionListener; |
6 |
import java.awt.event.KeyAdapter; |
7 |
import java.awt.event.KeyEvent; |
8 |
import java.io.File; |
9 |
import java.net.URL; |
10 |
import javax.swing.ImageIcon; |
11 |
import javax.swing.JButton; |
12 |
import javax.swing.JFileChooser; |
13 |
import javax.swing.JLabel; |
14 |
import javax.swing.JTextField; |
15 |
import javax.swing.filechooser.FileFilter; |
16 |
import org.apache.commons.lang3.ArrayUtils; |
17 |
import org.gvsig.tools.ToolsLocator; |
18 |
import org.gvsig.tools.dataTypes.DataTypes; |
19 |
import org.gvsig.tools.folders.FoldersManager; |
20 |
import org.gvsig.tools.i18n.I18nManager; |
21 |
import org.gvsig.tools.swing.api.pickercontroller.AbstractPickerController; |
22 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
23 |
import org.gvsig.tools.swing.api.pickercontroller.FilePickerController; |
24 |
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
25 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
26 |
import org.gvsig.tools.swing.icontheme.IconThemeManager; |
27 |
|
28 |
/**
|
29 |
*
|
30 |
* @author jjdelcerro
|
31 |
*/
|
32 |
public class FilePickerControllerImpl |
33 |
extends AbstractPickerController<File> |
34 |
implements FilePickerController {
|
35 |
|
36 |
private final JButton btnFile; |
37 |
private final JTextField txtFile; |
38 |
private final File initialPath; |
39 |
private final String fileChooserID; |
40 |
private final String dialogTitle; |
41 |
private File value; |
42 |
private FileFilter filter; |
43 |
|
44 |
public static void selfRegister() { |
45 |
URL imageResource = DatePickerControllerImpl.class.getClassLoader().getResource("org/gvsig/tools/swing/picker/picker-file.png"); |
46 |
if (imageResource != null) { |
47 |
IconThemeManager iconThemeManager = ToolsSwingLocator.getIconThemeManager(); |
48 |
IconTheme theme = iconThemeManager.getCurrent(); |
49 |
ImageIcon icon = new ImageIcon(imageResource); |
50 |
theme.registerDefault("tools", "picker", "picker-file", icon, imageResource); |
51 |
} |
52 |
imageResource = DatePickerControllerImpl.class.getClassLoader().getResource("org/gvsig/tools/swing/picker/picker-cleartext.png");
|
53 |
if (imageResource != null) { |
54 |
IconThemeManager iconThemeManager = ToolsSwingLocator.getIconThemeManager(); |
55 |
IconTheme theme = iconThemeManager.getCurrent(); |
56 |
ImageIcon icon = new ImageIcon(imageResource); |
57 |
theme.registerDefault("tools", "picker", "picker-cleartext", icon, imageResource); |
58 |
} |
59 |
} |
60 |
private final JButton btnClear; |
61 |
|
62 |
public FilePickerControllerImpl(JTextField txtFile, JButton btnFile, String dialogTitle) { |
63 |
this(txtFile, btnFile, dialogTitle, null, null, true); |
64 |
} |
65 |
|
66 |
public FilePickerControllerImpl(JTextField txtFile, JButton btnFile) { |
67 |
this(txtFile, btnFile, null, null, null, true); |
68 |
} |
69 |
|
70 |
public FilePickerControllerImpl(JTextField txtFile, JButton btnFile, String dialogTitle, String fileChooserID, File initialPath, boolean seticon) { |
71 |
this.value = null; |
72 |
this.txtFile = txtFile;
|
73 |
this.btnFile = btnFile;
|
74 |
this.fileChooserID = fileChooserID;
|
75 |
this.initialPath = initialPath;
|
76 |
if (dialogTitle == null) { |
77 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
78 |
this.dialogTitle = i18n.getTranslation("_Select_file"); |
79 |
} else {
|
80 |
this.dialogTitle = dialogTitle;
|
81 |
} |
82 |
if (seticon) {
|
83 |
this.btnFile.setIcon(this.getIcon("picker-file")); |
84 |
this.btnFile.setText(""); |
85 |
} |
86 |
this.btnFile.addActionListener(new ActionListener() { |
87 |
@Override
|
88 |
public void actionPerformed(ActionEvent e) { |
89 |
doSelectFile(); |
90 |
} |
91 |
}); |
92 |
this.btnClear = new JButton(getIcon("picker-cleartext")); |
93 |
btnClear.setBorderPainted(false);
|
94 |
btnClear.setFocusPainted(false);
|
95 |
btnClear.setContentAreaFilled(false);
|
96 |
btnClear.setOpaque(false);
|
97 |
btnClear.addActionListener(new ActionListener() { |
98 |
@Override
|
99 |
public void actionPerformed(ActionEvent e) { |
100 |
doClear(); |
101 |
} |
102 |
}); |
103 |
//this.txtFile.setEditable(false);
|
104 |
this.txtFile.setLayout(new BorderLayout()); |
105 |
this.txtFile.add(btnClear, BorderLayout.EAST); |
106 |
this.txtFile.addKeyListener(new KeyAdapter() { |
107 |
@Override
|
108 |
public void keyPressed(KeyEvent e) { |
109 |
if (e.getKeyCode() == KeyEvent.VK_ENTER) { |
110 |
coerceAndSet(txtFile.getText()); |
111 |
fireChangeEvent(); |
112 |
} |
113 |
super.keyPressed(e);
|
114 |
} |
115 |
}); |
116 |
} |
117 |
|
118 |
private void doClear() { |
119 |
this.set(null); |
120 |
} |
121 |
|
122 |
@Override
|
123 |
public boolean isEmpty() { |
124 |
return this.value == null; |
125 |
} |
126 |
|
127 |
@Override
|
128 |
public File get() { |
129 |
coerceAndSet(txtFile.getText()); |
130 |
return this.value; |
131 |
} |
132 |
|
133 |
@Override
|
134 |
public void set(File value) { |
135 |
this.value = value;
|
136 |
if (value == null) { |
137 |
this.txtFile.setText(""); |
138 |
} else {
|
139 |
this.txtFile.setText(value.toString());
|
140 |
} |
141 |
} |
142 |
|
143 |
@Override
|
144 |
public void setEditable(boolean editable) { |
145 |
super.setEditable(editable);
|
146 |
this.txtFile.setEditable(editable);
|
147 |
} |
148 |
|
149 |
@Override
|
150 |
public void setEnabled(boolean enabled) { |
151 |
this.btnFile.setEnabled(enabled);
|
152 |
this.txtFile.setEnabled(enabled);
|
153 |
this.btnClear.setEnabled(enabled);
|
154 |
} |
155 |
|
156 |
@Override
|
157 |
public boolean isEnabled() { |
158 |
return this.btnFile.isEnabled(); |
159 |
} |
160 |
|
161 |
private void doSelectFile() { |
162 |
if (!this.isEditable()) { |
163 |
return;
|
164 |
} |
165 |
ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
166 |
FoldersManager folderManager = ToolsLocator.getFoldersManager(); |
167 |
File pathToUse = initialPath;
|
168 |
if (this.value != null) { |
169 |
pathToUse = this.value;
|
170 |
} |
171 |
File[] f = dialogs.showChooserDialog( |
172 |
this.dialogTitle,
|
173 |
JFileChooser.OPEN_DIALOG,
|
174 |
JFileChooser.FILES_ONLY,
|
175 |
true,
|
176 |
folderManager.getLastPath( |
177 |
this.fileChooserID,
|
178 |
pathToUse |
179 |
), |
180 |
this.filter,
|
181 |
true
|
182 |
); |
183 |
|
184 |
if (ArrayUtils.isEmpty(f)) {
|
185 |
return;
|
186 |
} |
187 |
this.set(f[0]); |
188 |
this.fireChangeEvent();
|
189 |
} |
190 |
|
191 |
@Override
|
192 |
public void coerceAndSet(Object value) { |
193 |
this.set((File) this.coerce(DataTypes.FILE, value, null)); |
194 |
} |
195 |
|
196 |
@Override
|
197 |
public void setFileFilter(FileFilter filter) { |
198 |
this.filter = filter;
|
199 |
} |
200 |
|
201 |
} |