1 3 27 28 30 package de.qfs.apps.qflog.logview.command; 31 32 34 import java.awt.Frame ; 35 36 import java.io.File ; 37 import java.io.FileInputStream ; 38 import java.io.FileOutputStream ; 39 import java.io.IOException ; 40 41 import java.util.Properties ; 42 43 import javax.swing.JFileChooser ; 44 import javax.swing.JOptionPane ; 45 import javax.swing.filechooser.FileFilter ; 46 import javax.swing.tree.TreePath ; 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 66 72 public class CommandSaveConfiguration 73 extends Command 74 implements Invokable 75 { 76 78 81 private final static Logger logger = 82 new Logger (CommandSaveConfiguration.class); 83 84 87 private boolean save; 88 89 92 private LogFrame frame; 93 94 97 private LogView view; 98 99 102 private Frame parent; 103 104 107 114 public CommandSaveConfiguration(Object 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 132 public CommandSaveConfiguration(Object source, Frame 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 143 145 152 public Object 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 182 184 191 private void save(Configuration cfg) 192 throws CommandException 193 { 194 JFileChooser fc = new JFileChooser (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 file = filter.forceExtension(fc.getSelectedFile()); 206 207 if (file.exists()) { 208 if (Message.showMessage(parent, "logView.askOverwrite", 209 new String [] {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 (file)); 224 failed = false; 225 String dir = file.getParent(); 226 if (dir != null) { 227 view.setConfigurationDirectory(new File (dir)); 228 } 229 } catch (IOException 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 [] {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 252 259 private void restore(Configuration cfg) 260 throws CommandException 261 { 262 JFileChooser fc = new JFileChooser (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 file = fc.getSelectedFile(); 274 275 boolean failed = true; 276 view.getStatusLine().pushMessage 277 (view.getResources().getString 278 ("logView.status.loadingConfig", 279 "Restoring configuration...")); 280 LogFilterTreeModel filterModel = view.getFilterView().getModel(); 282 Properties oops1 = filterModel.getConfigurableState(); 283 filterModel.removeLevel 284 (new TreePath (new Object [] {filterModel.getRoot()}), true); 285 Properties 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 (new Object [] {levelModel.getRoot()}), true); 292 } 293 try { 294 cfg.restore(new FileInputStream (file)); 295 failed = false; 296 String dir = file.getParent(); 297 if (dir != null) { 298 view.setConfigurationDirectory(new File (dir)); 299 } 300 view.getFilterView().showSetLevels(); 301 if (view.getLevelView() != null) { 302 view.getLevelView().showSetLevels(); 303 } 304 } catch (IOException 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 [] {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 328 330 334 private class MyConfiguration 335 extends Configuration 336 { 337 340 public MyConfiguration() 341 { 342 } 343 } 344 345 } 347 | Popular Tags |