Link samples table to GUI events

This commit is contained in:
2018-09-10 16:57:44 +02:00
parent e2cbce5812
commit 014116ba6f
2 changed files with 79 additions and 41 deletions

View File

@@ -7,6 +7,7 @@ import ch.psi.mxsc.Puck.PuckType;
import ch.psi.pshell.core.Context;
import ch.psi.pshell.core.DevicePool;
import ch.psi.pshell.core.DevicePoolListener;
import ch.psi.pshell.core.JsonSerializer;
import ch.psi.pshell.device.Device;
import ch.psi.pshell.device.DeviceAdapter;
import ch.psi.pshell.device.DeviceListener;
@@ -23,6 +24,7 @@ import java.util.logging.Level;
import java.util.logging.Logger;
import javax.script.ScriptException;
import javax.swing.JComponent;
import javax.swing.table.DefaultTableModel;
/**
*
@@ -189,6 +191,9 @@ public class Controller {
}
});
updateDevices();
updatePuckTypes();
clearSamplesTable();
refreshSamplesTable();
}
@@ -196,6 +201,8 @@ public class Controller {
public void onStateChange(State state, State former) {
if (state == State.Initializing){
getMainFrame().removeDevice(basePlate);
} else if (state==State.Ready){
refreshSamplesTable();
}
}
@@ -205,6 +212,7 @@ public class Controller {
} catch (Exception ex) {
setPuckLoading(false);
}
refreshSamplesTable();
}
final DeviceListener hexiposiListener = new DeviceAdapter() {
@@ -498,7 +506,39 @@ public class Controller {
getMainFrame().execute(statement, background, showReturn);
}
////////////////////// Sample Info /////////////////////////////////
String samplesTableJson;
final Object samplesTableLock = new Object();
void refreshSamplesTable() {
synchronized(samplesTableLock){
try {
String json = (String) Context.getInstance().evalLineBackground("get_samples_info()");
if (!json.equals(samplesTableJson)) {
samplesTableJson = json;
//SamplesInfo sampleInfo = (SamplesInfo) JsonSerializer.decode(json, SampleInfo.class);
SampleInfo[] samples = (SampleInfo[]) JsonSerializer.decode(json, SampleInfo[].class);
Object[][] sampleData = new Object[samples.length][];
for (int i = 0; i < samples.length; i++) {
sampleData[i] = samples[i].getData();
}
getMainFrame().setSamplesTable(sampleData);
}
} catch (Exception ex) {
clearSamplesTable();
Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
void clearSamplesTable() {
samplesTableJson = null;
getMainFrame().setSamplesTable(new Object[][]{});
}
////////////////////// Puck Loading /////////////////////////////////
Boolean puckLoading;
Puck.Detection[] currentDetection;

View File

@@ -81,6 +81,27 @@ public class MainPanel extends Panel {
}
}
});
tableSamples.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
try {
String add = String.valueOf(tableSamples.getModel().getValueAt(tableSamples.getSelectedRow(), 3));
if (!add.trim().isEmpty()) {
String cur = Controller.getInstance().getCurrentSelection();
if (cur != null) {
cur = cur.substring(0, 2);
}
if (!add.equals(cur)) {
Puck puck = Controller.getInstance().getPuck(add);
Controller.getInstance().selectPuck(puck);
}
}
} catch (Exception ex) {
showException(ex);
}
}
});
}
@Override
@@ -159,11 +180,8 @@ public class MainPanel extends Panel {
}
setPuckDatamatrix(null);
setSampleDatamatrix(null);
Controller.getInstance().updatePuckTypes();
startTimer(3000, 1000);
updateCameraView();
refreshSamplesTable();
}
@Override
@@ -425,44 +443,14 @@ public class MainPanel extends Panel {
return panelDetail.getSize();
}
String[] HEADER = new String[]{
"User Name", "Dewar Name",
"Puck Name", "Puck Address", "Puck Barcode", "Puck Type",
"Sample Name", "Sample Position", "Sample Status", "Mount Count"
};
void refreshSamplesTable() {
try {
String json = (String) Context.getInstance().evalLineBackground("get_samples_info()");
//SamplesInfo sampleInfo = (SamplesInfo) JsonSerializer.decode(json, SampleInfo.class);
SampleInfo[] samples = (SampleInfo[]) JsonSerializer.decode(json, SampleInfo[].class);
Object[][] sampleData = new Object[samples.length][];
for (int i = 0; i < samples.length; i++) {
sampleData[i] = samples[i].getData();
}
tableSamples.setModel(new DefaultTableModel(
sampleData,
HEADER
) {
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
}
);
} catch (Exception ex) {
clearSamplesTable();
getLogger().log(Level.SEVERE, null, ex);
}
}
void clearSamplesTable() {
void setSamplesTable(Object[][] sampleData) {
tableSamples.setModel(new DefaultTableModel(
new Object[][]{},
HEADER
sampleData,
new String[]{
"User Name", "Dewar Name",
"Puck Name", "Puck Address", "Puck Barcode", "Puck Type",
"Sample Name", "Sample Position", "Sample Status", "Mount Count"
}
) {
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
@@ -475,7 +463,7 @@ public class MainPanel extends Panel {
if (segment != null) {
String add = segment + String.valueOf(puck);
for (int i = 0; i < tablePucks.getModel().getRowCount(); i++) {
if (tablePucks.getModel().getValueAt(i, 0).equals(add)) {
if (add.equals(tablePucks.getModel().getValueAt(i, 0))) {
if (tablePucks.getSelectedRow() != i) {
tablePucks.setRowSelectionInterval(i, i);
SwingUtils.scrollToVisible(tablePucks, i, 0);
@@ -483,6 +471,16 @@ public class MainPanel extends Panel {
break;
}
}
for (int i = 0; i < tableSamples.getModel().getRowCount(); i++) {
if (add.equals(tableSamples.getModel().getValueAt(i, 3))) {
if (tableSamples.getSelectedRow() != i) {
tableSamples.setRowSelectionInterval(i, i);
SwingUtils.scrollToVisible(tableSamples, i, 0);
}
break;
}
}
}
}