123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489 |
- package ui.view.canvas;
- import java.awt.BasicStroke;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.RenderingHints;
- import java.awt.event.MouseEvent;
- import java.io.IOException;
- import java.util.HashSet;
- import classes.AbstractCanvasObject;
- import classes.Edge;
- import classes.GroupNode;
- import classes.HolonObject;
- import classes.HolonSwitch;
- import classes.Node;
- import ui.controller.Control;
- import ui.model.Consumer;
- import ui.model.DecoratedCable;
- import ui.model.DecoratedGroupNode;
- import ui.model.DecoratedSwitch;
- import ui.model.ExitCable;
- import ui.model.Model;
- import ui.model.Passiv;
- import ui.model.Supplier;
- import ui.view.inspector.UnitGraph;
- import utility.ImageImport;
- import utility.Vector2Int;
- //TODO delete GroupNodeCanvas completely and only have canvas Class
- public class GroupNodeCanvas extends Canvas {
- private String parentPath;
- private Component parentComponent;
- public GroupNodeCanvas(Model mod, Control control, UnitGraph unitGraph, GroupNode groupNode, String parentPath,
- Component parentComponent) {
- super(mod, control, unitGraph);
- this.groupNode = groupNode;
- this.parentPath = parentPath;
- this.parentComponent = parentComponent;
- }
- public void setGroupNode(GroupNode upperNode) {
- this.groupNode = upperNode;
- }
- public GroupNode getGroupNode() {
- return groupNode;
- }
- public String getParentPath() {
- return parentPath;
- }
- public Component getParentComponent() {
- return parentComponent;
- }
- public void paintComponent(Graphics g) {
- // super.paintComponent(g);
- Graphics2D g2d = (Graphics2D) g;
- g2d.clearRect(0, 0, this.getWidth(), this.getHeight());
- g2d.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
- // -->Old code
- if (drawEdge) {
- g2d.setColor(Color.BLACK);
- g2d.setStroke(new BasicStroke(1));
- g2d.drawLine(tempCps.getPosition().getX(), tempCps.getPosition().getY(), x, y);
- }
- // <--
- // SelectedCable
- HashSet<Edge> selectedEdges = new HashSet<Edge>();
- for (AbstractCanvasObject aCps : model.getSelectedObjects()) {
- for (Edge edge : aCps.getConnections()) {
- selectedEdges.add(edge);
- }
- }
- if (model.getSelectedEdge() != null)
- selectedEdges.add(model.getSelectedEdge());
- DecoratedGroupNode actualGroupNode = controller.getSimManager().getActualVisualRepresentationalState()
- .getCreatedGroupNodes().get(groupNode);
- // VisualState Representation:
- for (ExitCable cable : actualGroupNode.getExitCableList()) {
- paintExitCable(g2d, cable);
- }
- for (DecoratedCable cable : actualGroupNode.getInternCableList()) {
- paintCable(g2d, cable, selectedEdges.contains(cable.getModel()));
- }
- for (DecoratedGroupNode dGroupNode : actualGroupNode.getGroupNodeList()) {
- paintGroupNode(g2d, dGroupNode);
- }
- for (Consumer con : actualGroupNode.getConsumerList()) {
- paintConsumer(g2d, con);
- }
- for (Supplier sup : actualGroupNode.getSupplierList()) {
- paintSupplier(g2d, sup);
- }
- for (Passiv pas : actualGroupNode.getPassivList()) {
- paintCanvasObject(g2d, pas);
- }
- for (DecoratedSwitch dSwitch : actualGroupNode.getSwitchList()) {
- paintSwitch(g2d, dSwitch);
- }
- for (Node node : actualGroupNode.getNodeList()) {
- drawCanvasObject(g2d, "/Images/node.png", node.getPosition());
- }
- // -->oldCode
- if (doMark) {
- g2d.setColor(Color.BLACK);
- g2d.setStroke(new BasicStroke(0));
- drawMarker(g2d);
- }
- // Test Selection
- // Objects:
- g2d.setColor(Color.BLUE);
- g2d.setStroke(new BasicStroke(1));
- Color transparentGrey = new Color(128, 174, 247, 40);
- for (AbstractCanvasObject aCps : model.getSelectedObjects()) {
- if (aCps instanceof Node) {
- Vector2Int pos = aCps.getPosition();
- g2d.setColor(transparentGrey);
- g2d.fillOval(pos.getX() - (int) (controller.getScaleDiv2()),
- pos.getY() - (int) (controller.getScaleDiv2()), controller.getScale(), controller.getScale());
- g2d.setColor(Color.LIGHT_GRAY);
- g2d.setStroke(new BasicStroke(2));
- g2d.drawOval(pos.getX() - (int) (controller.getScaleDiv2()),
- pos.getY() - (int) (controller.getScaleDiv2()), controller.getScale(), controller.getScale());
- } else {
- Vector2Int pos = aCps.getPosition();
- g2d.setColor(transparentGrey);
- g2d.fillRect(pos.getX() - (int) (controller.getScaleDiv2() * 1.5f),
- pos.getY() - (int) (controller.getScaleDiv2() * 1.5f), (int) (controller.getScale() * 1.5f),
- (int) (controller.getScale() * 1.5f));
- g2d.setColor(Color.LIGHT_GRAY);
- g2d.setStroke(new BasicStroke(2));
- g2d.drawRect(pos.getX() - (int) (controller.getScaleDiv2() * 1.5f),
- pos.getY() - (int) (controller.getScaleDiv2() * 1.5f), (int) (controller.getScale() * 1.5f),
- (int) (controller.getScale() * 1.5f));
- }
- }
- // maybeReplace:
- if (mayBeReplaced != null) {
- g2d.setColor(Color.RED);
- Vector2Int pos = mayBeReplaced.getPosition();
- g.drawImage(ImageImport.loadImage("/Images/replace.png"), pos.getX() + controller.getScaleDiv2(),
- pos.getY() - controller.getScale(), controller.getScaleDiv2(), controller.getScaleDiv2(), null);
- }
- // <-- OldCode
- }
- @Override
- public void mousePressed(MouseEvent e) {
- this.grabFocus();
- if (!disabled) {
- tempCps = null;
- dataSelected = null;
- edgeHighlight = null;
- controller.setSelecteEdge(null);
- controller.setSelectedObjectID(-1);
- // Object Selection
- if (e.getX() > 0) {
- for (AbstractCanvasObject cps : groupNode.getNodes()) {
- cx = cps.getPosition().getX() - model.getScaleDiv2();
- cy = cps.getPosition().getY() - model.getScaleDiv2();
- if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
- tempCps = cps;
- dragging = true;
- if (e.isControlDown() && tempCps != null) {
- System.out.println("Add Remove Selection GROUPNODECANVAS");
- if (model.getSelectedObjects().contains(tempCps)) {
- controller.removeObjectFromSelection(tempCps);
- } else {
- controller.addSelectedObject(tempCps);
- if (tempCps instanceof GroupNode)
- controller.getObjectsInDepth();
- }
- }
- // If drawing an Edge (CTRL down)
- if (tempCps.getClass() == HolonObject.class) {
- HolonObject tempObj = ((HolonObject) tempCps);
- dataSelected = tempObj.getElements();
- }
- if (e.isShiftDown()) {
- drawEdge = true;
- dragging = false;
- }
- break;
- }
- }
- } else {
- // look for objects connected to uppernode
- int count = 0;
- for (Edge ed : groupNode.getConnections()) {
- AbstractCanvasObject cps;
- if (ed.getA().equals(this.groupNode)) {
- cps = ed.getB();
- } else {
- cps = ed.getA();
- }
- if (x - controller.getScale() <= (model.getScaleDiv2())
- && y - controller.getScale() <= (scalediv20 + 5
- + (model.getScale() + scalediv20 + 10) * count)
- && x >= model.getScaleDiv2()
- && y >= (scalediv20 + 5 + (model.getScale() + scalediv20 + 10) * count)) {
- tempCps = cps;
- // If drawing an Edge (CTRL down)
- if (tempCps.getClass() == HolonObject.class) {
- HolonObject tempObj = ((HolonObject) tempCps);
- dataSelected = tempObj.getElements();
- }
- if (e.isShiftDown()) {
- drawEdge = true;
- }
- }
- count++;
- }
- }
- // Edge Selection
- if (e.getButton() == MouseEvent.BUTTON1) {
- if (tempCps == null) {
- edgeHighlight = mousePositionOnEdge(x, y);
- controller.setSelecteEdge(edgeHighlight);
- controller.setSelectedObjectID(0);
- if (!e.isControlDown() && e.getButton() != MouseEvent.BUTTON3) {
- controller.clearSelection();
- }
- }
- if (edgeHighlight == null && tempCps == null) {
- sx = e.getX();
- sy = e.getY();
- doMark = true;
- }
- repaint();
- }
- }
- }
- @Override
- public void mouseReleased(MouseEvent e) {
- if (!disabled) {
- x = e.getX();
- y = e.getY();
- dragging = false;
- if (drawEdge) {
- drawEdge = false;
- drawDeleteEdge();
- }
- if (dragged) {
- try {
- /**
- * Save State before performing NodePlacement, replacement e.g.
- */
- controller.autoSave();
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- /**
- * check if tempCps could replace an Object on the UpperNodeanvas
- */
- if (model.getSelectedObjects().size() == 1 && checkForReplacement(groupNode.getNodes(), tempCps,
- tempCps.getPosition().getX(), tempCps.getPosition().getY())) {
- /**
- * if UpperNode would be replaced, close its tabs
- */
- if (mayBeReplaced instanceof GroupNode)
- closeUpperNodeTab(mayBeReplaced.getId());
- /**
- * replace on canvas
- */
- controller.replaceObjUpperNode(mayBeReplaced, tempCps, groupNode);
- mayBeReplaced = null;
- }
- }
- if (!e.isControlDown() && !dragged && tempCps != null && MouseEvent.BUTTON3 != e.getButton()) {
- controller.clearSelection();
- controller.addSelectedObject(tempCps);
- if (tempCps instanceof GroupNode)
- controller.getObjectsInDepth();
- }
- dragged = false;
- // Rightclick List
- setRightClickMenu(e);
- markObjects();
- boolean doubleclick = doubleClick();
- if (doubleclick && tempCps != null && tempCps instanceof HolonSwitch) {
- ((HolonSwitch) tempCps).switchState();
- }
- if (doubleclick && tempCps != null && tempCps instanceof GroupNode) {
- controller.getGui().openNewUpperNodeTab((GroupNode)tempCps);
- }
- controller.calculateStateAndVisualForTimeStep(model.getCurrentIteration());
- repaint();
- }
- }
- @Override
- public void mouseDragged(MouseEvent e) {
- if (!disabled) {
- // If Edge is drawn
- x = e.getX();
- y = e.getY();
- if (!model.getSelectedObjects().contains(tempCps) && !doMark) {
- controller.clearSelection();
- if (tempCps != null) {
- controller.addSelectedObject(tempCps);
- }
- }
- if (dragging) {
- try {
- // tempCps in the upperNode? else its a connected Object from
- // outside
- if (groupNode.getNodes().contains(tempCps)) {
- dragged = true;
- float xDist, yDist; // Distance
- x = e.getX();
- y = e.getY();
- // Make sure its in bounds
- if (e.getX() < controller.getScaleDiv2() + + 5)
- x = controller.getScaleDiv2() + + 5;
- else if (e.getX() > this.getWidth() - controller.getScaleDiv2())
- x = this.getWidth() - controller.getScaleDiv2();
- if (e.getY() < controller.getScaleDiv2())
- y = controller.getScaleDiv2();
- else if (e.getY() > this.getHeight() - controller.getScaleDiv2())
- y = this.getHeight() - controller.getScaleDiv2();
- // Distance
- xDist = x - tempCps.getPosition().getX();
- yDist = y - tempCps.getPosition().getY();
- tempCps.setPosition(x, y); // Drag Position
- // TipText Position and name
- toolTip = true;
- toolTipText = tempCps.getName() + ", " + tempCps.getId();
- toolTipPos.setX(tempCps.getPosition().getX() - model.getScaleDiv2());
- toolTipPos.setY(tempCps.getPosition().getY() - model.getScaleDiv2());
- // All Selected Objects
- for (AbstractCanvasObject cps : model.getSelectedObjects()) {
- if (cps != tempCps) {
- x = (int) (cps.getPosition().getX() + xDist);
- y = (int) (cps.getPosition().getY() + yDist);
- // Make sure its in bounds
- if (x < + 5 + controller.getScaleDiv2())
- x = controller.getScaleDiv2() + + 5;
- else if (x > this.getWidth() - controller.getScaleDiv2())
- x = this.getWidth() - controller.getScaleDiv2();
- if (y <= controller.getScaleDiv2())
- y = controller.getScaleDiv2();
- else if (y > this.getHeight() - controller.getScaleDiv2())
- y = this.getHeight() - controller.getScaleDiv2();
- cps.setPosition(x, y);
- }
- }
- }
- /**
- * check if something would be replaced
- */
- if (model.getSelectedObjects().size() == 1)
- checkForReplacement(groupNode.getNodes(), tempCps, x, y);
- repaint();
- } catch (Exception eex) {
- }
- }
- // Mark Objects
- if (doMark) {
- tempSelected.clear();
- for (AbstractCanvasObject cps : groupNode.getNodes()) {
- int x1 = sx, x2 = x, y1 = sy, y2 = y;
- if (sx >= x) {
- x1 = x;
- x2 = sx;
- }
- if (sy >= y) {
- y1 = y;
- y2 = sy;
- }
- if (x1 <= cps.getPosition().getX() + model.getScaleDiv2()
- && y1 <= cps.getPosition().getY() + model.getScaleDiv2() && x2 >= cps.getPosition().getX()
- && y2 >= cps.getPosition().getY()) {
- tempSelected.add(cps);
- }
- }
- int count = 0;
- for (Edge ed : groupNode.getConnections()) {
- AbstractCanvasObject cps;
- if (ed.getA().equals(groupNode)) {
- cps = ed.getB();
- } else {
- cps = ed.getA();
- }
- int x1 = sx, x2 = x, y1 = sy, y2 = y;
- if (sx >= x) {
- x1 = x;
- x2 = sx;
- }
- if (sy >= y) {
- y1 = y;
- y2 = sy;
- }
- if (x1 <= 0
- && y1 <= (int) (5 + (model.getScale() + scalediv20 + 10) * count) + model.getScaleDiv2()
- && x2 >= 0
- && y2 >= (int) (5 + (model.getScale() + scalediv20 + 10) * count)) {
- tempSelected.add(cps);
- }
- count++;
- }
- }
- repaint();
- }
- }
- @Override
- public void mouseMoved(MouseEvent e) {
- {
- x = e.getX();
- y = e.getY();
- // Everything for the tooltip :)
- boolean on = false;
- for (AbstractCanvasObject cps : groupNode.getNodes()) {
- cx = cps.getPosition().getX() - controller.getScaleDiv2();
- cy = cps.getPosition().getY() - controller.getScaleDiv2();
- on = setToolTipInfoAndPosition(on, cps);
- }
- int count = 0;
- for (Edge ed : groupNode.getConnections()) {
- AbstractCanvasObject cps;
- if (ed.getA().equals(this.groupNode)) {
- cps = ed.getB();
- } else {
- cps = ed.getA();
- }
- cx = 0 ;
- cy = (int) (scalediv20 + 5 + (50 + scalediv20 + 10) * count);
- if (x - 50 <= cx && y - 50 <= cy && x >= cx && y >= cy) {
- on = true;
- toolTipPos.setX(cx - 25);
- toolTipPos.setY(cy + 50);
- toolTipText = cps.getName() + ", " + cps.getId();
- }
- count++;
- }
- toolTip = on;
- repaint();
- }
- }
- }
|