Model.java 15 KB

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