svn-gvsig-desktop / trunk / frameworks / _fwAndami / src / com / iver / andami / preferences / GenericDlgPreferences.java @ 6596
History | View | Annotate | Download (13.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.ActionListener; |
47 |
import java.util.Enumeration; |
48 |
import java.util.Hashtable; |
49 |
import java.util.Iterator; |
50 |
import java.util.Set; |
51 |
|
52 |
import javax.swing.JLabel; |
53 |
import javax.swing.JPanel; |
54 |
import javax.swing.JScrollPane; |
55 |
import javax.swing.JSeparator; |
56 |
import javax.swing.JSplitPane; |
57 |
import javax.swing.JTree; |
58 |
import javax.swing.tree.DefaultMutableTreeNode; |
59 |
import javax.swing.tree.DefaultTreeCellRenderer; |
60 |
import javax.swing.tree.DefaultTreeModel; |
61 |
|
62 |
import org.gvsig.gui.beans.swing.JButton; |
63 |
|
64 |
import com.iver.andami.PluginServices; |
65 |
import com.iver.andami.ui.mdiManager.View; |
66 |
import com.iver.andami.ui.mdiManager.ViewInfo; |
67 |
import com.iver.utiles.extensionPoints.ExtensionPoint; |
68 |
import com.iver.utiles.extensionPoints.ExtensionPoints; |
69 |
import com.iver.utiles.extensionPoints.ExtensionPointsSingleton; |
70 |
|
71 |
/**
|
72 |
* @author fjp
|
73 |
*
|
74 |
* The reason behind this class is to have the opportunity of use a DlgPreferences
|
75 |
* in a local way. Then, you don't need to be a SingletonView.
|
76 |
*/
|
77 |
public class GenericDlgPreferences extends JPanel implements View { |
78 |
private ViewInfo viewInfo = null; |
79 |
private IPreference activePreference;
|
80 |
|
81 |
private static Hashtable preferences = new Hashtable(); |
82 |
DefaultTreeModel treeModel = null; |
83 |
|
84 |
private JTree jTreePlugins = null; |
85 |
|
86 |
private JPanel jPanelButtons = null; |
87 |
|
88 |
private JButton jButtonOK = null; |
89 |
|
90 |
private JButton jButtonCancel = null; |
91 |
private DefaultMutableTreeNode root = new DefaultMutableTreeNode(); |
92 |
|
93 |
private JPanel jPanelCenter = null; |
94 |
|
95 |
private JLabel jLabelBigTitle = null; |
96 |
|
97 |
private JScrollPane jScrollPane = null; |
98 |
|
99 |
private JSplitPane jSplitPaneCenter = null; |
100 |
|
101 |
private JPanel jPanelContainer = null; |
102 |
private JButton jButtonRestore; |
103 |
|
104 |
private class MyTreeCellRenderer extends DefaultTreeCellRenderer |
105 |
{ |
106 |
public MyTreeCellRenderer() {
|
107 |
} |
108 |
|
109 |
public Component getTreeCellRendererComponent( |
110 |
JTree tree,
|
111 |
Object value,
|
112 |
boolean sel,
|
113 |
boolean expanded,
|
114 |
boolean leaf,
|
115 |
int row,
|
116 |
boolean hasFocus) {
|
117 |
|
118 |
super.getTreeCellRendererComponent(
|
119 |
tree, value, sel, |
120 |
expanded, leaf, row, |
121 |
hasFocus); |
122 |
if (value instanceof DefaultMutableTreeNode) |
123 |
{ |
124 |
DefaultMutableTreeNode node = (DefaultMutableTreeNode) value; |
125 |
if (node.getUserObject() instanceof IPreference) |
126 |
{ |
127 |
IPreference pref = (IPreference) node.getUserObject(); |
128 |
this.setText(pref.getTitle());
|
129 |
} |
130 |
} |
131 |
return this; |
132 |
} |
133 |
|
134 |
} |
135 |
|
136 |
|
137 |
public GenericDlgPreferences() {
|
138 |
super();
|
139 |
initialize(); |
140 |
} |
141 |
|
142 |
private void initialize() { |
143 |
setLayout(new BorderLayout()); |
144 |
setSize(new java.awt.Dimension(588,479)); |
145 |
super.add(getJPanelButtons(), BorderLayout.SOUTH); |
146 |
setPreferredSize(new java.awt.Dimension(390,369)); |
147 |
super.add(getJSplitPaneCenter(), java.awt.BorderLayout.CENTER);
|
148 |
getJSplitPaneCenter().setLeftComponent(getJScrollPane()); |
149 |
getJSplitPaneCenter().setRightComponent(getJPanelNorth()); |
150 |
|
151 |
|
152 |
|
153 |
treeModel = new DefaultTreeModel(root); |
154 |
|
155 |
refreshTree(); |
156 |
|
157 |
} |
158 |
|
159 |
private void refreshTree() { |
160 |
ExtensionPoints extensionPoints = |
161 |
ExtensionPointsSingleton.getInstance(); |
162 |
|
163 |
ExtensionPoint extensionPoint =(ExtensionPoint)extensionPoints.get("AplicationPreferences");
|
164 |
|
165 |
Iterator iterator = extensionPoint.keySet().iterator();
|
166 |
while (iterator.hasNext()) {
|
167 |
try {
|
168 |
IPreference obj = (IPreference)extensionPoint.create((String)iterator.next());
|
169 |
this.addPreferencePage(obj);
|
170 |
} catch (InstantiationException e) { |
171 |
// TODO Auto-generated catch block
|
172 |
e.printStackTrace(); |
173 |
} catch (IllegalAccessException e) { |
174 |
// TODO Auto-generated catch block
|
175 |
e.printStackTrace(); |
176 |
} catch (ClassCastException e) { |
177 |
// TODO Auto-generated catch block
|
178 |
e.printStackTrace(); |
179 |
} |
180 |
} |
181 |
} |
182 |
|
183 |
|
184 |
/**
|
185 |
* It is very common to be confused with this method. But
|
186 |
* the one you are looking for is addPreferencePage(IPreference)
|
187 |
*/
|
188 |
public Component add(Component c) { |
189 |
//throw new Error("Do not use com.iver.cit.gvsig.gui.preferences.DlgPreferences.add(Component) use com.iver.cit.gvsig.gui.preferences.DlgPreferences.addPreferencePage(IPreference) instead");
|
190 |
throw new Error("Do not use com.iver.cit.gvsig.gui.preferences.DlgPreferences.add(Component) register an extension point instead"); |
191 |
} |
192 |
|
193 |
public Component add(Component c, int i) { |
194 |
return add(c);
|
195 |
} |
196 |
|
197 |
public void add(Component c, Object o) { |
198 |
add(c); |
199 |
} |
200 |
|
201 |
public void add(Component c, Object o, int i) { |
202 |
add(c); |
203 |
} |
204 |
|
205 |
public ViewInfo getViewInfo() {
|
206 |
if (viewInfo == null) { |
207 |
viewInfo = new ViewInfo(ViewInfo.MODALDIALOG | ViewInfo.RESIZABLE | ViewInfo.PALETTE);
|
208 |
viewInfo.setTitle(PluginServices.getText(this,
|
209 |
"Preferences"));
|
210 |
viewInfo.setWidth(this.getWidth()+8); |
211 |
viewInfo.setHeight(this.getHeight());
|
212 |
} |
213 |
return viewInfo;
|
214 |
} |
215 |
|
216 |
/**
|
217 |
* This method initializes jTreePlugins
|
218 |
*
|
219 |
* @return javax.swing.JTree
|
220 |
*/
|
221 |
private JTree getJTreePlugins() { |
222 |
if (jTreePlugins == null) { |
223 |
jTreePlugins = new JTree(); |
224 |
jTreePlugins.setRootVisible(false);
|
225 |
MyTreeCellRenderer treeCellRenderer = new MyTreeCellRenderer();
|
226 |
treeCellRenderer.setOpenIcon(null);
|
227 |
treeCellRenderer.setClosedIcon(null);
|
228 |
treeCellRenderer.setLeafIcon(null);
|
229 |
|
230 |
|
231 |
|
232 |
jTreePlugins.setCellRenderer(treeCellRenderer); |
233 |
jTreePlugins.setShowsRootHandles(true);
|
234 |
jTreePlugins |
235 |
.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
|
236 |
public void valueChanged(javax.swing.event.TreeSelectionEvent e) { |
237 |
DefaultMutableTreeNode node = (DefaultMutableTreeNode) |
238 |
jTreePlugins.getLastSelectedPathComponent(); |
239 |
|
240 |
if (node == null) return; |
241 |
setActivePage((IPreference) node.getUserObject()); |
242 |
} |
243 |
}); |
244 |
jTreePlugins.putClientProperty("JTree.linestyle", "Angled"); |
245 |
} |
246 |
|
247 |
return jTreePlugins;
|
248 |
} |
249 |
|
250 |
private void addPreferencePage(IPreference page) |
251 |
{ |
252 |
preferences.put(page.getID(), page); |
253 |
page.initializeValues(); // init values from the last settings
|
254 |
//page.storeValues(); // apply them
|
255 |
doInsertNode(page); |
256 |
getJTreePlugins().setModel(treeModel); |
257 |
} |
258 |
|
259 |
private DefaultMutableTreeNode findNode(String searchID) |
260 |
{ |
261 |
Enumeration e = root.breadthFirstEnumeration();
|
262 |
while (e.hasMoreElements())
|
263 |
{ |
264 |
DefaultMutableTreeNode nodeAux = (DefaultMutableTreeNode) e.nextElement(); |
265 |
if (nodeAux != null) |
266 |
{ |
267 |
IPreference pref = (IPreference) nodeAux.getUserObject(); |
268 |
if (pref == null) continue; // Nodo ra?z |
269 |
if (pref.getID().equals(searchID))
|
270 |
{ |
271 |
return nodeAux;
|
272 |
} |
273 |
} |
274 |
} |
275 |
return null; |
276 |
|
277 |
} |
278 |
|
279 |
private void doInsertNode(IPreference page) |
280 |
{ |
281 |
if (findNode(page.getID()) != null) // Ya est? incluido |
282 |
return;
|
283 |
if (page.getParentID() != null) |
284 |
{ |
285 |
if (preferences.containsKey(page.getParentID()))
|
286 |
{ |
287 |
IPreference parent = (IPreference) preferences.get(page.getParentID()); |
288 |
DefaultMutableTreeNode nodeParent = findNode(parent.getID());
|
289 |
if (nodeParent == null) // Todav?a no hemos a?adido al padre. |
290 |
{ |
291 |
// Lo a?adimos de manera recursiva
|
292 |
doInsertNode(parent); |
293 |
} |
294 |
else
|
295 |
{ |
296 |
DefaultMutableTreeNode nodeValue = new DefaultMutableTreeNode(page); |
297 |
treeModel.insertNodeInto(nodeValue, nodeParent, nodeParent.getChildCount()); |
298 |
} |
299 |
} |
300 |
} |
301 |
else // Nodo de los de primera l?nea ("General", "Edicion", etc) |
302 |
{ |
303 |
DefaultMutableTreeNode nodeValue = new DefaultMutableTreeNode(page); |
304 |
treeModel.insertNodeInto(nodeValue, root, root.getChildCount()); |
305 |
} |
306 |
} |
307 |
|
308 |
/**
|
309 |
* This method initializes jPanelButtons
|
310 |
*
|
311 |
* @return javax.swing.JPanel
|
312 |
*/
|
313 |
private JPanel getJPanelButtons() { |
314 |
if (jPanelButtons == null) { |
315 |
jPanelButtons = new JPanel(new BorderLayout()); |
316 |
JPanel jPanelAux = new JPanel(); |
317 |
JLabel l = new JLabel(); |
318 |
l.setPreferredSize(new Dimension(40, 20)); |
319 |
jPanelButtons.add(new JSeparator(JSeparator.HORIZONTAL), BorderLayout.NORTH); |
320 |
jPanelAux.add(getJButtonRestore(), BorderLayout.WEST);
|
321 |
jPanelAux.add(l, BorderLayout.CENTER);
|
322 |
jPanelAux.add(getJButtonOK(), BorderLayout.EAST);
|
323 |
jPanelAux.add(getJButtonCancel(), BorderLayout.EAST);
|
324 |
|
325 |
jPanelButtons.add(jPanelAux); |
326 |
} |
327 |
return jPanelButtons;
|
328 |
} |
329 |
|
330 |
/**
|
331 |
* This method initializes jPanelButtons
|
332 |
*
|
333 |
* @return javax.swing.JPanel
|
334 |
*/
|
335 |
|
336 |
|
337 |
/**
|
338 |
* This method initializes jButtonOK
|
339 |
*
|
340 |
* @return JButton
|
341 |
*/
|
342 |
private JButton getJButtonOK() { |
343 |
if (jButtonOK == null) { |
344 |
jButtonOK = new JButton(); |
345 |
jButtonOK.setText(PluginServices.getText(this, "aceptar")); |
346 |
jButtonOK.addActionListener(new ActionListener() |
347 |
{ |
348 |
public void actionPerformed(java.awt.event.ActionEvent e) { |
349 |
Iterator it = preferences.keySet().iterator();
|
350 |
boolean shouldClose = true; |
351 |
while (it.hasNext()) {
|
352 |
IPreference preference = (IPreference) preferences.get(it.next()); |
353 |
if (!preference.storeValues())
|
354 |
shouldClose = false;
|
355 |
} |
356 |
if (shouldClose)
|
357 |
closeView(); |
358 |
}; |
359 |
}); |
360 |
} |
361 |
return jButtonOK;
|
362 |
} |
363 |
|
364 |
/**
|
365 |
* This method initializes jButtonOK
|
366 |
*
|
367 |
* @return JButton
|
368 |
*/
|
369 |
private JButton getJButtonRestore() { |
370 |
if (jButtonRestore == null) { |
371 |
jButtonRestore = new JButton(); |
372 |
jButtonRestore.setText(PluginServices.getText(this, "restore_defaults")); |
373 |
jButtonRestore.addActionListener(new ActionListener() |
374 |
{ |
375 |
public void actionPerformed(java.awt.event.ActionEvent e) { |
376 |
if (activePreference!=null) |
377 |
activePreference.initializeDefaults(); |
378 |
}; |
379 |
}); |
380 |
} |
381 |
return jButtonRestore;
|
382 |
} |
383 |
|
384 |
private void closeView() { |
385 |
PluginServices.getMDIManager().closeView(this);
|
386 |
} |
387 |
|
388 |
/**
|
389 |
* This method initializes jButtonCancel
|
390 |
*
|
391 |
* @return JButton
|
392 |
*/
|
393 |
private JButton getJButtonCancel() { |
394 |
if (jButtonCancel == null) { |
395 |
jButtonCancel = new JButton(); |
396 |
jButtonCancel.setText(PluginServices.getText(this, "cancelar")); |
397 |
jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
|
398 |
public void actionPerformed(java.awt.event.ActionEvent e) { |
399 |
Iterator it = preferences.keySet().iterator();
|
400 |
while (it.hasNext()) {
|
401 |
((IPreference) preferences.get(it.next())).cancelAction(); |
402 |
} |
403 |
closeView(); |
404 |
} |
405 |
}); |
406 |
} |
407 |
return jButtonCancel;
|
408 |
} |
409 |
|
410 |
/**
|
411 |
* This method initializes jPanelNorth
|
412 |
*
|
413 |
* @return javax.swing.JPanel
|
414 |
*/
|
415 |
private JPanel getJPanelNorth() { |
416 |
if (jPanelCenter == null) { |
417 |
jLabelBigTitle = new JLabel(); |
418 |
jLabelBigTitle.setText("General");
|
419 |
jLabelBigTitle.setFont(new java.awt.Font("MS Sans Serif", java.awt.Font.BOLD, 14)); |
420 |
jLabelBigTitle.setHorizontalTextPosition(javax.swing.SwingConstants.TRAILING); |
421 |
jLabelBigTitle.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); |
422 |
jPanelCenter = new JPanel(); |
423 |
JPanel jPanelPageTitle = new JPanel(new BorderLayout()); |
424 |
JPanel jPanelAux = new JPanel(new BorderLayout()); |
425 |
jPanelAux.add(jLabelBigTitle, java.awt.BorderLayout.NORTH); |
426 |
jPanelPageTitle.add(jPanelAux, java.awt.BorderLayout.WEST); |
427 |
jPanelPageTitle.add(new JSeparator(JSeparator.HORIZONTAL), BorderLayout.SOUTH); |
428 |
jPanelCenter.setLayout(new BorderLayout()); |
429 |
jPanelCenter.add(jPanelPageTitle, BorderLayout.NORTH);
|
430 |
jPanelCenter.add(getJPanelContainer(), java.awt.BorderLayout.CENTER); |
431 |
|
432 |
} |
433 |
return jPanelCenter;
|
434 |
} |
435 |
|
436 |
/**
|
437 |
* This method initializes jScrollPane
|
438 |
*
|
439 |
* @return javax.swing.JScrollPane
|
440 |
*/
|
441 |
private JScrollPane getJScrollPane() { |
442 |
if (jScrollPane == null) { |
443 |
jScrollPane = new JScrollPane(); |
444 |
jScrollPane.setPreferredSize(new java.awt.Dimension(140,322)); |
445 |
jScrollPane.setViewportView(getJTreePlugins()); |
446 |
} |
447 |
return jScrollPane;
|
448 |
} |
449 |
|
450 |
/**
|
451 |
* This method initializes jSplitPaneCenter
|
452 |
*
|
453 |
* @return javax.swing.JSplitPane
|
454 |
*/
|
455 |
private JSplitPane getJSplitPaneCenter() { |
456 |
if (jSplitPaneCenter == null) { |
457 |
jSplitPaneCenter = new JSplitPane(); |
458 |
jSplitPaneCenter.setResizeWeight(0.2);
|
459 |
} |
460 |
return jSplitPaneCenter;
|
461 |
} |
462 |
|
463 |
/**
|
464 |
* This method initializes jPanelContainer
|
465 |
*
|
466 |
* @return javax.swing.JPanel
|
467 |
*/
|
468 |
private JPanel getJPanelContainer() { |
469 |
if (jPanelContainer == null) { |
470 |
jPanelContainer = new JPanel(); |
471 |
} |
472 |
return jPanelContainer;
|
473 |
} |
474 |
|
475 |
/**
|
476 |
*
|
477 |
*/
|
478 |
public void setActivePage(IPreference page) { |
479 |
activePreference = page; |
480 |
jLabelBigTitle.setText(activePreference.getTitle()); |
481 |
JPanel prefPanel = activePreference.getPanel();
|
482 |
jLabelBigTitle.setIcon(activePreference.getIcon()); |
483 |
jPanelContainer.removeAll(); |
484 |
jPanelContainer.add(prefPanel); |
485 |
prefPanel.setVisible(true);
|
486 |
repaint(); |
487 |
} |
488 |
|
489 |
|
490 |
} // @jve:decl-index=0:visual-constraint="10,10"
|