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 @ 1746
History | View | Annotate | Download (4.65 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.io.File; |
6 |
import java.net.URL; |
7 |
import javax.swing.ImageIcon; |
8 |
import javax.swing.JButton; |
9 |
import javax.swing.JFileChooser; |
10 |
import javax.swing.JTextField; |
11 |
import javax.swing.filechooser.FileFilter; |
12 |
import org.apache.commons.lang3.ArrayUtils; |
13 |
import org.gvsig.tools.ToolsLocator; |
14 |
import org.gvsig.tools.dataTypes.DataTypes; |
15 |
import org.gvsig.tools.folders.FoldersManager; |
16 |
import org.gvsig.tools.i18n.I18nManager; |
17 |
import org.gvsig.tools.swing.api.pickercontroller.AbstractPickerController; |
18 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
19 |
import org.gvsig.tools.swing.api.pickercontroller.FilePickerController; |
20 |
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
21 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
22 |
import org.gvsig.tools.swing.icontheme.IconThemeManager; |
23 |
|
24 |
/**
|
25 |
*
|
26 |
* @author jjdelcerro
|
27 |
*/
|
28 |
public class FilePickerControllerImpl |
29 |
extends AbstractPickerController<File> |
30 |
implements FilePickerController
|
31 |
{ |
32 |
|
33 |
private final JButton btnFile; |
34 |
private final JTextField txtFile; |
35 |
private final File initialPath; |
36 |
private final String fileChooserID; |
37 |
private final String dialogTitle; |
38 |
private File value; |
39 |
private FileFilter filter; |
40 |
|
41 |
public static void selfRegister() { |
42 |
URL imageResource = DatePickerControllerImpl.class.getClassLoader().getResource("org/gvsig/tools/swing/picker/picker-file.png"); |
43 |
if (imageResource != null) { |
44 |
IconThemeManager iconThemeManager = ToolsSwingLocator.getIconThemeManager(); |
45 |
IconTheme theme = iconThemeManager.getCurrent(); |
46 |
ImageIcon icon = new ImageIcon(imageResource); |
47 |
theme.registerDefault("tools", "picker", "picker-file", icon, imageResource); |
48 |
} |
49 |
} |
50 |
|
51 |
public FilePickerControllerImpl(JTextField txtFile, JButton btnFile, String dialogTitle) { |
52 |
this(txtFile, btnFile, dialogTitle, null, null, true); |
53 |
} |
54 |
|
55 |
public FilePickerControllerImpl(JTextField txtFile, JButton btnFile) { |
56 |
this(txtFile, btnFile, null, null, null, true); |
57 |
} |
58 |
|
59 |
public FilePickerControllerImpl(JTextField txtFile, JButton btnFile, String dialogTitle, String fileChooserID, File initialPath, boolean seticon) { |
60 |
this.value = null; |
61 |
this.txtFile = txtFile;
|
62 |
this.btnFile = btnFile;
|
63 |
this.fileChooserID = fileChooserID;
|
64 |
this.initialPath = initialPath;
|
65 |
this.txtFile.setEditable(false); |
66 |
if( dialogTitle==null ) { |
67 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
68 |
this.dialogTitle = i18n.getTranslation("_Select_file"); |
69 |
} else {
|
70 |
this.dialogTitle = dialogTitle;
|
71 |
} |
72 |
if( seticon ) {
|
73 |
this.btnFile.setIcon(this.getIcon("picker-file")); |
74 |
this.btnFile.setText(""); |
75 |
} |
76 |
this.btnFile.addActionListener(new ActionListener() { |
77 |
@Override
|
78 |
public void actionPerformed(ActionEvent e) { |
79 |
doSelectFile(); |
80 |
} |
81 |
}); |
82 |
this.txtFile.setEditable(false); |
83 |
} |
84 |
|
85 |
@Override
|
86 |
public boolean isEmpty() { |
87 |
return this.value==null; |
88 |
} |
89 |
|
90 |
@Override
|
91 |
public File get() { |
92 |
return this.value; |
93 |
} |
94 |
|
95 |
@Override
|
96 |
public void set(File value) { |
97 |
this.value = value;
|
98 |
this.txtFile.setText(value.toString());
|
99 |
} |
100 |
|
101 |
@Override
|
102 |
public void setEnabled(boolean enabled) { |
103 |
this.btnFile.setEnabled(enabled);
|
104 |
this.txtFile.setEnabled(enabled);
|
105 |
} |
106 |
|
107 |
@Override
|
108 |
public boolean isEnabled() { |
109 |
return this.btnFile.isEnabled(); |
110 |
} |
111 |
|
112 |
private void doSelectFile() { |
113 |
if( !this.isEditable() ) { |
114 |
return;
|
115 |
} |
116 |
ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
117 |
FoldersManager folderManager = ToolsLocator.getFoldersManager(); |
118 |
File[] f = dialogs.showChooserDialog( |
119 |
this.dialogTitle,
|
120 |
JFileChooser.OPEN_DIALOG,
|
121 |
JFileChooser.FILES_ONLY,
|
122 |
true,
|
123 |
folderManager.getLastPath( |
124 |
this.fileChooserID,
|
125 |
this.initialPath
|
126 |
), |
127 |
this.filter,
|
128 |
true
|
129 |
); |
130 |
|
131 |
if( ArrayUtils.isEmpty(f) ) {
|
132 |
return;
|
133 |
} |
134 |
this.set(f[0]); |
135 |
this.fireChangeEvent();
|
136 |
} |
137 |
|
138 |
@Override
|
139 |
public void coerceAndSet(Object value) { |
140 |
this.set((File) this.coerce(DataTypes.FILE, value, null)); |
141 |
} |
142 |
|
143 |
@Override
|
144 |
public void setFileFilter(FileFilter filter) { |
145 |
this.filter = filter;
|
146 |
} |
147 |
|
148 |
} |