Model.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. package ui.model;
  2. import java.awt.Color;
  3. import java.awt.RenderingHints;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.LinkedList;
  7. import java.util.List;
  8. import javax.swing.JTable;
  9. import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader.Array;
  10. import classes.Category;
  11. import classes.CpsEdge;
  12. import classes.CpsUpperNode;
  13. import classes.AbstractCpsObject;
  14. import classes.HolonElement;
  15. import classes.HolonObject;
  16. import classes.HolonSwitch;
  17. import interfaces.CategoryListener;
  18. import interfaces.GraphListener;
  19. import interfaces.ObjectListener;
  20. import ui.view.Console;
  21. import ui.view.DefaulTable;
  22. import ui.view.PropertyTable;
  23. import ui.view.DefaulTable;
  24. /**
  25. * The Class Model is the class where everything is saved. All changes made to
  26. * the Data is managed via a controller.
  27. *
  28. * @author Gruppe14
  29. *
  30. */
  31. public class Model {
  32. // Global Variables
  33. private int canvasX = 1000;
  34. private int canvasY = 1000;
  35. private static int sCALE = 50; // Picture Scale
  36. private static int sCALEdIV2 = sCALE / 2;
  37. private static int holonBodysCALE = 100; // Picture Scale
  38. private static final int ITERATIONS = 100;
  39. private int curIteration = 0;
  40. private LinkedList<Color> subNetColors = new LinkedList<>();
  41. // ID of the Selected Object
  42. private AbstractCpsObject selectedCpsObject = null;
  43. private HolonElement selectedHolonElement;
  44. private CpsEdge selectedEdge;
  45. private ArrayList<AbstractCpsObject> selectedObjects = new ArrayList<AbstractCpsObject>();
  46. private ArrayList<AbstractCpsObject> clipboardObjects = new ArrayList<AbstractCpsObject>();
  47. private Console console;
  48. private HashMap<Integer, ArrayList<HolonElement>> eleToDelete;
  49. // Capacity for Edge
  50. private float maxCapacity;
  51. // Table for HolonElements --> all cells are editable
  52. private JTable tableHolonElement;
  53. private ArrayList<GraphListener> graphListeners = new ArrayList<GraphListener>();
  54. // Iteration Speed
  55. private int timerSpeed = 1000;
  56. // Simulation boolean
  57. private boolean isSimulation = false;
  58. private int selectedID = 0;
  59. // number of the current autosave
  60. private int autoSaveNr = -1;
  61. // number of max simultaneous autosaves
  62. private int numberOfSaves = 35;
  63. /*
  64. * Array of all categories in the model. It is set by default with the
  65. * categories ENERGY, BUILDINGS and COMPONENTS
  66. */
  67. private ArrayList<Category> categories;
  68. /*
  69. * Array of all HolonObj and HolonSwitches, that should be tracked through
  70. * out the statistics tab
  71. */
  72. private ArrayList<AbstractCpsObject> trackedObjects;
  73. /*
  74. * Array of all CpsObjects in our canvas. It is set by default as an empty
  75. * list.
  76. */
  77. private ArrayList<AbstractCpsObject> objectsOnCanvas;
  78. private HashMap<String, Integer> cgIdx;
  79. private HashMap<Integer, Integer> cvsObjIdx;
  80. /*
  81. * Array of all CpsObjects in our canvas. It is set by default as an empty
  82. * list.
  83. */
  84. private ArrayList<CpsEdge> edgesOnCanvas;
  85. /*
  86. * Array for all Listeners
  87. */
  88. private List<CategoryListener> categoryListeners;
  89. private List<ObjectListener> objectListeners;
  90. private PropertyTable tableModelHolonElementMulti;
  91. private PropertyTable tableModelHolonElementSingle;
  92. private DefaulTable tableModelProperties;
  93. public String[] colNames = { "Field", "Information" };
  94. /*
  95. * Object that runs the Algorithm
  96. */
  97. private Object algorithm = null;
  98. private int selectedHolonBody;
  99. /**
  100. * Constructor for the model. It initializes the categories and
  101. * objectsOnCanvas by default values. Listeners are also initialized by
  102. * default values.
  103. */
  104. public Model() {
  105. setCategories(new ArrayList<Category>());
  106. setObjectsOnCanvas(new ArrayList<AbstractCpsObject>());
  107. setEdgesOnCanvas(new ArrayList<CpsEdge>());
  108. setCategoryListeners(new LinkedList<CategoryListener>());
  109. setObjectListeners(new LinkedList<ObjectListener>());
  110. setCgIdx(new HashMap<String, Integer>());
  111. setCvsObjIdx(new HashMap<Integer, Integer>());
  112. setClipboradObjects(new ArrayList<AbstractCpsObject>());
  113. setTrackingObj(new ArrayList<AbstractCpsObject>());
  114. setEleToDelete(new HashMap<Integer, ArrayList<HolonElement>>());
  115. setSingleTable(new PropertyTable());
  116. setMultiTable(new PropertyTable());
  117. setPropertyTable(new DefaulTable(1000, colNames.length));
  118. getPropertyTable().setColumnIdentifiers(colNames);
  119. setTableHolonElement(new JTable());
  120. }
  121. /**
  122. * Returns all Categories.
  123. *
  124. * @return the categories
  125. */
  126. public ArrayList<Category> getCategories() {
  127. return categories;
  128. }
  129. /**
  130. * Sets all Categories.
  131. *
  132. * @param categories
  133. * the categories to set
  134. */
  135. public void setCategories(ArrayList<Category> categories) {
  136. this.categories = categories;
  137. }
  138. /**
  139. * Transform the Arraylist of categories into a string of all objectName
  140. * with a separation (',') between each name.
  141. *
  142. * @return String of all names separeted by ','
  143. */
  144. public String toStringCat() {
  145. String text = "";
  146. for (int i = 0; i < categories.size(); i++) {
  147. if (text == "") {
  148. text = categories.get(i).getName();
  149. } else {
  150. text = text + ", " + categories.get(i).getName();
  151. }
  152. }
  153. return text;
  154. }
  155. /**
  156. * Returns all Objects on the Canvas.
  157. *
  158. * @return the objectsOnCanvas
  159. */
  160. public ArrayList<AbstractCpsObject> getObjectsOnCanvas() {
  161. return objectsOnCanvas;
  162. }
  163. /**
  164. * Sets all Objects on the Canvas.
  165. *
  166. * @param objectsOnCanvas
  167. * the objectsOnCanvas to set
  168. */
  169. public void setObjectsOnCanvas(ArrayList<AbstractCpsObject> objectsOnCanvas) {
  170. this.objectsOnCanvas = objectsOnCanvas;
  171. }
  172. /**
  173. * Get all Edges on the Canvas.
  174. *
  175. * @return the objectsOnCanvas
  176. */
  177. public ArrayList<CpsEdge> getEdgesOnCanvas() {
  178. return edgesOnCanvas;
  179. }
  180. /**
  181. * Adds an Edge to The Canvas.
  182. *
  183. * @param edge
  184. * the edgesOnCanvas to add
  185. */
  186. public void addEdgeOnCanvas(CpsEdge edge) {
  187. this.edgesOnCanvas.add(edge);
  188. }
  189. /**
  190. * Remove an edge from the Canvas.
  191. *
  192. * @param edge
  193. * the edge to remove
  194. */
  195. public void removeEdgesOnCanvas(CpsEdge edge) {
  196. this.edgesOnCanvas.remove(edge);
  197. }
  198. /**
  199. * Sets the edges on the Canvas.
  200. *
  201. * @param arrayList
  202. * the edgesOnCanvas to set
  203. */
  204. public void setEdgesOnCanvas(ArrayList<CpsEdge> arrayList) {
  205. this.edgesOnCanvas = arrayList;
  206. }
  207. /**
  208. * Returns the ObjectListener.
  209. *
  210. * @return the objectListeners
  211. */
  212. public List<ObjectListener> getObjectListeners() {
  213. return objectListeners;
  214. }
  215. /**
  216. * Sets the ObjectListener.
  217. *
  218. * @param linkedList
  219. * the objectListeners to set
  220. */
  221. public void setObjectListeners(LinkedList<ObjectListener> linkedList) {
  222. this.objectListeners = linkedList;
  223. }
  224. /**
  225. * Returns the CategorieListener.
  226. *
  227. * @return the categoryListeners
  228. */
  229. public List<CategoryListener> getCategoryListeners() {
  230. return categoryListeners;
  231. }
  232. /**
  233. * Sets the CategorieListener.
  234. *
  235. * @param linkedList
  236. * the categoryListeners to set
  237. */
  238. public void setCategoryListeners(LinkedList<CategoryListener> linkedList) {
  239. this.categoryListeners = linkedList;
  240. }
  241. /**
  242. * Set the ID of the selected Object 0 = no Object is selected.
  243. *
  244. * @param id
  245. * the ID
  246. *
  247. */
  248. public void setSelectedObjectID(int id) {
  249. this.selectedID = id;
  250. }
  251. /**
  252. * Returns the ID of the selected Object 0 = no Object is selected.
  253. *
  254. * @return ID
  255. */
  256. public int getSelectedObjectID() {
  257. return selectedID;
  258. }
  259. /**
  260. * Returns the Selected Cps Object.
  261. *
  262. * @return selected Cps Object
  263. */
  264. public AbstractCpsObject getSelectedCpsObject() {
  265. return selectedCpsObject;
  266. }
  267. /**
  268. * Set the Selected Objecs.
  269. *
  270. * @param selectedCpsObject
  271. * Objects that are selected
  272. */
  273. public void setSelectedCpsObject(AbstractCpsObject selectedCpsObject) {
  274. this.selectedCpsObject = selectedCpsObject;
  275. }
  276. /**
  277. * Returns all selected Objects on the Canvas.
  278. *
  279. * @return The selected Objects
  280. */
  281. public ArrayList<AbstractCpsObject> getSelectedCpsObjects() {
  282. return selectedObjects;
  283. }
  284. /**
  285. * Returns all selected Objects on the Canvas.
  286. *
  287. * @return The selected Objects
  288. */
  289. public void setSelectedCpsObjects(ArrayList<AbstractCpsObject> arr) {
  290. this.selectedObjects = arr;
  291. }
  292. /**
  293. * Returns the Selected Holon Element.
  294. *
  295. * @return selected Holon Element
  296. */
  297. public HolonElement getSelectedHolonElement() {
  298. return selectedHolonElement;
  299. }
  300. /**
  301. * Sets the Selecte HolonElement.
  302. *
  303. * @param selectedHolonElement
  304. * that is Selected
  305. */
  306. public void setSelectedHolonElement(HolonElement selectedHolonElement) {
  307. this.selectedHolonElement = selectedHolonElement;
  308. }
  309. /**
  310. * Returns the sCale (Scale for the Images).
  311. *
  312. * @return sCALE
  313. */
  314. public int getScale() {
  315. return sCALE;
  316. }
  317. /**
  318. * Returns sCALEdIV2 (The Scale divided by 2).
  319. *
  320. * @return sCALEdIV2
  321. */
  322. public int getScaleDiv2() {
  323. return sCALEdIV2;
  324. }
  325. /**
  326. * Sets the Image Scale.
  327. *
  328. * @param scale
  329. * for the image
  330. */
  331. public void setScale(int scale) {
  332. sCALE = scale;
  333. sCALEdIV2 = sCALE / 2;
  334. }
  335. /**
  336. * Returns ITERATIONS.
  337. *
  338. * @return ITERATIONS
  339. */
  340. public int getIterations() {
  341. return ITERATIONS;
  342. }
  343. /**
  344. * sets the current Iteration.
  345. *
  346. * @param curIT
  347. * the current Iteration
  348. */
  349. public void setCurIteration(int curIT) {
  350. this.curIteration = curIT;
  351. notifyGraphListeners();
  352. }
  353. private void notifyGraphListeners() {
  354. for (GraphListener gl : graphListeners) {
  355. gl.repaintTree();
  356. }
  357. }
  358. /**
  359. * Returns cURiTERATION.
  360. *
  361. * @return cURiTERATION
  362. */
  363. public int getCurIteration() {
  364. return curIteration;
  365. }
  366. /**
  367. * Set the selected Edge.
  368. *
  369. * @param edge
  370. * that is selected
  371. *
  372. */
  373. public void setSelectedEdge(CpsEdge edge) {
  374. this.selectedEdge = edge;
  375. }
  376. /**
  377. * Returns the selected Edge.
  378. *
  379. * @return selectedEdge
  380. */
  381. public CpsEdge getSelectedEdge() {
  382. return selectedEdge;
  383. }
  384. /**
  385. * Returns the Categorie Index.
  386. *
  387. * @return the cgIdx
  388. */
  389. public HashMap<String, Integer> getCgIdx() {
  390. return cgIdx;
  391. }
  392. /**
  393. * Sets the Categorie Index.
  394. *
  395. * @param cgIdx
  396. * the cgIdx to set
  397. */
  398. public void setCgIdx(HashMap<String, Integer> cgIdx) {
  399. this.cgIdx = cgIdx;
  400. }
  401. /**
  402. * Returns the CanvasObject Index.
  403. *
  404. * @return the cvsObjIdx
  405. */
  406. public HashMap<Integer, Integer> getCvsObjIdx() {
  407. return cvsObjIdx;
  408. }
  409. /**
  410. * Sets the CanvasObject Index.
  411. *
  412. * @param cvsObjIdx
  413. * the cvsObjIdx to set
  414. */
  415. public void setCvsObjIdx(HashMap<Integer, Integer> cvsObjIdx) {
  416. this.cvsObjIdx = cvsObjIdx;
  417. }
  418. /**
  419. * Sets the auto save Number.
  420. *
  421. * @param autoSaveNr
  422. * the auto save number
  423. */
  424. public void setAutoSaveNr(int autoSaveNr) {
  425. this.autoSaveNr = autoSaveNr;
  426. }
  427. /**
  428. * Returns the auto save Number.
  429. *
  430. * @return the auto save Number
  431. */
  432. public int getAutoSaveNr() {
  433. return autoSaveNr;
  434. }
  435. /**
  436. * Returns the Number of Saves.
  437. *
  438. * @return the numberOfSaves
  439. */
  440. public int getNumberOfSaves() {
  441. return numberOfSaves;
  442. }
  443. /**
  444. * Set the Number of Saves.
  445. *
  446. * @param numberOfSaves
  447. * the numberOfSaves to set
  448. */
  449. public void setNumberOfSaves(int numberOfSaves) {
  450. this.numberOfSaves = numberOfSaves;
  451. }
  452. /**
  453. * Sets the ClipboardObjects.
  454. *
  455. * @param c
  456. * Array of Objects
  457. */
  458. public void setClipboradObjects(ArrayList<AbstractCpsObject> c) {
  459. this.clipboardObjects = c;
  460. }
  461. /**
  462. * Returns all Objects in the Clipboard.
  463. *
  464. * @return Objects in the Clipboard
  465. */
  466. public ArrayList<AbstractCpsObject> getClipboradObjects() {
  467. return clipboardObjects;
  468. }
  469. /**
  470. * Sets the console.
  471. *
  472. * @param console
  473. * the console
  474. */
  475. public void setConsole(Console console) {
  476. this.console = console;
  477. }
  478. /**
  479. * Returns the Console.
  480. *
  481. * @return console the console
  482. */
  483. public Console getConsole() {
  484. return console;
  485. }
  486. /**
  487. * @return the maxCapacity
  488. */
  489. public float getMaxCapacity() {
  490. return maxCapacity;
  491. }
  492. /**
  493. * @param maxCapacity
  494. * the maxCapacity to set
  495. */
  496. public void setMaxCapacity(float maxCapacity) {
  497. this.maxCapacity = maxCapacity;
  498. }
  499. /**
  500. * Sets the Interval in ms between each Iteration.
  501. *
  502. * @param t
  503. * speed for the Iterations
  504. */
  505. public void setTimerSpeed(int t) {
  506. this.timerSpeed = t;
  507. }
  508. /**
  509. * get the Interval in ms between each Iteration.
  510. *
  511. * @return timerSpeed speed for the Iterations
  512. */
  513. public int getTimerSpeed() {
  514. return this.timerSpeed;
  515. }
  516. /**
  517. * Sets the Simulation state (true = simulation, false = modeling).
  518. *
  519. * @param isSimulation
  520. * boolean for for isSimulation
  521. */
  522. public void setIsSimulation(boolean isSimulation) {
  523. this.isSimulation = isSimulation;
  524. }
  525. /**
  526. * Returns the Simulation state (true = simulation, false = modeling).
  527. *
  528. * @return isSimulation boolean for for isSimulation
  529. */
  530. public boolean getIsSimulation() {
  531. return this.isSimulation;
  532. }
  533. /**
  534. * Get Canvas X Size.
  535. *
  536. * @return the cANVAS_X
  537. */
  538. public int getCanvasX() {
  539. return canvasX;
  540. }
  541. /**
  542. * Set Canvas X Size.
  543. *
  544. * @param canvasX
  545. * the cANVAS_X to set
  546. */
  547. public void setCanvasX(int canvasX) {
  548. this.canvasX = canvasX;
  549. }
  550. /**
  551. * get Canvas Y size.
  552. *
  553. * @return the cANVAS_Y
  554. */
  555. public int getCanvasY() {
  556. return canvasY;
  557. }
  558. /**
  559. * Set Canvas Y size.
  560. *
  561. * @param canvasY
  562. * the cANVAS_Y to set
  563. */
  564. public void setCanvasY(int canvasY) {
  565. this.canvasY = canvasY;
  566. }
  567. /**
  568. * get the Algorithm.
  569. *
  570. * @return the Algorithm
  571. */
  572. public Object getAlgorithm() {
  573. return algorithm;
  574. }
  575. /**
  576. * Set the Algorithm.
  577. *
  578. * @param obj
  579. * the Algorithm
  580. */
  581. public void setAlgorithm(Object obj) {
  582. this.algorithm = null;
  583. this.algorithm = obj;
  584. }
  585. /**
  586. * Add a SubNetColor.
  587. *
  588. * @param c
  589. * the Color
  590. */
  591. public void addSubNetColor(Color c) {
  592. this.subNetColors.add(c);
  593. }
  594. /**
  595. * Get the SubNetColors.
  596. *
  597. * @return SubNetColors
  598. */
  599. public LinkedList<Color> getSubNetColors() {
  600. return this.subNetColors;
  601. }
  602. public void setTrackingObj(ArrayList<AbstractCpsObject> toTrack) {
  603. trackedObjects = toTrack;
  604. }
  605. public ArrayList<AbstractCpsObject> getTrackingObj() {
  606. return trackedObjects;
  607. }
  608. public void addGraphListener(GraphListener gl) {
  609. graphListeners.add(gl);
  610. }
  611. public void setEleToDelete(HashMap<Integer, ArrayList<HolonElement>> theHash) {
  612. this.eleToDelete = theHash;
  613. }
  614. public HashMap<Integer, ArrayList<HolonElement>> getEleToDelete() {
  615. return this.eleToDelete;
  616. }
  617. public void setSingleTable(PropertyTable pt) {
  618. this.tableModelHolonElementSingle = pt;
  619. }
  620. public PropertyTable getSingleTable() {
  621. return this.tableModelHolonElementSingle;
  622. }
  623. public PropertyTable getMultiTable() {
  624. return this.tableModelHolonElementMulti;
  625. }
  626. public void setMultiTable(PropertyTable pt) {
  627. this.tableModelHolonElementMulti = pt;
  628. }
  629. public void addObjectsToGraphListeners() {
  630. for (GraphListener gl : graphListeners) {
  631. gl.addTrackedObject(trackedObjects);
  632. gl.repaintTree();
  633. }
  634. }
  635. public DefaulTable getPropertyTable() {
  636. return this.tableModelProperties;
  637. }
  638. public void setPropertyTable(DefaulTable pt) {
  639. this.tableModelProperties = pt;
  640. }
  641. public JTable getTableHolonElement() {
  642. return tableHolonElement;
  643. }
  644. public void setTableHolonElement(JTable tableHolonElement) {
  645. this.tableHolonElement = tableHolonElement;
  646. }
  647. /**
  648. * Sets the HolonBody Scale.
  649. *
  650. * @param scale
  651. * for the HolonBody
  652. */
  653. public void setHolonBodyScale(int scale) {
  654. holonBodysCALE = scale;
  655. }
  656. /**
  657. * Returns the sCale (Scale for the Images).
  658. *
  659. * @return sCALE
  660. */
  661. public int getHolonBodyScale() {
  662. return holonBodysCALE;
  663. }
  664. /**
  665. * Sets the ID of the selected HolonBody
  666. *
  667. * @param i
  668. * int
  669. */
  670. public void setSelectedHolonBody(int i) {
  671. selectedHolonBody = i;
  672. }
  673. /**
  674. * Returns the ID of the selected HolonBody
  675. *
  676. * @return selectedHolonBody
  677. */
  678. public int getSelectedHolonBody() {
  679. return selectedHolonBody;
  680. }
  681. /**
  682. * get all Switches
  683. */
  684. public ArrayList<HolonSwitch> getSwitches() {
  685. ArrayList<HolonSwitch> switches = new ArrayList<>();
  686. for (AbstractCpsObject obj : getObjectsOnCanvas()) {
  687. if (obj instanceof HolonSwitch) {
  688. switches.add((HolonSwitch) obj);
  689. } else if (obj instanceof CpsUpperNode) {
  690. getSwitchesRec(((CpsUpperNode) obj).getNodes(), switches);
  691. }
  692. }
  693. return switches;
  694. }
  695. /**
  696. * get the Amount of Switches help function
  697. *
  698. * @param objects
  699. * objects
  700. * @param switches
  701. * List of switches
  702. */
  703. private ArrayList<HolonSwitch> getSwitchesRec(ArrayList<AbstractCpsObject> objects,
  704. ArrayList<HolonSwitch> switches) {
  705. for (AbstractCpsObject obj : objects) {
  706. if (obj instanceof HolonSwitch) {
  707. switches.add((HolonSwitch) obj);
  708. } else if (obj instanceof CpsUpperNode) {
  709. getSwitchesRec(((CpsUpperNode) obj).getNodes(), switches);
  710. }
  711. }
  712. return switches;
  713. }
  714. }