Puck Loading Dialog
This commit is contained in:
407
src/main/java/ch/psi/mxsc/PuckLoadingDialog.java
Normal file
407
src/main/java/ch/psi/mxsc/PuckLoadingDialog.java
Normal file
@@ -0,0 +1,407 @@
|
||||
package ch.psi.mxsc;
|
||||
|
||||
import ch.psi.pshell.core.Context;
|
||||
import ch.psi.pshell.swing.DataPanel;
|
||||
import ch.psi.utils.swing.MainFrame;
|
||||
import ch.psi.utils.swing.SwingUtils;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import javax.swing.JDialog;
|
||||
import javax.swing.Timer;
|
||||
import javax.swing.table.DefaultTableModel;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class PuckLoadingDialog extends JDialog {
|
||||
Path dialogPersistPath;
|
||||
DefaultTableModel model;
|
||||
|
||||
/**
|
||||
* Creates new form PuckLoadingDialog
|
||||
*/
|
||||
public PuckLoadingDialog(java.awt.Frame parent, boolean modal) {
|
||||
super(parent, "Puck Loading", modal);
|
||||
initComponents();
|
||||
this.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
|
||||
this.setName("PuckLoadingDialog");
|
||||
dialogPersistPath = Paths.get(Context.getInstance().getSetup().getContextPath(), getClass().getSimpleName());
|
||||
setStatusLabel(null,-1);
|
||||
setSugestionLabel(null,-1);
|
||||
model = (DefaultTableModel) table.getModel();
|
||||
table.getTableHeader().setFont(table.getTableHeader().getFont().deriveFont(20.0f));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setVisible(boolean value){
|
||||
boolean visible = isVisible();
|
||||
if (visible && !value){
|
||||
try {
|
||||
MainFrame.save(PuckLoadingDialog.this, dialogPersistPath);
|
||||
} catch (Exception ex) {
|
||||
Logger.getLogger(DataPanel.class.getName()).log(Level.WARNING, null, ex);
|
||||
}
|
||||
}
|
||||
super.setVisible(value);
|
||||
if (!visible && value){
|
||||
try {
|
||||
MainFrame.restore(PuckLoadingDialog.this, dialogPersistPath);
|
||||
} catch (Exception ex) {
|
||||
Logger.getLogger(DataPanel.class.getName()).log(Level.WARNING, null, ex);
|
||||
}
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void update() {
|
||||
Map dms = Controller.getInstance().getPuckDatamatrix();
|
||||
if (dms == null) {
|
||||
dms = new HashMap();
|
||||
}
|
||||
Object[] keys = dms.keySet().toArray();
|
||||
Arrays.sort(keys);
|
||||
if (keys.length != model.getRowCount()){
|
||||
model.setRowCount(keys.length);
|
||||
}
|
||||
for (int i=0; i< keys.length; i++) {
|
||||
model.setValueAt(keys[i], i, 0);
|
||||
model.setValueAt(dms.get(keys[i]), i, 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void onPuckScanned(String datamatrix){
|
||||
textScannedDatamatrix.setText((datamatrix == null) ? "" : datamatrix.trim());
|
||||
if ((datamatrix != null) && (!datamatrix.isEmpty())){
|
||||
setStatusLabel("Scanned puck " + datamatrix, 5000);
|
||||
makeVisible(datamatrix);
|
||||
} else {
|
||||
setStatusLabel(null, -1);
|
||||
}
|
||||
setSugestionLabel(null, -1);
|
||||
buttonSet.setEnabled(false);
|
||||
detectedPuck = null;
|
||||
textDetectedPuck.setText("");
|
||||
}
|
||||
|
||||
Puck detectedPuck;
|
||||
String detectedDatamatrix;
|
||||
void onPuckMounted(Puck puck, String datamatrix){
|
||||
if ((datamatrix != null) && (!datamatrix.isEmpty())) {
|
||||
Controller.getInstance().linkPuckDatamatrix(puck, datamatrix, false);
|
||||
setStatusLabel("Mounted puck " + datamatrix + " at " + puck.getName(), 5000);
|
||||
setSugestionLabel(null, -1);
|
||||
buttonSet.setEnabled(false);
|
||||
textDetectedPuck.setText("");
|
||||
detectedPuck = null;
|
||||
} else {
|
||||
setStatusLabel("Detected puck at " + puck.getName(), -1);
|
||||
setSugestionLabel("Select the datamatrix and press 'Set'", -1);
|
||||
detectedPuck = puck;
|
||||
textDetectedPuck.setText(puck.getName());
|
||||
buttonSet.setEnabled(true);
|
||||
table.clearSelection();
|
||||
}
|
||||
update();
|
||||
}
|
||||
|
||||
Timer timerClear;
|
||||
void onPuckUnmounted(Puck puck){
|
||||
if (puck == detectedPuck){
|
||||
|
||||
}
|
||||
Controller.getInstance().linkPuckDatamatrix(puck, null, false);
|
||||
setStatusLabel("Unmounted puck from "+ puck.getName(), 5000);
|
||||
if (puck == detectedPuck){
|
||||
buttonSet.setEnabled(false);
|
||||
detectedPuck = null;
|
||||
}
|
||||
makeVisible(puck);
|
||||
update();
|
||||
}
|
||||
|
||||
|
||||
void onManualSetPuckLocation(String datamatrix){
|
||||
if (detectedPuck!=null){
|
||||
Controller.getInstance().linkPuckDatamatrix(detectedPuck, datamatrix, false);
|
||||
setStatusLabel("Set puck " + datamatrix + " at " + detectedPuck.getName(), 5000);
|
||||
setSugestionLabel(null, -1);
|
||||
buttonSet.setEnabled(false);
|
||||
detectedPuck = null;
|
||||
textDetectedPuck.setText("");
|
||||
}
|
||||
update();
|
||||
}
|
||||
|
||||
Timer timerLabelStatus;
|
||||
void setStatusLabel(String message, int delay){
|
||||
final String defaultStatus = " "; //Waiting for Puck Detection";
|
||||
if (timerLabelStatus!=null){
|
||||
timerLabelStatus.stop();
|
||||
timerLabelStatus = null;
|
||||
}
|
||||
|
||||
labelStatus.setText(message==null ? defaultStatus : message);
|
||||
|
||||
if (delay>0){
|
||||
timerLabelStatus = new Timer(delay, (e)->{
|
||||
labelStatus.setText(defaultStatus);
|
||||
});
|
||||
timerLabelStatus.start();
|
||||
}
|
||||
}
|
||||
|
||||
Timer timerLabelSuggestion;
|
||||
void setSugestionLabel(String message, int delay){
|
||||
final String defaultSuggestion = "Scan, Insert or Remove Pucks"; //Waiting for Puck Detection";
|
||||
if (timerLabelSuggestion!=null){
|
||||
timerLabelSuggestion.stop();
|
||||
timerLabelSuggestion = null;
|
||||
}
|
||||
|
||||
labelSuggestion.setText(message==null ? defaultSuggestion : message);
|
||||
|
||||
if (delay>0){
|
||||
timerLabelSuggestion = new Timer(delay, (e)->{
|
||||
labelSuggestion.setText(defaultSuggestion);
|
||||
});
|
||||
timerLabelSuggestion.start();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void makeVisible(String datamatrix){
|
||||
if (datamatrix!=null){
|
||||
datamatrix = datamatrix.trim();
|
||||
for (int i=0; i<model.getRowCount(); i++){
|
||||
if (datamatrix.equals(model.getValueAt(i, 0))){
|
||||
SwingUtils.scrollToVisible(table, i, 0);
|
||||
table.setRowSelectionInterval(i, i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void makeVisible(Puck puck){
|
||||
if (puck!=null){
|
||||
for (int i=0; i<model.getRowCount(); i++){
|
||||
if (puck.getName().equals(model.getValueAt(i, 1))){
|
||||
SwingUtils.scrollToVisible(table, i, 0);
|
||||
table.setRowSelectionInterval(i, i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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() {
|
||||
|
||||
jLabel1 = new javax.swing.JLabel();
|
||||
textScannedDatamatrix = new javax.swing.JTextField();
|
||||
jScrollPane1 = new javax.swing.JScrollPane();
|
||||
table = new javax.swing.JTable();
|
||||
buttonSet = new javax.swing.JButton();
|
||||
labelSuggestion = new javax.swing.JLabel();
|
||||
labelStatus = new javax.swing.JLabel();
|
||||
jLabel2 = new javax.swing.JLabel();
|
||||
textDetectedPuck = new javax.swing.JTextField();
|
||||
|
||||
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
|
||||
|
||||
jLabel1.setFont(new java.awt.Font("Lucida Grande", 0, 18)); // NOI18N
|
||||
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
|
||||
jLabel1.setText("Scanned Dartamatrix:");
|
||||
|
||||
textScannedDatamatrix.setEditable(false);
|
||||
textScannedDatamatrix.setFont(new java.awt.Font("Lucida Grande", 0, 24)); // NOI18N
|
||||
textScannedDatamatrix.setHorizontalAlignment(javax.swing.JTextField.CENTER);
|
||||
|
||||
table.setFont(new java.awt.Font("Lucida Grande", 0, 28)); // NOI18N
|
||||
table.setModel(new javax.swing.table.DefaultTableModel(
|
||||
new Object [][] {
|
||||
|
||||
},
|
||||
new String [] {
|
||||
"Datamatrix", "Puck Position"
|
||||
}
|
||||
) {
|
||||
Class[] types = new Class [] {
|
||||
java.lang.String.class, java.lang.String.class
|
||||
};
|
||||
boolean[] canEdit = new boolean [] {
|
||||
false, false
|
||||
};
|
||||
|
||||
public Class getColumnClass(int columnIndex) {
|
||||
return types [columnIndex];
|
||||
}
|
||||
|
||||
public boolean isCellEditable(int rowIndex, int columnIndex) {
|
||||
return canEdit [columnIndex];
|
||||
}
|
||||
});
|
||||
table.setRowHeight(40);
|
||||
table.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
|
||||
table.getTableHeader().setResizingAllowed(false);
|
||||
table.getTableHeader().setReorderingAllowed(false);
|
||||
jScrollPane1.setViewportView(table);
|
||||
|
||||
buttonSet.setFont(new java.awt.Font("Lucida Grande", 0, 24)); // NOI18N
|
||||
buttonSet.setText("Set");
|
||||
buttonSet.setEnabled(false);
|
||||
buttonSet.addActionListener(new java.awt.event.ActionListener() {
|
||||
public void actionPerformed(java.awt.event.ActionEvent evt) {
|
||||
buttonSetActionPerformed(evt);
|
||||
}
|
||||
});
|
||||
|
||||
labelSuggestion.setFont(new java.awt.Font("Lucida Grande", 0, 24)); // NOI18N
|
||||
labelSuggestion.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
|
||||
labelSuggestion.setText("Scan, Insert or Remove Pucks");
|
||||
|
||||
labelStatus.setFont(new java.awt.Font("Lucida Grande", 0, 24)); // NOI18N
|
||||
labelStatus.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
|
||||
labelStatus.setText("Waiting for Puck Detection");
|
||||
labelStatus.setBorder(javax.swing.BorderFactory.createEtchedBorder());
|
||||
|
||||
jLabel2.setFont(new java.awt.Font("Lucida Grande", 0, 18)); // NOI18N
|
||||
jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
|
||||
jLabel2.setText("Detected Puck:");
|
||||
|
||||
textDetectedPuck.setEditable(false);
|
||||
textDetectedPuck.setFont(new java.awt.Font("Lucida Grande", 0, 24)); // NOI18N
|
||||
textDetectedPuck.setHorizontalAlignment(javax.swing.JTextField.CENTER);
|
||||
|
||||
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
|
||||
getContentPane().setLayout(layout);
|
||||
layout.setHorizontalGroup(
|
||||
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
||||
.addGroup(layout.createSequentialGroup()
|
||||
.addContainerGap()
|
||||
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
||||
.addGroup(layout.createSequentialGroup()
|
||||
.addGap(0, 0, Short.MAX_VALUE)
|
||||
.addComponent(buttonSet, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE)
|
||||
.addGap(0, 0, Short.MAX_VALUE))
|
||||
.addComponent(labelSuggestion, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
|
||||
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 484, Short.MAX_VALUE)
|
||||
.addGroup(layout.createSequentialGroup()
|
||||
.addComponent(jLabel1)
|
||||
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
|
||||
.addComponent(textScannedDatamatrix))
|
||||
.addGroup(layout.createSequentialGroup()
|
||||
.addComponent(jLabel2)
|
||||
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
|
||||
.addComponent(textDetectedPuck)))
|
||||
.addContainerGap())
|
||||
.addComponent(labelStatus, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
|
||||
);
|
||||
|
||||
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel1, jLabel2});
|
||||
|
||||
layout.setVerticalGroup(
|
||||
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
||||
.addGroup(layout.createSequentialGroup()
|
||||
.addContainerGap()
|
||||
.addComponent(labelSuggestion)
|
||||
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
|
||||
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
|
||||
.addComponent(jLabel1)
|
||||
.addComponent(textScannedDatamatrix, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
|
||||
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
|
||||
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
|
||||
.addComponent(jLabel2)
|
||||
.addComponent(textDetectedPuck, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
|
||||
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
|
||||
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE)
|
||||
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
|
||||
.addComponent(buttonSet)
|
||||
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
|
||||
.addComponent(labelStatus))
|
||||
);
|
||||
|
||||
pack();
|
||||
}// </editor-fold>//GEN-END:initComponents
|
||||
|
||||
private void buttonSetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSetActionPerformed
|
||||
try {
|
||||
int row = table.getSelectedRow();
|
||||
String datamatrix = (row >= 0) ? String.valueOf(table.getValueAt(row, 0)) : null;
|
||||
if (datamatrix != null) {
|
||||
onManualSetPuckLocation(datamatrix);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
SwingUtils.showException(this, ex);
|
||||
}
|
||||
}//GEN-LAST:event_buttonSetActionPerformed
|
||||
|
||||
/**
|
||||
* @param args the command line arguments
|
||||
*/
|
||||
public static void main(String args[]) {
|
||||
/* Set the Nimbus look and feel */
|
||||
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
|
||||
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
|
||||
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
|
||||
*/
|
||||
try {
|
||||
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
|
||||
if ("Nimbus".equals(info.getName())) {
|
||||
javax.swing.UIManager.setLookAndFeel(info.getClassName());
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (ClassNotFoundException ex) {
|
||||
java.util.logging.Logger.getLogger(PuckLoadingDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
|
||||
} catch (InstantiationException ex) {
|
||||
java.util.logging.Logger.getLogger(PuckLoadingDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
|
||||
} catch (IllegalAccessException ex) {
|
||||
java.util.logging.Logger.getLogger(PuckLoadingDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
|
||||
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
|
||||
java.util.logging.Logger.getLogger(PuckLoadingDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
|
||||
}
|
||||
//</editor-fold>
|
||||
|
||||
/* Create and display the dialog */
|
||||
java.awt.EventQueue.invokeLater(new Runnable() {
|
||||
public void run() {
|
||||
PuckLoadingDialog dialog = new PuckLoadingDialog(new javax.swing.JFrame(), true);
|
||||
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
|
||||
@Override
|
||||
public void windowClosing(java.awt.event.WindowEvent e) {
|
||||
System.exit(0);
|
||||
}
|
||||
});
|
||||
dialog.setVisible(true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Variables declaration - do not modify//GEN-BEGIN:variables
|
||||
javax.swing.JButton buttonSet;
|
||||
private javax.swing.JLabel jLabel1;
|
||||
private javax.swing.JLabel jLabel2;
|
||||
private javax.swing.JScrollPane jScrollPane1;
|
||||
private javax.swing.JLabel labelStatus;
|
||||
private javax.swing.JLabel labelSuggestion;
|
||||
javax.swing.JTable table;
|
||||
javax.swing.JTextField textDetectedPuck;
|
||||
javax.swing.JTextField textScannedDatamatrix;
|
||||
// End of variables declaration//GEN-END:variables
|
||||
}
|
||||
Reference in New Issue
Block a user