Source code: com/virtuosotechnologies/asaph/maingui/MainGuiPlugin.java
1 /*
2 ================================================================================
3
4 FILE: MainGuiPlugin.java
5
6 PROJECT:
7
8 Asaph
9
10 CONTENTS:
11
12 Implementation of main gui.
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.util.logging.Logger;
46 import java.util.prefs.Preferences;
47 import java.io.File;
48 import java.awt.Dimension;
49 import java.awt.Point;
50 import java.awt.BorderLayout;
51 import java.awt.event.WindowAdapter;
52 import java.awt.event.WindowEvent;
53 import javax.swing.JComponent;
54 import javax.swing.JFrame;
55 import javax.swing.JSplitPane;
56 import javax.swing.JPanel;
57 import javax.swing.JToolBar;
58 import javax.swing.SwingUtilities;
59 import javax.swing.WindowConstants;
60
61 import com.virtuosotechnologies.lib.plugin.PluginInitializer;
62 import com.virtuosotechnologies.lib.plugin.PluginInitializerException;
63 import com.virtuosotechnologies.lib.plugin.PluginLinker;
64 import com.virtuosotechnologies.lib.swing.ModalProgressTracker;
65 import com.virtuosotechnologies.lib.asyncjob.AsyncJobRunner;
66 import com.virtuosotechnologies.lib.basiccommand.swing.CommandNodeMenuBar;
67 import com.virtuosotechnologies.lib.basiccommand.swing.CommandNodeToolBar;
68
69 import com.virtuosotechnologies.asaph.launch.Launcher;
70 import com.virtuosotechnologies.asaph.launch.LaunchCompleter;
71 import com.virtuosotechnologies.asaph.modelutils.SongUtils;
72
73
74 /**
75 * Implementation of main gui.
76 */
77 public class MainGuiPlugin
78 implements PluginInitializer
79 {
80 private static final String WINDOW_WIDTH_PREFSKEY = "mainwindow_width";
81 private static final String WINDOW_HEIGHT_PREFSKEY = "mainwindow_height";
82 private static final String WINDOW_X_PREFSKEY = "mainwindow_x";
83 private static final String WINDOW_Y_PREFSKEY = "mainwindow_y";
84 private static final String LISTS_WIDTH_PREFSKEY = "lists_width";
85 private static final String LISTS_VERTICAL_DIVIDER_PREFSKEY = "lists_top_divider";
86 private static final String LISTS_VERTICAL2_DIVIDER_PREFSKEY = "lists_bottom_divider";
87
88
89 private Logger logger_;
90
91 private Preferences prefs_;
92
93 private DatabaseManagerImpl databaseManager_;
94 private ListsImpl listsImpl_;
95 private PaneManagerImpl paneManager_;
96 private CommandManagerImpl commandManager_;
97 private GuiEnvironmentManagerImpl guiEnvironmentManager_;
98 private PrefsWindowManagerImpl prefsWindowManager_;
99 private Launcher launcher_;
100
101 private JFrame window_;
102 private JComponent dialogParent_;
103 private JSplitPane splitPane_;
104
105 private File fileChooserDirectory_;
106 private ModalProgressTracker progressTracker_;
107
108
109 /**
110 * Constructor
111 */
112 public MainGuiPlugin()
113 {
114 logger_ = Logger.getLogger("com.virtuosotechnologies.asaph.maingui");
115 logger_.fine("Starting MainGui");
116 }
117
118
119 /**
120 * Perform first initialization of the plugin. This is called after the plugin
121 * is instantiated, but before it is asked to provide any of its API implementations.
122 * Any APIs the plugin declared it needed for initialization will be available
123 * through the linker when this method is called.
124 * <p>
125 * Plugins should perform any time-consuming initialization in this method, rather
126 * than in the constructor or static initializers, and should use this method to
127 * report any fatal errors during initialization.
128 *
129 * @param linker the linker for this plugin.
130 * @exception PluginInitializerException thrown if the plugin could not
131 * initialize itself.
132 */
133 public void initialize(
134 PluginLinker linker)
135 throws
136 PluginInitializerException
137 {
138 prefs_ = Preferences.userNodeForPackage(getClass());
139
140 fileChooserDirectory_ = new File(System.getProperty("user.dir"));
141 window_ = new JFrame(ResourceAccess.Strings.buildString("MainGui_FrameTitle"));
142 JPanel contentPane = new JPanel(new BorderLayout());
143 window_.setContentPane(contentPane);
144 dialogParent_ = contentPane;
145 progressTracker_ = new ModalProgressTracker(dialogParent_);
146
147 SongUtils songUtils = (SongUtils)linker.getAPI(SongUtils.API_NAME).getImplementation();
148
149 launcher_ = (Launcher)linker.getAPI(Launcher.API_NAME).getImplementation();
150 prefsWindowManager_ = new PrefsWindowManagerImpl();
151 databaseManager_ = new DatabaseManagerImpl();
152 listsImpl_ = new ListsImpl(songUtils, databaseManager_, dialogParent_);
153 paneManager_ = new PaneManagerImpl(listsImpl_);
154 guiEnvironmentManager_ = new GuiEnvironmentManagerImpl();
155 commandManager_ = new CommandManagerImpl(dialogParent_, launcher_, songUtils, databaseManager_,
156 guiEnvironmentManager_, listsImpl_, paneManager_, prefsWindowManager_, this);
157 paneManager_.setCommandManager(commandManager_);
158 listsImpl_.setCommandManager(commandManager_);
159 prefsWindowManager_.registerPrefsPaneProvider(
160 new MainGuiPrefsPane(listsImpl_),
161 ResourceAccess.Strings.buildString("MainGuiPrefs_Title"),
162 ResourceAccess.Strings.buildString("MainGuiPrefs_Description"));
163
164 splitPane_ = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
165 listsImpl_.getJComponent(), paneManager_.getJComponent());
166 contentPane.add(splitPane_, BorderLayout.CENTER);
167 // contentPane.add(new CommandNodeToolBar(commandManager_.getToolBarRoot()),
168 // BorderLayout.NORTH);
169 window_.setJMenuBar(new CommandNodeMenuBar(commandManager_.getMenuBarRoot()));
170
171 window_.setSize(prefs_.getInt(WINDOW_WIDTH_PREFSKEY, 800),
172 prefs_.getInt(WINDOW_HEIGHT_PREFSKEY, 600));
173 window_.validate();
174 splitPane_.setDividerLocation(prefs_.getInt(LISTS_WIDTH_PREFSKEY, 200));
175 listsImpl_.setTopSplitPanePosition(prefs_.getInt(LISTS_VERTICAL_DIVIDER_PREFSKEY, 100));
176 listsImpl_.setBottomSplitPanePosition(prefs_.getInt(LISTS_VERTICAL2_DIVIDER_PREFSKEY, -1));
177 Point loc = new Point(prefs_.getInt(WINDOW_X_PREFSKEY, -1),
178 prefs_.getInt(WINDOW_Y_PREFSKEY, -1));
179 if (loc.x >=0 && loc.y >= 0)
180 {
181 window_.setLocation(loc);
182 }
183
184 window_.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
185 window_.addWindowListener(
186 new WindowAdapter()
187 {
188 public void windowClosing(
189 WindowEvent ev)
190 {
191 commandManager_.doSafeQuit();
192 }
193 });
194
195 launcher_.addLaunchCompleter(
196 new LaunchCompleter()
197 {
198 public void completeLaunch()
199 {
200 SwingUtilities.invokeLater(
201 new Runnable()
202 {
203 public void run()
204 {
205 window_.setVisible(true);
206 }
207 });
208 }
209 });
210
211 logger_.fine("Initialized MainGui");
212 }
213
214
215 /**
216 * A plugin must implement this method to provide the implementations of the APIs
217 * that it provides. This method is called after the initialize() method.
218 * Any APIs the plugin declared it needed in to implement this API will be available
219 * through the linker when this method is called.
220 * <p>
221 * Plugins should perform any time-consuming initialization in this method, rather
222 * than in the constructor or static initializers, and should use this method to
223 * report any fatal errors during initialization.
224 *
225 * @param apiName the name of the API to implement
226 * @param linker the linker for this plugin.
227 * @return an object implementing the API.
228 * @exception PluginInitializerException thrown if the plugin could not implement the API.
229 */
230 public Object getAPIImplementation(
231 String apiName,
232 PluginLinker linker)
233 throws
234 PluginInitializerException
235 {
236 if (apiName.equals(CommandManager.API_NAME))
237 {
238 return commandManager_;
239 }
240 else if (apiName.equals(DatabaseManager.API_NAME))
241 {
242 return databaseManager_;
243 }
244 else if (apiName.equals(GuiEnvironmentManager.API_NAME))
245 {
246 return guiEnvironmentManager_;
247 }
248 else if (apiName.equals(ListUpdateManager.API_NAME))
249 {
250 return listsImpl_;
251 }
252 else if (apiName.equals(PaneManager.API_NAME))
253 {
254 return paneManager_;
255 }
256 else if (apiName.equals(SelectionManager.API_NAME))
257 {
258 return listsImpl_;
259 }
260 else if (apiName.equals(PrefsWindowManager.API_NAME))
261 {
262 return prefsWindowManager_;
263 }
264 throw new PluginInitializerException("Unknown API: "+apiName);
265 }
266
267
268 /*package*/ void shutDown()
269 {
270 listsImpl_.shutdown();
271 Dimension dim = window_.getSize();
272 prefs_.putInt(WINDOW_WIDTH_PREFSKEY, dim.width);
273 prefs_.putInt(WINDOW_HEIGHT_PREFSKEY, dim.height);
274 Point loc = window_.getLocation();
275 prefs_.putInt(WINDOW_X_PREFSKEY, loc.x);
276 prefs_.putInt(WINDOW_Y_PREFSKEY, loc.y);
277 prefs_.putInt(LISTS_WIDTH_PREFSKEY, splitPane_.getDividerLocation());
278 prefs_.putInt(LISTS_VERTICAL_DIVIDER_PREFSKEY, listsImpl_.getTopSplitPanePosition());
279 prefs_.putInt(LISTS_VERTICAL2_DIVIDER_PREFSKEY, listsImpl_.getBottomSplitPanePosition());
280 window_.dispose();
281 }
282
283
284 private class GuiEnvironmentManagerImpl
285 implements GuiEnvironmentManager
286 {
287 /**
288 * Get the dialog parent. This component should be used as the parent
289 * for displaying JDialogs and JOptionPanes.
290 *
291 * @return the gui's dialog parent component
292 */
293 public JComponent getDialogParent()
294 {
295 return dialogParent_;
296 }
297
298
299 /**
300 * Get the current directory for file choosers
301 *
302 * @return the current file chooser directory
303 */
304 public File getFileChooserDirectory()
305 {
306 return fileChooserDirectory_;
307 }
308
309
310 /**
311 * Set the current directory for file choosers
312 *
313 * @param directory new file chooser directory
314 */
315 public void setFileChooserDirectory(
316 File directory)
317 {
318 fileChooserDirectory_ = directory;
319 }
320
321
322 /**
323 * Register an AsyncJobRunner that can run long operations. The main
324 * gui will respond to jobs being run on this runner by showing a progress
325 * dialog.
326 *
327 * @param runner AsyncJobRunner
328 */
329 public void registerAsyncJobRunner(
330 AsyncJobRunner runner)
331 {
332 runner.addAsyncJobListener(progressTracker_);
333 }
334 }
335 }