Created application main panel and separated Controller class

This commit is contained in:
gac-S_Changer
2018-01-10 11:21:06 +01:00
parent c4ce826ae8
commit 9c5a74d2a8
8 changed files with 294 additions and 146 deletions

View File

@@ -0,0 +1,110 @@
/*
* Copyright (c) 2014 Paul Scherrer Institute. All rights reserved.
*/
package ch.psi.mxsc;
import ch.psi.pshell.core.DevicePoolListener;
import ch.psi.pshell.device.Device;
import ch.psi.pshell.device.GenericDevice;
import ch.psi.pshell.ui.Panel;
/**
*
*/
public class Controller {
static Controller instance;
final BasePlate basePlate;
final Panel mainFrame;
public static Controller getInstance(){
return instance;
}
static void createInstance(Panel mainFrame){
instance = new Controller(mainFrame);
}
enum PuckSensorAccess{
RaspberryPi,
Esera;
}
final PuckSensorAccess puckSensorAccess = PuckSensorAccess.RaspberryPi;
static String PUCK_ESERA_DEVICE = "onewire";
public static final int NUMBER_OF_PUCKS = 30;
private Controller(Panel mainFrame){
basePlate = new BasePlate();
puckState = new PuckState[NUMBER_OF_PUCKS];
for (int i=0; i<NUMBER_OF_PUCKS; i++ ){
puckState[i] = new PuckState(i+1);
}
this.mainFrame =mainFrame;
instance = this;
}
public Panel getMainFrame(){
return mainFrame;
}
void onInitialize(int runCount) {
GenericDevice former = getMainFrame().getDevice("BasePlate");
if (former!=null){
getMainFrame().removeDevice(former);
}
getMainFrame().addDevice(basePlate);
clearPuckStates();
if (puckSensorAccess == PuckSensorAccess.Esera){
getMainFrame().getContext().getDevicePool().addListener(new DevicePoolListener() {
@Override
public void onDeviceAdded(GenericDevice dev) {
if (dev.getName().equals(PUCK_ESERA_DEVICE)){
detection = new EseraDetection((Device) dev);
}
}
@Override
public void onDeviceRemoved(GenericDevice dev) {
if (dev.getName().equals(PUCK_ESERA_DEVICE)){
detection.close();
detection = null;
}
}
});
if (detection != null){
detection.close();
detection = null;
}
if ((Device) getMainFrame().getDevice(PUCK_ESERA_DEVICE) != null){
detection = new EseraDetection((Device) getMainFrame().getDevice(PUCK_ESERA_DEVICE));
}
}
}
final PuckState[] puckState;
public PuckState[] getPuckStates(){
return puckState;
}
EseraDetection detection;
//From 1 to PUCKS_NUMBER
public PuckState getPuckState(int id) throws Exception{
if ((id<=0) || (id>NUMBER_OF_PUCKS)){
throw new Exception("invalid puck id: "+ id);
}
return getPuckStates()[id-1];
}
public void clearPuckStates(){
for (PuckState puck:getPuckStates()){
puck.clear();
}
getMainFrame().repaint();
}
}

View File

@@ -1,6 +1,6 @@
package ch.psi.mxsc;
import static ch.psi.mxsc.MainPanel.NUMBER_OF_PUCKS;
import static ch.psi.mxsc.Controller.NUMBER_OF_PUCKS;
import ch.psi.pshell.device.Device;
import ch.psi.pshell.device.DeviceAdapter;
import ch.psi.utils.State;
@@ -24,17 +24,17 @@ public class EseraDetection implements AutoCloseable{
@Override
public void onStateChanged(Device device, State state, State former) {
if (!state.isInitialized()){
MainPanel.getInstance().clearPuckStates();
Controller.getInstance().clearPuckStates();
}
}
@Override
public void onValueChanged(Device device, Object value, Object former) {
if ((value == null) || !(value instanceof List)){
MainPanel.getInstance().clearPuckStates();
Controller.getInstance().clearPuckStates();
} else {
List l = (List)value;
PuckState[] puckState = MainPanel.getInstance().getPuckStates();
PuckState[] puckState = Controller.getInstance().getPuckStates();
for (int i=0; i<NUMBER_OF_PUCKS; i++ ){
try{
List det = (List) l.get(i);
@@ -43,7 +43,7 @@ public class EseraDetection implements AutoCloseable{
puckState[i].clear();
}
}
MainPanel.getInstance().repaint();
Controller.getInstance().getMainFrame().repaint();
}
}
};
@@ -53,6 +53,6 @@ public class EseraDetection implements AutoCloseable{
if (device!=null){
device.removeListener(listener);
}
MainPanel.getInstance().clearPuckStates();
Controller.getInstance().clearPuckStates();
}
}

View File

@@ -31,41 +31,15 @@
<Layout>
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="1" attributes="0">
<EmptySpace min="0" pref="386" max="32767" attributes="0"/>
<Component id="comboMode" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace min="0" pref="442" max="32767" attributes="0"/>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<Component id="comboMode" min="-2" max="-2" attributes="0"/>
<EmptySpace min="0" pref="280" max="32767" attributes="0"/>
</Group>
<EmptySpace min="0" pref="300" max="32767" attributes="0"/>
</Group>
</DimensionLayout>
</Layout>
<SubComponents>
<Component class="javax.swing.JComboBox" name="comboMode">
<Properties>
<Property name="model" type="javax.swing.ComboBoxModel" editor="org.netbeans.modules.form.editors2.ComboBoxModelEditor">
<StringArray count="4">
<StringItem index="0" value="Item 1"/>
<StringItem index="1" value="Item 2"/>
<StringItem index="2" value="Item 3"/>
<StringItem index="3" value="Item 4"/>
</StringArray>
</Property>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="comboModeActionPerformed"/>
</Events>
<AuxValues>
<AuxValue name="JavaCodeGenerator_TypeParameters" type="java.lang.String" value="&lt;String&gt;"/>
</AuxValues>
</Component>
</SubComponents>
</Container>
</SubComponents>
</Form>

View File

@@ -4,105 +4,25 @@
package ch.psi.mxsc;
import ch.psi.pshell.core.DevicePoolListener;
import ch.psi.pshell.device.Device;
import ch.psi.pshell.device.GenericDevice;
import ch.psi.pshell.ui.Panel;
import ch.psi.utils.swing.SwingUtils;
import java.awt.Component;
/**
*
*/
public class MainPanel extends Panel {
BasePlate basePlate;
/** Creates new form Panel */
static MainPanel instance;
/** Creates new form Panel */
enum PuckSensorAccess{
RaspberryPi,
Esera;
}
final PuckSensorAccess puckSensorAccess = PuckSensorAccess.RaspberryPi;
static String PUCK_ESERA_DEVICE = "onewire";
public static final int NUMBER_OF_PUCKS = 30;
final PuckState[] puckState;
public PuckState[] getPuckStates(){
return puckState;
}
EseraDetection detection;
//From 1 to PUCKS_NUMBER
public PuckState getPuckState(int id) throws Exception{
if ((id<=0) || (id>NUMBER_OF_PUCKS)){
throw new Exception("invalid puck id: "+ id);
}
return getPuckStates()[id-1];
}
public void clearPuckStates(){
for (PuckState puck:getPuckStates()){
puck.clear();
}
repaint();
}
public static MainPanel getInstance(){
return instance;
}
public MainPanel() {
initComponents();
instance = this;
basePlate = new BasePlate();
SwingUtils.setEnumCombo(comboMode, BasePlatePanel.Mode.class);
comboMode.setSelectedItem(basePlatePanel.getMode());
this.setPersistedComponents(new Component[]{comboMode});
puckState = new PuckState[NUMBER_OF_PUCKS];
for (int i=0; i<NUMBER_OF_PUCKS; i++ ){
puckState[i] = new PuckState(i+1);
}
Controller.createInstance(this);
}
@Override
public void onInitialize(int runCount) {
basePlatePanel.setDevice(basePlate);
GenericDevice former = getDevice("BasePlate");
if (former!=null){
removeDevice(former);
}
addDevice(basePlate);
clearPuckStates();
if (puckSensorAccess == PuckSensorAccess.Esera){
getContext().getDevicePool().addListener(new DevicePoolListener() {
@Override
public void onDeviceAdded(GenericDevice dev) {
if (dev.getName().equals(PUCK_ESERA_DEVICE)){
detection = new EseraDetection((Device) dev);
}
}
@Override
public void onDeviceRemoved(GenericDevice dev) {
if (dev.getName().equals(PUCK_ESERA_DEVICE)){
detection.close();
detection = null;
}
}
});
if (detection != null){
detection.close();
detection = null;
}
if ((Device) getDevice(PUCK_ESERA_DEVICE) != null){
detection = new EseraDetection((Device) getDevice(PUCK_ESERA_DEVICE));
}
}
Controller.getInstance().onInitialize(runCount);
basePlatePanel.setDevice((Device) getDevice("BasePlate"));
}
@@ -116,28 +36,16 @@ public class MainPanel extends Panel {
private void initComponents() {
basePlatePanel = new ch.psi.mxsc.BasePlatePanel();
comboMode = new javax.swing.JComboBox<>();
comboMode.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
comboMode.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
comboModeActionPerformed(evt);
}
});
javax.swing.GroupLayout basePlatePanelLayout = new javax.swing.GroupLayout(basePlatePanel);
basePlatePanel.setLayout(basePlatePanelLayout);
basePlatePanelLayout.setHorizontalGroup(
basePlatePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, basePlatePanelLayout.createSequentialGroup()
.addGap(0, 386, Short.MAX_VALUE)
.addComponent(comboMode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(0, 442, Short.MAX_VALUE)
);
basePlatePanelLayout.setVerticalGroup(
basePlatePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(basePlatePanelLayout.createSequentialGroup()
.addComponent(comboMode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 280, Short.MAX_VALUE))
.addGap(0, 300, Short.MAX_VALUE)
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
@@ -152,14 +60,9 @@ public class MainPanel extends Panel {
);
}// </editor-fold>//GEN-END:initComponents
private void comboModeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboModeActionPerformed
basePlatePanel.setMode((BasePlatePanel.Mode) comboMode.getSelectedItem());
}//GEN-LAST:event_comboModeActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private ch.psi.mxsc.BasePlatePanel basePlatePanel;
private javax.swing.JComboBox<String> comboMode;
// End of variables declaration//GEN-END:variables
}

View File

@@ -0,0 +1,71 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Form version="1.3" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JPanelFormInfo">
<AuxValues>
<AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="0"/>
<AuxValue name="FormSettings_autoSetComponentName" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_generateFQN" type="java.lang.Boolean" value="true"/>
<AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_i18nAutoMode" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/>
<AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/>
<AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/>
</AuxValues>
<Layout>
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<Component id="basePlatePanel" alignment="0" max="32767" attributes="0"/>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<Component id="basePlatePanel" alignment="1" max="32767" attributes="0"/>
</Group>
</DimensionLayout>
</Layout>
<SubComponents>
<Container class="ch.psi.mxsc.BasePlatePanel" name="basePlatePanel">
<Layout>
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="1" attributes="0">
<EmptySpace min="0" pref="386" max="32767" attributes="0"/>
<Component id="comboMode" min="-2" max="-2" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<Component id="comboMode" min="-2" max="-2" attributes="0"/>
<EmptySpace min="0" pref="280" max="32767" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
</Layout>
<SubComponents>
<Component class="javax.swing.JComboBox" name="comboMode">
<Properties>
<Property name="model" type="javax.swing.ComboBoxModel" editor="org.netbeans.modules.form.editors2.ComboBoxModelEditor">
<StringArray count="4">
<StringItem index="0" value="Item 1"/>
<StringItem index="1" value="Item 2"/>
<StringItem index="2" value="Item 3"/>
<StringItem index="3" value="Item 4"/>
</StringArray>
</Property>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="comboModeActionPerformed"/>
</Events>
<AuxValues>
<AuxValue name="JavaCodeGenerator_TypeParameters" type="java.lang.String" value="&lt;String&gt;"/>
</AuxValues>
</Component>
</SubComponents>
</Container>
</SubComponents>
</Form>

View File

@@ -0,0 +1,90 @@
/*
* Copyright (c) 2014 Paul Scherrer Institute. All rights reserved.
*/
package ch.psi.mxsc;
import ch.psi.pshell.device.Device;
import ch.psi.pshell.ui.Panel;
import ch.psi.utils.swing.SwingUtils;
import java.awt.Component;
/**
*
*/
public class MainPanelTest extends Panel {
/** Creates new form Panel */
public MainPanelTest() {
initComponents();
SwingUtils.setEnumCombo(comboMode, BasePlatePanel.Mode.class);
comboMode.setSelectedItem(basePlatePanel.getMode());
setPersistedComponents(new Component[]{comboMode});
Controller.createInstance(this);
}
@Override
public void onInitialize(int runCount) {
Controller.getInstance().onInitialize(runCount);
basePlatePanel.setDevice((Device) getDevice("BasePlate"));
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
basePlatePanel = new ch.psi.mxsc.BasePlatePanel();
comboMode = new javax.swing.JComboBox<>();
comboMode.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
comboMode.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
comboModeActionPerformed(evt);
}
});
javax.swing.GroupLayout basePlatePanelLayout = new javax.swing.GroupLayout(basePlatePanel);
basePlatePanel.setLayout(basePlatePanelLayout);
basePlatePanelLayout.setHorizontalGroup(
basePlatePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, basePlatePanelLayout.createSequentialGroup()
.addGap(0, 386, Short.MAX_VALUE)
.addComponent(comboMode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
);
basePlatePanelLayout.setVerticalGroup(
basePlatePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(basePlatePanelLayout.createSequentialGroup()
.addComponent(comboMode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 280, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(basePlatePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(basePlatePanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
}// </editor-fold>//GEN-END:initComponents
private void comboModeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboModeActionPerformed
basePlatePanel.setMode((BasePlatePanel.Mode) comboMode.getSelectedItem());
}//GEN-LAST:event_comboModeActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private ch.psi.mxsc.BasePlatePanel basePlatePanel;
private javax.swing.JComboBox<String> comboMode;
// End of variables declaration//GEN-END:variables
}

View File

@@ -44,7 +44,7 @@ public class PuckDetection extends DeviceBase{
while (!Thread.currentThread().isInterrupted()) {
if (chrono.isTimeout(3000)){
setState(State.Offline);
MainPanel.getInstance().clearPuckStates();
Controller.getInstance().clearPuckStates();
}
Thread.sleep(1000);
}
@@ -71,14 +71,14 @@ public class PuckDetection extends DeviceBase{
String contents = subscriber.recvStr();
//System.out.println(contents);
processMessage(contents);
if (MainPanel.getInstance()!=null){
MainPanel.getInstance().repaint();
if (Controller.getInstance()!=null){
Controller.getInstance().getMainFrame().repaint();
}
setState(State.Ready);
chrono = new Chrono();
}
} finally{
MainPanel.getInstance().clearPuckStates();
Controller.getInstance().clearPuckStates();
subscriber.close();
context.term();
}
@@ -96,7 +96,7 @@ public class PuckDetection extends DeviceBase{
String[] tokens = line.split(" ");
int id = Integer.valueOf(tokens[0].substring(1));
present.add(id);
PuckState puck = MainPanel.getInstance().getPuckState(id);
PuckState puck = Controller.getInstance().getPuckState(id);
if (tokens.length<3){
puck.clear();
} else {
@@ -106,10 +106,10 @@ public class PuckDetection extends DeviceBase{
getLogger().log(Level.INFO, null, ex);
}
}
for (int i=1; i<= MainPanel.NUMBER_OF_PUCKS; i++){
for (int i=1; i<= Controller.NUMBER_OF_PUCKS; i++){
if (!Arr.containsEqual(present.toArray(), i)){
try{
MainPanel.getInstance().getPuckState(i).clear();
Controller.getInstance().getPuckState(i).clear();
} catch (Exception ex){
getLogger().log(Level.INFO, null, ex);
}

View File

@@ -18,10 +18,10 @@ public class PuckState {
}
BasePlate getBasePlate() {
if (MainPanel.getInstance() == null) {
if (Controller.getInstance() == null) {
return null;
}
return MainPanel.getInstance().basePlate;
return Controller.getInstance().basePlate;
}
void clear() {