Statistics
| Revision:

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

History | View | Annotate | Download (14.5 KB)

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

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

    
64
import com.iver.andami.PluginServices;
65
import com.iver.andami.ui.mdiManager.IWindow;
66
import com.iver.andami.ui.mdiManager.WindowInfo;
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 IWindow {
78
        private WindowInfo viewInfo = null;
79
        private IPreference activePreference;
80

    
81
        private 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
        private ActionListener action;
104

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

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

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

    
135
        }
136

    
137

    
138
        public GenericDlgPreferences() {
139
                super();
140
                this.action = new ActionListener() {
141
                        public void actionPerformed(ActionEvent e) {
142
                                PluginServices.getMDIManager().setWaitCursor();
143
                                String actionCommand = e.getActionCommand();
144
                                if ("RESTORE".equals(actionCommand)) {
145
                                        // Restore default values in current page
146
                                        if (activePreference!=null)
147
                                                activePreference.initializeDefaults();
148
                                } else {
149
                                        Iterator it = preferences.keySet().iterator();
150

    
151
                                        if ("CANCEL".equals(actionCommand)) {
152
                                                // Restore previous values in all pages
153
                                                while (it.hasNext()) {
154
                                                        IPreference pref = (IPreference) preferences.get(it.next());
155
                                                        if (pref.isValueChanged())
156
                                                                pref.initializeValues(); //
157
                                                }
158
                                                closeView();
159
                                        } else if ("OK".equals(actionCommand)) {
160
                                                // Apply values in all pages
161
                                                boolean shouldClose = true;
162
                                                while (it.hasNext()) {
163
                                                        IPreference preference = (IPreference) preferences.get(it.next());
164
                                                        try{
165
                                                                if (preference.isValueChanged()) {
166
                                                                        preference.saveValues();
167
                                                                        preference.initializeValues();
168
                                                                }
169
                                                        }catch (StoreException ex) {
170
                                                                // Reach this code should mean that the page has wrong values
171
                                                                shouldClose = false;
172
                                                                PluginServices.getMDIManager().restoreCursor();
173

    
174
                                                                // Show error message
175
                                                                JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),ex.getMessage());
176

    
177
                                                                // Focus on error message
178
                                                                setActivePage(preference);
179
                                                        }
180
                                                }
181
                                                if (shouldClose)
182
                                                        closeView();
183
                                        }
184
                                }
185
                                PluginServices.getMDIManager().restoreCursor();
186
                        }
187
                };
188
                initialize();
189
        }
190

    
191
        private void initialize() {
192
                setLayout(new BorderLayout());
193
                setSize(new java.awt.Dimension(700,479));
194
                super.add(getJPanelButtons(), BorderLayout.SOUTH);
195
                setPreferredSize(new java.awt.Dimension(390,369));
196
                super.add(getJSplitPaneCenter(), java.awt.BorderLayout.CENTER);
197
                getJSplitPaneCenter().setLeftComponent(getJScrollPane());
198
                getJSplitPaneCenter().setRightComponent(getJPanelNorth());
199

    
200

    
201

    
202
                treeModel = new DefaultTreeModel(root);
203
        }
204

    
205
        public void refreshExtensionPoints() {
206
                ExtensionPoints extensionPoints =
207
                        ExtensionPointsSingleton.getInstance();
208

    
209
                ExtensionPoint extensionPoint =(ExtensionPoint)extensionPoints.get("AplicationPreferences");
210

    
211
                Iterator iterator = extensionPoint.keySet().iterator();
212
                while (iterator.hasNext()) {
213
                        try {
214
                                IPreference obj = (IPreference)extensionPoint.create((String)iterator.next());
215
                                this.addPreferencePage(obj);
216
                        } catch (InstantiationException e) {
217
                                // TODO Auto-generated catch block
218
                                e.printStackTrace();
219
                        } catch (IllegalAccessException e) {
220
                                // TODO Auto-generated catch block
221
                                e.printStackTrace();
222
                        } catch (ClassCastException e) {
223
                                // TODO Auto-generated catch block
224
                                e.printStackTrace();
225
                        }
226
                }
227
        }
228

    
229

    
230
        /**
231
         * It is very common to be confused with this method. But
232
         * the one you are looking for is addPreferencePage(IPreference)
233
         */
234
        public Component add(Component c) {
235
                //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");
236
                throw new Error("Do not use com.iver.cit.gvsig.gui.preferences.DlgPreferences.add(Component) register an extension point instead");
237
        }
238

    
239
        public Component add(Component c, int i) {
240
                return add(c);
241
        }
242

    
243
        public void add(Component c, Object o) {
244
                add(c);
245
        }
246

    
247
        public void add(Component c, Object o, int i) {
248
                add(c);
249
        }
250

    
251
        public WindowInfo getWindowInfo() {
252
                if (viewInfo == null) {
253
                        viewInfo = new WindowInfo(WindowInfo.MODALDIALOG | WindowInfo.RESIZABLE | WindowInfo.PALETTE);
254
                        viewInfo.setTitle(PluginServices.getText(this,
255
                                        "Preferences"));
256
                        viewInfo.setWidth(this.getWidth()+8);
257
                        viewInfo.setHeight(this.getHeight());
258
                }
259
                return viewInfo;
260
        }
261

    
262
        /**
263
         * This method initializes jTreePlugins
264
         *
265
         * @return javax.swing.JTree
266
         */
267
        private JTree getJTreePlugins() {
268
                if (jTreePlugins == null) {
269
                        jTreePlugins = new JTree();
270
                        jTreePlugins.setRootVisible(false);
271
                        MyTreeCellRenderer treeCellRenderer = new MyTreeCellRenderer();
272
                        treeCellRenderer.setOpenIcon(null);
273
                        treeCellRenderer.setClosedIcon(null);
274
                        treeCellRenderer.setLeafIcon(null);
275

    
276

    
277

    
278
                        jTreePlugins.setCellRenderer(treeCellRenderer);
279
                        jTreePlugins.setShowsRootHandles(true);
280
                        jTreePlugins
281
                                        .addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
282
                                                public void valueChanged(javax.swing.event.TreeSelectionEvent e) {
283
                                                         DefaultMutableTreeNode node = (DefaultMutableTreeNode)
284
                                       jTreePlugins.getLastSelectedPathComponent();
285

    
286
                                                                 if (node == null) return;
287
                                                                 setActivePage((IPreference) node.getUserObject());
288
                                                }
289
                                        });
290
                        jTreePlugins.putClientProperty("JTree.linestyle", "Angled");
291
                }
292

    
293
                return jTreePlugins;
294
        }
295

    
296
        public void addPreferencePage(IPreference page)
297
        {
298
                preferences.put(page.getID(), page);
299
                page.initializeValues(); // init values from the last settings
300
                //page.storeValues();                 // apply them
301
                doInsertNode(page);
302
                getJTreePlugins().setModel(treeModel);
303
        }
304

    
305
        private DefaultMutableTreeNode findNode(String searchID)
306
        {
307
                Enumeration e = root.breadthFirstEnumeration();
308
                while (e.hasMoreElements())
309
                {
310
                        DefaultMutableTreeNode nodeAux = (DefaultMutableTreeNode) e.nextElement();
311
                        if (nodeAux != null)
312
                        {
313
                                IPreference pref = (IPreference) nodeAux.getUserObject();
314
                                if (pref == null) continue; // Nodo ra?z
315
                                if (pref.getID().equals(searchID))
316
                                {
317
                                        return nodeAux;
318
                                }
319
                        }
320
                }
321
                return null;
322

    
323
        }
324

    
325
        private void doInsertNode(IPreference page)
326
        {
327
                if (findNode(page.getID()) != null) // Ya est? incluido
328
                        return;
329
                if (page.getParentID() != null)
330
                {
331
                        if (preferences.containsKey(page.getParentID()))
332
                        {
333
                                IPreference parent = (IPreference) preferences.get(page.getParentID());
334
                                DefaultMutableTreeNode nodeParent = findNode(parent.getID());
335
                                if (nodeParent == null) // Todav?a no hemos a?adido al padre.
336
                                {
337
                                        // Lo a?adimos de manera recursiva
338
                                        doInsertNode(parent);
339
                                }
340
                                else
341
                                {
342
                                        DefaultMutableTreeNode nodeValue = new DefaultMutableTreeNode(page);
343
                                        treeModel.insertNodeInto(nodeValue, nodeParent, nodeParent.getChildCount());
344
                                }
345
                        }
346
                }
347
                else // Nodo de los de primera l?nea ("General", "Edicion", etc)
348
                {
349
                        DefaultMutableTreeNode nodeValue = new DefaultMutableTreeNode(page);
350
                        treeModel.insertNodeInto(nodeValue, root, root.getChildCount());
351
                }
352
        }
353

    
354
        /**
355
         * This method initializes jPanelButtons
356
         *
357
         * @return javax.swing.JPanel
358
         */
359
        private JPanel getJPanelButtons() {
360
                if (jPanelButtons == null) {
361
                        jPanelButtons = new JPanel(new BorderLayout());
362
                        JPanel jPanelAux = new JPanel();
363
                        JLabel l = new JLabel();
364
                        l.setPreferredSize(new Dimension(40, 20));
365
                        jPanelButtons.add(new JSeparator(JSeparator.HORIZONTAL), BorderLayout.NORTH);
366
                        jPanelAux.add(getJButtonRestore(), BorderLayout.WEST);
367
                        jPanelAux.add(l, BorderLayout.CENTER);
368
                        jPanelAux.add(getJButtonOK(), BorderLayout.EAST);
369
                        jPanelAux.add(getJButtonCancel(), BorderLayout.EAST);
370

    
371
                        jPanelButtons.add(jPanelAux);
372
                }
373
                return jPanelButtons;
374
        }
375

    
376
        /**
377
         * This method initializes jPanelButtons
378
         *
379
         * @return javax.swing.JPanel
380
         */
381

    
382

    
383
        /**
384
         * This method initializes jButtonOK
385
         *
386
         * @return JButton
387
         */
388
        private JButton getJButtonOK() {
389
                if (jButtonOK == null) {
390
                        jButtonOK = new JButton();
391
                        jButtonOK.setText(PluginServices.getText(this, "aceptar"));
392
                        jButtonOK.setActionCommand("OK");
393
                        jButtonOK.addActionListener(action);
394
                }
395
                return jButtonOK;
396
        }
397

    
398
        /**
399
         * This method initializes jButtonOK
400
         *
401
         * @return JButton
402
         */
403
        private JButton getJButtonRestore() {
404
                if (jButtonRestore == null) {
405
                        jButtonRestore = new JButton();
406
                        jButtonRestore.setText(PluginServices.getText(this, "restore_defaults"));
407
                        jButtonRestore.setActionCommand("RESTORE");
408
                        jButtonRestore.addActionListener(action);
409
                }
410
                return jButtonRestore;
411
        }
412

    
413
        private void closeView() {
414
                PluginServices.getMDIManager().closeWindow(this);
415
        }
416

    
417
        /**
418
         * This method initializes jButtonCancel
419
         *
420
         * @return JButton
421
         */
422
        private JButton getJButtonCancel() {
423
                if (jButtonCancel == null) {
424
                        jButtonCancel = new JButton();
425
                        jButtonCancel.setText(PluginServices.getText(this, "cancelar"));
426
                        jButtonCancel.setActionCommand("CANCEL");
427
                        jButtonCancel.addActionListener(action);
428
                }
429
                return jButtonCancel;
430
        }
431

    
432
        /**
433
         * This method initializes jPanelNorth
434
         *
435
         * @return javax.swing.JPanel
436
         */
437
        private JPanel getJPanelNorth() {
438
                if (jPanelCenter == null) {
439
                        jLabelBigTitle = new JLabel();
440
                        jLabelBigTitle.setText("General");
441
                        jLabelBigTitle.setFont(new java.awt.Font("MS Sans Serif", java.awt.Font.BOLD, 14));
442
                        jLabelBigTitle.setHorizontalTextPosition(javax.swing.SwingConstants.TRAILING);
443
                        jLabelBigTitle.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
444
                        jPanelCenter = new JPanel();
445
                        JPanel jPanelPageTitle = new JPanel(new BorderLayout());
446
                        JPanel jPanelAux = new JPanel(new BorderLayout());
447
                        jPanelAux.add(jLabelBigTitle, java.awt.BorderLayout.NORTH);
448
                        jPanelPageTitle.add(jPanelAux, java.awt.BorderLayout.WEST);
449
                        jPanelPageTitle.add(new JSeparator(JSeparator.HORIZONTAL), BorderLayout.SOUTH);
450
                        jPanelCenter.setLayout(new BorderLayout());
451
                        jPanelCenter.add(jPanelPageTitle, BorderLayout.NORTH);
452
                        jPanelCenter.add(getJPanelContainer(), java.awt.BorderLayout.CENTER);
453

    
454
                }
455
                return jPanelCenter;
456
        }
457

    
458
        /**
459
         * This method initializes jScrollPane
460
         *
461
         * @return javax.swing.JScrollPane
462
         */
463
        private JScrollPane getJScrollPane() {
464
                if (jScrollPane == null) {
465
                        jScrollPane = new JScrollPane();
466
                        jScrollPane.setPreferredSize(new java.awt.Dimension(140,322));
467
                        jScrollPane.setViewportView(getJTreePlugins());
468
                }
469
                return jScrollPane;
470
        }
471

    
472
        /**
473
         * This method initializes jSplitPaneCenter
474
         *
475
         * @return javax.swing.JSplitPane
476
         */
477
        private JSplitPane getJSplitPaneCenter() {
478
                if (jSplitPaneCenter == null) {
479
                        jSplitPaneCenter = new JSplitPane();
480
                        jSplitPaneCenter.setResizeWeight(0.2);
481
                }
482
                return jSplitPaneCenter;
483
        }
484

    
485
        /**
486
         * This method initializes jPanelContainer
487
         *
488
         * @return javax.swing.JPanel
489
         */
490
        private JPanel getJPanelContainer() {
491
                if (jPanelContainer == null) {
492
                        jPanelContainer = new JPanel();
493
                }
494
                return jPanelContainer;
495
        }
496

    
497
        /**
498
         *
499
         */
500
        public void setActivePage(IPreference page) {
501
                activePreference = page;
502
                jLabelBigTitle.setText(activePreference.getTitle());
503
                JPanel prefPanel = activePreference.getPanel();
504
                jLabelBigTitle.setIcon(activePreference.getIcon());
505
                jPanelContainer.removeAll();
506
                jPanelContainer.add(prefPanel);
507
                prefPanel.setVisible(true);
508
                repaint();
509
        }
510

    
511

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