123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575 |
- 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.JSplitPane;
- 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;
- /**
- * This Class is the Canvas. All Objects will be visualized here
- *
- * @author Gruppe14
- */
- public class UpperNodeCanvas 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
- private float scalediv20;
- // Path
- public String path;
- private JLabel breadCrumb;
- ArrayList<HolonElement> dataSelected = new ArrayList<HolonElement>();
- ArrayList<AbstractCpsObject> tempSelected = new ArrayList<AbstractCpsObject>();
- // The UpperNode
- public CpsUpperNode upperNode;
- private boolean[] showedInformation = new boolean[5];
- private boolean dragging = false; // for dragging
- private boolean dragged = false; // if an object/objects was/were dragged
- private boolean drawEdge = false; // for drawing edges
- private boolean click = false; // for double click
- private boolean doMark = false; // for double click
- public AbstractCpsObject tempCps = null;
- private CpsEdge edgeHighlight = null;
- // PopUpMenu
- private JPopupMenu popmenu = new JPopupMenu();
- private JMenuItem itemDelete = new JMenuItem("Delete");
- private JMenuItem itemCut = new JMenuItem("Cut");
- private JMenuItem itemCopy = new JMenuItem("Copy");
- public JMenuItem itemPaste = new JMenuItem("Paste");
- public JMenuItem itemGroup = new JMenuItem("Group");
- public JMenuItem itemUngroup = new JMenuItem("Ungroup");
- public JMenuItem itemTrack = new JMenuItem("Track");
- public JMenuItem itemUntrack = new JMenuItem("Untrack");
- // Tooltip
- private boolean toolTip; // Tooltip on or off
- private Position toolTipPos = new Position(); // Tooltip Position
- private String toolTipText = "";
- private Point mousePosition = new Point(); // Mouse Position when
- // rightclicked
- // contains the value of the Capacity for new created Edges
- private UpdateController updCon;
- // Animation Stuff
- javax.swing.Timer animT; // animation Timer
- private final int ANIMTIME = 500; // animation Time
- private ArrayList<AbstractCpsObject> animCps = null;
- private int animFPS = 60;
- private int animDuration = ANIMTIME; // animation Duration
- private int animDelay = 1000 / animFPS; // animation Delay
- private int animSteps = animDuration / animDelay; // animation Steps;
- private Position unPos;
- private ArrayList<Position> savePos;
- /**
- * Constructor.
- *
- * @param mod
- * the Model
- * @param control
- * the Controller
- * @param unitGraph
- */
- public UpperNodeCanvas(Model mod, Control control, UnitGraph unitGraph, CpsUpperNode UpperNode, String parentPath) {
- toolTip = false;
- this.controller = control;
- this.model = mod;
- this.upperNode = UpperNode;
- this.path = parentPath + upperNode.getName();
- this.breadCrumb = new JLabel(path);
- // this.add(breadCrumb);
- scalediv20 = model.getScale() / 20;
- // Cps objecte aus dem border links schieben
- upperNode.setLeftBorder((int) (50 + scalediv20 + scalediv20 + 10));
- for (AbstractCpsObject cps : upperNode.getNodes()) {
- if (cps.getPosition().x < model.getScaleDiv2() + upperNode.getLeftBorder() + 5) {
- cps.setPosition(
- new Position(upperNode.getLeftBorder() + 5 + model.getScaleDiv2(), cps.getPosition().y));
- }
- }
- showedInformation[0] = true;
- showedInformation[1] = true;
- showedInformation[4] = true;
- 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);
- itemDelete.setEnabled(false);
- itemCut.setEnabled(false);
- itemCopy.setEnabled(false);
- itemPaste.setEnabled(true);
- itemGroup.setEnabled(false);
- itemUngroup.setEnabled(false);
- itemTrack.setEnabled(false);
- itemUntrack.setEnabled(false);
- updCon = new UpdateController(model, controller);
- itemGroup.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- // calculate uppernode pos (taken from the controller)
- unPos = new Position(0, 0);
- animCps = new ArrayList<>();
- for (AbstractCpsObject cps : model.getSelectedCpsObjects()) {
- animCps.add(cps); // add to animation Cps ArrayList
- unPos.x += cps.getPosition().x;
- unPos.y += cps.getPosition().y;
- }
- unPos.x /= animCps.size();
- unPos.y /= animCps.size();
- // save old Position
- 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", upperNode, model.getSelectedCpsObjects());
- controller.calculateStateForCurrentTimeStep();
- repaint();
- }
- }
- });
- animT.start();
- }
- });
- itemUngroup.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- // save old Position
- 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, upperNode);
- 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()) {
- 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.addTrackingObj((HolonObject) o);
- ((HolonObject) o).updateTrackingInfo();
- }
- if (model.getShowConsoleLog()) {
- 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) {
- // Removed from tracking array and tracking
- // information reseted
- controller.removeTrackingObj((HolonObject) o);
- ((HolonObject) o).setTrackingProd(new float[100]);
- ((HolonObject) o).setTrackingCons(new float[100]);
- }
- }
- }
- System.out.println(controller.getTrackingObj());
- }
- });
- itemDelete.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- // Remove the selected Object objects
- for (AbstractCpsObject cps : model.getSelectedCpsObjects()) {
- if (upperNode.getNodes().contains(cps)) {
- controller.delObjUpperNode(cps, upperNode);
- // Removes the object from the tracked objects, in case
- // it was tracked
- controller.removeTrackingObj(cps);
- // Remove UpperNodeTab if UpperNode deleted
- if (cps instanceof CpsUpperNode) {
- boolean splitView = false;
- JSplitPane tempSplit = (JSplitPane) getParent().getParent().getParent().getParent();
- JTabbedPane tabbedPane;
- JTabbedPane tabbedPane2;
- // if SplitView is activated
- if (tempSplit.getLeftComponent() instanceof JTabbedPane
- && tempSplit.getRightComponent() instanceof JTabbedPane) {
- splitView = true;
- tabbedPane = (JTabbedPane) tempSplit.getLeftComponent();
- tabbedPane2 = (JTabbedPane) tempSplit.getRightComponent();
- } else {
- tabbedPane = (JTabbedPane) tempSplit.getLeftComponent();
- tabbedPane2 = null;
- }
- // Look if the uppernode is open in a Tab
- for (int i = 4; i < tabbedPane.getTabCount(); i++) {
- if (tabbedPane.getComponentAt(i) == null) {
- } else if (((UpperNodeCanvas) ((JScrollPane) tabbedPane.getComponentAt(i)).getViewport()
- .getComponent(0)).upperNode.getId() == cps.getId()) {
- ((ButtonTabComponent) tabbedPane.getTabComponentAt(i)).removeTabs();
- break;
- }
- }
- // If SplitView is on and the view on
- // tabbedPane2 is the deleted upperNode
- try {
- if (tabbedPane2 != null
- && ((UpperNodeCanvas) ((JScrollPane) tabbedPane2.getSelectedComponent())
- .getViewport().getComponent(0)).upperNode.getId() == cps.getId()) {
- ((ButtonTabComponent) tabbedPane.getTabComponentAt(tabbedPane2.getSelectedIndex()))
- .removeTabs();
- }
- } catch (Exception e2) {
- }
- }
- }
- }
- model.getSelectedCpsObjects().clear();
- tempCps = null;
- repaint();
- }
- });
- itemCut.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- controller.cut(upperNode);
- itemPaste.setEnabled(true);
- repaint();
- }
- });
- itemCopy.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- controller.copy(upperNode);
- itemPaste.setEnabled(true);
- repaint();
- }
- });
- itemPaste.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- try {
- controller.paste(upperNode, mousePosition);
- unitGraph.update(model.getSelectedCpsObjects());
- } catch (JsonParseException | UnsupportedFlavorException | IOException e1) {
- // TODO Auto-generated catch block
- 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);
- }
- /**
- * Paints all Components on the Canvas.
- *
- * @param g
- * Graphics
- */
- public void paintComponent(Graphics g) {
- String maxCap;
- super.paintComponent(g);
- ((JScrollPane) this.getParent().getParent()).setColumnHeaderView(breadCrumb);
- // Rendering
- g2 = (Graphics2D) g;
- RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
- g2.setRenderingHints(rh);
- // Paint the Background
- if (!upperNode.getImagePath().isEmpty()) {
- img = new ImageIcon(upperNode.getImagePath()).getImage();
- switch (upperNode.getBackgroundMode()) {
- case BackgroundPopUp.IMAGE_PIXELS:
- g2.drawImage(img, upperNode.getLeftBorder(), 0, img.getWidth(null), img.getHeight(null), null);
- break;
- case BackgroundPopUp.STRETCHED:
- g2.drawImage(img, upperNode.getLeftBorder(), 0, model.getCanvasX(), model.getCanvasY(), null);
- break;
- case BackgroundPopUp.CUSTOM:
- g2.drawImage(img, upperNode.getLeftBorder(), 0, upperNode.getImageWidht(), upperNode.getImageHeight(),
- null);
- break;
- default:
- break;
- }
- }
- // Draw Left Border
- g2.setColor(new Color(230, 230, 230));
- g2.fillRect(0, 0, upperNode.getLeftBorder(), this.getHeight());
- g2.setColor(Color.BLACK);
- g2.drawLine(0, 0, this.getWidth(), 0);
- // Test SubNet Coloring
- 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)));
- }
- for (HolonObject cps : s.getObjects()) {
- cps.setBorderColor(model.getSubNetColors().get(i));
- }
- i++;
- }
- // drawEdges that is being dragged
- if (drawEdge) {
- g2.setColor(Color.BLACK);
- g2.setStroke(new BasicStroke(2));
- // If TempCps is an outside Object
- if (!upperNode.getNodes().contains(tempCps)) {
- int count = 0;
- for (CpsEdge e : upperNode.getConnections()) {
- if (e.getA().equals(tempCps)) {
- g2.drawLine(upperNode.getLeftBorder() >> 1, (int) (model.getScaleDiv2() + scalediv20 + 5
- + (model.getScale() + scalediv20 + 10) * count), x, y);
- } else if (e.getB().equals(tempCps)) {
- g2.drawLine(upperNode.getLeftBorder() >> 1, (int) (model.getScaleDiv2() + scalediv20 + 5
- + (model.getScale() + scalediv20 + 10) * count), x, y);
- }
- count++;
- }
- } else {
- g2.drawLine(tempCps.getPosition().x, tempCps.getPosition().y, x, y);
- }
- }
- // draw Edges
- for (CpsEdge con : upperNode.getNodeEdges()) {
- 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);
- }
- }
- }
- }
- // Objects connected to upperNode
- int count = 0;
- for (CpsEdge e : upperNode.getConnections()) {
- AbstractCpsObject cps;
- if (e.getA().equals(this.upperNode)) {
- cps = e.getB();
- } else {
- cps = e.getA();
- }
- // Show and Highlight
- if (model.getSelectedCpsObjects().contains(cps) || showedInformation[4] == true) {
- for (CpsEdge ed : cps.getConnections()) {
- AbstractCpsObject obj = null;
- if (upperNode.getNodes().contains(ed.getA())) {
- obj = ed.getA();
- } else if (upperNode.getNodes().contains(ed.getB())) {
- obj = ed.getB();
- }
- if (obj != null) {
- if (ed.getConnected() == 0) {
- if (ed.getState()) {
- g2.setColor(Color.GREEN);
- if (ed.getCapacity() != -1) {
- g2.setStroke(
- new BasicStroke(Math.min(((ed.getFlow() / ed.getCapacity() * 3) + 1), 4)));
- }
- } else {
- g2.setColor(Color.RED);
- g2.setStroke(new BasicStroke(2));
- }
- if (ed.getA().getId() == model.getSelectedObjectID()
- || ed.getB().getId() == model.getSelectedObjectID() || edgeHighlight == ed)
- g2.setColor(Color.BLUE);
- } else {
- g2.setColor(Color.DARK_GRAY);
- g2.setStroke(new BasicStroke(2));
- }
- g2.drawLine(obj.getPosition().x, obj.getPosition().y, (upperNode.getLeftBorder() >> 1),
- (int) (scalediv20 + 5 + (50 + scalediv20 + 10) * count) + 25);
- if (showedInformation[0]) {
- if (ed.getCapacity() == -1) {
- maxCap = Character.toString('\u221e');
- } else if (ed.getCapacity() == -2) {
- maxCap = "???";
- } else {
- maxCap = String.valueOf(ed.getCapacity());
- }
- if (ed.getConnected() == 0 || ed.getConnected() == 1) {
- g2.drawString(ed.getFlow() + "/" + maxCap,
- (obj.getPosition().x + (upperNode.getLeftBorder() >> 1)) / 2,
- (obj.getPosition().y + (int) (scalediv20 + 5 + (50 + scalediv20 + 10) * count)
- + 25) / 2);
- } else {
- g2.drawString("not connected",
- (obj.getPosition().x + (upperNode.getLeftBorder() >> 1)) / 2,
- (obj.getPosition().y + (int) (scalediv20 + 5 + (50 + scalediv20 + 10) * count)
- + 25) / 2);
- }
- }
- }
- }
- }
- // Border Highlighting
- if (showedInformation[3]) {
- g2.setColor(cps.getBorderColor());
- if (g2.getColor() != Color.WHITE) {
- g2.fillRect((int) ((upperNode.getLeftBorder() >> 1) - 25 - scalediv20) - 3,
- (int) (scalediv20 + 5 + (25 + scalediv20 + 10) * count - scalediv20) - 3,
- (int) (50 + ((scalediv20 + 3) * 2)), (int) (50 + ((scalediv20 + 3) * 2)));
- }
- }
- // node image
- 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);
- }
- }
- // Highlighting
- if ((cps == tempCps && model.getSelectedCpsObjects().size() == 0 && tempSelected.size() == 0)
- || model.getSelectedCpsObjects().contains(cps) || tempSelected.contains(cps)) {
- g2.setColor(Color.BLUE);
- g2.fillRect((int) ((upperNode.getLeftBorder() >> 1) - 25 - scalediv20),
- (int) (scalediv20 + 5 + (50 + scalediv20 + 10) * count - scalediv20),
- (int) (50 + (scalediv20 * 2)), (int) (50 + (scalediv20 * 2)));
- } else if (cps instanceof HolonObject) {
- g2.setColor(((HolonObject) cps).getColor());
- g2.fillRect((int) ((upperNode.getLeftBorder() >> 1) - 25 - scalediv20),
- (int) (scalediv20 + 5 + (50 + scalediv20 + 10) * count - scalediv20),
- (int) (50 + (scalediv20 * 2)), (int) (50 + (scalediv20 * 2)));
- }
- // draw image
- 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, (upperNode.getLeftBorder() >> 1) - 25,
- (int) (scalediv20 + 5 + (50 + scalediv20 + 10) * count), 50, 50, null);
- count++;
- }
- // Highlighted Edge
- if (model.getSelectedObjectID() > 0 || !model.getSelectedCpsObjects().isEmpty() || !tempSelected.isEmpty()) {
- g2.setColor(Color.BLUE);
- for (CpsEdge con : upperNode.getNodeEdges()) {
- 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));
- }
- if (upperNode.getNodeEdges().contains(edgeHighlight)) {
- 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);
- }
- }
- }
- // Objects in upper node
- for (AbstractCpsObject cps : upperNode.getNodes()) {
- // Border Highlighting
- if (showedInformation[3]) {
- g2.setColor(cps.getBorderColor());
- if (g2.getColor() != Color.WHITE) {
- g2.fillRect((int) (cps.getPosition().x - controller.getScaleDiv2() - scalediv20 - 3),
- (int) (cps.getPosition().y - model.getScaleDiv2() - scalediv20 - 3),
- (int) (controller.getScale() + ((scalediv20 + 3) * 2)),
- (int) (controller.getScale() + ((scalediv20 + 3) * 2)));
- }
- }
- // node image
- 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);
- }
- }
- // Highlighting
- 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 - model.getScaleDiv2() - scalediv20),
- (int) (cps.getPosition().y - model.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 - model.getScaleDiv2(),
- cps.getPosition().y - model.getScaleDiv2() - 10);
- }
- } else if (cps instanceof HolonObject) {
- g2.setColor(((HolonObject) cps).getColor());
- g2.fillRect((int) (cps.getPosition().x - model.getScaleDiv2() - scalediv20),
- (int) (cps.getPosition().y - model.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 - model.getScaleDiv2(),
- cps.getPosition().y - model.getScaleDiv2() - 10);
- }
- }
- // draw image
- 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 - model.getScaleDiv2(), cps.getPosition().y - model.getScaleDiv2(),
- controller.getScale(), controller.getScale(), null);
- }
- // Dragg Highlighting
- g2.setStroke(new BasicStroke(1));
- if (doMark) {
- g2.setColor(Color.BLACK);
- 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);
- }
- }
- // Border Line
- g2.setColor(Color.BLACK);
- g2.drawLine(upperNode.getLeftBorder(), 0, upperNode.getLeftBorder(), this.getHeight());
- // Tooltip
- if (toolTip) {
- g2.setColor(new Color(255, 225, 150));
- g2.setStroke(new BasicStroke(1));
- int textWidth = g.getFontMetrics().stringWidth(toolTipText) + 2; // Text
- // width
- int fixXPos = toolTipPos.x - (textWidth >> 1) + model.getScaleDiv2(); // Position
- // fixed
- // x
- // Position
- // to
- // the
- // screen
- int fixYPos = toolTipPos.y; //// Position fixed y Position to the
- //// screen
- 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) {
- updCon.refreshTableHolonElement(model.getMultiTable(), model.getSingleTable());
- }
- @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);
- controller.setSelectedObjectID(-1);
- // Object Selection
- // Erase old data in the PropertyTable
- if (model.getPropertyTable().getRowCount() > 0) {
- for (int i = model.getPropertyTable().getRowCount() - 1; i > -1; i--) {
- model.getPropertyTable().removeRow(i);
- }
- }
- if (e.getX() > upperNode.getLeftBorder()) {
- for (AbstractCpsObject cps : upperNode.getNodes()) {
- cx = cps.getPosition().x - model.getScaleDiv2();
- cy = cps.getPosition().y - model.getScaleDiv2();
- if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
- tempCps = cps;
- if (model.getShowConsoleLog()) {
- 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 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 (CpsEdge ed : upperNode.getConnections()) {
- AbstractCpsObject cps;
- if (ed.getA().equals(this.upperNode)) {
- cps = ed.getB();
- } else {
- cps = ed.getA();
- }
- if (x - controller.getScale() <= ((upperNode.getLeftBorder() >> 1) - model.getScaleDiv2())
- && y - controller.getScale() <= (scalediv20 + 5 + (model.getScale() + scalediv20 + 10) * count)
- && x >= (upperNode.getLeftBorder() >> 1) - model.getScaleDiv2()
- && y >= (scalediv20 + 5 + (model.getScale() + scalediv20 + 10) * count)) {
- tempCps = cps;
- if (model.getShowConsoleLog()) {
- 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);
- controller.setSelectedObjectID(tempCps.getId());
- }
- // If drawing an Edge (CTRL down)
- if (tempCps.getClass() == HolonObject.class) {
- HolonObject tempObj = ((HolonObject) tempCps);
- dataSelected = tempObj.getElements();
- }
- if (e.isShiftDown()) {
- drawEdge = true;
- }
- }
- count++;
- }
- }
- // Selection of CpsObject
- // model.setSelectedCpsObject(tempCps);
- // Edge Selection
- if (e.getButton() == e.BUTTON1) {
- if (tempCps == null) {
- edgeHighlight = mousePositionOnEdge(x, y);
- controller.setSelecteEdge(edgeHighlight);
- controller.setSelectedObjectID(0);
- if (!e.isControlDown() && e.getButton() != MouseEvent.BUTTON3) {
- model.getSelectedCpsObjects().clear();
- }
- updCon.deleteRows(model.getMultiTable());
- updCon.deleteRows(model.getSingleTable());
- }
- if (edgeHighlight == null && tempCps == null) {
- sx = e.getX();
- sy = e.getY();
- doMark = true;
- }
- // System.out.println("Selected Objects");
- // for (AbstractCpsObject temp : model.getSelectedCpsObjects()) {
- // System.out.println(temp.getName() + " " + temp.getID());
- // }
- repaint();
- }
- }
- @Override
- public void mouseReleased(MouseEvent e) {
- x = e.getX();
- y = e.getY();
- dragging = false;
- if (model.getSelectedCpsObjects().size() > 1) {
- model.getTableHolonElement().setModel(model.getMultiTable());
- } else if (model.getSelectedCpsObjects().size() == 1) {
- model.getTableHolonElement().setModel(model.getSingleTable());
- }
- if (drawEdge) {
- drawEdge = false;
- drawDeleteEdge();
- }
- if (dragged == true) {
- try {
- controller.autoSave();
- } catch (IOException ex) {
- // TODO Auto-generated catch block
- ex.printStackTrace();
- }
- }
- if (!e.isControlDown() && dragged == false && tempCps != null && MouseEvent.BUTTON3 != e.getButton()) {
- model.getSelectedCpsObjects().clear();
- controller.addSelectedObject(tempCps);
- }
- dragged = false;
- // Rightclick List
- if (e.getButton() == MouseEvent.BUTTON3) {
- if (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) {
- ((HolonSwitch) tempCps).switchState();
- }
- controller.calculateStateForTimeStep(model.getCurIteration());
- updCon.paintProperties(tempCps);
- updCon.refreshTableHolonElement(model.getMultiTable(), model.getSingleTable());
- updCon.refreshTableProperties(model.getPropertyTable());
- repaint();
- }
- @Override
- public void mouseDragged(MouseEvent e) {
- // If Edge is drawn
- 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 {
- // tempCps in the upperNode? else its a connected Object from
- // outside
- if (upperNode.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() + upperNode.getLeftBorder() + 5)
- x = controller.getScaleDiv2() + upperNode.getLeftBorder() + 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().x;
- yDist = y - tempCps.getPosition().y;
- tempCps.setPosition(x, y); // Drag Position
- // TipText Position and name
- toolTip = true;
- toolTipText = tempCps.getName() + ", " + tempCps.getId();
- toolTipPos.x = tempCps.getPosition().x - model.getScaleDiv2();
- toolTipPos.y = tempCps.getPosition().y + model.getScaleDiv2();
- // All Selected Objects
- for (AbstractCpsObject 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 < upperNode.getLeftBorder() + 5 + controller.getScaleDiv2())
- x = controller.getScaleDiv2() + upperNode.getLeftBorder() + 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);
- }
- }
- }
- repaint();
- } catch (Exception eex) {
- }
- }
- // Mark Objects
- if (doMark) {
- tempSelected.clear();
- for (AbstractCpsObject cps : upperNode.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().x + model.getScaleDiv2() && y1 <= cps.getPosition().y + model.getScaleDiv2()
- && x2 >= cps.getPosition().x && y2 >= cps.getPosition().y) {
- tempSelected.add(cps);
- }
- }
- int count = 0;
- for (CpsEdge ed : upperNode.getConnections()) {
- AbstractCpsObject cps = null;
- if (ed.getA().equals(upperNode)) {
- 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 <= upperNode.getLeftBorder() >> 1
- && y1 <= (int) (5 + (model.getScale() + scalediv20 + 10) * count) + model.getScaleDiv2()
- && x2 >= upperNode.getLeftBorder() >> 1
- && 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();
- // Everytghing for the tooltip :)
- boolean on = false;
- for (AbstractCpsObject cps : upperNode.getNodes()) {
- 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 - model.getScaleDiv2();
- toolTipPos.y = cps.getPosition().y + model.getScaleDiv2();
- toolTipText = cps.getName() + ", " + cps.getId();
- }
- }
- int count = 0;
- for (CpsEdge ed : upperNode.getConnections()) {
- AbstractCpsObject cps;
- if (ed.getA().equals(this.upperNode)) {
- cps = ed.getB();
- } else {
- cps = ed.getA();
- }
- cx = upperNode.getLeftBorder() >> 1;
- cy = (int) (scalediv20 + 5 + (50 + scalediv20 + 10) * count);
- if (x - 50 <= cx && y - 50 <= cy && x >= cx && y >= cy) {
- on = true;
- toolTipPos.x = cx - 25;
- toolTipPos.y = cy + 50;
- toolTipText = cps.getName() + ", " + cps.getId();
- }
- count++;
- }
- if (on) {
- toolTip = true;
- } else {
- toolTip = false;
- }
- repaint();
- }
- /**
- * Draws or Deletes an Edge.
- */
- private void drawDeleteEdge() {
- boolean node = true; // new node?
- boolean newEdge = true;
- boolean onEdge = true;
- boolean deleteNode = false;
- boolean outsideCon = !upperNode.getNodes().contains(tempCps); // Connection
- // to
- // the
- // outside
- boolean found = false; // dont search for outside connetion if inside
- // connection is found
- CpsEdge e = null;
- for (AbstractCpsObject cps : upperNode.getNodes()) {
- 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) {
- found = true;
- 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) {
- if (outsideCon) {
- controller.disconnectNodes(e, upperNode);
- } else {
- controller.delEdgeUpperNode(e, upperNode);
- }
- // 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()) {
- deleteNode = true;
- }
- }
- if (newEdge) {
- e = new CpsEdge(cps, tempCps, model.getMaxCapacity());
- if (outsideCon) {
- controller.connectNodes(e, upperNode);
- } else {
- controller.addEdgeUpperNode(e, upperNode);
- }
- }
- }
- }
- if (!found && !outsideCon) {
- int count = 0;
- for (CpsEdge ed : upperNode.getConnections()) {
- AbstractCpsObject cps = null;
- if (ed.getA().equals(upperNode)) {
- cps = ed.getB();
- } else {
- cps = ed.getA();
- }
- cx = upperNode.getLeftBorder() >> 1;
- cy = (int) (scalediv20 + 5 + (model.getScale() + scalediv20 + 10) * count);
- if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy
- && cps != tempCps) {
- outsideCon = true;
- 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) {
- if (outsideCon) {
- controller.disconnectNodes(e, upperNode);
- } else {
- controller.delEdgeUpperNode(e, upperNode);
- }
- // 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()) {
- deleteNode = true;
- }
- }
- if (newEdge) {
- CpsEdge edge = new CpsEdge(cps, tempCps, model.getMaxCapacity());
- if (outsideCon) {
- controller.connectNodes(edge, upperNode);
- } else {
- controller.addEdgeUpperNode(edge, upperNode);
- }
- }
- }
- count++;
- }
- }
- // 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.setPosition(x, y);
- controller.addObjUpperNode(n, upperNode);
- 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.delEdgeUpperNode(p, upperNode);
- if (outsideCon) {
- controller.connectNodes(e, upperNode);
- } else {
- controller.addEdgeUpperNode(e, upperNode);
- }
- controller.addEdgeUpperNode(e1, upperNode);
- controller.addEdgeUpperNode(e2, upperNode);
- }
- }
- // ins leere Gedragged
- if (node && x > upperNode.getLeftBorder()) {
- CpsNode n = new CpsNode("Node");
- n.setPosition(x, y);
- controller.addObjUpperNode(n, upperNode);
- e = new CpsEdge(n, tempCps, model.getMaxCapacity());
- if (outsideCon) {
- controller.connectNodes(e, upperNode);
- } else {
- controller.addEdgeUpperNode(e, upperNode);
- }
- }
- // Wenn ein Node ohne Connections da ist
- if (deleteNode) {
- controller.delCanvasObject(tempCps, true);
- tempCps = null;
- }
- }
- /**
- * 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) {
- x += controller.getScaleDiv2();
- y += controller.getScaleDiv2();
- int lx, ly, hx, hy;
- for (CpsEdge p : upperNode.getNodeEdges()) {
- 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;
- }
- }
- // edge to outside connection
- int count = 0;
- for (CpsEdge e : upperNode.getConnections()) {
- AbstractCpsObject cps;
- if (e.getA().equals(this.upperNode)) {
- cps = e.getB();
- } else {
- cps = e.getA();
- }
- for (CpsEdge p : cps.getConnections()) {
- AbstractCpsObject obj = null;
- boolean doTest = false;
- if (upperNode.getNodes().contains(p.getA())) {
- obj = p.getA();
- doTest = true;
- } else if (upperNode.getNodes().contains(p.getB())) {
- obj = p.getB();
- doTest = true;
- }
- // g2.drawImage(img, (borderPos >> 1) - 25, (int) (scalediv20 +
- // 5 + (50 + scalediv20 + 10) * count), 50, 50, null);
- if (doTest) {
- Line2D l = new Line2D.Float((upperNode.getLeftBorder() >> 1) - 25,
- (int) (scalediv20 + 5 + 25 + (50 + scalediv20 + 10) * count), obj.getPosition().x,
- obj.getPosition().y);
- if ((upperNode.getLeftBorder() >> 1) > obj.getPosition().x) {
- hx = (upperNode.getLeftBorder() >> 1) - 25 + model.getScaleDiv2() + 7;
- lx = obj.getPosition().x + model.getScaleDiv2() - 7;
- } else {
- lx = (upperNode.getLeftBorder() >> 1) - 25 + model.getScaleDiv2() - 7;
- hx = obj.getPosition().x + model.getScaleDiv2() + 7;
- }
- if ((int) (scalediv20 + 5 + (50 + scalediv20 + 10) * count) > obj.getPosition().y) {
- hy = (int) (scalediv20 + 5 + (50 + scalediv20 + 10) * count) + model.getScaleDiv2() + 7;
- ly = obj.getPosition().y + model.getScaleDiv2() - 7;
- } else {
- ly = (int) (scalediv20 + 5 + (50 + scalediv20 + 10) * count) + model.getScaleDiv2() - 7;
- hy = obj.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;
- }
- }
- }
- count++;
- }
- 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;
- }
- /**
- * sets the Edge Capacity.
- *
- * @param cap
- * capacity
- */
- public void setEdgeCapacity(float cap) {
- controller.setMaxCapacity(cap);
- }
- /**
- * Set if Information should be shown.
- *
- * @param connection
- * boolean for conecction
- * @param object
- * boolean for objects
- */
- public void setShowedInformation(boolean connection, boolean object, boolean nodeOfnode) {
- showedInformation[0] = connection;
- showedInformation[1] = object;
- showedInformation[4] = nodeOfnode;
- }
- /**
- * copies a set of given informations
- *
- * @param informations
- */
- public void setShowedInformation(boolean[] informations) {
- showedInformation = informations;
- }
- /**
- * Returns if Information should be shown.
- *
- * @return Array of boolean [0] = connection, [1] = objects
- */
- public boolean[] getShowedInformation() {
- return showedInformation;
- }
- /**
- * Set the Background Image;
- *
- * @param imagePath
- * Image Path
- * @param mode
- * Image Mode
- * @param width
- * Image custom width
- * @param height
- * Image custom height
- */
- public void setBackgroundImage(String imagePath, int mode, int width, int height) {
- upperNode.setBackgroundImage(imagePath, mode, width, height);
- }
- }
|