Model.java 13 KB

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