Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / preferencespage / SymbologyPage.java @ 28960

History | View | Annotate | Download (15.5 KB)

1 22981 vcaballero
package com.iver.cit.gvsig.gui.preferencespage;
2
3 22996 vcaballero
import java.awt.BorderLayout;
4 22981 vcaballero
import java.awt.Color;
5
import java.awt.Dimension;
6
import java.awt.FlowLayout;
7
import java.awt.Font;
8
import java.awt.GridBagLayout;
9 22996 vcaballero
import java.awt.GridLayout;
10 22981 vcaballero
import java.awt.event.ActionEvent;
11
import java.awt.event.ActionListener;
12
import java.io.File;
13
14 22996 vcaballero
import javax.swing.BoxLayout;
15 22981 vcaballero
import javax.swing.ImageIcon;
16
import javax.swing.JButton;
17 22996 vcaballero
import javax.swing.JCheckBox;
18 22981 vcaballero
import javax.swing.JLabel;
19
import javax.swing.JPanel;
20
import javax.swing.JSlider;
21
import javax.swing.JTextField;
22
import javax.swing.border.TitledBorder;
23
import javax.swing.event.ChangeEvent;
24
import javax.swing.event.ChangeListener;
25
import javax.swing.filechooser.FileFilter;
26
27 22996 vcaballero
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
28
import org.gvsig.gui.beans.swing.JBlank;
29 22981 vcaballero
import org.gvsig.gui.beans.swing.JComboBoxFontSizes;
30
import org.gvsig.gui.beans.swing.JComboBoxFonts;
31
import org.gvsig.gui.beans.swing.JFileChooser;
32
33
import com.iver.andami.PluginServices;
34
import com.iver.andami.preferences.AbstractPreferencePage;
35
import com.iver.andami.preferences.StoreException;
36 22996 vcaballero
import com.iver.cit.gvsig.fmap.core.FShape;
37 22981 vcaballero
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
38
import com.iver.cit.gvsig.gui.panels.ColorChooserPanel;
39 22996 vcaballero
import com.iver.cit.gvsig.project.documents.view.legend.gui.JSymbolPreviewButton;
40 22981 vcaballero
import com.iver.utiles.StringUtilities;
41
import com.iver.utiles.XMLEntity;
42
43
44
/**
45
 * This class extends AbstractPreferencesPage. This component is a preferences page for
46
 * symbology and allows select default fore color, fill color, font and size text and
47
 * the path where the images that compound symbols are located.
48
 *
49
 */
50
public class SymbologyPage extends AbstractPreferencePage{
51
52
        private static final long serialVersionUID = 1L;
53
54
        private ColorChooserPanel defaultColor = null;
55
        private ColorChooserPanel defaultFillColor = null;
56
        private JSlider jsDefaultSelectionAlpha = null;
57
        private JSlider jsFillSelelctionAlpha = null;
58
        protected String id;
59
        private boolean panelStarted = false;
60
        private JButton btnSelectProjectsFolder=null;
61
        private ActionListener btnFileChooserAction=null;
62
        private JTextField txtProjectsFolder=null;
63
        private JComboBoxFonts fonts= null;
64
        private JComboBoxFontSizes sizes= null;
65
        private ImageIcon icon=null;
66 22996 vcaballero
        private JCheckBox aleatoryFillColor;
67 22981 vcaballero
68
69
        private static final String SYMBOL_FORECOLOR_PROPERTY_NAME = "ForeColor";
70
        private static final String SYMBOL_FILLCOLOR_PROPERTY_NAME = "FillColor";
71
        private static final String SYMBOL_FONT_TYPE_PROPERTY_NAME = "FontType";
72
        private static final String SYMBOL_FONT_SIZE_PROPERTY_NAME = "FontSize";
73
        private static final String SYMBOL_IMAGE_FOLDER_PROPERTY_NAME = "ImageFolder";
74 22996 vcaballero
        private static final String SYMBOL_ALEATORY_FORECOLOR = "AleatoryForeColor";
75
        private static final String SYMBOL_ALEATORY_FILLCOLOR = "AleatoryFillColor";
76 22981 vcaballero
77
78
79
        public SymbologyPage(){
80
                super();
81
                id = this.getClass().getName();
82
        }
83
84
        // pending of a proposed refactor, don't erase
85
        public void persistPreferences() throws StoreException {
86
                File f;
87
                String path, propertyName;
88
                Color foreColor=defaultColor.getColor();
89
                Color fillColor=defaultFillColor.getColor();
90
                PluginServices ps = PluginServices.getPluginServices(this);
91
                XMLEntity xml = ps.getPersistentXML();
92
93
                xml.putProperty(SYMBOL_FORECOLOR_PROPERTY_NAME,
94
                                StringUtilities.color2String(foreColor));
95
96 22996 vcaballero
97 22981 vcaballero
                xml.putProperty(SYMBOL_FILLCOLOR_PROPERTY_NAME,
98
                                StringUtilities.color2String(fillColor));
99 22996 vcaballero
                xml.putProperty(SYMBOL_ALEATORY_FILLCOLOR,aleatoryFillColor.isSelected());
100 22981 vcaballero
101
                propertyName = SYMBOL_IMAGE_FOLDER_PROPERTY_NAME;
102
                path = txtProjectsFolder.getText();
103
104
                if (path.equals("")) {
105
                        if (xml.contains(propertyName)) {
106
                                xml.remove(propertyName);
107
                        }
108
                } else {
109
                        f = new File(path);
110
                        if (f.exists()) {
111
                                if (xml.contains(propertyName)) {
112
                                        xml.remove(propertyName);
113
                                }
114
                                xml.putProperty(propertyName, f.getAbsolutePath());
115
                        }
116
                }
117
118
                String nameFont=(String)fonts.getSelectedItem();
119
                Integer sizeFontAux =(Integer)sizes.getSelectedItem();
120
                int sizeFont=sizeFontAux.intValue();
121
122
                propertyName=SYMBOL_FONT_TYPE_PROPERTY_NAME;
123
                xml.putProperty(propertyName, nameFont);
124
125
                propertyName=SYMBOL_FONT_SIZE_PROPERTY_NAME;
126
                xml.putProperty(propertyName, sizeFont);
127
128
129
        }
130
131
        @Override
132
        public void setChangesApplied() {
133
                setChanged(false);
134
135
        }
136
137
        public void applyValuesFromPersistence() throws StoreException {
138
                PluginServices ps = PluginServices.getPluginServices(this);
139
                XMLEntity xml = ps.getPersistentXML();
140
                xml = ps.getPersistentXML();
141
142
143
144
                if(xml.contains(SYMBOL_FORECOLOR_PROPERTY_NAME)){
145
                //Color
146
                        Color color=StringUtilities.string2Color(xml.getStringProperty(SYMBOL_FORECOLOR_PROPERTY_NAME));
147
                        SymbologyFactory.DefaultSymbolColor=color;
148
                }else{
149
                        SymbologyFactory.DefaultSymbolColor=SymbologyFactory.FactoryDefaultSymbolColor;
150
                }
151
152
153
                //FillColor
154
                if(xml.contains(SYMBOL_FILLCOLOR_PROPERTY_NAME)){
155
                        Color colorFill=StringUtilities.string2Color(xml.getStringProperty(SYMBOL_FILLCOLOR_PROPERTY_NAME));
156
                        SymbologyFactory.DefaultFillSymbolColor=colorFill;
157
                }else{
158
                        SymbologyFactory.DefaultFillSymbolColor=SymbologyFactory.FactoryDefaultFillSymbolColor;
159
                }
160
161
                //Path
162
                if(xml.contains(SYMBOL_IMAGE_FOLDER_PROPERTY_NAME)){
163
                        SymbologyFactory.SymbolLibraryPath=xml.getStringProperty(SYMBOL_IMAGE_FOLDER_PROPERTY_NAME);
164
                }else{
165
                        SymbologyFactory.SymbolLibraryPath=SymbologyFactory.FactorySymbolLibraryPath;
166
                }
167
                //Font
168
                if(xml.contains(SYMBOL_FONT_TYPE_PROPERTY_NAME)){
169
                        int size=xml.getIntProperty(SYMBOL_FONT_SIZE_PROPERTY_NAME);
170
                        String type=xml.getStringProperty(SYMBOL_FONT_TYPE_PROPERTY_NAME);
171
                        Font font= new Font(type, Font.BOLD, size);
172
                        SymbologyFactory.DefaultTextFont=font;
173
                }else{
174
                        SymbologyFactory.DefaultTextFont=SymbologyFactory.FactoryDefaultTextFont;
175
                }
176
177
        }
178
179
        public String getID() {
180
                return id;
181
        }
182
183
        public ImageIcon getIcon() {
184 25607 vcaballero
                if (icon == null){
185
                        icon=PluginServices.getIconTheme().get("symbol-pref");
186
                }
187 22981 vcaballero
                return icon;
188
        }
189
190
        public JPanel getPanel() {
191
                if(panelStarted)return this;
192
                panelStarted=true;
193
                addComponent(new JLabel(" "));
194
195 22996 vcaballero
                GridBagLayoutPanel selectionDefaultColorPanel = new GridBagLayoutPanel();
196 22981 vcaballero
                selectionDefaultColorPanel.setBorder(new TitledBorder(PluginServices.getText(this, "default_color")));
197
                selectionDefaultColorPanel.setLayout(new GridBagLayout());
198
                selectionDefaultColorPanel.add(new JLabel(PluginServices.getText(this,"fill")));
199
                selectionDefaultColorPanel.add(defaultColor = new ColorChooserPanel());
200
201
                selectionDefaultColorPanel.add(new JLabel(PluginServices.getText(this,"alpha")));
202
                selectionDefaultColorPanel.add(jsDefaultSelectionAlpha = new JSlider(0,255));
203 22996 vcaballero
                selectionDefaultColorPanel.add(new JBlank(50,50));
204 22981 vcaballero
205
                jsDefaultSelectionAlpha.setPreferredSize(new Dimension(100,30));
206
                jsDefaultSelectionAlpha.addChangeListener(new ChangeListener(){
207
                        public void stateChanged(ChangeEvent e) {
208
                                defaultColor.setAlpha(((JSlider)e.getSource()).getValue());
209
                }});
210 22996 vcaballero
                addComponent(new JLabel(" "));
211 22981 vcaballero
                addComponent(selectionDefaultColorPanel);
212
213 22996 vcaballero
                GridBagLayoutPanel selectionFillColor = new GridBagLayoutPanel();
214 22981 vcaballero
                selectionFillColor.setBorder(new TitledBorder(PluginServices.getText(this, "default_fill_color")));
215
                selectionFillColor.setLayout(new GridBagLayout());
216
                selectionFillColor.add(new JLabel(PluginServices.getText(this,"fill")));
217
                selectionFillColor.add(defaultFillColor = new ColorChooserPanel());
218
219
                selectionFillColor.add(new JLabel(PluginServices.getText(this,"alpha")));
220
                selectionFillColor.add(jsFillSelelctionAlpha = new JSlider(0,255));
221
222
                jsFillSelelctionAlpha.setPreferredSize(new Dimension(100,30));
223
                jsFillSelelctionAlpha.addChangeListener(new ChangeListener(){
224
                        public void stateChanged(ChangeEvent e) {
225
                                defaultFillColor.setAlpha(((JSlider)e.getSource()).getValue());
226
                }});
227
228 22996 vcaballero
                selectionFillColor.add(new JBlank(50,50));
229
                selectionFillColor.add(aleatoryFillColor = new JCheckBox());
230
                selectionFillColor.add(new JLabel("   " + PluginServices.getText(this,"aleatory")));
231
232
233
                aleatoryFillColor.addActionListener(new ActionListener(){
234
235
                        public void actionPerformed(ActionEvent e) {
236
                                if(e.getSource() == aleatoryFillColor){
237
                                        defaultFillColor.setEnabled(!aleatoryFillColor.isSelected());
238
                                        jsFillSelelctionAlpha.setEnabled(!aleatoryFillColor.isSelected());
239
                                }
240
                        }
241
242
                });
243
244 22981 vcaballero
                addComponent(new JLabel(" "));
245
                addComponent(selectionFillColor);
246
247
                btnFileChooserAction = new ActionListener() {
248
                        public void actionPerformed(ActionEvent e) {
249
                                String path;
250
                                if (e.getSource().equals(btnSelectProjectsFolder)) {
251
                                        path = txtProjectsFolder.getText();
252
253
254
                                FileFilter def =  new FileFilter(){
255
                                        public boolean accept(File f) {
256
                                                return (f.isDirectory());
257
                                        }
258
259
                                        public String getDescription() {
260
                                                return null;
261
                                        }
262
                                };
263
264
                                File file = new File(path);
265
                                JFileChooser fc;
266
                                if (file.exists()) {
267
                                        fc = new JFileChooser("SYMBOLOGY_PREFERENCE_PAGE_FILECHOOSER", file);
268
                                } else {
269
                                        fc= new JFileChooser("SYMBOLOGY_PREFERENCE_PAGE_FILECHOOSER",JFileChooser.getLastPath("SYMBOLOGY_PREFERENCE_PAGE_FILECHOOSER", file));
270
                                }
271
272
273
                                fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
274
                fc.setMultiSelectionEnabled(false);
275
                fc.setAcceptAllFileFilterUsed(false);
276
                fc.addChoosableFileFilter(def);
277
                int result = fc.showOpenDialog(SymbologyPage.this);
278
279
                if (result == JFileChooser.APPROVE_OPTION && (file = fc.getSelectedFile()) != null)
280
                        if (e.getSource().equals(btnSelectProjectsFolder))
281
                                            txtProjectsFolder.setText(file.getAbsolutePath());
282
                }
283
                        }
284
285
                };
286
                btnSelectProjectsFolder = new JButton(PluginServices.getText(this, "browse"));
287
                btnSelectProjectsFolder.addActionListener(btnFileChooserAction);
288
289
                JPanel panelBrowser = new JPanel();
290
                panelBrowser.setBorder(new TitledBorder(PluginServices.getText(this, "folder_images")));
291
292
                panelBrowser.add(txtProjectsFolder = new JTextField(30));
293
                panelBrowser.add(btnSelectProjectsFolder);
294
295
                addComponent(panelBrowser);
296
297
                fonts= new JComboBoxFonts();
298
                sizes = new JComboBoxFontSizes();
299
300
                JPanel panelFont = new JPanel(new FlowLayout());
301
                panelFont.setBorder(new TitledBorder(PluginServices.getText(this, "default_font")));
302
                panelFont.add(fonts);
303
                panelFont.add(sizes);
304
305
                addComponent(panelFont);
306
                initializeValues();
307
                return this;
308
        }
309
310
        public String getTitle() {
311
                return PluginServices.getText(this, "symbology");
312
        }
313
        // pending of a refactoring do not delete (swap commented lines)
314
//        public void initializeComponents() {
315
        public void initializeValues() {
316
                if (!panelStarted) getPanel();
317
318
                PluginServices ps = PluginServices.getPluginServices(this);
319
                XMLEntity xml = ps.getPersistentXML();
320
321 22996 vcaballero
322 22981 vcaballero
                //Default Color
323
                if (xml.contains(SYMBOL_FORECOLOR_PROPERTY_NAME)) {
324
                        Color color=StringUtilities.string2Color(xml.getStringProperty(SYMBOL_FORECOLOR_PROPERTY_NAME));
325
                        defaultColor.setColor(color);
326
                        defaultColor.setAlpha(color.getAlpha());
327
                        jsDefaultSelectionAlpha.setValue(color.getAlpha());
328 22996 vcaballero
                        SymbologyFactory.DefaultSymbolColor = color;
329 22981 vcaballero
                }else{
330
                        Color color=SymbologyFactory.FactoryDefaultSymbolColor;
331
                        defaultColor.setColor(color);
332
                        defaultColor.setAlpha(color.getAlpha());
333
                        jsDefaultSelectionAlpha.setValue(color.getAlpha());
334 22996 vcaballero
                        SymbologyFactory.DefaultSymbolColor = color;
335 22981 vcaballero
                }
336
337 22996 vcaballero
                if(xml.contains(SYMBOL_ALEATORY_FILLCOLOR)){
338
                        aleatoryFillColor.setSelected(xml.getBooleanProperty(SYMBOL_ALEATORY_FILLCOLOR));
339
                        SymbologyFactory.DefaultAleatoryFillColor = aleatoryFillColor.isSelected();
340
                }else{
341
                        aleatoryFillColor.setSelected(SymbologyFactory.FactoryDefaultAleatoryFillColor);
342
                        SymbologyFactory.DefaultAleatoryFillColor = aleatoryFillColor.isSelected();
343
                }
344
345 22981 vcaballero
                //Fill Color
346
                if (xml.contains(SYMBOL_FILLCOLOR_PROPERTY_NAME)) {
347
                        Color color=StringUtilities.string2Color(xml.getStringProperty(SYMBOL_FILLCOLOR_PROPERTY_NAME));
348
                        defaultFillColor.setColor(color);
349
                        defaultFillColor.setAlpha(color.getAlpha());
350
                        jsFillSelelctionAlpha.setValue(color.getAlpha());
351 22996 vcaballero
                        SymbologyFactory.DefaultFillSymbolColor = color;
352 22981 vcaballero
                }else{
353
                        Color color=SymbologyFactory.FactoryDefaultFillSymbolColor;
354
                        defaultFillColor.setColor(color);
355
                        defaultFillColor.setAlpha(color.getAlpha());
356
                        jsFillSelelctionAlpha.setValue(color.getAlpha());
357 22996 vcaballero
                        SymbologyFactory.DefaultFillSymbolColor = color;
358 22981 vcaballero
                }
359
360 22996 vcaballero
                defaultFillColor.setEnabled(!aleatoryFillColor.isSelected());
361
                jsFillSelelctionAlpha.setEnabled(!aleatoryFillColor.isSelected());
362
363 22981 vcaballero
                //Path
364
                if (xml.contains(SYMBOL_IMAGE_FOLDER_PROPERTY_NAME)) {
365
                        String path=xml.getStringProperty(SYMBOL_IMAGE_FOLDER_PROPERTY_NAME);
366
                        txtProjectsFolder.setText(path);
367 22996 vcaballero
                        SymbologyFactory.SymbolLibraryPath = path;
368 22981 vcaballero
                }else{
369
                        txtProjectsFolder.setText(SymbologyFactory.FactorySymbolLibraryPath);
370 22996 vcaballero
                        SymbologyFactory.SymbolLibraryPath = SymbologyFactory.FactorySymbolLibraryPath;
371 22981 vcaballero
                }
372
373
                //Font Size
374
                if(xml.contains(SYMBOL_FONT_TYPE_PROPERTY_NAME)){
375
                        String type=xml.getStringProperty(SYMBOL_FONT_TYPE_PROPERTY_NAME);
376
                        fonts.setSelectedItem(type);
377
                }
378
                else{
379
                        String font =SymbologyFactory.FactoryDefaultTextFont.getFontName();
380
                        fonts.setSelectedItem(font);
381
                }
382
383
                if(xml.contains(SYMBOL_FONT_SIZE_PROPERTY_NAME)){
384
                        int size=xml.getIntProperty(SYMBOL_FONT_SIZE_PROPERTY_NAME);
385
                        sizes.setSelectedItem(size);
386
                }
387
                else{
388
                        int size =SymbologyFactory.FactoryDefaultTextFont.getSize();
389
                        sizes.setSelectedItem(size);
390
                }
391 22996 vcaballero
                SymbologyFactory.DefaultTextFont = new Font(fonts.getFont().getName(),fonts.getFont().getStyle(),(Integer) sizes.getSelectedItem());
392 22981 vcaballero
        }
393
394
        public void initializeDefaults() {
395
                defaultColor.setColor(SymbologyFactory.FactoryDefaultSymbolColor);
396
                jsDefaultSelectionAlpha.setValue(255);
397
398
                defaultFillColor.setColor(SymbologyFactory.FactoryDefaultFillSymbolColor);
399
                jsFillSelelctionAlpha.setValue(255);
400
401
                fonts.setSelectedItem(SymbologyFactory.FactoryDefaultTextFont.getFamily());
402
                sizes.setSelectedItem(SymbologyFactory.FactoryDefaultTextFont.getSize());
403
                txtProjectsFolder.setText(SymbologyFactory.FactorySymbolLibraryPath);
404 22996 vcaballero
405
                aleatoryFillColor.setSelected(SymbologyFactory.FactoryDefaultAleatoryFillColor);
406 22981 vcaballero
        }
407
408
        public boolean isValueChanged() {
409
                return super.hasChanged();
410
        }
411
412
        // pending of a refactoring, following method would be removed
413
        @Override
414
        public void storeValues() throws StoreException {
415
                setPropertiesFromPanel();
416
                persistPreferences();
417
        }
418
419
        private void setPropertiesFromPanel(){
420 22996 vcaballero
421 22981 vcaballero
                if(defaultColor.getColor()!=null){
422
                        Color color = defaultColor.getColor();
423
                        color = new Color(color.getRed(),color.getGreen(),color.getBlue(),jsDefaultSelectionAlpha.getValue());
424
                        SymbologyFactory.DefaultSymbolColor = color;
425
                }
426
427 22996 vcaballero
                if(aleatoryFillColor != null){
428
                        SymbologyFactory.DefaultAleatoryFillColor = aleatoryFillColor.isSelected();
429
                }
430 22981 vcaballero
                //Fill Color
431
                if (defaultFillColor.getColor()  !=  null) {
432
                        Color color = defaultFillColor.getColor();
433
                        color = new Color(color.getRed(),color.getGreen(),color.getBlue(),jsFillSelelctionAlpha.getValue());
434
                        SymbologyFactory.DefaultFillSymbolColor = color;
435
                }
436
437
                //Path
438
                if (txtProjectsFolder.getText() != null) {
439
                        SymbologyFactory.SymbolLibraryPath = txtProjectsFolder.getText();
440
                }
441
442
                //Font Size
443
                if(fonts.getFont() != null && sizes.getSelectedItem() != null){
444
                        Font font = fonts.getFont();
445
                        font= new Font(fonts.getFont().getFontName(), Font.BOLD, (Integer)sizes.getSelectedItem());
446
                        SymbologyFactory.DefaultTextFont = font;
447
                }
448
449
450
        }
451
452
}