Main.java 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. package de.tu_darmstadt.informatik.tk.scopviz.main;
  2. import java.math.BigDecimal;
  3. import java.math.BigInteger;
  4. import de.tu_darmstadt.informatik.tk.scopviz.graphs.GraphManager;
  5. import de.tu_darmstadt.informatik.tk.scopviz.graphs.MyGraph;
  6. import de.tu_darmstadt.informatik.tk.scopviz.io.GraphMLExporter;
  7. import de.tu_darmstadt.informatik.tk.scopviz.ui.GraphDisplayManager;
  8. import de.tu_darmstadt.informatik.tk.scopviz.ui.OptionsManager;
  9. import de.tu_darmstadt.informatik.tk.scopviz.ui.handlers.MyAnimationTimer;
  10. import javafx.animation.AnimationTimer;
  11. import javafx.stage.Stage;
  12. /**
  13. * Main Class to contain all core functionality. Built as a Singleton, use
  14. * getInstance() to get access to the functionality
  15. *
  16. * @author Jan Enders (jan.enders@stud.tu-darmstadt.de)
  17. * @version 1.0
  18. *
  19. */
  20. public final class Main {
  21. /**
  22. * Singular instance of the Class, facilitates Singleton pattern.
  23. */
  24. private static Main instance;
  25. /**
  26. * Current mode of the application for creating new Nodes and Edges.
  27. */
  28. private CreationMode creationMode = CreationMode.CREATE_NONE;
  29. /**
  30. * The root window of the application.
  31. */
  32. private Stage primaryStage;
  33. /**
  34. * Private constructor to prevent initialization, facilitates Singleton
  35. * pattern. Initializes an AnimationTimer to call all Functionality that
  36. * needs to be executed every Frame.
  37. */
  38. private Main() {
  39. AnimationTimer alwaysPump = new MyAnimationTimer();
  40. alwaysPump.start();
  41. }
  42. /**
  43. * Returns the singular instance of the Class, grants access to the
  44. * Singleton. Initializes the instance when called for the first time.
  45. *
  46. * @return the singular instance of the class
  47. */
  48. public static Main getInstance() {
  49. if (instance == null) {
  50. initialize();
  51. }
  52. return instance;
  53. }
  54. /**
  55. * Initializes the singular instance.
  56. */
  57. private static void initialize() {
  58. instance = new Main();
  59. }
  60. /**
  61. * Returns a reference to the GraphManager object currently used by the app.
  62. *
  63. * @return the visualizer in use
  64. */
  65. public GraphManager getGraphManager() {
  66. return GraphDisplayManager.getGraphManager();
  67. }
  68. /**
  69. * Returns a unique id for a new Node or Edge not yet used by the graph.
  70. *
  71. * @return a new unused id as a String
  72. */
  73. public String getUnusedID() {
  74. int i = 0;
  75. while (true) {
  76. String tempID = i + "";
  77. if (getGraphManager().getGraph().getNode(tempID) == null
  78. && getGraphManager().getGraph().getEdge(tempID) == null
  79. && (getGraphManager().getActiveSubGraph() == null || getGraphManager().getGraph().getNode(getGraphManager().getActiveSubGraph().getId() + tempID) == null)) {
  80. return (tempID);
  81. } else {
  82. i++;
  83. }
  84. }
  85. }
  86. public String getUnusedID(GraphManager gm) {
  87. int i = 0;
  88. while (true) {
  89. String tempID = i + "";
  90. if (gm.getGraph().getNode(tempID) == null && gm.getGraph().getEdge(tempID) == null
  91. && gm.getGraph().getNode(gm.getGraph().getId() + tempID) == null) {
  92. return (tempID);
  93. } else {
  94. i++;
  95. }
  96. }
  97. }
  98. public String getUnusedID(MyGraph g) {
  99. int i = 0;
  100. while (true) {
  101. String tempID = i + "";
  102. if (g.getNode(tempID) == null && g.getEdge(tempID) == null
  103. && g.getNode(g.getId() + tempID) == null) {
  104. return (tempID);
  105. } else {
  106. i++;
  107. }
  108. }
  109. }
  110. /**
  111. * Returns the primary Stage for the Application Window.
  112. *
  113. * @return the primary Stage
  114. */
  115. public Stage getPrimaryStage() {
  116. return primaryStage;
  117. }
  118. /**
  119. * Sets the Reference to the primary Stage of the Application Window.
  120. *
  121. * @param primaryStage
  122. * the primary Stage of the Window.
  123. */
  124. public void setPrimaryStage(Stage primaryStage) {
  125. this.primaryStage = primaryStage;
  126. }
  127. /**
  128. * Returns the current Creation Mode.
  129. *
  130. * @return the current creationMode
  131. */
  132. public CreationMode getCreationMode() {
  133. return creationMode;
  134. }
  135. /**
  136. * Switches the App to a given Creation Mode.
  137. *
  138. * @param creationMode
  139. * the creationMode to switch to
  140. */
  141. public void setCreationMode(CreationMode creationMode) {
  142. this.creationMode = creationMode;
  143. }
  144. // TODO replace throw by something better for debug
  145. /**
  146. * Converts a given Attribute into the type of result
  147. *
  148. * @param attribute
  149. * the Attribute to be converted. supported types: byte, short,
  150. * integer, long, float, double, BigInteger, BigDecimal, String
  151. *
  152. * @param result
  153. * the Attribute will be written in here after the conversion.
  154. * the supported types are the same as above except for String
  155. *
  156. * @return the value of result
  157. */
  158. // don't worry I checked all the conversions
  159. @SuppressWarnings("unchecked")
  160. public <T extends Number> T convertAttributeTypes(Object attribute, T result) {
  161. if (attribute == null) {
  162. return null;
  163. }
  164. String currentType = attribute.getClass().getSimpleName().toLowerCase();
  165. String targetType = result.getClass().getSimpleName().toLowerCase();
  166. switch (targetType) {
  167. case "byte":
  168. switch (currentType) {
  169. case "byte":
  170. case "short":
  171. case "integer":
  172. case "long":
  173. case "biginteger":
  174. case "float":
  175. case "double":
  176. case "bigdecimal":
  177. result = (T) new Byte(((Number) attribute).byteValue());
  178. break;
  179. case "string":
  180. result = (T) new Byte(new BigDecimal((String) attribute).byteValue());
  181. break;
  182. default:
  183. throw new IllegalArgumentException("invalid type: " + attribute.getClass());
  184. }
  185. break;
  186. case "short":
  187. switch (currentType) {
  188. case "byte":
  189. case "short":
  190. case "integer":
  191. case "long":
  192. case "biginteger":
  193. case "float":
  194. case "double":
  195. case "bigdecimal":
  196. result = (T) new Short(((Number) attribute).shortValue());
  197. break;
  198. case "string":
  199. result = (T) new Short(new BigDecimal((String) attribute).shortValue());
  200. break;
  201. default:
  202. throw new IllegalArgumentException("invalid type: " + attribute.getClass());
  203. }
  204. break;
  205. case "integer":
  206. switch (currentType) {
  207. case "byte":
  208. case "short":
  209. case "integer":
  210. case "long":
  211. case "biginteger":
  212. case "float":
  213. case "double":
  214. case "bigdecimal":
  215. result = (T) new Integer(((Number) attribute).intValue());
  216. break;
  217. case "string":
  218. result = (T) new Integer(new BigDecimal((String) attribute).intValue());
  219. break;
  220. default:
  221. throw new IllegalArgumentException("invalid type: " + attribute.getClass());
  222. }
  223. break;
  224. case "long":
  225. switch (currentType) {
  226. case "byte":
  227. case "short":
  228. case "integer":
  229. case "long":
  230. case "biginteger":
  231. case "float":
  232. case "double":
  233. case "bigdecimal":
  234. result = (T) new Long(((Number) attribute).longValue());
  235. break;
  236. case "string":
  237. result = (T) new Long(new BigDecimal((String) attribute).longValue());
  238. break;
  239. default:
  240. throw new IllegalArgumentException("invalid type: " + attribute.getClass());
  241. }
  242. break;
  243. case "biginteger":
  244. BigInteger integer;
  245. switch (currentType) {
  246. case "byte":
  247. integer = new BigInteger(Byte.toString((byte) attribute));
  248. break;
  249. case "short":
  250. integer = new BigInteger(Short.toString((short) attribute));
  251. break;
  252. case "integer":
  253. integer = new BigInteger(Integer.toString((int) attribute));
  254. break;
  255. case "long":
  256. integer = new BigInteger(Long.toString((long) attribute));
  257. break;
  258. case "float":
  259. integer = new BigInteger(Integer.toString((int) (float) attribute));
  260. break;
  261. case "double":
  262. integer = new BigInteger(Long.toString((long) (double) attribute));
  263. break;
  264. case "biginteger":
  265. integer = (BigInteger) attribute;
  266. break;
  267. case "bigdecimal":
  268. integer = ((BigDecimal) attribute).toBigInteger();
  269. break;
  270. case "string":
  271. integer = new BigDecimal((String) attribute).toBigInteger();
  272. break;
  273. default:
  274. throw new IllegalArgumentException("invalid type: " + attribute.getClass());
  275. }
  276. result = (T) integer;
  277. break;
  278. case "float":
  279. switch (currentType) {
  280. case "byte":
  281. case "short":
  282. case "integer":
  283. case "long":
  284. case "biginteger":
  285. case "float":
  286. case "double":
  287. case "bigdecimal":
  288. result = (T) new Float(((Number) attribute).floatValue());
  289. break;
  290. case "string":
  291. result = (T) new Float(new BigDecimal((String) attribute).floatValue());
  292. break;
  293. default:
  294. throw new IllegalArgumentException("invalid type: " + attribute.getClass());
  295. }
  296. break;
  297. case "double":
  298. switch (currentType) {
  299. case "byte":
  300. case "short":
  301. case "integer":
  302. case "long":
  303. case "biginteger":
  304. case "float":
  305. case "double":
  306. case "bigdecimal":
  307. result = (T) new Double(((Number) attribute).doubleValue());
  308. break;
  309. case "string":
  310. result = (T) new Double(new BigDecimal((String) attribute).doubleValue());
  311. break;
  312. default:
  313. throw new IllegalArgumentException("invalid type: " + attribute.getClass());
  314. }
  315. break;
  316. case "bigdecimal":
  317. BigDecimal decimal;
  318. switch (currentType) {
  319. case "byte":
  320. decimal = new BigDecimal((byte) attribute);
  321. break;
  322. case "short":
  323. decimal = new BigDecimal((short) attribute);
  324. break;
  325. case "integer":
  326. decimal = new BigDecimal((int) attribute);
  327. break;
  328. case "long":
  329. decimal = new BigDecimal((long) attribute);
  330. break;
  331. case "float":
  332. decimal = new BigDecimal((float) attribute);
  333. break;
  334. case "double":
  335. decimal = new BigDecimal((double) attribute);
  336. break;
  337. case "biginteger":
  338. decimal = new BigDecimal((BigInteger) attribute);
  339. break;
  340. case "bigdecimal":
  341. decimal = (BigDecimal) attribute;
  342. break;
  343. case "string":
  344. decimal = new BigDecimal((String) attribute);
  345. break;
  346. default:
  347. throw new IllegalArgumentException("invalid type: " + attribute.getClass());
  348. }
  349. result = (T) decimal;
  350. break;
  351. }
  352. return result;
  353. }
  354. public void closeProgram() {
  355. GraphMLExporter exp = new GraphMLExporter();
  356. exp.writeGraph(GraphDisplayManager.getGraphManager(Layer.UNDERLAY).getGraph(), "underlay-shutdown.graphml",
  357. false);
  358. exp.writeGraph(GraphDisplayManager.getGraphManager(Layer.OPERATOR).getGraph(), "operator-shutdown.graphml",
  359. false);
  360. OptionsManager.save();
  361. System.exit(0);
  362. }
  363. }