123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 |
- package ui.view;
- import java.awt.BasicStroke;
- import java.awt.Color;
- import java.awt.Container;
- import java.awt.Dimension;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Image;
- import java.awt.Paint;
- 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.util.ArrayList;
- import java.util.LinkedList;
- import java.util.Timer;
- import java.util.TimerTask;
- import javax.swing.AbstractButton;
- import javax.swing.ImageIcon;
- import javax.swing.JComponent;
- import javax.swing.JLabel;
- import javax.swing.JMenuItem;
- import javax.swing.JPanel;
- import javax.swing.JPopupMenu;
- import javax.swing.JToolTip;
- import classes.CpsObject;
- import classes.HolonElement;
- import classes.HolonObject;
- import classes.HolonSwitch;
- <<<<<<< HEAD
- =======
- import classes.HolonTransformer;
- >>>>>>> b21529a76393a3f2ba0825f9a04ab5b2a8e80a33
- import ui.controller.Control;
- import ui.model.*;
- class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
- 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;
- private int cy;
- ArrayList<HolonElement> dataSelected = new ArrayList<HolonElement>();
- private boolean dragging = false; //for dragging
- private boolean drawEdge = false; //for drawing edges
- private boolean click = false; // for double click
- private CpsObject tempCps = null;
- private Rectangle selectRect = new Rectangle();
- // PopUpMenu
- private JPopupMenu popmenu = new JPopupMenu();
- private JMenuItem itemDelete = new JMenuItem("Delete Object");
- private JToolTip objectTT = new JToolTip();
- public MyCanvas(final Model model, Control control) {
- this.add(objectTT);
- this.controller = control;
- this.model = model;
- popmenu.add(itemDelete);
- itemDelete.setEnabled(false);
- itemDelete.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- //Remove the selected Object object
- model.getObjectsOnCanvas().remove(tempCps);
- for (CpsObject cps : model.getObjectsOnCanvas()) {
- cps.getConnectedTo().remove(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);
- // Selection
- if (selectRect != null) {
- g2.setColor(Color.GREEN);
- g2.fillRect((int) selectRect.getX(), (int) selectRect.getY(), (int) selectRect.getWidth(),
- (int) selectRect.getHeight());
- }
- // drawEdges
- g2.setColor(Color.BLACK);
- if (drawEdge)
- <<<<<<< HEAD
- g2.drawLine(tempCps.getPos().x + controller.getScaleDiv2(), tempCps.getPos().y + controller.getScaleDiv2(),
- x, y);
- =======
- g2.drawLine(tempCps.getPosition().x + controller.getScaleDiv2(),
- tempCps.getPosition().y + controller.getScaleDiv2(), x, y);
- >>>>>>> b21529a76393a3f2ba0825f9a04ab5b2a8e80a33
- for (CpsObject cps : model.getObjectsOnCanvas()) {
- for (CpsObject con : cps.getConnectedTo()) {
- if (con.getID() != model.getSelectedObjectID() && cps.getID() != model.getSelectedObjectID())
- <<<<<<< HEAD
- g2.drawLine(cps.getPos().x + controller.getScaleDiv2(), cps.getPos().y + controller.getScaleDiv2(),
- con.getPos().x + controller.getScaleDiv2(), con.getPos().y + controller.getScaleDiv2());
- =======
- g2.drawLine(cps.getPosition().x + controller.getScaleDiv2(),
- cps.getPosition().y + controller.getScaleDiv2(),
- con.getPosition().x + controller.getScaleDiv2(),
- con.getPosition().y + controller.getScaleDiv2());
- >>>>>>> b21529a76393a3f2ba0825f9a04ab5b2a8e80a33
- }
- }
- // Highlighted Edge
- g2.setColor(Color.GREEN);
- for (CpsObject cps : model.getObjectsOnCanvas()) {
- for (CpsObject con : cps.getConnectedTo()) {
- if (con.getID() == model.getSelectedObjectID())
- <<<<<<< HEAD
- g2.drawLine(cps.getPos().x + controller.getScaleDiv2(), cps.getPos().y + controller.getScaleDiv2(),
- con.getPos().x + controller.getScaleDiv2(), con.getPos().y + controller.getScaleDiv2());
- =======
- g2.drawLine(cps.getPosition().x + controller.getScaleDiv2(),
- cps.getPosition().y + controller.getScaleDiv2(),
- con.getPosition().x + controller.getScaleDiv2(),
- con.getPosition().y + controller.getScaleDiv2());
- >>>>>>> b21529a76393a3f2ba0825f9a04ab5b2a8e80a33
- }
- }
- // Objects
- for (CpsObject cps : model.getObjectsOnCanvas()) {
- img = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage();
- g2.drawImage(img, cps.getPosition().x, cps.getPosition().y, controller.getScale(), controller.getScale(),
- null);
- }
- }
- @Override
- public void mouseClicked(MouseEvent e) {
- //If double clicked on a Switch change the Image to on/off
- if (doubleClick() && tempCps != null && tempCps.getClass() == HolonSwitch.class) {
- System.out.println("trans double click");
- if (tempCps.getImage().compareTo("/Images/switch-on.png") == 0) {
- tempCps.setImage("/Images/switch-off.png");
- System.out.println("off");
- } else {
- tempCps.setImage("/Images/switch-on.png");
- System.out.println("on");
- }
- }
- repaint();
- }
- @Override
- public void mouseEntered(MouseEvent e) {
- // TODO Auto-generated method stub
- }
- @Override
- public void mouseExited(MouseEvent e) {
- // TODO Auto-generated method stub
- }
- @Override
- public void mousePressed(MouseEvent e) {
- // TODO Auto-generated method stub
- x = e.getX();
- y = e.getY();
- tempCps = 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;
- //If drawing an Edge (CTRL down)
- if (e.isControlDown())
- <<<<<<< HEAD
- drawEdge = true;
- if (tempCps.getClass() == HolonObject.class) {
- HolonObject tempObj = ((HolonObject) tempCps);
- dataSelected = tempObj.getElements();
- } else {
- dataSelected = null;
- }
- =======
- drawEdge = true;
- >>>>>>> b21529a76393a3f2ba0825f9a04ab5b2a8e80a33
- }
- }
- // 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;
- tempCps.setPosition(e.getX() - controller.getScaleDiv2(), e.getY() - controller.getScaleDiv2());
- }
- // Rightclick List
- if (e.getButton() == e.BUTTON3) {
- if (e.getButton() == e.BUTTON3 && tempCps != null) {
- itemDelete.setEnabled(true);
- } else {
- itemDelete.setEnabled(false);
- }
- popmenu.show(e.getComponent(), e.getX(), e.getY());
- }
- repaint();
- }
- @Override
- public void mouseDragged(MouseEvent e) {
- //If Edge is drawn
- if (drawEdge) {
- x = e.getX();
- y = e.getY();
- repaint();
- } else {
- try {
- //Drag Position
- tempCps.setPosition(e.getX() - controller.getScaleDiv2(), e.getY() - controller.getScaleDiv2());
- dragging = true;
- //Highlighting Position
- selectRect.setLocation(tempCps.getPosition().x - (controller.getScale() / 20),
- tempCps.getPosition().y - (controller.getScale() / 20));
- //TipText Position and name
- objectTT.setTipText(tempCps.getName());
- objectTT.setLocation(tempCps.getPosition().x, tempCps.getPosition().y+controller.getScale());
- repaint();
- } catch (Exception e2) {
- }
- }
- }
- @Override
- public void mouseMoved(MouseEvent e) {
- x = e.getX();
- y = e.getY();
- //Everytghin 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
- */
- private void objectSelectionHighlighting() {
- if (tempCps != null) {
- selectRect.setBounds(tempCps.getPosition().x - (controller.getScale() / 20),
- tempCps.getPosition().y - (controller.getScale() / 20),
- controller.getScale() + controller.getScale() / 10,
- controller.getScale() + controller.getScale() / 10);
- controller.setSelectedObjectID(tempCps.getID());
- } else {
- controller.setSelectedObjectID(0);
- selectRect.setRect(0, 0, 0, 0);
- }
- }
- /**
- * Draws or Deletes an Edge
- */
- private void drawDeleteEdge() {
- 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) {
- if (!cps.getConnectedTo().contains(tempCps)) {
- cps.AddConnection(tempCps);
- tempCps.AddConnection(cps);
- } else {
- cps.getConnectedTo().remove(tempCps);
- tempCps.getConnectedTo().remove(cps);
- }
- }
- }
- }
-
- /**
- * 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;
- }
- }
|