AbstractCanvas.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. package ui.view;
  2. import classes.*;
  3. import ui.controller.Control;
  4. import ui.controller.UpdateController;
  5. import ui.model.Model;
  6. import javax.swing.*;
  7. import javax.swing.table.JTableHeader;
  8. import java.awt.*;
  9. import java.awt.event.MouseEvent;
  10. import java.io.File;
  11. import java.io.IOException;
  12. import java.util.ArrayList;
  13. import java.util.TimerTask;
  14. /**
  15. * Collection of methods and values needed in both <code>MyCanvas</code> and
  16. * <code>UpperNodeCanvas</code>
  17. * <p>
  18. * Although Java works on references we chose to add explicit return values for
  19. * clearer code understanding in most cases
  20. *
  21. * @author: I. Dix
  22. */
  23. public abstract class AbstractCanvas extends JPanel {
  24. /**
  25. * Version
  26. */
  27. private static final long serialVersionUID = 1L;
  28. final JMenuItem itemDelete = new JMenuItem(Languages.getLanguage()[98]);
  29. final JMenuItem itemCut = new JMenuItem(Languages.getLanguage()[95]);
  30. final JMenuItem itemCopy = new JMenuItem(Languages.getLanguage()[96]);
  31. final JMenuItem itemPaste = new JMenuItem(Languages.getLanguage()[97]);
  32. final JMenuItem itemGroup = new JMenuItem(Languages.getLanguage()[99]);
  33. final JMenuItem itemUngroup = new JMenuItem(Languages.getLanguage()[100]);
  34. final JMenuItem itemTrack = new JMenuItem(Languages.getLanguage()[101]);
  35. final JMenuItem itemUntrack = new JMenuItem(Languages.getLanguage()[102]);
  36. final JMenuItem itemAlign = new JMenuItem("Align selected");
  37. final JMenuItem itemCreateTemplate = new JMenuItem(Languages.getLanguage()[Languages.right_click_create_template]);
  38. final int ANIMTIME = 500; // animation Time
  39. private final int animFPS = 60;
  40. final int animDelay = 1000 / animFPS; // animation Delay
  41. protected Model model;
  42. protected Control controller;
  43. protected int x = 0;
  44. protected int y = 0;
  45. // Selection
  46. AbstractCpsObject tempCps = null;
  47. UpdateController updCon;
  48. //Replacement
  49. /**
  50. * the CpsObject that might be replaced by drag&drop
  51. */
  52. protected AbstractCpsObject mayBeReplaced = null;
  53. // PopUpMenu
  54. JPopupMenu popmenu = new JPopupMenu();
  55. // Tooltip
  56. boolean toolTip; // Tooltip on or off
  57. Position toolTipPos = new Position(); // Tooltip Position
  58. String toolTipText = "";
  59. ArrayList<HolonElement> dataSelected = new ArrayList<>();
  60. ArrayList<AbstractCpsObject> tempSelected = new ArrayList<>();
  61. boolean[] showedInformation = new boolean[5];
  62. boolean dragging = false; // for dragging
  63. boolean dragged = false; // if an object/objects was/were dragged
  64. boolean drawEdge = false; // for drawing edges
  65. boolean doMark = false; // for double click
  66. CpsEdge edgeHighlight = null;
  67. Point mousePosition = new Point(); // Mouse Position when
  68. ArrayList<Position> savePos;
  69. // edge Object Start Point
  70. int cx, cy;
  71. int sx, sy; // Mark Coords
  72. Position unPos;
  73. // Animation
  74. Timer animT; // animation Timer
  75. int animDuration = ANIMTIME; // animation Duration
  76. int animSteps = animDuration / animDelay; // animation Steps;
  77. ArrayList<AbstractCpsObject> animCps = null;
  78. // Graphics
  79. Image img = null; // Contains the image to draw on the Canvas
  80. Graphics2D g2; // For Painting
  81. float scalediv20;
  82. // Mouse
  83. private boolean click = false;
  84. // ------------------------------------------ METHODS
  85. // ------------------------------------------
  86. /**
  87. * Paints the SupplyBar for the given cps object on the canvas
  88. *
  89. * @param g
  90. * Graphics used
  91. * @param cps
  92. * cpsObject which the supplyBar should be drawn for
  93. * @deprecated
  94. */
  95. protected void paintSupplyBar(Graphics g, AbstractCpsObject cps) {
  96. g2.setColor(Color.black);
  97. /**
  98. * draw and fill the supply Bar
  99. */
  100. if (model.getShowSupplyBars() && (cps instanceof HolonObject || cps instanceof HolonBattery))
  101. {
  102. // set Color & Percentage
  103. /**
  104. * percentage the SupplyBar is representing
  105. */
  106. float percentage = 0;
  107. /**
  108. * color of the SupplyBar
  109. */
  110. Color paintColor = Color.WHITE;
  111. if(cps instanceof HolonObject)
  112. {
  113. HolonObject hl = (HolonObject) cps;
  114. if (hl == null || !(hl.getState() == HolonObject.NOT_SUPPLIED
  115. || hl.getState() == HolonObject.PARTIALLY_SUPPLIED || hl.getState() == HolonObject.OVER_SUPPLIED)) {
  116. /**
  117. * don't show Bars for unsupplied oder fully supplied Objects
  118. */
  119. return;
  120. }
  121. /**
  122. * calculate Percentage & set Color
  123. */
  124. percentage = hl.getSuppliedPercentage();
  125. paintColor = hl.getColor();
  126. }
  127. else if (cps instanceof HolonBattery){
  128. HolonBattery hB = (HolonBattery) cps;
  129. if(hB == null || hB.getCapacity() == 0){
  130. return;
  131. }
  132. /**
  133. * get percentage filled
  134. */
  135. percentage = hB.getStateOfChargeAtTimeStep(model.getCurIteration()-1) / hB.getCapacity();
  136. /**
  137. * calculate the Color (Red->Yellow->Green)
  138. */
  139. Color color1 = Color.RED;
  140. Color color2 = Color.GREEN;
  141. //
  142. float colorPercentage;
  143. if(percentage < 0.5f){
  144. colorPercentage = percentage * 2;
  145. color1 = Color.RED;
  146. color2 = Color.YELLOW;
  147. }
  148. else {
  149. colorPercentage = (percentage - 0.5f) * 2;
  150. color1 = Color.YELLOW;
  151. color2 = Color.GREEN;
  152. }
  153. final int dRed = color2.getRed() - color1.getRed();
  154. final int dGreen = color2.getGreen() - color1.getGreen();
  155. final int dBlue = color2.getBlue() - color1.getBlue();
  156. int resultRed = color1.getRed() + (int)(colorPercentage * dRed);
  157. int resultGreen = color1.getGreen() + (int)(colorPercentage * dGreen);
  158. int resultBlue = color1.getBlue() + (int)( colorPercentage * dBlue);
  159. paintColor = new Color( resultRed,resultGreen,resultBlue);
  160. }
  161. /**
  162. * Starting position x of the bar
  163. */
  164. int barX = (int) (cps.getPosition().x - controller.getScaleDiv2() - scalediv20);
  165. /**
  166. * Starting position y of the bar
  167. */
  168. int barY = (int) (cps.getPosition().y - controller.getScaleDiv2() + controller.getScale() + 1);
  169. /**
  170. * if object should be replaced -> move bar below the ReplacementIndicator
  171. */
  172. if(mayBeReplaced==cps) barY += 3;
  173. /**
  174. * Width of the bar
  175. */
  176. int barWidth = (int) (controller.getScale() + ((scalediv20) * 2) - 1);
  177. /**
  178. * Height of the bar
  179. */
  180. int barHeight = (int) (controller.getScale() / 5);
  181. /**
  182. * draw Rectangle below the image
  183. */
  184. g2.setStroke(new BasicStroke(1));
  185. g2.drawRect(barX, barY, barWidth, barHeight);
  186. g2.setColor(paintColor);
  187. /** fill it accordingly if filled partially */
  188. if (percentage < 1)
  189. g2.fillRect(barX + 1, barY + 1, (int) ((barWidth - 1) * percentage), barHeight - 1);
  190. else /** over supplied / supplied bar should be fully filled */
  191. g2.fillRect(barX + 1, barY + 1, (int) (barWidth - 1), barHeight - 1);
  192. /** write percentage */
  193. if(percentage>1)
  194. g2.setColor(Color.WHITE);
  195. else
  196. g2.setColor(Color.BLACK);
  197. /** original font */
  198. Font oldFont = g2.getFont();
  199. g.setFont(new Font("TimesRoman", Font.PLAIN, (int) (barHeight * 1.5) - 2));
  200. String percentageString = (Math.round((percentage * 100))) + "%";
  201. int stringWidth = (int) g2.getFontMetrics().getStringBounds(percentageString, g2).getWidth();
  202. g2.drawString(percentageString, barX + barWidth / 2 + 1 - stringWidth / 2, barY + barHeight);
  203. /** recover Font and Color */
  204. g2.setFont(oldFont);
  205. g2.setColor(Color.BLACK);
  206. }
  207. }
  208. void drawMarker() {
  209. if (sx > x && sy > y) {
  210. g2.drawRect(x, y, sx - x, sy - y);
  211. } else if (sx < x && sy < y) {
  212. g2.drawRect(sx, sy, x - sx, y - sy);
  213. } else if (sx >= x) {
  214. g2.drawRect(x, sy, sx - x, y - sy);
  215. } else if (sy >= y) {
  216. g2.drawRect(sx, y, x - sx, sy - y);
  217. }
  218. }
  219. /**
  220. * @deprecated
  221. * @param g
  222. */
  223. void showTooltip(Graphics g) {
  224. if (toolTip) {
  225. g2.setColor(new Color(255, 225, 150));
  226. g2.setStroke(new BasicStroke(1));
  227. int textWidth = g.getFontMetrics().stringWidth(toolTipText) + 2; // Text
  228. // width
  229. // fixed x and y Position to the screen
  230. int fixXPos = toolTipPos.x - (textWidth >> 1) + model.getScaleDiv2();
  231. int fixYPos = toolTipPos.y;
  232. if (fixXPos < 0) {
  233. fixXPos = 0;
  234. } else if (fixXPos + textWidth + 1 > this.getWidth()) {
  235. fixXPos -= (fixXPos + textWidth + 1) - this.getWidth();
  236. }
  237. if (fixYPos + 16 > this.getHeight()) {
  238. fixYPos -= (fixYPos + 16) - this.getHeight();
  239. }
  240. g2.fillRect(fixXPos, fixYPos, textWidth, 15);
  241. g2.setColor(Color.BLACK);
  242. g2.drawRect(fixXPos, fixYPos, textWidth, 15);
  243. g2.drawString(toolTipText, fixXPos + 2, fixYPos + 12);
  244. }
  245. }
  246. void setRightClickMenu(MouseEvent e) {
  247. if (e.getButton() == MouseEvent.BUTTON3) {
  248. itemPaste.setEnabled(true);
  249. if (tempCps != null) {
  250. itemPaste.setEnabled(true);
  251. itemDelete.setEnabled(true);
  252. itemCut.setEnabled(true);
  253. itemCopy.setEnabled(true);
  254. itemAlign.setEnabled(true);
  255. // tracking
  256. if (tempCps != null) {
  257. itemGroup.setEnabled(true);
  258. itemTrack.setEnabled(true);
  259. itemUntrack.setEnabled(true);
  260. }
  261. // ungrouping
  262. if (tempCps instanceof CpsUpperNode)
  263. itemUngroup.setEnabled(true);
  264. else
  265. itemUngroup.setEnabled(false);
  266. if (model.getSelectedCpsObjects().size() == 0) {
  267. controller.addSelectedObject(tempCps);
  268. }
  269. if (tempCps instanceof HolonObject) {
  270. itemCreateTemplate.setEnabled(true);
  271. } else {
  272. itemCreateTemplate.setEnabled(false);
  273. }
  274. } else {
  275. itemAlign.setEnabled(false);
  276. itemCut.setEnabled(false);
  277. itemCopy.setEnabled(false);
  278. itemGroup.setEnabled(false);
  279. itemUngroup.setEnabled(false);
  280. itemTrack.setEnabled(false);
  281. itemUntrack.setEnabled(false);
  282. itemCreateTemplate.setEnabled(false);
  283. if (edgeHighlight != null) {
  284. itemDelete.setEnabled(true);
  285. itemPaste.setEnabled(false);
  286. } else {
  287. itemDelete.setEnabled(false);
  288. itemPaste.setEnabled(true);
  289. }
  290. }
  291. mousePosition = this.getMousePosition();
  292. popmenu.show(e.getComponent(), e.getX(), e.getY());
  293. }
  294. }
  295. void markObjects() {
  296. if (doMark) {
  297. doMark = false;
  298. for (AbstractCpsObject cps : tempSelected) {
  299. if (!model.getSelectedCpsObjects().contains(cps)) {
  300. controller.addSelectedObject(cps);
  301. }
  302. }
  303. controller.getObjectsInDepth();
  304. tempSelected.clear();
  305. }
  306. }
  307. int[] determineMousePositionOnEdge(CpsEdge p) {
  308. int lx, ly, hx, hy;
  309. if (p.getA().getPosition().x > p.getB().getPosition().x) {
  310. hx = p.getA().getPosition().x + model.getScaleDiv2() + 7;
  311. lx = p.getB().getPosition().x + model.getScaleDiv2() - 7;
  312. } else {
  313. lx = p.getA().getPosition().x + model.getScaleDiv2() - 7;
  314. hx = p.getB().getPosition().x + model.getScaleDiv2() + 7;
  315. }
  316. if (p.getA().getPosition().y > p.getB().getPosition().y) {
  317. hy = p.getA().getPosition().y + model.getScaleDiv2() + 7;
  318. ly = p.getB().getPosition().y + model.getScaleDiv2() - 7;
  319. } else {
  320. ly = p.getA().getPosition().y + model.getScaleDiv2() - 7;
  321. hy = p.getB().getPosition().y + model.getScaleDiv2() + 7;
  322. }
  323. return new int[] { lx, ly, hx, hy };
  324. }
  325. /**
  326. * Checks if a double click was made.
  327. *
  328. * @return true if doublecklick, false if not
  329. */
  330. boolean doubleClick() {
  331. if (click) {
  332. click = false;
  333. return true;
  334. } else {
  335. click = true;
  336. java.util.Timer t = new java.util.Timer("doubleclickTimer", false);
  337. t.schedule(new TimerTask() {
  338. @Override
  339. public void run() {
  340. click = false;
  341. }
  342. }, 500);
  343. }
  344. return false;
  345. }
  346. boolean setToolTipInfoAndPosition(boolean on, AbstractCpsObject cps) {
  347. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
  348. on = true;
  349. toolTipPos.x = cps.getPosition().x - controller.getScaleDiv2();
  350. toolTipPos.y = cps.getPosition().y + controller.getScaleDiv2();
  351. toolTipText = cps.getName() + ", " + cps.getId();
  352. }
  353. return on;
  354. }
  355. abstract void drawDeleteEdge();
  356. void triggerUpdateController() {
  357. updCon.paintProperties(tempCps);
  358. updCon.refreshTableHolonElement(model.getMultiTable(), model.getSingleTable());
  359. updCon.refreshTableProperties(model.getPropertyTable());
  360. }
  361. /**
  362. * Checks if {@code draggedCps} or a new cpsObject at Position (x,y) could replace exactly one object
  363. * in {@code objects}.
  364. * Saves the object that would be replaced in {@link AbstractCanvas}.{@code MayBeReplaced}
  365. * @param objects list of objects that could be replaced
  366. * @param draggedCps Object that might replace
  367. * @param x Position of the objects that might replace
  368. * @param y Position of the objects that might replace
  369. * @return true if exactly one Object could be replaced
  370. */
  371. protected boolean checkForReplacement(ArrayList<AbstractCpsObject> objects, AbstractCpsObject draggedCps, int x, int y){
  372. /** distance treshold for replacement */
  373. int treshhold = controller.getScale()/2;
  374. /** number of Objects that might be replaced (should be 1) */
  375. int replaceCounter = 0;
  376. /** last object that could be replaced */
  377. AbstractCpsObject toBeReplaced = null;
  378. /** Position of object that might be replaced */
  379. Position p;
  380. /** for each cps on Canvas */
  381. if(draggedCps == null || !(draggedCps instanceof CpsNode) && !(draggedCps instanceof CpsNode)){
  382. for (AbstractCpsObject cps : objects){
  383. /** same object -> ignore */
  384. if(cps == draggedCps)continue;
  385. /** set Position of object that might be replaced */
  386. p = cps.getPosition();
  387. /** if near enough */
  388. if(Math.abs(x-p.x)<treshhold && Math.abs(y-p.y)<treshhold){
  389. replaceCounter++;
  390. toBeReplaced = cps;
  391. /**
  392. * if too many Objects could be replaced:
  393. * stop searching, because it would not be clear which one should
  394. * be replaced
  395. */
  396. if(replaceCounter>1)break;
  397. }
  398. }
  399. }
  400. /**
  401. * return true if exactly one obect would be replaced
  402. */
  403. if( replaceCounter == 1 && toBeReplaced != null){
  404. mayBeReplaced = toBeReplaced;
  405. return true;
  406. }else{
  407. mayBeReplaced = null;
  408. return false;
  409. }
  410. }
  411. /**
  412. * Checks if an inserted new Object could replace exactly one object on the canvas.
  413. * Saves the object that would be replaced in {@link AbstractCanvas}.{@code MayBeReplaced}
  414. * @param x Position of the objects that might replace
  415. * @param y Position of the objects that might replace
  416. * @return true if exactly one Object could be replaced
  417. */
  418. public abstract boolean checkForReplacement(int x, int y);
  419. /**
  420. * highlights the object that mayBeReplaced
  421. * @param g2
  422. */
  423. protected void highlightMayBeReplaced(Graphics2D g2) {
  424. if(mayBeReplaced != null){
  425. g2.setColor(Color.RED);
  426. g2.fillRect(
  427. (int) (mayBeReplaced.getPosition().x
  428. - controller.getScaleDiv2() - (scalediv20 + 3)),
  429. (int) (mayBeReplaced.getPosition().y
  430. - controller.getScaleDiv2() - (scalediv20 + 3)),
  431. (int) (controller.getScale() + ((scalediv20 + 3) * 2)),
  432. (int) (controller.getScale() + ((scalediv20 + 3) * 2)));
  433. }
  434. }
  435. /**
  436. * Align alle Objects on the Canvas to a Grid with objects every 10 pixels
  437. */
  438. public abstract void tryToAlignObjects();
  439. /**
  440. * Aligns the Object the a grid
  441. * @param cps Object that should be aligned
  442. * @param distance distance between the AlignmentGrid Lines. (objects every 'distance' pixels
  443. */
  444. protected void align(AbstractCpsObject cps, int distance) {
  445. /** Position of the AbstractCpsObject which should be aligned */
  446. Position p = cps.getPosition();
  447. //calculate how many pixels the cps should be decreased to align
  448. /** x offset relative to a grid with lines every distance pixels */
  449. int x_off = cps.getPosition().x % distance;
  450. /** y offset relative to a grid with lines every distance pixels */
  451. int y_off = cps.getPosition().y % distance;
  452. //align to the other Line, if it is nearer
  453. if(x_off > distance/2)
  454. x_off -= distance;
  455. if(y_off > distance/2)
  456. y_off -= distance;
  457. /** set new Position */
  458. cps.setPosition(p.x-x_off, p.y-y_off);
  459. }
  460. /**
  461. * Stops Editing in HolonElementTable and PropertyTable
  462. */
  463. protected void stopEditing() {
  464. /**
  465. * Stop Editing, if mouse exits the Table
  466. */
  467. JTable holElem = model.getTableHolonElement();
  468. CellEditor cellEditor = holElem.getCellEditor();
  469. if (cellEditor != null) {
  470. if (cellEditor.getCellEditorValue() != null) {
  471. /** TODO: Maybe try to save current Data */
  472. cellEditor.stopCellEditing();
  473. } else {
  474. cellEditor.cancelCellEditing();
  475. }
  476. }
  477. JTable propertys = model.getTableProperties();
  478. cellEditor = propertys.getCellEditor();
  479. if (cellEditor != null) {
  480. if (cellEditor.getCellEditorValue() != null) {
  481. /** TODO: Maybe try to save current Data */
  482. cellEditor.stopCellEditing();
  483. } else {
  484. cellEditor.cancelCellEditing();
  485. }
  486. }
  487. }
  488. /**
  489. * Closes a tab of the UpperNode with ID upperNodeID
  490. * @param upperNodeId
  491. */
  492. public abstract void closeUpperNodeTab(int upperNodeId);
  493. }