PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/UMLet/src/src/com/umlet/constants/Constants.java

https://github.com/dev693/swe
Java | 366 lines | 280 code | 51 blank | 35 comment | 79 complexity | d3f0288ce2c8e0a95c589ade4c43e573 MD5 | raw file
  1. // The UMLet source code is distributed under the terms of the GPL; see license.txt
  2. package com.umlet.constants;
  3. import java.awt.BasicStroke;
  4. import java.awt.Color;
  5. import java.awt.Cursor;
  6. import java.awt.Dimension;
  7. import java.awt.Point;
  8. import java.awt.RenderingHints;
  9. import java.awt.event.ActionEvent;
  10. import java.awt.event.InputEvent;
  11. import java.util.HashMap;
  12. import java.util.Map;
  13. import java.util.Vector;
  14. import javax.swing.UIManager;
  15. import com.umlet.control.Umlet;
  16. import com.umlet.gui.eclipse.EclipseGUI;
  17. // class in extra package because of security of custom elements
  18. public class Constants {
  19. /**** PLATTFORM AND JAVA SPECIFIC SETTINGS ****/
  20. public enum Os {
  21. WINDOWS, LINUX, UNIX, MAC, UNKNOWN
  22. }
  23. public enum JavaImplementation {
  24. OPEN, SUN
  25. }
  26. public enum UmletType {
  27. STANDALONE, ECLIPSE_PLUGIN
  28. }
  29. public static final Os OS;
  30. public static final JavaImplementation JAVAIMPL;
  31. public static final UmletType UMLETTYPE;
  32. static {
  33. // Initialize OS, JAVAIMPL AND UMLETTYPE variable
  34. if (System.getProperty("os.name").toUpperCase().startsWith("WINDOWS")) OS = Os.WINDOWS;
  35. else if (System.getProperty("os.name").toUpperCase().startsWith("MAC")) OS = Os.MAC;
  36. else if (System.getProperty("os.name").toUpperCase().startsWith("LINUX")) OS = Os.LINUX;
  37. else OS = Os.UNKNOWN;
  38. if (System.getProperty("java.runtime.name").toUpperCase().contains("OPEN")) JAVAIMPL = JavaImplementation.OPEN;
  39. else JAVAIMPL = JavaImplementation.SUN;
  40. if (Umlet.getInstance().getGUI() instanceof EclipseGUI) UMLETTYPE = UmletType.ECLIPSE_PLUGIN;
  41. else UMLETTYPE = UmletType.STANDALONE;
  42. }
  43. /**
  44. * This method checks if the drawing of graphics should start at pixel (1,1) instead of (0,0) or not
  45. */
  46. public static boolean displaceDrawingByOnePixel() {
  47. if (JAVAIMPL == JavaImplementation.OPEN) return true;
  48. else return false;
  49. }
  50. public static boolean isOkButtonOnLeftSide() {
  51. if (OS == Os.WINDOWS) return true;
  52. else return false;
  53. }
  54. // public static final ArrayList<JavaImplementation> displaceDrawingByOnePixel = new ArrayList<JavaImplementation>();
  55. // // A list with every os which uses Meta instead of Ctrl
  56. // public static final ArrayList<Os> replaceCtrlWithMeta = new ArrayList<Os>();
  57. // static {
  58. // displaceDrawingByOnePixel.add(JavaImplementation.OPEN);
  59. // replaceCtrlWithMeta.add(Os.MAC);
  60. // }
  61. /**** CTRL KEY - FOR META KEY ENABLING ON MAC ****/
  62. public final static String CTRLNAME;
  63. public final static int CTRLMETA_MASK;
  64. public final static int CTRLMETA_DOWN_MASK;
  65. static {
  66. if (OS == Os.MAC) {
  67. CTRLMETA_MASK = ActionEvent.META_MASK;
  68. CTRLMETA_DOWN_MASK = InputEvent.META_DOWN_MASK;
  69. CTRLNAME = "\u2318";
  70. }
  71. else {
  72. CTRLMETA_MASK = ActionEvent.CTRL_MASK;
  73. CTRLMETA_DOWN_MASK = InputEvent.CTRL_DOWN_MASK;
  74. CTRLNAME = "Ctrl";
  75. }
  76. }
  77. /**** NEWLINE CHARACTER AND DEFAULT HELP- AND MAILTEXT ****/
  78. public static final String NEWLINE = "\n";
  79. public static final String DEFAULT_HELPTEXT;
  80. public static final String DEFAULT_MAILTEXT;
  81. static {
  82. DEFAULT_HELPTEXT =
  83. "// Uncomment the following line to change the fontsize:" + NEWLINE +
  84. "// fontsize=14" + NEWLINE +
  85. "" + NEWLINE +
  86. "" + NEWLINE +
  87. "//////////////////////////////////////////////////////////////////////////////////////////////" + NEWLINE +
  88. "// Welcome to UMLet!" + NEWLINE +
  89. "//" + NEWLINE +
  90. "// Double-click on UML elements to add them to the diagram, or to copy them" + NEWLINE +
  91. "// Edit elements by modifying the text in this panel" + NEWLINE +
  92. "// Hold " + CTRLNAME + " to select multiple elements" + NEWLINE +
  93. "// Use " + CTRLNAME + "+mouse to select via lasso" + NEWLINE +
  94. "//" + NEWLINE +
  95. "// Use ± or " + CTRLNAME + "+mouse wheel to zoom" + NEWLINE +
  96. "// Drag a whole relation at its central square icon" + NEWLINE +
  97. "//" + NEWLINE +
  98. "// Press " + CTRLNAME + "+C to copy the whole diagram to the system clipboard (then just paste it to, eg, Word)" + NEWLINE +
  99. "// Edit the files in the \"palettes\" directory to create your own element palettes" + NEWLINE +
  100. "//" + NEWLINE +
  101. "// Select \"Custom Elements > New...\" to create new element types" + NEWLINE +
  102. "//////////////////////////////////////////////////////////////////////////////////////////////" + NEWLINE +
  103. "" + NEWLINE +
  104. "" + NEWLINE +
  105. "// This text will be stored with each diagram; use it for notes.";
  106. DEFAULT_MAILTEXT =
  107. "Type your message here.." + NEWLINE +
  108. "" + NEWLINE +
  109. "__" + NEWLINE +
  110. "To edit the diagram, open the attached uxf-file with the free UML tool UMLet (http://www.umlet.com)";
  111. }
  112. /**** TEXT FORMATTING LABELS ****/
  113. public static class FormatLabels {
  114. public static final String UNDERLINE = "_";
  115. public static final String BOLD = "*";
  116. public static final String ITALIC = "/";
  117. }
  118. /**** OTHER CONSTANTS ****/
  119. public static final int DEFAULTGRIDSIZE = 10;
  120. public static final int PRINTPADDING = 20;
  121. public static final int INTERFACE_LINE_LENGTH = 40;
  122. public static final int CUSTOM_ELEMENT_COMPILE_INTERVAL = 500;
  123. public static final int LEFT = 0, RIGHT = 2, CENTER = 1, TOP = 0, BOTTOM = 2;
  124. public static final int RESIZE_TOP = 1, RESIZE_RIGHT = 2, RESIZE_BOTTOM = 4, RESIZE_LEFT = 8;
  125. public static final String NOAUTORESIZE = "autoresize=false";
  126. public static final String AUTORESIZE = "autoresize=";
  127. public static final Cursor LR_CURSOR = new Cursor(Cursor.E_RESIZE_CURSOR);
  128. public static final Cursor TB_CURSOR = new Cursor(Cursor.N_RESIZE_CURSOR);
  129. public static final Cursor NW_CURSOR = new Cursor(Cursor.NW_RESIZE_CURSOR);
  130. public static final Cursor NE_CURSOR = new Cursor(Cursor.NE_RESIZE_CURSOR);
  131. public static final Cursor HAND_CURSOR = new Cursor(Cursor.HAND_CURSOR);
  132. public static final Cursor MOVE_CURSOR = new Cursor(Cursor.MOVE_CURSOR);
  133. public static final Cursor DEFAULT_CURSOR = new Cursor(Cursor.DEFAULT_CURSOR);
  134. public static final Cursor CROSS_CURSOR = new Cursor(Cursor.CROSSHAIR_CURSOR);
  135. public static final Cursor TEXT_CURSOR = new Cursor(Cursor.TEXT_CURSOR);
  136. public static final String DELIMITER_ENTITIES = "~~~~~|||||~~~~~|||||";
  137. public static final String DELIMITER_STATE_AND_HIDDEN_STATE = "/////<<<<</////<<<<<";
  138. public static final String DELIMITER_FIELDS = "#####_____#####_____";
  139. public static final String DELIMITER_ADDITIONAL_ATTRIBUTES = ";";
  140. public static final int PASTE_DISPLACEMENT = 20;
  141. public static final String FONT = "SansSerif";
  142. public static final int MIN_MAIL_SPLIT_POSITION = 100;
  143. public static final Color GRID_COLOR = new Color(235, 235, 235);
  144. /**** CONFIG FILENAME ****/
  145. public static final String configFilename;
  146. static {
  147. if (UMLETTYPE == UmletType.STANDALONE) configFilename = "umlet.cfg";
  148. else if (UMLETTYPE == UmletType.ECLIPSE_PLUGIN) configFilename = "umletplugin.cfg";
  149. else configFilename = "";
  150. }
  151. /**** VALUES LOADED FROM CONFIG ****/
  152. public static int defaultFontsize = 14;
  153. public static boolean start_maximized = false;
  154. public static boolean show_stickingpolygon = true;
  155. public static boolean show_grid = false;
  156. public static int main_split_position = 600;
  157. public static int right_split_position = 400;
  158. public static int mail_split_position = 250;
  159. public static Dimension umlet_size = new Dimension(960, 750);
  160. public static Point umlet_location = new Point(5, 5);
  161. public static String ui_manager;
  162. static {
  163. // The default MacOS theme looks ugly, therefore we set metal
  164. if (OS == Os.MAC) ui_manager = "javax.swing.plaf.metal.MetalLookAndFeel";
  165. // The GTKLookAndFeel crashes the eclipse plugin therefore we set metal as default instead
  166. else if ((UMLETTYPE == UmletType.ECLIPSE_PLUGIN) && UIManager.getSystemLookAndFeelClassName().equals("com.sun.java.swing.plaf.gtk.GTKLookAndFeel")) {
  167. ui_manager = "javax.swing.plaf.metal.MetalLookAndFeel";
  168. }
  169. else ui_manager = UIManager.getSystemLookAndFeelClassName();
  170. }
  171. public static String mail_smtp = "";
  172. public static boolean mail_smtp_auth = false;
  173. public static String mail_smtp_user = "";
  174. public static boolean mail_smtp_pw_store = false;
  175. public static String mail_smtp_pw = "";
  176. public static String mail_from = "";
  177. public static String mail_to = "";
  178. public static String mail_cc = "";
  179. public static String mail_bcc = "";
  180. public static boolean mail_uxf = true;
  181. public static boolean mail_gif = true;
  182. public static boolean mail_pdf = false;
  183. /**** STATIC HELPER METHODS ****/
  184. // public static File createRandomFile(String extension) {
  185. // File randomFile = new File("tmp.diagram." + new Date().getTime() + "." + extension);
  186. // randomFile.deleteOnExit();
  187. // return randomFile;
  188. // }
  189. public static Point normalize(Point p, int pixels) {
  190. Point ret = new Point();
  191. double d = Math.sqrt(p.x * p.x + p.y * p.y);
  192. ret.x = (int) (p.x / d * pixels);
  193. ret.y = (int) (p.y / d * pixels);
  194. return ret;
  195. }
  196. public static Vector<String> decomposeStringsWithEmptyLines(String s, String delimiter) {
  197. return decomposeStringsWFilter(s, delimiter, true, false);
  198. }
  199. public static Vector<String> decomposeStrings(String s, String delimiter) {
  200. return decomposeStringsWFilter(s, delimiter, true, true);
  201. }
  202. public static Vector<String> decomposeStringsWithComments(String s, String delimiter) {
  203. return decomposeStringsWFilter(s, delimiter, false, true);
  204. }
  205. public static Vector<String> decomposeStrings(String s) {
  206. return decomposeStrings(s, NEWLINE);
  207. }
  208. private static Vector<String> decomposeStringsWFilter(String s, String delimiter, boolean filterComments, boolean filterNewLines) {
  209. s = s.replaceAll("\r\n", delimiter); // compatibility to windows \r\n
  210. Vector<String> ret = new Vector<String>();
  211. for (;;) {
  212. int index = s.indexOf(delimiter);
  213. if (index < 0) {
  214. if (filterComments) {
  215. s = filterComment(s);
  216. if (s.startsWith("bg=") || s.startsWith("fg=") ||
  217. s.startsWith(Constants.AUTORESIZE)) s = ""; // filter color-setting strings
  218. }
  219. if (!s.equals("") || !filterNewLines) {
  220. ret.add(s);
  221. }
  222. return ret;
  223. }
  224. String tmp = s.substring(0, index);
  225. if (filterComments) {
  226. tmp = filterComment(tmp);
  227. if (tmp.startsWith("bg=") || tmp.startsWith("fg=") ||
  228. s.startsWith(Constants.AUTORESIZE)) tmp = ""; // filter color-setting strings
  229. }
  230. if (!tmp.equals("") || !filterNewLines) ret.add(tmp);
  231. s = s.substring(index + delimiter.length(), s.length());
  232. }
  233. }
  234. private static String filterComment(String s) {
  235. int pos = s.indexOf("//");
  236. char c;
  237. while (pos >= 0) {
  238. if (pos == 0) return "";
  239. c = s.charAt(pos - 1);
  240. if (s.length() > pos + 2) {
  241. if ((s.charAt(pos + 2) != '/') && (c != '/') && (c != ':')) return s.substring(0, pos);
  242. }
  243. else if ((c != '/') && (c != ':')) return s.substring(0, pos);
  244. pos = s.indexOf("//", pos + 1);
  245. }
  246. return s;
  247. }
  248. public static Vector<String> decomposeStringsIncludingEmptyStrings(String s, String delimiter) {
  249. return decomposeStringsWFilter(s, delimiter, false, false);
  250. }
  251. public static String composeStrings(Vector<String> v, String delimiter) {
  252. String ret = null;
  253. if (v != null) {
  254. for (int i = 0; i < v.size(); i++) {
  255. if (ret == null) {
  256. ret = new String(v.elementAt(i));
  257. }
  258. else {
  259. ret = ret + delimiter + v.elementAt(i);
  260. }
  261. }
  262. }
  263. if (ret == null) ret = "";
  264. return ret;
  265. }
  266. public static BasicStroke getStroke(int strokeType, int lineThickness) {
  267. BasicStroke stroke = null;
  268. switch (strokeType) {
  269. case 0:
  270. stroke = new BasicStroke(lineThickness);
  271. break;
  272. case 1:
  273. float dash1[] = { 8.0f, 5.0f };
  274. stroke = new BasicStroke(lineThickness, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 5.0f, dash1, 0.0f);
  275. break;
  276. case 2:
  277. float dash2[] = { 1.0f, 2.0f };
  278. stroke = new BasicStroke(lineThickness, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 5.0f, dash2, 0.0f);
  279. ;
  280. case 3:
  281. break;
  282. }
  283. return stroke;
  284. }
  285. public static Map<RenderingHints.Key, Object> getUxRenderingQualityHigh() {
  286. HashMap<RenderingHints.Key, Object> renderingHints = new HashMap<RenderingHints.Key, Object>();
  287. renderingHints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  288. renderingHints.put(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
  289. renderingHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
  290. renderingHints.put(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
  291. renderingHints.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
  292. renderingHints.put(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
  293. renderingHints.put(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
  294. renderingHints.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
  295. return renderingHints;
  296. }
  297. // public static Map<RenderingHints.Key, Object> getUxRenderingQualityLow() {
  298. // HashMap<RenderingHints.Key, Object> renderingHints = new HashMap<RenderingHints.Key, Object>();
  299. // return renderingHints;
  300. // }
  301. /**
  302. * Calculates and returns the angle of the line defined by the coordinates
  303. */
  304. public static double getAngle(double x1, double y1, double x2, double y2) {
  305. double res;
  306. double x = x2 - x1;
  307. double y = y2 - y1;
  308. res = Math.atan(y / x);
  309. if ((x >= 0.0) && (y >= 0.0)) res += 0.0;
  310. else if ((x < 0.0) && (y >= 0.0)) res += Math.PI;
  311. else if ((x < 0.0) && (y < 0.0)) res += Math.PI;
  312. else if ((x >= 0.0) && (y < 0.0)) res += 2.0 * Math.PI;
  313. return res;
  314. }
  315. }