Statistics
| Revision:

root / tags / J2ME_compat_v1_2_Build_1209 / frameworks / _fwAndami / src / com / iver / andami / preferences / GenericDlgPreferences.java @ 19520

History | View | Annotate | Download (18.1 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.FlowLayout;
47
import java.awt.event.ActionEvent;
48
import java.awt.event.ActionListener;
49
import java.util.ArrayList;
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.TreeSelectionModel;
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
//        bug 240
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
                ArrayList prefList = new ArrayList(preferences.values());
243
                addPreferencePages(prefList);
244
        }
245
        //        TODO este m?todo es un parche provisional mientras se hace un refactoring de
246
        //las PreferencePages para disponer de un m?todo que inicialize las variables
247
        //a partir de las preferencias.
248
        public void storeValues() {
249
                ExtensionPoints extensionPoints =
250
                        ExtensionPointsSingleton.getInstance();
251

    
252
                ExtensionPoint extensionPoint =(ExtensionPoint)extensionPoints.get("AplicationPreferences");
253

    
254
                Iterator iterator = extensionPoint.keySet().iterator();
255
                while (iterator.hasNext()) {
256
                        try {
257
                                IPreference obj = (IPreference)extensionPoint.create((String)iterator.next());
258
                                this.addPreferencePage(obj);
259

    
260
                                {
261
                                        try {
262
                                                obj.saveValues();
263
                                        } catch (StoreException e) {
264
                                                e.printStackTrace();
265
                                        }
266
                                }
267
                        } catch (InstantiationException e) {
268
                                e.printStackTrace();
269
                        } catch (IllegalAccessException e) {
270
                                e.printStackTrace();
271
                        } catch (ClassCastException e) {
272
                                e.printStackTrace();
273
                        }
274
                }
275

    
276
        }
277

    
278
        /**
279
         * It is very common to be confused with this method. But
280
         * the one you are looking for is addPreferencePage(IPreference)
281
         */
282
        public Component add(Component c) {
283
                //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");
284
                throw new Error("Do not use com.iver.cit.gvsig.gui.preferences.DlgPreferences.add(Component) register an extension point instead");
285
        }
286

    
287
        public Component add(Component c, int i) {
288
                return add(c);
289
        }
290

    
291
        public void add(Component c, Object o) {
292
                add(c);
293
        }
294

    
295
        public void add(Component c, Object o, int i) {
296
                add(c);
297
        }
298

    
299
        public WindowInfo getWindowInfo() {
300
                if (viewInfo == null) {
301
                        viewInfo = new WindowInfo(WindowInfo.MODALDIALOG | WindowInfo.RESIZABLE | WindowInfo.PALETTE);
302
                        viewInfo.setTitle(PluginServices.getText(this,
303
                                        "Preferences"));
304
                        viewInfo.setWidth(this.getWidth()+8);
305
                        viewInfo.setHeight(this.getHeight());
306
                }
307
                return viewInfo;
308
        }
309

    
310
        /**
311
         * This method initializes jTreePlugins
312
         *
313
         * @return javax.swing.JTree
314
         */
315
        private JTree getJTreePlugins() {
316
                if (jTreePlugins == null) {
317
                        jTreePlugins = new JTree();
318
                        jTreePlugins.setRootVisible(false);
319
                        MyTreeCellRenderer treeCellRenderer = new MyTreeCellRenderer();
320
                        treeCellRenderer.setOpenIcon(null);
321
                        treeCellRenderer.setClosedIcon(null);
322
                        treeCellRenderer.setLeafIcon(null);
323

    
324
                        jTreePlugins.setCellRenderer(treeCellRenderer);
325
                        jTreePlugins.setShowsRootHandles(true);
326
                        jTreePlugins
327
                                        .addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
328
                                                public void valueChanged(javax.swing.event.TreeSelectionEvent e) {
329
                                                         DefaultMutableTreeNode node = (DefaultMutableTreeNode)
330
                                                                                                         jTreePlugins.getLastSelectedPathComponent();
331

    
332
                                                                 if (node == null) return;
333
                                                                 setActivePage((IPreference) node.getUserObject());
334
                                                }
335
                                        });
336
                        jTreePlugins.putClientProperty("JTree.linestyle", "Angled");
337
                        jTreePlugins.getSelectionModel().
338
                                setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
339
                }
340

    
341
                return jTreePlugins;
342
        }
343

    
344
        /**
345
         * It takes an IPreference page and adds it to the application's preferences
346
         * dialog. The preference page is added in alphabetical order within the
347
         * branch where the page is hanging on, and defined by its title.
348
         * @param page
349
         */
350
        public void addPreferencePage(IPreference page)
351
        {
352
                if (preferences.containsKey(page.getID()))
353
                        return;
354
                preferences.put(page.getID(), page);
355
                page.initializeValues(); // init values from the last settings
356
                if (dirtyTree) {
357
                        // rebuild page tree
358
                        dirtyTree = false;
359

    
360
                        DefaultTreeModel model = new DefaultTreeModel(root);
361
                        treeModel = model;
362
                        jTreePlugins.setModel(model);
363
                }
364

    
365
                doInsertNode(treeModel, page);
366
                getJTreePlugins().setModel(treeModel);
367
                getJTreePlugins().repaint();
368
        }
369
        private void addPreferencePages(ArrayList prefs){
370
                while (prefs.size()>0){
371
                        IPreference pref= (IPreference) prefs.get(0);
372
//                                System.err.println("IPreference a persistir=  "+pref.getID());
373
                        if (pref.getParentID()!=null && preferences.get(pref.getParentID())==null){
374
                                prefs.remove(pref);
375
                                addPreferencePages(prefs);
376
//                                System.err.println("IPreference =  "+pref.getID());
377
                                addPreference(pref);
378
                        }else{
379
//                                System.err.println("IPreference =  "+pref.getID());
380
                                addPreference(pref);
381
                                prefs.remove(pref);
382
                        }
383
                }
384
        }
385
        private void addPreference(IPreference pref) {
386
                DefaultTreeModel model = new DefaultTreeModel(root);
387
                doInsertNode(model, pref);
388
        }
389

    
390
        private DefaultMutableTreeNode findNode(String searchID)
391
        {
392
                Enumeration e = root.breadthFirstEnumeration();
393
                while (e.hasMoreElements())
394
                {
395
                        DefaultMutableTreeNode nodeAux = (DefaultMutableTreeNode) e.nextElement();
396
                        if (nodeAux != null)
397
                        {
398
                                IPreference pref = (IPreference) nodeAux.getUserObject();
399
                                if (pref == null) continue; // Root node
400
                                if (pref.getID().equals(searchID))
401
                                {
402
                                        return nodeAux;
403
                                }
404
                        }
405
                }
406
                return null;
407

    
408
        }
409

    
410
        private void doInsertNode(DefaultTreeModel treeModel, IPreference page)
411
        {
412

    
413
                dirtyTree = ((page.getParentID() != null) && (findNode(page.getParentID())==null));
414
                if (findNode(page.getID()) != null) // It is already added
415
                        return;
416
                if (page.getParentID() != null)
417
                {
418
                        if (preferences.containsKey(page.getParentID()))
419
                        {
420
                                IPreference parent = (IPreference) preferences.get(page.getParentID());
421
                                DefaultMutableTreeNode nodeParent = findNode(parent.getID());
422
                                if (nodeParent == null) // the parent is empty
423
                                {
424
                                        // Recursively add it
425
                                        doInsertNode(treeModel, parent);
426
                                }
427
                                else
428
                                {
429
                                        DefaultMutableTreeNode nodeValue = new DefaultMutableTreeNode(page);
430
                                        int children = nodeParent.getChildCount();
431
                                        int pos=0;
432
                                        for (int i = 0; i < children; i++) {
433
                                                DefaultMutableTreeNode node = (DefaultMutableTreeNode) treeModel.getChild(nodeParent, i);
434
                                                if (node.getUserObject() instanceof IPreference) {
435
                                                        String pageTitle = ((IPreference) node.getUserObject()).getTitle();
436
                                                        if (pageTitle.compareTo(page.getTitle()) < 0) ++pos;
437
                                                }
438
                                        }
439
                                        treeModel.insertNodeInto(nodeValue, nodeParent, pos);
440
                                }
441
                        }
442
                }
443
                else // First level node ("General", "Edition")
444
                {
445
                        DefaultMutableTreeNode nodeValue = new DefaultMutableTreeNode(page);
446
                        int children = root.getChildCount();
447
                        int pos=0;
448
                        for (int i = 0; i < children; i++) {
449
                                DefaultMutableTreeNode node = (DefaultMutableTreeNode) treeModel.getChild(root, i);
450
                                if (node.getUserObject() instanceof IPreference) {
451
                                        String pageTitle = ((IPreference) node.getUserObject()).getTitle();
452
                                        if (pageTitle.compareTo(page.getTitle()) < 0) ++pos;
453
                                }
454
                        }
455
                        treeModel.insertNodeInto(nodeValue, root, pos);
456
                }
457
        }
458

    
459
        /**
460
         * This method initializes jPanelButtons
461
         *
462
         * @return javax.swing.JPanel
463
         */
464
        private JPanel getJPanelButtons() {
465
                if (jPanelButtons == null) {
466
                        jPanelButtons = new JPanel(new BorderLayout());
467
                        JPanel jPanelAux = new JPanel();
468
                        JLabel l = new JLabel();
469
                        l.setPreferredSize(new Dimension(40, 20));
470
                        jPanelButtons.add(new JSeparator(JSeparator.HORIZONTAL), BorderLayout.NORTH);
471
                        jPanelAux.add(getJButtonRestore(), BorderLayout.WEST);
472
                        jPanelAux.add(l, BorderLayout.CENTER);
473
                        jPanelAux.add(getJButtonOK(), BorderLayout.EAST);
474
                        jPanelAux.add(getJButtonCancel(), BorderLayout.EAST);
475

    
476
                        jPanelButtons.add(jPanelAux);
477
                }
478
                return jPanelButtons;
479
        }
480

    
481
        /**
482
         * This method initializes jPanelButtons
483
         *
484
         * @return javax.swing.JPanel
485
         */
486

    
487

    
488
        /**
489
         * This method initializes jButtonOK
490
         *
491
         * @return JButton
492
         */
493
        private JButton getJButtonOK() {
494
                if (jButtonOK == null) {
495
                        jButtonOK = new JButton();
496
                        jButtonOK.setText(PluginServices.getText(this, "aceptar"));
497
                        jButtonOK.setActionCommand("OK");
498
                        jButtonOK.addActionListener(action);
499
                }
500
                return jButtonOK;
501
        }
502

    
503
        /**
504
         * This method initializes jButtonOK
505
         *
506
         * @return JButton
507
         */
508
        private JButton getJButtonRestore() {
509
                if (jButtonRestore == null) {
510
                        jButtonRestore = new JButton();
511
                        jButtonRestore.setText(PluginServices.getText(this, "restore_defaults"));
512
                        jButtonRestore.setActionCommand("RESTORE");
513
                        jButtonRestore.addActionListener(action);
514
                }
515
                return jButtonRestore;
516
        }
517

    
518
        private void closeView() {
519
                PluginServices.getMDIManager().closeWindow(this);
520
        }
521

    
522
        /**
523
         * This method initializes jButtonCancel
524
         *
525
         * @return JButton
526
         */
527
        private JButton getJButtonCancel() {
528
                if (jButtonCancel == null) {
529
                        jButtonCancel = new JButton();
530
                        jButtonCancel.setText(PluginServices.getText(this, "cancelar"));
531
                        jButtonCancel.setActionCommand("CANCEL");
532
                        jButtonCancel.addActionListener(action);
533
                }
534
                return jButtonCancel;
535
        }
536

    
537
        /**
538
         * This method initializes jPanelNorth
539
         *
540
         * @return javax.swing.JPanel
541
         */
542
        private JPanel getJPanelNorth() {
543
                if (jPanelCenter == null) {
544
                        jLabelBigTitle = new JLabel();
545
                        jLabelBigTitle.setText("General");
546
                        jLabelBigTitle.setFont(new java.awt.Font("MS Sans Serif", java.awt.Font.BOLD, 14));
547
                        jLabelBigTitle.setHorizontalTextPosition(javax.swing.SwingConstants.TRAILING);
548
                        jLabelBigTitle.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
549
                        jPanelCenter = new JPanel();
550
                        JPanel jPanelPageTitle = new JPanel(new BorderLayout());
551
                        JPanel jPanelAux = new JPanel(new BorderLayout());
552
                        jPanelAux.add(jLabelBigTitle, java.awt.BorderLayout.NORTH);
553
                        jPanelPageTitle.add(jPanelAux, java.awt.BorderLayout.WEST);
554
                        jPanelPageTitle.add(new JSeparator(JSeparator.HORIZONTAL), BorderLayout.SOUTH);
555
                        jPanelCenter.setLayout(new BorderLayout());
556
                        jPanelCenter.add(jPanelPageTitle, BorderLayout.NORTH);
557
                        jPanelCenter.add(getJPanelContainer(), java.awt.BorderLayout.CENTER);
558

    
559
                }
560
                return jPanelCenter;
561
        }
562

    
563
        /**
564
         * This method initializes jScrollPane
565
         *
566
         * @return javax.swing.JScrollPane
567
         */
568
        private JScrollPane getJScrollPane() {
569
                if (jScrollPane == null) {
570
                        jScrollPane = new JScrollPane();
571
                        jScrollPane.setPreferredSize(new java.awt.Dimension(140,322));
572
                        jScrollPane.setViewportView(getJTreePlugins());
573
                }
574
                return jScrollPane;
575
        }
576

    
577
        /**
578
         * This method initializes jSplitPaneCenter
579
         *
580
         * @return javax.swing.JSplitPane
581
         */
582
        private JSplitPane getJSplitPaneCenter() {
583
                if (jSplitPaneCenter == null) {
584
                        jSplitPaneCenter = new JSplitPane();
585
                        jSplitPaneCenter.setResizeWeight(0.2);
586
                        jSplitPaneCenter.setDividerLocation(200);
587
                }
588
                return jSplitPaneCenter;
589
        }
590

    
591
        /**
592
         * This method initializes jPanelContainer
593
         *
594
         * @return javax.swing.JPanel
595
         */
596
        private JPanel getJPanelContainer() {
597
                if (jPanelContainer == null) {
598
                        jPanelContainer = new JPanel();
599
                }
600
                return jPanelContainer;
601
        }
602

    
603
        /**
604
         *
605
         */
606
        public void setActivePage(IPreference page) {
607
                activePreference = page;
608
                jLabelBigTitle.setText(activePreference.getTitle());
609
                JPanel prefPanel = activePreference.getPanel();
610
                jLabelBigTitle.setIcon(activePreference.getIcon());
611
                jPanelContainer.removeAll();
612
                if ((prefPanel instanceof AbstractPreferencePage) &&
613
                                ((AbstractPreferencePage) prefPanel).isResizeable()) {
614
                        jPanelContainer.setLayout(new BorderLayout());
615
                        jPanelContainer.add(prefPanel, BorderLayout.CENTER);
616
                } else {
617
                        jPanelContainer.setLayout(new FlowLayout());
618
                        jPanelContainer.add(prefPanel);
619
                }
620
                prefPanel.setVisible(true);
621
                repaint();
622
        }
623
}