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