MainPanel prototype

This commit is contained in:
gac-S_Changer
2018-05-02 16:09:06 +02:00
parent 8f3409060b
commit 01528ae2b1
7 changed files with 725 additions and 546 deletions

View File

@@ -1,339 +1,344 @@
/*
* Copyright (c) 2014 Paul Scherrer Institute. All rights reserved.
*/
package ch.psi.mxsc;
import ch.psi.mxsc.BasePlate.DrawMode;
import ch.psi.pshell.core.Context;
import ch.psi.pshell.imaging.Data;
import ch.psi.pshell.imaging.ImageListener;
import ch.psi.pshell.imaging.Source;
import ch.psi.pshell.swing.DevicePanel;
import ch.psi.utils.State;
import ch.psi.utils.swing.SwingUtils;
import java.awt.Component;
import java.awt.Point;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
/**
*
*/
public class BasePlatePanel extends DevicePanel {
JPopupMenu samplePopupMenu;
JMenuItem menuLoadSample;
JMenuItem menuUnloadSample;
/**
* Creates new form BasePlatePanel
*/
public BasePlatePanel() {
initComponents();
addMouseListener(mouseAdapter);
samplePopupMenu = new JPopupMenu();
menuLoadSample = new JMenuItem("Load");
menuLoadSample.addActionListener((ActionEvent e) -> {
try {
getDevice().loadSample();
} catch (Exception ex) {
SwingUtils.showException(this, ex);
}
});
menuUnloadSample = new JMenuItem("Unload");
menuUnloadSample.addActionListener((ActionEvent e) -> {
try {
getDevice().unloadSample(getDevice().getSelectedSample());
} catch (Exception ex) {
SwingUtils.showException(this, ex);
}
});
samplePopupMenu.add(menuLoadSample);
samplePopupMenu.add(menuUnloadSample);
}
@Override
public BasePlate getDevice() {
return (BasePlate) super.getDevice();
}
Mode mode = Mode.horizontal;
public enum Mode {
single,
horizontal,
vertical,
overlapped,
}
public Mode getMode() {
return mode;
}
public void setMode(Mode mode) {
this.mode = mode;
repaint();
}
/**
* 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() {
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
}// </editor-fold>//GEN-END:initComponents
@Override
protected void onDeviceStateChanged(State state, State former) {
repaint();
}
Rectangle platePlotRect;
Rectangle puckPlotRect;
Source source;
@Override
public void paint(Graphics g) {
super.paint(g);
if (getDevice() != null) {
Graphics2D g2d = (Graphics2D) g;
Dimension size = getSize();
if ((size.width > 40) && (size.height > 40)) {
int border = 0;
int borderPuck = 20;
Rectangle plotRect = new Rectangle(border, border, size.width - 2 * border, size.height - 2 * border);
Puck selectedPuck = getDevice().getSelectedPuck();
platePlotRect = null;
puckPlotRect = null;
switch (mode) {
case single:
platePlotRect = plotRect;
puckPlotRect = null;
getDevice().draw(g2d, platePlotRect, (img==null), false, true, DrawMode.center, img);
break;
case horizontal:
platePlotRect = new Rectangle(plotRect.x, plotRect.y, plotRect.width / 2, plotRect.height);
getDevice().draw(g2d, platePlotRect, false, true, true, DrawMode.fill, null);
if (selectedPuck!=null){
puckPlotRect = new Rectangle(plotRect.x + plotRect.width / 2 + borderPuck, plotRect.y + borderPuck, plotRect.width / 2 - 2 * borderPuck, plotRect.height - 2 * borderPuck);
selectedPuck.draw(g2d, puckPlotRect, true, false, true, null);
}
break;
case vertical:
platePlotRect = new Rectangle(plotRect.x, plotRect.y, plotRect.width, plotRect.height / 2);
getDevice().draw(g2d, platePlotRect, false, true, true, DrawMode.fill, null);
if (selectedPuck!=null){
puckPlotRect = new Rectangle(plotRect.x + borderPuck, plotRect.y + plotRect.height / 2 + borderPuck, plotRect.width - 2 * borderPuck, plotRect.height / 2 - 2 * borderPuck);
selectedPuck.draw(g2d, puckPlotRect, true, false, true, null);
}
break;
case overlapped:
//getDevice().draw(g2d, plotRect, false, true);
platePlotRect = new Rectangle(plotRect.x - ((int) (plotRect.width * 0.14)), plotRect.y - ((int) (plotRect.height * 0.14)), (int) (plotRect.width * 1.28), (int) (plotRect.height * 1.28));
getDevice().draw(g2d, platePlotRect, false, true, false, DrawMode.fill, null);
if (selectedPuck!=null){
int overlappedSize = (int) (1.5 * selectedPuck.getDrawSize());
puckPlotRect = new Rectangle((int) (plotRect.getCenterX() - overlappedSize / 2), (int) (plotRect.getCenterY() - overlappedSize / 2), overlappedSize, overlappedSize);
selectedPuck.draw(g2d, puckPlotRect, true, false, true, null);
}
break;
}
}
}
}
BufferedImage img;
ImageListener imageListener = new ImageListener() {
@Override
public void onImage(Object origin, BufferedImage image, Data data) {
img = image;
repaint();
}
@Override
public void onError(Object origin, Exception ex) {
img = null;
repaint();
}
};
void setCameraView(Source source){
img = null;
if (this.source!=null){
this.source.removeListener(imageListener);
}
this.source = source;
if (source!=null){
source.addListener(imageListener);
}
repaint();
}
MouseAdapter mouseAdapter = new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
checkMouseEvent(e, true);
}
@Override
public void mouseReleased(MouseEvent e) {
checkMouseEvent(e, false);
}
private void checkMouseEvent(MouseEvent e, boolean pressed) {
if (isEnabled() && (getDevice()!=null) && getDevice().isSelectable()) {
try {
Sample sample = getSample(e.getX(), e.getY());
Puck puck = getPuck(e.getX(), e.getY());
if (e.isPopupTrigger()) {
if (sample != null) {
onSamplePopupMenu(e, sample);
} else if (puck != null){
onPuckPopupMenu(e, puck);
} else {
onBasePlatePopupMenu(e);
}
} else if ((pressed) && (e.getClickCount() % 2 == 0)) {
if (sample != null) {
onSampleDoubleClicked(e, sample);
} else if (puck != null){
onPuckDoubleClicked(e, puck);
}
} else if ((e.getButton() == java.awt.event.MouseEvent.BUTTON1)) {
if (sample != null) {
if (pressed) {
onSamplePressed(e, sample);
} else {
onSampleReleased(e, sample);
}
}else if (puck != null){
if (pressed) {
onPuckPressed(e, puck);
} else {
onPuckReleased(e, puck);
}
}
}
} catch (Exception ex) {
Logger.getLogger(BasePlatePanel.class.getName()).log(Level.WARNING, null, ex);
}
}
}
@Override
public void mouseClicked(MouseEvent e) {
if (isEnabled()&& (getDevice()!=null)) {
if (e.getButton() == java.awt.event.MouseEvent.BUTTON1) {
Sample sample = getSample(e.getX(), e.getY());
Puck puck = getPuck(e.getX(), e.getY());
if (sample != null) {
onSampleClicked(e, sample);
} else {
onPuckClicked(e, puck);
}
}
}
}
};
Sample getSample(int x, int y) {
Point p = new Point(x, y);
for (Puck puck : getDevice().getPucks()){
if (puck.isSelected() || (mode==Mode.single)){
for (Sample sample :puck.getSamples()){
if (sample.getDrawPosition().distance(p) <= (sample.getDrawSize() / 2)) {
return sample;
}
}
}
}
return null;
}
Puck getPuck(int x, int y) {
Point p = new Point(x, y);
for (Puck puck : getDevice().getPucks()){
if (puck.getDrawPosition().distance(p) <= (puck.getDrawSize() / 2)) {
return puck;
}
}
return null;
}
void onSampleClicked(MouseEvent e, Sample sample){
}
void onSamplePressed(MouseEvent e, Sample sample){
sample.setSelected(true);
repaint();
}
void onSampleReleased(MouseEvent e, Sample sample){
}
void onSamplePopupMenu(MouseEvent e,Sample sample){
System.out.println(sample);
menuLoadSample.setEnabled(sample.isPresent() && getDevice().getLoadedSample()==null);
menuUnloadSample.setEnabled(!sample.isPresent() && getDevice().getLoadedSample()!=null);
samplePopupMenu.show(e.getComponent(), e.getX(), e.getY());
}
void onSampleDoubleClicked(MouseEvent e,Sample sample){
}
void onPuckClicked(MouseEvent e, Puck puck){
}
void onPuckPressed(MouseEvent e, Puck puck){
puck.setSelected(true);
repaint();
}
void onPuckReleased(MouseEvent e, Puck puck){
}
void onPuckPopupMenu(MouseEvent e, Puck puck){
System.out.println(puck);
}
void onPuckDoubleClicked(MouseEvent e, Puck puck){
}
void onBasePlatePopupMenu(MouseEvent e){
}
// Variables declaration - do not modify//GEN-BEGIN:variables
// End of variables declaration//GEN-END:variables
}
/*
* Copyright (c) 2014 Paul Scherrer Institute. All rights reserved.
*/
package ch.psi.mxsc;
import ch.psi.mxsc.BasePlate.DrawMode;
import ch.psi.pshell.core.Context;
import ch.psi.pshell.imaging.Data;
import ch.psi.pshell.imaging.ImageListener;
import ch.psi.pshell.imaging.Source;
import ch.psi.pshell.swing.DevicePanel;
import ch.psi.utils.State;
import ch.psi.utils.swing.SwingUtils;
import java.awt.Component;
import java.awt.Point;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
/**
*
*/
public class BasePlatePanel extends DevicePanel {
JPopupMenu samplePopupMenu;
JMenuItem menuLoadSample;
JMenuItem menuUnloadSample;
/**
* Creates new form BasePlatePanel
*/
public BasePlatePanel() {
initComponents();
addMouseListener(mouseAdapter);
samplePopupMenu = new JPopupMenu();
menuLoadSample = new JMenuItem("Load");
menuLoadSample.addActionListener((ActionEvent e) -> {
try {
getDevice().loadSample();
} catch (Exception ex) {
SwingUtils.showException(this, ex);
}
});
menuUnloadSample = new JMenuItem("Unload");
menuUnloadSample.addActionListener((ActionEvent e) -> {
try {
getDevice().unloadSample(getDevice().getSelectedSample());
} catch (Exception ex) {
SwingUtils.showException(this, ex);
}
});
samplePopupMenu.add(menuLoadSample);
samplePopupMenu.add(menuUnloadSample);
}
@Override
public BasePlate getDevice() {
return (BasePlate) super.getDevice();
}
Mode mode = Mode.horizontal;
public enum Mode {
single,
horizontal,
vertical,
overlapped,
}
public Mode getMode() {
return mode;
}
public void setMode(Mode mode) {
this.mode = mode;
repaint();
}
/**
* 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() {
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
}// </editor-fold>//GEN-END:initComponents
@Override
protected void onDeviceStateChanged(State state, State former) {
repaint();
}
Rectangle platePlotRect;
Rectangle puckPlotRect;
Source source;
@Override
public void paint(Graphics g) {
super.paint(g);
if (getDevice() != null) {
Graphics2D g2d = (Graphics2D) g;
Dimension size = getSize();
if ((size.width > 40) && (size.height > 40)) {
int border = 0;
int borderPuck = 20;
Rectangle plotRect = new Rectangle(border, border, size.width - 2 * border, size.height - 2 * border);
Puck selectedPuck = getDevice().getSelectedPuck();
platePlotRect = null;
puckPlotRect = null;
switch (mode) {
case single:
platePlotRect = plotRect;
puckPlotRect = null;
getDevice().draw(g2d, platePlotRect, (img==null), false, true, DrawMode.center, img);
break;
case horizontal:
platePlotRect = new Rectangle(plotRect.x, plotRect.y, plotRect.width / 2, plotRect.height);
getDevice().draw(g2d, platePlotRect, false, true, true, DrawMode.fill, null);
if (selectedPuck!=null){
puckPlotRect = new Rectangle(plotRect.x + plotRect.width / 2 + borderPuck, plotRect.y + borderPuck, plotRect.width / 2 - 2 * borderPuck, plotRect.height - 2 * borderPuck);
selectedPuck.draw(g2d, puckPlotRect, true, false, true, null);
}
break;
case vertical:
platePlotRect = new Rectangle(plotRect.x, plotRect.y, plotRect.width, plotRect.height / 2);
getDevice().draw(g2d, platePlotRect, false, true, true, DrawMode.fill, null);
if (selectedPuck!=null){
puckPlotRect = new Rectangle(plotRect.x + borderPuck, plotRect.y + plotRect.height / 2 + borderPuck, plotRect.width - 2 * borderPuck, plotRect.height / 2 - 2 * borderPuck);
selectedPuck.draw(g2d, puckPlotRect, true, false, true, null);
}
break;
case overlapped:
//getDevice().draw(g2d, plotRect, false, true);
platePlotRect = new Rectangle(plotRect.x - ((int) (plotRect.width * 0.14)), plotRect.y - ((int) (plotRect.height * 0.14)), (int) (plotRect.width * 1.28), (int) (plotRect.height * 1.28));
getDevice().draw(g2d, platePlotRect, false, true, false, DrawMode.fill, null);
if (selectedPuck!=null){
int overlappedSize = (int) (1.5 * selectedPuck.getDrawSize());
puckPlotRect = new Rectangle((int) (plotRect.getCenterX() - overlappedSize / 2), (int) (plotRect.getCenterY() - overlappedSize / 2), overlappedSize, overlappedSize);
selectedPuck.draw(g2d, puckPlotRect, true, false, true, null);
}
break;
}
}
}
}
BufferedImage img;
ImageListener imageListener = new ImageListener() {
@Override
public void onImage(Object origin, BufferedImage image, Data data) {
img = image;
repaint();
}
@Override
public void onError(Object origin, Exception ex) {
img = null;
repaint();
}
};
void setCameraView(Source source){
img = null;
if (this.source!=null){
this.source.removeListener(imageListener);
}
this.source = source;
if (source!=null){
source.addListener(imageListener);
}
repaint();
}
MouseAdapter mouseAdapter = new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
checkMouseEvent(e, true);
}
@Override
public void mouseReleased(MouseEvent e) {
checkMouseEvent(e, false);
}
private void checkMouseEvent(MouseEvent e, boolean pressed) {
if (isEnabled() && (getDevice()!=null)) {
try {
Sample sample = getSample(e.getX(), e.getY());
Puck puck = getPuck(e.getX(), e.getY());
if (e.isPopupTrigger()) {
if (sample != null) {
onSamplePopupMenu(e, sample);
} else if (puck != null){
onPuckPopupMenu(e, puck);
} else {
onBasePlatePopupMenu(e);
}
} else if ((pressed) && (e.getClickCount() % 2 == 0)) {
if (sample != null) {
onSampleDoubleClicked(e, sample);
} else if (puck != null){
onPuckDoubleClicked(e, puck);
}
} else if ((e.getButton() == java.awt.event.MouseEvent.BUTTON1)) {
if (sample != null) {
if (pressed) {
onSamplePressed(e, sample);
} else {
onSampleReleased(e, sample);
}
}else if (puck != null){
if (pressed) {
onPuckPressed(e, puck);
} else {
onPuckReleased(e, puck);
}
}
}
} catch (Exception ex) {
Logger.getLogger(BasePlatePanel.class.getName()).log(Level.WARNING, null, ex);
}
}
}
@Override
public void mouseClicked(MouseEvent e) {
if (isEnabled()&& (getDevice()!=null)) {
if (e.getButton() == java.awt.event.MouseEvent.BUTTON1) {
Sample sample = getSample(e.getX(), e.getY());
Puck puck = getPuck(e.getX(), e.getY());
if (sample != null) {
onSampleClicked(e, sample);
} else {
onPuckClicked(e, puck);
}
}
}
}
};
Sample getSample(int x, int y) {
Point p = new Point(x, y);
for (Puck puck : getDevice().getPucks()){
if (puck.isSelected() || (mode==Mode.single)){
for (Sample sample :puck.getSamples()){
if (sample.getDrawPosition().distance(p) <= (sample.getDrawSize() / 2)) {
return sample;
}
}
}
}
return null;
}
Puck getPuck(int x, int y) {
Point p = new Point(x, y);
for (Puck puck : getDevice().getPucks()){
if (puck.getDrawPosition().distance(p) <= (puck.getDrawSize() / 2)) {
return puck;
}
}
return null;
}
void onSampleClicked(MouseEvent e, Sample sample){
}
void onSamplePressed(MouseEvent e, Sample sample){
if (getDevice().isSelectable()){
sample.toggleSelected(true);
repaint();
}
Controller.getInstance().onSamplePressed(sample);
}
void onSampleReleased(MouseEvent e, Sample sample){
}
void onSamplePopupMenu(MouseEvent e,Sample sample){
System.out.println(sample);
menuLoadSample.setEnabled(sample.isPresent() && getDevice().getLoadedSample()==null);
menuUnloadSample.setEnabled(!sample.isPresent() && getDevice().getLoadedSample()!=null);
samplePopupMenu.show(e.getComponent(), e.getX(), e.getY());
}
void onSampleDoubleClicked(MouseEvent e,Sample sample){
}
void onPuckClicked(MouseEvent e, Puck puck){
}
void onPuckPressed(MouseEvent e, Puck puck){
if (getDevice().isSelectable()){
puck.toggleSelected(true);
repaint();
}
Controller.getInstance().onPuckPressed(puck);
}
void onPuckReleased(MouseEvent e, Puck puck){
}
void onPuckPopupMenu(MouseEvent e, Puck puck){
System.out.println(puck);
}
void onPuckDoubleClicked(MouseEvent e, Puck puck){
}
void onBasePlatePopupMenu(MouseEvent e){
}
// Variables declaration - do not modify//GEN-BEGIN:variables
// End of variables declaration//GEN-END:variables
}