123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630 |
- package ui.view;
- import java.awt.BasicStroke;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Image;
- import java.awt.Rectangle;
- import java.awt.RenderingHints;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.awt.event.MouseMotionListener;
- import java.awt.geom.Line2D;
- import java.io.File;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.LinkedList;
- import java.util.Timer;
- import java.util.TimerTask;
- import javax.swing.ImageIcon;
- import javax.swing.JMenuItem;
- import javax.swing.JPanel;
- import javax.swing.JPopupMenu;
- import javax.swing.JToolTip;
- import classes.CpsEdge;
- import classes.CpsNode;
- import classes.CpsObject;
- import classes.HolonElement;
- import classes.HolonObject;
- import classes.HolonSwitch;
- import classes.HolonTransformer;
- import ui.controller.Control;
- import ui.model.Model;
- import ui.model.idCounter;
- public class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- private Image img = null; // Contains the image to draw on MyCanvas
- private int x = 0;
- private int y = 0;
- // edge Object Start Point
- private Model model;
- private final Control controller;
- Graphics2D g2; // For Painting
- private int cx, cy;
- private int sx, sy; // Mark Coords
- ArrayList<HolonElement> dataSelected = new ArrayList<HolonElement>();
- ArrayList<CpsObject> TempSelected = new ArrayList<CpsObject>();
- private boolean dragging = false; // for dragging
- private boolean drawEdge = false; // for drawing edges
- private boolean click = false; // for double click
- private boolean doMark = false; // for double click
- public CpsObject tempCps = null;
- private Rectangle selectRect = new Rectangle();
- private CpsEdge edgeHighlight = null;
- // PopUpMenu
- private JPopupMenu popmenu = new JPopupMenu();
- private JMenuItem itemDelete = new JMenuItem("Delete Object");
- private JToolTip objectTT = new JToolTip();
- // contains the value of the Capacity for new created Edges
- private float edgeCapacity;
- public MyCanvas(final Model model, Control control) {
- this.add(objectTT);
- this.controller = control;
- this.model = model;
- edgeCapacity = 100;
- popmenu.add(itemDelete);
- itemDelete.setEnabled(false);
- itemDelete.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- // Remove the selected Object object
- controller.delCanvasObject(tempCps);
- tempCps = null;
- selectRect.setRect(0, 0, 0, 0);
- repaint();
- }
- });
- this.addMouseListener(this);
- this.addMouseMotionListener(this);
- }
- /**
- * Paints all Components on the Canvas
- *
- * @param Graphics
- *
- */
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- // Rendering
- g2 = (Graphics2D) g;
- RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
- g2.setRenderingHints(rh);
- // drawEdges
- // g2.setColor(Color.BLACK);
- if (drawEdge) {
- g2.setColor(Color.BLACK);
- g2.setStroke(new BasicStroke(2));
- g2.drawLine(tempCps.getPosition().x + controller.getScaleDiv2(),
- tempCps.getPosition().y + controller.getScaleDiv2(), x, y);
- }
- for (CpsEdge con : model.getEdgesOnCanvas()) {
- if (con.getA().getID() != model.getSelectedObjectID() && con.getB().getID() != model.getSelectedObjectID()
- && con != edgeHighlight) {
- if (con.getFlow() <= con.getCapacity()) {
- g2.setColor(Color.GREEN);
- g2.setStroke(new BasicStroke(Math.min((con.getFlow() / con.getCapacity() * 4), 4)));
- } else {
- g2.setColor(Color.RED);
- g2.setStroke(new BasicStroke(2));
- }
- g2.drawLine(con.getA().getPosition().x + controller.getScaleDiv2(),
- con.getA().getPosition().y + controller.getScaleDiv2(),
- con.getB().getPosition().x + controller.getScaleDiv2(),
- con.getB().getPosition().y + controller.getScaleDiv2());
- g2.drawString(con.getFlow() + "/" + con.getCapacity(),
- (con.getA().getPosition().x + con.getB().getPosition().x) / 2 + controller.getScaleDiv2(),
- (con.getA().getPosition().y + con.getB().getPosition().y) / 2 + controller.getScaleDiv2());
- }
- }
- // Highlighted Edge
- if (model.getSelectedObjectID() > 0) {
- g2.setColor(Color.BLUE);
- for (CpsEdge con : model.getEdgesOnCanvas()) {
- if (con.getFlow() <= con.getCapacity()) {
- g2.setStroke(new BasicStroke(Math.min((con.getFlow() / con.getCapacity() * 4), 4)));
- } else {
- g2.setStroke(new BasicStroke(2));
- }
- if (con.getA().getID() == model.getSelectedObjectID()
- || con.getB().getID() == model.getSelectedObjectID() && con != edgeHighlight) {
- g2.drawLine(con.getA().getPosition().x + controller.getScaleDiv2(),
- con.getA().getPosition().y + controller.getScaleDiv2(),
- con.getB().getPosition().x + controller.getScaleDiv2(),
- con.getB().getPosition().y + controller.getScaleDiv2());
- g2.drawString(con.getFlow() + "/" + con.getCapacity(),
- (con.getA().getPosition().x + con.getB().getPosition().x) / 2 + controller.getScaleDiv2(),
- (con.getA().getPosition().y + con.getB().getPosition().y) / 2 + controller.getScaleDiv2());
- }
- }
- } else if (edgeHighlight != null) {
- g2.setColor(Color.BLUE);
- g2.setStroke(new BasicStroke(2));
- g2.drawLine(edgeHighlight.getA().getPosition().x + controller.getScaleDiv2(),
- edgeHighlight.getA().getPosition().y + controller.getScaleDiv2(),
- edgeHighlight.getB().getPosition().x + controller.getScaleDiv2(),
- edgeHighlight.getB().getPosition().y + controller.getScaleDiv2());
- g2.drawString(edgeHighlight.getFlow() + "/" + edgeHighlight.getCapacity(),
- (edgeHighlight.getA().getPosition().x + edgeHighlight.getB().getPosition().x) / 2
- + controller.getScaleDiv2(),
- (edgeHighlight.getA().getPosition().y + edgeHighlight.getB().getPosition().y) / 2
- + controller.getScaleDiv2());
- }
- // Objects
- for (CpsObject cps : model.getObjectsOnCanvas()) {
- if (cps.getID() == model.getSelectedObjectID() && controller.searchByID(model.getSelectedObjectID()) != null
- && controller.searchByID(model.getSelectedObjectID()) instanceof CpsNode) {
- img = new ImageIcon(this.getClass().getResource("/Images/node_selected.png")).getImage();
- } else {
- if (cps instanceof HolonSwitch) {
- if (((HolonSwitch) cps).getActiveAt()[model.getCurIteration()]) {
- ((HolonSwitch) cps).setState(true);
- } else {
- ((HolonSwitch) cps).setState(false);
- }
- }
- if ((cps == tempCps && model.getSelectedCpsObjects().size() == 0 && TempSelected.size() == 0)
- || model.getSelectedCpsObjects().contains(cps) || TempSelected.contains(cps)) {
- g2.setColor(Color.BLUE);
- g2.fillRect(cps.getPosition().x - (controller.getScale() / 20),
- cps.getPosition().y - (controller.getScale() / 20),
- controller.getScale() + ((controller.getScale() / 20) * 2),
- controller.getScale() + ((controller.getScale() / 20) * 2));
- } else if (cps instanceof HolonObject) {
- if (((HolonObject) cps).getState() == 4) {
- g2.setColor(Color.YELLOW);
- }
- if (((HolonObject) cps).getState() == 3) {
- g2.setColor(Color.lightGray);
- }
- if (((HolonObject) cps).getState() == 2) {
- g2.setColor(Color.GREEN);
- }
- if (((HolonObject) cps).getState() == 1) {
- g2.setColor(Color.ORANGE);
- }
- if (((HolonObject) cps).getState() == 0) {
- g2.setColor(Color.WHITE);
- }
- g2.fillRect(cps.getPosition().x - (controller.getScale() / 20),
- cps.getPosition().y - (controller.getScale() / 20),
- controller.getScale() + ((controller.getScale() / 20) * 2),
- controller.getScale() + ((controller.getScale() / 20) * 2));
- }
- File checkPath = new File(cps.getImage());
- if (checkPath.exists()) {
- img = new ImageIcon(cps.getImage()).getImage();
- } else {
- img = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage();
- }
- }
- g2.drawImage(img, cps.getPosition().x, cps.getPosition().y, controller.getScale(), controller.getScale(),
- null);
- }
- // Dragg Highlighting
- if (doMark) {
- g2.setColor(Color.BLACK);
- g2.setStroke(new BasicStroke(1));
- if (sx > x && sy > y) {
- g2.drawRect(x, y, sx - x, sy - y);
- } else if (sx < x && sy < y) {
- g2.drawRect(sx, sy, x - sx, y - sy);
- } else if (sx >= x) {
- g2.drawRect(x, sy, sx - x, y - sy);
- } else if (sy >= y) {
- g2.drawRect(sx, y, x - sx, sy - y);
- }
- }
- }
- @Override
- public void mouseClicked(MouseEvent e) {
- // clear SelectedObjects
- if (!e.isControlDown() && dragging == false) {
- model.getSelectedCpsObjects().clear();
- TempSelected.clear();
- }
- }
- @Override
- public void mouseEntered(MouseEvent e) {
- }
- @Override
- public void mouseExited(MouseEvent e) {
- }
- @Override
- public void mousePressed(MouseEvent e) {
- tempCps = null;
- dataSelected = null;
- edgeHighlight = null;
- controller.setSelecteEdge(null);
- // Object Selection
- for (CpsObject cps : model.getObjectsOnCanvas()) {
- cx = cps.getPosition().x;
- cy = cps.getPosition().y;
- if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
- tempCps = cps;
- dragging = true;
- // 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;
- }
- }
- }
- // Edge Selection
- if (tempCps == null) {
- edgeHighlight = mousePositionOnEdge(x, y);
- controller.setSelecteEdge(edgeHighlight);
- }
- if (edgeHighlight == null && tempCps == null) {
- sx = e.getX();
- sy = e.getY();
- doMark = true;
- }
- // Object Selection Highlighting (selectRect)
- objectSelectionHighlighting();
- repaint();
- }
- @Override
- public void mouseReleased(MouseEvent e) {
- if (drawEdge) {
- drawEdge = false;
- drawDeleteEdge();
- }
- // if Dragged reposition the Object
- if (dragging) {
- x = e.getX();
- y = e.getY();
- dragging = false;
- }
- // Rightclick List
- if (e.getButton() == MouseEvent.BUTTON3) {
- if (e.getButton() == MouseEvent.BUTTON3 && tempCps != null) {
- itemDelete.setEnabled(true);
- } else {
- itemDelete.setEnabled(false);
- }
- popmenu.show(e.getComponent(), e.getX(), e.getY());
- }
- if (doMark) {
- doMark = false;
- for (CpsObject cps : TempSelected) {
- if (!model.getSelectedCpsObjects().contains(cps)) {
- controller.addSelectedObject(cps);
- }
- }
- }
- controller.calculateStateForTimeStep(model.getCurIteration());
- repaint();
- }
- @Override
- public void mouseDragged(MouseEvent e) {
- // If Edge is drawn
- x = e.getX();
- y = e.getY();
- if (!model.getSelectedCpsObjects().contains(tempCps)) {
- model.getSelectedCpsObjects().clear();
- TempSelected.clear();
- }
- if (dragging) {
- try {
- float xDist, yDist; //Distance
-
- x = e.getX() - controller.getScaleDiv2();
- y = e.getY() - controller.getScaleDiv2();
- //Make sure its in bounds
- if (e.getX() < controller.getScaleDiv2())
- x = 0;
- else if (e.getX() > this.getWidth() - controller.getScaleDiv2())
- x = this.getWidth() - controller.getScale();
- if (e.getY() < controller.getScaleDiv2())
- y = 0;
- else if (e.getY() > this.getHeight() - controller.getScaleDiv2())
- y = this.getHeight() - controller.getScale();
- //Distance
- xDist = x-tempCps.getPosition().x;
- yDist = y-tempCps.getPosition().y;
-
- tempCps.setPosition(x, y); // Drag Position
- selectRect.setLocation(x - (controller.getScale() / 20), y - (controller.getScale() / 20)); // Highlighting-Position
-
- // TipText Position and name
- objectTT.setTipText(tempCps.getName());
- objectTT.setLocation(x, y + controller.getScale());
-
- //All Selected Objects
- for (CpsObject cps : model.getSelectedCpsObjects()) {
- if (cps != tempCps) {
- x = (int) (cps.getPosition().x+xDist);
- y = (int) (cps.getPosition().y+yDist);
- //Make sure its in bounds
- if (x < 0)
- x = 0;
- else if (x > this.getWidth())
- x = this.getWidth() - controller.getScale();
- if (y < controller.getScaleDiv2())
- y = 0;
- else if (y > this.getHeight())
- y = this.getHeight() - controller.getScale();
-
- cps.setPosition(x, y);
- }
- }
- repaint();
- } catch (Exception e2) {
- }
- }
- // Mark Objects
- TempSelected.clear();
- if (doMark)
- {
- for (CpsObject cps : model.getObjectsOnCanvas()) {
- 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().x && y1 <= cps.getPosition().y && x2 >= cps.getPosition().x
- && y2 >= cps.getPosition().y) {
- TempSelected.add(cps);
- }
- }
- }
- repaint();
- }
- @Override
- public void mouseMoved(MouseEvent e) {
- x = e.getX();
- y = e.getY();
- // Everytghing for the tooltip :)
- boolean on = false;
- for (CpsObject cps : model.getObjectsOnCanvas()) {
- cx = cps.getPosition().x;
- cy = cps.getPosition().y;
- if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
- objectTT.setTipText(cps.getName());
- objectTT.setLocation(cx, cy + controller.getScale());
- on = true;
- }
- }
- if (!on) {
- objectTT.setLocation(-200, -200);
- objectTT.setTipText("");
- }
- }
- /**
- * Sets the Highlighting of the Selected Object
- */
- public void objectSelectionHighlighting() {
- if (tempCps != null) {
- selectRect.setBounds(tempCps.getPosition().x - (controller.getScale() / 20),
- tempCps.getPosition().y - (controller.getScale() / 20),
- controller.getScale() + ((controller.getScale() / 20) * 2),
- controller.getScale() + ((controller.getScale() / 20) * 2));
- controller.setSelectedObjectID(tempCps.getID());
- } else {
- controller.setSelectedObjectID(0);
- selectRect.setRect(0, 0, 0, 0);
- }
- }
- /**
- * Draws or Deletes an Edge
- */
- private void drawDeleteEdge() {
- boolean node = true;
- boolean newEdge = true;
- boolean onEdge = true;
- boolean deleteNode = false;
- CpsEdge e = null;
- CpsObject tempCPS = null;
- for (CpsObject cps : model.getObjectsOnCanvas()) {
- cx = cps.getPosition().x;
- cy = cps.getPosition().y;
- if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy
- && cps != tempCps) {
- node = false;
- onEdge = false;
- for (CpsEdge p : tempCps.getConnections()) {
- if ((p.getA() == tempCps && p.getB() == cps) || (p.getB() == tempCps && p.getA() == cps)) {
- newEdge = false;
- e = p;
- }
- }
- if (!newEdge) {
- controller.removeEdgesOnCanvas(e);
- // Node ohne Edge?
- if (e.getA().getClass() == CpsNode.class && e.getA().getConnections().isEmpty()) {
- tempCps = e.getA();
- deleteNode = true;
- }
- if (e.getB().getClass() == CpsNode.class && e.getB().getConnections().isEmpty()) {
- tempCPS = e.getB();
- deleteNode = true;
- }
- }
- if (newEdge) {
- e = new CpsEdge(cps, tempCps, edgeCapacity);
- controller.AddEdgeOnCanvas(e);
- }
- }
- }
- // Edge auf eine Edge gezogen?
- if (onEdge) {
- CpsEdge p = mousePositionOnEdge(x, y);
- if (p != null) {
- CpsEdge e1 = null;
- CpsEdge e2 = null;
- node = false;
- CpsNode n = new CpsNode("Node");
- n.setID(idCounter.nextId());
- n.setPosition(x - model.getScaleDiv2(), y - model.getScaleDiv2());
- controller.addObjectCanvas(n);
- CpsObject r, k;
- r = p.getA();
- k = p.getB();
- e = new CpsEdge(n, tempCps, edgeCapacity);
- e1 = new CpsEdge(n, r, edgeCapacity);
- e2 = new CpsEdge(n, k, edgeCapacity);
- controller.removeEdgesOnCanvas(p);
- controller.AddEdgeOnCanvas(e);
- controller.AddEdgeOnCanvas(e1);
- controller.AddEdgeOnCanvas(e2);
- }
- }
- // ins leere Gedragged
- if (node) {
- CpsNode n = new CpsNode("Node");
- n.setID(idCounter.nextId());
- n.setPosition(x - model.getScaleDiv2(), y - model.getScaleDiv2());
- controller.addObjectCanvas(n);
- e = new CpsEdge(n, tempCps, edgeCapacity);
- controller.AddEdgeOnCanvas(e);
- System.out.println("node ID: " + n.getID());
- }
- // Wenn ein Node ohne Connections da ist
- if (deleteNode) {
- controller.delCanvasObject(tempCps);
- controller.delCanvasObject(tempCPS);
- tempCPS = null;
- tempCps = null;
- objectSelectionHighlighting();
- }
- }
- /**
- * Checks if the mouse is on an Edge
- *
- * @param x
- * Position of the Mouse
- * @param y
- * Position of the Mouse
- *
- * @return CpsEdge the Mouse is on, null if the mouse is not on an Edge
- */
- public CpsEdge mousePositionOnEdge(int x, int y) {
- int lx, ly, hx, hy;
- for (CpsEdge p : model.getEdgesOnCanvas()) {
- Line2D l = new Line2D.Float(p.getA().getPosition().x, p.getA().getPosition().y, p.getB().getPosition().x,
- p.getB().getPosition().y);
- if (p.getA().getPosition().x > p.getB().getPosition().x) {
- hx = p.getA().getPosition().x + model.getScaleDiv2() + 7;
- lx = p.getB().getPosition().x + model.getScaleDiv2() - 7;
- } else {
- lx = p.getA().getPosition().x + model.getScaleDiv2() - 7;
- hx = p.getB().getPosition().x + model.getScaleDiv2() + 7;
- }
- if (p.getA().getPosition().y > p.getB().getPosition().y) {
- hy = p.getA().getPosition().y + model.getScaleDiv2() + 7;
- ly = p.getB().getPosition().y + model.getScaleDiv2() - 7;
- } else {
- ly = p.getA().getPosition().y + model.getScaleDiv2() - 7;
- hy = p.getB().getPosition().y + model.getScaleDiv2() + 7;
- }
- // distance from a point to a line and between both Objects
- if (l.ptLineDistSq(x - model.getScaleDiv2(), y - model.getScaleDiv2()) < 20 && x > lx && x < hx && y > ly
- && y < hy) {
- return p;
- }
- }
- return null;
- }
- /**
- * Checks if a double click was made
- *
- * @return true if doublecklick, false if not
- */
- private boolean doubleClick() {
- if (click) {
- click = false;
- return true;
- } else {
- click = true;
- Timer t = new Timer("doubleclickTimer", false);
- t.schedule(new TimerTask() {
- @Override
- public void run() {
- click = false;
- }
- }, 500);
- }
- return false;
- }
- public void setEdgeCapacity(float cap) {
- edgeCapacity = cap;
- }
- }
|