From 53a8cefdbb7a2cf97b634dc52fc50f9fedd88a93 Mon Sep 17 00:00:00 2001 From: manzerbredes Date: Thu, 31 Mar 2016 21:46:34 +0200 Subject: [PATCH] Add GUI by type --- src/app/App.java | 2 +- src/client/GUI.java | 22 ++++++++++ src/client/GUITypeA.java | 69 ++++++++++++++++++++++++++++++++ src/client/MainWindow.java | 82 +++++++++++--------------------------- 4 files changed, 116 insertions(+), 59 deletions(-) create mode 100644 src/client/GUI.java create mode 100644 src/client/GUITypeA.java diff --git a/src/app/App.java b/src/app/App.java index 186202c..34c91ff 100644 --- a/src/app/App.java +++ b/src/app/App.java @@ -26,7 +26,7 @@ public class App // Else run GUI try { MainWindow mw = new MainWindow(aDriver); - } catch (InstantiationException e) { + } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } diff --git a/src/client/GUI.java b/src/client/GUI.java new file mode 100644 index 0000000..8c561ee --- /dev/null +++ b/src/client/GUI.java @@ -0,0 +1,22 @@ +package org.manzerbredes.open_klm.client; + +import org.manzerbredes.open_klm.drivers.Driver; + +public interface GUI{ + + /** + * Get the type of driver the GUI handle + * + * @return class that represent the type of the driver the GUI handle + */ + public Class getType(); + + /** + * + * Init the GUI with driver + * + * @param driver + * @return + */ + public boolean initGUI(Driver driver); +} \ No newline at end of file diff --git a/src/client/GUITypeA.java b/src/client/GUITypeA.java new file mode 100644 index 0000000..dcca028 --- /dev/null +++ b/src/client/GUITypeA.java @@ -0,0 +1,69 @@ +package org.manzerbredes.open_klm.client; + +import java.awt.Label; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.BoxLayout; +import javax.swing.JButton; +import javax.swing.JComboBox; +import javax.swing.JPanel; + +import org.manzerbredes.open_klm.drivers.Driver; +import org.manzerbredes.open_klm.drivers.DriverTypeA; +import org.manzerbredes.open_klm.drivers.DriverTypeA.Color; +import org.manzerbredes.open_klm.drivers.DriverTypeA.Intensity; +import org.manzerbredes.open_klm.drivers.DriverTypeA.Region; + +public class GUITypeA extends JPanel implements GUI{ + + + private JComboBox left; + private JComboBox middle; + private JComboBox right; + private JButton apply=new JButton("Apply"); + private DriverTypeA keyboardTypeA; + + @Override + public boolean initGUI(Driver driver){ + + if(driver.getType().equals(DriverTypeA.class)){ + + this.keyboardTypeA=(DriverTypeA) driver; + + this.left=new JComboBox<>(Color.values()); + this.middle=new JComboBox<>(Color.values()); + this.right=new JComboBox<>(Color.values()); + + this.apply.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + Color leftRegion=(Color) left.getSelectedItem(); + Color middleRegion=(Color) middle.getSelectedItem(); + Color rightRegion=(Color) right.getSelectedItem(); + + keyboardTypeA.setRegionColor(Region.LEFT, leftRegion, Intensity.HIGH); + keyboardTypeA.setRegionColor(Region.MIDDLE, middleRegion, Intensity.HIGH); + keyboardTypeA.setRegionColor(Region.RIGHT, rightRegion, Intensity.HIGH); + + + } + }); + BoxLayout gridLayout=new BoxLayout(this, BoxLayout.Y_AXIS); + this.add(new Label("Test interface")); + this.add(left); + this.add(middle); + this.add(right); + this.add(this.apply); + return true; + } + return false; + } + + @Override + public Class getType() { + return DriverTypeA.class; + } + +} \ No newline at end of file diff --git a/src/client/MainWindow.java b/src/client/MainWindow.java index 1147592..f42ecef 100644 --- a/src/client/MainWindow.java +++ b/src/client/MainWindow.java @@ -18,71 +18,37 @@ import org.manzerbredes.open_klm.drivers.DriverTypeA.*; public class MainWindow extends JFrame { - - private JComboBox left; - private JComboBox middle; - private JComboBox right; - private JButton apply=new JButton("Apply"); - - private DriverTypeA keyboardTypeA; - private Class driverType; - private DriverManager drvMan=new DriverManager(); - - public MainWindow(Driver aDriver) throws InstantiationException{ - this.initUI(); - if(aDriver==null){ - System.err.println("No driver avalaible (try as root)"); - System.exit(1); - } - else{ - this.driverType=aDriver.getType(); - this.keyboardTypeA=(DriverTypeA) aDriver; - } - - this.left=new JComboBox<>(Color.values()); - this.middle=new JComboBox<>(Color.values()); - this.right=new JComboBox<>(Color.values()); - - this.apply.addActionListener(new ActionListener() { - - @Override - public void actionPerformed(ActionEvent e) { - Color leftRegion=(Color) left.getSelectedItem(); - Color middleRegion=(Color) middle.getSelectedItem(); - Color rightRegion=(Color) right.getSelectedItem(); - - keyboardTypeA.setRegionColor(Region.LEFT, leftRegion, Intensity.HIGH); - keyboardTypeA.setRegionColor(Region.MIDDLE, middleRegion, Intensity.HIGH); - keyboardTypeA.setRegionColor(Region.RIGHT, rightRegion, Intensity.HIGH); - - } - }); - - - JPanel container = new JPanel(); - BoxLayout gridLayout=new BoxLayout(container, BoxLayout.Y_AXIS); - - container.add(new Label("Test interface")); - container.add(left); - container.add(middle); - container.add(right); - - container.add(this.apply); - - this.add(container); - - - - this.setVisible(true); - } + private Class[] availableGUI={ + GUITypeA.class + }; - private void initUI(){ + public MainWindow(Driver aDriver){ + // Configure MainWindow this.setTitle("Open KLM"); this.setSize(700, 500); setLocationRelativeTo(null); setDefaultCloseOperation(EXIT_ON_CLOSE); + + + for(int i=0;i