12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127 |
- 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.Point;
- import java.awt.RenderingHints;
- import java.awt.datatransfer.UnsupportedFlavorException;
- 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.Timer;
- import java.util.TimerTask;
- import javax.swing.ImageIcon;
- import javax.swing.JLabel;
- import javax.swing.JMenuItem;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JPopupMenu;
- import javax.swing.JScrollPane;
- import javax.swing.JTabbedPane;
- import com.google.gson.JsonParseException;
- import classes.CpsEdge;
- import classes.CpsNode;
- import classes.CpsUpperNode;
- import classes.AbstractCpsObject;
- import classes.HolonElement;
- import classes.HolonObject;
- import classes.HolonSwitch;
- import classes.Position;
- import classes.SubNet;
- import ui.controller.Control;
- import ui.controller.UpdateController;
- import ui.model.Model;
- public class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
- private static final long serialVersionUID = 1L;
- private Image img = null;
- private int x = 0;
- private int y = 0;
-
- private Model model;
- private final Control controller;
- Graphics2D g2;
- private int cx, cy;
- private int sx, sy;
- private float scalediv20;
- private Position unPos;
- private ArrayList<Position> savePos;
- private UpdateController updCon;
- ArrayList<HolonElement> dataSelected = new ArrayList<HolonElement>();
- ArrayList<AbstractCpsObject> tempSelected = new ArrayList<AbstractCpsObject>();
- private boolean[] showedInformation = new boolean[4];
- private boolean dragging = false;
- private boolean dragged = false;
- private boolean drawEdge = false;
- public boolean click = false;
- private boolean doMark = false;
- public AbstractCpsObject tempCps = null;
- private CpsEdge edgeHighlight = null;
-
- private JPopupMenu popmenu = new JPopupMenu();
- private JMenuItem itemDelete = new JMenuItem(Languages.getLanguage()[98]);
- private JMenuItem itemCut = new JMenuItem(Languages.getLanguage()[95]);
- private JMenuItem itemCopy = new JMenuItem(Languages.getLanguage()[96]);
- public JMenuItem itemPaste = new JMenuItem(Languages.getLanguage()[97]);
- public JMenuItem itemGroup = new JMenuItem(Languages.getLanguage()[99]);
- public JMenuItem itemUngroup = new JMenuItem(Languages.getLanguage()[100]);
- public JMenuItem itemTrack = new JMenuItem(Languages.getLanguage()[101]);
- public JMenuItem itemUntrack = new JMenuItem(Languages.getLanguage()[102]);
-
- private boolean toolTip;
- private Position toolTipPos = new Position();
- private String toolTipText = "";
- private Point mousePosition = new Point();
-
-
- javax.swing.Timer animT;
- private final int ANIMTIME = 500;
- private ArrayList<AbstractCpsObject> animCps = null;
- private int animFPS = 60;
- private int animDuration = ANIMTIME;
- private int animDelay = 1000 / animFPS;
- private int animSteps = animDuration / animDelay;
-
-
- public MyCanvas(Model mod, Control control) {
- toolTip = false;
- this.controller = control;
- this.model = mod;
- scalediv20 = model.getScale() / 20;
- showedInformation[0] = true;
- showedInformation[1] = true;
- showedInformation[3] = false;
- control.setMaxCapacity(10000);
- popmenu.add(itemCut);
- popmenu.add(itemCopy);
- popmenu.add(itemPaste);
- popmenu.add(itemDelete);
- popmenu.addSeparator();
- popmenu.add(itemGroup);
- popmenu.add(itemUngroup);
- popmenu.add(itemTrack);
- popmenu.add(itemUntrack);
- updCon = new UpdateController(mod, control);
- itemDelete.setEnabled(false);
- itemCut.setEnabled(false);
- itemCopy.setEnabled(false);
- itemPaste.setEnabled(true);
- itemGroup.setEnabled(false);
- itemUngroup.setEnabled(false);
- itemTrack.setEnabled(false);
- itemUntrack.setEnabled(false);
- itemCut.setText(Languages.getLanguage()[95]);
- itemGroup.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
-
- unPos = new Position(0, 0);
- animCps = new ArrayList<>();
- for (AbstractCpsObject cps : model.getSelectedCpsObjects()) {
- animCps.add(cps);
- unPos.x += cps.getPosition().x;
- unPos.y += cps.getPosition().y;
- }
- unPos.x /= animCps.size();
- unPos.y /= animCps.size();
-
- savePos = new ArrayList<>();
- for (int i = 0; i < animCps.size(); i++) {
- savePos.add(new Position(0, 0));
- savePos.get(i).x = animCps.get(i).getPosition().x;
- savePos.get(i).y = animCps.get(i).getPosition().y;
- }
- animT = new javax.swing.Timer(animDelay, new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- if (animDuration - animDelay > 0 && animCps.size() > 1) {
- for (int i = 0; i < animCps.size(); i++) {
- double x1 = animCps.get(i).getPosition().x - unPos.x;
- double y1 = animCps.get(i).getPosition().y - unPos.y;
- animCps.get(i).getPosition().x -= x1 / animSteps;
- animCps.get(i).getPosition().y -= y1 / animSteps;
- }
- repaint();
- animDuration -= animDelay;
- animSteps--;
- } else {
- animDuration = ANIMTIME;
- animSteps = animDuration / animDelay;
- animT.stop();
- for (int i = 0; i < animCps.size(); i++) {
- animCps.get(i).getPosition().x = savePos.get(i).x;
- animCps.get(i).getPosition().y = savePos.get(i).y;
- }
- controller.addUpperNode("NodeOfNode", null, animCps);
- controller.calculateStateForCurrentTimeStep();
- repaint();
- }
- }
- });
- animT.start();
- }
- });
- itemUngroup.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
-
- JTabbedPane tabbedPane = (JTabbedPane) getParent().getParent().getParent();
- for (int i = 4; i < tabbedPane.getTabCount(); i++) {
- if (((UpperNodeCanvas) ((JScrollPane) tabbedPane.getComponentAt(i)).getViewport()
- .getComponent(0)).upperNode.getId() == ((CpsUpperNode) tempCps).getId()) {
- tabbedPane.remove(i);
- break;
- }
- }
- savePos = new ArrayList<>();
- animCps = ((CpsUpperNode) tempCps).getNodes();
- controller.delUpperNode((CpsUpperNode) tempCps, null);
- for (int i = 0; i < animCps.size(); i++) {
- savePos.add(new Position(0, 0));
- savePos.get(i).x = animCps.get(i).getPosition().x;
- savePos.get(i).y = animCps.get(i).getPosition().y;
- }
- for (AbstractCpsObject cps : animCps) {
- int x = ((CpsUpperNode) tempCps).getPosition().x;
- int y = ((CpsUpperNode) tempCps).getPosition().y;
- cps.setPosition(new Position(x, y));
- }
- animT = new javax.swing.Timer(animDelay, new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- if (animDuration - animDelay >= 0) {
- for (int i = 0; i < animCps.size(); i++) {
- double x1 = animCps.get(i).getPosition().x - savePos.get(i).x;
- double y1 = animCps.get(i).getPosition().y - savePos.get(i).y;
- animCps.get(i).getPosition().x -= x1 / animSteps;
- animCps.get(i).getPosition().y -= y1 / animSteps;
- }
- repaint();
- animDuration -= animDelay;
- animSteps--;
- } else {
- animDuration = ANIMTIME;
- animSteps = animDuration / animDelay;
- animT.stop();
- for (int i = 0; i < animCps.size(); i++) {
- animCps.get(i).getPosition().x = savePos.get(i).x;
- animCps.get(i).getPosition().y = savePos.get(i).y;
- }
- controller.calculateStateForCurrentTimeStep();
- repaint();
- }
- }
- });
- animT.start();
- }
- });
-
- itemTrack.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- for (AbstractCpsObject o : model.getSelectedCpsObjects()) {
- boolean found = false;
- if (controller.getTrackingObj() != null) {
- if (controller.getTrackingObj().contains(o)) {
- found = true;
- }
- }
- if (!found) {
- controller.addTrackingObj(o);
- if (o instanceof HolonObject) {
- ((HolonObject) o).updateTrackingInfo();
- }
- }
- controller.addTextToConsole("Tracking: ", Color.BLACK, 12, false, false, false);
- controller.addTextToConsole("" + o.getName(), Color.BLUE, 12, true, false, false);
- controller.addTextToConsole(", ID:", Color.BLACK, 12, false, false, false);
- controller.addTextToConsole("" + o.getId(), Color.RED, 12, true, false, true);
- }
- }
- });
- itemUntrack.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- for (AbstractCpsObject o : model.getSelectedCpsObjects()) {
- if (o instanceof HolonObject) {
- boolean found = false;
- if (controller.getTrackingObj() != null) {
- for (AbstractCpsObject obj : controller.getTrackingObj()) {
- if (obj instanceof HolonObject) {
- if (obj.getId() == o.getId()) {
- found = true;
- }
- }
- }
- }
- if (found) {
-
-
- controller.removeTrackingObj((HolonObject) o);
- ((HolonObject) o).setTrackingProd(new float[100]);
- ((HolonObject) o).setTrackingCons(new float[100]);
- }
- controller.addTextToConsole("Untracking: ", Color.BLACK, 12, false, false, false);
- controller.addTextToConsole("" + o.getName(), Color.BLUE, 12, true, false, false);
- controller.addTextToConsole(", ID:", Color.BLACK, 12, false, false, false);
- controller.addTextToConsole("" + o.getId(), Color.RED, 12, true, false, true);
- }
- }
- }
- });
- itemDelete.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
-
- boolean save = false;
- for (int j = 0; j < model.getSelectedCpsObjects().size(); j++) {
- AbstractCpsObject cps = model.getSelectedCpsObjects().get(j);
- if (j == model.getSelectedCpsObjects().size() - 1)
- save = true;
- controller.delCanvasObject(cps, save);
- controller.removeTrackingObj(cps);
-
- if (cps instanceof CpsUpperNode) {
- JTabbedPane tabbedPane = (JTabbedPane) getParent().getParent().getParent();
- for (int i = 4; i < tabbedPane.getTabCount(); i++) {
- if (((UpperNodeCanvas) ((JScrollPane) tabbedPane.getComponentAt(i)).getViewport()
- .getComponent(0)).upperNode.getId() == cps.getId()) {
- tabbedPane.remove(i);
- i = tabbedPane.getTabCount();
- }
- }
- }
- toolTip = false;
- }
- model.getSelectedCpsObjects().clear();
- tempCps = null;
- repaint();
- }
- });
- itemCut.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- controller.cut(null);
- ;
- itemPaste.setEnabled(true);
- repaint();
- }
- });
- itemCopy.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- controller.copy(null);
- itemPaste.setEnabled(true);
- repaint();
- }
- });
- itemPaste.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- try {
- controller.paste(null, mousePosition);
- } catch (JsonParseException | UnsupportedFlavorException | IOException e1) {
-
- JLabel message = new JLabel("The Clipboard information cannot be pastet into Application.");
- JOptionPane.showMessageDialog(null, message, "", JOptionPane.ERROR_MESSAGE);
- }
- repaint();
- }
- });
- this.addMouseListener(this);
- this.addMouseMotionListener(this);
- }
-
- public void paintComponent(Graphics g) {
- String maxCap;
- super.paintComponent(g);
-
- g2 = (Graphics2D) g;
- RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
- g2.setRenderingHints(rh);
-
- if (!model.getCanvasImagePath().isEmpty()) {
- img = new ImageIcon(model.getCanvasImagePath()).getImage();
- switch (model.getCanvasImageMode()) {
- case BackgroundPopUp.IMAGE_PIXELS:
- g2.drawImage(img, 0, 0, img.getWidth(null), img.getHeight(null), null);
- break;
- case BackgroundPopUp.STRETCHED:
- g2.drawImage(img, 0, 0, model.getCanvasX(), model.getCanvasY(), null);
- break;
- case BackgroundPopUp.CUSTOM:
- g2.drawImage(img, 0, 0, model.getCanvasImageWidth(), model.getCanvasImageHeight(), null);
- break;
- default:
- break;
- }
- }
-
- int i = 0;
- for (SubNet s : controller.getSimManager().getSubNets()) {
- if (model.getSubNetColors().size() - 1 < i) {
- controller.addSubNetColor(new Color((int) (Math.random() * 255), (int) (Math.random() * 255),
- (int) (Math.random() * 255)));
- }
- if (showedInformation[3]) {
- for (HolonObject cps : s.getObjects()) {
- cps.setBorderColor(model.getSubNetColors().get(i));
- }
- }
- i++;
- }
-
- if (drawEdge) {
- g2.setColor(Color.BLACK);
- g2.setStroke(new BasicStroke(2));
- g2.drawLine(tempCps.getPosition().x, tempCps.getPosition().y, x, y);
- }
- for (CpsEdge con : model.getEdgesOnCanvas()) {
- if (con.getA().getId() != model.getSelectedObjectID() && con.getB().getId() != model.getSelectedObjectID()
- && con != edgeHighlight) {
- if (con.getConnected() == 0) {
- if (con.getState()) {
- g2.setColor(Color.GREEN);
- if (con.getCapacity() != -1) {
- g2.setStroke(new BasicStroke(Math.min(((con.getFlow() / con.getCapacity() * 3) + 1), 4)));
- }
- } else {
- g2.setColor(Color.RED);
- g2.setStroke(new BasicStroke(2));
- }
- } else {
- g2.setColor(Color.DARK_GRAY);
- g2.setStroke(new BasicStroke(2));
- }
- g2.drawLine(con.getA().getPosition().x, con.getA().getPosition().y, con.getB().getPosition().x,
- con.getB().getPosition().y);
- if (con.getCapacity() == -1) {
- maxCap = Character.toString('\u221e');
- } else if (con.getCapacity() == -2) {
- maxCap = "???";
- } else {
- maxCap = String.valueOf(con.getCapacity());
- }
- if (showedInformation[0]) {
- if (con.getConnected() == 0 || con.getConnected() == 1) {
- g2.drawString(con.getFlow() + "/" + maxCap,
- (con.getA().getPosition().x + con.getB().getPosition().x) / 2,
- (con.getA().getPosition().y + con.getB().getPosition().y) / 2);
- } else {
- g2.drawString("not connected", (con.getA().getPosition().x + con.getB().getPosition().x) / 2,
- (con.getA().getPosition().y + con.getB().getPosition().y) / 2);
- }
- }
- }
- }
-
- if (model.getSelectedObjectID() > 0 || !model.getSelectedCpsObjects().isEmpty() || !tempSelected.isEmpty()) {
- g2.setColor(Color.BLUE);
- for (CpsEdge con : model.getEdgesOnCanvas()) {
- if (con.getFlow() <= con.getCapacity()) {
- g2.setStroke(new BasicStroke(Math.min(((con.getFlow() / con.getCapacity() * 3) + 1), 4)));
- } else {
- g2.setStroke(new BasicStroke(2));
- }
- if (con.getA().getId() == model.getSelectedObjectID()
- || model.getSelectedCpsObjects().contains(con.getA()) || tempSelected.contains(con.getA())
- || con.getB().getId() == model.getSelectedObjectID()
- || model.getSelectedCpsObjects().contains(con.getB())
- || tempSelected.contains(con.getB()) && con != edgeHighlight) {
- g2.drawLine(con.getA().getPosition().x, con.getA().getPosition().y, con.getB().getPosition().x,
- con.getB().getPosition().y);
- if (con.getCapacity() == -1) {
- maxCap = Character.toString('\u221e');
- } else if (con.getCapacity() == -2) {
- maxCap = "???";
- } else {
- maxCap = String.valueOf(con.getCapacity());
- }
- if (showedInformation[0]) {
- if (con.getConnected() == 0 || con.getConnected() == 1) {
- g2.drawString(con.getFlow() + "/" + maxCap,
- (con.getA().getPosition().x + con.getB().getPosition().x) / 2,
- (con.getA().getPosition().y + con.getB().getPosition().y) / 2);
- } else {
- g2.drawString("not connected",
- (con.getA().getPosition().x + con.getB().getPosition().x) / 2,
- (con.getA().getPosition().y + con.getB().getPosition().y) / 2);
- }
- }
- }
- }
- } else if (edgeHighlight != null) {
- g2.setColor(Color.BLUE);
- if (edgeHighlight.getFlow() <= edgeHighlight.getCapacity()) {
- g2.setStroke(new BasicStroke(
- Math.min(((edgeHighlight.getFlow() / edgeHighlight.getCapacity() * 3) + 1), 4)));
- } else {
- g2.setStroke(new BasicStroke(2));
- }
- g2.drawLine(edgeHighlight.getA().getPosition().x, edgeHighlight.getA().getPosition().y,
- edgeHighlight.getB().getPosition().x, edgeHighlight.getB().getPosition().y);
- if (edgeHighlight.getCapacity() == -1) {
- maxCap = Character.toString('\u221e');
- } else if (edgeHighlight.getCapacity() == -2) {
- maxCap = "???";
- } else {
- maxCap = String.valueOf(edgeHighlight.getCapacity());
- }
- if (showedInformation[0]) {
- g2.drawString(edgeHighlight.getFlow() + "/" + maxCap,
- (edgeHighlight.getA().getPosition().x + edgeHighlight.getB().getPosition().x) / 2,
- (edgeHighlight.getA().getPosition().y + edgeHighlight.getB().getPosition().y) / 2);
- }
- }
-
- for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
-
- if (showedInformation[3]) {
- g2.setColor(cps.getBorderColor());
- if (!(cps instanceof CpsNode)){
-
- g2.fillRect((int) (cps.getPosition().x - controller.getScaleDiv2() - scalediv20 - 3),
- (int) (cps.getPosition().y - controller.getScaleDiv2() - scalediv20 - 3),
- (int) (controller.getScale() + ((scalediv20 + 3) * 2)),
- (int) (controller.getScale() + ((scalediv20 + 3) * 2)));
- }
- }
-
- if (cps instanceof CpsNode && (cps == tempCps || model.getSelectedCpsObject() == cps
- || model.getSelectedCpsObjects().contains(cps) || tempSelected.contains(cps))) {
- img = new ImageIcon(this.getClass().getResource("/Images/node_selected.png")).getImage();
- } else {
- if (cps instanceof HolonSwitch) {
- if (((HolonSwitch) cps).getActiveAt()[model.getCurIteration()]) {
- ((HolonSwitch) cps).setAutoState(true);
- } else {
- ((HolonSwitch) cps).setAutoState(false);
- }
- }
-
- if ((cps == tempCps && model.getSelectedCpsObjects().size() == 0 && tempSelected.size() == 0)
- || model.getSelectedCpsObjects().contains(cps) || tempSelected.contains(cps)) {
- g2.setColor(Color.BLUE);
- g2.fillRect((int) (cps.getPosition().x - controller.getScaleDiv2() - scalediv20),
- (int) (cps.getPosition().y - controller.getScaleDiv2() - scalediv20),
- (int) (controller.getScale() + (scalediv20 * 2)),
- (int) (controller.getScale() + (scalediv20 * 2)));
- if (showedInformation[1] && cps instanceof HolonObject) {
- g2.setColor(Color.BLACK);
- float totalEnergy = ((HolonObject) cps).getCurrentEnergyAtTimeStep(model.getCurIteration());
- g2.drawString(Float.toString(totalEnergy), cps.getPosition().x - controller.getScaleDiv2(),
- cps.getPosition().y - controller.getScaleDiv2() - 10);
- }
- } else if (cps instanceof HolonObject) {
- g2.setColor(((HolonObject) cps).getColor());
- g2.fillRect((int) (cps.getPosition().x - controller.getScaleDiv2() - scalediv20),
- (int) (cps.getPosition().y - controller.getScaleDiv2() - scalediv20),
- (int) (controller.getScale() + (scalediv20 * 2)),
- (int) (controller.getScale() + (scalediv20 * 2)));
- if (showedInformation[1]) {
- g2.setColor(Color.BLACK);
- float totalEnergy = ((HolonObject) cps).getCurrentEnergyAtTimeStep(model.getCurIteration());
- g2.drawString(Float.toString(totalEnergy), cps.getPosition().x - controller.getScaleDiv2(),
- cps.getPosition().y - controller.getScaleDiv2() - 10);
- }
- }
-
- 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 - controller.getScaleDiv2(),
- cps.getPosition().y - controller.getScaleDiv2(), controller.getScale(), controller.getScale(),
- null);
- }
-
- 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);
- }
- }
-
- if (toolTip) {
- g2.setColor(new Color(255, 225, 150));
- g2.setStroke(new BasicStroke(1));
- int textWidth = g.getFontMetrics().stringWidth(toolTipText) + 2;
-
- int fixXPos = toolTipPos.x - (textWidth >> 1) + model.getScaleDiv2();
-
-
-
-
-
-
- int fixYPos = toolTipPos.y;
-
- if (fixXPos < 0) {
- fixXPos = 0;
- } else if (fixXPos + textWidth + 1 > this.getWidth()) {
- fixXPos -= (fixXPos + textWidth + 1) - this.getWidth();
- }
- if (fixYPos + 16 > this.getHeight()) {
- fixYPos -= (fixYPos + 16) - this.getHeight();
- }
- g2.fillRect(fixXPos, fixYPos, textWidth, 15);
- g2.setColor(Color.BLACK);
- g2.drawRect(fixXPos, fixYPos, textWidth, 15);
- g2.drawString(toolTipText, fixXPos + 2, fixYPos + 12);
- }
- }
- @Override
- public void mouseClicked(MouseEvent e) {
- if (e.getButton() == e.BUTTON1) {
- if (model.getPropertyTable().getRowCount() > 0) {
- for (int i = model.getPropertyTable().getRowCount() - 1; i > -1; i--) {
- model.getPropertyTable().removeRow(i);
- }
- }
- updCon.paintProperties(tempCps);
- updCon.refreshTableHolonElement(model.getMultiTable(), model.getSingleTable());
- updCon.refreshTableProperties(model.getPropertyTable());
- }
- }
- @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);
-
- for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
- cx = cps.getPosition().x - controller.getScaleDiv2();
- cy = cps.getPosition().y - controller.getScaleDiv2();
- if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
- tempCps = cps;
- controller.addTextToConsole("Selected: ", Color.BLACK, 12, false, false, false);
- controller.addTextToConsole("" + cps.getName(), Color.BLUE, 12, true, false, false);
- controller.addTextToConsole(", ID:", Color.BLACK, 12, false, false, false);
- controller.addTextToConsole("" + cps.getId(), Color.RED, 12, true, false, true);
- dragging = true;
- if (e.isControlDown() && tempCps != null) {
- if (model.getSelectedCpsObjects().contains(tempCps)) {
- controller.deleteSelectedObject(tempCps);
- } else {
- controller.addSelectedObject(tempCps);
- }
- }
-
- if (tempCps.getClass() == HolonObject.class) {
- HolonObject tempObj = ((HolonObject) tempCps);
- dataSelected = tempObj.getElements();
- }
- if (e.isShiftDown()) {
- drawEdge = true;
- dragging = false;
- }
- }
- }
-
- if (tempCps == null) {
- edgeHighlight = mousePositionOnEdge(x, y);
- controller.setSelecteEdge(edgeHighlight);
- controller.setSelectedObjectID(0);
- if (!e.isControlDown() && e.getButton() != MouseEvent.BUTTON3) {
- model.getSelectedCpsObjects().clear();
- }
- }
- if (edgeHighlight == null && tempCps == null) {
- sx = e.getX();
- sy = e.getY();
- doMark = true;
- }
- repaint();
- }
- @Override
- public void mouseReleased(MouseEvent e) {
- x = e.getX();
- y = e.getY();
- dragging = false;
- if (drawEdge) {
- drawEdge = false;
- drawDeleteEdge();
- }
- if (dragged == true) {
- try {
- controller.autoSave();
- } catch (IOException ex) {
-
- ex.printStackTrace();
- }
- }
- if (!e.isControlDown() && dragged == false && tempCps != null && e.BUTTON3 != e.getButton()) {
- model.getSelectedCpsObjects().clear();
- controller.addSelectedObject(tempCps);
- }
- dragged = false;
-
- if (e.getButton() == MouseEvent.BUTTON3) {
- if (e.getButton() == MouseEvent.BUTTON3 && tempCps != null) {
- itemDelete.setEnabled(true);
- itemCut.setEnabled(true);
- itemCopy.setEnabled(true);
- if (tempCps != null) {
- itemGroup.setEnabled(true);
- itemTrack.setEnabled(true);
- itemUntrack.setEnabled(true);
- }
- if (tempCps instanceof CpsUpperNode)
- itemUngroup.setEnabled(true);
- else
- itemUngroup.setEnabled(false);
- if (model.getSelectedCpsObjects().size() == 0) {
- controller.addSelectedObject(tempCps);
- }
- } else {
- itemCut.setEnabled(false);
- itemCopy.setEnabled(false);
- itemDelete.setEnabled(false);
- itemGroup.setEnabled(false);
- itemUngroup.setEnabled(false);
- itemTrack.setEnabled(false);
- itemUntrack.setEnabled(false);
- }
- mousePosition = this.getMousePosition();
- popmenu.show(e.getComponent(), e.getX(), e.getY());
- }
- if (doMark) {
- doMark = false;
- for (AbstractCpsObject cps : tempSelected) {
- if (!model.getSelectedCpsObjects().contains(cps)) {
- controller.addSelectedObject(cps);
- }
- }
- controller.getObjectsInDepth();
- tempSelected.clear();
- }
- if (doubleClick() && tempCps != null && tempCps instanceof HolonSwitch && MouseEvent.BUTTON3 != e.getButton()) {
- ((HolonSwitch) tempCps).switchState();
- }
- controller.calculateStateForTimeStep(model.getCurIteration());
- repaint();
- }
- @Override
- public void mouseDragged(MouseEvent e) {
-
- x = e.getX();
- y = e.getY();
- if (!model.getSelectedCpsObjects().contains(tempCps) && doMark == false) {
- model.getSelectedCpsObjects().clear();
- if (tempCps != null) {
- controller.addSelectedObject(tempCps);
- }
- }
- if (dragging) {
- try {
- dragged = true;
- float xDist, yDist;
- x = e.getX();
- y = e.getY();
-
- if (e.getX() < controller.getScaleDiv2())
- x = controller.getScaleDiv2();
- 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();
-
- xDist = x - tempCps.getPosition().x;
- yDist = y - tempCps.getPosition().y;
- tempCps.setPosition(x, y);
-
- toolTip = true;
- toolTipText = tempCps.getName() + ", " + tempCps.getId();
- toolTipPos.x = tempCps.getPosition().x - controller.getScaleDiv2();
- toolTipPos.y = tempCps.getPosition().y + controller.getScaleDiv2();
-
- for (AbstractCpsObject cps : model.getSelectedCpsObjects()) {
- if (cps != tempCps) {
- x = (int) (cps.getPosition().x + xDist);
- y = (int) (cps.getPosition().y + yDist);
-
- if (x <= controller.getScaleDiv2())
- x = controller.getScaleDiv2();
- 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);
- }
- }
- repaint();
- } catch (Exception eex) {
- }
- }
-
- if (doMark) {
- tempSelected.clear();
- for (AbstractCpsObject 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 + model.getScaleDiv2() && y1 <= cps.getPosition().y + model.getScaleDiv2()
- && x2 >= cps.getPosition().x && y2 >= cps.getPosition().y) {
- tempSelected.add(cps);
- }
- }
- }
- repaint();
- }
- @Override
- public void mouseMoved(MouseEvent e) {
- x = e.getX();
- y = e.getY();
-
- boolean on = false;
- for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
- cx = cps.getPosition().x - controller.getScaleDiv2();
- cy = cps.getPosition().y - controller.getScaleDiv2();
- if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
- on = true;
- toolTipPos.x = cps.getPosition().x - controller.getScaleDiv2();
- toolTipPos.y = cps.getPosition().y + controller.getScaleDiv2();
- toolTipText = cps.getName() + ", " + cps.getId();
- }
- }
- if (on) {
- toolTip = true;
- } else {
- toolTip = false;
- }
- repaint();
- }
-
- private void drawDeleteEdge() {
- boolean node = true;
- boolean newEdge = true;
- boolean onEdge = true;
- boolean deleteNode = false;
- CpsEdge e = null;
- for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
- cx = cps.getPosition().x - controller.getScaleDiv2();
- cy = cps.getPosition().y - controller.getScaleDiv2();
- 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);
-
- 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()) {
- deleteNode = true;
- }
- }
- if (newEdge) {
- e = new CpsEdge(cps, tempCps, model.getMaxCapacity());
- controller.addEdgeOnCanvas(e);
- }
- }
- }
-
- if (onEdge) {
- CpsEdge p = mousePositionOnEdge(x, y);
- if (p != null) {
- CpsEdge e1 = null;
- CpsEdge e2 = null;
- node = false;
- CpsNode n = new CpsNode("Node");
- n.setPosition(x, y);
- controller.addObjectCanvas(n);
- AbstractCpsObject r, k;
- r = p.getA();
- k = p.getB();
- e = new CpsEdge(n, tempCps, model.getMaxCapacity());
- e1 = new CpsEdge(n, r, model.getMaxCapacity());
- e2 = new CpsEdge(n, k, model.getMaxCapacity());
- controller.removeEdgesOnCanvas(p);
- controller.addEdgeOnCanvas(e);
- controller.addEdgeOnCanvas(e1);
- controller.addEdgeOnCanvas(e2);
- }
- }
-
- if (node) {
- CpsNode n = new CpsNode("Node");
- n.setPosition(x, y);
- controller.addObjectCanvas(n);
- e = new CpsEdge(n, tempCps, model.getMaxCapacity());
- controller.addEdgeOnCanvas(e);
- }
-
- if (deleteNode) {
- controller.delCanvasObject(tempCps, true);
- tempCps = null;
- }
- }
-
- public CpsEdge mousePositionOnEdge(int x, int y) {
- x += controller.getScaleDiv2();
- y += controller.getScaleDiv2();
- 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;
- }
-
- if (l.ptLineDistSq(x - model.getScaleDiv2(), y - model.getScaleDiv2()) < 20 && x > lx && x < hx && y > ly
- && y < hy) {
- return p;
- }
- }
- return null;
- }
- public void updateLanguages() {
- itemCut.setText(Languages.getLanguage()[95]);
- itemCopy.setText(Languages.getLanguage()[96]);
- itemPaste.setText(Languages.getLanguage()[97]);
- itemDelete.setText(Languages.getLanguage()[98]);
- itemGroup.setText(Languages.getLanguage()[99]);
- itemUngroup.setText(Languages.getLanguage()[100]);
- itemTrack.setText(Languages.getLanguage()[101]);
- itemUntrack.setText(Languages.getLanguage()[102]);
- }
-
- 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 setShowedInformation(boolean connection, boolean object, boolean border) {
- showedInformation[0] = connection;
- showedInformation[1] = object;
- showedInformation[3] = border;
- }
-
- public boolean[] getShowedInformation() {
- return showedInformation;
- }
- }
|