Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / preferences / DlgPreferences.java @ 5727

History | View | Annotate | Download (12.5 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.gui.preferences;
42

    
43
import java.awt.BorderLayout;
44
import java.awt.Component;
45
import java.awt.Dimension;
46
import java.awt.event.ActionListener;
47
import java.util.Enumeration;
48
import java.util.Hashtable;
49
import java.util.Iterator;
50

    
51
import javax.swing.JButton;
52
import javax.swing.JLabel;
53
import javax.swing.JPanel;
54
import javax.swing.JScrollPane;
55
import javax.swing.JSeparator;
56
import javax.swing.JSplitPane;
57
import javax.swing.JTree;
58
import javax.swing.tree.DefaultMutableTreeNode;
59
import javax.swing.tree.DefaultTreeCellRenderer;
60
import javax.swing.tree.DefaultTreeModel;
61

    
62
import com.iver.andami.PluginServices;
63
import com.iver.andami.ui.mdiManager.SingletonView;
64
import com.iver.andami.ui.mdiManager.ViewInfo;
65

    
66
public class DlgPreferences extends JPanel implements SingletonView {
67
        private ViewInfo viewInfo = null;
68
        private IPreference activePreference;
69

    
70
        private static DlgPreferences singletonInstance = null;
71
        
72
        private static Hashtable preferences = new Hashtable();
73
        DefaultTreeModel treeModel = null;
74

    
75
        private JTree jTreePlugins = null;
76

    
77
        private JPanel jPanelButtons = null;
78

    
79
        private JButton jButtonOK = null;
80

    
81
        private JButton jButtonCancel = null;
82
        private DefaultMutableTreeNode root = new DefaultMutableTreeNode();
83

    
84
        private JPanel jPanelCenter = null;
85

    
86
        private JLabel jLabelBigTitle = null;
87

    
88
        private JScrollPane jScrollPane = null;
89

    
90
        private JSplitPane jSplitPaneCenter = null;
91

    
92
        private JPanel jPanelContainer = null;
93
        private JButton jButtonRestore;
94
        
95
        private class MyTreeCellRenderer extends DefaultTreeCellRenderer
96
        {
97
            public MyTreeCellRenderer() {
98
            }
99

    
100
            public Component getTreeCellRendererComponent(
101
                                JTree tree,
102
                                Object value,
103
                                boolean sel,
104
                                boolean expanded,
105
                                boolean leaf,
106
                                int row,
107
                                boolean hasFocus) {
108

    
109
                super.getTreeCellRendererComponent(
110
                                tree, value, sel,
111
                                expanded, leaf, row,
112
                                hasFocus);
113
                if (value instanceof DefaultMutableTreeNode)
114
                {
115
                        DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
116
                        if (node.getUserObject() instanceof IPreference)
117
                        {
118
                                IPreference pref = (IPreference) node.getUserObject();
119
                                this.setText(pref.getTitle());
120
                        }
121
                }
122
                return this;
123
            }
124

    
125
        }
126

    
127
        public static DlgPreferences getInstance() {
128
                if (singletonInstance == null)
129
                        singletonInstance = new DlgPreferences();
130
                return singletonInstance;
131
        }
132

    
133
        DlgPreferences() {
134
                super();
135
                initialize();
136
        }
137

    
138
        private void initialize() {
139
                setLayout(new BorderLayout());
140
                setSize(new java.awt.Dimension(588,479));
141
                super.add(getJPanelButtons(), BorderLayout.SOUTH);
142
                setPreferredSize(new java.awt.Dimension(390,369));
143
                super.add(getJSplitPaneCenter(), java.awt.BorderLayout.CENTER);
144
                getJSplitPaneCenter().setLeftComponent(getJScrollPane());
145
                getJSplitPaneCenter().setRightComponent(getJPanelNorth());
146
                
147
                
148
                
149
                treeModel = new DefaultTreeModel(root);
150
                                
151
        }
152
        /**
153
         * It is very common to be confused with this method. But
154
         * the one you are looking for is addPreferencePage(IPreference)
155
         */
156
        public Component add(Component c) {
157
                throw new Error("Do not use com.iver.cit.gvsig.gui.preferences.DlgPreferences.add(Component) use com.iver.cit.gvsig.gui.preferences.DlgPreferences.addPreferencePage(IPreference) instead");
158
        }
159
        
160
        public Component add(Component c, int i) {
161
                return add(c);
162
        }
163
        
164
        public void add(Component c, Object o) {
165
                add(c);
166
        }
167
        
168
        public void add(Component c, Object o, int i) {
169
                add(c);
170
        }
171
        
172
        public ViewInfo getViewInfo() {
173
                if (viewInfo == null) {
174
                        viewInfo = new ViewInfo(ViewInfo.PALETTE | ViewInfo.RESIZABLE);
175
                        viewInfo.setTitle(PluginServices.getText(this,
176
                                        "Preferences"));
177
                        viewInfo.setWidth(this.getWidth()+8);
178
                        viewInfo.setHeight(this.getHeight());
179
                }
180
                return viewInfo;
181
        }
182

    
183
        /**
184
         * This method initializes jTreePlugins
185
         * 
186
         * @return javax.swing.JTree
187
         */
188
        private JTree getJTreePlugins() {
189
                if (jTreePlugins == null) {
190
                        jTreePlugins = new JTree();
191
                        jTreePlugins.setRootVisible(false);
192
                        MyTreeCellRenderer treeCellRenderer = new MyTreeCellRenderer();
193
                        treeCellRenderer.setOpenIcon(null);
194
                        treeCellRenderer.setClosedIcon(null);
195
                        treeCellRenderer.setLeafIcon(null);
196
                        
197
                        
198

    
199
                        jTreePlugins.setCellRenderer(treeCellRenderer);
200
                        jTreePlugins.setShowsRootHandles(true);
201
                        jTreePlugins
202
                                        .addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
203
                                                public void valueChanged(javax.swing.event.TreeSelectionEvent e) {
204
                                                         DefaultMutableTreeNode node = (DefaultMutableTreeNode)
205
                                       jTreePlugins.getLastSelectedPathComponent();
206

    
207
                                                                 if (node == null) return;
208
                                                                 activePreference = (IPreference) node.getUserObject();
209
                                                                 jLabelBigTitle.setText(activePreference.getTitle());
210
                                                                 JPanel prefPanel = activePreference.getPanel();
211
                                                                 jLabelBigTitle.setIcon(activePreference.getIcon());
212
                                                                 jPanelContainer.removeAll();
213
                                                                 jPanelContainer.add(prefPanel);
214
                                                                 prefPanel.setVisible(true);
215
                                                                 repaint();
216
                                                }
217
                                        });
218
                        jTreePlugins.putClientProperty("JTree.linestyle", "Angled"); 
219
                }
220
                        
221
                return jTreePlugins;
222
        }
223
        
224
        public void addPreferencePage(IPreference page)
225
        {
226
                preferences.put(page.getID(), page);
227
                page.initializeValues(); // init values from the last settings
228
                page.storeValues();                 // apply them
229
                doInsertNode(page);
230
                getJTreePlugins().setModel(treeModel);
231
        }
232
        
233
        private DefaultMutableTreeNode findNode(String searchID)
234
        {
235
                Enumeration e = root.breadthFirstEnumeration();
236
                while (e.hasMoreElements())
237
                {
238
                        DefaultMutableTreeNode nodeAux = (DefaultMutableTreeNode) e.nextElement();
239
                        if (nodeAux != null)
240
                        {
241
                                IPreference pref = (IPreference) nodeAux.getUserObject();
242
                                if (pref == null) continue; // Nodo ra?z
243
                                if (pref.getID().equals(searchID))
244
                                {
245
                                        return nodeAux;
246
                                }
247
                        }
248
                }
249
                return null;
250
                
251
        }
252
        
253
        private void doInsertNode(IPreference page)
254
        {
255
                if (findNode(page.getID()) != null) // Ya est? incluido
256
                        return;
257
                if (page.getParentID() != null)
258
                {
259
                        if (preferences.containsKey(page.getParentID()))
260
                        {
261
                                IPreference parent = (IPreference) preferences.get(page.getParentID());
262
                                DefaultMutableTreeNode nodeParent = findNode(parent.getID()); 
263
                                if (nodeParent == null) // Todav?a no hemos a?adido al padre.
264
                                {
265
                                        // Lo a?adimos de manera recursiva
266
                                        doInsertNode(parent);
267
                                }
268
                                else
269
                                {
270
                                        DefaultMutableTreeNode nodeValue = new DefaultMutableTreeNode(page);
271
                                        treeModel.insertNodeInto(nodeValue, nodeParent, nodeParent.getChildCount());
272
                                }
273
                        }
274
                }
275
                else // Nodo de los de primera l?nea ("General", "Edicion", etc)
276
                {
277
                        DefaultMutableTreeNode nodeValue = new DefaultMutableTreeNode(page);
278
                        treeModel.insertNodeInto(nodeValue, root, root.getChildCount());                        
279
                }
280
        }
281

    
282
        /**
283
         * This method initializes jPanelButtons
284
         * 
285
         * @return javax.swing.JPanel
286
         */
287
        private JPanel getJPanelButtons() {
288
                if (jPanelButtons == null) {
289
                        jPanelButtons = new JPanel(new BorderLayout());
290
                        JPanel jPanelAux = new JPanel();
291
                        JLabel l = new JLabel();
292
                        l.setPreferredSize(new Dimension(40, 20));
293
                        jPanelButtons.add(new JSeparator(JSeparator.HORIZONTAL), BorderLayout.NORTH);
294
                        jPanelAux.add(getJButtonRestore(), BorderLayout.WEST);
295
                        jPanelAux.add(l, BorderLayout.CENTER);
296
                        jPanelAux.add(getJButtonOK(), BorderLayout.EAST);
297
                        jPanelAux.add(getJButtonCancel(), BorderLayout.EAST);
298
                        
299
                        jPanelButtons.add(jPanelAux);
300
                }
301
                return jPanelButtons;
302
        }
303
        
304
        /**
305
         * This method initializes jPanelButtons
306
         * 
307
         * @return javax.swing.JPanel
308
         */
309
        
310

    
311
        /**
312
         * This method initializes jButtonOK
313
         * 
314
         * @return javax.swing.JButton
315
         */
316
        private JButton getJButtonOK() {
317
                if (jButtonOK == null) {
318
                        jButtonOK = new JButton();
319
                        jButtonOK.setText(PluginServices.getText(this, "aceptar"));
320
                        jButtonOK.addActionListener(new ActionListener()
321
                                        {
322
                                                public void actionPerformed(java.awt.event.ActionEvent e) {
323
                                                        Iterator it = preferences.keySet().iterator();
324
                                                        boolean shouldClose = true;
325
                                                        while (it.hasNext()) {
326
                                                                IPreference preference = (IPreference) preferences.get(it.next());
327
                                                                if (!preference.storeValues())
328
                                                                        shouldClose = false;
329
                                                        }
330
                                                        if (shouldClose)
331
                                                                closeView();
332
                                                };
333
                                        });
334
                }
335
                return jButtonOK;
336
        }
337
        
338
        /**
339
         * This method initializes jButtonOK
340
         * 
341
         * @return javax.swing.JButton
342
         */
343

    
344
        /**
345
         * This method initializes jButtonOK
346
         * 
347
         * @return javax.swing.JButton
348
         */
349
        private JButton getJButtonRestore() {
350
                if (jButtonRestore == null) {
351
                        jButtonRestore = new JButton();
352
                        jButtonRestore.setText(PluginServices.getText(this, "restore_defaults"));
353
                        jButtonRestore.addActionListener(new ActionListener()
354
                                        {
355
                                                public void actionPerformed(java.awt.event.ActionEvent e) {
356
                                                        if (activePreference!=null)
357
                                                                activePreference.initializeDefaults();
358
                                                };
359
                                        });
360
                }
361
                return jButtonRestore;
362
        }
363

    
364
        private void closeView() {
365
                PluginServices.getMDIManager().closeView(this);
366
        }
367

    
368
        /**
369
         * This method initializes jButtonCancel
370
         * 
371
         * @return javax.swing.JButton
372
         */
373
        private JButton getJButtonCancel() {
374
                if (jButtonCancel == null) {
375
                        jButtonCancel = new JButton();
376
                        jButtonCancel.setText(PluginServices.getText(this, "cancelar"));
377
                        jButtonCancel.addActionListener(new java.awt.event.ActionListener() { 
378
                                public void actionPerformed(java.awt.event.ActionEvent e) {    
379
                                        closeView();
380
                                }
381
                        });
382
                }
383
                return jButtonCancel;
384
        }
385

    
386
        /**
387
         * This method initializes jPanelNorth        
388
         *         
389
         * @return javax.swing.JPanel        
390
         */
391
        private JPanel getJPanelNorth() {
392
                if (jPanelCenter == null) {
393
                        jLabelBigTitle = new JLabel();
394
                        jLabelBigTitle.setText("General");
395
                        jLabelBigTitle.setFont(new java.awt.Font("MS Sans Serif", java.awt.Font.BOLD, 14));
396
                        jLabelBigTitle.setHorizontalTextPosition(javax.swing.SwingConstants.TRAILING);
397
                        jLabelBigTitle.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
398
                        jPanelCenter = new JPanel();
399
                        JPanel jPanelPageTitle = new JPanel(new BorderLayout());
400
                        JPanel jPanelAux = new JPanel(new BorderLayout());
401
                        jPanelAux.add(jLabelBigTitle, java.awt.BorderLayout.NORTH);
402
                        jPanelPageTitle.add(jPanelAux, java.awt.BorderLayout.WEST);
403
                        jPanelPageTitle.add(new JSeparator(JSeparator.HORIZONTAL), BorderLayout.SOUTH);
404
                        jPanelCenter.setLayout(new BorderLayout());
405
                        jPanelCenter.add(jPanelPageTitle, BorderLayout.NORTH);
406
                        jPanelCenter.add(getJPanelContainer(), java.awt.BorderLayout.CENTER);
407
                        
408
                }
409
                return jPanelCenter;
410
        }
411

    
412
        /**
413
         * This method initializes jScrollPane        
414
         *         
415
         * @return javax.swing.JScrollPane        
416
         */
417
        private JScrollPane getJScrollPane() {
418
                if (jScrollPane == null) {
419
                        jScrollPane = new JScrollPane();
420
                        jScrollPane.setPreferredSize(new java.awt.Dimension(140,322));
421
                        jScrollPane.setViewportView(getJTreePlugins());
422
                }
423
                return jScrollPane;
424
        }
425

    
426
        /**
427
         * This method initializes jSplitPaneCenter        
428
         *         
429
         * @return javax.swing.JSplitPane        
430
         */
431
        private JSplitPane getJSplitPaneCenter() {
432
                if (jSplitPaneCenter == null) {
433
                        jSplitPaneCenter = new JSplitPane();
434
                }
435
                return jSplitPaneCenter;
436
        }
437

    
438
        /**
439
         * This method initializes jPanelContainer        
440
         *         
441
         * @return javax.swing.JPanel        
442
         */
443
        private JPanel getJPanelContainer() {
444
                if (jPanelContainer == null) {
445
                        jPanelContainer = new JPanel();
446
                }
447
                return jPanelContainer;
448
        }
449

    
450
        public Object getViewModel() {
451
                return this.getClass();
452
        }
453

    
454
}  //  @jve:decl-index=0:visual-constraint="10,10"