001: /*
002: * AboutDialog.java
003: *
004: * Copyright (C) 1998-2004 Peter Graves
005: * $Id: AboutDialog.java,v 1.4 2004/09/24 15:15:25 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import java.io.BufferedReader;
025: import java.io.IOException;
026: import java.io.InputStreamReader;
027: import java.text.SimpleDateFormat;
028: import java.util.Date;
029: import javax.swing.Box;
030: import javax.swing.JPanel;
031:
032: public class AboutDialog extends AbstractDialog {
033: private long totalMemory;
034: private long freeMemory;
035:
036: public AboutDialog() {
037: super (Editor.getCurrentFrame(), "About J", true);
038: Editor.getCurrentFrame().setWaitCursor();
039: memory();
040: JPanel panel = Utilities.createPanel("J");
041: mainPanel.add(panel);
042: String longVersionString = Version.getLongVersionString();
043: if (longVersionString != null)
044: addStaticText(panel, longVersionString);
045: String snapshotInformation = Version.getSnapshotInformation();
046: if (snapshotInformation != null)
047: addStaticText(panel, snapshotInformation);
048: addStaticText(panel,
049: "Copyright (C) 1998-2004 Peter Graves (peter@armedbear.org)");
050: addStaticText(panel,
051: "J is free software; see the source for copying conditions.");
052: addStaticText(panel, "There is ABSOLUTELY NO WARRANTY.");
053: addStaticText(panel,
054: "The latest version of j is available from:");
055: addStaticText(panel, " http://armedbear.org");
056: addStaticText(panel, "Please report bugs to:");
057: addStaticText(panel,
058: " armedbear-j-devel@lists.sourceforge.net");
059: addStaticText(panel, getUptimeString());
060: panel = Utilities.createPanel("System Information");
061: addVerticalStrut();
062: mainPanel.add(panel);
063: FastStringBuffer sb = new FastStringBuffer("Java ");
064: sb.append(System.getProperty("java.version"));
065: sb.append(' ');
066: sb.append(System.getProperty("java.vendor"));
067: addStaticText(panel, sb.toString());
068: // Additional information if available.
069: String fullversion = System.getProperty("java.fullversion");
070: if (fullversion != null)
071: addStaticText(panel, fullversion);
072: String vm = System.getProperty("java.vm.name");
073: if (vm != null)
074: addStaticText(panel, vm);
075: Log.debug("total memory " + totalMemory);
076: Log.debug("used " + (totalMemory - freeMemory));
077: Log.debug("free " + freeMemory);
078: sb.setLength(0);
079: sb.append(formatMemory(totalMemory));
080: sb.append(" total Java memory (");
081: sb.append(formatMemory(totalMemory - freeMemory));
082: sb.append(" used, ");
083: sb.append(formatMemory(freeMemory));
084: sb.append(" free)");
085: addStaticText(panel, sb.toString());
086: sb.setLength(0);
087: sb.append(System.getProperty("os.name"));
088: sb.append(' ');
089: sb.append(System.getProperty("os.version"));
090: addStaticText(panel, sb.toString());
091: addVerticalStrut();
092: addOK();
093: pack();
094: okButton.requestFocus();
095: Editor.getCurrentFrame().setDefaultCursor();
096: }
097:
098: private static String getUptimeString() {
099: final int millisecondsPerMinute = 60 * 1000;
100: final int millisecondsPerHour = 60 * millisecondsPerMinute;
101: final int millisecondsPerDay = 24 * millisecondsPerHour;
102:
103: long now = System.currentTimeMillis();
104: SimpleDateFormat dateFormatter = new SimpleDateFormat(
105: "EEEE MMM d yyyy h:mm a");
106: String dateString = dateFormatter.format(new Date(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 panel, String s) {
144: panel.add(Box.createVerticalStrut(6));
145: panel.add(new StaticTextField(s));
146: }
147:
148: private void memory() {
149: Runtime runtime = Runtime.getRuntime();
150: try {
151: runtime.gc();
152: Thread.currentThread().sleep(100);
153: runtime.runFinalization();
154: Thread.currentThread().sleep(100);
155: runtime.gc();
156: Thread.currentThread().sleep(100);
157: } catch (InterruptedException e) {
158: Log.error(e);
159: }
160: totalMemory = runtime.totalMemory();
161: freeMemory = runtime.freeMemory();
162: }
163:
164: private String formatMemory(long value) {
165: if (value < 1000) {
166: return String.valueOf(value) + " bytes";
167: } else if (value < 1000 * 1024) {
168: double k = Math.round(value * 10 / (float) 1024) / 10.0;
169: return String.valueOf(k) + "K";
170: } else if (value < 1000 * 1024 * 1024) {
171: double m = Math.round(value * 10 / (float) (1024 * 1024)) / 10.0;
172: return String.valueOf(m) + "M";
173: } else {
174: double g = Math.round(value * 10
175: / (float) (1024 * 1024 * 1024)) / 10.0;
176: return String.valueOf(g) + "G";
177: }
178: }
179:
180: private int parseInteger(String s, String caption) throws Exception {
181: int index = s.indexOf(caption);
182: if (index < 0)
183: throw new Exception();
184: index += caption.length();
185: while (Character.isWhitespace(s.charAt(index)))
186: ++index;
187: return Utilities.parseInt(s.substring(index));
188: }
189:
190: public static void about() {
191: AboutDialog d = new AboutDialog();
192: Editor.currentEditor().centerDialog(d);
193: d.show();
194: }
195: }
|