Model.java 14 KB

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