Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_2_Build_898 / frameworks / _fwAndami / src / com / iver / andami / preferences / GenericDlgPreferences.java @ 10513

History | View | Annotate | Download (16.6 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.ActionEvent;
47
import java.awt.event.ActionListener;
48
import java.util.ArrayList;
49
import java.util.Collection;
50
import java.util.Enumeration;
51
import java.util.Hashtable;
52
import java.util.Iterator;
53

    
54
import javax.swing.JButton;
55
import javax.swing.JLabel;
56
import javax.swing.JOptionPane;
57
import javax.swing.JPanel;
58
import javax.swing.JScrollPane;
59
import javax.swing.JSeparator;
60
import javax.swing.JSplitPane;
61
import javax.swing.JTree;
62
import javax.swing.tree.DefaultMutableTreeNode;
63
import javax.swing.tree.DefaultTreeCellRenderer;
64
import javax.swing.tree.DefaultTreeModel;
65
import javax.swing.tree.TreeModel;
66

    
67
import com.iver.andami.PluginServices;
68
import com.iver.andami.ui.mdiManager.IWindow;
69
import com.iver.andami.ui.mdiManager.WindowInfo;
70
import com.iver.utiles.extensionPoints.ExtensionPoint;
71
import com.iver.utiles.extensionPoints.ExtensionPoints;
72
import com.iver.utiles.extensionPoints.ExtensionPointsSingleton;
73

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

    
84
        private Hashtable preferences = new Hashtable();
85
        DefaultTreeModel treeModel = null;
86

    
87
        private JTree jTreePlugins = null;
88

    
89
        private JPanel jPanelButtons = null;
90

    
91
        private JButton jButtonOK = null;
92

    
93
        private JButton jButtonCancel = null;
94
        private DefaultMutableTreeNode root = new DefaultMutableTreeNode();
95

    
96
        private JPanel jPanelCenter = null;
97

    
98
        private JLabel jLabelBigTitle = null;
99

    
100
        private JScrollPane jScrollPane = null;
101

    
102
        private JSplitPane jSplitPaneCenter = null;
103

    
104
        private JPanel jPanelContainer = null;
105
        private JButton jButtonRestore;
106
        private ActionListener action;
107
        private boolean dirtyTree = false;
108

    
109
        private class MyTreeCellRenderer extends DefaultTreeCellRenderer
110
        {
111
            public MyTreeCellRenderer() {
112
            }
113

    
114
            public Component getTreeCellRendererComponent(
115
                                JTree tree,
116
                                Object value,
117
                                boolean sel,
118
                                boolean expanded,
119
                                boolean leaf,
120
                                int row,
121
                                boolean hasFocus) {
122

    
123
                super.getTreeCellRendererComponent(
124
                                tree, value, sel,
125
                                expanded, leaf, row,
126
                                hasFocus);
127
                if (value instanceof DefaultMutableTreeNode)
128
                {
129
                        DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
130
                        if (node.getUserObject() instanceof IPreference)
131
                        {
132
                                IPreference pref = (IPreference) node.getUserObject();
133
                                this.setText(pref.getTitle());
134
                        }
135
                }
136
                return this;
137
            }
138

    
139
        }
140

    
141

    
142
        public GenericDlgPreferences() {
143
                super();
144
                this.action = new ActionListener() {
145
                        public void actionPerformed(ActionEvent e) {
146
                                PluginServices.getMDIManager().setWaitCursor();
147
                                String actionCommand = e.getActionCommand();
148
                                if ("RESTORE".equals(actionCommand)) {
149
                                        // Restore default values in current page
150
                                        if (activePreference!=null) {
151
                                                activePreference.initializeDefaults();
152
                                                try {
153
                                                        activePreference.saveValues();
154
                                                } catch (StoreException sEx) {
155
                                                        /*
156
                                                         * If you reach this code you coded your page
157
                                                         * with wrong factory default values.
158
                                                         * Check them out.
159
                                                         */
160
                                                        PluginServices.getMDIManager().restoreCursor();
161

    
162
                                                        // Show error message
163
                                                        JOptionPane.showMessageDialog((Component) PluginServices.
164
                                                                        getMainFrame(),sEx.getMessage());
165

    
166
                                                }
167
                                        }
168
                                } else {
169
                                        Iterator it = preferences.keySet().iterator();
170

    
171
                                        if ("CANCEL".equals(actionCommand)) {
172
                                                // Restore previous values in all pages
173
                                                while (it.hasNext()) {
174
                                                        IPreference pref = (IPreference) preferences.get(it.next());
175
                                                        if (pref.isValueChanged())
176
                                                                pref.initializeValues(); //
177
                                                }
178
                                                closeView();
179
                                        } else if ("OK".equals(actionCommand)) {
180
                                                // Apply values in all pages
181
                                                boolean shouldClose = true;
182
                                                while (it.hasNext()) {
183
                                                        IPreference preference = (IPreference) preferences.get(it.next());
184
                                                        try{
185
                                                                if (preference.isValueChanged()) {
186
                                                                        preference.saveValues();
187
                                                                        preference.initializeValues();
188
                                                                }
189
                                                        }catch (StoreException ex) {
190
                                                                // Reach this code should mean that the page has wrong values
191
                                                                shouldClose = false;
192
                                                                PluginServices.getMDIManager().restoreCursor();
193

    
194
                                                                // Show error message
195
                                                                JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),ex.getMessage());
196

    
197
                                                                // Focus on error page
198
                                                                setActivePage(preference);
199
                                                        }
200
                                                }
201
                                                if (shouldClose)
202
                                                        closeView();
203
                                        }
204
                                }
205
                                PluginServices.getMDIManager().restoreCursor();
206
                        }
207
                };
208
                initialize();
209
        }
210

    
211
        private void initialize() {
212
                setLayout(new BorderLayout());
213
                setSize(new java.awt.Dimension(750,479));
214
                super.add(getJPanelButtons(), BorderLayout.SOUTH);
215
                setPreferredSize(new java.awt.Dimension(390,369));
216
                super.add(getJSplitPaneCenter(), java.awt.BorderLayout.CENTER);
217
                getJSplitPaneCenter().setLeftComponent(getJScrollPane());
218
                getJSplitPaneCenter().setRightComponent(getJPanelNorth());
219
                treeModel = new DefaultTreeModel(root);
220
        }
221

    
222
        public void refreshExtensionPoints() {
223
                ExtensionPoints extensionPoints =
224
                        ExtensionPointsSingleton.getInstance();
225

    
226
                ExtensionPoint extensionPoint =(ExtensionPoint)extensionPoints.get("AplicationPreferences");
227

    
228
                Iterator iterator = extensionPoint.keySet().iterator();
229
                while (iterator.hasNext()) {
230
                        try {
231
                                IPreference obj = (IPreference)extensionPoint.create((String)iterator.next());
232
                                this.addPreferencePage(obj);
233
                        } catch (InstantiationException e) {
234
                                e.printStackTrace();
235
                        } catch (IllegalAccessException e) {
236
                                e.printStackTrace();
237
                        } catch (ClassCastException e) {
238
                                e.printStackTrace();
239
                        }
240
                }
241
        }
242

    
243

    
244
        /**
245
         * It is very common to be confused with this method. But
246
         * the one you are looking for is addPreferencePage(IPreference)
247
         */
248
        public Component add(Component c) {
249
                //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");
250
                throw new Error("Do not use com.iver.cit.gvsig.gui.preferences.DlgPreferences.add(Component) register an extension point instead");
251
        }
252

    
253
        public Component add(Component c, int i) {
254
                return add(c);
255
        }
256

    
257
        public void add(Component c, Object o) {
258
                add(c);
259
        }
260

    
261
        public void add(Component c, Object o, int i) {
262
                add(c);
263
        }
264

    
265
        public WindowInfo getWindowInfo() {
266
                if (viewInfo == null) {
267
                        viewInfo = new WindowInfo(WindowInfo.MODALDIALOG | WindowInfo.RESIZABLE | WindowInfo.PALETTE);
268
                        viewInfo.setTitle(PluginServices.getText(this,
269
                                        "Preferences"));
270
                        viewInfo.setWidth(this.getWidth()+8);
271
                        viewInfo.setHeight(this.getHeight());
272
                }
273
                return viewInfo;
274
        }
275

    
276
        /**
277
         * This method initializes jTreePlugins
278
         *
279
         * @return javax.swing.JTree
280
         */
281
        private JTree getJTreePlugins() {
282
                if (jTreePlugins == null) {
283
                        jTreePlugins = new JTree();
284
                        jTreePlugins.setRootVisible(false);
285
                        MyTreeCellRenderer treeCellRenderer = new MyTreeCellRenderer();
286
                        treeCellRenderer.setOpenIcon(null);
287
                        treeCellRenderer.setClosedIcon(null);
288
                        treeCellRenderer.setLeafIcon(null);
289

    
290
                        jTreePlugins.setCellRenderer(treeCellRenderer);
291
                        jTreePlugins.setShowsRootHandles(true);
292
                        jTreePlugins
293
                                        .addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
294
                                                public void valueChanged(javax.swing.event.TreeSelectionEvent e) {
295
                                                         DefaultMutableTreeNode node = (DefaultMutableTreeNode)
296
                                       jTreePlugins.getLastSelectedPathComponent();
297

    
298
                                                                 if (node == null) return;
299
                                                                 setActivePage((IPreference) node.getUserObject());
300
                                                }
301
                                        });
302
                        jTreePlugins.putClientProperty("JTree.linestyle", "Angled");
303
                }
304

    
305
                return jTreePlugins;
306
        }
307

    
308
        /**
309
         * It takes an IPreference page and adds it to the application's preferences
310
         * dialog. The preference page is added in alphabetical order within the
311
         * branch where the page is hanging on, and defined by its title.
312
         * @param page
313
         */
314
        public void addPreferencePage(IPreference page)
315
        {
316
                preferences.put(page.getID(), page);
317
                page.initializeValues(); // init values from the last settings
318
                //page.storeValues();                 // apply them
319
                if (dirtyTree) {
320
                        // rebuild page tree
321
                        dirtyTree = false;
322

    
323
                        ArrayList prefList = new ArrayList(preferences.values());
324
                        ArrayList alreadyAdded = new ArrayList();
325
                        DefaultTreeModel model = new DefaultTreeModel(root);
326
                        while (prefList.size()>0) {
327
                                IPreference pref = (IPreference) prefList.get(0);
328
                                while (pref.getParentID() != null &&
329
                                                !alreadyAdded.contains(preferences.get(pref.getParentID()))) {
330
                                        pref = (IPreference) preferences.get(pref.getParentID());
331
                                }
332
                                doInsertNode(model, pref);
333
                                prefList.remove(pref);
334
                                alreadyAdded.add(pref);
335
                        }
336
                        treeModel = model;
337
                        jTreePlugins.setModel(model);
338
                }
339

    
340
                doInsertNode(treeModel, page);
341
                getJTreePlugins().setModel(treeModel);
342
                getJTreePlugins().repaint();
343
        }
344

    
345
        private DefaultMutableTreeNode findNode(String searchID)
346
        {
347
                Enumeration e = root.breadthFirstEnumeration();
348
                while (e.hasMoreElements())
349
                {
350
                        DefaultMutableTreeNode nodeAux = (DefaultMutableTreeNode) e.nextElement();
351
                        if (nodeAux != null)
352
                        {
353
                                IPreference pref = (IPreference) nodeAux.getUserObject();
354
                                if (pref == null) continue; // Root node
355
                                if (pref.getID().equals(searchID))
356
                                {
357
                                        return nodeAux;
358
                                }
359
                        }
360
                }
361
                return null;
362

    
363
        }
364

    
365
        private void doInsertNode(DefaultTreeModel treeModel, IPreference page)
366
        {
367

    
368
                dirtyTree = ((page.getParentID() != null) && (findNode(page.getParentID())==null));
369
                if (findNode(page.getID()) != null) // It is already added
370
                        return;
371
                if (page.getParentID() != null)
372
                {
373
                        if (preferences.containsKey(page.getParentID()))
374
                        {
375
                                IPreference parent = (IPreference) preferences.get(page.getParentID());
376
                                DefaultMutableTreeNode nodeParent = findNode(parent.getID());
377
                                if (nodeParent == null) // the parent is empty
378
                                {
379
                                        // Recursively add it
380
                                        doInsertNode(treeModel, parent);
381
                                }
382
                                else
383
                                {
384
                                        DefaultMutableTreeNode nodeValue = new DefaultMutableTreeNode(page);
385
                                        int children = nodeParent.getChildCount();
386
                                        int pos=0;
387
                                        for (int i = 0; i < children; i++) {
388
                                                DefaultMutableTreeNode node = (DefaultMutableTreeNode) treeModel.getChild(nodeParent, i);
389
                                                if (node.getUserObject() instanceof IPreference) {
390
                                                        String pageTitle = ((IPreference) node.getUserObject()).getTitle();
391
                                                        if (pageTitle.compareTo(page.getTitle()) < 0) ++pos;
392
                                                }
393
                                        }
394
                                        treeModel.insertNodeInto(nodeValue, nodeParent, pos);
395
                                }
396
                        }
397
                }
398
                else // First level node ("General", "Edition")
399
                {
400
                        DefaultMutableTreeNode nodeValue = new DefaultMutableTreeNode(page);
401
                        int children = root.getChildCount();
402
                        int pos=0;
403
                        for (int i = 0; i < children; i++) {
404
                                DefaultMutableTreeNode node = (DefaultMutableTreeNode) treeModel.getChild(root, i);
405
                                if (node.getUserObject() instanceof IPreference) {
406
                                        String pageTitle = ((IPreference) node.getUserObject()).getTitle();
407
                                        if (pageTitle.compareTo(page.getTitle()) < 0) ++pos;
408
                                }
409
                        }
410
                        treeModel.insertNodeInto(nodeValue, root, pos);
411
                }
412
        }
413

    
414
        /**
415
         * This method initializes jPanelButtons
416
         *
417
         * @return javax.swing.JPanel
418
         */
419
        private JPanel getJPanelButtons() {
420
                if (jPanelButtons == null) {
421
                        jPanelButtons = new JPanel(new BorderLayout());
422
                        JPanel jPanelAux = new JPanel();
423
                        JLabel l = new JLabel();
424
                        l.setPreferredSize(new Dimension(40, 20));
425
                        jPanelButtons.add(new JSeparator(JSeparator.HORIZONTAL), BorderLayout.NORTH);
426
                        jPanelAux.add(getJButtonRestore(), BorderLayout.WEST);
427
                        jPanelAux.add(l, BorderLayout.CENTER);
428
                        jPanelAux.add(getJButtonOK(), BorderLayout.EAST);
429
                        jPanelAux.add(getJButtonCancel(), BorderLayout.EAST);
430

    
431
                        jPanelButtons.add(jPanelAux);
432
                }
433
                return jPanelButtons;
434
        }
435

    
436
        /**
437
         * This method initializes jPanelButtons
438
         *
439
         * @return javax.swing.JPanel
440
         */
441

    
442

    
443
        /**
444
         * This method initializes jButtonOK
445
         *
446
         * @return JButton
447
         */
448
        private JButton getJButtonOK() {
449
                if (jButtonOK == null) {
450
                        jButtonOK = new JButton();
451
                        jButtonOK.setText(PluginServices.getText(this, "aceptar"));
452
                        jButtonOK.setActionCommand("OK");
453
                        jButtonOK.addActionListener(action);
454
                }
455
                return jButtonOK;
456
        }
457

    
458
        /**
459
         * This method initializes jButtonOK
460
         *
461
         * @return JButton
462
         */
463
        private JButton getJButtonRestore() {
464
                if (jButtonRestore == null) {
465
                        jButtonRestore = new JButton();
466
                        jButtonRestore.setText(PluginServices.getText(this, "restore_defaults"));
467
                        jButtonRestore.setActionCommand("RESTORE");
468
                        jButtonRestore.addActionListener(action);
469
                }
470
                return jButtonRestore;
471
        }
472

    
473
        private void closeView() {
474
                PluginServices.getMDIManager().closeWindow(this);
475
        }
476

    
477
        /**
478
         * This method initializes jButtonCancel
479
         *
480
         * @return JButton
481
         */
482
        private JButton getJButtonCancel() {
483
                if (jButtonCancel == null) {
484
                        jButtonCancel = new JButton();
485
                        jButtonCancel.setText(PluginServices.getText(this, "cancelar"));
486
                        jButtonCancel.setActionCommand("CANCEL");
487
                        jButtonCancel.addActionListener(action);
488
                }
489
                return jButtonCancel;
490
        }
491

    
492
        /**
493
         * This method initializes jPanelNorth
494
         *
495
         * @return javax.swing.JPanel
496
         */
497
        private JPanel getJPanelNorth() {
498
                if (jPanelCenter == null) {
499
                        jLabelBigTitle = new JLabel();
500
                        jLabelBigTitle.setText("General");
501
                        jLabelBigTitle.setFont(new java.awt.Font("MS Sans Serif", java.awt.Font.BOLD, 14));
502
                        jLabelBigTitle.setHorizontalTextPosition(javax.swing.SwingConstants.TRAILING);
503
                        jLabelBigTitle.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
504
                        jPanelCenter = new JPanel();
505
                        JPanel jPanelPageTitle = new JPanel(new BorderLayout());
506
                        JPanel jPanelAux = new JPanel(new BorderLayout());
507
                        jPanelAux.add(jLabelBigTitle, java.awt.BorderLayout.NORTH);
508
                        jPanelPageTitle.add(jPanelAux, java.awt.BorderLayout.WEST);
509
                        jPanelPageTitle.add(new JSeparator(JSeparator.HORIZONTAL), BorderLayout.SOUTH);
510
                        jPanelCenter.setLayout(new BorderLayout());
511
                        jPanelCenter.add(jPanelPageTitle, BorderLayout.NORTH);
512
                        jPanelCenter.add(getJPanelContainer(), java.awt.BorderLayout.CENTER);
513

    
514
                }
515
                return jPanelCenter;
516
        }
517

    
518
        /**
519
         * This method initializes jScrollPane
520
         *
521
         * @return javax.swing.JScrollPane
522
         */
523
        private JScrollPane getJScrollPane() {
524
                if (jScrollPane == null) {
525
                        jScrollPane = new JScrollPane();
526
                        jScrollPane.setPreferredSize(new java.awt.Dimension(140,322));
527
                        jScrollPane.setViewportView(getJTreePlugins());
528
                }
529
                return jScrollPane;
530
        }
531

    
532
        /**
533
         * This method initializes jSplitPaneCenter
534
         *
535
         * @return javax.swing.JSplitPane
536
         */
537
        private JSplitPane getJSplitPaneCenter() {
538
                if (jSplitPaneCenter == null) {
539
                        jSplitPaneCenter = new JSplitPane();
540
                        jSplitPaneCenter.setResizeWeight(0.2);
541
                        jSplitPaneCenter.setDividerLocation(200);
542
                }
543
                return jSplitPaneCenter;
544
        }
545

    
546
        /**
547
         * This method initializes jPanelContainer
548
         *
549
         * @return javax.swing.JPanel
550
         */
551
        private JPanel getJPanelContainer() {
552
                if (jPanelContainer == null) {
553
                        jPanelContainer = new JPanel();
554
                }
555
                return jPanelContainer;
556
        }
557

    
558
        /**
559
         *
560
         */
561
        public void setActivePage(IPreference page) {
562
                activePreference = page;
563
                jLabelBigTitle.setText(activePreference.getTitle());
564
                JPanel prefPanel = activePreference.getPanel();
565
                jLabelBigTitle.setIcon(activePreference.getIcon());
566
                jPanelContainer.removeAll();
567
                jPanelContainer.add(prefPanel);
568
                prefPanel.setVisible(true);
569
                repaint();
570
        }
571
}