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