KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > tracelog > utils > Util


1 package net.sourceforge.tracelog.utils;
2
3 import java.io.BufferedReader JavaDoc;
4 import java.io.InputStream JavaDoc;
5 import java.io.InputStreamReader JavaDoc;
6
7 import org.apache.log4j.Logger;
8 import org.eclipse.swt.SWT;
9 import org.eclipse.swt.graphics.Color;
10 import org.eclipse.swt.graphics.Rectangle;
11 import org.eclipse.swt.widgets.Display;
12 import org.eclipse.swt.widgets.Shell;
13
14 public class Util {
15     private static Logger log = Logger.getLogger(Util.class);
16     public static final String JavaDoc FILE_PROJECT_PROPERTIES = "/net/sourceforge/tracelog/resources/resource.properties";
17
18     private static final Display DISPLAY = new Display();
19     public static final Color COLOR_WHITE = DISPLAY.getSystemColor(SWT.COLOR_WHITE);
20     public static final Color COLOR_GRAY = DISPLAY.getSystemColor(SWT.COLOR_GRAY);
21
22     private static String JavaDoc appVersion;
23
24     private static long currentTimeMillis = System.currentTimeMillis();
25
26     /**
27      * Generates and returns an unique id.
28      *
29      * @return Unique ID.
30      */

31     public synchronized static String JavaDoc getUniqueId() {
32         return "" + currentTimeMillis++;
33     }
34
35     public static boolean isEmpty(String JavaDoc value) {
36         return value == null || value.trim().equals("");
37     }
38
39     /**
40      * Returns the latest version.
41      *
42      * @return Latest version number.
43      */

44     public static String JavaDoc getLatestVersion() {
45         if (appVersion == null) {
46             String JavaDoc line = "";
47             String JavaDoc version = "";
48             boolean loop = true;
49
50             ProjectProperties projectProperties = ProjectProperties.getInstance();
51
52             try {
53                 BufferedReader JavaDoc rd = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(getOwnResource(projectProperties.getVersionFilePath())));
54
55                 while (loop && (line = rd.readLine()) != null) {
56                     if (!line.trim().equals("")) {
57                         version = line.substring(line.indexOf("-") + 1, line.indexOf("\t")).trim();
58                         loop = false;
59                     }
60                 }
61
62                 rd.close();
63             }
64             catch (Exception JavaDoc e) {
65                 log.error(e.getMessage());
66             }
67
68             appVersion = "v" + version;
69         }
70
71         return appVersion;
72     }
73
74     /**
75      * Returns the display.
76      *
77      * @return Display.
78      */

79     public static Display getDisplay() {
80         return DISPLAY;
81     }
82
83     /**
84      * Get project resource file.
85      *
86      * @param obj
87      * @param fileName
88      * @return
89      */

90     public static InputStream JavaDoc getOwnResource(String JavaDoc fileName) {
91         return Util.class.getResourceAsStream(fileName);
92     }
93
94     /**
95      * Center aligned shell in relative to parent shell.
96      *
97      * @param parentShell
98      * Parent shell
99      * @param shell
100      * Shell to be aligned
101      */

102     public static void centerAlignedShell(Shell parentShell, Shell shell) {
103         Rectangle shellRect = shell.getBounds();
104         Rectangle parentShellRect = parentShell.getBounds();
105
106         int x = (parentShellRect.width - shellRect.width) / 2;
107         int y = (parentShellRect.height - shellRect.height) / 2;
108
109         shell.setLocation(parentShellRect.x + x, parentShellRect.y + y);
110     }
111 }
112
Popular Tags