AbstractCanvas.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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. protected class ACpsHandle{
  209. public AbstractCpsObject object;
  210. ACpsHandle(AbstractCpsObject object){
  211. this.object = object;
  212. }
  213. public String toString() {
  214. return object.toString();
  215. }
  216. }
  217. void drawMarker() {
  218. if (sx > x && sy > y) {
  219. g2.drawRect(x, y, sx - x, sy - y);
  220. } else if (sx < x && sy < y) {
  221. g2.drawRect(sx, sy, x - sx, y - sy);
  222. } else if (sx >= x) {
  223. g2.drawRect(x, sy, sx - x, y - sy);
  224. } else if (sy >= y) {
  225. g2.drawRect(sx, y, x - sx, sy - y);
  226. }
  227. }
  228. /**
  229. * @deprecated
  230. * @param g
  231. */
  232. void showTooltip(Graphics g) {
  233. if (toolTip) {
  234. g2.setColor(new Color(255, 225, 150));
  235. g2.setStroke(new BasicStroke(1));
  236. int textWidth = g.getFontMetrics().stringWidth(toolTipText) + 2; // Text
  237. // width
  238. // fixed x and y Position to the screen
  239. int fixXPos = toolTipPos.x - (textWidth >> 1) + model.getScaleDiv2();
  240. int fixYPos = toolTipPos.y;
  241. if (fixXPos < 0) {
  242. fixXPos = 0;
  243. } else if (fixXPos + textWidth + 1 > this.getWidth()) {
  244. fixXPos -= (fixXPos + textWidth + 1) - this.getWidth();
  245. }
  246. if (fixYPos + 16 > this.getHeight()) {
  247. fixYPos -= (fixYPos + 16) - this.getHeight();
  248. }
  249. g2.fillRect(fixXPos, fixYPos, textWidth, 15);
  250. g2.setColor(Color.BLACK);
  251. g2.drawRect(fixXPos, fixYPos, textWidth, 15);
  252. g2.drawString(toolTipText, fixXPos + 2, fixYPos + 12);
  253. }
  254. }
  255. void setRightClickMenu(MouseEvent e) {
  256. if (e.getButton() == MouseEvent.BUTTON3) {
  257. itemPaste.setEnabled(true);
  258. if (tempCps != null) {
  259. itemPaste.setEnabled(true);
  260. itemDelete.setEnabled(true);
  261. itemCut.setEnabled(true);
  262. itemCopy.setEnabled(true);
  263. itemAlign.setEnabled(true);
  264. // tracking
  265. if (tempCps != null) {
  266. itemGroup.setEnabled(true);
  267. itemTrack.setEnabled(true);
  268. itemUntrack.setEnabled(true);
  269. }
  270. // ungrouping
  271. if (tempCps instanceof CpsUpperNode)
  272. itemUngroup.setEnabled(true);
  273. else
  274. itemUngroup.setEnabled(false);
  275. if (model.getSelectedCpsObjects().size() == 0) {
  276. controller.addSelectedObject(tempCps);
  277. }
  278. if (tempCps instanceof HolonObject) {
  279. itemCreateTemplate.setEnabled(true);
  280. } else {
  281. itemCreateTemplate.setEnabled(false);
  282. }
  283. } else {
  284. itemAlign.setEnabled(false);
  285. itemCut.setEnabled(false);
  286. itemCopy.setEnabled(false);
  287. itemGroup.setEnabled(false);
  288. itemUngroup.setEnabled(false);
  289. itemTrack.setEnabled(false);
  290. itemUntrack.setEnabled(false);
  291. itemCreateTemplate.setEnabled(false);
  292. if (edgeHighlight != null) {
  293. itemDelete.setEnabled(true);
  294. itemPaste.setEnabled(false);
  295. } else {
  296. itemDelete.setEnabled(false);
  297. itemPaste.setEnabled(true);
  298. }
  299. }
  300. mousePosition = this.getMousePosition();
  301. popmenu.show(e.getComponent(), e.getX(), e.getY());
  302. }
  303. }
  304. void markObjects() {
  305. if (doMark) {
  306. doMark = false;
  307. for (AbstractCpsObject cps : tempSelected) {
  308. if (!model.getSelectedCpsObjects().contains(cps)) {
  309. controller.addSelectedObject(cps);
  310. }
  311. }
  312. controller.getObjectsInDepth();
  313. tempSelected.clear();
  314. }
  315. }
  316. int[] determineMousePositionOnEdge(CpsEdge p) {
  317. int lx, ly, hx, hy;
  318. if (p.getA().getPosition().x > p.getB().getPosition().x) {
  319. hx = p.getA().getPosition().x + model.getScaleDiv2() + 7;
  320. lx = p.getB().getPosition().x + model.getScaleDiv2() - 7;
  321. } else {
  322. lx = p.getA().getPosition().x + model.getScaleDiv2() - 7;
  323. hx = p.getB().getPosition().x + model.getScaleDiv2() + 7;
  324. }
  325. if (p.getA().getPosition().y > p.getB().getPosition().y) {
  326. hy = p.getA().getPosition().y + model.getScaleDiv2() + 7;
  327. ly = p.getB().getPosition().y + model.getScaleDiv2() - 7;
  328. } else {
  329. ly = p.getA().getPosition().y + model.getScaleDiv2() - 7;
  330. hy = p.getB().getPosition().y + model.getScaleDiv2() + 7;
  331. }
  332. return new int[] { lx, ly, hx, hy };
  333. }
  334. /**
  335. * Checks if a double click was made.
  336. *
  337. * @return true if doublecklick, false if not
  338. */
  339. boolean doubleClick() {
  340. if (click) {
  341. click = false;
  342. return true;
  343. } else {
  344. click = true;
  345. java.util.Timer t = new java.util.Timer("doubleclickTimer", false);
  346. t.schedule(new TimerTask() {
  347. @Override
  348. public void run() {
  349. click = false;
  350. }
  351. }, 500);
  352. }
  353. return false;
  354. }
  355. boolean setToolTipInfoAndPosition(boolean on, AbstractCpsObject cps) {
  356. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
  357. on = true;
  358. toolTipPos.x = cps.getPosition().x - controller.getScaleDiv2();
  359. toolTipPos.y = cps.getPosition().y + controller.getScaleDiv2();
  360. toolTipText = cps.getName() + ", " + cps.getId();
  361. }
  362. return on;
  363. }
  364. abstract void drawDeleteEdge();
  365. void triggerUpdateController() {
  366. updCon.paintProperties(tempCps);
  367. updCon.refreshTableHolonElement(model.getMultiTable(), model.getSingleTable());
  368. updCon.refreshTableProperties(model.getPropertyTable());
  369. }
  370. /**
  371. * Checks if {@code draggedCps} or a new cpsObject at Position (x,y) could replace exactly one object
  372. * in {@code objects}.
  373. * Saves the object that would be replaced in {@link AbstractCanvas}.{@code MayBeReplaced}
  374. * @param objects list of objects that could be replaced
  375. * @param draggedCps Object that might replace
  376. * @param x Position of the objects that might replace
  377. * @param y Position of the objects that might replace
  378. * @return true if exactly one Object could be replaced
  379. */
  380. protected boolean checkForReplacement(ArrayList<AbstractCpsObject> objects, AbstractCpsObject draggedCps, int x, int y){
  381. /** distance treshold for replacement */
  382. int treshhold = controller.getScale()/2;
  383. /** number of Objects that might be replaced (should be 1) */
  384. int replaceCounter = 0;
  385. /** last object that could be replaced */
  386. AbstractCpsObject toBeReplaced = null;
  387. /** Position of object that might be replaced */
  388. Position p;
  389. /** for each cps on Canvas */
  390. if(draggedCps == null || !(draggedCps instanceof CpsNode) && !(draggedCps instanceof CpsNode)){
  391. for (AbstractCpsObject cps : objects){
  392. /** same object -> ignore */
  393. if(cps == draggedCps)continue;
  394. /** set Position of object that might be replaced */
  395. p = cps.getPosition();
  396. /** if near enough */
  397. if(Math.abs(x-p.x)<treshhold && Math.abs(y-p.y)<treshhold){
  398. replaceCounter++;
  399. toBeReplaced = cps;
  400. /**
  401. * if too many Objects could be replaced:
  402. * stop searching, because it would not be clear which one should
  403. * be replaced
  404. */
  405. if(replaceCounter>1)break;
  406. }
  407. }
  408. }
  409. /**
  410. * return true if exactly one obect would be replaced
  411. */
  412. if( replaceCounter == 1 && toBeReplaced != null){
  413. mayBeReplaced = toBeReplaced;
  414. return true;
  415. }else{
  416. mayBeReplaced = null;
  417. return false;
  418. }
  419. }
  420. /**
  421. * Checks if an inserted new Object could replace exactly one object on the canvas.
  422. * Saves the object that would be replaced in {@link AbstractCanvas}.{@code MayBeReplaced}
  423. * @param x Position of the objects that might replace
  424. * @param y Position of the objects that might replace
  425. * @return true if exactly one Object could be replaced
  426. */
  427. public abstract boolean checkForReplacement(int x, int y);
  428. /**
  429. * highlights the object that mayBeReplaced
  430. * @param g2
  431. */
  432. protected void highlightMayBeReplaced(Graphics2D g2) {
  433. if(mayBeReplaced != null){
  434. g2.setColor(Color.RED);
  435. g2.fillRect(
  436. (int) (mayBeReplaced.getPosition().x
  437. - controller.getScaleDiv2() - (scalediv20 + 3)),
  438. (int) (mayBeReplaced.getPosition().y
  439. - controller.getScaleDiv2() - (scalediv20 + 3)),
  440. (int) (controller.getScale() + ((scalediv20 + 3) * 2)),
  441. (int) (controller.getScale() + ((scalediv20 + 3) * 2)));
  442. }
  443. }
  444. /**
  445. * Align alle Objects on the Canvas to a Grid with objects every 10 pixels
  446. */
  447. public abstract void tryToAlignObjects();
  448. /**
  449. * Aligns the Object the a grid
  450. * @param cps Object that should be aligned
  451. * @param distance distance between the AlignmentGrid Lines. (objects every 'distance' pixels
  452. */
  453. protected void align(AbstractCpsObject cps, int distance) {
  454. /** Position of the AbstractCpsObject which should be aligned */
  455. Position p = cps.getPosition();
  456. //calculate how many pixels the cps should be decreased to align
  457. /** x offset relative to a grid with lines every distance pixels */
  458. int x_off = cps.getPosition().x % distance;
  459. /** y offset relative to a grid with lines every distance pixels */
  460. int y_off = cps.getPosition().y % distance;
  461. //align to the other Line, if it is nearer
  462. if(x_off > distance/2)
  463. x_off -= distance;
  464. if(y_off > distance/2)
  465. y_off -= distance;
  466. /** set new Position */
  467. cps.setPosition(p.x-x_off, p.y-y_off);
  468. }
  469. /**
  470. * Stops Editing in HolonElementTable and PropertyTable
  471. */
  472. protected void stopEditing() {
  473. /**
  474. * Stop Editing, if mouse exits the Table
  475. */
  476. JTable holElem = model.getTableHolonElement();
  477. CellEditor cellEditor = holElem.getCellEditor();
  478. if (cellEditor != null) {
  479. if (cellEditor.getCellEditorValue() != null) {
  480. /** TODO: Maybe try to save current Data */
  481. cellEditor.stopCellEditing();
  482. } else {
  483. cellEditor.cancelCellEditing();
  484. }
  485. }
  486. JTable propertys = model.getTableProperties();
  487. cellEditor = propertys.getCellEditor();
  488. if (cellEditor != null) {
  489. if (cellEditor.getCellEditorValue() != null) {
  490. /** TODO: Maybe try to save current Data */
  491. cellEditor.stopCellEditing();
  492. } else {
  493. cellEditor.cancelCellEditing();
  494. }
  495. }
  496. }
  497. /**
  498. * Closes a tab of the UpperNode with ID upperNodeID
  499. * @param upperNodeId
  500. */
  501. public abstract void closeUpperNodeTab(int upperNodeId);
  502. }