KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > jawe > config > ConfigDialog


1 /*
2  * Authors:
3  * Stefanovic Nenad chupo@iis.ns.ac.yu
4  * Bojanic Sasa sasaboy@neobee.net
5  *
6  */

7
8 package org.enhydra.jawe.config;
9
10
11
12
13 import org.enhydra.jawe.*;
14
15 import java.util.*;
16
17 import java.awt.*;
18 import java.awt.event.*;
19
20 import javax.swing.*;
21 import javax.swing.event.*;
22 import javax.swing.border.*;
23 import javax.swing.tree.*;
24
25 import org.enhydra.jawe.*;
26
27 public class ConfigDialog extends JDialog implements ActionListener, TreeSelectionListener{
28
29
30    private static PackageEditor parent;
31
32    private JTree paneTree;
33    private OptionTreeModel treeModel;
34    private ConfigElements jaweOptionsRoot;
35    private static ConfigDialog optionsDialog;
36
37    private String JavaDoc currPaneName;
38
39    private JPanel cardPanel, configsPanel;
40    private JButton okButton, cancelButton, applyButton, restoreDefaultButton;
41
42    public static ConfigDialog getInstance () {
43       if (optionsDialog==null) {
44          parent = JaWE.getInstance().getPackageEditor();
45          optionsDialog = new ConfigDialog((JFrame)parent.getWindow());
46       }
47       return optionsDialog;
48    }
49
50    /**
51     * Call this to show the config dialog
52     */

53    public void showConfigDialog(){
54       optionsDialog.refresh();
55       optionsDialog.setVisible(true);
56       SwingUtilities.updateComponentTreeUI(ConfigDialog.getInstance());
57    }
58
59    private void refresh() {
60       ArrayList cfgPanes = jaweOptionsRoot.getMembers();
61       for (int i = 0; i < cfgPanes.size(); i++){
62          ((BaseConfigPane) cfgPanes.get(i)).readConf();
63       }
64    }
65
66    private ConfigDialog(Frame owner){
67       super(owner,ResourceManager.getLanguageDependentString("ConfigTitleLabel"),true);
68
69       getContentPane().setLayout(new BorderLayout());
70       ((JPanel)getContentPane()).setBorder(
71          BorderFactory.createEmptyBorder(5,5,5,5));
72
73       cardPanel = new JPanel(new CardLayout());//panel for every group of options
74
cardPanel.setBorder(BorderFactory.createLineBorder(Color.ORANGE));
75       getContentPane().add(cardPanel, BorderLayout.CENTER);
76
77       paneTree = new JTree(treeModel = createJaWETreeModel());//panel for tree
78
paneTree.setCellRenderer(new PaneNameRenderer());
79       paneTree.putClientProperty("JTree.lineStyle", "Angled");
80       paneTree.setShowsRootHandles(true);
81       getContentPane().add(
82          new JScrollPane(paneTree,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
83                          JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED),
84          BorderLayout.WEST);
85
86       JPanel buttonsPanel = new JPanel();//panel for buttons
87

88       okButton = new JButton(
89          ResourceManager.getLanguageDependentString("OKKey"),
90          new ImageIcon(ResourceManager.getResource("OKImage")));
91       okButton.addActionListener(this);
92       getRootPane().setDefaultButton(okButton);
93
94       cancelButton = new JButton(
95          ResourceManager.getLanguageDependentString("CancelKey"),
96          new ImageIcon(ResourceManager.getResource("CancelImage")));
97       cancelButton.addActionListener(this);
98
99       applyButton = new JButton(
100          ResourceManager.getLanguageDependentString("ApplyKey"),
101          new ImageIcon(ResourceManager.getResource("ApplyImage")));
102       applyButton.addActionListener(this);
103
104       restoreDefaultButton = new JButton(
105          ResourceManager.getLanguageDependentString("RestoreDefaultKey"),
106          new ImageIcon(ResourceManager.getResource("RestoreDefaultImage")));
107       restoreDefaultButton.addActionListener(this);
108
109       buttonsPanel.add(okButton);
110       buttonsPanel.add(cancelButton);
111       buttonsPanel.add(applyButton);
112       buttonsPanel.add(Box.createHorizontalStrut(10));
113       buttonsPanel.add(restoreDefaultButton);
114       getContentPane().add(buttonsPanel, BorderLayout.SOUTH);
115
116       addWindowListener(new WindowAdapter(){
117                public void windowClosing(WindowEvent e){
118                   cancel();
119                }
120             });
121
122       TreePath jawePath = new TreePath(new Object JavaDoc[] { treeModel.getRoot(),
123                   jaweOptionsRoot.getMember(0) });
124       paneTree.setSelectionPath(jawePath);
125
126       paneTree.getSelectionModel().addTreeSelectionListener(this);
127
128       getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
129             .put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE,0,false),"Cancel");
130       getRootPane().getActionMap().put("Cancel", new AbstractAction() {
131          public void actionPerformed(ActionEvent e) {
132             cancel();
133          }
134       });
135
136       Utils.center(this,100,250);
137
138    }
139
140    private OptionTreeModel createJaWETreeModel(){
141
142       jaweOptionsRoot = new ConfigElements("JaWEConfiguration");
143
144       OptionTreeModel jaweTreeModel = new OptionTreeModel(jaweOptionsRoot);
145
146       addOptionPane(new CfgGeneral("General"), jaweOptionsRoot);
147       addOptionPane(new CfgUI("UI"), jaweOptionsRoot);
148       addOptionPane(new CfgLDAP("LDAP"), jaweOptionsRoot);
149       addOptionPane(new CfgColors("Color"), jaweOptionsRoot);
150       addOptionPane(new CfgObjectSize("Size"), jaweOptionsRoot);
151
152       return jaweTreeModel;
153    }
154
155    private void addOptionPane(BaseConfigPane child, ConfigElements parent){
156       String JavaDoc name = child.getName();
157       //cardPanel.add(child.getComponent(),name);
158
//cardPanel.add(child,name);
159

160       configsPanel = new JPanel(new BorderLayout());
161       configsPanel.add(child.getComponent(),BorderLayout.NORTH);
162       cardPanel.add(new JScrollPane(configsPanel,
163                                     JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
164                                     JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED),name);
165
166       parent.addOptionPane(child);
167    }
168
169    private void cancel(){
170       optionsDialog.setVisible(false);
171    }
172
173    private void ok(boolean close){
174       OptionTreeModel m = (OptionTreeModel) paneTree.getModel();
175       ((ConfigElements) m.getRoot()).save();
176       applyChanges();
177
178       //If OK button is pressed
179
if(close){
180          optionsDialog.setVisible(false);
181       }
182    }
183
184    private void applyChanges () {
185       OptionTreeModel m = (OptionTreeModel) paneTree.getModel();
186       ((ConfigElements) m.getRoot()).readConf();
187
188       //Changes of L'n'F, status bar and grid size and status
189
PackageEditor pe=JaWE.getInstance().getPackageEditor();
190       pe.refreshEditorConfiguration();
191       SwingUtilities.updateComponentTreeUI(ConfigDialog.getInstance());
192       //TODO ne radi bas najbolje za elemente u stablu package hijerarhije
193
JaWE.getInstance().getPackageEditor().getPackageTreePanel().refreshPackageTreePanel();
194       JaWEConfig.getInstance().saveConf();
195    }
196
197    /**
198     * Invoked when an action occurs.
199     */

200    public void actionPerformed(ActionEvent e) {
201       Object JavaDoc source = e.getSource();
202       if (source == okButton){
203          ok(true);
204       }else if (source == cancelButton){
205          cancel();
206       }else if (source == applyButton){
207          ok(false);
208       }else if (source == restoreDefaultButton){
209          JaWEConfig.getInstance().restoreDefaultSettings();
210          applyChanges();
211       }
212    }
213
214    /**
215     * Called whenever the value of the selection changes.
216     * @param evt the event that characterizes the change.
217     */

218    public void valueChanged(TreeSelectionEvent evt) {
219       TreePath path = evt.getPath();
220
221       if (path == null || !(path.getLastPathComponent() instanceof BaseConfigPane))
222          return;
223
224       Object JavaDoc[] nodes = path.getPath();
225       int lastIdx = nodes.length - 1;
226       currPaneName = null;
227
228       for (int i = paneTree.isRootVisible() ? 0 : 1; i <= lastIdx; i++){
229          if (nodes[i] instanceof BaseConfigPane){
230             currPaneName = ((BaseConfigPane)nodes[i]).getName();
231          } else if (nodes[i] instanceof ConfigElements) {
232             currPaneName = ((ConfigElements)nodes[i]).getName();
233          } else {
234             continue;
235          }
236
237       }
238       ((CardLayout) cardPanel.getLayout()).show(cardPanel, currPaneName);
239    }
240
241    public void refreshLanguageDependentStrings () {
242       setTitle(ResourceManager.getLanguageDependentString("ConfigTitleLabel"));
243       okButton.setText(ResourceManager.getLanguageDependentString("OKKey"));
244       cancelButton.setText(ResourceManager.getLanguageDependentString("CancelKey"));
245       applyButton.setText(ResourceManager.getLanguageDependentString("ApplyKey"));
246       restoreDefaultButton.setText(ResourceManager.getLanguageDependentString("RestoreDefaultKey"));
247       OptionTreeModel m = (OptionTreeModel) paneTree.getModel();
248       ((ConfigElements) m.getRoot()).refreshLanguageDependentStrings();
249    }
250
251    class PaneNameRenderer extends JLabel implements TreeCellRenderer{
252       private Border noFocusBorder = BorderFactory.createEmptyBorder(1, 1, 1, 1);
253       private Border focusBorder = BorderFactory.createLineBorder(Color.ORANGE);
254
255       private Font paneFont;
256       private Font rootFont;
257
258       public PaneNameRenderer(){
259          setOpaque(true);
260
261          paneFont = UIManager.getFont("Tree.font");
262          rootFont = new Font(paneFont.getName(),
263                               paneFont.getStyle() | Font.BOLD,
264                               paneFont.getSize()+1);
265       }
266
267       public Component getTreeCellRendererComponent(JTree tree, Object JavaDoc value,
268                                                     boolean selected,
269                                                     boolean expanded,
270                                                     boolean leaf, int row,
271                                                     boolean hasFocus){
272
273          if (selected){
274             this.setBackground(UIManager.getColor("Tree.selectionBackground"));
275             this.setForeground(UIManager.getColor("Tree.selectionForeground"));
276          } else {
277             this.setBackground(tree.getBackground());
278             this.setForeground(tree.getForeground());
279          }
280
281          String JavaDoc name = null;
282
283          if (value instanceof ConfigElements){
284             name = ((ConfigElements) value).getName();
285             this.setFont(rootFont);
286          } else if (value instanceof BaseConfigPane) {
287             name = ((BaseConfigPane) value).getName();
288             this.setFont(paneFont);
289          }
290
291          if (name == null){
292             setText(null);
293          } else {
294             String JavaDoc label = ResourceManager.getLanguageDependentString("Config."+ name +
295                                                                 ".Label");
296
297             if (label == null){
298                setText(name);
299             } else {
300                setText(label);
301             }
302          }
303
304          setBorder(hasFocus ? focusBorder : noFocusBorder);
305          return this;
306       }
307    }
308
309    class OptionTreeModel implements TreeModel{
310
311       private ConfigElements root;// = new OptionGroup("root");
312
private EventListenerList listenerList = new EventListenerList();
313
314       public OptionTreeModel(ConfigElements root){
315          this.root=root;
316       }
317
318       /**
319        * Messaged when the user has altered the value for the item identified
320        * by <code>path</code> to <code>newValue</code>.
321        * If <code>newValue</code> signifies a truly new value
322        * the model should post a <code>treeNodesChanged</code> event.
323        *
324        * @param path path to the node that the user has altered
325        * @param newValue the new value from the TreeCellEditor
326        */

327       public void valueForPathChanged(TreePath path, Object JavaDoc newValue) {
328          // TODO
329
}
330
331       /**
332        * Returns the root of the tree. Returns <code>null</code>
333        * only if the tree has no nodes.
334        *
335        * @return the root of the tree
336        */

337       public Object JavaDoc getRoot() {
338          return root;
339       }
340
341       /**
342        * Returns the index of child in parent. If <code>parent</code>
343        * is <code>null</code> or <code>child</code> is <code>null</code>,
344        * returns -1.
345        *
346        * @param parent a note in the tree, obtained from this data source
347        * @param child the node we are interested in
348        * @return the index of the child in the parent, or -1 if either
349        * <code>child</code> or <code>parent</code> are <code>null</code>
350        */

351       public int getIndexOfChild(Object JavaDoc parent, Object JavaDoc child) {
352          if (parent instanceof ConfigElements){
353             return ((ConfigElements) parent).getMemberIndex(child);
354          } else {
355             return -1;
356          }
357       }
358
359       /**
360        * Returns the child of <code>parent</code> at index <code>index</code>
361        * in the parent's
362        * child array. <code>parent</code> must be a node previously obtained
363        * from this data source. This should not return <code>null</code>
364        * if <code>index</code>
365        * is a valid index for <code>parent</code> (that is <code>index >= 0 &&
366        * index < getChildCount(parent</code>)).
367        *
368        * @param parent a node in the tree, obtained from this data source
369        * @return the child of <code>parent</code> at index <code>index</code>
370        */

371       public Object JavaDoc getChild(Object JavaDoc parent, int index) {
372          if (parent instanceof ConfigElements){
373             return ((ConfigElements)parent).getMember(index);
374          } else {
375             return null;
376          }
377       }
378
379       /**
380        * Removes a listener previously added with
381        * <code>addTreeModelListener</code>.
382        *
383        * @see #addTreeModelListener
384        * @param l the listener to remove
385        */

386       public void removeTreeModelListener(TreeModelListener l) {
387          listenerList.remove(TreeModelListener.class, l);
388       }
389
390       /**
391        * Returns the number of children of <code>parent</code>.
392        * Returns 0 if the node
393        * is a leaf or if it has no children. <code>parent</code> must be a node
394        * previously obtained from this data source.
395        *
396        * @param parent a node in the tree, obtained from this data source
397        * @return the number of children of the node <code>parent</code>
398        */

399       public int getChildCount(Object JavaDoc parent) {
400          if (parent instanceof ConfigElements){
401             return ((ConfigElements)parent).getMemberCount();
402          } else {
403             return 0;
404          }
405       }
406
407       /**
408        * Returns <code>true</code> if <code>node</code> is a leaf.
409        * It is possible for this method to return <code>false</code>
410        * even if <code>node</code> has no children.
411        * A directory in a filesystem, for example,
412        * may contain no files; the node representing
413        * the directory is not a leaf, but it also has no children.
414        *
415        * @param node a node in the tree, obtained from this data source
416        * @return true if <code>node</code> is a leaf
417        */

418       public boolean isLeaf(Object JavaDoc node) {
419          return node instanceof BaseConfigPane;
420       }
421
422       /**
423        * Adds a listener for the <code>TreeModelEvent</code>
424        * posted after the tree changes.
425        *
426        * @param l the listener to add
427        * @see #removeTreeModelListener
428        */

429       public void addTreeModelListener(TreeModelListener l) {
430          listenerList.add(TreeModelListener.class, l);
431       }
432    }
433
434
435 }
436
437
438
Popular Tags