Statistics
| Revision:

gvsig-3d / 2.0 / trunk / org.gvsig.gvsig3d.app / org.gvsig.gvsig3d.app.extension / src / main / java / org / gvsig / gvsig3d / app / gui / styling / PictureSymbolTab.java @ 376

History | View | Annotate | Download (11.7 KB)

1
package org.gvsig.gvsig3d.app.gui.styling;
2

    
3
import java.awt.BorderLayout;
4
import java.awt.Component;
5
import java.awt.Dimension;
6
import java.awt.Graphics2D;
7
import java.awt.GridBagConstraints;
8
import java.awt.GridBagLayout;
9
import java.awt.GridLayout;
10
import java.awt.Image;
11
import java.awt.Insets;
12
import java.awt.event.ActionEvent;
13
import java.awt.event.ActionListener;
14
import java.awt.image.BufferedImage;
15
import java.io.File;
16
import java.io.IOException;
17
import java.net.MalformedURLException;
18
import java.util.ArrayList;
19
import java.util.Arrays;
20
import java.util.Hashtable;
21
import java.util.Iterator;
22
import java.util.Map;
23

    
24
import javax.swing.DefaultComboBoxModel;
25
import javax.swing.ImageIcon;
26
import javax.swing.JButton;
27
import javax.swing.JComboBox;
28
import javax.swing.JFileChooser;
29
import javax.swing.JLabel;
30
import javax.swing.JPanel;
31
import javax.swing.JRadioButton;
32
import javax.swing.JScrollPane;
33
import javax.swing.JSpinner;
34
import javax.swing.JTabbedPane;
35
import javax.swing.JTextField;
36
import javax.swing.SpinnerNumberModel;
37
import javax.swing.border.TitledBorder;
38
import javax.swing.filechooser.FileFilter;
39

    
40
import org.gvsig.gvsig3d.impl.symbology3d.marker.impl.PictureMarker3DSymbol;
41
import org.gvsig.gvsig3d.symbology3d.I3DSymbol;
42
import org.gvsig.gvsig3d.symbology3d.marker.ISimpleMarker3DSymbol;
43

    
44
/**
45
 * @author AI2
46
 * @version $Id$
47
 * 
48
 */
49
public class PictureSymbolTab extends JPanel implements ISymbolSelectorTab {
50

    
51
        /**
52
         * 
53
         */
54
        private static final long serialVersionUID = -1094386262695367773L;
55

    
56
        private JTextField textField;
57

    
58
        private JPanel _picturePanel;
59

    
60
        private ISimpleMarker3DSymbol _symbol;
61
        private String _pictureURL = "";
62

    
63
        private JSpinner sizeSpinner;
64

    
65
        private JLabel background;
66

    
67
        private ImageIcon icon;
68

    
69
        private JPanel previewPanel;
70

    
71
        private JPanel panel;
72

    
73
        private JPanel panel_4;
74

    
75
        private JPanel previews;
76

    
77
        private Hashtable<JRadioButton, String> _radioMap = new Hashtable<JRadioButton, String>();
78

    
79
        private JComboBox combo;
80

    
81
        private String path;
82

    
83
        /**
84
         * Create the panel.
85
         */
86
        public PictureSymbolTab(Component parent) {
87
                setLayout(new BorderLayout(0, 0));
88

    
89
                JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
90
                tabbedPane.setPreferredSize(new Dimension(180, 300));
91
                add(tabbedPane, BorderLayout.CENTER);
92

    
93
                panel = new JPanel();
94
                tabbedPane.addTab("Marker", null, panel, null);
95
                GridBagLayout gbl_panel = new GridBagLayout();
96
                gbl_panel.columnWidths = new int[] { 175 };
97
                gbl_panel.rowHeights = new int[] { 180, 70 };
98
                gbl_panel.columnWeights = new double[] { 0.0 };
99
                gbl_panel.rowWeights = new double[] { 0.0, 0.0 };
100
                panel.setLayout(gbl_panel);
101

    
102
                previewPanel = new JPanel();
103
                previewPanel.setBorder(new TitledBorder(null, "Preview",
104
                                TitledBorder.LEADING, TitledBorder.TOP, null, null));
105
                GridBagConstraints gbc_panel_2 = new GridBagConstraints();
106
                gbc_panel_2.insets = new Insets(0, 0, 5, 5);
107
                gbc_panel_2.fill = GridBagConstraints.BOTH;
108
                gbc_panel_2.gridx = 0;
109
                gbc_panel_2.gridy = 0;
110
                panel.add(previewPanel, gbc_panel_2);
111
                previewPanel.setLayout(new BorderLayout(0, 0));
112

    
113
                icon = new ImageIcon("");
114
                background = new JLabel(icon);
115
                previewPanel.add(background, BorderLayout.CENTER);
116

    
117
                _picturePanel = new JPanel();
118
                previewPanel.add(_picturePanel, BorderLayout.CENTER);
119
                _picturePanel.setLayout(new BorderLayout(0, 0));
120
                _picturePanel.add(new JLabel(scale(icon.getImage())),
121
                                BorderLayout.CENTER);
122

    
123
                JPanel propertiesPanel = new JPanel();
124
                propertiesPanel.setBorder(new TitledBorder(null, "Properties",
125
                                TitledBorder.LEADING, TitledBorder.TOP, null, null));
126
                GridBagConstraints gbc_propertiesPanel = new GridBagConstraints();
127
                gbc_propertiesPanel.fill = GridBagConstraints.HORIZONTAL;
128
                gbc_propertiesPanel.ipady = 10;
129
                gbc_propertiesPanel.ipadx = 10;
130
                gbc_propertiesPanel.insets = new Insets(0, 0, 0, 5);
131
                gbc_propertiesPanel.anchor = GridBagConstraints.NORTHWEST;
132
                gbc_propertiesPanel.gridx = 0;
133
                gbc_propertiesPanel.gridy = 1;
134
                panel.add(propertiesPanel, gbc_propertiesPanel);
135
                propertiesPanel.setLayout(new GridLayout(0, 3, 10, 10));
136

    
137
                JLabel lblNewLabel_1 = new JLabel("Path:");
138
                propertiesPanel.add(lblNewLabel_1);
139

    
140
                textField = new JTextField();
141
                textField.setEditable(false);
142
                // textField.setText(_nodeURL);
143
                propertiesPanel.add(textField);
144
                textField.setColumns(10);
145

    
146
                JButton btnBrowse = new JButton("Browse");
147
                btnBrowse.addActionListener(new ActionListener() {
148
                        public void actionPerformed(ActionEvent e) {
149

    
150
                                JFileChooser fc = new JFileChooser();
151
                                FileFilter ff = new TypeFileFilter();
152
                                fc.setFileFilter(ff);
153

    
154
                                ImagePreviewPanel preview = new ImagePreviewPanel();
155
                                fc.setAccessory(preview);
156
                                fc.addPropertyChangeListener(preview);
157

    
158
                                int returnVal = fc.showDialog(PictureSymbolTab.this, "Accept");
159

    
160
                                if (returnVal == JFileChooser.APPROVE_OPTION) {
161
                                        File file = fc.getSelectedFile();
162
                                        _pictureURL = file.getAbsolutePath();
163
                                        // Aqui refrescar la imagen en el jpanel
164
                                        textField.setText(_pictureURL);
165
                                        icon = new ImageIcon(_pictureURL);
166
                                        background = new JLabel(icon);
167
                                        _picturePanel.removeAll();
168
                                        _picturePanel.add(new JLabel(scale(icon.getImage())),
169
                                                        BorderLayout.CENTER);
170
                                        PictureSymbolTab.this.repaint();
171

    
172
                                } else {
173

    
174
                                }
175

    
176
                                // _symbol.setColor(newColor);
177
                                // _colorPanel.setBackground(newColor);
178
                                // _colorPanel.repaint();
179
                                // symbolPreviewPanel.repaint();
180
                                // ((SymbolSelector3D) _parent).setSymbol(_symbol);
181

    
182
                        }
183
                });
184
                propertiesPanel.add(btnBrowse);
185

    
186
                JLabel lblSize = new JLabel("Size:");
187
                propertiesPanel.add(lblSize);
188

    
189
                JPanel panel_3 = new JPanel();
190
                propertiesPanel.add(panel_3);
191
                panel_3.setLayout(new BorderLayout(0, 0));
192

    
193
                sizeSpinner = new JSpinner();
194
                sizeSpinner.setModel(new SpinnerNumberModel(32.0, 1.0, 1000.0, 1.0));
195
                panel_3.add(sizeSpinner);
196

    
197
                propertiesPanel.setPreferredSize(new Dimension(80, 80));
198

    
199
                panel_4 = new JPanel();
200
                tabbedPane.addTab("Collections", null, panel_4, null);
201
                GridBagLayout gbl_panel_4 = new GridBagLayout();
202
                gbl_panel_4.columnWidths = new int[] { 150 };
203
                gbl_panel_4.rowHeights = new int[] { 30, 220 };
204
                gbl_panel_4.columnWeights = new double[] { 0.0 };
205
                gbl_panel_4.rowWeights = new double[] { 0.0, 0.0 };
206
                panel_4.setLayout(gbl_panel_4);
207

    
208
                path = PictureSymbolTab.class.getResource("/markers").getPath();
209

    
210
                combo = new JComboBox();
211
                combo.addActionListener(new ActionListener() {
212
                        public void actionPerformed(ActionEvent arg0) {
213

    
214
                                String dir = (String) combo.getSelectedItem();
215
                                previews.removeAll();
216
                                _radioMap.clear();
217
                                fillPreviews(path + "/" + dir);
218
                                PictureSymbolTab.this.repaint();
219
                        }
220
                });
221
                File dir = new File(path);
222
                combo.setModel(new DefaultComboBoxModel(new String[] {}));
223
                GridBagConstraints gbc_combo = new GridBagConstraints();
224
                // gbc_combo.fill = GridBagConstraints.HORIZONTAL;
225
                gbc_combo.anchor = GridBagConstraints.WEST;
226
                gbc_combo.insets = new Insets(0, 0, 5, 5);
227
                gbc_combo.gridx = 0;
228
                gbc_combo.gridy = 0;
229
                panel_4.add(combo, gbc_combo);
230

    
231
                previews = new JPanel();
232
                GridBagLayout gbl_previews = new GridBagLayout();
233
                // gbl_previews.columnWidths = new int[] { 15, 60, 15, 60 };
234
                // gbl_previews.rowHeights = new int[] { 40, 40, 40, 40 };
235
                gbl_previews.columnWeights = new double[] {};
236
                // gbl_previews.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0 };
237
                GridBagConstraints gbc_previews = new GridBagConstraints();
238
                gbc_previews.fill = GridBagConstraints.BOTH;
239
                gbc_previews.anchor = GridBagConstraints.WEST;
240
                gbc_previews.insets = new Insets(0, 0, 5, 5);
241
                gbc_previews.gridx = 0;
242
                gbc_previews.gridy = 1;
243
                previews.setLayout(gbl_previews);
244

    
245
                JScrollPane scroll = new JScrollPane();
246
                scroll.setViewportView(previews);
247
                scroll.getVerticalScrollBar().setUnitIncrement(16);
248
                // scroll.setPreferredSize(new Dimension(180,300));
249
                panel_4.add(scroll, gbc_previews);
250

    
251
                fillPreviews(path + "/base");
252
                fillCombo(combo, path);
253

    
254
        }
255

    
256
        private void fillCombo(JComboBox combo, String path) {
257

    
258
                File dir = new File(path);
259

    
260
                if (dir.isDirectory()) {
261
                        File[] files = dir.listFiles();
262
                        Arrays.sort(files);
263
                        for (File child : files) {
264
                                if (".".equals(child.getName()) || "..".equals(child.getName())
265
                                                || !child.isDirectory()) {
266
                                        continue; // Ignore the self and parent aliases.
267
                                }
268

    
269
                                combo.addItem(child.getName());
270

    
271
                        }
272
                }
273

    
274
        }
275

    
276
        private void fillPreviews(String path) {
277

    
278
                File dir = new File(path);
279
                int xpos = 0, ypos = 2;
280

    
281
                if (dir.isDirectory()) {
282
                        for (File child : dir.listFiles()) {
283
                                if (".".equals(child.getName()) || "..".equals(child.getName())
284
                                                || child.isDirectory()) {
285
                                        continue; // Ignore the self and parent aliases.
286
                                }
287
                                JRadioButton radio = new JRadioButton("");
288
                                GridBagConstraints gbc_radio = new GridBagConstraints();
289
                                gbc_radio.anchor = GridBagConstraints.WEST;
290
                                gbc_radio.insets = new Insets(0, 0, 5, 5);
291
                                gbc_radio.gridx = xpos % 4;
292
                                gbc_radio.gridy = ypos / 2;
293
                                previews.add(radio, gbc_radio);
294

    
295
                                xpos++;
296

    
297
                                JPanel panel = new JPanel();
298
                                GridBagConstraints gbc_panel = new GridBagConstraints();
299
                                gbc_panel.fill = GridBagConstraints.BOTH;
300
                                gbc_panel.insets = new Insets(0, 0, 5, 5);
301
                                gbc_panel.gridx = xpos % 4;
302
                                gbc_panel.gridy = ypos / 2;
303

    
304
                                xpos++;
305
                                ypos++;
306

    
307
                                panel.setLayout(new BorderLayout(0, 0));
308
                                panel.add(new JLabel(new ImageIcon(child.getAbsolutePath())));
309
                                previews.add(panel, gbc_panel);
310

    
311
                                _radioMap.put(radio, child.getAbsolutePath());
312

    
313
                                radio.addActionListener(new ActionListener() {
314
                                        public void actionPerformed(ActionEvent arg0) {
315

    
316
                                                File file = new File(_radioMap.get(arg0.getSource()));
317

    
318
                                                _pictureURL = file.getName();
319

    
320
                                                System.out.println(_pictureURL);
321

    
322
                                                for (JRadioButton key : _radioMap.keySet()) {
323

    
324
                                                        if (key != arg0.getSource()) {
325
                                                                key.setSelected(false);
326
                                                        }
327

    
328
                                                }
329

    
330
                                        }
331
                                });
332

    
333
                                // System.out.println("A?adido icono: " + child.getName());
334

    
335
                        }
336
                }
337

    
338
        }
339

    
340
        public I3DSymbol getNewSymbol() {
341
                // TODO Auto-generated method stub
342
                _symbol = new PictureMarker3DSymbol();
343

    
344
                if (_pictureURL == null)
345
                        return null;
346

    
347
                if (panel.isVisible()) {
348
                        ((PictureMarker3DSymbol) _symbol).setPictureFileURL(_pictureURL);
349
                        ((PictureMarker3DSymbol) _symbol).setSize((Double) sizeSpinner
350
                                        .getValue());
351
                        System.out.println("PictureURL = " + _pictureURL);
352
                } else {
353

    
354
                        ((PictureMarker3DSymbol) _symbol).setPictureFileURL(_pictureURL);
355
                        ((PictureMarker3DSymbol) _symbol).setSize((Double) sizeSpinner
356
                                        .getValue());
357
                        System.out.println("PictureURL = " + _pictureURL);
358

    
359
                }
360
                return _symbol;
361
        }
362

    
363
        public void updatePanel(I3DSymbol symbol) {
364
                // TODO Auto-generated method stub
365
                _pictureURL = ((PictureMarker3DSymbol) symbol).getPictureFileURL();
366
                icon = new ImageIcon(_pictureURL);
367
                background = new JLabel(icon);
368
                _picturePanel.removeAll();
369
                _picturePanel.add(new JLabel(scale(icon.getImage())),
370
                                BorderLayout.CENTER);
371
                background.repaint();
372
                panel.repaint();
373
                sizeSpinner.setValue(((PictureMarker3DSymbol) symbol).getSize());
374
                // System.out.println("Setting text" + _pictureURL);
375
                textField.setText(_pictureURL);
376
                // loadFile();
377
        }
378

    
379
        public void saveParameters() {
380
                // TODO Auto-generated method stub
381

    
382
        }
383

    
384
        public class TypeFileFilter extends FileFilter {
385
                private final String[] okFileExtensions = new String[] { "jpg", "png",
386
                                "bmp", "jpeg", "tiff" };
387

    
388
                public boolean accept(File file) {
389
                        for (String extension : okFileExtensions) {
390
                                if (file.getName().toLowerCase().endsWith(extension)
391
                                                || file.isDirectory()) {
392
                                        return true;
393
                                }
394

    
395
                        }
396
                        return false;
397
                }
398

    
399
                @Override
400
                public String getDescription() {
401
                        // TODO Auto-generated method stub
402
                        return "Supported Image Types";
403
                }
404
        }
405

    
406
        private ImageIcon scale(Image src) {
407
                int w = 115;
408
                int h = 115;
409
                int type = BufferedImage.TYPE_INT_ARGB;
410
                BufferedImage dst = new BufferedImage(w, h, type);
411
                Graphics2D g2 = dst.createGraphics();
412
                g2.drawImage(src, 0, 0, w, h, this);
413
                g2.dispose();
414
                return new ImageIcon(dst);
415
        }
416

    
417
}