KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > rero > dialogs > OptionWindow


1 package rero.dialogs;
2
3 import javax.swing.*;
4 import javax.swing.event.*;
5 import java.awt.*;
6 import java.awt.event.*;
7 import javax.swing.table.*;
8 import javax.swing.tree.*;
9
10 import rero.config.*;
11 import rero.dck.*;
12
13 import rero.gui.*;
14
15 import java.util.*;
16
17 public class OptionWindow extends JDialog implements DCapabilities, TreeSelectionListener
18 {
19     private static OptionWindow dialog;
20
21     JPanel content;
22     JLabel title;
23
24     HashMap dialogs;
25     DefaultMutableTreeNode items;
26
27     public DMain current;
28
29     public void forceSave()
30     {
31        if (current != null)
32        {
33           current.save();
34           ClientState.getClientState().sync();
35        }
36     }
37
38     public void refresh()
39     {
40        if (current != null)
41           current.refresh();
42     }
43
44     public void closeDialog()
45     {
46        KeyBindings.is_dialog_active = false;
47        setVisible(false);
48     }
49
50     public void saveCurrent(DMain newDialog)
51     {
52        if (current != null)
53        {
54           current.save();
55           ClientState.getClientState().sync();
56        }
57
58        current = newDialog;
59     }
60
61     public static void displaySpecificDialog(String JavaDoc name)
62     {
63        dialog.displayDialog(name);
64     }
65
66     public void displayDialog(String JavaDoc name)
67     {
68        if (dialogs.containsKey(name))
69        {
70           changeDialogs((DMain)dialogs.get(name));
71        }
72     }
73
74     public String JavaDoc addDialog(DMain dialog)
75     {
76        dialogs.put(dialog.getTitle(), dialog);
77        dialog.installCapabilities(this);
78
79        return dialog.getTitle();
80     }
81
82     public void buildTables()
83     {
84        dialogs = new HashMap();
85
86        items = new DefaultMutableTreeNode("Options");
87        
88        DefaultMutableTreeNode category, option;
89
90        current = new SetupDialog();
91
92        category = new DefaultMutableTreeNode("Setup"); items.add(category);
93           option = new DefaultMutableTreeNode(addDialog(current)); category.add(option);
94           option = new DefaultMutableTreeNode(addDialog(new IdentDialog())); category.add(option);
95           option = new DefaultMutableTreeNode(addDialog(new ProxyDialog())); category.add(option);
96           option = new DefaultMutableTreeNode(addDialog(new PerformDialog())); category.add(option);
97
98        category = new DefaultMutableTreeNode("Client Options"); items.add(category);
99           option = new DefaultMutableTreeNode(addDialog(new IRCOptions())); category.add(option);
100           option = new DefaultMutableTreeNode(addDialog(new ClientOptions())); category.add(option);
101           option = new DefaultMutableTreeNode(addDialog(new DCCOptions())); category.add(option);
102           option = new DefaultMutableTreeNode(addDialog(new LoggingDialog())); category.add(option);
103           option = new DefaultMutableTreeNode(addDialog(new AutoWindowDialog())); category.add(option);
104
105        category = new DefaultMutableTreeNode("Scripts"); items.add(category);
106           option = new DefaultMutableTreeNode(addDialog(new ScriptDialog())); category.add(option);
107           option = new DefaultMutableTreeNode(addDialog(new ThemeDialog())); category.add(option);
108
109        category = new DefaultMutableTreeNode("User Lists"); items.add(category);
110           option = new DefaultMutableTreeNode(addDialog(new NotifyDialog())); category.add(option);
111           option = new DefaultMutableTreeNode(addDialog(new IgnoreDialog())); category.add(option);
112
113        category = new DefaultMutableTreeNode("Interface Options"); items.add(category);
114           option = new DefaultMutableTreeNode(addDialog(new UIDialog())); category.add(option);
115           option = new DefaultMutableTreeNode(addDialog(new FontDialog())); category.add(option);
116           option = new DefaultMutableTreeNode(addDialog(new SwitchBarDialog())); category.add(option);
117           option = new DefaultMutableTreeNode(addDialog(new WindowsDialog())); category.add(option);
118           option = new DefaultMutableTreeNode(addDialog(new ImageDialog())); category.add(option);
119           option = new DefaultMutableTreeNode(addDialog(new ExternalDialog())); category.add(option);
120     }
121
122     public void valueChanged(TreeSelectionEvent e)
123     {
124        JTree theTree = (JTree)e.getSource();
125        DefaultMutableTreeNode node = (DefaultMutableTreeNode)theTree.getLastSelectedPathComponent();
126
127        if (node == null)
128           return;
129
130        DMain temp = (DMain)dialogs.get(node.getUserObject());
131        changeDialogs(temp);
132     }
133
134     private void changeDialogs(DMain temp)
135     {
136        if (temp == null)
137           return;
138
139        title.setText(temp.getDescription());
140
141        saveCurrent(temp);
142
143        content.removeAll();
144        content.add(temp.getDialog(), BorderLayout.CENTER);
145        temp.refresh();
146
147        content.revalidate();
148        content.repaint();
149     }
150
151     private static Frame frame;
152
153     public static void initialize(Component comp)
154     {
155        if (JOptionPane.getFrameForComponent(comp) != frame)
156        {
157            frame = JOptionPane.getFrameForComponent(comp);
158            dialog = new OptionWindow(frame);
159        }
160     }
161
162     public static String JavaDoc showDialog(Component comp)
163     {
164         // tell the client state to save the state
165
KeyBindings.is_dialog_active = true;
166         dialog.setLocationRelativeTo(comp);
167         dialog.setVisible(true);
168
169         dialog.addWindowListener(new WindowAdapter()
170         {
171            public void windowClosing(WindowEvent ev)
172            {
173                KeyBindings.is_dialog_active = false;
174            }
175         });
176
177         dialog.refresh();
178
179         return "";
180     }
181
182     private OptionWindow(Frame frame)
183     {
184         super(frame, "jIRCii Options", false);
185
186         buildTables();
187
188         //
189
//buttons
190
//
191
JButton closeButton = new JButton("OK");
192         closeButton.setMnemonic('O');
193
194         JButton cancelButton = new JButton("Cancel");
195         cancelButton.setMnemonic('C');
196
197         closeButton.addActionListener(new ActionListener()
198         {
199             public void actionPerformed(ActionEvent e)
200             {
201                 forceSave();
202                 OptionWindow.dialog.setVisible(false);
203                 KeyBindings.is_dialog_active = false;
204             }
205         });
206  
207         cancelButton.addActionListener(new ActionListener()
208         {
209             public void actionPerformed(ActionEvent e)
210             {
211                 // tell client state that we canceled
212

213                 OptionWindow.dialog.setVisible(false);
214                 dialog = null;
215                 OptionWindow.frame = null;
216                 KeyBindings.is_dialog_active = false;
217             }
218         });
219
220         getContentPane().setLayout(new BorderLayout(5, 5));
221         
222         JPanel main = new JPanel();
223     main.setLayout(new BorderLayout(5, 5));
224     main.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
225
226         getContentPane().add(main, BorderLayout.CENTER);
227
228         //
229
// Left - the tabbed pane and its doings
230
//
231
JPanel general = new JPanel();
232         general.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
233
234         JPanel left = new JPanel();
235         left.setBorder(BorderFactory.createEtchedBorder());
236         left.setPreferredSize(new Dimension(165, 295));
237         left.setLayout(new BorderLayout());
238         left.add(general, BorderLayout.CENTER);
239
240     //
241
// General Tab
242
//
243
JTree genOptions = new JTree(items);
244         genOptions.setRootVisible(false);
245         genOptions.setToggleClickCount(1); // 1 click to expand the tree...
246
genOptions.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
247
248         for (int x = 0; x < genOptions.getRowCount(); x++)
249            genOptions.expandPath(genOptions.getPathForRow(x));
250
251         genOptions.addTreeSelectionListener(this);
252
253         JScrollPane genScroller = new JScrollPane(genOptions);
254         genScroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
255         genScroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
256
257         general.setLayout(new BorderLayout(5, 5));
258
259         general.add(new JLabel("Options:"), BorderLayout.NORTH);
260         general.add(genScroller, BorderLayout.CENTER);
261
262         //
263
// Center - the display pane and its doings
264
//
265
JPanel center = new JPanel();
266     center.setLayout(new BorderLayout(5, 5));
267
268         JPanel titlep = new JPanel();
269         title = new JLabel("Configuration");
270         
271         titlep.add(title, BorderLayout.CENTER);
272         titlep.setBorder(BorderFactory.createEtchedBorder());
273
274         center.add(titlep, BorderLayout.NORTH);
275         
276         content = new JPanel();
277         content.setLayout(new BorderLayout());
278         content.setBorder(BorderFactory.createEtchedBorder());
279
280         center.add(content, BorderLayout.CENTER);
281
282         content.add(current.getDialog(), BorderLayout.CENTER);
283
284         //
285
// Bottom - a Close button aligned to the right
286
//
287
JPanel south = new JPanel();
288     south.setLayout(new BorderLayout(5, 5));
289
290         JPanel evil = new JPanel();
291         GridLayout gl = new GridLayout(1, 3);
292         gl.setHgap(5);
293         evil.setLayout(gl);
294         evil.add(closeButton);
295         evil.add(cancelButton);
296
297     south.add(evil, BorderLayout.EAST);
298         south.add(new JPanel(), BorderLayout.CENTER);
299
300     //
301
// putting it all together
302
//
303
main.add(left, BorderLayout.WEST);
304     main.add(center, BorderLayout.CENTER);
305         main.add(south, BorderLayout.SOUTH);
306
307         pack();
308
309     setSize(new Dimension(520, 400)); // was 520x363
310
}
311 }
312
Popular Tags