Model.java 10 KB

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