Source code: com/virtuosotechnologies/asaph/maingui/PrefsWindowManagerImpl.java
1 /*
2 ================================================================================
3
4 FILE: PrefsWindowManagerImpl.java
5
6 PROJECT:
7
8 Asaph
9
10 CONTENTS:
11
12 Implementation of PrefsWindowManager
13
14 PROGRAMMERS:
15
16 Daniel Azuma (DA) <dazuma@kagi.com>
17
18 COPYRIGHT:
19
20 Copyright (C) 2003 Daniel Azuma (dazuma@kagi.com)
21
22 This program is free software; you can redistribute it and/or
23 modify it under the terms of the GNU General Public License as
24 published by the Free Software Foundation; either version 2
25 of the License, or (at your option) any later version.
26
27 This program is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 GNU General Public License for more details.
31
32 You should have received a copy of the GNU General Public
33 License along with this program; if not, write to
34 Free Software Foundation, Inc.
35 59 Temple Place, Suite 330
36 Boston, MA 02111-1307 USA
37
38 ================================================================================
39 */
40
41
42 package com.virtuosotechnologies.asaph.maingui;
43
44
45 import java.awt.BorderLayout;
46 import java.awt.event.WindowAdapter;
47 import java.awt.event.WindowEvent;
48 import java.util.Vector;
49 import java.util.Iterator;
50 import javax.swing.JComponent;
51 import javax.swing.JPanel;
52 import javax.swing.JList;
53 import javax.swing.JFrame;
54 import javax.swing.JScrollPane;
55 import javax.swing.BorderFactory;
56 import javax.swing.ListSelectionModel;
57 import javax.swing.event.ListSelectionListener;
58 import javax.swing.event.ListSelectionEvent;
59
60
61 /**
62 * Implementation of PrefsWindowManager
63 */
64 /*package*/ class PrefsWindowManagerImpl
65 implements PrefsWindowManager
66 {
67 private static final String STR_PrefsWindow_Title =
68 ResourceAccess.Strings.buildString("PrefsWindow_Title");
69
70
71 private class PaneInfoRec
72 {
73 private PrefsPaneProvider provider_;
74 private String title_;
75 private String description_;
76 private JComponent component_;
77
78 /*package*/ PaneInfoRec(
79 PrefsPaneProvider provider,
80 String title,
81 String description)
82 {
83 provider_ = provider;
84 title_ = title;
85 description_ = description;
86 component_ = null;
87 }
88
89 /*package*/ PrefsPaneProvider getProvider()
90 {
91 return provider_;
92 }
93
94 /*package*/ String getTitle()
95 {
96 return title_;
97 }
98
99 /*package*/ String getDescription()
100 {
101 return description_;
102 }
103
104 /*package*/ void activate(
105 JComponent parent)
106 {
107 assert component_ == null;
108 component_ = provider_.createPane(parent);
109 }
110
111 /*package*/ void deactivate()
112 {
113 assert component_ != null;
114 provider_.paneDisposed();
115 component_ = null;
116 }
117
118 /*package*/ JComponent getJComponent()
119 {
120 return component_;
121 }
122
123 public String toString()
124 {
125 return title_;
126 }
127 }
128
129
130 private Vector paneInfoRecs_;
131 private JFrame frame_;
132 private int curPaneNum_;
133 private JList categoryList_;
134
135
136 /**
137 * Constructor
138 */
139 /*package*/ PrefsWindowManagerImpl()
140 {
141 paneInfoRecs_ = new Vector();
142 curPaneNum_ = -1;
143 frame_ = null;
144 }
145
146
147 /**
148 * Opens the preferences window if it isn't already open, and brings it to the front.
149 */
150 /*package*/ void openPrefsWindow()
151 {
152 if (frame_ != null)
153 {
154 frame_.setVisible(true);
155 frame_.toFront();
156 return;
157 }
158
159 frame_ = new JFrame(STR_PrefsWindow_Title);
160 JPanel contentPanel = new JPanel(new BorderLayout());
161 frame_.setContentPane(contentPanel);
162 final JPanel panePanel = new JPanel(new BorderLayout());
163 contentPanel.add(panePanel, BorderLayout.CENTER);
164
165 for (Iterator iter = paneInfoRecs_.iterator(); iter.hasNext(); )
166 {
167 PaneInfoRec rec = (PaneInfoRec)iter.next();
168 rec.activate(contentPanel);
169 }
170
171 categoryList_ = new JList(paneInfoRecs_);
172 categoryList_.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
173 categoryList_.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
174 JScrollPane listScroller = new JScrollPane(categoryList_,
175 JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
176 contentPanel.add(listScroller, BorderLayout.WEST);
177 categoryList_.addListSelectionListener(
178 new ListSelectionListener()
179 {
180 public void valueChanged(
181 ListSelectionEvent ev)
182 {
183 if (curPaneNum_ != -1)
184 {
185 PaneInfoRec rec = (PaneInfoRec)paneInfoRecs_.get(curPaneNum_);
186 rec.getProvider().paneUnfocused();
187 }
188 panePanel.removeAll();
189 curPaneNum_ = categoryList_.getSelectedIndex();
190 if (curPaneNum_ != -1)
191 {
192 PaneInfoRec rec = (PaneInfoRec)paneInfoRecs_.get(curPaneNum_);
193 panePanel.add(rec.getJComponent(), BorderLayout.CENTER);
194 rec.getProvider().paneFocused();
195 }
196 panePanel.validate();
197 panePanel.repaint();
198 }
199 });
200
201 frame_.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
202 frame_.setSize(500, 400);
203 frame_.setVisible(true);
204 }
205
206
207 /*package*/ void closePrefsWindow()
208 {
209 if (frame_ == null)
210 {
211 return;
212 }
213 categoryList_.clearSelection();
214 for (Iterator iter = paneInfoRecs_.iterator(); iter.hasNext(); )
215 {
216 PaneInfoRec rec = (PaneInfoRec)iter.next();
217 rec.deactivate();
218 }
219 frame_.dispose();
220 frame_ = null;
221 categoryList_ = null;
222 }
223
224
225 /**
226 * Register a PrefsPaneProvider
227 *
228 * @param provider PrefsPaneProvider to register
229 * @param title title for the pane
230 * @param description a text description for the pane
231 */
232 public void registerPrefsPaneProvider(
233 PrefsPaneProvider provider,
234 String title,
235 String description)
236 {
237 paneInfoRecs_.add(new PaneInfoRec(provider, title, description));
238 }
239 }