From 186175e3fcc5f2dfcc7fa1ca695a5ad52767b095 Mon Sep 17 00:00:00 2001 From: myk Date: Sun, 10 Mar 2013 09:47:23 +0000 Subject: [PATCH] clean up ForgeProfileProperties and move profile dir creation to FModel --- .gitignore | 1 + src/main/java/forge/gui/GuiImportPicture.java | 56 +------------------ src/main/java/forge/model/FModel.java | 12 ++++ .../properties/ForgeProfileProperties.java | 47 ++++++++-------- src/main/java/forge/view/Main.java | 15 ----- 5 files changed, 39 insertions(+), 92 deletions(-) diff --git a/.gitignore b/.gitignore index 55b9475830b..71a6bf47a00 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ /*.iml /*.tmp /.metadata +/forge.profile.properties res/PerSetTrackingResults res/oracleScript.log res/reprintSetInfo.log diff --git a/src/main/java/forge/gui/GuiImportPicture.java b/src/main/java/forge/gui/GuiImportPicture.java index 547c8c31e91..ce77d11fb73 100644 --- a/src/main/java/forge/gui/GuiImportPicture.java +++ b/src/main/java/forge/gui/GuiImportPicture.java @@ -45,17 +45,8 @@ import forge.properties.NewConstants; import forge.util.CopyFiles; import forge.util.FileFinder; -/** - *

- * GUI_ImportPicture class. - *

- * - * @author Forge - * @version $Id$ - */ +@SuppressWarnings("serial") public class GuiImportPicture extends JDialog { - /** Constant serialVersionUID=-4191539152208389089L. */ - private static final long serialVersionUID = -4191539152208389089L; private JPanel jContentPane = null; private JLabel jLabel = null; private JLabel jLabel1 = null; @@ -65,12 +56,10 @@ public class GuiImportPicture extends JDialog { private JCheckBox jCheckBox = null; private JButton jButtonStart = null; - /** The frame. */ private final GuiImportPicture frame; private JLabel jLabelHDDFree = null; private JLabel jLabelNeedSpace = null; - /** The j label total files. */ private JLabel jLabelTotalFiles = null; private List listFiles; private ArrayList fileCopyList; @@ -79,23 +68,12 @@ public class GuiImportPicture extends JDialog { private String oldText; private JProgressBar jProgressBar = null; - /** - *

- * Constructor for GUI_ImportPicture. - *

- * - * @param owner - * a {@link javax.swing.JFrame} object. - */ public GuiImportPicture(final JFrame owner) { super(owner); this.frame = this; this.initialize(); } - /** - * This method initializes this. - */ private void initialize() { final Dimension screen = this.getToolkit().getScreenSize(); final Rectangle bounds = this.getBounds(); @@ -112,11 +90,6 @@ public class GuiImportPicture extends JDialog { this.setContentPane(this.getJContentPane()); } - /** - * This method initializes jContentPane. - * - * @return javax.swing.JPanel - */ private JPanel getJContentPane() { if (this.jContentPane == null) { this.jLabelTotalFiles = new JLabel(); @@ -163,11 +136,6 @@ public class GuiImportPicture extends JDialog { return this.jContentPane; } - /** - * This method initializes jButtonSource. - * - * @return javax.swing.JButton - */ private JButton getJButtonSource() { if (this.jButtonSource == null) { this.jButtonSource = new JButton(); @@ -254,11 +222,6 @@ public class GuiImportPicture extends JDialog { return this.jButtonSource; } - /** - * This method initializes jPanel. - * - * @return javax.swing.JPanel - */ private JPanel getJPanel() { if (this.jPanel == null) { final GridBagConstraints gridBagConstraints = new GridBagConstraints(); @@ -275,11 +238,6 @@ public class GuiImportPicture extends JDialog { return this.jPanel; } - /** - * This method initializes jCheckBox. - * - * @return javax.swing.JCheckBox - */ private JCheckBox getJCheckBox() { if (this.jCheckBox == null) { this.jCheckBox = new JCheckBox(); @@ -347,11 +305,6 @@ public class GuiImportPicture extends JDialog { return this.jCheckBox; } - /** - * This method initializes jButtonStart. - * - * @return javax.swing.JButton - */ private JButton getJButtonStart() { if (this.jButtonStart == null) { this.jButtonStart = new JButton(); @@ -404,11 +357,6 @@ public class GuiImportPicture extends JDialog { return this.jButtonStart; } - /** - * This method initializes jProgressBar. - * - * @return javax.swing.JProgressBar - */ private JProgressBar getJProgressBar() { if (this.jProgressBar == null) { this.jProgressBar = new JProgressBar(); @@ -418,4 +366,4 @@ public class GuiImportPicture extends JDialog { } return this.jProgressBar; } -} // @jve:decl-index=0:visual-constraint="10,10" +} diff --git a/src/main/java/forge/model/FModel.java b/src/main/java/forge/model/FModel.java index babd6dc9420..8ab5ce81c23 100644 --- a/src/main/java/forge/model/FModel.java +++ b/src/main/java/forge/model/FModel.java @@ -103,6 +103,18 @@ public enum FModel { // Fire up log file and exception handling ExceptionHandler.registerErrorHandling(); + // create profile dirs if they don't already exist + for (String dname : NewConstants.PROFILE_DIRS) { + File path = new File(dname); + if (path.isDirectory()) { + // already exists + continue; + } + if (!path.mkdirs()) { + throw new RuntimeException("cannot create profile directory: " + dname); + } + } + final File logFile = new File(NewConstants.LOG_FILE); final boolean deleteSucceeded = logFile.delete(); diff --git a/src/main/java/forge/properties/ForgeProfileProperties.java b/src/main/java/forge/properties/ForgeProfileProperties.java index fe454a94e0d..5f2954c1c3d 100644 --- a/src/main/java/forge/properties/ForgeProfileProperties.java +++ b/src/main/java/forge/properties/ForgeProfileProperties.java @@ -35,9 +35,9 @@ public class ForgeProfileProperties { public final String cacheDir; public final String cardPicsDir; - private final String _USER_DIR_KEY = "userDir"; - private final String _CACHE_DIR_KEY = "cacheDir"; - private final String _CARD_PICS_DIR_KEY = "cardPicsDir"; + private static final String _USER_DIR_KEY = "userDir"; + private static final String _CACHE_DIR_KEY = "cacheDir"; + private static final String _CARD_PICS_DIR_KEY = "cardPicsDir"; public ForgeProfileProperties(String filename) { Properties props = new Properties(); @@ -47,31 +47,29 @@ public class ForgeProfileProperties { props.load(new FileInputStream(propFile)); } } catch (IOException e) { - // ignore + System.err.println("error while reading from profile properties file: " + filename); } Pair defaults = _getDefaultDirs(); - - String propUserDir = props.getProperty(_USER_DIR_KEY, defaults.getLeft()); - String propCacheDir = props.getProperty(_CACHE_DIR_KEY, defaults.getRight()); - - // use defaults if the dirs are "defined" as empty strings in the properties file - propUserDir = StringUtils.isEmpty(propUserDir) ? defaults.getLeft() : propUserDir.trim(); - propCacheDir = StringUtils.isEmpty(propCacheDir) ? defaults.getRight() : propCacheDir.trim(); - - propUserDir += propUserDir.endsWith("/") || propUserDir.endsWith(File.pathSeparator) ? "" : "/"; - propCacheDir += propCacheDir.endsWith("/") || propCacheDir.endsWith(File.pathSeparator) ? "" : "/"; - - String propCardPicsDir = props.getProperty(_CARD_PICS_DIR_KEY, propCacheDir + "pics/cards/"); - propCardPicsDir += propCardPicsDir.endsWith("/") || propCardPicsDir.endsWith(File.pathSeparator) ? "" : "/"; - - userDir = propUserDir; - cacheDir = propCacheDir; - cardPicsDir = propCardPicsDir; + userDir = _getDir(props, _USER_DIR_KEY, defaults.getLeft()); + cacheDir = _getDir(props, _CACHE_DIR_KEY, defaults.getRight()); + cardPicsDir = _getDir(props, _CARD_PICS_DIR_KEY, cacheDir + "pics/cards/"); + } + + private static String _getDir(Properties props, String propertyKey, String defaultVal) { + String retDir = props.getProperty(propertyKey, defaultVal).trim(); + if (retDir.isEmpty()) { + // use default if dir is "defined" as an empty or whitespace string in the properties file + return defaultVal; + } + if (retDir.endsWith("/") || retDir.endsWith(File.pathSeparator)) { + return retDir; + } + return retDir + "/"; } // returns a pair - private Pair _getDefaultDirs() { + private static Pair _getDefaultDirs() { String osName = System.getProperty("os.name"); String homeDir = System.getProperty("user.home"); @@ -83,7 +81,8 @@ public class ForgeProfileProperties { if (StringUtils.containsIgnoreCase("windows", osName)) { // the split between appdata and localappdata on windows is relatively recent. If - // localappdata is not defined, use appdata for both. + // localappdata is not defined, use appdata for both. and if appdata is not defined, + // fall back to a linux-style dot dir in the home directory String appRoot = System.getenv().get("APPDATA"); if (StringUtils.isEmpty(appRoot)) { appRoot = fallbackDataDir; @@ -93,6 +92,8 @@ public class ForgeProfileProperties { cacheRoot = appRoot; } // just use '/' everywhere instead of file.separator. it always works + // the cache dir is Forge/Cache instead of just Forge since appRoot and cacheRoot might be the + // same directory on windows and we need to distinguish them. return Pair.of(String.format("%s/Forge", appRoot), String.format("%s/Forge/Cache", cacheRoot)); } else if (StringUtils.containsIgnoreCase("mac os x", osName)) { diff --git a/src/main/java/forge/view/Main.java b/src/main/java/forge/view/Main.java index 0eb7890cdad..c2d44a60be8 100644 --- a/src/main/java/forge/view/Main.java +++ b/src/main/java/forge/view/Main.java @@ -17,12 +17,9 @@ */ package forge.view; -import java.io.File; - import forge.Singletons; import forge.control.FControl; import forge.model.FModel; -import forge.properties.NewConstants; /** * Main class for Forge's swing application view. @@ -35,18 +32,6 @@ public final class Main { // HACK - temporary solution to "Comparison method violates it's general contract!" crash System.setProperty("java.util.Arrays.useLegacyMergeSort", "true"); - // create profile dirs if they don't already exist - for (String dname : NewConstants.PROFILE_DIRS) { - File path = new File(dname); - if (path.isDirectory()) { - // already exists - continue; - } - if (!path.mkdirs()) { - throw new RuntimeException("cannot create profile directory: " + dname); - } - } - // Start splash screen first, then data models, then controller. Singletons.setView(FView.SINGLETON_INSTANCE); Singletons.setModel(FModel.SINGLETON_INSTANCE);