From 7ca84094a566977d9a46feb47276feceff949ef1 Mon Sep 17 00:00:00 2001 From: Jesse Morgan Date: Fri, 19 Oct 2012 23:04:51 -0700 Subject: Added preset loading. --- src/net/tuschhcm/routercontrol/Preset.java | 12 ++-- src/net/tuschhcm/routercontrol/SwitcherApp.java | 74 +++++++++++++++++++------ 2 files changed, 64 insertions(+), 22 deletions(-) diff --git a/src/net/tuschhcm/routercontrol/Preset.java b/src/net/tuschhcm/routercontrol/Preset.java index ef2ba49..7b64333 100644 --- a/src/net/tuschhcm/routercontrol/Preset.java +++ b/src/net/tuschhcm/routercontrol/Preset.java @@ -26,20 +26,20 @@ public class Preset { /** * Load a preset from the given file. * - * @param filename The file to load. + * @param file The file to load. * @return the Preset object * @throws IllegalArgumentException if the preset can't be loaded. */ - public static Preset loadPresetFile(String filename) throws IllegalArgumentException { + public static Preset loadPresetFile(File file) throws IllegalArgumentException { try { Preset preset = new Preset(); - BufferedReader in = new BufferedReader(new FileReader(filename)); + BufferedReader in = new BufferedReader(new FileReader(file)); int lineno = 1; String line = in.readLine(); if (line == null) { - throw new IllegalArgumentException("Empty preset file: " + filename); + throw new IllegalArgumentException("Empty preset file: " + file); } // The name is optional and starts with a # @@ -60,7 +60,7 @@ public class Preset { } catch (final NumberFormatException nfe) { throw new IllegalArgumentException( String.format("%s: Input set for output %d on line %d is not a number.", - filename, output, lineno)); + file, output, lineno)); } line = in.readLine(); @@ -72,7 +72,7 @@ public class Preset { return preset; } catch (final IOException e) { - throw new IllegalArgumentException("Could not read " + filename, e); + throw new IllegalArgumentException("Could not read " + file, e); } } diff --git a/src/net/tuschhcm/routercontrol/SwitcherApp.java b/src/net/tuschhcm/routercontrol/SwitcherApp.java index 99d1907..635999e 100644 --- a/src/net/tuschhcm/routercontrol/SwitcherApp.java +++ b/src/net/tuschhcm/routercontrol/SwitcherApp.java @@ -1,5 +1,8 @@ package net.tuschhcm.routercontrol; +import java.io.File; +import java.io.IOException; + import java.util.Enumeration; import java.util.HashMap; import java.util.Map; @@ -17,17 +20,17 @@ public class SwitcherApp { * The router I control. */ private final Router mRouter; - + /** * My user interface. */ private final UserInterface mUI; - + /** * My list of presets. */ private final Map mPresets; - + /** * Create a switcher app. * @param portName the port name for the router. @@ -36,36 +39,75 @@ public class SwitcherApp { mUI = new ConsoleUI(); mRouter = new ShinyBow5544Router(portName); mPresets = new HashMap(); - + // Setup UI actions mUI.setPresetSelectionAction(new Action() { public void onAction() { handlePresetSelected(); } }); - + mUI.setToggleControlLockAction(new Action() { public void onAction() { handleLockToggled(); } }); } - + /** * Start the application. */ public void run() { - // TODO: Load the presets - // TODO: Send the presets to the UI + // Load the presets + loadPresets(); + + // Send the presets to the UI + for (Map.Entry presetEntry : mPresets.entrySet()) { + mUI.addPreset(presetEntry.getKey(), presetEntry.getValue().getName()); + } + mUI.run(); } - + + private void loadPresets() { + try { + File presetDir = new File("presets"); + if (!presetDir.isDirectory()) { + System.err.println("Could not find presets directory!"); + System.exit(1); + } + + int i = 1; + for (File file : presetDir.listFiles()) { + if (file.isFile()) { + Preset p = Preset.loadPresetFile(file); + mPresets.put(i, p); + i++; + } + } + + } catch (IllegalArgumentException e) { + System.err.println("Problem loading preset: " + e.getMessage()); + System.exit(1); + } + } + /** * Handles a preset selection. */ private void handlePresetSelected() { - int selectedPreset = mUI.getSelectedPreset(); - // TODO: What to do when the preset is selected + final int selectedPreset = mUI.getSelectedPreset(); + final Preset p = mPresets.get(selectedPreset); + + if (p == null) { + System.err.println(selectedPreset + " is not a preset."); + return; + } + + // Set all the outputs + for (int output = 1; output <= p.getNumberOfOutputs(); output++) { + mRouter.switchInput(output, p.getInputForOutput(output)); + } } /** @@ -74,7 +116,7 @@ public class SwitcherApp { private void handleLockToggled() { mRouter.setLockControls(mUI.getControlLockStatus()); } - + /** * Entry-point for the application. * @param args Command line arguments @@ -94,22 +136,22 @@ public class SwitcherApp { } String comPort = args[0]; - + SwitcherApp app = new SwitcherApp(comPort); app.run(); } private static void listCommPorts() { Enumeration ports = CommPortIdentifier.getPortIdentifiers(); - + if (ports.hasMoreElements()) { System.out.println("These ports are available on your system: "); while (ports.hasMoreElements()) { CommPortIdentifier port = (CommPortIdentifier) ports.nextElement(); - if (port.getPortType() == CommPortIdentifier.PORT_SERIAL) { + if (port.getPortType() == CommPortIdentifier.PORT_SERIAL) { System.out.println(port.getName()); } - } + } } else { System.out.println("There are no serial ports on your system."); -- cgit v1.2.3