Model.java 12 KB

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