KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > qfs > apps > qflog > logview > command > CommandSaveConfiguration


1 // {{{ copyright
2

3 /********************************************************************
4  *
5  * $Id: CommandSaveConfiguration.java,v 1.6 2000/07/05 14:07:45 gs Exp $
6  *
7  * The contents of this file are subject to the Mozilla Public
8  * License Version 1.1 (the "License"); you may not use this file
9  * except in compliance with the License. You may obtain a copy of
10  * the License at http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS
13  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
14  * implied. See the License for the specific language governing
15  * rights and limitations under the License.
16  *
17  * The Original Code is qfs.de code.
18  *
19  * The Initial Developer of the Original Code is Gregor Schmid.
20  * Portions created by Gregor Schmid are
21  * Copyright (C) 1999 Quality First Software, Gregor Schmid.
22  * All Rights Reserved.
23  *
24  * Contributor(s):
25  *
26  *******************************************************************/

27
28 // }}}
29

30 package de.qfs.apps.qflog.logview.command;
31
32 // {{{ imports
33

34 import java.awt.Frame JavaDoc;
35
36 import java.io.File JavaDoc;
37 import java.io.FileInputStream JavaDoc;
38 import java.io.FileOutputStream JavaDoc;
39 import java.io.IOException JavaDoc;
40
41 import java.util.Properties JavaDoc;
42
43 import javax.swing.JFileChooser JavaDoc;
44 import javax.swing.JOptionPane JavaDoc;
45 import javax.swing.filechooser.FileFilter JavaDoc;
46 import javax.swing.tree.TreePath JavaDoc;
47
48 import de.qfs.lib.command.CancelCommandException;
49 import de.qfs.lib.command.Command;
50 import de.qfs.lib.command.CommandException;
51 import de.qfs.lib.command.Invokable;
52 import de.qfs.lib.command.MessageCommandException;
53 import de.qfs.lib.config.Configuration;
54 import de.qfs.lib.gui.Message;
55 import de.qfs.lib.log.Log;
56 import de.qfs.lib.log.Logger;
57
58 import de.qfs.apps.qflog.logview.LogFilterTreeModel;
59 import de.qfs.apps.qflog.logview.LogLevelTreeModel;
60 import de.qfs.apps.qflog.logview.LogFileFilter;
61 import de.qfs.apps.qflog.logview.LogFrame;
62 import de.qfs.apps.qflog.logview.LogView;
63
64 // }}}
65

66 /**
67  * Save or restore a LogView's or LogFrame's Configuration.
68  *
69  * @author Gregor Schmid
70  * @version $Revision: 1.6 $
71  */

72 public class CommandSaveConfiguration
73     extends Command
74     implements Invokable
75 {
76     // {{{ variables
77

78     /**
79      * The Logger used for logging.
80      */

81     private final static Logger logger =
82         new Logger (CommandSaveConfiguration.class);
83
84     /**
85      * True to save, false to restore.
86      */

87     private boolean save;
88
89     /**
90      * The LogFrame whose configuration should be saved.
91      */

92     private LogFrame frame;
93
94     /**
95      * The LogView whose configuration should be saved.
96      */

97     private LogView view;
98
99     /**
100      * Another frame parent for the dialog.
101      */

102     private Frame JavaDoc parent;
103
104     // }}}
105
// {{{ constructors
106

107     /**
108      * Create a new CommandSaveConfiguration for a LogFrame.
109      *
110      * @param source The source of the command.
111      * @param frame The LogFrame whose configuration should be saved.
112      * @param save True to save, false to restore.
113      */

114     public CommandSaveConfiguration(Object JavaDoc source, LogFrame frame,
115                                     boolean save)
116     {
117         super(source);
118         parent = frame;
119         this.frame = frame;
120         view = frame.getView();
121         this.save = save;
122     }
123
124     /**
125      * Create a new CommandSaveConfiguration for a LogView.
126      *
127      * @param source The source of the command.
128      * @param parent A parent frame for the file dialog.
129      * @param view The LogView whose configuration should be saved.
130      * @param save True to save, false to restore.
131      */

132     public CommandSaveConfiguration(Object JavaDoc source, Frame JavaDoc parent, LogView view,
133                                     boolean save)
134     {
135         super(source);
136         this.parent = parent;
137         this.view = view;
138         this.save = save;
139     }
140
141     // }}}
142

143     // {{{ invoke
144

145     /**
146      * Invoke the command.
147      *
148      * @return Always null.
149      *
150      * @throws CommandException If an I/O error occurs.
151      */

152     public Object JavaDoc invoke()
153         throws CommandException
154     {
155         boolean excluded = ! view.isLoggingEnabled();
156         if (excluded) {
157             Log.excludeThread();
158         }
159         try {
160             MyConfiguration cfg = new MyConfiguration ();
161
162             if (frame != null) {
163                 frame.getConfigurator().copy().register(cfg);
164             }
165             view.getConfigurator().copy().register(cfg);
166
167             if (save) {
168                 save(cfg);
169             } else {
170                 restore(cfg);
171             }
172             return null;
173         } finally {
174             if (excluded) {
175                 Log.includeThread();
176             }
177         }
178     }
179
180     // }}}
181

182     // {{{ save
183

184     /**
185      * Save the configuration.
186      *
187      * @param cfg The Configuration to save.
188      *
189      * @throws CommandException If an I/O error occurs.
190      */

191     private void save(Configuration cfg)
192         throws CommandException
193     {
194         JFileChooser JavaDoc fc = new JFileChooser JavaDoc (view.getConfigurationDirectory());
195
196         LogFileFilter filter = new LogFileFilter
197             (view.getResources().getString("logView.fileFilter.qlc",
198                                            "qflog configuration file"),
199              ".qlc");
200         fc.setFileFilter(filter);
201
202         if (fc.showSaveDialog(parent) != fc.APPROVE_OPTION) {
203             throw new CancelCommandException ();
204         }
205         File JavaDoc file = filter.forceExtension(fc.getSelectedFile());
206
207         if (file.exists()) {
208             if (Message.showMessage(parent, "logView.askOverwrite",
209                                     new String JavaDoc[] {file.getName()})
210                 != JOptionPane.OK_OPTION) {
211                 throw new CancelCommandException ();
212             }
213         }
214
215         boolean failed = true;
216         if (view.getStatusLine() != null) {
217             view.getStatusLine().pushMessage
218                 (view.getResources().getString
219                  ("logView.status.savingConfig",
220                   "Saving configuration..."));
221         }
222         try {
223             cfg.save(new FileOutputStream JavaDoc (file));
224             failed = false;
225             String JavaDoc dir = file.getParent();
226             if (dir != null) {
227                 view.setConfigurationDirectory(new File JavaDoc (dir));
228             }
229         } catch (IOException JavaDoc ex) {
230             if (logger.level >= Log.WRN) {
231                 logger.log(Log.WRN, "save(Configuration)", ex);
232             }
233             throw new MessageCommandException
234                 (ex.getMessage(), parent, "logView.saveError",
235                  new String JavaDoc[] {file.getPath()});
236         } finally {
237             if (view.getStatusLine() != null) {
238                 view.getStatusLine().popMessage();
239                 view.getStatusLine().setTemporaryMessage
240                     (view.getResources().getString
241                      (failed ? "logView.status.savingConfig.failed"
242                       : "logView.status.savingConfig.done",
243                       "Saving configuration..." +
244                       (failed ? "failed" : "done")));
245             }
246         }
247     }
248
249     // }}}
250
// {{{ restore
251

252     /**
253      * Restore the configuration.
254      *
255      * @param cfg The Configuration to restore.
256      *
257      * @throws CommandException If an I/O error occurs.
258      */

259     private void restore(Configuration cfg)
260         throws CommandException
261     {
262         JFileChooser JavaDoc fc = new JFileChooser JavaDoc (view.getConfigurationDirectory());
263
264         LogFileFilter filter = new LogFileFilter
265             (view.getResources().getString("logView.fileFilter.qlc",
266                                         "qflog configuration file"),
267              ".qlc");
268         fc.setFileFilter(filter);
269
270         if (fc.showOpenDialog(parent) != fc.APPROVE_OPTION) {
271             throw new CancelCommandException ();
272         }
273         File JavaDoc file = fc.getSelectedFile();
274
275         boolean failed = true;
276         view.getStatusLine().pushMessage
277             (view.getResources().getString
278              ("logView.status.loadingConfig",
279               "Restoring configuration..."));
280         // If the restore fails we need to reset the current levels
281
LogFilterTreeModel filterModel = view.getFilterView().getModel();
282         Properties JavaDoc oops1 = filterModel.getConfigurableState();
283         filterModel.removeLevel
284             (new TreePath JavaDoc (new Object JavaDoc[] {filterModel.getRoot()}), true);
285         Properties JavaDoc oops2 = null;
286         LogLevelTreeModel levelModel = null;
287         if (view.getLevelView() != null) {
288             levelModel = view.getLevelView().getModel();
289             oops2 = levelModel.getConfigurableState();
290             levelModel.removeLogLevel
291                 (new TreePath JavaDoc (new Object JavaDoc[] {levelModel.getRoot()}), true);
292         }
293         try {
294             cfg.restore(new FileInputStream JavaDoc (file));
295             failed = false;
296             String JavaDoc dir = file.getParent();
297             if (dir != null) {
298                 view.setConfigurationDirectory(new File JavaDoc (dir));
299             }
300             view.getFilterView().showSetLevels();
301             if (view.getLevelView() != null) {
302                 view.getLevelView().showSetLevels();
303             }
304         } catch (IOException JavaDoc ex) {
305             if (logger.level >= Log.WRN) {
306                 logger.log(Log.WRN, "restore(Configuration)", ex);
307             }
308             filterModel.setConfigurableState(oops1);
309             if (levelModel != null) {
310                 levelModel.setConfigurableState(oops2);
311             }
312             throw new MessageCommandException
313                 (ex.getMessage(), parent, "logView.loadError",
314                  new String JavaDoc[] {file.getPath()});
315         } finally {
316             view.getStatusLine().popMessage();
317             view.getStatusLine().setTemporaryMessage
318                 (view.getResources().getString
319                  (failed ? "logView.status.loadingConfig.failed"
320                   : "logView.status.loadingConfig.done",
321                   "Loading configuration..." +
322                   (failed ? "failed" : "done")));
323         }
324     }
325
326     // }}}
327

328     // {{{ class Configuration
329

330     /**
331      * Helper class that allows Construction of a separate Configuration
332      * instance.
333      */

334     private class MyConfiguration
335         extends Configuration
336     {
337         /**
338          * Create a new MyConfiguration.
339          */

340         public MyConfiguration()
341         {
342         }
343     }
344
345     // }}}
346
}
347
Popular Tags