/* * UFO Saved Game Editor * Copyright (C) 2010 Christopher Davoren * * 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 3 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, see <http://www.gnu.org/licenses/>. */ package net.rubikscomplex.ufosge; import java.io.IOException; import java.util.ArrayList; import javax.swing.JOptionPane; import javax.swing.SwingUtilities; import javax.swing.UIManager; import net.rubikscomplex.ufosge.data.SaveInfo; import net.rubikscomplex.ufosge.data.SavedGame; import net.rubikscomplex.ufosge.gui.PreferencesDialog; import net.rubikscomplex.ufosge.gui.SaveSelectFrame; import net.rubikscomplex.ufosge.resources.BaseBit; import net.rubikscomplex.ufosge.resources.BigLetter; import net.rubikscomplex.ufosge.resources.ResourceManager; /** * Main class - contains the main() method which is executed at application * startup and constants relating to command line arguments. * * @author Chris Davoren */ public class Main { /** * String of the command line argument that deletes all preferences for the application. */ public static final String ARG_DELETE_PREFS = "cleanprefs"; /** * Initialises and displays the GUI. * * The look and feel is set, resources are loaded before starting the * attempting to load the list of saved games. This list is displayed in * a new SaveSelectFrame as the first interactive window presented to the * user. */ private static void createAndShowGUI() { ArrayList<SaveInfo> sgSummaries = null; try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { JOptionPane.showMessageDialog(null, "Unable to set the UI look and feel. Appearance may be unusual.", "Warning", JOptionPane.WARNING_MESSAGE); } try { BaseBit.loadBaseBits(); BigLetter.loadBigLetters(); } catch (IOException e) { JOptionPane.showMessageDialog(null, "An I/O error occured while loading a required resource:\n\n" + e.getMessage() + "\n\nPlease check that the program installed correctly.", "Load Error", JOptionPane.ERROR_MESSAGE); System.exit(-1); } if (ResourceManager.getUserPreferences().get(ResourceManager.PREF_UFO_DIR, null) == null) { PreferencesDialog prefsDialog = new PreferencesDialog(null); prefsDialog.setVisible(true); if (prefsDialog.getResult() == PreferencesDialog.RESULT_CANCEL) { System.exit(-1); } } int preferencesDialogResult = PreferencesDialog.RESULT_OK; while (sgSummaries == null && preferencesDialogResult == PreferencesDialog.RESULT_OK) { try { sgSummaries = SavedGame.loadSummaries(ResourceManager.getUserPreferences().get(ResourceManager.PREF_UFO_DIR, null)); } catch (IOException e) { JOptionPane.showMessageDialog(null, "Unable to load saved game listing. Please confirm your UFO directory.", "Load Error", JOptionPane.ERROR_MESSAGE); PreferencesDialog prefsDialog = new PreferencesDialog(null); prefsDialog.setVisible(true); preferencesDialogResult = prefsDialog.getResult(); } } try { SaveSelectFrame saveSelectFrame = new SaveSelectFrame(sgSummaries); saveSelectFrame.setVisible(true); } catch (Exception e) { JOptionPane.showMessageDialog(null, "A fatal error has occurred:\n\n" + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); System.exit(-1); } } /** * Main startup method that loads resources and starts the graphical user * interface. * * Command line arguments are parsed before the GUI is started. The effect * of some command line arguments may cause the program to terminate without * starting the GUI. ARG_DELETE_PREFS is one example of this - if this * command line argument is found, application preferences will be entirely * deleted and then the program will quit. * * @param args the command line arguments */ public static void main(String[] args) { for(int i = 0; i < args.length; i++) { if (args[i].equals("-" + ARG_DELETE_PREFS)) { ResourceManager.deleteUserPreferences(); return; } } SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }