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 @ 1954
History | View | Annotate | Download (6.48 KB)
1 | 1746 | jjdelcerro | package org.gvsig.tools.swing.impl.pickercontroller; |
---|---|---|---|
2 | |||
3 | 1754 | jjdelcerro | import java.awt.BorderLayout; |
4 | 1746 | jjdelcerro | import java.awt.event.ActionEvent; |
5 | import java.awt.event.ActionListener; |
||
6 | 1954 | jjdelcerro | import java.awt.event.KeyAdapter; |
7 | import java.awt.event.KeyEvent; |
||
8 | 1746 | jjdelcerro | 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 | 1754 | jjdelcerro | import javax.swing.JLabel; |
14 | 1746 | jjdelcerro | 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 | |||
37 | private final JButton btnFile; |
||
38 | private final JTextField txtFile; |
||
39 | private final File initialPath; |
||
40 | private final String fileChooserID; |
||
41 | private final String dialogTitle; |
||
42 | private File value; |
||
43 | private FileFilter filter; |
||
44 | |||
45 | public static void selfRegister() { |
||
46 | URL imageResource = DatePickerControllerImpl.class.getClassLoader().getResource("org/gvsig/tools/swing/picker/picker-file.png"); |
||
47 | if (imageResource != null) { |
||
48 | IconThemeManager iconThemeManager = ToolsSwingLocator.getIconThemeManager(); |
||
49 | IconTheme theme = iconThemeManager.getCurrent(); |
||
50 | ImageIcon icon = new ImageIcon(imageResource); |
||
51 | theme.registerDefault("tools", "picker", "picker-file", icon, imageResource); |
||
52 | } |
||
53 | 1754 | jjdelcerro | imageResource = DatePickerControllerImpl.class.getClassLoader().getResource("org/gvsig/tools/swing/picker/picker-cleartext.png");
|
54 | if (imageResource != null) { |
||
55 | IconThemeManager iconThemeManager = ToolsSwingLocator.getIconThemeManager(); |
||
56 | IconTheme theme = iconThemeManager.getCurrent(); |
||
57 | ImageIcon icon = new ImageIcon(imageResource); |
||
58 | theme.registerDefault("tools", "picker", "picker-cleartext", icon, imageResource); |
||
59 | } |
||
60 | 1746 | jjdelcerro | } |
61 | 1754 | jjdelcerro | private final JButton btnClear; |
62 | 1746 | jjdelcerro | |
63 | public FilePickerControllerImpl(JTextField txtFile, JButton btnFile, String dialogTitle) { |
||
64 | this(txtFile, btnFile, dialogTitle, null, null, true); |
||
65 | } |
||
66 | |||
67 | public FilePickerControllerImpl(JTextField txtFile, JButton btnFile) { |
||
68 | this(txtFile, btnFile, null, null, null, true); |
||
69 | } |
||
70 | |||
71 | public FilePickerControllerImpl(JTextField txtFile, JButton btnFile, String dialogTitle, String fileChooserID, File initialPath, boolean seticon) { |
||
72 | this.value = null; |
||
73 | this.txtFile = txtFile;
|
||
74 | this.btnFile = btnFile;
|
||
75 | this.fileChooserID = fileChooserID;
|
||
76 | this.initialPath = initialPath;
|
||
77 | if( dialogTitle==null ) { |
||
78 | I18nManager i18n = ToolsLocator.getI18nManager(); |
||
79 | this.dialogTitle = i18n.getTranslation("_Select_file"); |
||
80 | } else {
|
||
81 | this.dialogTitle = dialogTitle;
|
||
82 | } |
||
83 | if( seticon ) {
|
||
84 | this.btnFile.setIcon(this.getIcon("picker-file")); |
||
85 | this.btnFile.setText(""); |
||
86 | } |
||
87 | this.btnFile.addActionListener(new ActionListener() { |
||
88 | @Override
|
||
89 | public void actionPerformed(ActionEvent e) { |
||
90 | doSelectFile(); |
||
91 | } |
||
92 | }); |
||
93 | 1754 | jjdelcerro | this.btnClear = new JButton(getIcon("picker-cleartext")); |
94 | btnClear.setBorderPainted( false );
|
||
95 | btnClear.setFocusPainted( false );
|
||
96 | btnClear.setContentAreaFilled( false );
|
||
97 | btnClear.setOpaque(false);
|
||
98 | btnClear.addActionListener(new ActionListener() { |
||
99 | @Override
|
||
100 | public void actionPerformed(ActionEvent e) { |
||
101 | doClear(); |
||
102 | } |
||
103 | }); |
||
104 | 1954 | jjdelcerro | //this.txtFile.setEditable(false);
|
105 | 1754 | jjdelcerro | this.txtFile.setLayout(new BorderLayout()); |
106 | this.txtFile.add(btnClear, BorderLayout.EAST); |
||
107 | 1954 | jjdelcerro | this.txtFile.addKeyListener(new KeyAdapter() { |
108 | @Override
|
||
109 | public void keyPressed(KeyEvent e) { |
||
110 | if( e.getKeyCode()==KeyEvent.VK_ENTER ) { |
||
111 | coerceAndSet(txtFile.getText()); |
||
112 | fireChangeEvent(); |
||
113 | } |
||
114 | super.keyPressed(e);
|
||
115 | } |
||
116 | }); |
||
117 | 1746 | jjdelcerro | } |
118 | |||
119 | 1754 | jjdelcerro | private void doClear() { |
120 | this.set(null); |
||
121 | } |
||
122 | |||
123 | 1746 | jjdelcerro | @Override
|
124 | public boolean isEmpty() { |
||
125 | return this.value==null; |
||
126 | } |
||
127 | |||
128 | @Override
|
||
129 | public File get() { |
||
130 | return this.value; |
||
131 | } |
||
132 | |||
133 | @Override
|
||
134 | public void set(File value) { |
||
135 | this.value = value;
|
||
136 | 1754 | jjdelcerro | if( value==null ) { |
137 | this.txtFile.setText(""); |
||
138 | } else {
|
||
139 | this.txtFile.setText(value.toString());
|
||
140 | } |
||
141 | 1746 | jjdelcerro | } |
142 | |||
143 | @Override
|
||
144 | 1954 | jjdelcerro | public void setEditable(boolean editable) { |
145 | super.setEditable(editable);
|
||
146 | this.txtFile.setEditable(editable);
|
||
147 | } |
||
148 | |||
149 | @Override
|
||
150 | 1746 | jjdelcerro | public void setEnabled(boolean enabled) { |
151 | this.btnFile.setEnabled(enabled);
|
||
152 | this.txtFile.setEnabled(enabled);
|
||
153 | 1754 | jjdelcerro | this.btnClear.setEnabled(enabled);
|
154 | 1746 | jjdelcerro | } |
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[] f = dialogs.showChooserDialog( |
||
168 | this.dialogTitle,
|
||
169 | JFileChooser.OPEN_DIALOG,
|
||
170 | JFileChooser.FILES_ONLY,
|
||
171 | true,
|
||
172 | folderManager.getLastPath( |
||
173 | this.fileChooserID,
|
||
174 | this.initialPath
|
||
175 | ), |
||
176 | this.filter,
|
||
177 | true
|
||
178 | ); |
||
179 | |||
180 | if( ArrayUtils.isEmpty(f) ) {
|
||
181 | return;
|
||
182 | } |
||
183 | this.set(f[0]); |
||
184 | this.fireChangeEvent();
|
||
185 | } |
||
186 | |||
187 | @Override
|
||
188 | public void coerceAndSet(Object value) { |
||
189 | this.set((File) this.coerce(DataTypes.FILE, value, null)); |
||
190 | } |
||
191 | |||
192 | @Override
|
||
193 | public void setFileFilter(FileFilter filter) { |
||
194 | this.filter = filter;
|
||
195 | } |
||
196 | |||
197 | } |