Added mouse interaction to puck panel

This commit is contained in:
2017-03-16 12:14:34 +01:00
parent 02c75df33d
commit 4d0b19d801
6 changed files with 845 additions and 760 deletions

View File

@@ -1,147 +1,203 @@
/*
* Copyright (c) 2014 Paul Scherrer Institute. All rights reserved.
*/
package ch.psi.mxsc;
import ch.psi.pshell.device.Device;
import ch.psi.pshell.device.DeviceBase;
import ch.psi.pshell.imaging.DimensionDouble;
import ch.psi.pshell.imaging.PointDouble;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.io.IOException;
import java.util.ArrayList;
/**
*
*/
public class BasePlate extends DeviceBase {
final static PointDouble[] pucksPosition = new PointDouble[]{
new PointDouble(0, 75),
new PointDouble(0, 150),
new PointDouble(64.95, 187.5),
new PointDouble(129.9, 150),
new PointDouble(64.95, 112.5),
new PointDouble(64.95, 37.5),
new PointDouble(129.9, 75),
new PointDouble(194.85, 37.5),
new PointDouble(194.85, -37.5),
new PointDouble(129.9, 0),
new PointDouble(64.95, -37.5),
new PointDouble(129.9, -75),
new PointDouble(129.9, -150),
new PointDouble(64.95, -187.5),
new PointDouble(64.95, -112.5),
new PointDouble(0, -75),
new PointDouble(0, -150),
new PointDouble(-64.95, -187.5),
new PointDouble(-129.9, -150),
new PointDouble(-64.95, -112.5),
new PointDouble(-64.95, -37.5),
new PointDouble(-129.9, -75),
new PointDouble(-194.85, -37.5),
new PointDouble(-194.85, 37.5),
new PointDouble(-129.9, 0),
new PointDouble(-64.95, 37.5),
new PointDouble(-129.9, 75),
new PointDouble(-129.9, 150),
new PointDouble(-64.95, 187.5),
new PointDouble(-64.95, 112.5)
};
final static int numberOfPucks = pucksPosition.length;
//final static DimensionDouble size = new DimensionDouble(580.0, 580.0);
final static DimensionDouble size = new DimensionDouble(580.0, 580.0);
BasePlate() {
super("BasePlate", new BasePlateConfig());
ArrayList<Puck> pucks = new ArrayList<>();
for (int i = 0; i < numberOfPucks; i++) {
Puck puck = new Puck();
puck.index = i;
puck.basePlate = this;
this.addChild(puck);
}
getPucks()[0].setSelected(true);
}
@Override
protected void doInitialize() throws IOException, InterruptedException {
super.doInitialize();
}
@Override
public BasePlateConfig getConfig() {
return (BasePlateConfig) super.getConfig();
}
public Puck[] getPucks() {
ArrayList<Puck> ret = new ArrayList<>();
for (Device d : getChildren()) {
ret.add((Puck) d);
}
return ret.toArray(new Puck[0]);
}
Puck getSelectedPuck(){
for (Puck p:getPucks()){
if (p.isSelected()){
return p;
}
}
return null;
}
Sample getSelectedSample(){
Puck puck = getSelectedPuck();
if (puck != null){
for (Sample s: puck.getSamples()){
if (s.isSelected()){
return s;
}
}
}
return null;
}
DimensionDouble getSize(){
return size;
}
public int getNumberOfPucks() {
return numberOfPucks;
}
public PointDouble getPuckPosition(Puck puck) {
return pucksPosition[puck.index];
}
Rectangle plotRect = new Rectangle(0, 0, 0, 0);
public Rectangle getPlotRect() {
return plotRect;
}
void draw(Graphics2D g, Rectangle plotRect, boolean drawSamples, boolean drawIds, boolean drawContour) {
this.plotRect = plotRect;
g.setColor(Color.BLACK);
if (drawContour){
//int size = Math.min(plotRect.width, plotRect.height);
//g.drawOval((int)(plotRect.getCenterX() - size/2),(int)(plotRect.getCenterY() - size/2), size, size);
g.drawOval((int)(plotRect.width*0.05),
(int)(plotRect.height * 0.05),
(int)(plotRect.width*0.90),
(int)(plotRect.height*0.90));
}
for (Puck puck : getPucks()) {
puck.draw(g, null, drawSamples, drawIds) ;
}
}
}
/*
* Copyright (c) 2014 Paul Scherrer Institute. All rights reserved.
*/
package ch.psi.mxsc;
import ch.psi.pshell.device.Device;
import ch.psi.pshell.device.DeviceBase;
import ch.psi.pshell.imaging.DimensionDouble;
import ch.psi.pshell.imaging.PointDouble;
import ch.psi.utils.swing.MainFrame;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.io.IOException;
import java.util.ArrayList;
/**
*
*/
public class BasePlate extends DeviceBase {
final static PointDouble[] pucksPosition = new PointDouble[]{
new PointDouble(0, 75),
new PointDouble(0, 150),
new PointDouble(64.95, 187.5),
new PointDouble(129.9, 150),
new PointDouble(64.95, 112.5),
new PointDouble(64.95, 37.5),
new PointDouble(129.9, 75),
new PointDouble(194.85, 37.5),
new PointDouble(194.85, -37.5),
new PointDouble(129.9, 0),
new PointDouble(64.95, -37.5),
new PointDouble(129.9, -75),
new PointDouble(129.9, -150),
new PointDouble(64.95, -187.5),
new PointDouble(64.95, -112.5),
new PointDouble(0, -75),
new PointDouble(0, -150),
new PointDouble(-64.95, -187.5),
new PointDouble(-129.9, -150),
new PointDouble(-64.95, -112.5),
new PointDouble(-64.95, -37.5),
new PointDouble(-129.9, -75),
new PointDouble(-194.85, -37.5),
new PointDouble(-194.85, 37.5),
new PointDouble(-129.9, 0),
new PointDouble(-64.95, 37.5),
new PointDouble(-129.9, 75),
new PointDouble(-129.9, 150),
new PointDouble(-64.95, 187.5),
new PointDouble(-64.95, 112.5)
};
final static int numberOfPucks = pucksPosition.length;
//final static DimensionDouble size = new DimensionDouble(580.0, 580.0);
final static DimensionDouble size = new DimensionDouble(580.0, 580.0);
BasePlate() {
super("BasePlate", new BasePlateConfig());
ArrayList<Puck> pucks = new ArrayList<>();
for (int i = 0; i < numberOfPucks; i++) {
new Puck(this, i);
}
getPucks()[0].setSelected(true);
}
@Override
protected void doInitialize() throws IOException, InterruptedException {
super.doInitialize();
}
@Override
public BasePlateConfig getConfig() {
return (BasePlateConfig) super.getConfig();
}
public Puck[] getPucks() {
ArrayList<Puck> ret = new ArrayList<>();
for (Device d : getChildren()) {
ret.add((Puck) d);
}
return ret.toArray(new Puck[0]);
}
Puck getSelectedPuck(){
for (Puck p:getPucks()){
if (p.isSelected()){
return p;
}
}
return null;
}
Sample getSelectedSample(){
Puck puck = getSelectedPuck();
if (puck != null){
for (Sample s: puck.getSamples()){
if (s.isSelected()){
return s;
}
}
}
return null;
}
Sample getLoadedSample(){
for (Puck p:getPucks()){
for (Sample s: p.getSamples()){
if (s.isLoaded()){
return s;
}
}
}
return null;
}
void resetLoadedSample(){
for (Puck p:getPucks()){
for (Sample s: p.getSamples()){
s.setLoaded(false);
}
}
}
DimensionDouble getSize(){
return size;
}
public int getNumberOfPucks() {
return numberOfPucks;
}
public PointDouble getPuckPosition(Puck puck) {
return pucksPosition[puck.index];
}
Rectangle plotRect = new Rectangle(0, 0, 0, 0);
public Rectangle getPlotRect() {
return plotRect;
}
Color getBorderColor() {
//return new Color(32,32,32);
return MainFrame.isDark() ? new Color(32,32,32) : Color.DARK_GRAY;
}
Color getColor() {
return MainFrame.isDark() ? new Color(67,71,73) /*new Color(65,81,109)*/ : new Color(200, 204, 213);
}
boolean drawContour;
void draw(Graphics2D g, Rectangle plotRect, boolean drawSamples, boolean drawIds, boolean drawContour) {
this.plotRect = plotRect;
this.drawContour = drawContour;
if (drawContour){
g.setColor(getColor());
g.fillOval((int)(plotRect.width*0.05), (int)(plotRect.height * 0.05), (int)(plotRect.width*0.90), (int)(plotRect.height*0.90));
g.setColor(getBorderColor());
g.drawOval((int)(plotRect.width*0.05), (int)(plotRect.height * 0.05), (int)(plotRect.width*0.90), (int)(plotRect.height*0.90));
}
for (Puck puck : getPucks()) {
puck.draw(g, null, drawSamples, drawIds, this) ;
}
}
public void loadSample(Sample sample) throws Exception{
Sample loaded = getLoadedSample();
if (loaded !=null){
throw new Exception("Sample already loaded: " + loaded);
}
if (sample!=null){
resetLoadedSample();
//TODO
sample.setLoaded(true);
}
}
public void unloadSample(Sample sample) throws Exception{
if (sample!=null){
//TODO
resetLoadedSample();
}
}
public void loadSample() throws Exception{
loadSample(getSelectedSample());
}
public void unloadSample() throws Exception{
unloadSample (getLoadedSample());
}
}

View File

@@ -1,276 +1,304 @@
/*
* Copyright (c) 2014 Paul Scherrer Institute. All rights reserved.
*/
package ch.psi.mxsc;
import ch.psi.pshell.swing.DevicePanel;
import ch.psi.utils.State;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
*/
public class BasePlatePanel extends DevicePanel {
/**
* Creates new form BasePlatePanel
*/
public BasePlatePanel() {
initComponents();
//addMouseListener(mouseAdapter);
}
@Override
public BasePlate getDevice() {
return (BasePlate) super.getDevice();
}
Mode mode = Mode.single;
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;
@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, true, false, true);
break;
case horizontal:
platePlotRect = new Rectangle(plotRect.x, plotRect.y, plotRect.width / 2, plotRect.height);
getDevice().draw(g2d, platePlotRect, false, true, true);
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);
}
break;
case vertical:
platePlotRect = new Rectangle(plotRect.x, plotRect.y, plotRect.width, plotRect.height / 2);
getDevice().draw(g2d, platePlotRect, false, true, true);
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);
}
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);
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);
}
break;
}
}
}
}
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()) {
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()) {
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) {
return getDevice().getPucks()[0].getSamples()[0];
/*
Point point = new Point(x, y);
for (int basket = 0; basket < mBaskets; basket++) {
for (int position = 0; position < mPositions; position++) {
Point center = getSampleCenter(basket, position);
double dist = center.distance(point);
if (dist <= (((double) getSampleSize()) / 2)) {
return new Sample(basket, position);
}
}
Point center = getBasketCenter(basket);
double dist = center.distance(point);
if (dist <= (((double) getBasketSize()) / 2)) {
return new Sample(basket, -1);
}
}
return null;
*/
}
Puck getPuck(int x, int y) {
return getDevice().getPucks()[0];
}
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){
}
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){
}
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.pshell.swing.DevicePanel;
import ch.psi.utils.State;
import ch.psi.utils.swing.SwingUtils;
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.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;
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;
@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, true, false, true);
break;
case horizontal:
platePlotRect = new Rectangle(plotRect.x, plotRect.y, plotRect.width / 2, plotRect.height);
getDevice().draw(g2d, platePlotRect, false, true, true);
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, null);
}
break;
case vertical:
platePlotRect = new Rectangle(plotRect.x, plotRect.y, plotRect.width, plotRect.height / 2);
getDevice().draw(g2d, platePlotRect, false, true, true);
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, 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);
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, null);
}
break;
}
}
}
}
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()) {
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()) {
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
}

View File

@@ -7,6 +7,7 @@ import ch.psi.pshell.device.Device;
import ch.psi.pshell.device.DeviceBase;
import ch.psi.pshell.imaging.DimensionDouble;
import ch.psi.pshell.imaging.PointDouble;
import ch.psi.utils.swing.MainFrame;
import ch.psi.utils.swing.SwingUtils;
import java.awt.Color;
import java.awt.Dimension;
@@ -40,27 +41,25 @@ public class Puck extends DeviceBase {
new PointDouble(-28.39445573, 44.18263554)
};
final static PointDouble referencePosition = new PointDouble(0.0, -66.9);
final static Double referenceSize = 6.2;
final static PointDouble referencePosition = new PointDouble(0.0, -66.9 - 0.2);
final static Double referenceSize = 6.2 + 0.2;
final int numberOfSamples = samplesPosition.length;
final int index;
Puck() {
super();
Puck(BasePlate basePlate, int index) {
super(String.valueOf(index+1));
this.setParent(basePlate);
this.index = index;
for (int i =0; i< numberOfSamples; i++){
Sample sample = new Sample();
sample.puck = this;
sample.index = i;
addChild(sample);
new Sample(this, i);
}
}
BasePlate basePlate;
}
public BasePlate getBasePlate() {
return basePlate;
return (BasePlate) getParent();
}
DimensionDouble getSize(){
@@ -82,16 +81,11 @@ public class Puck extends DeviceBase {
}
int index;
public int getIndex() {
return index;
}
public void setIndex(int value) {
index = value;
}
String id;
public String getId() {
@@ -145,7 +139,10 @@ public class Puck extends DeviceBase {
}
}
}
selected = value;
for (Sample sample: getSamples()) {
sample.selected = false;
}
selected = value;
}
}
@@ -184,10 +181,10 @@ public class Puck extends DeviceBase {
return Math.min(plotRect.width, plotRect.height);
}
//All pucks
Rectangle plotRect = basePlate.getPlotRect();
Rectangle plotRect = getBasePlate().getPlotRect();
int ret = Math.min(
(int)((getSize().getWidth() / basePlate.getSize().getWidth()) * plotRect.width),
(int)((getSize().getHeight() / basePlate.getSize().getHeight()) * plotRect.height)
(int)((getSize().getWidth() / getBasePlate().getSize().getWidth()) * plotRect.width),
(int)((getSize().getHeight() / getBasePlate().getSize().getHeight()) * plotRect.height)
);
if (isSelected()) {
ret += 2;
@@ -201,9 +198,9 @@ public class Puck extends DeviceBase {
return new Point((int)plotRect.getCenterX(), (int)plotRect.getCenterY());
}
//All pucks
Rectangle plotRect = basePlate.getPlotRect();
DimensionDouble plateSize = basePlate.getSize();
PointDouble pos = basePlate.getPuckPosition(this);
Rectangle plotRect = getBasePlate().getPlotRect();
DimensionDouble plateSize = getBasePlate().getSize();
PointDouble pos = getBasePlate().getPuckPosition(this);
return new Point( (int) ((pos.x / plateSize.getWidth())*plotRect.width + plotRect.getCenterX()) ,
(int) ((pos.y / plateSize.getHeight())*plotRect.height + plotRect.getCenterY())
);
@@ -231,7 +228,7 @@ public class Puck extends DeviceBase {
if (!isEnabled()){
return Color.GRAY;
} else if (isSelected()){
return Color.BLACK;
return new Color(32,32,32);
}
return Color.GRAY;
}
@@ -250,8 +247,9 @@ public class Puck extends DeviceBase {
(int) ((referencePosition.y / puckSize.getHeight())* puckDrawSize/2 + puckCenter.y) );
}
Rectangle plotRect;
void draw (Graphics2D g, Rectangle plotRect, boolean drawSamples, boolean drawId){
void draw (Graphics2D g, Rectangle plotRect, boolean drawSamples, boolean drawId, BasePlate enclosingPlate){
this.plotRect = plotRect;
Point position = getDrawPosition();
int size = getDrawSize();
@@ -268,7 +266,12 @@ public class Puck extends DeviceBase {
}
//Draw reference
g.setColor(Color.DARK_GRAY);
Color refColor = MainFrame.isDark() ? Color.DARK_GRAY : new Color(214,217,223);
if ((enclosingPlate!=null) && enclosingPlate.drawContour){
refColor = enclosingPlate.getColor();
}
g.setColor(refColor);
position = getReferenceDrawPosition();
size = getReferenceDrawSize();
g.fillArc(position.x - size / 2, position.y - size / 2, size, size, 180, 180);

View File

@@ -1,73 +1,72 @@
/*
* Copyright (c) 2014 Paul Scherrer Institute. All rights reserved.
*/
package ch.psi.mxsc;
import ch.psi.pshell.swing.DevicePanel;
import ch.psi.utils.State;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
/**
*
*/
public class PuckPanel extends DevicePanel {
/**
* Creates new form BasePlatePanel
*/
public PuckPanel() {
initComponents();
}
@Override
public Puck getDevice(){
return (Puck) super.getDevice();
}
/**
* 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();
}
@Override
public void paint(Graphics g) {
super.paint(g);
if (getDevice()!=null){
Graphics2D g2d = (Graphics2D) g;
Dimension size = getSize();
if ((size.width > 10) && (size.height > 10)) {
int border = 5;
Rectangle plotRect = new Rectangle(border, border, size.width - 2*border, size.height - 2*border);
getDevice().draw(g2d, plotRect, true, false);
}
}
}
// 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.pshell.swing.DevicePanel;
import ch.psi.utils.State;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
/**
*
*/
public class PuckPanel extends DevicePanel {
/**
* Creates new form BasePlatePanel
*/
public PuckPanel() {
initComponents();
}
@Override
public Puck getDevice(){
return (Puck) super.getDevice();
}
/**
* 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();
}
@Override
public void paint(Graphics g) {
super.paint(g);
if (getDevice()!=null){
Graphics2D g2d = (Graphics2D) g;
Dimension size = getSize();
if ((size.width > 10) && (size.height > 10)) {
int border = 5;
Rectangle plotRect = new Rectangle(border, border, size.width - 2*border, size.height - 2*border);
getDevice().draw(g2d, plotRect, true, false, null);
}
}
}
// Variables declaration - do not modify//GEN-BEGIN:variables
// End of variables declaration//GEN-END:variables
}

View File

@@ -1,182 +1,181 @@
/*
* Copyright (c) 2014 Paul Scherrer Institute. All rights reserved.
*/
package ch.psi.mxsc;
import ch.psi.pshell.device.Device;
import ch.psi.pshell.device.DeviceBase;
import ch.psi.pshell.imaging.DimensionDouble;
import ch.psi.pshell.imaging.PointDouble;
import ch.psi.utils.swing.SwingUtils;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
/**
*
*/
public class Sample extends DeviceBase {
Puck puck;
public Puck getPuck() {
return puck;
}
DimensionDouble getSize(){
return new DimensionDouble(12.0, 12.0);
}
int index;
public int getIndex() {
return index;
}
public void setIndex(int value) {
index = value;
}
boolean enabled;
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean value) {
enabled = value;
}
boolean selected;
public boolean isSelected() {
return selected;
}
public void setSelected(boolean value) {
if (value != selected){
if (value == true){
((Puck)getParent()).setSelected(true);
for (Device d : getParent().getChildren()){
if (d instanceof Sample){
((Sample)d).selected = false;
}
}
}
selected = value;
}
}
boolean present;
public boolean isPresent() {
return present;
}
public void setPresent(boolean value) {
present = value;
}
boolean loaded;
public boolean isLoaded() {
return loaded;
}
public void setLoaded(boolean value) {
loaded = value;
if (value) {
wasLoaded = true;
}
}
boolean wasLoaded;
public boolean wasLoaded() {
return wasLoaded;
}
Color getColor() {
Color ret = Color.LIGHT_GRAY;
if (isLoaded()) {
ret = Color.BLUE;
} else if (wasLoaded()) {
ret = Color.GREEN;
} else if (isPresent()) {
ret = Color.CYAN.darker().darker();
}
if (selected) {
ret = ret.brighter();
}
return ret;
}
int getDrawSize() {
int ret = (int)((getSize().getWidth() / puck.getSize().getWidth()) * puck.getDrawSize());
if (isSelected()) {
ret += 2;
}
return ret;
}
Point getDrawPosition() {
Point puckCenter = puck.getDrawPosition();
int puckDrawSize = puck.getDrawSize();
DimensionDouble puckSize = puck.getSize();
int size = getDrawSize();
PointDouble pos = puck.getSamplePosition(this);
return new Point( (int) ((pos.x / puckSize.getWidth())* puckDrawSize/2 + puckCenter.x) ,
(int) ((pos.y / puckSize.getHeight())* puckDrawSize/2 + puckCenter.y) );
}
Color getLabelColor() {
return Color.BLACK;
}
Font getLabelFont() {
return new Font("Times New Roman", Font.PLAIN, 9);
}
Point getLabelPosition(String text, Graphics g) {
Point center = getDrawPosition();
Dimension textSize = SwingUtils.getTextSize(text, g.getFontMetrics());
return new Point(center.x - textSize.width / 2 + 1, center.y + (g.getFontMetrics().getAscent()/2));
}
Color getBorderColor() {
if (!isEnabled()) {
return Color.GRAY;
} else if (isSelected()) {
return Color.BLACK;
}
return Color.GRAY;
}
void draw (Graphics2D g){
Point position = getDrawPosition();
int size = getDrawSize();
g.setColor(getColor());
g.fillOval(position.x - size / 2, position.y - size / 2, size, size);
g.setColor(getBorderColor());
g.drawOval(position.x - size / 2, position.y - size / 2, size, size);
String text = String.valueOf(index + 1);
Point labelPosition = getLabelPosition(text, g);
g.setColor(getLabelColor());
g.setFont(getLabelFont());
g.drawString(text, labelPosition.x, labelPosition.y);
g.setPaintMode();
}
}
/*
* Copyright (c) 2014 Paul Scherrer Institute. All rights reserved.
*/
package ch.psi.mxsc;
import ch.psi.pshell.device.Device;
import ch.psi.pshell.device.DeviceBase;
import ch.psi.pshell.imaging.DimensionDouble;
import ch.psi.pshell.imaging.PointDouble;
import ch.psi.utils.swing.SwingUtils;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
/**
*
*/
public class Sample extends DeviceBase {
final int index;
public Sample(Puck puck, int index){
super((puck.getIndex()+1) + ":" + (index+1));
setParent(puck);
this.index = index;
}
public Puck getPuck() {
return (Puck) getParent();
}
DimensionDouble getSize(){
return new DimensionDouble(12.0, 12.0);
}
public int getIndex() {
return index;
}
boolean enabled;
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean value) {
enabled = value;
}
boolean selected;
public boolean isSelected() {
return selected;
}
public void setSelected(boolean value) {
if (value != selected){
if (value == true){
((Puck)getParent()).setSelected(true);
for (Device d : getParent().getChildren()){
if (d instanceof Sample){
((Sample)d).selected = false;
}
}
}
selected = value;
}
}
boolean present;
public boolean isPresent() {
return present;
}
public void setPresent(boolean value) {
present = value;
}
boolean loaded;
public boolean isLoaded() {
return loaded;
}
public void setLoaded(boolean value) {
loaded = value;
if (value) {
wasLoaded = true;
}
}
boolean wasLoaded;
public boolean wasLoaded() {
return wasLoaded;
}
Color getColor() {
Color ret = Color.LIGHT_GRAY;
if (isLoaded()) {
ret = Color.BLUE;
} else if (wasLoaded()) {
ret = Color.GREEN;
} else if (isPresent()) {
ret = Color.CYAN.darker().darker();
}
if (selected) {
ret = ret.brighter();
}
return ret;
}
int getDrawSize() {
int ret = (int)((getSize().getWidth() / getPuck().getSize().getWidth()) * getPuck().getDrawSize());
if (isSelected()) {
ret += 2;
}
return ret;
}
Point getDrawPosition() {
Point puckCenter = getPuck().getDrawPosition();
int puckDrawSize = getPuck().getDrawSize();
DimensionDouble puckSize = getPuck().getSize();
int size = getDrawSize();
PointDouble pos = getPuck().getSamplePosition(this);
return new Point( (int) ((pos.x / puckSize.getWidth())* puckDrawSize/2 + puckCenter.x) ,
(int) ((pos.y / puckSize.getHeight())* puckDrawSize/2 + puckCenter.y) );
}
Color getLabelColor() {
return Color.BLACK;
}
Font getLabelFont() {
return new Font("Times New Roman", Font.PLAIN, 9);
}
Point getLabelPosition(String text, Graphics g) {
Point center = getDrawPosition();
Dimension textSize = SwingUtils.getTextSize(text, g.getFontMetrics());
return new Point(center.x - textSize.width / 2 + 1, center.y + (g.getFontMetrics().getAscent()/2));
}
Color getBorderColor() {
if (!isEnabled()) {
return Color.GRAY;
} else if (isSelected()) {
return new Color(32,32,32);
}
return Color.GRAY;
}
void draw (Graphics2D g){
Point position = getDrawPosition();
int size = getDrawSize();
g.setColor(getColor());
g.fillOval(position.x - size / 2, position.y - size / 2, size, size);
g.setColor(getBorderColor());
g.drawOval(position.x - size / 2, position.y - size / 2, size, size);
String text = String.valueOf(index + 1);
Point labelPosition = getLabelPosition(text, g);
g.setColor(getLabelColor());
g.setFont(getLabelFont());
g.drawString(text, labelPosition.x, labelPosition.y);
g.setPaintMode();
}
}