Statistics
| Revision:

root / trunk / frameworks / _fwAndami / src / com / iver / andami / preferences / GenericDlgPreferences.java @ 6337

History | View | Annotate | Download (13.4 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.andami.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
import java.util.Set;
51

    
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 org.gvsig.gui.beans.swing.JButton;
63

    
64
import com.iver.andami.PluginServices;
65
import com.iver.andami.ui.mdiManager.View;
66
import com.iver.andami.ui.mdiManager.ViewInfo;
67
import com.iver.utiles.extensionPoints.ExtensionPoint;
68
import com.iver.utiles.extensionPoints.ExtensionPoints;
69
import com.iver.utiles.extensionPoints.ExtensionPointsSingleton;
70

    
71
/**
72
 * @author fjp
73
 *
74
 * The reason behind this class is to have the opportunity of use a DlgPreferences
75
 * in a local way. Then, you don't need to be a SingletonView.
76
 */
77
public class GenericDlgPreferences extends JPanel implements View {
78
        private ViewInfo viewInfo = null;
79
        private IPreference activePreference;
80

    
81
        private static Hashtable preferences = new Hashtable();
82
        DefaultTreeModel treeModel = null;
83

    
84
        private JTree jTreePlugins = null;
85

    
86
        private JPanel jPanelButtons = null;
87

    
88
        private JButton jButtonOK = null;
89

    
90
        private JButton jButtonCancel = null;
91
        private DefaultMutableTreeNode root = new DefaultMutableTreeNode();
92

    
93
        private JPanel jPanelCenter = null;
94

    
95
        private JLabel jLabelBigTitle = null;
96

    
97
        private JScrollPane jScrollPane = null;
98

    
99
        private JSplitPane jSplitPaneCenter = null;
100

    
101
        private JPanel jPanelContainer = null;
102
        private JButton jButtonRestore;
103

    
104
        private class MyTreeCellRenderer extends DefaultTreeCellRenderer
105
        {
106
            public MyTreeCellRenderer() {
107
            }
108

    
109
            public Component getTreeCellRendererComponent(
110
                                JTree tree,
111
                                Object value,
112
                                boolean sel,
113
                                boolean expanded,
114
                                boolean leaf,
115
                                int row,
116
                                boolean hasFocus) {
117

    
118
                super.getTreeCellRendererComponent(
119
                                tree, value, sel,
120
                                expanded, leaf, row,
121
                                hasFocus);
122
                if (value instanceof DefaultMutableTreeNode)
123
                {
124
                        DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
125
                        if (node.getUserObject() instanceof IPreference)
126
                        {
127
                                IPreference pref = (IPreference) node.getUserObject();
128
                                this.setText(pref.getTitle());
129
                        }
130
                }
131
                return this;
132
            }
133

    
134
        }
135

    
136

    
137
        public GenericDlgPreferences() {
138
                super();
139
                initialize();
140
        }
141

    
142
        private void initialize() {
143
                setLayout(new BorderLayout());
144
                setSize(new java.awt.Dimension(588,479));
145
                super.add(getJPanelButtons(), BorderLayout.SOUTH);
146
                setPreferredSize(new java.awt.Dimension(390,369));
147
                super.add(getJSplitPaneCenter(), java.awt.BorderLayout.CENTER);
148
                getJSplitPaneCenter().setLeftComponent(getJScrollPane());
149
                getJSplitPaneCenter().setRightComponent(getJPanelNorth());
150

    
151

    
152

    
153
                treeModel = new DefaultTreeModel(root);
154
                
155
                refreshTree();
156

    
157
        }
158
        
159
        private void refreshTree() {
160
                ExtensionPoints extensionPoints = 
161
                        ExtensionPointsSingleton.getInstance();
162
                
163
                ExtensionPoint extensionPoint =(ExtensionPoint)extensionPoints.get("AplicationPreferences");
164
                
165
                Iterator iterator = extensionPoint.keySet().iterator();
166
                while (iterator.hasNext()) {
167
                        try {
168
                                IPreference obj = (IPreference)extensionPoint.create((String)iterator.next());
169
                                this.addPreferencePage(obj);
170
                        } catch (InstantiationException e) {
171
                                // TODO Auto-generated catch block
172
                                e.printStackTrace();
173
                        } catch (IllegalAccessException e) {
174
                                // TODO Auto-generated catch block
175
                                e.printStackTrace();
176
                        } catch (ClassCastException e) {
177
                                // TODO Auto-generated catch block
178
                                e.printStackTrace();
179
                        }
180
                }
181
        }
182
        
183
        
184
        /**
185
         * It is very common to be confused with this method. But
186
         * the one you are looking for is addPreferencePage(IPreference)
187
         */
188
        public Component add(Component c) {
189
                //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");
190
                throw new Error("Do not use com.iver.cit.gvsig.gui.preferences.DlgPreferences.add(Component) register an extension point instead");
191
        }
192

    
193
        public Component add(Component c, int i) {
194
                return add(c);
195
        }
196

    
197
        public void add(Component c, Object o) {
198
                add(c);
199
        }
200

    
201
        public void add(Component c, Object o, int i) {
202
                add(c);
203
        }
204

    
205
        public ViewInfo getViewInfo() {
206
                if (viewInfo == null) {
207
                        viewInfo = new ViewInfo(ViewInfo.MODALDIALOG | ViewInfo.RESIZABLE | ViewInfo.PALETTE);
208
                        viewInfo.setTitle(PluginServices.getText(this,
209
                                        "Preferences"));
210
                        viewInfo.setWidth(this.getWidth()+8);
211
                        viewInfo.setHeight(this.getHeight());
212
                }
213
                return viewInfo;
214
        }
215

    
216
        /**
217
         * This method initializes jTreePlugins
218
         *
219
         * @return javax.swing.JTree
220
         */
221
        private JTree getJTreePlugins() {
222
                if (jTreePlugins == null) {
223
                        jTreePlugins = new JTree();
224
                        jTreePlugins.setRootVisible(false);
225
                        MyTreeCellRenderer treeCellRenderer = new MyTreeCellRenderer();
226
                        treeCellRenderer.setOpenIcon(null);
227
                        treeCellRenderer.setClosedIcon(null);
228
                        treeCellRenderer.setLeafIcon(null);
229

    
230

    
231

    
232
                        jTreePlugins.setCellRenderer(treeCellRenderer);
233
                        jTreePlugins.setShowsRootHandles(true);
234
                        jTreePlugins
235
                                        .addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
236
                                                public void valueChanged(javax.swing.event.TreeSelectionEvent e) {
237
                                                         DefaultMutableTreeNode node = (DefaultMutableTreeNode)
238
                                       jTreePlugins.getLastSelectedPathComponent();
239

    
240
                                                                 if (node == null) return;
241
                                                                 setActivePage((IPreference) node.getUserObject());
242
                                                }
243
                                        });
244
                        jTreePlugins.putClientProperty("JTree.linestyle", "Angled");
245
                }
246

    
247
                return jTreePlugins;
248
        }
249

    
250
        private void addPreferencePage(IPreference page)
251
        {
252
                preferences.put(page.getID(), page);
253
                page.initializeValues(); // init values from the last settings
254
                //page.storeValues();                 // apply them
255
                doInsertNode(page);
256
                getJTreePlugins().setModel(treeModel);
257
        }
258

    
259
        private DefaultMutableTreeNode findNode(String searchID)
260
        {
261
                Enumeration e = root.breadthFirstEnumeration();
262
                while (e.hasMoreElements())
263
                {
264
                        DefaultMutableTreeNode nodeAux = (DefaultMutableTreeNode) e.nextElement();
265
                        if (nodeAux != null)
266
                        {
267
                                IPreference pref = (IPreference) nodeAux.getUserObject();
268
                                if (pref == null) continue; // Nodo ra?z
269
                                if (pref.getID().equals(searchID))
270
                                {
271
                                        return nodeAux;
272
                                }
273
                        }
274
                }
275
                return null;
276

    
277
        }
278

    
279
        private void doInsertNode(IPreference page)
280
        {
281
                if (findNode(page.getID()) != null) // Ya est? incluido
282
                        return;
283
                if (page.getParentID() != null)
284
                {
285
                        if (preferences.containsKey(page.getParentID()))
286
                        {
287
                                IPreference parent = (IPreference) preferences.get(page.getParentID());
288
                                DefaultMutableTreeNode nodeParent = findNode(parent.getID());
289
                                if (nodeParent == null) // Todav?a no hemos a?adido al padre.
290
                                {
291
                                        // Lo a?adimos de manera recursiva
292
                                        doInsertNode(parent);
293
                                }
294
                                else
295
                                {
296
                                        DefaultMutableTreeNode nodeValue = new DefaultMutableTreeNode(page);
297
                                        treeModel.insertNodeInto(nodeValue, nodeParent, nodeParent.getChildCount());
298
                                }
299
                        }
300
                }
301
                else // Nodo de los de primera l?nea ("General", "Edicion", etc)
302
                {
303
                        DefaultMutableTreeNode nodeValue = new DefaultMutableTreeNode(page);
304
                        treeModel.insertNodeInto(nodeValue, root, root.getChildCount());
305
                }
306
        }
307

    
308
        /**
309
         * This method initializes jPanelButtons
310
         *
311
         * @return javax.swing.JPanel
312
         */
313
        private JPanel getJPanelButtons() {
314
                if (jPanelButtons == null) {
315
                        jPanelButtons = new JPanel(new BorderLayout());
316
                        JPanel jPanelAux = new JPanel();
317
                        JLabel l = new JLabel();
318
                        l.setPreferredSize(new Dimension(40, 20));
319
                        jPanelButtons.add(new JSeparator(JSeparator.HORIZONTAL), BorderLayout.NORTH);
320
                        jPanelAux.add(getJButtonRestore(), BorderLayout.WEST);
321
                        jPanelAux.add(l, BorderLayout.CENTER);
322
                        jPanelAux.add(getJButtonOK(), BorderLayout.EAST);
323
                        jPanelAux.add(getJButtonCancel(), BorderLayout.EAST);
324

    
325
                        jPanelButtons.add(jPanelAux);
326
                }
327
                return jPanelButtons;
328
        }
329

    
330
        /**
331
         * This method initializes jPanelButtons
332
         *
333
         * @return javax.swing.JPanel
334
         */
335

    
336

    
337
        /**
338
         * This method initializes jButtonOK
339
         *
340
         * @return JButton
341
         */
342
        private JButton getJButtonOK() {
343
                if (jButtonOK == null) {
344
                        jButtonOK = new JButton();
345
                        jButtonOK.setText(PluginServices.getText(this, "aceptar"));
346
                        jButtonOK.addActionListener(new ActionListener()
347
                                        {
348
                                                public void actionPerformed(java.awt.event.ActionEvent e) {
349
                                                        Iterator it = preferences.keySet().iterator();
350
                                                        boolean shouldClose = true;
351
                                                        while (it.hasNext()) {
352
                                                                IPreference preference = (IPreference) preferences.get(it.next());
353
                                                                if (!preference.storeValues())
354
                                                                        shouldClose = false;
355
                                                        }
356
                                                        if (shouldClose)
357
                                                                closeView();
358
                                                };
359
                                        });
360
                }
361
                return jButtonOK;
362
        }
363

    
364
        /**
365
         * This method initializes jButtonOK
366
         *
367
         * @return JButton
368
         */
369
        private JButton getJButtonRestore() {
370
                if (jButtonRestore == null) {
371
                        jButtonRestore = new JButton();
372
                        jButtonRestore.setText(PluginServices.getText(this, "restore_defaults"));
373
                        jButtonRestore.addActionListener(new ActionListener()
374
                                        {
375
                                                public void actionPerformed(java.awt.event.ActionEvent e) {
376
                                                        if (activePreference!=null)
377
                                                                activePreference.initializeDefaults();
378
                                                };
379
                                        });
380
                }
381
                return jButtonRestore;
382
        }
383

    
384
        private void closeView() {
385
                PluginServices.getMDIManager().closeView(this);
386
        }
387

    
388
        /**
389
         * This method initializes jButtonCancel
390
         *
391
         * @return JButton
392
         */
393
        private JButton getJButtonCancel() {
394
                if (jButtonCancel == null) {
395
                        jButtonCancel = new JButton();
396
                        jButtonCancel.setText(PluginServices.getText(this, "cancelar"));
397
                        jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
398
                                public void actionPerformed(java.awt.event.ActionEvent e) {                                        
399
                                        closeView();
400
                                }
401
                        });
402
                }
403
                return jButtonCancel;
404
        }
405

    
406
        /**
407
         * This method initializes jPanelNorth
408
         *
409
         * @return javax.swing.JPanel
410
         */
411
        private JPanel getJPanelNorth() {
412
                if (jPanelCenter == null) {
413
                        jLabelBigTitle = new JLabel();
414
                        jLabelBigTitle.setText("General");
415
                        jLabelBigTitle.setFont(new java.awt.Font("MS Sans Serif", java.awt.Font.BOLD, 14));
416
                        jLabelBigTitle.setHorizontalTextPosition(javax.swing.SwingConstants.TRAILING);
417
                        jLabelBigTitle.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
418
                        jPanelCenter = new JPanel();
419
                        JPanel jPanelPageTitle = new JPanel(new BorderLayout());
420
                        JPanel jPanelAux = new JPanel(new BorderLayout());
421
                        jPanelAux.add(jLabelBigTitle, java.awt.BorderLayout.NORTH);
422
                        jPanelPageTitle.add(jPanelAux, java.awt.BorderLayout.WEST);
423
                        jPanelPageTitle.add(new JSeparator(JSeparator.HORIZONTAL), BorderLayout.SOUTH);
424
                        jPanelCenter.setLayout(new BorderLayout());
425
                        jPanelCenter.add(jPanelPageTitle, BorderLayout.NORTH);
426
                        jPanelCenter.add(getJPanelContainer(), java.awt.BorderLayout.CENTER);
427

    
428
                }
429
                return jPanelCenter;
430
        }
431

    
432
        /**
433
         * This method initializes jScrollPane
434
         *
435
         * @return javax.swing.JScrollPane
436
         */
437
        private JScrollPane getJScrollPane() {
438
                if (jScrollPane == null) {
439
                        jScrollPane = new JScrollPane();
440
                        jScrollPane.setPreferredSize(new java.awt.Dimension(140,322));
441
                        jScrollPane.setViewportView(getJTreePlugins());
442
                }
443
                return jScrollPane;
444
        }
445

    
446
        /**
447
         * This method initializes jSplitPaneCenter
448
         *
449
         * @return javax.swing.JSplitPane
450
         */
451
        private JSplitPane getJSplitPaneCenter() {
452
                if (jSplitPaneCenter == null) {
453
                        jSplitPaneCenter = new JSplitPane();
454
                }
455
                return jSplitPaneCenter;
456
        }
457

    
458
        /**
459
         * This method initializes jPanelContainer
460
         *
461
         * @return javax.swing.JPanel
462
         */
463
        private JPanel getJPanelContainer() {
464
                if (jPanelContainer == null) {
465
                        jPanelContainer = new JPanel();
466
                }
467
                return jPanelContainer;
468
        }
469

    
470
        /**
471
         *
472
         */
473
        public void setActivePage(IPreference page) {
474
                activePreference = page;
475
                jLabelBigTitle.setText(activePreference.getTitle());
476
                JPanel prefPanel = activePreference.getPanel();
477
                jLabelBigTitle.setIcon(activePreference.getIcon());
478
                jPanelContainer.removeAll();
479
                jPanelContainer.add(prefPanel);
480
                prefPanel.setVisible(true);
481
                repaint();
482
        }
483

    
484

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