Statistics
| Revision:

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

History | View | Annotate | Download (16.8 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
import javax.swing.tree.TreeSelectionModel;
67

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

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

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

    
88
        private JTree jTreePlugins = null;
89

    
90
        private JPanel jPanelButtons = null;
91

    
92
        private JButton jButtonOK = null;
93

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

    
97
        private JPanel jPanelCenter = null;
98

    
99
        private JLabel jLabelBigTitle = null;
100

    
101
        private JScrollPane jScrollPane = null;
102

    
103
        private JSplitPane jSplitPaneCenter = null;
104

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

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

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

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

    
140
        }
141

    
142

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

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

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

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

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

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

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

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

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

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

    
244

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

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

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

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

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

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

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

    
299
                                                                 if (node == null) return;
300
                                                                 setActivePage((IPreference) node.getUserObject());
301
                                                }
302
                                        });
303
                        jTreePlugins.putClientProperty("JTree.linestyle", "Angled");
304
                        jTreePlugins.getSelectionModel().
305
                                setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
306
                }
307

    
308
                return jTreePlugins;
309
        }
310

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

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

    
343
                doInsertNode(treeModel, page);
344
                getJTreePlugins().setModel(treeModel);
345
                getJTreePlugins().repaint();
346
        }
347

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

    
366
        }
367

    
368
        private void doInsertNode(DefaultTreeModel treeModel, IPreference page)
369
        {
370

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

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

    
434
                        jPanelButtons.add(jPanelAux);
435
                }
436
                return jPanelButtons;
437
        }
438

    
439
        /**
440
         * This method initializes jPanelButtons
441
         *
442
         * @return javax.swing.JPanel
443
         */
444

    
445

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

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

    
476
        private void closeView() {
477
                PluginServices.getMDIManager().closeWindow(this);
478
        }
479

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

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

    
517
                }
518
                return jPanelCenter;
519
        }
520

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

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

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

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