Add GUI by type

This commit is contained in:
manzerbredes 2016-03-31 21:46:34 +02:00
parent a1d64fd67f
commit 53a8cefdbb
4 changed files with 116 additions and 59 deletions

View file

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

22
src/client/GUI.java Normal file
View file

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

69
src/client/GUITypeA.java Normal file
View file

@ -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<Color> left;
private JComboBox<Color> middle;
private JComboBox<Color> 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;
}
}

View file

@ -18,71 +18,37 @@ import org.manzerbredes.open_klm.drivers.DriverTypeA.*;
public class MainWindow extends JFrame {
private JComboBox<Color> left;
private JComboBox<Color> middle;
private JComboBox<Color> 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<this.availableGUI.length;i++){
try {
GUI gui=(GUI) availableGUI[i].newInstance();
if(gui.getType().equals(aDriver.getType())){
if(gui.initGUI(aDriver)){
this.add((JPanel) gui);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
this.setVisible(true);
}
}