Model.java 11 KB

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