/* * 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 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; import java.util.ArrayList; /** * */ public class Puck extends DeviceBase { final static PointDouble[] samplesPosition = new PointDouble[]{ new PointDouble(0, 24.24), new PointDouble(23.05360995, 7.490571944), new PointDouble(14.24791452, -19.61057194), new PointDouble(-14.24791452, -19.61057194), new PointDouble(-23.05360995, 7.490571944), new PointDouble(0, 52.52), new PointDouble(28.39445573, 44.18263554), new PointDouble(47.7738724, 21.81759648), new PointDouble(51.98542213, -7.474375306), new PointDouble(39.69196765, -34.39328575), new PointDouble(14.79659389, -50.39257097), new PointDouble(-14.79659389, -50.39257097), new PointDouble(-39.69196765, -34.39328575), new PointDouble(-51.98542213, -7.474375306), new PointDouble(-47.7738724, 21.81759648), new PointDouble(-28.39445573, 44.18263554) }; 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(BasePlate basePlate, int index) { super(String.valueOf(index+1)); this.setParent(basePlate); this.index = index; for (int i =0; i< numberOfSamples; i++){ new Sample(this, i); } } public BasePlate getBasePlate() { return (BasePlate) getParent(); } DimensionDouble getSize(){ return new DimensionDouble(67.0, 67.0); } public Sample[] getSamples() { ArrayList ret = new ArrayList<>(); for (Device d : getChildren()) { ret.add((Sample) d); } return ret.toArray(new Sample[0]); } public PointDouble getSamplePosition(Sample sample) { return samplesPosition[sample.index]; } public int getIndex() { return index; } String id; public String getId() { //return "XXX000" + index; return id; } public void seId(String value) { id = value; } boolean enabled; public boolean isEnabled() { return enabled; } public void setEnabled(boolean value) { enabled = value; } public enum Detection{ Empty, Present, Offline, Error } Detection detection = Detection.Error; public Detection getDetection() { return detection; } boolean detectionError; public boolean isDetectionError() { return detectionError; } boolean selected; public boolean isSelected() { return selected; } public void setSelected(boolean value) { if (value != selected){ if (value == true){ for (Device d : getParent().getChildren()){ if (d instanceof Puck){ ((Puck)d).selected = false; } } } for (Sample sample: getSamples()) { sample.selected = false; } selected = value; } } public int getNumberOfSamples() { return numberOfSamples; } Color getColor() { Color ret = Color.LIGHT_GRAY; switch (detection){ case Empty: ret = Color.LIGHT_GRAY; break; case Present: ret = new Color(0, 92, 92); break; case Error: ret = new Color(128, 0, 0); break; case Offline: ret = new Color(92, 92, 0); break; } boolean selected = isSelected(); if (isSelected()) { ret = ret.brighter(); } return ret; } int getDrawSize() { //Single puck plot if (plotRect != null){ return Math.min(plotRect.width, plotRect.height); } //All pucks Rectangle plotRect = getBasePlate().getPlotRect(); int ret = Math.min( (int)((getSize().getWidth() / getBasePlate().getSize().getWidth()) * plotRect.width), (int)((getSize().getHeight() / getBasePlate().getSize().getHeight()) * plotRect.height) ); if (isSelected()) { ret += 2; } return ret; } Point getDrawPosition() { //Single puck plot if (plotRect != null){ return new Point((int)plotRect.getCenterX(), (int)plotRect.getCenterY()); } //All pucks 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()) ); } Color getLabelColor() { return Color.BLACK; } Font getLabelFont() { if (plotRect != null){ return new Font("Times New Roman", Font.BOLD, 18); } return new Font("Times New Roman", Font.BOLD, 10); } Font getIdFont() { if (plotRect != null){ return new Font("Times New Roman", Font.PLAIN, 12); } 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, 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; } int getReferenceDrawSize() { return (int)((referenceSize / getSize().getWidth()) * getDrawSize()); } Point getReferenceDrawPosition() { Point puckCenter = getDrawPosition(); int puckDrawSize = getDrawSize(); DimensionDouble puckSize = getSize(); int size = getReferenceDrawSize(); return new Point( (int) ((referencePosition.x / puckSize.getWidth())* puckDrawSize/2 + puckCenter.x) , (int) ((referencePosition.y / puckSize.getHeight())* puckDrawSize/2 + puckCenter.y) ); } Rectangle plotRect; void draw (Graphics2D g, Rectangle plotRect, boolean drawSamples, boolean drawId, BasePlate enclosingPlate){ this.plotRect = plotRect; 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); if (drawSamples){ //Draw samples for (Sample sample: getSamples()) { sample.draw(g); } } //Draw reference 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); //Draw text String text = String.valueOf(getIndex() + 1); Point labelPosition = getLabelPosition(text, g); g.setColor(getLabelColor()); if (drawId){ String id = getId(); if (id!=null) { labelPosition.setLocation(labelPosition.x, labelPosition.y - 6); g.setFont(getIdFont()); Dimension textSize = SwingUtils.getTextSize(id, g.getFontMetrics()); g.drawString(id, getDrawPosition().x - textSize.width/2 , getDrawPosition().y + 10 ); } } g.setFont(getLabelFont()); g.drawString(text, labelPosition.x, labelPosition.y); } }