Model.java 12 KB

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