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