root / branches / v2_0_0_prep / extensions / org.gvsig.symbology.app / org.gvsig.symbology.app.importsymbols / src / main / java / org / gvsig / symbology / app / importsymbols / ImportPictureMarkerSymbolsPanel.java @ 38782
History | View | Annotate | Download (8.34 KB)
1 | 38782 | jjdelcerro | package org.gvsig.symbology.app.importsymbols; |
---|---|---|---|
2 | |||
3 | import java.awt.event.ActionEvent; |
||
4 | import java.awt.event.ActionListener; |
||
5 | import java.io.File; |
||
6 | import java.io.FilenameFilter; |
||
7 | import java.util.ArrayList; |
||
8 | import java.util.Arrays; |
||
9 | import java.util.List; |
||
10 | |||
11 | import javax.swing.ComboBoxModel; |
||
12 | import javax.swing.JFileChooser; |
||
13 | import javax.swing.JOptionPane; |
||
14 | import javax.swing.ListModel; |
||
15 | import javax.swing.SpinnerNumberModel; |
||
16 | import javax.swing.event.ListDataListener; |
||
17 | import javax.swing.event.ListSelectionEvent; |
||
18 | import javax.swing.event.ListSelectionListener; |
||
19 | import javax.swing.filechooser.FileFilter; |
||
20 | |||
21 | import org.apache.commons.io.FilenameUtils; |
||
22 | import org.gvsig.app.ApplicationLocator; |
||
23 | import org.gvsig.app.ApplicationManager; |
||
24 | import org.gvsig.symbology.app.importsymbols.ImportPictureMarkerSymbolsProcess.YesNoAsk; |
||
25 | import org.gvsig.tools.util.FolderSet; |
||
26 | import org.slf4j.Logger; |
||
27 | import org.slf4j.LoggerFactory; |
||
28 | |||
29 | public class ImportPictureMarkerSymbolsPanel extends ImportPictureMarkerSymbolsPanelLayout { |
||
30 | |||
31 | private static final long serialVersionUID = 1919826881234125305L; |
||
32 | |||
33 | private static Logger logger = LoggerFactory.getLogger(ImportPictureMarkerSymbolsPanel.class); |
||
34 | private ImportPictureMarkerSymbolsProcess process = null; |
||
35 | private ApplicationManager application;
|
||
36 | private List<File> selectedFiles = null; |
||
37 | |||
38 | public ImportPictureMarkerSymbolsPanel(ImportPictureMarkerSymbolsProcess process) {
|
||
39 | super();
|
||
40 | this.process = process;
|
||
41 | this.application = ApplicationLocator.getManager();
|
||
42 | initComponents(); |
||
43 | } |
||
44 | |||
45 | private void closeWindow() { |
||
46 | this.setVisible(false); |
||
47 | } |
||
48 | |||
49 | private class OverwriteItem { |
||
50 | private String label; |
||
51 | private YesNoAsk value;
|
||
52 | |||
53 | OverwriteItem(String label, YesNoAsk value) {
|
||
54 | this.label = label;
|
||
55 | this.value = value;
|
||
56 | } |
||
57 | public String toString() { |
||
58 | return this.label; |
||
59 | } |
||
60 | public YesNoAsk getValue() {
|
||
61 | return value;
|
||
62 | } |
||
63 | } |
||
64 | |||
65 | private class FileItem { |
||
66 | private File value; |
||
67 | |||
68 | FileItem(File value) {
|
||
69 | this.value = value;
|
||
70 | } |
||
71 | public String toString() { |
||
72 | return this.value.getName(); |
||
73 | } |
||
74 | public File getValue() { |
||
75 | return value;
|
||
76 | } |
||
77 | } |
||
78 | |||
79 | private void initComponents() { |
||
80 | // ------------------------------------------------------------
|
||
81 | // Image file names
|
||
82 | this.ctrlBrowseImagesToImport.addActionListener(new ActionListener() { |
||
83 | public void actionPerformed(ActionEvent arg0) { |
||
84 | doBrowseImagesToImport(); |
||
85 | } |
||
86 | |||
87 | }); |
||
88 | |||
89 | // ------------------------------------------------------------
|
||
90 | // Default symbol size
|
||
91 | this.ctrlDefaultSize.setValue(this.process.getSymbolSize()); |
||
92 | |||
93 | // ------------------------------------------------------------
|
||
94 | // Target folder name
|
||
95 | if( this.process.getTargetFolderName()!=null ) { |
||
96 | this.ctrltFolderName.setText(this.process.getTargetFolderName()); |
||
97 | } |
||
98 | |||
99 | // ------------------------------------------------------------
|
||
100 | // List of existing target folders
|
||
101 | FolderSet repo = this.process.getSymbolsRepository();
|
||
102 | final File[] folders = repo.listFiles(new FilenameFilter() { |
||
103 | public boolean accept(File arg0, String arg1) { |
||
104 | return arg0.isDirectory();
|
||
105 | } |
||
106 | }); |
||
107 | final List<FileItem> folderItems = new ArrayList<FileItem>(); |
||
108 | for( int i=0; i<folders.length; i ++ ) { |
||
109 | folderItems.add(new FileItem(folders[i]));
|
||
110 | } |
||
111 | ctrlExistingFolders.setModel(new ListModel() { |
||
112 | public void removeListDataListener(ListDataListener arg0) { |
||
113 | // Do nothing
|
||
114 | } |
||
115 | public int getSize() { |
||
116 | return folderItems.size();
|
||
117 | } |
||
118 | public Object getElementAt(int arg0) { |
||
119 | return folderItems.get(arg0);
|
||
120 | |||
121 | } |
||
122 | public void addListDataListener(ListDataListener arg0) { |
||
123 | // Do nothing
|
||
124 | } |
||
125 | }); |
||
126 | this.ctrlExistingFolders.addListSelectionListener(new ListSelectionListener() { |
||
127 | public void valueChanged(ListSelectionEvent arg0) { |
||
128 | if( !arg0.getValueIsAdjusting() ) {
|
||
129 | try {
|
||
130 | File file = ((FileItem)ctrlExistingFolders.getSelectedValue()).getValue();
|
||
131 | ctrltFolderName.setText(file.getName()); |
||
132 | } catch(Exception ex) { |
||
133 | logger.info("Error getting selected filename",ex);
|
||
134 | } |
||
135 | } |
||
136 | } |
||
137 | }); |
||
138 | |||
139 | // ------------------------------------------------------------
|
||
140 | // Overwrite options
|
||
141 | final OverwriteItem[] overwriteItems = new OverwriteItem[] { |
||
142 | new OverwriteItem(application.translate("_Ask_to_the user"), YesNoAsk.ASK), |
||
143 | new OverwriteItem(application.translate("_Dont_overwrite"), YesNoAsk.NO), |
||
144 | new OverwriteItem(application.translate("_Overwrite_always"), YesNoAsk.YES) |
||
145 | }; |
||
146 | this.ctrlOverwriteOptions.setModel(new ComboBoxModel() { |
||
147 | OverwriteItem selected = null;
|
||
148 | public void removeListDataListener(ListDataListener arg0) { |
||
149 | // Do nothing
|
||
150 | } |
||
151 | public int getSize() { |
||
152 | return overwriteItems.length;
|
||
153 | } |
||
154 | public Object getElementAt(int arg0) { |
||
155 | return overwriteItems[arg0];
|
||
156 | } |
||
157 | public void addListDataListener(ListDataListener arg0) { |
||
158 | // Do nothing
|
||
159 | } |
||
160 | public void setSelectedItem(Object arg0) { |
||
161 | this.selected = (OverwriteItem) arg0;
|
||
162 | } |
||
163 | public Object getSelectedItem() { |
||
164 | return this.selected; |
||
165 | } |
||
166 | }); |
||
167 | this.ctrlOverwriteOptions.setSelectedIndex(0); |
||
168 | |||
169 | // ------------------------------------------------------------
|
||
170 | // Import button
|
||
171 | this.ctrlImport.addActionListener(new ActionListener() { |
||
172 | public void actionPerformed(ActionEvent arg0) { |
||
173 | doImport(); |
||
174 | } |
||
175 | }); |
||
176 | |||
177 | // ------------------------------------------------------------
|
||
178 | // Cancel button
|
||
179 | this.ctrlCancel.addActionListener(new ActionListener() { |
||
180 | public void actionPerformed(ActionEvent arg0) { |
||
181 | doCancel(); |
||
182 | } |
||
183 | }); |
||
184 | |||
185 | |||
186 | } |
||
187 | |||
188 | private void doImport() { |
||
189 | if( this.ctrltFolderName.getText().trim().length()==0 ) { |
||
190 | application.messageDialog( |
||
191 | application.translate("_Select_a_folder_name"),
|
||
192 | this.process.getName(),
|
||
193 | JOptionPane.WARNING_MESSAGE);
|
||
194 | return;
|
||
195 | } |
||
196 | if( this.selectedFiles==null || this.selectedFiles.size()==0 ) { |
||
197 | application.messageDialog( |
||
198 | application.translate("_Select_images"),
|
||
199 | this.process.getName(),
|
||
200 | JOptionPane.WARNING_MESSAGE);
|
||
201 | return;
|
||
202 | } |
||
203 | this.process.setTargetFolderName(this.ctrltFolderName.getText()); |
||
204 | this.process.setSymbolSize( (( SpinnerNumberModel)this.ctrlDefaultSize.getModel()).getNumber().doubleValue()); |
||
205 | this.process.setOverwrite( ((OverwriteItem)this.ctrlOverwriteOptions.getSelectedItem()).getValue() ); |
||
206 | this.process.getSelectedImageFiles().clear();
|
||
207 | this.process.getSelectedImageFiles().addAll(selectedFiles);
|
||
208 | this.closeWindow();
|
||
209 | } |
||
210 | |||
211 | private void doCancel() { |
||
212 | this.process.cancelRequest();
|
||
213 | this.closeWindow();
|
||
214 | } |
||
215 | |||
216 | private List<File> fileChooser(String title, boolean multiselect, FileFilter filter) { |
||
217 | JFileChooser fc = new JFileChooser(); |
||
218 | fc.setFileFilter(filter); |
||
219 | fc.setMultiSelectionEnabled(multiselect); |
||
220 | int r = fc.showDialog(application.getRootComponent(), application.translate("_Select_symbols")); |
||
221 | if( r == JFileChooser.APPROVE_OPTION ) { |
||
222 | return Arrays.asList(fc.getSelectedFiles()); |
||
223 | } |
||
224 | return null; |
||
225 | } |
||
226 | |||
227 | private void doBrowseImagesToImport() { |
||
228 | List<File> files = fileChooser(application.translate("_Select_symbols"), true, new FileFilter() { |
||
229 | public String getDescription() { |
||
230 | StringBuffer buffer = new StringBuffer(); |
||
231 | List<String> extensions = new ArrayList<String>(); |
||
232 | extensions.addAll(process.getAllowedFileExtensions()); |
||
233 | int l=extensions.size();
|
||
234 | if( l>=2 ) { |
||
235 | for( int i=0; i<l-2; i++) { |
||
236 | buffer.append(extensions.get(i)); |
||
237 | buffer.append(", ");
|
||
238 | } |
||
239 | buffer.append( extensions.get(l-2));
|
||
240 | buffer.append(" & ");
|
||
241 | buffer.append( extensions.get(l-1));
|
||
242 | } else {
|
||
243 | buffer.append( extensions.get(0));
|
||
244 | } |
||
245 | buffer.append(" ");
|
||
246 | buffer.append(application.translate("_Images"));
|
||
247 | return buffer.toString();
|
||
248 | } |
||
249 | |||
250 | public boolean accept(File f) { |
||
251 | if (f.isDirectory()) {
|
||
252 | return true; |
||
253 | } |
||
254 | String extension = FilenameUtils.getExtension(f.getName()).toLowerCase();
|
||
255 | return extension != null && process.getAllowedFileExtensions().contains(extension); |
||
256 | } |
||
257 | }); |
||
258 | if( files != null ) { |
||
259 | this.selectedFiles = files;
|
||
260 | final List<FileItem> folderItems = new ArrayList<FileItem>(); |
||
261 | for( int i=0; i<files.size(); i ++ ) { |
||
262 | folderItems.add(new FileItem(files.get(i)));
|
||
263 | } |
||
264 | ctrlImagesToImport.setModel(new ListModel() { |
||
265 | public void removeListDataListener(ListDataListener arg0) { |
||
266 | // Do nothing
|
||
267 | } |
||
268 | public int getSize() { |
||
269 | return folderItems.size();
|
||
270 | } |
||
271 | public Object getElementAt(int arg0) { |
||
272 | return folderItems.get(arg0);
|
||
273 | |||
274 | } |
||
275 | public void addListDataListener(ListDataListener arg0) { |
||
276 | // Do nothing
|
||
277 | } |
||
278 | }); |
||
279 | } |
||
280 | } |
||
281 | } |