Model.java 15 KB

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