KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > AboutDialog


1 /*
2  * AboutDialog.java
3  *
4  * Copyright (C) 1998-2004 Peter Graves
5  * $Id: AboutDialog.java,v 1.4 2004/09/24 15:15:25 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import java.io.BufferedReader JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStreamReader JavaDoc;
27 import java.text.SimpleDateFormat JavaDoc;
28 import java.util.Date JavaDoc;
29 import javax.swing.Box JavaDoc;
30 import javax.swing.JPanel JavaDoc;
31
32 public class AboutDialog extends AbstractDialog
33 {
34     private long totalMemory;
35     private long freeMemory;
36
37     public AboutDialog()
38     {
39         super(Editor.getCurrentFrame(), "About J", true);
40         Editor.getCurrentFrame().setWaitCursor();
41         memory();
42         JPanel JavaDoc panel = Utilities.createPanel("J");
43         mainPanel.add(panel);
44         String JavaDoc longVersionString = Version.getLongVersionString();
45         if (longVersionString != null)
46             addStaticText(panel, longVersionString);
47         String JavaDoc snapshotInformation = Version.getSnapshotInformation();
48         if (snapshotInformation != null)
49             addStaticText(panel, snapshotInformation);
50         addStaticText(panel,
51             "Copyright (C) 1998-2004 Peter Graves (peter@armedbear.org)");
52         addStaticText(panel,
53             "J is free software; see the source for copying conditions.");
54         addStaticText(panel, "There is ABSOLUTELY NO WARRANTY.");
55         addStaticText(panel, "The latest version of j is available from:");
56         addStaticText(panel, " http://armedbear.org");
57         addStaticText(panel, "Please report bugs to:");
58         addStaticText(panel, " armedbear-j-devel@lists.sourceforge.net");
59         addStaticText(panel, getUptimeString());
60         panel = Utilities.createPanel("System Information");
61         addVerticalStrut();
62         mainPanel.add(panel);
63         FastStringBuffer sb = new FastStringBuffer("Java ");
64         sb.append(System.getProperty("java.version"));
65         sb.append(' ');
66         sb.append(System.getProperty("java.vendor"));
67         addStaticText(panel, sb.toString());
68         // Additional information if available.
69
String JavaDoc fullversion = System.getProperty("java.fullversion");
70         if (fullversion != null)
71             addStaticText(panel, fullversion);
72         String JavaDoc vm = System.getProperty("java.vm.name");
73         if (vm != null)
74             addStaticText(panel, vm);
75         Log.debug("total memory " + totalMemory);
76         Log.debug("used " + (totalMemory - freeMemory));
77         Log.debug("free " + freeMemory);
78         sb.setLength(0);
79         sb.append(formatMemory(totalMemory));
80         sb.append(" total Java memory (");
81         sb.append(formatMemory(totalMemory - freeMemory));
82         sb.append(" used, ");
83         sb.append(formatMemory(freeMemory));
84         sb.append(" free)");
85         addStaticText(panel, sb.toString());
86         sb.setLength(0);
87         sb.append(System.getProperty("os.name"));
88         sb.append(' ');
89         sb.append(System.getProperty("os.version"));
90         addStaticText(panel, sb.toString());
91         addVerticalStrut();
92         addOK();
93         pack();
94         okButton.requestFocus();
95         Editor.getCurrentFrame().setDefaultCursor();
96     }
97
98     private static String JavaDoc getUptimeString()
99     {
100         final int millisecondsPerMinute = 60 * 1000;
101         final int millisecondsPerHour = 60 * millisecondsPerMinute;
102         final int millisecondsPerDay = 24 * millisecondsPerHour;
103
104         long now = System.currentTimeMillis();
105         SimpleDateFormat JavaDoc dateFormatter = new SimpleDateFormat JavaDoc ("EEEE MMM d yyyy h:mm a");
106         String JavaDoc dateString = dateFormatter.format(new Date JavaDoc(now));
107         long uptime = now - Editor.getStartTimeMillis();
108
109         // Don't show uptime if less than 1 minute.
110
if (uptime < millisecondsPerMinute)
111             return dateString;
112
113         int days = (int) (uptime / millisecondsPerDay);
114         int remainder = (int) (uptime % millisecondsPerDay);
115         int hours = remainder / millisecondsPerHour;
116         remainder = remainder % millisecondsPerHour;
117         int minutes = remainder / millisecondsPerMinute;
118
119         FastStringBuffer sb = new FastStringBuffer(dateString);
120         sb.append(" up ");
121         if (uptime < millisecondsPerHour) {
122             sb.append(minutes);
123             sb.append(" minute");
124             if (minutes > 1)
125                 sb.append('s');
126         } else {
127             if (days > 0) {
128                 sb.append(days);
129                 sb.append(" day");
130                 if (days > 1)
131                     sb.append('s');
132                 sb.append(", ");
133             }
134             sb.append(hours);
135             sb.append(':');
136             if (minutes < 10)
137                 sb.append('0');
138             sb.append(minutes);
139         }
140         return sb.toString();
141     }
142
143     private void addStaticText(JPanel JavaDoc panel, String JavaDoc s)
144     {
145         panel.add(Box.createVerticalStrut(6));
146         panel.add(new StaticTextField(s));
147     }
148
149     private void memory()
150     {
151         Runtime JavaDoc runtime = Runtime.getRuntime();
152         try {
153             runtime.gc();
154             Thread.currentThread().sleep(100);
155             runtime.runFinalization();
156             Thread.currentThread().sleep(100);
157             runtime.gc();
158             Thread.currentThread().sleep(100);
159         }
160         catch (InterruptedException JavaDoc e) {
161             Log.error(e);
162         }
163         totalMemory = runtime.totalMemory();
164         freeMemory = runtime.freeMemory();
165     }
166
167     private String JavaDoc formatMemory(long value)
168     {
169         if (value < 1000) {
170             return String.valueOf(value) + " bytes";
171         } else if (value < 1000 * 1024) {
172             double k = Math.round(value * 10 / (float) 1024) / 10.0;
173             return String.valueOf(k) + "K";
174         } else if (value < 1000 * 1024 * 1024) {
175             double m = Math.round(value * 10 / (float) (1024 * 1024)) / 10.0;
176             return String.valueOf(m) + "M";
177         } else {
178             double g = Math.round(value * 10 / (float) (1024 * 1024 * 1024)) / 10.0;
179             return String.valueOf(g) + "G";
180         }
181     }
182
183     private int parseInteger(String JavaDoc s, String JavaDoc caption) throws Exception JavaDoc
184     {
185         int index = s.indexOf(caption);
186         if (index < 0)
187             throw new Exception JavaDoc();
188         index += caption.length();
189         while (Character.isWhitespace(s.charAt(index)))
190             ++index;
191         return Utilities.parseInt(s.substring(index));
192     }
193
194     public static void about()
195     {
196         AboutDialog d = new AboutDialog();
197         Editor.currentEditor().centerDialog(d);
198         d.show();
199     }
200 }
201
Popular Tags