12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181 |
- package ui.view;
- import classes.*;
- import com.google.gson.JsonParseException;
- import ui.controller.Control;
- import ui.controller.UpdateController;
- import ui.model.CableWithState;
- import ui.model.Consumer;
- import ui.model.DecoratedCable;
- import ui.model.DecoratedHolonObject;
- import ui.model.DecoratedHolonObject.HolonObjectState;
- import ui.model.DecoratedNetwork;
- import ui.model.DecoratedSwitch;
- import ui.model.DecoratedSwitch.SwitchState;
- import ui.model.MinimumModel;
- import ui.model.Model;
- import ui.model.Passiv;
- import ui.model.Supplier;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.datatransfer.UnsupportedFlavorException;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.awt.event.MouseMotionListener;
- import java.awt.font.LineMetrics;
- import java.awt.geom.Line2D;
- import java.io.IOException;
- import java.util.ArrayList;
- /**
- * This Class is the Canvas. All Objects will be visualized here
- *
- * @author Gruppe14
- */
- public class MyCanvas extends AbstractCanvas implements MouseListener,
- MouseMotionListener {
- private static final long serialVersionUID = 1L;
- /**
- * Constructor.
- *
- * @param mod
- * the Model
- * @param control
- * the Controller
- * @param unitGraph
- */
- public MyCanvas(Model mod, Control control, UnitGraph unitGraph) {
- toolTip = false;
- this.controller = control;
- this.model = mod;
- scalediv20 = model.getScale() / 20;
- showedInformation[0] = true;
- showedInformation[1] = true;
- showedInformation[3] = false;
- showedInformation[4] = true;
- control.setMaxCapacity(10000);
- popmenu.add(itemCut);
- popmenu.add(itemCopy);
- popmenu.add(itemPaste);
- popmenu.add(itemDelete);
- popmenu.add(itemAlign);
- popmenu.addSeparator();
- popmenu.add(itemGroup);
- popmenu.add(itemUngroup);
- popmenu.add(itemTrack);
- popmenu.add(itemUntrack);
- popmenu.add(itemCreateTemplate);
- updCon = new UpdateController(mod, control);
- itemDelete.setEnabled(false);
- itemCut.setEnabled(false);
- itemCopy.setEnabled(false);
- itemPaste.setEnabled(true);
- itemAlign.setEnabled(false);
- itemGroup.setEnabled(false);
- itemUngroup.setEnabled(false);
- itemTrack.setEnabled(false);
- itemUntrack.setEnabled(false);
- itemCut.setText(Languages.getLanguage()[95]);
-
- itemGroup.addActionListener(actionEvent -> {
- // 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, actionEvent1 -> {
- if (animDuration - animDelay > 0 && animCps.size() > 1) {
- for (AbstractCpsObject animCpObject : animCps) {
- double x1 = animCpObject.getPosition().x - unPos.x;
- double y1 = animCpObject.getPosition().y - unPos.y;
- animCpObject.getPosition().x -= x1 / animSteps;
- animCpObject.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();
- triggerUpdateController();
- repaint();
- }
- });
- animT.start();
- });
- itemUngroup
- .addActionListener(actionEvent -> {
- // save old Position
- int upperNodeId = tempCps.getId();
-
- closeUpperNodeTab(upperNodeId);
- 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 = tempCps.getPosition().x;
- int y = tempCps.getPosition().y;
- cps.setPosition(new Position(x, y));
- }
- animT = new javax.swing.Timer(
- animDelay,
- actionEvent1 -> {
- if (animDuration - animDelay >= 0) {
- for (int i = 0; i < animCps.size(); i++) {
- Position pos = animCps.get(i).getPosition();
- double x1 = pos.x - savePos.get(i).x;
- double y1 = pos.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();
- triggerUpdateController();
- repaint();
- }
- });
- animT.start();
- });
- // adds the selected object(s) to the statistic panel
- itemTrack.addActionListener(actionEvent -> {
- for (AbstractCpsObject o : model.getSelectedCpsObjects()) {
- if (!((controller.getTrackingObj() != null) && (controller.getTrackingObj().contains(o)))) {
- controller.addTrackingObj(o);
- if (o instanceof HolonObject) {
- ((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(actionEvent -> {
- 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(o);
- ((HolonObject) o).setTrackingProd(new float[100]);
- ((HolonObject) o).setTrackingCons(new float[100]);
- }
- if (model.getShowConsoleLog()) {
- 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(actionEvent -> {
- // Remove the selected Object objects
- //Edge Deleting
- if (tempCps == null && edgeHighlight != null) {
- controller.removeEdgesOnCanvas(edgeHighlight);
- //Look for a CPSNode with no Connections and delete them
- if(edgeHighlight.getA().getClass() == CpsNode.class && edgeHighlight.getA().getConnections().size() == 0){
- controller.delCanvasObject(edgeHighlight.getA(), false);
- }
- if(edgeHighlight.getB().getClass() == CpsNode.class && edgeHighlight.getB().getConnections().size() == 0){ //Look on the other end of the cable
- controller.delCanvasObject(edgeHighlight.getB(), false);
- }
- edgeHighlight = null;
- }
- 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);
- // Remove UpperNodeTab if UpperNode deleted
- if (cps instanceof CpsUpperNode) {
- JSplitPane tempSplit = (JSplitPane) getParent().getParent()
- .getParent().getParent();
- JTabbedPane tabbedPane;
- JTabbedPane tabbedPane2;
- // if SplitView is activated
- if (tempSplit.getLeftComponent() instanceof JTabbedPane
- && tempSplit.getRightComponent() instanceof JTabbedPane) {
- 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
- && ((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) {
- }
- }
- toolTip = false;
- }
- model.getSelectedCpsObjects().clear();
- tempCps = null;
- repaint();
- });
- itemCut.addActionListener(actionEvent -> {
- controller.cut(null);
- itemPaste.setEnabled(true);
- repaint();
- });
- itemCopy.addActionListener(actionEvent -> {
- if(tempCps instanceof CpsUpperNode)
- controller.getObjectsInDepth();
- controller.copy(null);
- itemPaste.setEnabled(true);
- repaint();
- });
- itemAlign.addActionListener(actionEvent ->
- {
- for(AbstractCpsObject cps: model.getSelectedCpsObjects())
- align(cps, model.getScaleDiv2());
- repaint();
- });
- itemPaste
- .addActionListener(actionEvent -> {
- try {
- controller.paste(null, mousePosition);
- unitGraph.update(model.getSelectedCpsObjects());
- } 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();
- });
-
- /*
- * create Template
- */
- itemCreateTemplate.addActionListener(actionEvent -> {
- controller.createTemplate((HolonObject)tempCps,(JFrame)SwingUtilities.getRoot(this));
- });
- this.addMouseListener(this);
- this.addMouseMotionListener(this);
- }
- /**
- * Paints all Components on the Canvas.
- *
- * @param g
- * Graphics
- */
-
- private Color getStateColor(HolonObjectState state) {
- switch(state) {
- case NOT_SUPPLIED:
- return new Color(230, 120, 100);
- case NO_ENERGY:
- return Color.white;
- case OVER_SUPPLIED:
- return new Color(138, 43, 226);
- case PARTIALLY_SUPPLIED:
- return Color.yellow;
- case PRODUCER:
- return Color.lightGray;
- case SUPPLIED:
- return Color.green;
- default:
- return Color.BLACK;
- }
- }
- private void paintCanvasObject(Graphics2D g, DecoratedHolonObject decoratedHolonObject){
- Position pos = decoratedHolonObject.getModel().getPosition();
- Color statecolor = getStateColor(decoratedHolonObject.getState());
- g.setColor(statecolor);
- g.fillRect(pos.x - controller.getScaleDiv2(), pos.y - controller.getScaleDiv2(), controller.getScale(), controller.getScale());
- drawCanvasObject(g, decoratedHolonObject.getModel().getImage(), pos);
-
- }
- private void paintConsumer(Graphics2D g, Consumer con){
- paintCanvasObject(g, con);
- paintSupplyBar(g,con.getSupplyBarPercentage(), getStateColor(con.getState()), con.getModel().getPosition());
- }
- private void drawCanvasObject(Graphics2D g, String Image, Position pos) {
- g.drawImage(Util.loadImage(Image) ,
- pos.x - controller.getScaleDiv2(),
- pos.y - controller.getScaleDiv2(),
- controller.getScale(), controller.getScale(), null);
- }
-
- private void paintCable(Graphics2D g, DecoratedCable cable)
- {
- Position start = cable.getModel().getA().getPosition();
- Position end = cable.getModel().getB().getPosition();
- float currentEnergy = cable.getFlowEnergy();
- float capacity = cable.getModel().getCapacity();
- g.setColor(Color.BLACK);
- g.setStroke(new BasicStroke(2));
- switch(cable.getState()) {
- case Burned:
- g.setColor(Color.RED);
- g.setStroke(new BasicStroke(2));
- break;
- case Working:
- g.setColor(Color.GREEN);
- g.setStroke(new BasicStroke((currentEnergy / capacity* 3) + 1));
- break;
- }
- g.drawLine(start.x, start.y, end.x, end.y);
- Position middle = new Position((start.x + end.x) / 2, (start.y + end.y) / 2);
- g.drawString(currentEnergy + "/" + capacity , middle.x, middle.y);
- }
- private void paintSwitch(Graphics2D g, DecoratedSwitch dSwitch)
- {
- drawCanvasObject(g, dSwitch.getState() == SwitchState.Open ? HolonSwitch.getSwitchOpenImage(): HolonSwitch.getSwitchClosedImage() , dSwitch.getModel().getPosition());
- }
- private void paintSupplyBar(Graphics2D g, float percentage, Color color, Position pos) {
- // +1, -2, -1 little Ajustment for pixelperfect allignment
- int barWidth = (int) (controller.getScale());
- int barHeight = (int) (controller.getScale() / 5);
- g.setColor(Color.WHITE);
- g.fillRect(pos.x - controller.getScaleDiv2(), pos.y + controller.getScaleDiv2() - 1, (int) barWidth, barHeight);
- g.setColor(color);
- g.fillRect(pos.x - controller.getScaleDiv2(), pos.y + controller.getScaleDiv2() - 1, (int) (barWidth * (percentage < 1 ? percentage : 1.0f) - 1), barHeight);
- g.setColor(Color.BLACK);
- g.setStroke(new BasicStroke(1));
- g.drawRect(pos.x - controller.getScaleDiv2(), pos.y + controller.getScaleDiv2() - 1, barWidth - 1 , barHeight);
- g.setFont(new Font("TimesRoman", Font.PLAIN, (int) (barHeight * 1.5) - 2));
- String percentageString = (Math.round((percentage * 100))) + "%";
- int stringWidth = (int) g.getFontMetrics().getStringBounds(percentageString, g).getWidth();
- if(percentage > 1.0f) g.setColor(Color.WHITE); //Just to see better on purple
- g.drawString(percentageString, pos.x + 1 - stringWidth / 2, pos.y + controller.getScaleDiv2() - 1+ barHeight);
-
- }
-
- //old code
- void drawMarker(Graphics2D g) {
- if (sx > x && sy > y) {
- g.drawRect(x, y, sx - x, sy - y);
- } else if (sx < x && sy < y) {
- g.drawRect(sx, sy, x - sx, y - sy);
- } else if (sx >= x) {
- g.drawRect(x, sy, sx - x, y - sy);
- } else if (sy >= y) {
- g.drawRect(sx, y, x - sx, sy - y);
- }
- }
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- Graphics2D g2d = (Graphics2D) g;
- g2d.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING,
- RenderingHints.VALUE_ANTIALIAS_ON));
- //-->Old code
- if (drawEdge) {
- g2d.setColor(Color.BLACK);
- g2d.setStroke(new BasicStroke(1));
- g2d.drawLine(tempCps.getPosition().x, tempCps.getPosition().y, x, y);
- }
- //<--
- g2d.setColor(Color.BLACK);
- for(DecoratedCable cable : controller.getSimManager().getDecorState().getLeftOverEdges()) {
- paintCable(g2d, cable);
- }
- for(DecoratedNetwork network : controller.getSimManager().getDecorState().getNetworkList()) {
- for(DecoratedCable cable : network.getDecoratedCableList()) {
- paintCable(g2d, cable);
- }
- for(Consumer con: network.getConsumerList()) {
- paintConsumer(g2d, con);
- }
- for(Consumer con: network.getConsumerSelfSuppliedList()) {
- paintConsumer(g2d, con);
- }
- for(Supplier sup: network.getSupplierList()) {
- paintCanvasObject(g2d, sup);
- }
- for(Passiv pas: network.getPassivNoEnergyList()) {
- paintCanvasObject(g2d, pas);
- }
- }
-
- for(DecoratedSwitch dSwitch : controller.getSimManager().getDecorState().getDecoratedSwitches()) {
- paintSwitch(g2d, dSwitch);
- }
- for(CpsNode node : controller.getSimManager().getMinimumModel().getNodeList()) {
- drawCanvasObject(g2d, "/Images/node_selected.png" , node.getPosition());
- }
- //oldCode
- if (doMark) {
- g2d.setColor(Color.BLACK);
- g2d.setStroke(new BasicStroke(0));
- drawMarker(g2d);
- }
- /*
- *
-
-
- for(HolonSwitch switch2 : controller.getSimManager().getMinimumModel().getSwitchList()) {
- drawCanvasObject(g2d, switch2.getSwitchClosedImage() , switch2.getPosition());
- }
- */
- // String maxCap = null;
- // // Rendering
- // g2 = (Graphics2D) g;
- // RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
- // RenderingHints.VALUE_ANTIALIAS_ON);
- // g2.setRenderingHints(rh);
- //
- // // Paint the Background
- // 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;
- // }
- // }
- //
- // // 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)));
- // }
- // if (showedInformation[3]) {
- // 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));
- // g2.drawLine(tempCps.getPosition().x, tempCps.getPosition().y, x, y);
- // }
- //
- // if(model.getEdgesOnCanvas().isEmpty() && !model.getObjectsOnCanvas().isEmpty()
- // && !model.getObjectsOnCanvas().get(0).getConnections().isEmpty()){
- // ArrayList<CpsEdge> edgesOnCanvas= model.getEdgesOnCanvas();
- // for(AbstractCpsObject cps :model.getObjectsOnCanvas())
- // for(CpsEdge e: cps.getConnections()){
- // if(!edgesOnCanvas.contains(e))
- // model.addEdgeOnCanvas(e);
- // }
- // }
- //
- // for (CpsEdge con : model.getEdgesOnCanvas()) {
- // maxCap = paintEdge(con, maxCap);
- // }
- //
- // // Highlighted Edge
- // if (!model.getSelectedCpsObjects().isEmpty() || !tempSelected.isEmpty() || model.getSelectedObjectID() > 0) {
- // g2.setColor(Color.BLUE);
- // for (CpsEdge con : model.getEdgesOnCanvas()) {
- // if (con.getFlow() <= con.getCapacity()) {
- // g2.setStroke(new BasicStroke(Math.min(
- // ((con.getFlow() / con.getCapacity() * 3) + 1), 4)));
- // } else {
- // g2.setStroke(new BasicStroke(2));
- // }
- //
- // maxCap = drawEdgeLine(con, maxCap);
- // }
- // } 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);
- //
- // maxCap = setCapacityString(edgeHighlight, maxCap);
- //
- // 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);
- // }
- // }
- //
- //
- // /**
- // * highlight the Object that would be replaced
- // */
- // highlightMayBeReplaced(g2);
- //
- // // Objects
- // for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
- // // Border Highlighting
- // if (showedInformation[3]) {
- // g2.setColor(cps.getBorderColor());
- // if (g2.getColor() != Color.WHITE && !(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)));
- // }
- // }
- //
- // setEdgePictureAndHighlighting(cps);
- //
- // g2.drawImage(img, cps.getPosition().x - controller.getScaleDiv2(),
- // cps.getPosition().y - controller.getScaleDiv2(),
- // controller.getScale(), controller.getScale(), null);
- //
- // paintSupplyBar(g, cps);
- // }
- //
- // // Dragged marker Highlighting
- // if (doMark) {
- // g2.setColor(Color.BLACK);
- // g2.setStroke(new BasicStroke(0));
- // drawMarker();
- // }
- // // Tooltip
- // showTooltip(g);
- }
- @Override
- public void mouseClicked(MouseEvent e) {
- if (e.getButton() == MouseEvent.BUTTON1) {
- DefaulTable propertyTable = model.getPropertyTable();
- if (propertyTable.getRowCount() > 0) {
- for (int i = propertyTable.getRowCount() - 1; i > -1; i--) {
- propertyTable.removeRow(i);
- }
- }
- triggerUpdateController();
- }
-
- stopEditing();
- }
- @Override
- public void mouseEntered(MouseEvent e) {
- }
- @Override
- public void mouseExited(MouseEvent e) {
- }
- @Override
- public void mousePressed(MouseEvent e) {
- stopEditing();
- tempCps = null;
- edgeHighlight = null;
- controller.setSelecteEdge(null);
- // Object Selection
- 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;
- setConsoleTextAfterSelect(cps);
- dragging = true;
- if (e.isControlDown() && tempCps != null) {
- if (model.getSelectedCpsObjects().contains(tempCps)) {
- controller.deleteSelectedObject(tempCps);
- //TODO: RemoveDepth
- } else {
- controller.addSelectedObject(tempCps);
- if(tempCps instanceof CpsUpperNode)
- controller.getObjectsInDepth();
- }
- }
- // If drawing an Edge (CTRL down)
- if (tempCps.getClass() == HolonObject.class) {
- HolonObject tempObj = ((HolonObject) tempCps);
- dataSelected = tempObj.getElements();
- }
- if (e.isShiftDown()) {
- drawEdge = true;
- dragging = false;
- }
- }
- }
- // Edge Selection
- 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) {
- try {
- /**
- * Save before further Dragged interactions happen
- */
- controller.autoSave();
-
- } catch (IOException ex) {
- System.err.println("AutoSave error by dragging");
- ex.printStackTrace();
- }
-
- /**
- * check if a unique tempCps could replace an Object on the canvas
- */
- if(model.getSelectedCpsObjects().size()==1
- && checkForReplacement(model.getObjectsOnCanvas(), tempCps, tempCps.getPosition().x, tempCps.getPosition().y)){
- /**
- * if UpperNode would be replaced, close its tabs
- */
- if(mayBeReplaced instanceof CpsUpperNode)
- closeUpperNodeTab(mayBeReplaced.getId());
- /**
- * replace on canvas (will save)
- */
- controller.replaceCanvasObject(mayBeReplaced, tempCps);
- mayBeReplaced=null;
- }
- }
- if (!e.isControlDown() && !dragged && tempCps != null
- && MouseEvent.BUTTON3 != e.getButton()) {
- model.getSelectedCpsObjects().clear();
- controller.addSelectedObject(tempCps);
- model.setSelectedCpsObject(tempCps);
- if(tempCps instanceof CpsUpperNode)
- controller.getObjectsInDepth();
- }
- dragged = false;
- // Rightclick List
- setRightClickMenu(e);
- markObjects();
- if (doubleClick() && tempCps instanceof HolonSwitch
- && MouseEvent.BUTTON3 != e.getButton()&& tempCps != null) {
- ((HolonSwitch) tempCps).switchState();
- }
- controller.calculateStateForTimeStep(model.getCurIteration());
- triggerUpdateController();
- repaint();
- }
- @Override
- public void mouseDragged(MouseEvent e) {
- // If Edge is drawn
- x = e.getX();
- y = e.getY();
-
- if (!model.getSelectedCpsObjects().contains(tempCps) && !doMark) {
- model.getSelectedCpsObjects().clear();
- if (tempCps != null) {
- controller.addSelectedObject(tempCps);
- }
- }
- if (dragging) {
- try {
- dragged = true;
- float xDist, yDist; // Distance
- x = e.getX();
- y = e.getY();
- // Make sure its in bounds
- int scaleDiv2 = controller.getScaleDiv2();
- if (e.getX() < scaleDiv2)
- x = scaleDiv2;
- else if (e.getX() > this.getWidth() - scaleDiv2)
- x = this.getWidth() - scaleDiv2;
- if (e.getY() < scaleDiv2)
- y = scaleDiv2;
- else if (e.getY() > this.getHeight()
- - scaleDiv2)
- y = this.getHeight() - scaleDiv2;
- // Distance
- xDist = x - tempCps.getPosition().x;
- yDist = y - tempCps.getPosition().y;
- tempCps.setPosition(x, y); // Drag Position
- // ToolTipText Position and name
- toolTip = true;
- toolTipText = tempCps.getName() + ", " + tempCps.getId();
- toolTipPos.x = tempCps.getPosition().x
- - scaleDiv2;
- toolTipPos.y = tempCps.getPosition().y
- + scaleDiv2;
- // 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 <= scaleDiv2)
- x = scaleDiv2;
- else if (x > this.getWidth()
- - scaleDiv2)
- x = this.getWidth() - scaleDiv2;
- if (y <= scaleDiv2)
- y = scaleDiv2;
- else if (y > this.getHeight()
- - scaleDiv2)
- y = this.getHeight() - scaleDiv2;
- cps.setPosition(x, y);
- }
- }
-
- /**
- * check if something might be replaced
- */
- if(model.getSelectedCpsObjects().size()==1)
- checkForReplacement(model.getObjectsOnCanvas(), tempCps, x, y);
-
- repaint();
- } catch (Exception eex) {
- }
- }
- // Mark Objects
- 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();
- // Everything for the tooltip :)
- boolean on = false;
- for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
- cx = cps.getPosition().x - controller.getScaleDiv2();
- cy = cps.getPosition().y - controller.getScaleDiv2();
- on = setToolTipInfoAndPosition(on, cps);
- }
- if(on||(!on && toolTip))
- repaint();
- toolTip = on;
- }
- /**
- * Draws or Deletes an Edge.
- */
- void drawDeleteEdge() {
- if (getMousePosition() != null) {
- 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);
- // 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;
- }
- } else {
- e = new CpsEdge(cps, tempCps, model.getMaxCapacity());
- controller.addEdgeOnCanvas(e);
- }
- }
- }
- // Edge auf eine Edge gezogen?
- if (onEdge && !checkForReplacement(x, y)) {
- CpsEdge p = mousePositionOnEdge(x, y);
- if (p != null) {
- CpsEdge e1;
- CpsEdge e2;
- 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);
- }
- }else{
- mayBeReplaced = null;
- }
- // ins leere Gedragged
- if (node && !checkForReplacement(x, y)) {
- CpsNode n = new CpsNode("Node");
- n.setPosition(x, y);
-
- controller.addObjectCanvas(n);
- e = new CpsEdge(n, tempCps, model.getMaxCapacity());
- controller.addEdgeOnCanvas(e);
- }else{
- mayBeReplaced = null;
- }
- // 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
- */
- private CpsEdge mousePositionOnEdge(int x, int y) {
- x += controller.getScaleDiv2();
- y += controller.getScaleDiv2();
- 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);
- int[] positions = determineMousePositionOnEdge(p);
- int lx = positions[0];
- int ly = positions[1];
- int hx = positions[2];
- int hy = positions[3];
- // distance from a point to a line and between both Objects
- if (l.ptLineDistSq(x - model.getScaleDiv2(),
- y - model.getScaleDiv2()) < 20
- && x > lx && x < hx && y > ly && y < hy) {
- return p;
- }
- }
- return null;
- }
- 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]);
- }
- /**
- * Set if Information should be shown.
- *
- * @param connection
- * boolean for conecction
- * @param object
- * boolean for objects
- * @param nodeOfnode
- */
- void setShowedInformation(boolean connection, boolean object,
- boolean border, boolean nodeOfnode) {
- showedInformation[0] = connection;
- showedInformation[1] = object;
- showedInformation[3] = border;
- showedInformation[4] = nodeOfnode;
- }
- /**
- * Returns if Information should be shown.
- *
- * @return Array of boolean [0] = connection, [1] = objects
- */
- boolean[] getShowedInformation() {
- return showedInformation;
- }
- /**
- * set toolTip
- *
- * @param bool
- */
- void setToolTip(boolean bool) {
- this.toolTip = bool;
- }
- /**
- * Set the Mouse
- *
- * @param x
- * @param y
- */
- void setXY(int x, int y) {
- this.x = x;
- this.y = y;
- }
- @Override
- public boolean checkForReplacement(int x, int y) {
-
- return checkForReplacement(model.getObjectsOnCanvas(), null, x, y);
- }
-
- @Override
- public void tryToAlignObjects(){
- /**
- * Align all Objects
- */
- for(AbstractCpsObject cps: model.getObjectsOnCanvas())
- align(cps,3*model.getScaleDiv2());
-
- /**
- * AutoSave new Positons
- */
- try{
- controller.autoSave();
- } catch (IOException ex) {
- System.err.println("AutoSave error by aligning");
- ex.printStackTrace();
- }
- }
-
-
- @Override
- public void closeUpperNodeTab(int upperNodeId) {
- JTabbedPane tabbedPaneInner = (JTabbedPane) getParent()
- .getParent().getParent().getParent();
- for (int i = 1; i < tabbedPaneInner.getTabCount(); i++) {
- if (((UpperNodeCanvas) ((JScrollPane) tabbedPaneInner
- .getComponentAt(i)).getViewport().getComponent(
- 0)).upperNode.getId() == upperNodeId) {
- tabbedPaneInner.remove(i);
- break;
- }
- }
- }
- }
|