package art.servers.colorsserver.M.gui.diagrams; import art.library.gui.FlatGUI; import art.library.gui.flat.FlatPopupMenu; import art.library.gui.flat.FlatButton; import art.library.model.devices.colors.controller.M.M_ControllerConfiguration; import art.library.model.devices.colors.controller.M.M_SubcontrollerConfiguration; import art.library.model.devices.colors.controller.M.configuration.ALUVISA_Link; import art.library.model.devices.colors.controller.M.configuration.ALUVISA_Phase; import art.library.model.devices.colors.controller.M.configuration.ALUVISA_Plan; import art.library.gui.flat.FlatMenuItem; import art.servers.Shared; import java.awt.AlphaComposite; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Point; import java.awt.RenderingHints; import java.awt.Stroke; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionAdapter; import java.awt.geom.Ellipse2D; import java.awt.geom.GeneralPath; import java.awt.geom.Line2D; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; import java.util.ArrayList; import javax.swing.SwingUtilities; public class DiagramPlan extends FlatButton { private M_ControllerConfiguration m_controller = null; private M_SubcontrollerConfiguration m_subcontroller = null; private ALUVISA_Plan plan = null; private boolean editable = false; private FlatPopupMenu popupMenu = null; private DiagramPlanListener listener = null; private boolean isStructure = false; private DiagramALUVISAPhases diagramPhases = null; private ArrayList objetosGraficos = new ArrayList(); private double scale = 1.0; private double translatex = 0.0; private double translatey = 0.0; private Point mousePressed; private Point mouseDragged; private ObjetoGrafico objetoGraficoSeleccionado = null; public DiagramPlan(M_ControllerConfiguration m_controller, M_SubcontrollerConfiguration m_subcontroller, ALUVISA_Plan plan, boolean isStructure) { this.m_controller = m_controller; this.m_subcontroller = m_subcontroller; this.plan = plan; this.isStructure = isStructure; reload(); initialise(); } public DiagramPlan(M_ControllerConfiguration m_controller, M_SubcontrollerConfiguration m_subcontroller, ALUVISA_Plan plan, DiagramALUVISAPhases diagramPhases, boolean isStructure) { this.m_controller = m_controller; this.m_subcontroller = m_subcontroller; this.plan = plan; this.isStructure = isStructure; this.diagramPhases = diagramPhases; reload(); initialise(); } public void timer(int phase, int time) { if (diagramPhases != null) { diagramPhases.timer(phase, time); } } public void editable(boolean value) { this.editable = value; } public void setDiagramPlanListener(DiagramPlanListener listener) { this.listener = listener; } private void initialise() { this.popupMenu = new FlatPopupMenu(); FlatMenuItem item = new FlatMenuItem(); item.setText(Shared.getMessage("Delete")); item.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { ObjectoGraficoConector selected = (ObjectoGraficoConector)objetoGraficoSeleccionado; for (ALUVISA_Link link : plan.lLink) { if (link == selected.link) { m_subcontroller.removeStructureLink(plan, link); reload(); return; } } } catch (Exception exception) { } } }); popupMenu.add(item); this.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { if ((e.getKeyCode() == 8) || (e.getKeyCode() == 127)) { if (editable == true) { try { ObjectoGraficoConector selected = (ObjectoGraficoConector)objetoGraficoSeleccionado; for (ALUVISA_Link link : plan.lLink) { if (link == selected.link) { m_subcontroller.removeStructureLink(plan, link); reload(); } } } catch (Exception exception) { } } } } }); addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { objetoGraficoSeleccionado = null; for (ObjetoGrafico objetoGrafico : objetosGraficos) { mousePressed = e.getPoint(); objetoGrafico.mousePressed(mousePressed); repaint(); } // Popup Menu if (SwingUtilities.isRightMouseButton(e)) { if (editable == true) { if (objetoGraficoSeleccionado instanceof ObjectoGraficoConector) { popupMenu.show(DiagramPlan.this, e.getX(), e.getY()); } } } if (diagramPhases != null) { if (objetoGraficoSeleccionado != null) { if (objetoGraficoSeleccionado instanceof ObjetoGraficoFase) { diagramPhases.selectPhase(((ObjetoGraficoFase)objetoGraficoSeleccionado).phase); } else if (objetoGraficoSeleccionado instanceof ObjectoGraficoConector) { if (diagramPhases != null) diagramPhases.selectPhase(((ObjectoGraficoConector)objetoGraficoSeleccionado).link.lTransition); } } else { diagramPhases.selectPhase(); } } if (listener != null) { if (objetoGraficoSeleccionado != null) { if (objetoGraficoSeleccionado instanceof ObjetoGraficoFase) { listener.selected(((ObjetoGraficoFase)objetoGraficoSeleccionado).phase); } else if (objetoGraficoSeleccionado instanceof ObjectoGraficoConector) { listener.selected(((ObjectoGraficoConector)objetoGraficoSeleccionado).link); } } else { listener.selected(); } } } public void mouseReleased(MouseEvent e) { if (mouseDragged != null) { // Mirar nueva transicion ALUVISA_Phase origen = null; ALUVISA_Phase destino = null; for (ObjetoGrafico objetoGrafico : objetosGraficos) { if ((objetoGrafico.mousePressed == true) && (objetoGrafico instanceof ObjetoGraficoFase)) origen = ((ObjetoGraficoFase)objetoGrafico).phase; if (objetoGrafico.mouseDragged == true && (objetoGrafico instanceof ObjetoGraficoFase)) destino = ((ObjetoGraficoFase)objetoGrafico).phase; objetoGrafico.deseleccionar(); } if ((origen != null) && (destino != null)) { ALUVISA_Link link = plan.getLink(origen, destino); if (link == null) { if (origen != destino) { link = new ALUVISA_Link(); link.source = origen.number; link.destination = destino.number; m_subcontroller.addStructureLink(plan, link); } } else { plan.lLink.remove(link); } } objetoGraficoSeleccionado = null; listener.selected(); reload(); } mouseDragged = null; repaint(); } }); addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent e) { if (editable == false) return; for (ObjetoGrafico objetoGrafico : objetosGraficos) { mouseDragged = e.getPoint(); objetoGrafico.mouseDragged(mouseDragged); repaint(); } } }); } private ObjetoGraficoFase obtenerFaseGrafica(int number) { for (Object objeto : objetosGraficos) { if (objeto instanceof ObjetoGraficoFase) { ObjetoGraficoFase objetoGraficoFase = (ObjetoGraficoFase)objeto; if (objetoGraficoFase.phase.number == number) return objetoGraficoFase; } } return null; } public void reload(ALUVISA_Plan plan) { this.plan = plan; reload(); } public void reload() { objetosGraficos.clear(); objetoGraficoSeleccionado = null; for (int i=0; i centroSalida.getX()) { double x1 = centroSalida.getX() + dimensionSalida.getX()/2; double y1 = centroSalida.getY(); double x2 = centroLlegada.getX() - dimensionLlegada.getX()/2; double y2 = centroLlegada.getY(); y1 = y1 - (distanciaVerticalEntreLineas/2); y2 = y2 - (distanciaVerticalEntreLineas/2); rectangle = new Rectangle2D.Double(x1, y1 - 8, Math.abs(x2 - x1), y2 - y1 + 16); Line2D.Double linea = new Line2D.Double(x1,y1,x2-2.5,y2); GeneralPath triangulo = new GeneralPath(); triangulo.moveTo(x2-10, y2-5); triangulo.lineTo(x2, y2); triangulo.lineTo(x2-10, y2+5); triangulo.closePath(); g2.setStroke(stroke2); g2.draw(linea); g2.setStroke(stroke1); g2.fill(triangulo); g2.setFont(new Font("Arial",Font.BOLD|Font.ITALIC,12)); g2.setColor(Color.GRAY); textoIzquierdaDerecha(g2, getLinkText1(), rectangle.getMinX(), rectangle.getCenterY()); g2.setFont(new Font("Arial",Font.BOLD,12)); g2.setColor(color); textoIzquierdaDerecha(g2, getLinkText2(), rectangle.getMinX() + 25, rectangle.getCenterY()); } else { double x1 = centroSalida.getX() - dimensionSalida.getX()/2; double y1 = centroSalida.getY(); double x2 = centroLlegada.getX() + dimensionLlegada.getX()/2; double y2 = centroLlegada.getY(); y1 = y1 + (distanciaVerticalEntreLineas/2); y2 = y2 + (distanciaVerticalEntreLineas/2); rectangle = new Rectangle2D.Double(x2, y1 - 8, Math.abs(x2 - x1), y2 - y1 + 16); Line2D.Double linea = new Line2D.Double(x1,y1,x2+2.5,y2); GeneralPath triangulo = new GeneralPath(); triangulo.moveTo(x2+10, y2+5); triangulo.lineTo(x2, y2); triangulo.lineTo(x2+10, y2-5); triangulo.closePath(); g2.setStroke(stroke2); g2.draw(linea); g2.setStroke(stroke1); g2.fill(triangulo); g2.setFont(new Font("Arial",Font.BOLD|Font.ITALIC,12)); g2.setColor(Color.GRAY); textoDerechaIzquierda(g2, getLinkText1(), rectangle.getMaxX(), rectangle.getCenterY()); g2.setFont(new Font("Arial",Font.BOLD,12)); g2.setColor(color); textoDerechaIzquierda(g2, getLinkText2(), rectangle.getMaxX() - 25, rectangle.getCenterY()); } } else { double altura = distanciaVerticalEntreLineas; if (centroLlegada.getX() > centroSalida.getX()) { double x1 = centroSalida.getX(); double y1 = centroSalida.getY() - dimensionSalida.getY()/2 - altura; double x2 = centroLlegada.getX(); double y2 = centroLlegada.getY() - dimensionLlegada.getY()/2 - altura; rectangle = new Rectangle2D.Double(x1, y1 - 8, Math.abs(x2 - x1), y2 - y1 + 16); corregirAltura(-distanciaVerticalEntreLineas); y1 = rectangle.getCenterY(); y2 = rectangle.getCenterY(); double minySalida = faseGraficaOrigen.min().getY(); double minyLlegada = faseGraficaDestino.min().getY(); Line2D.Double linea1 = new Line2D.Double(x1,y1,x2,y2); Line2D.Double linea2 = new Line2D.Double(x1,y1,x1,minySalida); Line2D.Double linea3 = new Line2D.Double(x2,minyLlegada-2.5,x2,y2); GeneralPath triangulo = new GeneralPath(); triangulo.moveTo(x2,minyLlegada); triangulo.lineTo(x2+5,minyLlegada-10); triangulo.lineTo(x2-5,minyLlegada-10); triangulo.closePath(); g2.setStroke(stroke3); g2.draw(linea1); g2.setStroke(stroke2); g2.draw(linea2); g2.draw(linea3); g2.setStroke(stroke1); g2.fill(triangulo); g2.setFont(new Font("Arial",Font.BOLD|Font.ITALIC,12)); g2.setColor(Color.GRAY); textoIzquierdaDerecha(g2, getLinkText1(), rectangle.getMinX(), rectangle.getCenterY()); g2.setFont(new Font("Arial",Font.BOLD,12)); g2.setColor(color); textoIzquierdaDerecha(g2, getLinkText2(), rectangle.getMinX() + 25, rectangle.getCenterY()); } else { // X X // ^ | // |______| double x1 = centroSalida.getX(); double y1 = centroSalida.getY() + dimensionSalida.getY()/2 + altura; double x2 = centroLlegada.getX(); double y2 = centroLlegada.getY() + dimensionLlegada.getY()/2 + altura; rectangle = new Rectangle2D.Double(x2, y1 - 8, x1 - x2, y2 - y1 + 16); corregirAltura(distanciaVerticalEntreLineas); y1 = rectangle.getCenterY(); y2 = rectangle.getCenterY(); double maxySalida = faseGraficaOrigen.max().getY(); double maxyLlegada = faseGraficaDestino.max().getY(); Line2D.Double linea1 = new Line2D.Double(x1,y1,x2,y2); Line2D.Double linea2 = new Line2D.Double(x1,y1,x1,maxySalida); Line2D.Double linea3 = new Line2D.Double(x2,maxyLlegada+2.5,x2,y2); GeneralPath triangulo = new GeneralPath(); triangulo.moveTo(x2,maxyLlegada); triangulo.lineTo(x2+5,maxyLlegada+10); triangulo.lineTo(x2-5,maxyLlegada+10); triangulo.closePath(); g2.setStroke(stroke3); g2.draw(linea1); g2.setStroke(stroke2); g2.draw(linea2); g2.draw(linea3); g2.fill(triangulo); g2.setFont(new Font("Arial",Font.BOLD|Font.ITALIC,12)); g2.setColor(Color.GRAY); textoDerechaIzquierda(g2, getLinkText1(), rectangle.getMaxX(), rectangle.getCenterY()); g2.setFont(new Font("Arial",Font.BOLD,12)); g2.setColor(color); textoDerechaIzquierda(g2, getLinkText2(), rectangle.getMaxX() - 25, rectangle.getCenterY()); } } } private void corregirAltura(double dy) { for (ObjetoGrafico objeto : objetosGraficos) { if (objeto instanceof ObjectoGraficoConector) { ObjectoGraficoConector conector = (ObjectoGraficoConector)objeto; if (conector == this) { return; } else { Rectangle2D r2 = objeto.getRectangle(); if (rectangle.intersects(r2) == true) { rectangle.setFrame(rectangle.getX(), rectangle.getY() + dy, rectangle.getWidth(), rectangle.getHeight()); } } } } } private void textoDerechaIzquierda(Graphics2D g2, String texto, double x, double y) { FontMetrics metrics = g2.getFontMetrics(); int textWidth = metrics.stringWidth(texto); int textHeight = metrics.getHeight() - metrics.getDescent(); g2.drawString(texto, (float)x - textWidth - 10, (float)y - textHeight); } private void textoIzquierdaDerecha(Graphics2D g2, String texto, double x, double y) { FontMetrics metrics = g2.getFontMetrics(); int textHeight = metrics.getHeight() - metrics.getDescent(); g2.drawString(texto, (float)x + 10, (float)y - textHeight); } private String getLinkText1() { return "" + plan.getLinkPosition(link); } private String getLinkText2() { String result = ""; for (int i=0; i