Model.java 11 KB

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