Model.java 15 KB

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