Main.java 9.5 KB

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