Statistics
| Revision:

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 1746 jjdelcerro
package org.gvsig.tools.swing.impl.pickercontroller;
2
3
import java.awt.event.ActionEvent;
4 1954 jjdelcerro
import java.awt.event.KeyAdapter;
5
import java.awt.event.KeyEvent;
6 1746 jjdelcerro
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 2165 jjdelcerro
import javax.swing.text.JTextComponent;
13 1746 jjdelcerro
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 2000 jjdelcerro
import org.gvsig.tools.swing.api.ToolsSwingManager;
21 1746 jjdelcerro
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 1987 omartinez
public class FilePickerControllerImpl
31
        extends AbstractPickerController<File>
32
        implements FilePickerController {
33
34 1746 jjdelcerro
    private final JButton btnFile;
35 2165 jjdelcerro
    private final JTextComponent txtFile;
36 1746 jjdelcerro
    private final File initialPath;
37
    private final String fileChooserID;
38
    private final String dialogTitle;
39
    private File value;
40
    private FileFilter filter;
41 1987 omartinez
42 1746 jjdelcerro
    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 1754 jjdelcerro
        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 1746 jjdelcerro
    }
58 1987 omartinez
59 2165 jjdelcerro
    public FilePickerControllerImpl(JTextComponent txtFile, JButton btnFile, String dialogTitle) {
60 1746 jjdelcerro
        this(txtFile, btnFile, dialogTitle, null, null, true);
61
    }
62 1987 omartinez
63 2165 jjdelcerro
    public FilePickerControllerImpl(JTextComponent txtFile, JButton btnFile) {
64 1746 jjdelcerro
        this(txtFile, btnFile, null, null, null, true);
65
    }
66
67 2165 jjdelcerro
    public FilePickerControllerImpl(JTextComponent txtFile, JButton btnFile, String dialogTitle, String fileChooserID, File initialPath, boolean seticon) {
68 1746 jjdelcerro
        this.value = null;
69
        this.txtFile = txtFile;
70
        this.btnFile = btnFile;
71
        this.fileChooserID = fileChooserID;
72
        this.initialPath = initialPath;
73 1987 omartinez
        if (dialogTitle == null) {
74 1746 jjdelcerro
            I18nManager i18n = ToolsLocator.getI18nManager();
75
            this.dialogTitle = i18n.getTranslation("_Select_file");
76
        } else {
77
            this.dialogTitle = dialogTitle;
78
        }
79 1987 omartinez
        if (seticon) {
80 1746 jjdelcerro
            this.btnFile.setIcon(this.getIcon("picker-file"));
81
            this.btnFile.setText("");
82
        }
83 2165 jjdelcerro
        this.btnFile.addActionListener((ActionEvent e) -> {
84
          doSelectFile();
85 1746 jjdelcerro
        });
86 2000 jjdelcerro
        ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager();
87
        toolsSwingManager.setDefaultPopupMenu(txtFile);
88 2165 jjdelcerro
        toolsSwingManager.addClearButton(txtFile, (ActionEvent e) -> {
89
          doClear();
90 1754 jjdelcerro
        });
91 1954 jjdelcerro
        //this.txtFile.setEditable(false);
92
        this.txtFile.addKeyListener(new KeyAdapter() {
93
            @Override
94
            public void keyPressed(KeyEvent e) {
95 1987 omartinez
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
96 1954 jjdelcerro
                    coerceAndSet(txtFile.getText());
97
                    fireChangeEvent();
98
                }
99
                super.keyPressed(e);
100
            }
101
        });
102 1746 jjdelcerro
    }
103
104 1754 jjdelcerro
    private void doClear() {
105
        this.set(null);
106
    }
107 1987 omartinez
108 1746 jjdelcerro
    @Override
109
    public boolean isEmpty() {
110 2027 omartinez
        this.get();
111 1987 omartinez
        return this.value == null;
112 1746 jjdelcerro
    }
113 1987 omartinez
114 1746 jjdelcerro
    @Override
115
    public File get() {
116 1987 omartinez
        coerceAndSet(txtFile.getText());
117 1746 jjdelcerro
        return this.value;
118
    }
119
120
    @Override
121
    public void set(File value) {
122
        this.value = value;
123 1987 omartinez
        if (value == null) {
124 1754 jjdelcerro
            this.txtFile.setText("");
125
        } else {
126
            this.txtFile.setText(value.toString());
127
        }
128 1746 jjdelcerro
    }
129
130
    @Override
131 1954 jjdelcerro
    public void setEditable(boolean editable) {
132
        super.setEditable(editable);
133
        this.txtFile.setEditable(editable);
134
    }
135 1987 omartinez
136 1954 jjdelcerro
    @Override
137 1746 jjdelcerro
    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 1987 omartinez
        if (!this.isEditable()) {
149 1746 jjdelcerro
            return;
150
        }
151
        ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager();
152
        FoldersManager folderManager = ToolsLocator.getFoldersManager();
153 1987 omartinez
        File pathToUse = initialPath;
154
        if (this.value != null) {
155 2027 omartinez
            pathToUse = this.value;
156 1987 omartinez
        }
157 1746 jjdelcerro
        File[] f = dialogs.showChooserDialog(
158 1987 omartinez
                this.dialogTitle,
159
                JFileChooser.OPEN_DIALOG,
160
                JFileChooser.FILES_ONLY,
161
                true,
162 1746 jjdelcerro
                folderManager.getLastPath(
163 1987 omartinez
                        this.fileChooserID,
164
                        pathToUse
165 1746 jjdelcerro
                ),
166 1987 omartinez
                this.filter,
167 1746 jjdelcerro
                true
168
        );
169 1987 omartinez
170
        if (ArrayUtils.isEmpty(f)) {
171 1746 jjdelcerro
            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 1987 omartinez
187 1746 jjdelcerro
}