123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170 |
- package ui.view;
- import classes.*;
- import com.google.gson.JsonParseException;
- import ui.controller.Control;
- import ui.controller.UpdateController;
- import ui.model.IntermediateCableWithState;
- import ui.model.Consumer;
- import ui.model.DecoratedCable;
- import ui.model.DecoratedGroupNode;
- import ui.model.DecoratedHolonObject;
- import ui.model.DecoratedHolonObject.HolonObjectState;
- import ui.model.DecoratedNetwork;
- import ui.model.DecoratedSwitch;
- import ui.model.DecoratedSwitch.SwitchState;
- import utility.ImageImport;
- import ui.model.Model;
- import ui.model.Passiv;
- import ui.model.Supplier;
- import ui.model.VisualRepresentationalState;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.datatransfer.UnsupportedFlavorException;
- import java.awt.event.ActionEvent;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- 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.awt.image.BufferedImage;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.stream.Collectors;
- /**
- * 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;
- public boolean disabled = false;
- /**
- * 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;
-
- showConnectionInformation = 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 (AbstractCanvasObject 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 (AbstractCanvasObject 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("GroupNode", null, animCps);
- controller.calculateStateAndVisualForCurrentTimeStep();
- triggerUpdateController();
- model.getSelectedCpsObjects().clear();
- repaint();
- }
- });
- animT.start();
- });
- itemUngroup
- .addActionListener(actionEvent -> {
- // save old Position
- int upperNodeId = tempCps.getId();
-
- closeUpperNodeTab(upperNodeId);
- savePos = new ArrayList<>();
- animCps = ((GroupNode) tempCps).getNodes();
- controller.ungroupGroupNode((GroupNode) 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 (AbstractCanvasObject 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 -> {
- model.getSelectedCpsObjects().clear();
- 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.calculateStateAndVisualForCurrentTimeStep();
- triggerUpdateController();
- repaint();
- }
- });
- animT.start();
- });
- // adds the selected object(s) to the statistic panel
- itemTrack.addActionListener(actionEvent -> {
- for (AbstractCanvasObject o : model.getSelectedCpsObjects()) {
- if (!((controller.getTrackingObj() != null) && (controller.getTrackingObj().contains(o)))) {
- controller.addTrackingObj(o);
- if (o instanceof HolonObject) {
- ((HolonObject) o).updateTrackingInfo();
- }
- }
- }
- });
- itemUntrack.addActionListener(actionEvent -> {
- for (AbstractCanvasObject o : model.getSelectedCpsObjects()) {
- if (o instanceof HolonObject) {
- boolean found = false;
- if (controller.getTrackingObj() != null) {
- for (AbstractCanvasObject 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]);
- }
- }
- }
- }) ;
- 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() == Node.class && edgeHighlight.getA().getConnections().size() == 0){
- controller.delCanvasObject(edgeHighlight.getA(), false);
- }
- if(edgeHighlight.getB().getClass() == Node.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++) {
- AbstractCanvasObject 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 GroupNode) {
- JTabbedPane tabbedPane = (JTabbedPane)getParent().getParent()
- .getParent().getParent();
- // Look if the uppernode is open in a Tab
- for (int i = 4; i < tabbedPane.getTabCount(); i++) {
- if (tabbedPane.getComponentAt(i) != null
- && ((GroupNodeCanvas) ((JScrollPane) tabbedPane
- .getComponentAt(i)).getViewport()
- .getComponent(0)).upperNode.getId() == cps
- .getId()) {
- ((ButtonTabComponent) tabbedPane
- .getTabComponentAt(i)).removeTabs();
- break;
- }
- }
- }
- toolTip = false;
- }
- model.getSelectedCpsObjects().clear();
- tempCps = null;
- repaint();
- });
- itemCut.addActionListener(actionEvent -> {
- controller.cut(null);
- itemPaste.setEnabled(true);
- repaint();
- });
- itemCopy.addActionListener(actionEvent -> {
- if(tempCps instanceof GroupNode)
- controller.getObjectsInDepth();
- controller.copy(null);
- itemPaste.setEnabled(true);
- repaint();
- });
- itemAlign.addActionListener(actionEvent ->
- {
- for(AbstractCanvasObject 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(166, 78, 229);
- case PARTIALLY_SUPPLIED:
- return Color.yellow;
- case PRODUCER:
- return Color.lightGray;
- case SUPPLIED:
- return new Color(13, 175, 28);
- 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 drawCanvasObjectString(Graphics2D g, Position posOfCanvasObject, float energy) {
- g.setColor(Color.BLACK);
- g.setFont(new Font("TimesNewRoman", Font.PLAIN, (int) (controller.getScale() / 4f) ));
- g.drawString((energy > 0)? "+" + Float.toString(energy): Float.toString(energy), posOfCanvasObject.x - controller.getScaleDiv2(), posOfCanvasObject.y - controller.getScaleDiv2() - 1);
- }
- private void paintConsumer(Graphics2D g, Consumer con){
- paintCanvasObject(g, con);
- paintSupplyBar(g,con.getSupplyBarPercentage(), getStateColor(con.getState()), con.getModel().getPosition());
- drawCanvasObjectString(g, con.getModel().getPosition(), -con.getEnergyNeededFromNetwork());
- }
- private void paintSupplier(Graphics2D g, Supplier sup){
- paintCanvasObject(g, sup);
- drawCanvasObjectString(g, sup.getModel().getPosition(), sup.getEnergyToSupplyNetwork());
- }
-
- private void drawCanvasObject(Graphics2D g, String Image, Position pos) {
- g.drawImage(ImageImport.loadImage(Image, controller.getScale(), controller.getScale()) ,
- pos.x - controller.getScaleDiv2(),
- pos.y - controller.getScaleDiv2(),
- controller.getScale(), controller.getScale() , null);
- }
-
- private void paintCable(Graphics2D g, DecoratedCable cable, boolean isSelected)
- {
- Position start = cable.getModel().getA().getPosition();
- Position end = cable.getModel().getB().getPosition();
- float currentEnergy = cable.getFlowEnergy();
- float capacity = cable.getModel().getCapacity();
- boolean unlimited = cable.getModel().isUnlimitedCapacity();
- switch(cable.getState()) {
- case Burned:
- g.setColor(Color.RED);
- g.setStroke(new BasicStroke(2));
- break;
- case Working:
- g.setColor(new Color(13, 175, 28));
- g.setStroke(new BasicStroke(unlimited?2f:(currentEnergy / capacity * 2f) + 1));
- break;
- }
- if(isSelected){
- g.setColor(Color.lightGray);
- }
- g.drawLine(start.x, start.y, end.x, end.y);
- if(showConnectionInformation) {
- Position middle = new Position((start.x + end.x) / 2, (start.y + end.y) / 2);
- g.setFont(new Font("TimesRoman", Font.PLAIN, Math.max((int) (controller.getScale() / 3.5f), 10) ));
- g.drawString(currentEnergy + "/" + (unlimited?"\u221E":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 paintExitCable(Graphics2D g, ExitCable eCable) {
- Position start = eCable.getStart().getPosition();
- Position end = eCable.getFinish().getPosition();
- float currentEnergy = eCable.getCable().getFlowEnergy();
- float capacity = eCable.getCable().getModel().getCapacity();
- boolean unlimited = eCable.getCable().getModel().isUnlimitedCapacity();
- switch(eCable.getCable().getState()) {
- case Burned:
- g.setColor(Color.RED);
- g.setStroke(new BasicStroke(2));
- break;
- case Working:
- g.setColor(new Color(13, 175, 28));
- g.setStroke(new BasicStroke(unlimited?2f:(currentEnergy / capacity * 2f) + 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.setFont(new Font("TimesRoman", Font.PLAIN, Math.max((int) (controller.getScale() / 3.5f), 10) ));
- g.drawString(currentEnergy + "/" + (unlimited?"\u221E":capacity) , middle.x, middle.y);
- }
- private void paintGroupNode(Graphics2D g, DecoratedGroupNode dGroupNode) {
- Position pos = dGroupNode.getModel().getPosition();
- g.setColor(Color.lightGray);
- g.fillRect(pos.x - controller.getScaleDiv2(), pos.y - controller.getScaleDiv2(), controller.getScale(), controller.getScale());
- drawCanvasObject(g, "/Images/upper_node.png" , pos);
- paintGroupNodeBar(g, dGroupNode, pos);
- }
- private void paintGroupNodeBar(Graphics2D g, DecoratedGroupNode dGroupNode , Position pos) {
- // +1, -2, -1 little Adjustment for pixel perfect alignment
- 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);
- float[] percentages = getGroupNodeBarPercentages(dGroupNode);
- Color[] colors = new Color[6];
- colors[0] = getStateColor(HolonObjectState.PRODUCER);
- colors[1] = getStateColor(HolonObjectState.NOT_SUPPLIED);
- colors[2] = getStateColor(HolonObjectState.PARTIALLY_SUPPLIED);
- colors[3] = getStateColor(HolonObjectState.SUPPLIED);
- colors[4] = getStateColor(HolonObjectState.OVER_SUPPLIED);
- colors[5] = getStateColor(HolonObjectState.NO_ENERGY);
-
- for(int i = 5; i>=0; i--) {
- g.setColor(colors[i]);
- g.fillRect(pos.x - controller.getScaleDiv2(), pos.y + controller.getScaleDiv2() - 1, (int) (barWidth * percentages[i] - 1), 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);
- }
- /**
- * HardCoded Stuff dont try at Home ;)
- * @param dGroupNode
- * @return
- */
- public float[] getGroupNodeBarPercentages(DecoratedGroupNode dGroupNode) {
- int[] amountOfObjects = new int[6];
- amountOfObjects[0] = dGroupNode.getAmountOfSupplier();
- amountOfObjects[1] = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.NOT_SUPPLIED);
- amountOfObjects[2] = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.PARTIALLY_SUPPLIED);
- amountOfObjects[3] = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.SUPPLIED);
- amountOfObjects[4] = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.OVER_SUPPLIED);
- amountOfObjects[5] = dGroupNode.getAmountOfPassiv();
- int countHolonObjects = amountOfObjects[0] + amountOfObjects[1] + amountOfObjects[2] + amountOfObjects[3] + amountOfObjects[4] + amountOfObjects[5];
- float[] percentages = new float[6];
- int count = 0;
- for(int i = 0; i < 6; i++) {
- count += amountOfObjects[i];
- percentages[i] = (float)count / (float)countHolonObjects;
- }
- return percentages;
- }
- private void paintSupplyBar(Graphics2D g, float percentage, Color color, Position pos) {
- // +1, -2, -1 little Adjustment for pixel perfect alignment
- 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("TimesNewRoman", 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) {
- Color transparentGrey = new Color(128, 174, 247, 40);
- if (sx > x && sy > y) {
- g.drawRect(x, y, sx - x, sy - y);
- g.setColor(transparentGrey);
- g.fillRect(x, y, sx - x, sy - y);
- } else if (sx < x && sy < y) {
- g.drawRect(sx, sy, x - sx, y - sy);
- g.setColor(transparentGrey);
- g.fillRect(sx, sy, x - sx, y - sy);
- } else if (sx >= x) {
- g.drawRect(x, sy, sx - x, y - sy);
- g.setColor(transparentGrey);
- g.fillRect(x, sy, sx - x, y - sy);
- } else if (sy >= y) {
- g.drawRect(sx, y, x - sx, sy - y);
- g.setColor(transparentGrey);
- g.fillRect(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);
- }
- //<--
- //SelectedCable
- HashSet<Edge> selectedEdges = new HashSet<Edge>();
- for(AbstractCanvasObject aCps: model.getSelectedCpsObjects()) {
- for(Edge edge: aCps.getConnections()) {
- selectedEdges.add(edge);
- }
- }
- if(model.getSelectedEdge() != null) selectedEdges.add(model.getSelectedEdge());
- //timstep:
- g.setFont(new Font("TimesNewRoman", Font.PLAIN, Math.max((int) (controller.getScale() / 3.5f), 10) ));
- g2d.setColor(Color.BLACK);
-
- VisualRepresentationalState visualState = controller.getSimManager().getActualVisualRepresentationalState();
- //VisualState Representation:
- if(visualState == null) System.out.println("AHHH");
- for(ExitCable cable : visualState.getExitCableList()) {
- paintExitCable(g2d, cable);
- }
- for(DecoratedCable cable : visualState.getCableList()) {
- paintCable(g2d, cable, selectedEdges.contains(cable.getModel()));
- }
- for(DecoratedGroupNode dGroupNode : visualState.getGroupNodeList()) {
- paintGroupNode(g2d, dGroupNode);
- }
- for(Consumer con: visualState.getConsumerList()) {
- paintConsumer(g2d, con);
- }
- for(Supplier sup: visualState.getSupplierList()) {
- paintSupplier(g2d, sup);
- }
- for(Passiv pas: visualState.getPassivList()) {
- paintCanvasObject(g2d, pas);
- }
- for(DecoratedSwitch dSwitch : visualState.getSwitchList()) {
- paintSwitch(g2d, dSwitch);
- }
- for(Node node : visualState.getNodeList()) {
- drawCanvasObject(g2d, "/Images/node.png" , node.getPosition());
- }
-
- //-->oldCode
- if (doMark) {
- g2d.setColor(Color.BLACK);
- g2d.setStroke(new BasicStroke(0));
- drawMarker(g2d);
- }
- //Test Selection
- //Objects:
- g2d.setColor(Color.BLUE);
- g2d.setStroke(new BasicStroke(1));
- Color transparentGrey = new Color(128, 174, 247, 40);
- for(AbstractCanvasObject aCps: model.getSelectedCpsObjects()) {
- if(aCps instanceof Node) {
- Position pos = aCps.getPosition();
- g2d.setColor(transparentGrey);
- g2d.fillOval(pos.x - (int) (controller.getScaleDiv2()), pos.y - (int) (controller.getScaleDiv2()), controller.getScale(), controller.getScale());
- g2d.setColor(Color.LIGHT_GRAY);
- g2d.setStroke(new BasicStroke(2));
- g2d.drawOval(pos.x - (int) (controller.getScaleDiv2()), pos.y - (int) (controller.getScaleDiv2()), controller.getScale(), controller.getScale());
- }
- else {
- Position pos = aCps.getPosition();
- g2d.setColor(transparentGrey);
- g2d.fillRect(pos.x - (int) (controller.getScaleDiv2()* 1.5f), pos.y - (int) (controller.getScaleDiv2()* 1.5f), (int) (controller.getScale()* 1.5f) , (int) (controller.getScale()* 1.5f));
- g2d.setColor(Color.LIGHT_GRAY);
- g2d.setStroke(new BasicStroke(2));
- g2d.drawRect(pos.x - (int) (controller.getScaleDiv2()* 1.5f), pos.y - (int) (controller.getScaleDiv2()* 1.5f), (int) (controller.getScale()* 1.5f) , (int) (controller.getScale()* 1.5f));
- }
- }
- //maybeReplace:
- if(mayBeReplaced != null){
- g2d.setColor(Color.RED);
- Position pos = mayBeReplaced.getPosition();
- g2d.drawImage(ImageImport.loadImage("/Images/replace.png") ,
- pos.x + controller.getScaleDiv2(),
- pos.y - controller.getScale(),
- controller.getScaleDiv2(), controller.getScaleDiv2(), null);
- }
- //<-- OldCode
- }
- @Override
- public void mouseClicked(MouseEvent e) {
- if(!disabled){
- 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) {
- if(!disabled){
- stopEditing();
- tempCps = null;
- edgeHighlight = null;
- controller.setSelecteEdge(null);
- // Object Selection
- for (AbstractCanvasObject 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;
- dragging = true;
- if (e.isControlDown() && tempCps != null) {
- if (model.getSelectedCpsObjects().contains(tempCps)) {
- controller.deleteSelectedObject(tempCps);
- //TODO: RemoveDepth
- } else {
- controller.addSelectedObject(tempCps);
- if(tempCps instanceof GroupNode)
- 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) {
- if(!disabled){
- 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 GroupNode)
- 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 GroupNode)
- controller.getObjectsInDepth();
- }
- dragged = false;
- // Rightclick List
- setRightClickMenu(e);
- markObjects();
- if (doubleClick() && tempCps instanceof HolonSwitch
- && MouseEvent.BUTTON3 != e.getButton()&& tempCps != null) {
- ((HolonSwitch) tempCps).switchState();
- }
- controller.calculateStateAndVisualForTimeStep(model.getCurIteration());
- triggerUpdateController();
- repaint();
- }
- }
- @Override
- public void mouseDragged(MouseEvent e) {
- if(!disabled){
- // 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 (AbstractCanvasObject 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 (AbstractCanvasObject 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) {
- if(!disabled){
- x = e.getX();
- y = e.getY();
- // Everything for the tooltip :)
- boolean on = false;
- for (AbstractCanvasObject 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;
- Edge e = null;
- for (AbstractCanvasObject 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 (Edge 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() == Node.class
- && e.getA().getConnections().isEmpty()) {
- tempCps = e.getA();
- deleteNode = true;
- }
- if (e.getB().getClass() == Node.class
- && e.getB().getConnections().isEmpty()) {
- deleteNode = true;
- }
- } else {
- if(!(cps instanceof GroupNode || tempCps instanceof GroupNode)) {
- e = new Edge(cps, tempCps, model.getMaxCapacity());
- controller.addEdgeOnCanvas(e);
- }else if (cps instanceof GroupNode && !(tempCps instanceof GroupNode)){
- GroupNode thisUpperNode = (GroupNode)cps;
- Object[] possibilities = thisUpperNode.getNodes().stream().map(aCps -> new ACpsHandle(aCps)).filter(aCpsHandle -> !(aCpsHandle.object instanceof GroupNode)).toArray();
- if(possibilities.length != 0) {
- ACpsHandle selected = (ACpsHandle) JOptionPane.showInputDialog(this, "Select a inside Object:", "Connection To?", JOptionPane.OK_OPTION,new ImageIcon(new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB)) , possibilities, "");
- if(selected != null) {
- e = new Edge(selected.object, tempCps, model.getMaxCapacity());
- controller.addEdgeOnCanvas(e);
- }
- }else {
- Node n = new Node("Node");
- n.setPosition(x, y);
- controller.addObjUpperNode(n, thisUpperNode);
- e = new Edge(n, tempCps, model.getMaxCapacity());
- controller.addEdgeOnCanvas(e);
- }
-
- }
-
- }
- }
- }
- // Edge auf eine Edge gezogen?
- if (onEdge && !checkForReplacement(x, y)) {
- Edge p = mousePositionOnEdge(x, y);
- if (p != null) {
- Edge e1;
- Edge e2;
- node = false;
- Node n = new Node("Node");
- n.setPosition(x, y);
- controller.addObjectCanvas(n);
- AbstractCanvasObject r, k;
- r = p.getA();
- k = p.getB();
- e = new Edge(n, tempCps, model.getMaxCapacity());
- e1 = new Edge(n, r, model.getMaxCapacity());
- e2 = new Edge(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)) {
- Node n = new Node("Node");
- n.setPosition(x, y);
-
- controller.addObjectCanvas(n);
- e = new Edge(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 Edge mousePositionOnEdge(int x, int y) {
- x += controller.getScaleDiv2();
- y += controller.getScaleDiv2();
- for (Edge 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) {
- showConnectionInformation = 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(AbstractCanvasObject 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 (((GroupNodeCanvas) ((JScrollPane) tabbedPaneInner
- .getComponentAt(i)).getViewport().getComponent(
- 0)).upperNode.getId() == upperNodeId) {
- tabbedPaneInner.remove(i);
- break;
- }
- }
- }
-
-
-
- }
|