svn-gvsig-desktop / branches / v2_0_0_prep / extensions / org.gvsig.symbology.app / org.gvsig.symbology.app.importsymbols / src / main / java / org / gvsig / symbology / app / importsymbols / panel / ImportSymbolsPanel.java @ 37414
History | View | Annotate | Download (13.7 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
package org.gvsig.symbology.app.importsymbols.panel; |
23 |
|
24 |
import java.awt.Dimension; |
25 |
import java.awt.FlowLayout; |
26 |
import java.awt.event.ActionEvent; |
27 |
import java.beans.PropertyChangeListener; |
28 |
import java.io.File; |
29 |
import java.util.ArrayList; |
30 |
import java.util.HashSet; |
31 |
import java.util.Set; |
32 |
|
33 |
import javax.swing.Action; |
34 |
import javax.swing.JButton; |
35 |
import javax.swing.JFileChooser; |
36 |
import javax.swing.JLabel; |
37 |
import javax.swing.JPanel; |
38 |
import javax.swing.JTextField; |
39 |
import javax.swing.border.EmptyBorder; |
40 |
import javax.swing.filechooser.FileFilter; |
41 |
|
42 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
43 |
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager; |
44 |
import org.gvsig.symbology.app.importsymbols.wizard.ImportSymbolsWizard; |
45 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
46 |
import org.gvsig.tools.swing.api.usability.UsabilitySwingManager; |
47 |
|
48 |
/**
|
49 |
* @author gvSIG Team
|
50 |
* @version $Id$
|
51 |
*
|
52 |
*/
|
53 |
public class ImportSymbolsPanel extends JPanel { |
54 |
|
55 |
private static final long serialVersionUID = 8666781163497235054L; |
56 |
|
57 |
private static final Set<String> imageExtensionsAllowed; |
58 |
static {
|
59 |
imageExtensionsAllowed = new HashSet<String>(); |
60 |
imageExtensionsAllowed.add("png");
|
61 |
imageExtensionsAllowed.add("jpg");
|
62 |
imageExtensionsAllowed.add("jpeg");
|
63 |
imageExtensionsAllowed.add("gif");
|
64 |
imageExtensionsAllowed.add("bmp");
|
65 |
imageExtensionsAllowed.add("svg");
|
66 |
} |
67 |
|
68 |
ImportSymbolsWizard wizard; |
69 |
|
70 |
private JButton botonImportar; |
71 |
private JButton botonCerrar; |
72 |
private JButton botonExaminar; |
73 |
private JLabel labelOrigen; |
74 |
private JLabel labelDestino; |
75 |
private JTextField textFieldOrigen; |
76 |
private JTextField textFieldDestino; |
77 |
private JFileChooser fc = null; |
78 |
|
79 |
private File destinationFolder; |
80 |
|
81 |
private SelectSymbolsFolderImportPanel symbolsPanel;
|
82 |
|
83 |
public ImportSymbolsPanel(ImportSymbolsWizard wizard) {
|
84 |
super();
|
85 |
this.wizard = wizard;
|
86 |
initComponents(); |
87 |
createGUI(); |
88 |
} |
89 |
|
90 |
private void initComponents() { |
91 |
|
92 |
// primer seleccio de carpeta origen
|
93 |
labelOrigen = |
94 |
new JLabel("1) " + getText("_select_images_to_import") + ":"); |
95 |
|
96 |
// despres seleccio carpeta desti
|
97 |
labelDestino = |
98 |
new JLabel("2) " + getText("_select_destination_folder") + ":"); |
99 |
|
100 |
textFieldOrigen = new JTextField(20); |
101 |
textFieldDestino = new JTextField(20); |
102 |
|
103 |
symbolsPanel = new SelectSymbolsFolderImportPanel(this); |
104 |
} |
105 |
|
106 |
private void createGUI() { |
107 |
|
108 |
// java.awt.FlowLayout flowLayout;
|
109 |
// this.setLayout(new java.awt.FlowLayout());
|
110 |
|
111 |
java.awt.GridBagConstraints gridBagConstraints; |
112 |
this.setLayout(new java.awt.GridBagLayout()); |
113 |
|
114 |
// this.setSize(500,500);
|
115 |
|
116 |
// Add an empty margin
|
117 |
this.setBorder(new EmptyBorder(5, 5, 5, 5)); |
118 |
|
119 |
this.setBounds(3, 3, 2000, 2000); |
120 |
|
121 |
UsabilitySwingManager usabilitymanager = |
122 |
ToolsSwingLocator.getUsabilitySwingManager(); |
123 |
|
124 |
// JLabel layerText = new JLabel("_Select_a_layer:");
|
125 |
// JLabel transformText = new JLabel("_Select_a_transform:");
|
126 |
JPanel botones = new JPanel(); |
127 |
|
128 |
gridBagConstraints = new java.awt.GridBagConstraints();
|
129 |
// gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
|
130 |
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; |
131 |
gridBagConstraints.gridwidth = 2;
|
132 |
gridBagConstraints.gridx = 0;
|
133 |
gridBagConstraints.gridy = 0;
|
134 |
gridBagConstraints.weightx = 1.0;
|
135 |
gridBagConstraints.weighty = 0.0;
|
136 |
gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2); |
137 |
this.add(labelOrigen, gridBagConstraints);
|
138 |
|
139 |
gridBagConstraints = new java.awt.GridBagConstraints();
|
140 |
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; |
141 |
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; |
142 |
gridBagConstraints.gridx = 0;
|
143 |
gridBagConstraints.gridy = 1;
|
144 |
gridBagConstraints.weightx = 1.0;
|
145 |
gridBagConstraints.weighty = 0.0;
|
146 |
gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2); |
147 |
this.add(textFieldOrigen, gridBagConstraints);
|
148 |
|
149 |
gridBagConstraints = new java.awt.GridBagConstraints();
|
150 |
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; |
151 |
gridBagConstraints.gridx = 1;
|
152 |
gridBagConstraints.gridy = 1;
|
153 |
gridBagConstraints.weightx = 0.0;
|
154 |
gridBagConstraints.weighty = 0.0;
|
155 |
botonExaminar = usabilitymanager.createJButton(new ActionExamine());
|
156 |
botonExaminar.setText("_examinar");
|
157 |
gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2); |
158 |
this.add(botonExaminar, gridBagConstraints);
|
159 |
|
160 |
gridBagConstraints = new java.awt.GridBagConstraints();
|
161 |
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; |
162 |
gridBagConstraints.gridwidth = 2;
|
163 |
gridBagConstraints.gridx = 0;
|
164 |
gridBagConstraints.gridy = 2;
|
165 |
gridBagConstraints.weightx = 1.0;
|
166 |
gridBagConstraints.weighty = 0.0;
|
167 |
gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2); |
168 |
this.add(labelDestino, gridBagConstraints);
|
169 |
|
170 |
gridBagConstraints = new java.awt.GridBagConstraints();
|
171 |
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; |
172 |
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; |
173 |
gridBagConstraints.gridwidth = 2;
|
174 |
gridBagConstraints.gridx = 0;
|
175 |
gridBagConstraints.gridy = 3;
|
176 |
gridBagConstraints.weightx = 1.0;
|
177 |
gridBagConstraints.weighty = 0.0;
|
178 |
gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5); |
179 |
this.add(textFieldDestino, gridBagConstraints);
|
180 |
|
181 |
gridBagConstraints = new java.awt.GridBagConstraints();
|
182 |
// gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
|
183 |
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; |
184 |
gridBagConstraints.gridwidth = 2;
|
185 |
gridBagConstraints.gridx = 0;
|
186 |
gridBagConstraints.gridy = 4;
|
187 |
gridBagConstraints.weightx = 1.0;
|
188 |
gridBagConstraints.weighty = 1.0;
|
189 |
gridBagConstraints.insets = new java.awt.Insets(10, 2, 5, 2); |
190 |
this.add(symbolsPanel, gridBagConstraints);
|
191 |
|
192 |
botones.setLayout(new FlowLayout(FlowLayout.RIGHT)); |
193 |
botonImportar = usabilitymanager.createJButton(new ActionImport(this)); |
194 |
botonImportar.setText("_Import_simbols");
|
195 |
botonCerrar = usabilitymanager.createJButton(new ActionClose(this)); |
196 |
botonCerrar.setText("_Cancel");
|
197 |
botones.add(botonImportar); |
198 |
botones.add(botonCerrar); |
199 |
botonImportar.setEnabled(false);
|
200 |
|
201 |
gridBagConstraints = new java.awt.GridBagConstraints();
|
202 |
gridBagConstraints.gridx = 0;
|
203 |
gridBagConstraints.gridy = 5;
|
204 |
gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHEAST; |
205 |
gridBagConstraints.gridwidth = 2;
|
206 |
gridBagConstraints.weightx = 1.0;
|
207 |
gridBagConstraints.weighty = 0.0;
|
208 |
gridBagConstraints.insets = new java.awt.Insets(10, 2, 2, 2); |
209 |
this.add(botones, gridBagConstraints);
|
210 |
|
211 |
botonImportar.setEnabled(false);
|
212 |
|
213 |
updatePanel(); |
214 |
} |
215 |
|
216 |
private void showFileChooser() { |
217 |
// Create a file chooser
|
218 |
fc = new JFileChooser(); |
219 |
// Component parent = null;
|
220 |
|
221 |
// the fileChooser will only accept jpg, gif, png, jpeg, bmp and svg
|
222 |
// image formats
|
223 |
fc.setFileFilter(new FileFilter() { |
224 |
|
225 |
/*
|
226 |
* Get the extension of a file.
|
227 |
*/
|
228 |
public String getExtension(File f) { |
229 |
String ext = null; |
230 |
String s = f.getName();
|
231 |
int i = s.lastIndexOf('.'); |
232 |
|
233 |
if (i > 0 && i < s.length() - 1) { |
234 |
ext = s.substring(i + 1).toLowerCase();
|
235 |
} |
236 |
return ext;
|
237 |
} |
238 |
|
239 |
@Override
|
240 |
public String getDescription() { |
241 |
return "JPG, GIF, PNG, JPEG, BMP & SVG Images"; |
242 |
} |
243 |
|
244 |
@Override
|
245 |
public boolean accept(File f) { |
246 |
if (f.isDirectory()) {
|
247 |
return true; |
248 |
} |
249 |
|
250 |
String extension = getExtension(f);
|
251 |
return extension != null |
252 |
&& imageExtensionsAllowed.contains(extension); |
253 |
} |
254 |
}); |
255 |
|
256 |
fc.setMultiSelectionEnabled(true);
|
257 |
|
258 |
// fc.showDialog(parent, getText("_Import_symbols"));
|
259 |
fc.showDialog(getParent(), getText("_Select_symbols"));
|
260 |
|
261 |
} |
262 |
|
263 |
public ArrayList<File> getSelectedImages() { |
264 |
ArrayList<File> selectedImagesList = new ArrayList<File>(); |
265 |
|
266 |
// TODO: comprobar que passa quan no se selecciona cap arxiu
|
267 |
for (int i = 0; i < fc.getSelectedFiles().length; i++) { |
268 |
selectedImagesList.add(fc.getSelectedFiles()[i]); |
269 |
} |
270 |
return selectedImagesList;
|
271 |
} |
272 |
|
273 |
@Override
|
274 |
public Dimension getPreferredSize() { |
275 |
return new Dimension(250, 300); |
276 |
} |
277 |
|
278 |
private class ActionExamine implements Action { |
279 |
|
280 |
public void addPropertyChangeListener(PropertyChangeListener arg0) { |
281 |
} |
282 |
|
283 |
public Object getValue(String arg0) { |
284 |
return null; |
285 |
} |
286 |
|
287 |
public boolean isEnabled() { |
288 |
return true; |
289 |
} |
290 |
|
291 |
public void putValue(String arg0, Object arg1) { |
292 |
} |
293 |
|
294 |
public void removePropertyChangeListener(PropertyChangeListener arg0) { |
295 |
} |
296 |
|
297 |
public void setEnabled(boolean arg0) { |
298 |
} |
299 |
|
300 |
public void actionPerformed(ActionEvent arg0) { |
301 |
showFileChooser(); |
302 |
|
303 |
// set textFieldOrigen with the selected images
|
304 |
ArrayList<File> selectedImagesList = getSelectedImages(); |
305 |
|
306 |
// comprobar si s'ha seleccionat alguna imatge...
|
307 |
// if (selectedImagesList != null) {
|
308 |
String textOrigen = null; |
309 |
for (int i = 0; i < selectedImagesList.size(); i++) { |
310 |
if (textOrigen == null) { |
311 |
textOrigen = selectedImagesList.get(i).getName(); |
312 |
} else {
|
313 |
textOrigen += ", " + selectedImagesList.get(i).getName();
|
314 |
} |
315 |
} |
316 |
// }
|
317 |
textFieldOrigen.setText(textOrigen); |
318 |
updatePanel(); |
319 |
} |
320 |
|
321 |
} |
322 |
|
323 |
private class ActionImport implements Action { |
324 |
|
325 |
private ImportSymbolsPanel parent;
|
326 |
|
327 |
public ActionImport(ImportSymbolsPanel parent) {
|
328 |
this.parent = parent;
|
329 |
} |
330 |
|
331 |
public void addPropertyChangeListener(PropertyChangeListener listener) { |
332 |
} |
333 |
|
334 |
public Object getValue(String key) { |
335 |
return null; |
336 |
} |
337 |
|
338 |
public boolean isEnabled() { |
339 |
return true; |
340 |
} |
341 |
|
342 |
public void putValue(String key, Object value) { |
343 |
} |
344 |
|
345 |
public void removePropertyChangeListener(PropertyChangeListener listener) { |
346 |
} |
347 |
|
348 |
public void setEnabled(boolean b) { |
349 |
} |
350 |
|
351 |
public void actionPerformed(ActionEvent e) { |
352 |
updatePanel(); |
353 |
wizard.addImages(); |
354 |
this.parent.wizard.setVisible(false); |
355 |
} |
356 |
|
357 |
} |
358 |
|
359 |
private class ActionClose implements Action { |
360 |
|
361 |
private ImportSymbolsPanel parent;
|
362 |
|
363 |
public ActionClose(ImportSymbolsPanel parent) {
|
364 |
this.parent = parent;
|
365 |
} |
366 |
|
367 |
public void addPropertyChangeListener(PropertyChangeListener listener) { |
368 |
} |
369 |
|
370 |
public Object getValue(String key) { |
371 |
return null; |
372 |
} |
373 |
|
374 |
public boolean isEnabled() { |
375 |
return true; |
376 |
} |
377 |
|
378 |
public void putValue(String key, Object value) { |
379 |
} |
380 |
|
381 |
public void removePropertyChangeListener(PropertyChangeListener listener) { |
382 |
} |
383 |
|
384 |
public void setEnabled(boolean b) { |
385 |
} |
386 |
|
387 |
public void actionPerformed(ActionEvent e) { |
388 |
this.parent.wizard.setVisible(false); |
389 |
} |
390 |
|
391 |
} |
392 |
|
393 |
public void updatePanel() { |
394 |
|
395 |
SymbolManager manager = MapContextLocator.getSymbolManager(); |
396 |
File symbolsFolder =
|
397 |
new File(manager.getSymbolPreferences().getSymbolLibraryPath()); |
398 |
|
399 |
// update destination folder
|
400 |
if (textFieldDestino.getText().equals("")) { |
401 |
destinationFolder = symbolsFolder; |
402 |
} else {
|
403 |
destinationFolder = |
404 |
new File(symbolsFolder + "/" + textFieldDestino.getText()); |
405 |
} |
406 |
|
407 |
if (fc != null) { |
408 |
if (fc.getSelectedFiles().length > 0) { |
409 |
botonImportar.setEnabled(true);
|
410 |
} else {
|
411 |
botonImportar.setEnabled(false);
|
412 |
} |
413 |
} else {
|
414 |
botonImportar.setEnabled(false);
|
415 |
} |
416 |
|
417 |
textFieldOrigen.getText(); |
418 |
|
419 |
} |
420 |
|
421 |
private String getText(String msg) { |
422 |
return msg;
|
423 |
} |
424 |
|
425 |
public void setTextFieldFolder(String selectedFile) { |
426 |
textFieldDestino.setText(selectedFile); |
427 |
} |
428 |
|
429 |
public File getDestinationFolder() { |
430 |
return destinationFolder;
|
431 |
} |
432 |
|
433 |
} |