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 @ 1754
History | View | Annotate | Download (5.97 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.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.JLabel; |
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.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 |
|
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 |
private final JButton btnClear; |
60 |
|
61 |
public FilePickerControllerImpl(JTextField txtFile, JButton btnFile, String dialogTitle) { |
62 |
this(txtFile, btnFile, dialogTitle, null, null, true); |
63 |
} |
64 |
|
65 |
public FilePickerControllerImpl(JTextField txtFile, JButton btnFile) { |
66 |
this(txtFile, btnFile, null, null, null, true); |
67 |
} |
68 |
|
69 |
public FilePickerControllerImpl(JTextField txtFile, JButton btnFile, String dialogTitle, String fileChooserID, File initialPath, boolean seticon) { |
70 |
this.value = null; |
71 |
this.txtFile = txtFile;
|
72 |
this.btnFile = btnFile;
|
73 |
this.fileChooserID = fileChooserID;
|
74 |
this.initialPath = initialPath;
|
75 |
this.txtFile.setEditable(false); |
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 |
|
107 |
} |
108 |
|
109 |
private void doClear() { |
110 |
this.set(null); |
111 |
} |
112 |
|
113 |
@Override
|
114 |
public boolean isEmpty() { |
115 |
return this.value==null; |
116 |
} |
117 |
|
118 |
@Override
|
119 |
public File get() { |
120 |
return this.value; |
121 |
} |
122 |
|
123 |
@Override
|
124 |
public void set(File value) { |
125 |
this.value = value;
|
126 |
if( value==null ) { |
127 |
this.txtFile.setText(""); |
128 |
} else {
|
129 |
this.txtFile.setText(value.toString());
|
130 |
} |
131 |
} |
132 |
|
133 |
@Override
|
134 |
public void setEnabled(boolean enabled) { |
135 |
this.btnFile.setEnabled(enabled);
|
136 |
this.txtFile.setEnabled(enabled);
|
137 |
this.btnClear.setEnabled(enabled);
|
138 |
} |
139 |
|
140 |
@Override
|
141 |
public boolean isEnabled() { |
142 |
return this.btnFile.isEnabled(); |
143 |
} |
144 |
|
145 |
private void doSelectFile() { |
146 |
if( !this.isEditable() ) { |
147 |
return;
|
148 |
} |
149 |
ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
150 |
FoldersManager folderManager = ToolsLocator.getFoldersManager(); |
151 |
File[] f = dialogs.showChooserDialog( |
152 |
this.dialogTitle,
|
153 |
JFileChooser.OPEN_DIALOG,
|
154 |
JFileChooser.FILES_ONLY,
|
155 |
true,
|
156 |
folderManager.getLastPath( |
157 |
this.fileChooserID,
|
158 |
this.initialPath
|
159 |
), |
160 |
this.filter,
|
161 |
true
|
162 |
); |
163 |
|
164 |
if( ArrayUtils.isEmpty(f) ) {
|
165 |
return;
|
166 |
} |
167 |
this.set(f[0]); |
168 |
this.fireChangeEvent();
|
169 |
} |
170 |
|
171 |
@Override
|
172 |
public void coerceAndSet(Object value) { |
173 |
this.set((File) this.coerce(DataTypes.FILE, value, null)); |
174 |
} |
175 |
|
176 |
@Override
|
177 |
public void setFileFilter(FileFilter filter) { |
178 |
this.filter = filter;
|
179 |
} |
180 |
|
181 |
} |