Author: tchemit Date: 2008-01-18 01:48:07 +0000 (Fri, 18 Jan 2008) New Revision: 177 Added: trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/SimExplorerContext.java Log: le context utilisateur de l'application, un singleton charg?\195?\169 ?\195?\160 l'init de l'application et qui contient le parseur d'options, la config, ... Added: trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/SimExplorerContext.java =================================================================== --- trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/SimExplorerContext.java (rev 0) +++ trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/SimExplorerContext.java 2008-01-18 01:48:07 UTC (rev 177) @@ -0,0 +1,127 @@ +/* +* ##% Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Code Lutin, +* Tony Chemit +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +* ##% */ +package fr.cemagref.simexplorer.is; + +import fr.cemagref.simexplorer.is.actions.SimExplorerCommonActions; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.codelutin.option.ParserException; + +import java.io.BufferedOutputStream; +import java.io.FileOutputStream; +import java.io.IOException; + +/** + * Le context de l'application, implanté en singleton. + * <p/> + * Contient la configuration de l'application, le parseur d'options,... + * + * @author chemit + */ +public class SimExplorerContext { + + /** to use log facility, just put in your code: log.info(\"...\"); */ + static private Log log = LogFactory.getLog(SimExplorerContext.class); + + private SimExplorerOptionParser parser; + private boolean quit; + private boolean launchUI=true; + private boolean firstLaunch; + + public SimExplorerOptionParser getParser() { + if (parser == null) { + // creation du parseur + parser = new SimExplorerOptionParser(); + // enregistrement des actions concretes + parser.registerActions(SimExplorerCommonActions.class); + + // enregistrement des configs concretes + parser.registerConfig(SimExplorerConfig.class); + } + return parser; + } + + public SimExplorerConfig getConfig() { + return (SimExplorerConfig) getParser().getMainConfig(); + } + + public void save() throws IOException { + if (getConfig().getSource() == null) { + return; + } + BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(getConfig().getSource())); + try { + getConfig().save(out); + } finally { + out.flush(); + out.close(); + } + } + + public void saveSafely() { + try { + save(); + } catch (Exception e) { + log.warn("simexplorer.error.unsafe.save.config" + e.getMessage(), e); + } + } + + public void setQuit(boolean quit) { + this.quit = quit; + } + + public boolean isQuit() { + return quit; + } + + public void setLaunchUI(boolean launchUI) { + this.launchUI = launchUI; + } + + public boolean isLaunchUI() { + return launchUI; + } + + public void setFirstLaunch(boolean firstLaunch) { + this.firstLaunch = firstLaunch; + } + + public boolean isFirstLaunch() { + return firstLaunch; + } + + /** restricted access constructor */ + SimExplorerContext() { + + } + + void initParser(String... args) { + try { + getParser().doParse(args); + } catch (ParserException e) { + throw new RuntimeException(e); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + void initConfig() { + getConfig().initI18n(); + } +}