From 86d5eca3673a56966069e428923ba6320ebfed87 Mon Sep 17 00:00:00 2001 From: Diego Waxemberg Date: Fri, 20 Dec 2013 16:55:30 -0500 Subject: [PATCH] added a menu item for preferences and created a framework for preferences still requires some implementation before fully functional. --- src/com/limelight/gui/MainFrame.java | 16 +++- src/com/limelight/gui/PreferencesFrame.java | 83 +++++++++++++++++++ .../settings/GamepadSettingsManager.java | 66 ++------------- .../settings/PreferencesManager.java | 66 +++++++++++++++ .../limelight/settings/SettingsManager.java | 64 ++++++++++++++ 5 files changed, 231 insertions(+), 64 deletions(-) create mode 100644 src/com/limelight/gui/PreferencesFrame.java create mode 100644 src/com/limelight/settings/PreferencesManager.java diff --git a/src/com/limelight/gui/MainFrame.java b/src/com/limelight/gui/MainFrame.java index 5c882ca..a3c143c 100644 --- a/src/com/limelight/gui/MainFrame.java +++ b/src/com/limelight/gui/MainFrame.java @@ -121,16 +121,26 @@ public class MainFrame { private JMenuBar createMenuBar() { JMenuBar menuBar = new JMenuBar(); JMenu optionsMenu = new JMenu("Options"); - JMenuItem settings = new JMenuItem("Gamepad Settings"); + JMenuItem gamepadSettings = new JMenuItem("Gamepad Settings"); + JMenuItem generalSettings = new JMenuItem("Preferences"); - settings.addActionListener(new ActionListener() { + gamepadSettings.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { new SettingsFrame().build(); } }); - optionsMenu.add(settings); + generalSettings.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + new PreferencesFrame().build(); + } + }); + + optionsMenu.add(gamepadSettings); + optionsMenu.add(generalSettings); + menuBar.add(optionsMenu); return menuBar; diff --git a/src/com/limelight/gui/PreferencesFrame.java b/src/com/limelight/gui/PreferencesFrame.java new file mode 100644 index 0000000..3bf77cd --- /dev/null +++ b/src/com/limelight/gui/PreferencesFrame.java @@ -0,0 +1,83 @@ +package com.limelight.gui; + +import java.awt.Dimension; +import java.awt.Toolkit; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; + +import javax.swing.Box; +import javax.swing.BoxLayout; +import javax.swing.JCheckBox; +import javax.swing.JComboBox; +import javax.swing.JFrame; +import javax.swing.JPanel; + +import com.limelight.settings.PreferencesManager; +import com.limelight.settings.PreferencesManager.Preferences; + +public class PreferencesFrame extends JFrame { + private static final long serialVersionUID = 1L; + private JComboBox resolution; + private JCheckBox fullscreen; + private Preferences prefs; + private boolean prefsChanged = false; + + public PreferencesFrame() { + super("Preferences"); + this.setSize(200, 100); + this.setResizable(false); + this.setAlwaysOnTop(true); + } + + public void build() { + + JPanel mainPanel = new JPanel(); + mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS)); + + resolution = new JComboBox(); + resolution.addItem("1080p"); + resolution.addItem("720p"); + + fullscreen = new JCheckBox("Fullscreen"); + + + Box resolutionBox = Box.createHorizontalBox(); + resolutionBox.add(Box.createHorizontalGlue()); + resolutionBox.add(resolution); + resolutionBox.add(Box.createHorizontalGlue()); + + Box fullscreenBox = Box.createHorizontalBox(); + fullscreenBox.add(Box.createHorizontalGlue()); + fullscreenBox.add(fullscreen); + fullscreenBox.add(Box.createHorizontalGlue()); + + mainPanel.add(Box.createVerticalStrut(10)); + mainPanel.add(resolutionBox); + mainPanel.add(Box.createVerticalStrut(5)); + mainPanel.add(fullscreenBox); + mainPanel.add(Box.createVerticalGlue()); + + this.addWindowListener(new WindowAdapter() { + @Override + public void windowClosing(WindowEvent e) { + super.windowClosing(e); + if (prefsChanged) { + writePreferences(); + } + } + }); + + this.getContentPane().add(mainPanel); + + Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); + //center on screen + this.setLocation((int)dim.getWidth()/2-this.getWidth(), (int)dim.getHeight()/2-this.getHeight()); + + this.setVisible(true); + } + + private void writePreferences() { + PreferencesManager.writePreferences(prefs); + } + +} diff --git a/src/com/limelight/settings/GamepadSettingsManager.java b/src/com/limelight/settings/GamepadSettingsManager.java index 7b753b5..5fad7cd 100644 --- a/src/com/limelight/settings/GamepadSettingsManager.java +++ b/src/com/limelight/settings/GamepadSettingsManager.java @@ -1,16 +1,10 @@ package com.limelight.settings; import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; import com.limelight.input.GamepadMapping; -public class GamepadSettingsManager { +public abstract class GamepadSettingsManager { private static GamepadMapping cachedSettings; @@ -18,35 +12,8 @@ public class GamepadSettingsManager { if (cachedSettings == null) { System.out.println("Reading Gamepad Settings"); File gamepadFile = SettingsManager.getInstance().getGamepadFile(); - ObjectInputStream ois = null; - - try { - ois = new ObjectInputStream(new FileInputStream(gamepadFile)); - GamepadMapping savedSettings = (GamepadMapping)ois.readObject(); - cachedSettings = savedSettings; - - } catch (ClassNotFoundException e) { - System.out.println("Saved file is not of the correct type. It might have been modified externally."); - - } catch (FileNotFoundException e) { - System.out.println("Could not find gamepad settings file"); - e.printStackTrace(); - - } catch (IOException e) { - System.out.println("Could not read gamepad settings file"); - e.printStackTrace(); - - } finally { - if (ois != null) { - try { - ois.close(); - - } catch (IOException e) { - System.out.println("Could not close gamepad settings file"); - e.printStackTrace(); - } - } - } + GamepadMapping savedMapping = (GamepadMapping)SettingsManager.readSettings(gamepadFile); + cachedSettings = savedMapping; } if (cachedSettings == null) { System.out.println("Unable to get gamepad settings. Using an empty mapping instead."); @@ -61,31 +28,8 @@ public class GamepadSettingsManager { System.out.println("Writing Gamepad Settings"); File gamepadFile = SettingsManager.getInstance().getGamepadFile(); - - ObjectOutputStream ous = null; - - try { - ous = new ObjectOutputStream(new FileOutputStream(gamepadFile)); - ous.writeObject(settings); - - } catch (FileNotFoundException e) { - System.out.println("Could not find gamepad settings file"); - e.printStackTrace(); - - } catch (IOException e) { - System.out.println("Could not write gamepad settings file"); - e.printStackTrace(); - - } finally { - if (ous != null) { - try { - ous.close(); - } catch (IOException e) { - System.out.println("Unable to close gamepad settings file"); - e.printStackTrace(); - } - } - } + + SettingsManager.writeSettings(gamepadFile, settings); } } diff --git a/src/com/limelight/settings/PreferencesManager.java b/src/com/limelight/settings/PreferencesManager.java new file mode 100644 index 0000000..5e37969 --- /dev/null +++ b/src/com/limelight/settings/PreferencesManager.java @@ -0,0 +1,66 @@ +package com.limelight.settings; + +import java.io.File; +import java.io.Serializable; + +public abstract class PreferencesManager { + private static Preferences cachedPreferences = null; + + public static void writePreferences(Preferences prefs) { + File prefFile = SettingsManager.getInstance().getSettingsFile(); + + SettingsManager.writeSettings(prefFile, prefs); + } + + public static Preferences getPreferences() { + if (cachedPreferences == null) { + File prefFile = SettingsManager.getInstance().getSettingsFile(); + Preferences savedPref = (Preferences)SettingsManager.readSettings(prefFile); + cachedPreferences = savedPref; + } + if (cachedPreferences == null) { + System.out.println("Unabled to get preferences, using default"); + cachedPreferences = new Preferences(); + writePreferences(cachedPreferences); + } + return cachedPreferences; + } + + public static class Preferences implements Serializable { + private static final long serialVersionUID = -5575445156215348048L; + + public enum Resolution { RES_720, RES_1080 }; + + private Resolution res; + private boolean fullscreen; + + /** + * construcs default preferences: 720p fullscreen + */ + public Preferences() { + this.res = Resolution.RES_720; + this.fullscreen = true; + } + + public Preferences(Resolution res, boolean fullscreen) { + this.res = res; + this.fullscreen = fullscreen; + } + + public Resolution getResolution() { + return res; + } + + public boolean getFullscreen() { + return fullscreen; + } + + public void setResolution(Resolution res) { + this.res = res; + } + + public void setFullscreen(boolean fullscreen) { + this.fullscreen = fullscreen; + } + } +} diff --git a/src/com/limelight/settings/SettingsManager.java b/src/com/limelight/settings/SettingsManager.java index 6e05be7..e3ae41c 100644 --- a/src/com/limelight/settings/SettingsManager.java +++ b/src/com/limelight/settings/SettingsManager.java @@ -1,7 +1,13 @@ package com.limelight.settings; import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; public class SettingsManager { @@ -62,4 +68,62 @@ public class SettingsManager { return settingsFile; } + public static Object readSettings(File file) { + ObjectInputStream ois = null; + Object settings = null; + try { + ois = new ObjectInputStream(new FileInputStream(file)); + Object savedSettings = ois.readObject(); + settings = savedSettings; + } catch (ClassNotFoundException e) { + System.out.println("Saved file is not of the correct type. It might have been modified externally."); + + } catch (FileNotFoundException e) { + System.out.println("Could not find " + file.getName() + " settings file"); + e.printStackTrace(); + + } catch (IOException e) { + System.out.println("Could not read " + file.getName() + " settings file"); + e.printStackTrace(); + + } finally { + if (ois != null) { + try { + ois.close(); + + } catch (IOException e) { + System.out.println("Could not close gamepad settings file"); + e.printStackTrace(); + } + } + } + return settings; + } + + public static void writeSettings(File file, Serializable settings) { + ObjectOutputStream ous = null; + + try { + ous = new ObjectOutputStream(new FileOutputStream(file)); + ous.writeObject(settings); + + } catch (FileNotFoundException e) { + System.out.println("Could not find " + file.getName() + " settings file"); + e.printStackTrace(); + + } catch (IOException e) { + System.out.println("Could not write to " + file.getName() + " settings file"); + e.printStackTrace(); + + } finally { + if (ous != null) { + try { + ous.close(); + } catch (IOException e) { + System.out.println("Unable to close " + file.getName() + " settings file"); + e.printStackTrace(); + } + } + } + } }