Model.java 13 KB

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