MyCanvas.java 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  1. package ui.view;
  2. import classes.*;
  3. import com.google.gson.JsonParseException;
  4. import ui.controller.Control;
  5. import ui.controller.UpdateController;
  6. import ui.model.IntermediateCableWithState;
  7. import ui.model.Consumer;
  8. import ui.model.DecoratedCable;
  9. import ui.model.DecoratedGroupNode;
  10. import ui.model.DecoratedHolonObject;
  11. import ui.model.DecoratedHolonObject.HolonObjectState;
  12. import ui.model.DecoratedNetwork;
  13. import ui.model.DecoratedSwitch;
  14. import ui.model.DecoratedSwitch.SwitchState;
  15. import utility.ImageImport;
  16. import ui.model.Model;
  17. import ui.model.Passiv;
  18. import ui.model.Supplier;
  19. import ui.model.VisualRepresentationalState;
  20. import javax.swing.*;
  21. import java.awt.*;
  22. import java.awt.datatransfer.UnsupportedFlavorException;
  23. import java.awt.event.ActionEvent;
  24. import java.awt.event.KeyEvent;
  25. import java.awt.event.KeyListener;
  26. import java.awt.event.MouseEvent;
  27. import java.awt.event.MouseListener;
  28. import java.awt.event.MouseMotionListener;
  29. import java.awt.font.LineMetrics;
  30. import java.awt.geom.AffineTransform;
  31. import java.awt.geom.Line2D;
  32. import java.awt.image.BufferedImage;
  33. import java.io.IOException;
  34. import java.util.ArrayList;
  35. import java.util.HashMap;
  36. import java.util.HashSet;
  37. import java.util.stream.Collectors;
  38. /**
  39. * This Class is the Canvas. All Objects will be visualized here
  40. *
  41. * @author Gruppe14
  42. */
  43. public class MyCanvas extends AbstractCanvas implements MouseListener,
  44. MouseMotionListener {
  45. private static final long serialVersionUID = 1L;
  46. public boolean disabled = false;
  47. /**
  48. * Constructor.
  49. *
  50. * @param mod
  51. * the Model
  52. * @param control
  53. * the Controller
  54. * @param unitGraph
  55. */
  56. public MyCanvas(Model mod, Control control, UnitGraph unitGraph) {
  57. toolTip = false;
  58. this.controller = control;
  59. this.model = mod;
  60. scalediv20 = model.getScale() / 20;
  61. showConnectionInformation = true;
  62. showedInformation[1] = true;
  63. showedInformation[3] = false;
  64. showedInformation[4] = true;
  65. control.setMaxCapacity(10000);
  66. popmenu.add(itemCut);
  67. popmenu.add(itemCopy);
  68. popmenu.add(itemPaste);
  69. popmenu.add(itemDelete);
  70. popmenu.add(itemAlign);
  71. popmenu.addSeparator();
  72. popmenu.add(itemGroup);
  73. popmenu.add(itemUngroup);
  74. popmenu.add(itemTrack);
  75. popmenu.add(itemUntrack);
  76. popmenu.add(itemCreateTemplate);
  77. updCon = new UpdateController(mod, control);
  78. itemDelete.setEnabled(false);
  79. itemCut.setEnabled(false);
  80. itemCopy.setEnabled(false);
  81. itemPaste.setEnabled(true);
  82. itemAlign.setEnabled(false);
  83. itemGroup.setEnabled(false);
  84. itemUngroup.setEnabled(false);
  85. itemTrack.setEnabled(false);
  86. itemUntrack.setEnabled(false);
  87. itemCut.setText(Languages.getLanguage()[95]);
  88. itemGroup.addActionListener(actionEvent -> {
  89. // calculate uppernode pos (taken from the controller)
  90. unPos = new Position(0, 0);
  91. animCps = new ArrayList<>();
  92. for (AbstractCanvasObject cps : model.getSelectedCpsObjects()) {
  93. animCps.add(cps); // add to animation Cps ArrayList
  94. unPos.x += cps.getPosition().x;
  95. unPos.y += cps.getPosition().y;
  96. }
  97. unPos.x /= animCps.size();
  98. unPos.y /= animCps.size();
  99. // save old Position
  100. savePos = new ArrayList<>();
  101. for (int i = 0; i < animCps.size(); i++) {
  102. savePos.add(new Position(0, 0));
  103. savePos.get(i).x = animCps.get(i).getPosition().x;
  104. savePos.get(i).y = animCps.get(i).getPosition().y;
  105. }
  106. animT = new javax.swing.Timer(animDelay, actionEvent1 -> {
  107. if (animDuration - animDelay > 0 && animCps.size() > 1) {
  108. for (AbstractCanvasObject animCpObject : animCps) {
  109. double x1 = animCpObject.getPosition().x - unPos.x;
  110. double y1 = animCpObject.getPosition().y - unPos.y;
  111. animCpObject.getPosition().x -= x1 / animSteps;
  112. animCpObject.getPosition().y -= y1 / animSteps;
  113. }
  114. repaint();
  115. animDuration -= animDelay;
  116. animSteps--;
  117. } else {
  118. animDuration = ANIMTIME;
  119. animSteps = animDuration / animDelay;
  120. animT.stop();
  121. for (int i = 0; i < animCps.size(); i++) {
  122. animCps.get(i).getPosition().x = savePos.get(i).x;
  123. animCps.get(i).getPosition().y = savePos.get(i).y;
  124. }
  125. controller.addUpperNode("GroupNode", null, animCps);
  126. controller.calculateStateAndVisualForCurrentTimeStep();
  127. triggerUpdateController();
  128. model.getSelectedCpsObjects().clear();
  129. repaint();
  130. }
  131. });
  132. animT.start();
  133. });
  134. itemUngroup
  135. .addActionListener(actionEvent -> {
  136. // save old Position
  137. int upperNodeId = tempCps.getId();
  138. closeUpperNodeTab(upperNodeId);
  139. savePos = new ArrayList<>();
  140. animCps = ((GroupNode) tempCps).getNodes();
  141. controller.ungroupGroupNode((GroupNode) tempCps, null);
  142. for (int i = 0; i < animCps.size(); i++) {
  143. savePos.add(new Position(0, 0));
  144. savePos.get(i).x = animCps.get(i).getPosition().x;
  145. savePos.get(i).y = animCps.get(i).getPosition().y;
  146. }
  147. for (AbstractCanvasObject cps : animCps) {
  148. int x = tempCps.getPosition().x;
  149. int y = tempCps.getPosition().y;
  150. cps.setPosition(new Position(x, y));
  151. }
  152. animT = new javax.swing.Timer(
  153. animDelay,
  154. actionEvent1 -> {
  155. model.getSelectedCpsObjects().clear();
  156. if (animDuration - animDelay >= 0) {
  157. for (int i = 0; i < animCps.size(); i++) {
  158. Position pos = animCps.get(i).getPosition();
  159. double x1 = pos.x - savePos.get(i).x;
  160. double y1 = pos.y - savePos.get(i).y;
  161. animCps.get(i).getPosition().x -= x1 / animSteps;
  162. animCps.get(i).getPosition().y -= y1 / animSteps;
  163. }
  164. repaint();
  165. animDuration -= animDelay;
  166. animSteps--;
  167. } else {
  168. animDuration = ANIMTIME;
  169. animSteps = animDuration / animDelay;
  170. animT.stop();
  171. for (int i = 0; i < animCps.size(); i++) {
  172. animCps.get(i).getPosition().x = savePos
  173. .get(i).x;
  174. animCps.get(i).getPosition().y = savePos
  175. .get(i).y;
  176. }
  177. controller.calculateStateAndVisualForCurrentTimeStep();
  178. triggerUpdateController();
  179. repaint();
  180. }
  181. });
  182. animT.start();
  183. });
  184. // adds the selected object(s) to the statistic panel
  185. itemTrack.addActionListener(actionEvent -> {
  186. for (AbstractCanvasObject o : model.getSelectedCpsObjects()) {
  187. if (!((controller.getTrackingObj() != null) && (controller.getTrackingObj().contains(o)))) {
  188. controller.addTrackingObj(o);
  189. if (o instanceof HolonObject) {
  190. ((HolonObject) o).updateTrackingInfo();
  191. }
  192. }
  193. }
  194. });
  195. itemUntrack.addActionListener(actionEvent -> {
  196. for (AbstractCanvasObject o : model.getSelectedCpsObjects()) {
  197. if (o instanceof HolonObject) {
  198. boolean found = false;
  199. if (controller.getTrackingObj() != null) {
  200. for (AbstractCanvasObject obj : controller
  201. .getTrackingObj()) {
  202. if (obj instanceof HolonObject) {
  203. if (obj.getId() == o.getId()) {
  204. found = true;
  205. }
  206. }
  207. }
  208. }
  209. if (found) {
  210. // Removed from tracking array and tracking
  211. // information reseted
  212. controller.removeTrackingObj(o);
  213. ((HolonObject) o).setTrackingProd(new float[100]);
  214. ((HolonObject) o).setTrackingCons(new float[100]);
  215. }
  216. }
  217. }
  218. }) ;
  219. itemDelete.addActionListener(actionEvent -> {
  220. // Remove the selected Object objects
  221. //Edge Deleting
  222. if (tempCps == null && edgeHighlight != null) {
  223. controller.removeEdgesOnCanvas(edgeHighlight);
  224. //Look for a CPSNode with no Connections and delete them
  225. if(edgeHighlight.getA().getClass() == Node.class && edgeHighlight.getA().getConnections().size() == 0){
  226. controller.delCanvasObject(edgeHighlight.getA(), false);
  227. }
  228. if(edgeHighlight.getB().getClass() == Node.class && edgeHighlight.getB().getConnections().size() == 0){ //Look on the other end of the cable
  229. controller.delCanvasObject(edgeHighlight.getB(), false);
  230. }
  231. edgeHighlight = null;
  232. }
  233. boolean save = false;
  234. for (int j = 0; j < model.getSelectedCpsObjects().size(); j++) {
  235. AbstractCanvasObject cps = model.getSelectedCpsObjects()
  236. .get(j);
  237. if (j == model.getSelectedCpsObjects().size() - 1)
  238. save = true;
  239. controller.delCanvasObject(cps, save);
  240. controller.removeTrackingObj(cps);
  241. // Remove UpperNodeTab if UpperNode deleted
  242. if (cps instanceof GroupNode) {
  243. JTabbedPane tabbedPane = (JTabbedPane)getParent().getParent()
  244. .getParent().getParent();
  245. // Look if the uppernode is open in a Tab
  246. for (int i = 4; i < tabbedPane.getTabCount(); i++) {
  247. if (tabbedPane.getComponentAt(i) != null
  248. && ((GroupNodeCanvas) ((JScrollPane) tabbedPane
  249. .getComponentAt(i)).getViewport()
  250. .getComponent(0)).upperNode.getId() == cps
  251. .getId()) {
  252. ((ButtonTabComponent) tabbedPane
  253. .getTabComponentAt(i)).removeTabs();
  254. break;
  255. }
  256. }
  257. }
  258. toolTip = false;
  259. }
  260. model.getSelectedCpsObjects().clear();
  261. tempCps = null;
  262. repaint();
  263. });
  264. itemCut.addActionListener(actionEvent -> {
  265. controller.cut(null);
  266. itemPaste.setEnabled(true);
  267. repaint();
  268. });
  269. itemCopy.addActionListener(actionEvent -> {
  270. if(tempCps instanceof GroupNode)
  271. controller.getObjectsInDepth();
  272. controller.copy(null);
  273. itemPaste.setEnabled(true);
  274. repaint();
  275. });
  276. itemAlign.addActionListener(actionEvent ->
  277. {
  278. for(AbstractCanvasObject cps: model.getSelectedCpsObjects())
  279. align(cps, model.getScaleDiv2());
  280. repaint();
  281. });
  282. itemPaste
  283. .addActionListener(actionEvent -> {
  284. try {
  285. controller.paste(null, mousePosition);
  286. unitGraph.update(model.getSelectedCpsObjects());
  287. } catch (JsonParseException | UnsupportedFlavorException
  288. | IOException e1) {
  289. JLabel message = new JLabel(
  290. "The Clipboard information cannot be pastet into Application.");
  291. JOptionPane.showMessageDialog(null, message, "",
  292. JOptionPane.ERROR_MESSAGE);
  293. }
  294. repaint();
  295. });
  296. /*
  297. * create Template
  298. */
  299. itemCreateTemplate.addActionListener(actionEvent -> {
  300. controller.createTemplate((HolonObject)tempCps,(JFrame)SwingUtilities.getRoot(this));
  301. });
  302. this.addMouseListener(this);
  303. this.addMouseMotionListener(this);
  304. }
  305. /**
  306. * Paints all Components on the Canvas.
  307. *
  308. * @param g
  309. * Graphics
  310. */
  311. private Color getStateColor(HolonObjectState state) {
  312. switch(state) {
  313. case NOT_SUPPLIED:
  314. return new Color(230, 120, 100);
  315. case NO_ENERGY:
  316. return Color.white;
  317. case OVER_SUPPLIED:
  318. return new Color(166, 78, 229);
  319. case PARTIALLY_SUPPLIED:
  320. return Color.yellow;
  321. case PRODUCER:
  322. return Color.lightGray;
  323. case SUPPLIED:
  324. return new Color(13, 175, 28);
  325. default:
  326. return Color.BLACK;
  327. }
  328. }
  329. private void paintCanvasObject(Graphics2D g, DecoratedHolonObject decoratedHolonObject){
  330. Position pos = decoratedHolonObject.getModel().getPosition();
  331. Color statecolor = getStateColor(decoratedHolonObject.getState());
  332. g.setColor(statecolor);
  333. g.fillRect(pos.x - controller.getScaleDiv2(), pos.y - controller.getScaleDiv2(), controller.getScale(), controller.getScale());
  334. drawCanvasObject(g, decoratedHolonObject.getModel().getImage(), pos);
  335. }
  336. private void drawCanvasObjectString(Graphics2D g, Position posOfCanvasObject, float energy) {
  337. g.setColor(Color.BLACK);
  338. g.setFont(new Font("TimesNewRoman", Font.PLAIN, (int) (controller.getScale() / 4f) ));
  339. g.drawString((energy > 0)? "+" + Float.toString(energy): Float.toString(energy), posOfCanvasObject.x - controller.getScaleDiv2(), posOfCanvasObject.y - controller.getScaleDiv2() - 1);
  340. }
  341. private void paintConsumer(Graphics2D g, Consumer con){
  342. paintCanvasObject(g, con);
  343. paintSupplyBar(g,con.getSupplyBarPercentage(), getStateColor(con.getState()), con.getModel().getPosition());
  344. drawCanvasObjectString(g, con.getModel().getPosition(), -con.getEnergyNeededFromNetwork());
  345. }
  346. private void paintSupplier(Graphics2D g, Supplier sup){
  347. paintCanvasObject(g, sup);
  348. drawCanvasObjectString(g, sup.getModel().getPosition(), sup.getEnergyToSupplyNetwork());
  349. }
  350. private void drawCanvasObject(Graphics2D g, String Image, Position pos) {
  351. g.drawImage(ImageImport.loadImage(Image, controller.getScale(), controller.getScale()) ,
  352. pos.x - controller.getScaleDiv2(),
  353. pos.y - controller.getScaleDiv2(),
  354. controller.getScale(), controller.getScale() , null);
  355. }
  356. private void paintCable(Graphics2D g, DecoratedCable cable, boolean isSelected)
  357. {
  358. Position start = cable.getModel().getA().getPosition();
  359. Position end = cable.getModel().getB().getPosition();
  360. float currentEnergy = Math.abs(cable.getFlowEnergy());
  361. float capacity = cable.getModel().getCapacity();
  362. boolean unlimited = cable.getModel().isUnlimitedCapacity();
  363. switch(cable.getState()) {
  364. case Burned:
  365. g.setColor(Color.RED);
  366. g.setStroke(new BasicStroke(2));
  367. break;
  368. case Working:
  369. g.setColor(new Color(13, 175, 28));
  370. g.setStroke(new BasicStroke(unlimited?2f:(currentEnergy / capacity * 2f) + 1));
  371. break;
  372. }
  373. if(isSelected){
  374. g.setColor(Color.lightGray);
  375. }
  376. if (cable.getFlowEnergy() > 2)
  377. drawArrow(g, start.x, start.y, end.x, end.y);
  378. else if (cable.getFlowEnergy() < -2)
  379. drawArrow(g, end.x, end.y, start.x, start.y);
  380. else
  381. g.drawLine(start.x, start.y, end.x, end.y);
  382. if(showConnectionInformation) {
  383. double dx = start.x - end.x, dy = start.y - end.y;
  384. double angle = Math.atan2(dy, dx);
  385. int offsetX = (int)(Math.sin(angle - Math.PI) * 15);
  386. int offsetY = (int)(Math.cos(angle - Math.PI) * 15);
  387. Position middle = new Position((start.x + end.x) / 2 + offsetX, (start.y + end.y) / 2 + offsetY);
  388. g.setFont(new Font("TimesRoman", Font.PLAIN, Math.max((int) (controller.getScale() / 3.5f), 10) ));
  389. g.drawString(String.format("%.1f / %s", currentEnergy, (unlimited?"\u221E":capacity)) , middle.x, middle.y);
  390. }
  391. }
  392. private void drawArrow(Graphics g1, int x1, int y1, int x2, int y2) {
  393. Graphics2D g = (Graphics2D) g1.create();
  394. double dx = x2 - x1, dy = y2 - y1;
  395. double angle = Math.atan2(dy, dx);
  396. int len = (int) Math.sqrt(dx * dx + dy * dy);
  397. AffineTransform at = AffineTransform.getTranslateInstance(x1, y1);
  398. at.concatenate(AffineTransform.getRotateInstance(angle));
  399. g.transform(at);
  400. final int ARR_SIZE = 15;
  401. final int offset = 30;
  402. len -= offset;
  403. g.drawLine(0, 0, len, 0);
  404. g.fillPolygon(new int[]{len, len - ARR_SIZE, len - ARR_SIZE, len}, new int[]{0, -ARR_SIZE / 2, ARR_SIZE / 2, 0}, 4);
  405. }
  406. private void paintSwitch(Graphics2D g, DecoratedSwitch dSwitch)
  407. {
  408. drawCanvasObject(g, dSwitch.getState() == SwitchState.Open ? HolonSwitch.getSwitchOpenImage(): HolonSwitch.getSwitchClosedImage() , dSwitch.getModel().getPosition());
  409. }
  410. private void paintExitCable(Graphics2D g, ExitCable eCable) {
  411. Position start = eCable.getStart().getPosition();
  412. Position end = eCable.getFinish().getPosition();
  413. float currentEnergy = eCable.getCable().getFlowEnergy();
  414. float capacity = eCable.getCable().getModel().getCapacity();
  415. boolean unlimited = eCable.getCable().getModel().isUnlimitedCapacity();
  416. switch(eCable.getCable().getState()) {
  417. case Burned:
  418. g.setColor(Color.RED);
  419. g.setStroke(new BasicStroke(2));
  420. break;
  421. case Working:
  422. g.setColor(new Color(13, 175, 28));
  423. g.setStroke(new BasicStroke(unlimited?2f:(currentEnergy / capacity * 2f) + 1));
  424. break;
  425. }
  426. g.drawLine(start.x, start.y, end.x, end.y);
  427. Position middle = new Position((start.x + end.x) / 2, (start.y + end.y) / 2);
  428. g.setFont(new Font("TimesRoman", Font.PLAIN, Math.max((int) (controller.getScale() / 3.5f), 10) ));
  429. g.drawString(currentEnergy + "/" + (unlimited?"\u221E":capacity) , middle.x, middle.y);
  430. }
  431. private void paintGroupNode(Graphics2D g, DecoratedGroupNode dGroupNode) {
  432. Position pos = dGroupNode.getModel().getPosition();
  433. g.setColor(Color.lightGray);
  434. g.fillRect(pos.x - controller.getScaleDiv2(), pos.y - controller.getScaleDiv2(), controller.getScale(), controller.getScale());
  435. drawCanvasObject(g, "/Images/upper_node.png" , pos);
  436. paintGroupNodeBar(g, dGroupNode, pos);
  437. }
  438. private void paintGroupNodeBar(Graphics2D g, DecoratedGroupNode dGroupNode , Position pos) {
  439. // +1, -2, -1 little Adjustment for pixel perfect alignment
  440. int barWidth = (int) (controller.getScale());
  441. int barHeight = (int) (controller.getScale() / 5);
  442. g.setColor(Color.WHITE);
  443. g.fillRect(pos.x - controller.getScaleDiv2(), pos.y + controller.getScaleDiv2() - 1, (int) barWidth, barHeight);
  444. float[] percentages = getGroupNodeBarPercentages(dGroupNode);
  445. Color[] colors = new Color[6];
  446. colors[0] = getStateColor(HolonObjectState.PRODUCER);
  447. colors[1] = getStateColor(HolonObjectState.NOT_SUPPLIED);
  448. colors[2] = getStateColor(HolonObjectState.PARTIALLY_SUPPLIED);
  449. colors[3] = getStateColor(HolonObjectState.SUPPLIED);
  450. colors[4] = getStateColor(HolonObjectState.OVER_SUPPLIED);
  451. colors[5] = getStateColor(HolonObjectState.NO_ENERGY);
  452. for(int i = 5; i>=0; i--) {
  453. g.setColor(colors[i]);
  454. g.fillRect(pos.x - controller.getScaleDiv2(), pos.y + controller.getScaleDiv2() - 1, (int) (barWidth * percentages[i] - 1), barHeight);
  455. }
  456. // g.setColor(color);
  457. // g.fillRect(pos.x - controller.getScaleDiv2(), pos.y + controller.getScaleDiv2() - 1, (int) (barWidth * (percentage < 1 ? percentage : 1.0f) - 1), barHeight);
  458. g.setColor(Color.BLACK);
  459. g.setStroke(new BasicStroke(1));
  460. g.drawRect(pos.x - controller.getScaleDiv2(), pos.y + controller.getScaleDiv2() - 1, barWidth - 1 , barHeight);
  461. }
  462. /**
  463. * HardCoded Stuff dont try at Home ;)
  464. * @param dGroupNode
  465. * @return
  466. */
  467. public float[] getGroupNodeBarPercentages(DecoratedGroupNode dGroupNode) {
  468. int[] amountOfObjects = new int[6];
  469. amountOfObjects[0] = dGroupNode.getAmountOfSupplier();
  470. amountOfObjects[1] = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.NOT_SUPPLIED);
  471. amountOfObjects[2] = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.PARTIALLY_SUPPLIED);
  472. amountOfObjects[3] = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.SUPPLIED);
  473. amountOfObjects[4] = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.OVER_SUPPLIED);
  474. amountOfObjects[5] = dGroupNode.getAmountOfPassiv();
  475. int countHolonObjects = amountOfObjects[0] + amountOfObjects[1] + amountOfObjects[2] + amountOfObjects[3] + amountOfObjects[4] + amountOfObjects[5];
  476. float[] percentages = new float[6];
  477. int count = 0;
  478. for(int i = 0; i < 6; i++) {
  479. count += amountOfObjects[i];
  480. percentages[i] = (float)count / (float)countHolonObjects;
  481. }
  482. return percentages;
  483. }
  484. private void paintSupplyBar(Graphics2D g, float percentage, Color color, Position pos) {
  485. // +1, -2, -1 little Adjustment for pixel perfect alignment
  486. int barWidth = (int) (controller.getScale());
  487. int barHeight = (int) (controller.getScale() / 5);
  488. g.setColor(Color.WHITE);
  489. g.fillRect(pos.x - controller.getScaleDiv2(), pos.y + controller.getScaleDiv2() - 1, (int) barWidth, barHeight);
  490. g.setColor(color);
  491. g.fillRect(pos.x - controller.getScaleDiv2(), pos.y + controller.getScaleDiv2() - 1, (int) (barWidth * (percentage < 1 ? percentage : 1.0f) - 1), barHeight);
  492. g.setColor(Color.BLACK);
  493. g.setStroke(new BasicStroke(1));
  494. g.drawRect(pos.x - controller.getScaleDiv2(), pos.y + controller.getScaleDiv2() - 1, barWidth - 1 , barHeight);
  495. g.setFont(new Font("TimesNewRoman", Font.PLAIN, (int) (barHeight * 1.5) - 2));
  496. String percentageString = (Math.round((percentage * 100))) + "%";
  497. int stringWidth = (int) g.getFontMetrics().getStringBounds(percentageString, g).getWidth();
  498. if(percentage > 1.0f) g.setColor(Color.WHITE); //Just to see better on purple
  499. g.drawString(percentageString, pos.x + 1 - stringWidth / 2, pos.y + controller.getScaleDiv2() - 1+ barHeight);
  500. }
  501. //old code
  502. void drawMarker(Graphics2D g) {
  503. Color transparentGrey = new Color(128, 174, 247, 40);
  504. if (sx > x && sy > y) {
  505. g.drawRect(x, y, sx - x, sy - y);
  506. g.setColor(transparentGrey);
  507. g.fillRect(x, y, sx - x, sy - y);
  508. } else if (sx < x && sy < y) {
  509. g.drawRect(sx, sy, x - sx, y - sy);
  510. g.setColor(transparentGrey);
  511. g.fillRect(sx, sy, x - sx, y - sy);
  512. } else if (sx >= x) {
  513. g.drawRect(x, sy, sx - x, y - sy);
  514. g.setColor(transparentGrey);
  515. g.fillRect(x, sy, sx - x, y - sy);
  516. } else if (sy >= y) {
  517. g.drawRect(sx, y, x - sx, sy - y);
  518. g.setColor(transparentGrey);
  519. g.fillRect(sx, y, x - sx, sy - y);
  520. }
  521. }
  522. public void paintComponent(Graphics g) {
  523. super.paintComponent(g);
  524. Graphics2D g2d = (Graphics2D) g;
  525. g2d.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING,
  526. RenderingHints.VALUE_ANTIALIAS_ON));
  527. //-->Old code
  528. if (drawEdge) {
  529. g2d.setColor(Color.BLACK);
  530. g2d.setStroke(new BasicStroke(1));
  531. g2d.drawLine(tempCps.getPosition().x, tempCps.getPosition().y, x, y);
  532. }
  533. //<--
  534. //SelectedCable
  535. HashSet<Edge> selectedEdges = new HashSet<Edge>();
  536. for(AbstractCanvasObject aCps: model.getSelectedCpsObjects()) {
  537. for(Edge edge: aCps.getConnections()) {
  538. selectedEdges.add(edge);
  539. }
  540. }
  541. if(model.getSelectedEdge() != null) selectedEdges.add(model.getSelectedEdge());
  542. //timstep:
  543. g.setFont(new Font("TimesNewRoman", Font.PLAIN, Math.max((int) (controller.getScale() / 3.5f), 10) ));
  544. g2d.setColor(Color.BLACK);
  545. VisualRepresentationalState visualState = controller.getSimManager().getActualVisualRepresentationalState();
  546. //VisualState Representation:
  547. if(visualState == null) System.out.println("AHHH");
  548. for(ExitCable cable : visualState.getExitCableList()) {
  549. paintExitCable(g2d, cable);
  550. }
  551. for(DecoratedCable cable : visualState.getCableList()) {
  552. paintCable(g2d, cable, selectedEdges.contains(cable.getModel()));
  553. }
  554. for(DecoratedGroupNode dGroupNode : visualState.getGroupNodeList()) {
  555. paintGroupNode(g2d, dGroupNode);
  556. }
  557. for(Consumer con: visualState.getConsumerList()) {
  558. paintConsumer(g2d, con);
  559. }
  560. for(Supplier sup: visualState.getSupplierList()) {
  561. paintSupplier(g2d, sup);
  562. }
  563. for(Passiv pas: visualState.getPassivList()) {
  564. paintCanvasObject(g2d, pas);
  565. }
  566. for(DecoratedSwitch dSwitch : visualState.getSwitchList()) {
  567. paintSwitch(g2d, dSwitch);
  568. }
  569. for(Node node : visualState.getNodeList()) {
  570. drawCanvasObject(g2d, "/Images/node.png" , node.getPosition());
  571. }
  572. //-->oldCode
  573. if (doMark) {
  574. g2d.setColor(Color.BLACK);
  575. g2d.setStroke(new BasicStroke(0));
  576. drawMarker(g2d);
  577. }
  578. //Test Selection
  579. //Objects:
  580. g2d.setColor(Color.BLUE);
  581. g2d.setStroke(new BasicStroke(1));
  582. Color transparentGrey = new Color(128, 174, 247, 40);
  583. for(AbstractCanvasObject aCps: model.getSelectedCpsObjects()) {
  584. if(aCps instanceof Node) {
  585. Position pos = aCps.getPosition();
  586. g2d.setColor(transparentGrey);
  587. g2d.fillOval(pos.x - (int) (controller.getScaleDiv2()), pos.y - (int) (controller.getScaleDiv2()), controller.getScale(), controller.getScale());
  588. g2d.setColor(Color.LIGHT_GRAY);
  589. g2d.setStroke(new BasicStroke(2));
  590. g2d.drawOval(pos.x - (int) (controller.getScaleDiv2()), pos.y - (int) (controller.getScaleDiv2()), controller.getScale(), controller.getScale());
  591. }
  592. else {
  593. Position pos = aCps.getPosition();
  594. g2d.setColor(transparentGrey);
  595. 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));
  596. g2d.setColor(Color.LIGHT_GRAY);
  597. g2d.setStroke(new BasicStroke(2));
  598. 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));
  599. }
  600. }
  601. //maybeReplace:
  602. if(mayBeReplaced != null){
  603. g2d.setColor(Color.RED);
  604. Position pos = mayBeReplaced.getPosition();
  605. g2d.drawImage(ImageImport.loadImage("/Images/replace.png") ,
  606. pos.x + controller.getScaleDiv2(),
  607. pos.y - controller.getScale(),
  608. controller.getScaleDiv2(), controller.getScaleDiv2(), null);
  609. }
  610. //<-- OldCode
  611. }
  612. @Override
  613. public void mouseClicked(MouseEvent e) {
  614. if(!disabled){
  615. if (e.getButton() == MouseEvent.BUTTON1) {
  616. DefaulTable propertyTable = model.getPropertyTable();
  617. if (propertyTable.getRowCount() > 0) {
  618. for (int i = propertyTable.getRowCount() - 1; i > -1; i--) {
  619. propertyTable.removeRow(i);
  620. }
  621. }
  622. triggerUpdateController();
  623. }
  624. stopEditing();
  625. }
  626. }
  627. @Override
  628. public void mouseEntered(MouseEvent e) {
  629. }
  630. @Override
  631. public void mouseExited(MouseEvent e) {
  632. }
  633. @Override
  634. public void mousePressed(MouseEvent e) {
  635. if(!disabled){
  636. stopEditing();
  637. tempCps = null;
  638. edgeHighlight = null;
  639. controller.setSelecteEdge(null);
  640. // Object Selection
  641. for (AbstractCanvasObject cps : model.getObjectsOnCanvas()) {
  642. cx = cps.getPosition().x - controller.getScaleDiv2();
  643. cy = cps.getPosition().y - controller.getScaleDiv2();
  644. if (x - controller.getScale() <= cx
  645. && y - controller.getScale() <= cy && x >= cx && y >= cy) {
  646. tempCps = cps;
  647. dragging = true;
  648. if (e.isControlDown() && tempCps != null) {
  649. if (model.getSelectedCpsObjects().contains(tempCps)) {
  650. controller.deleteSelectedObject(tempCps);
  651. //TODO: RemoveDepth
  652. } else {
  653. controller.addSelectedObject(tempCps);
  654. if(tempCps instanceof GroupNode)
  655. controller.getObjectsInDepth();
  656. }
  657. }
  658. // If drawing an Edge (CTRL down)
  659. if (tempCps.getClass() == HolonObject.class) {
  660. HolonObject tempObj = ((HolonObject) tempCps);
  661. dataSelected = tempObj.getElements();
  662. }
  663. if (e.isShiftDown()) {
  664. drawEdge = true;
  665. dragging = false;
  666. }
  667. }
  668. }
  669. // Edge Selection
  670. if (tempCps == null) {
  671. edgeHighlight = mousePositionOnEdge(x, y);
  672. controller.setSelecteEdge(edgeHighlight);
  673. controller.setSelectedObjectID(0);
  674. if (!e.isControlDown() && e.getButton() != MouseEvent.BUTTON3) {
  675. model.getSelectedCpsObjects().clear();
  676. }
  677. }
  678. if (edgeHighlight == null && tempCps == null) {
  679. sx = e.getX();
  680. sy = e.getY();
  681. doMark = true;
  682. }
  683. repaint();
  684. }
  685. }
  686. @Override
  687. public void mouseReleased(MouseEvent e) {
  688. if(!disabled){
  689. x = e.getX();
  690. y = e.getY();
  691. dragging = false;
  692. if (drawEdge) {
  693. drawEdge = false;
  694. drawDeleteEdge();
  695. }
  696. if (dragged) {
  697. try {
  698. /**
  699. * Save before further Dragged interactions happen
  700. */
  701. controller.autoSave();
  702. } catch (IOException ex) {
  703. System.err.println("AutoSave error by dragging");
  704. ex.printStackTrace();
  705. }
  706. /**
  707. * check if a unique tempCps could replace an Object on the canvas
  708. */
  709. if(model.getSelectedCpsObjects().size()==1
  710. && checkForReplacement(model.getObjectsOnCanvas(), tempCps, tempCps.getPosition().x, tempCps.getPosition().y)){
  711. /**
  712. * if UpperNode would be replaced, close its tabs
  713. */
  714. if(mayBeReplaced instanceof GroupNode)
  715. closeUpperNodeTab(mayBeReplaced.getId());
  716. /**
  717. * replace on canvas (will save)
  718. */
  719. controller.replaceCanvasObject(mayBeReplaced, tempCps);
  720. mayBeReplaced=null;
  721. }
  722. }
  723. if (!e.isControlDown() && !dragged && tempCps != null
  724. && MouseEvent.BUTTON3 != e.getButton()) {
  725. model.getSelectedCpsObjects().clear();
  726. controller.addSelectedObject(tempCps);
  727. model.setSelectedCpsObject(tempCps);
  728. if(tempCps instanceof GroupNode)
  729. controller.getObjectsInDepth();
  730. }
  731. dragged = false;
  732. // Rightclick List
  733. setRightClickMenu(e);
  734. markObjects();
  735. if (doubleClick() && tempCps instanceof HolonSwitch
  736. && MouseEvent.BUTTON3 != e.getButton()&& tempCps != null) {
  737. ((HolonSwitch) tempCps).switchState();
  738. }
  739. controller.calculateStateAndVisualForTimeStep(model.getCurIteration());
  740. triggerUpdateController();
  741. repaint();
  742. }
  743. }
  744. @Override
  745. public void mouseDragged(MouseEvent e) {
  746. if(!disabled){
  747. // If Edge is drawn
  748. x = e.getX();
  749. y = e.getY();
  750. if (!model.getSelectedCpsObjects().contains(tempCps) && !doMark) {
  751. model.getSelectedCpsObjects().clear();
  752. if (tempCps != null) {
  753. controller.addSelectedObject(tempCps);
  754. }
  755. }
  756. if (dragging) {
  757. try {
  758. dragged = true;
  759. float xDist, yDist; // Distance
  760. x = e.getX();
  761. y = e.getY();
  762. // Make sure its in bounds
  763. int scaleDiv2 = controller.getScaleDiv2();
  764. if (e.getX() < scaleDiv2)
  765. x = scaleDiv2;
  766. else if (e.getX() > this.getWidth() - scaleDiv2)
  767. x = this.getWidth() - scaleDiv2;
  768. if (e.getY() < scaleDiv2)
  769. y = scaleDiv2;
  770. else if (e.getY() > this.getHeight()
  771. - scaleDiv2)
  772. y = this.getHeight() - scaleDiv2;
  773. // Distance
  774. xDist = x - tempCps.getPosition().x;
  775. yDist = y - tempCps.getPosition().y;
  776. tempCps.setPosition(x, y); // Drag Position
  777. // ToolTipText Position and name
  778. toolTip = true;
  779. toolTipText = tempCps.getName() + ", " + tempCps.getId();
  780. toolTipPos.x = tempCps.getPosition().x
  781. - scaleDiv2;
  782. toolTipPos.y = tempCps.getPosition().y
  783. + scaleDiv2;
  784. // All Selected Objects
  785. for (AbstractCanvasObject cps : model.getSelectedCpsObjects()) {
  786. if (cps != tempCps) {
  787. x = (int) (cps.getPosition().x + xDist);
  788. y = (int) (cps.getPosition().y + yDist);
  789. // Make sure its in bounds
  790. if (x <= scaleDiv2)
  791. x = scaleDiv2;
  792. else if (x > this.getWidth()
  793. - scaleDiv2)
  794. x = this.getWidth() - scaleDiv2;
  795. if (y <= scaleDiv2)
  796. y = scaleDiv2;
  797. else if (y > this.getHeight()
  798. - scaleDiv2)
  799. y = this.getHeight() - scaleDiv2;
  800. cps.setPosition(x, y);
  801. }
  802. }
  803. /**
  804. * check if something might be replaced
  805. */
  806. if(model.getSelectedCpsObjects().size()==1)
  807. checkForReplacement(model.getObjectsOnCanvas(), tempCps, x, y);
  808. repaint();
  809. } catch (Exception eex) {
  810. }
  811. }
  812. // Mark Objects
  813. if (doMark) {
  814. tempSelected.clear();
  815. for (AbstractCanvasObject cps : model.getObjectsOnCanvas()) {
  816. int x1 = sx, x2 = x, y1 = sy, y2 = y;
  817. if (sx >= x) {
  818. x1 = x;
  819. x2 = sx;
  820. }
  821. if (sy >= y) {
  822. y1 = y;
  823. y2 = sy;
  824. }
  825. if (x1 <= cps.getPosition().x + model.getScaleDiv2()
  826. && y1 <= cps.getPosition().y + model.getScaleDiv2()
  827. && x2 >= cps.getPosition().x
  828. && y2 >= cps.getPosition().y) {
  829. tempSelected.add(cps);
  830. }
  831. }
  832. }
  833. repaint();
  834. }
  835. }
  836. @Override
  837. public void mouseMoved(MouseEvent e) {
  838. if(!disabled){
  839. x = e.getX();
  840. y = e.getY();
  841. // Everything for the tooltip :)
  842. boolean on = false;
  843. for (AbstractCanvasObject cps : model.getObjectsOnCanvas()) {
  844. cx = cps.getPosition().x - controller.getScaleDiv2();
  845. cy = cps.getPosition().y - controller.getScaleDiv2();
  846. on = setToolTipInfoAndPosition(on, cps);
  847. }
  848. if(on||(!on && toolTip))
  849. repaint();
  850. toolTip = on;
  851. }
  852. }
  853. /**
  854. * Draws or Deletes an Edge.
  855. */
  856. void drawDeleteEdge() {
  857. if (getMousePosition() != null) {
  858. boolean node = true;
  859. boolean newEdge = true;
  860. boolean onEdge = true;
  861. boolean deleteNode = false;
  862. Edge e = null;
  863. for (AbstractCanvasObject cps : model.getObjectsOnCanvas()) {
  864. cx = cps.getPosition().x - controller.getScaleDiv2();
  865. cy = cps.getPosition().y - controller.getScaleDiv2();
  866. if (x - controller.getScale() <= cx
  867. && y - controller.getScale() <= cy && x >= cx
  868. && y >= cy && cps != tempCps) {
  869. node = false;
  870. onEdge = false;
  871. for (Edge p : tempCps.getConnections()) {
  872. if ((p.getA() == tempCps && p.getB() == cps)
  873. || (p.getB() == tempCps && p.getA() == cps)) {
  874. newEdge = false;
  875. e = p;
  876. }
  877. }
  878. if (!newEdge) {
  879. controller.removeEdgesOnCanvas(e);
  880. // Node ohne Edge?
  881. if (e.getA().getClass() == Node.class
  882. && e.getA().getConnections().isEmpty()) {
  883. tempCps = e.getA();
  884. deleteNode = true;
  885. }
  886. if (e.getB().getClass() == Node.class
  887. && e.getB().getConnections().isEmpty()) {
  888. deleteNode = true;
  889. }
  890. } else {
  891. if(!(cps instanceof GroupNode || tempCps instanceof GroupNode)) {
  892. e = new Edge(cps, tempCps, model.getMaxCapacity());
  893. controller.addEdgeOnCanvas(e);
  894. }else if (cps instanceof GroupNode && !(tempCps instanceof GroupNode)){
  895. GroupNode thisUpperNode = (GroupNode)cps;
  896. Object[] possibilities = thisUpperNode.getNodes().stream().map(aCps -> new ACpsHandle(aCps)).filter(aCpsHandle -> !(aCpsHandle.object instanceof GroupNode)).toArray();
  897. if(possibilities.length != 0) {
  898. 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, "");
  899. if(selected != null) {
  900. e = new Edge(selected.object, tempCps, model.getMaxCapacity());
  901. controller.addEdgeOnCanvas(e);
  902. }
  903. }else {
  904. Node n = new Node("Node");
  905. n.setPosition(x, y);
  906. controller.addObjUpperNode(n, thisUpperNode);
  907. e = new Edge(n, tempCps, model.getMaxCapacity());
  908. controller.addEdgeOnCanvas(e);
  909. }
  910. }
  911. }
  912. }
  913. }
  914. // Edge auf eine Edge gezogen?
  915. if (onEdge && !checkForReplacement(x, y)) {
  916. Edge p = mousePositionOnEdge(x, y);
  917. if (p != null) {
  918. Edge e1;
  919. Edge e2;
  920. node = false;
  921. Node n = new Node("Node");
  922. n.setPosition(x, y);
  923. controller.addObjectCanvas(n);
  924. AbstractCanvasObject r, k;
  925. r = p.getA();
  926. k = p.getB();
  927. e = new Edge(n, tempCps, model.getMaxCapacity());
  928. e1 = new Edge(n, r, model.getMaxCapacity());
  929. e2 = new Edge(n, k, model.getMaxCapacity());
  930. controller.removeEdgesOnCanvas(p);
  931. controller.addEdgeOnCanvas(e);
  932. controller.addEdgeOnCanvas(e1);
  933. controller.addEdgeOnCanvas(e2);
  934. }
  935. }else{
  936. mayBeReplaced = null;
  937. }
  938. // ins leere Gedragged
  939. if (node && !checkForReplacement(x, y)) {
  940. Node n = new Node("Node");
  941. n.setPosition(x, y);
  942. controller.addObjectCanvas(n);
  943. e = new Edge(n, tempCps, model.getMaxCapacity());
  944. controller.addEdgeOnCanvas(e);
  945. }else{
  946. mayBeReplaced = null;
  947. }
  948. // Wenn ein Node ohne Connections da ist
  949. if (deleteNode) {
  950. controller.delCanvasObject(tempCps, true);
  951. tempCps = null;
  952. }
  953. }
  954. }
  955. /**
  956. * Checks if the mouse is on an Edge.
  957. *
  958. * @param x
  959. * Position of the Mouse
  960. * @param y
  961. * Position of the Mouse
  962. * @return CpsEdge the Mouse is on, null if the mouse is not on an Edge
  963. */
  964. private Edge mousePositionOnEdge(int x, int y) {
  965. x += controller.getScaleDiv2();
  966. y += controller.getScaleDiv2();
  967. for (Edge p : model.getEdgesOnCanvas()) {
  968. Line2D l = new Line2D.Float(p.getA().getPosition().x, p.getA()
  969. .getPosition().y, p.getB().getPosition().x, p.getB()
  970. .getPosition().y);
  971. int[] positions = determineMousePositionOnEdge(p);
  972. int lx = positions[0];
  973. int ly = positions[1];
  974. int hx = positions[2];
  975. int hy = positions[3];
  976. // distance from a point to a line and between both Objects
  977. if (l.ptLineDistSq(x - model.getScaleDiv2(),
  978. y - model.getScaleDiv2()) < 20
  979. && x > lx && x < hx && y > ly && y < hy) {
  980. return p;
  981. }
  982. }
  983. return null;
  984. }
  985. void updateLanguages() {
  986. itemCut.setText(Languages.getLanguage()[95]);
  987. itemCopy.setText(Languages.getLanguage()[96]);
  988. itemPaste.setText(Languages.getLanguage()[97]);
  989. itemDelete.setText(Languages.getLanguage()[98]);
  990. itemGroup.setText(Languages.getLanguage()[99]);
  991. itemUngroup.setText(Languages.getLanguage()[100]);
  992. itemTrack.setText(Languages.getLanguage()[101]);
  993. itemUntrack.setText(Languages.getLanguage()[102]);
  994. }
  995. /**
  996. * Set if Information should be shown.
  997. *
  998. * @param connection
  999. * boolean for conecction
  1000. * @param object
  1001. * boolean for objects
  1002. * @param nodeOfnode
  1003. */
  1004. void setShowedInformation(boolean connection, boolean object,
  1005. boolean border, boolean nodeOfnode) {
  1006. showConnectionInformation = connection;
  1007. showedInformation[1] = object;
  1008. showedInformation[3] = border;
  1009. showedInformation[4] = nodeOfnode;
  1010. }
  1011. /**
  1012. * Returns if Information should be shown.
  1013. *
  1014. * @return Array of boolean [0] = connection, [1] = objects
  1015. */
  1016. boolean[] getShowedInformation() {
  1017. return showedInformation;
  1018. }
  1019. /**
  1020. * set toolTip
  1021. *
  1022. * @param bool
  1023. */
  1024. void setToolTip(boolean bool) {
  1025. this.toolTip = bool;
  1026. }
  1027. /**
  1028. * Set the Mouse
  1029. *
  1030. * @param x
  1031. * @param y
  1032. */
  1033. void setXY(int x, int y) {
  1034. this.x = x;
  1035. this.y = y;
  1036. }
  1037. @Override
  1038. public boolean checkForReplacement(int x, int y) {
  1039. return checkForReplacement(model.getObjectsOnCanvas(), null, x, y);
  1040. }
  1041. @Override
  1042. public void tryToAlignObjects(){
  1043. /**
  1044. * Align all Objects
  1045. */
  1046. for(AbstractCanvasObject cps: model.getObjectsOnCanvas())
  1047. align(cps,3*model.getScaleDiv2());
  1048. /**
  1049. * AutoSave new Positons
  1050. */
  1051. try{
  1052. controller.autoSave();
  1053. } catch (IOException ex) {
  1054. System.err.println("AutoSave error by aligning");
  1055. ex.printStackTrace();
  1056. }
  1057. }
  1058. @Override
  1059. public void closeUpperNodeTab(int upperNodeId) {
  1060. JTabbedPane tabbedPaneInner = (JTabbedPane) getParent()
  1061. .getParent().getParent().getParent();
  1062. for (int i = 1; i < tabbedPaneInner.getTabCount(); i++) {
  1063. if (((GroupNodeCanvas) ((JScrollPane) tabbedPaneInner
  1064. .getComponentAt(i)).getViewport().getComponent(
  1065. 0)).upperNode.getId() == upperNodeId) {
  1066. tabbedPaneInner.remove(i);
  1067. break;
  1068. }
  1069. }
  1070. }
  1071. }