clean up ForgeProfileProperties and move profile dir creation to FModel

This commit is contained in:
myk
2013-03-10 09:47:23 +00:00
parent ba1595f8c2
commit 186175e3fc
5 changed files with 39 additions and 92 deletions

1
.gitignore vendored
View File

@@ -2,6 +2,7 @@
/*.iml
/*.tmp
/.metadata
/forge.profile.properties
res/PerSetTrackingResults
res/oracleScript.log
res/reprintSetInfo.log

View File

@@ -45,17 +45,8 @@ import forge.properties.NewConstants;
import forge.util.CopyFiles;
import forge.util.FileFinder;
/**
* <p>
* GUI_ImportPicture class.
* </p>
*
* @author Forge
* @version $Id$
*/
@SuppressWarnings("serial")
public class GuiImportPicture extends JDialog {
/** Constant <code>serialVersionUID=-4191539152208389089L</code>. */
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<File> listFiles;
private ArrayList<File> fileCopyList;
@@ -79,23 +68,12 @@ public class GuiImportPicture extends JDialog {
private String oldText;
private JProgressBar jProgressBar = null;
/**
* <p>
* Constructor for GUI_ImportPicture.
* </p>
*
* @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"
}

View File

@@ -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();

View File

@@ -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<String, String> 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 <userDir, cacheDir>
private Pair<String, String> _getDefaultDirs() {
private static Pair<String, String> _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)) {

View File

@@ -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);