KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > ui > AboutDialog


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32 END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.drjava.ui;
35
36 import edu.rice.cs.drjava.platform.PlatformFactory;
37 import edu.rice.cs.util.swing.*;
38 import edu.rice.cs.util.UnexpectedException;
39 import edu.rice.cs.util.StringOps;
40 import edu.rice.cs.drjava.DrJava;
41 import edu.rice.cs.drjava.Version;
42 import javax.swing.*;
43 import javax.swing.border.*;
44 import javax.swing.text.*;
45 import javax.swing.event.ChangeListener JavaDoc;
46 import javax.swing.event.ChangeEvent JavaDoc;
47 import java.awt.datatransfer.Clipboard JavaDoc;
48 import java.awt.datatransfer.StringSelection JavaDoc;
49 import java.awt.event.*;
50 import java.awt.*;
51
52 import java.io.*;
53 import java.net.URL JavaDoc;
54 import java.util.Map JavaDoc;
55 /** About dialog.
56   * @version $Id: AboutDialog.java 4096 2007-01-30 02:37:18Z rcartwright $
57   */

58 public class AboutDialog extends JDialog implements ActionListener {
59
60   private static ImageInfo CSLOGO = new ImageInfo("RiceCS.gif",new Color(0x423585)),
61     SF = new ImageInfo("SourceForge.gif",Color.black),
62     DRJAVA = new ImageInfo("DrJava.png",new Color(0xCCCCFF));
63
64   private final JButton _okButton = new JButton("OK");
65
66   /** the button that copies the system properties to the clipboard */
67   private JButton _copyButton;
68   
69   /** the table with the System Properties information */
70   private JTable _propertiesTable;
71
72   /** index the System Properties tab, one of the tabs in _tabs */
73   private int _propertiesTabIndex;
74   
75   /** the pane with tabs to select */
76   private final JTabbedPane _tabs = new JTabbedPane();
77   
78   public AboutDialog(JFrame owner) {
79     super(owner, "About DrJava", true); // (changed to non-modal for now)
80

81     buildGUI(getContentPane());
82     getRootPane().setDefaultButton(_okButton);
83     // pack();
84
// setSize((int) (.8f*owner.getWidth()),(int) (.8f*owner.getHeight()));
85
setSize(550, 400);
86     // suggested from zaq@nosi.com, to keep the frame on the screen!
87
//System.out.println("Dialog created...");
88
}
89   
90   public void setVisible(boolean vis) {
91     _tabs.remove(0);
92     addTab(_tabs,"About",createCopyrightTab(), 0);
93     _tabs.setSelectedIndex(0);
94     
95     if (vis) {
96       // suggested from zaq@nosi.com, to keep the frame on the screen!
97
//Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
98
//Dimension frameSize = this.getSize();
99
MainFrame.setPopupLoc(this, getOwner());
100     }
101     super.setVisible(vis);
102   }
103
104   public void buildGUI(Container cp) {
105     cp.setLayout(new BorderLayout());
106     JLabel drjava = createImageLabel(DRJAVA,JLabel.LEFT);
107     if (drjava != null) {
108       drjava.setBorder(new CompoundBorder(new EmptyBorder(5,5,5,5), drjava.getBorder()));
109       drjava.setCursor(new Cursor(Cursor.HAND_CURSOR));
110       final String JavaDoc url = "http://drjava.org/";
111       drjava.setToolTipText(url);
112       drjava.addMouseListener(new MouseListener() {
113         public void mousePressed(MouseEvent e) { }
114         public void mouseReleased(MouseEvent e) { }
115         public void mouseEntered(MouseEvent e) { }
116         public void mouseExited(MouseEvent e) { }
117         public void mouseClicked(MouseEvent e) {
118           try {
119             PlatformFactory.ONLY.openURL(new URL JavaDoc(url));
120           } catch(Exception JavaDoc ex) { /* ignore, just not open web page */ }
121         }
122       });
123       
124       JPanel djPanel = new JPanel(new GridLayout(1,1));
125       djPanel.add(drjava);
126       djPanel.setBorder(new CompoundBorder(new EmptyBorder(5,5,5,5), new EtchedBorder()));
127       cp.add(djPanel,BorderLayout.NORTH);
128     }
129     addTab(_tabs,"About",createCopyrightTab());
130     LICENSE = getLicense();
131     if (LICENSE != null) addTab(_tabs,"DrJava License",createTextScroller(LICENSE));
132     
133     addTab(_tabs,"DynamicJava License",createTextScroller(DYADE_LICENSE));
134     addTab(_tabs,"System Properties",createSysPropTab());
135     _propertiesTabIndex = _tabs.getTabCount()-1;
136     cp.add(createBottomBar(),BorderLayout.SOUTH);
137     cp.add(_tabs,BorderLayout.CENTER);
138   }
139
140   private JComponent createSysPropTab() {
141     java.util.Properties JavaDoc props = System.getProperties();
142     int size = props.size();
143     String JavaDoc[][] rowData = new String JavaDoc[size][2];
144     java.util.Iterator JavaDoc entries = props.entrySet().iterator();
145     int rowNum = 0;
146     while (entries.hasNext()) {
147       Map.Entry JavaDoc entry = (Map.Entry JavaDoc) entries.next();
148       rowData[rowNum][0] = (String JavaDoc) entry.getKey();
149       rowData[rowNum][1] = (String JavaDoc) entry.getValue();
150       rowNum++;
151     }
152     java.util.Arrays.sort(rowData,new java.util.Comparator JavaDoc<String JavaDoc[]>() {
153       public int compare(String JavaDoc[] o1, String JavaDoc[] o2) {
154         return o1[0].compareTo(o2[0]);
155       }
156     });
157     String JavaDoc[] nvStrings = new String JavaDoc[]{"Name","Value"};
158     UneditableTableModel model = new UneditableTableModel(rowData, nvStrings);
159     _propertiesTable = new JTable(model);
160     JScrollPane scroller = new BorderlessScrollPane(_propertiesTable);
161     wrapBorder(scroller,new EmptyBorder(5,0,0,0));
162     JPanel propTab = new JPanel(new BorderLayout());
163     propTab.add(new JLabel("Current system properties:"),BorderLayout.NORTH);
164     propTab.add(scroller,BorderLayout.CENTER);
165     return propTab;
166   }
167
168   private static void addTab(JTabbedPane tabs, String JavaDoc title, JComponent tab) {
169     wrapBorder(tab,new EmptyBorder(5,6,6,5));
170     tabs.addTab(title,tab);
171   }
172
173   private static void addTab(JTabbedPane tabs, String JavaDoc title, JComponent tab, int i) {
174     wrapBorder(tab,new EmptyBorder(5,6,6,5));
175     tabs.insertTab(title, null, tab, "", i);
176   }
177   
178   public static JComponent createCopyrightTab() {
179     final JPanel panel = new JPanel(new BorderLayout());
180
181     final StringBuilder JavaDoc sb = new StringBuilder JavaDoc("DrJava Version : ");
182     sb.append(Version.getBuildTimeString());
183     sb.append("\n\nDrJava Configuration file: ");
184     sb.append(DrJava.getPropertiesFile().getAbsolutePath());
185     sb.append("\n\nUsed memory: about ");
186     sb.append(StringOps.memSizeToString(Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory()));
187     sb.append("\nFree memory: about ");
188     sb.append(StringOps.memSizeToString(Runtime.getRuntime().freeMemory()));
189     sb.append("\nTotal memory: about ");
190     sb.append(StringOps.memSizeToString(Runtime.getRuntime().totalMemory()));
191     sb.append("\nTotal memory can expand to: about ");
192     sb.append(StringOps.memSizeToString(Runtime.getRuntime().maxMemory()));
193     sb.append("\n\n");
194     sb.append(COPYRIGHT);
195     final JComponent copy = createTextScroller(sb.toString());
196     wrapBorder(copy,new EmptyBorder(0,0,5,0));
197
198     // deal with logos now (calibrate size)
199
final LogoList logos = new LogoList();
200     logos.addLogo(createBorderedLabel(CSLOGO,new EmptyBorder(5,5,5,5)), "http://compsci.rice.edu/");
201     logos.addLogo(createBorderedLabel(SF,null), "http://sourceforge.net/projects/drjava/");
202     logos.resizeLogos();
203
204     // add to panel
205
final JPanel logoPanel = new JPanel();
206     logoPanel.setLayout(new BoxLayout(logoPanel,BoxLayout.X_AXIS));
207     logoPanel.add(Box.createHorizontalGlue());
208     java.util.Iterator JavaDoc it = logos.iterator();
209     while(it.hasNext()) {
210       final JComponent l = (JComponent) it.next();
211       logoPanel.add(l);
212       l.setCursor(new Cursor(Cursor.HAND_CURSOR));
213       final String JavaDoc url = (String JavaDoc)l.getClientProperty("url");
214       if (url != null) {
215         l.setToolTipText(url);
216         l.addMouseListener(new MouseListener() {
217           public void mousePressed(MouseEvent e) { }
218           public void mouseReleased(MouseEvent e) { }
219           public void mouseEntered(MouseEvent e) { }
220           public void mouseExited(MouseEvent e) { }
221           public void mouseClicked(MouseEvent e) {
222             try { PlatformFactory.ONLY.openURL(new URL JavaDoc(url)); }
223             catch(Exception JavaDoc ex) { /* ignore, just not open web page */ }
224           }
225         });
226       }
227       logoPanel.add(Box.createHorizontalGlue());
228     }
229     panel.add(logoPanel,BorderLayout.SOUTH);
230     panel.add(copy,BorderLayout.CENTER);
231     return panel;
232   }
233
234   private static class LogoList extends java.util.LinkedList JavaDoc<JPanel> implements Serializable {
235     private int width = Integer.MIN_VALUE;
236     private int height = Integer.MIN_VALUE;
237     private void addLogo(JPanel logo, String JavaDoc url) {
238       if (logo != null) {
239         Dimension d = logo.getMinimumSize();
240         width = Math.max(width,d.width);
241         height = Math.max(height,d.height);
242         add(logo);
243         if (url != null) logo.putClientProperty("url", url);
244       }
245     }
246
247     private void resizeLogos() {
248       java.util.Iterator JavaDoc it = iterator();
249       Dimension d = new Dimension(width,height);
250       while(it.hasNext()) {
251         JComponent i = (JComponent) it.next();
252         i.setMinimumSize(d);
253         i.setMaximumSize(d);
254         i.setPreferredSize(d);
255       }
256     }
257   }
258
259   public static JPanel createBorderedLabel(ImageInfo info, EmptyBorder pad) {
260     JLabel label = createImageLabel(info,JLabel.CENTER);
261     if (label == null) return null;
262     JPanel panel = new JPanel(new GridLayout(1,1));
263     panel.setOpaque(true);
264     panel.setBackground(info.color);
265     panel.setBorder(pad);
266     wrapBorder(panel,new EtchedBorder());
267     panel.add(label);
268     return panel;
269   }
270
271   public static JLabel createImageLabel(ImageInfo info, int align) {
272     ImageIcon icon = MainFrame.getIcon(info.name);
273     if (icon==null) return null;
274     JLabel label = new JLabel(icon,align);
275     label.setOpaque(true);
276     label.setBackground(info.color);
277     return label;
278   }
279
280   public static JTextArea createTextArea(String JavaDoc text) {
281     JTextArea textArea = new JTextArea(text);
282     textArea.setEditable(false);
283     textArea.setLineWrap(true);
284     textArea.setWrapStyleWord(true);
285     textArea.setCaretPosition(0);
286     return textArea;
287   }
288
289   public static JScrollPane createTextScroller(String JavaDoc text) {
290     return new BorderlessScrollPane(createTextArea(text));
291   }
292
293   private JPanel createBottomBar() {
294     JPanel panel = new JPanel(new BorderLayout());
295     JPanel buttonPanel = new JPanel();
296     _copyButton = new JButton(new AbstractAction("Copy System Properties") {
297       public void actionPerformed(ActionEvent e) {
298         Clipboard JavaDoc cb = Toolkit.getDefaultToolkit().getSystemClipboard();
299         StringSelection JavaDoc contents = new StringSelection JavaDoc(DrJavaErrorWindow.getSystemAndDrJavaInfo());
300         cb.setContents(contents, null);
301       }
302     });
303     _tabs.addChangeListener(new ChangeListener JavaDoc() {
304       // This method is called whenever the selected tab changes
305
public void stateChanged(ChangeEvent JavaDoc evt) {
306         _copyButton.setVisible(_tabs.getSelectedIndex()==_propertiesTabIndex);
307       }
308     });
309     _copyButton.setVisible(_tabs.getSelectedIndex()==_propertiesTabIndex);
310     _okButton.addActionListener(this);
311     buttonPanel.add(_copyButton);
312     buttonPanel.add(_okButton);
313     panel.add(buttonPanel,BorderLayout.EAST);
314     wrapBorder(panel,new EmptyBorder(5,5,5,5));
315     return panel;
316   }
317
318   public void actionPerformed(ActionEvent e) {
319     setVisible(false);
320   }
321
322   public static final String JavaDoc COPYRIGHT =
323     "Copyright \u00a9 2001-2006 JavaPLT group at Rice University\n"+
324     "(javaplt@rice.edu)\n\n"+
325     "See http://www.drjava.org for more information on DrJava or to\n"+
326     "obtain the latest version of the program or its source code.\n\n"+
327     "DrJava is free software; you can redistribute it and/or modify\n"+
328     "it under the terms of the DrJava Open Source License. A copy\n"+
329     "of the license should have been included in the documentation\n"+
330     "for this software.";
331   private static String JavaDoc LICENSE;
332   private static boolean initLicense = false;
333   public static final String JavaDoc DYADE_LICENSE =
334     "DynamicJava - Copyright \u00a9 1999 Dyade\n\nPermission is hereby granted,"+
335     " free of charge, to any person obtaining a copy of this software and associated"+
336     " documentation files (the \"Software\"), to deal in the Software without restriction,"+
337     " including without limitation the rights to use, copy, modify, merge, publish, distribute,"+
338     " sublicense, and/or sell copies of the Software, and to permit persons to whom the Software"+
339     " is furnished to do so, subject to the following conditions:\n\n"+
340     "The above copyright notice and this permission notice shall be included in all copies or"+
341     " substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY"+
342     " OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,"+
343     " FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL DYADE BE LIABLE FOR ANY CLAIM,"+
344     " DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT"+
345     " OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n\n"+
346     "Except as contained in this notice, the name of Dyade shall not be used in advertising or otherwise"+
347     " to promote the sale, use or other dealings in this Software without prior written authorization from Dyade.";
348   public static final String JavaDoc INTRODUCTION =
349     "DrJava is a pedagogic programming environment for Java, intended to help students focus more on program"+
350     " design than on the features of a complicated development environment. It provides an Interactions"+
351     " window based on a \"read-eval-print loop\", which allows programmers to develop, test, and debug"+
352     " Java programs in an interactive, incremental fashion.\n\n"+
353     "Home Page: http://www.drjava.org\nPaper: http://drjava.sf.net/papers/drjava-paper.shtml";
354
355   public static class ImageInfo {
356     private final String JavaDoc name;
357     private final Color color;
358     public ImageInfo(String JavaDoc name, Color color) {
359       this.name = name;
360       this.color = color;
361     }
362   }
363
364   public static String JavaDoc getLicense() {
365     if (initLicense) return LICENSE;
366
367     try {
368       InputStream is = AboutDialog.class.getResourceAsStream("/edu/rice/cs/LICENSE");
369       if (is != null) {
370         BufferedReader r = new BufferedReader(new InputStreamReader(is));
371         try {
372           
373           final StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
374           for (String JavaDoc s = r.readLine(); s != null; s = r.readLine()) {
375             int lastSig = s.length()-1; // the last char index
376

377             while (lastSig >= 0 && Character.isWhitespace(s.charAt(lastSig))) lastSig--;
378             if (lastSig < 0) sb.append("\n"); // empty line, so insert two newlines.
379
else {
380               sb.append(s.substring(0,lastSig+1));
381               sb.append('\n');
382             }
383           }
384           LICENSE = sb.toString();
385           LICENSE = LICENSE.trim();
386           if (LICENSE.length() == 0) LICENSE = null;
387         }
388         finally {
389           is.close();
390           r.close();
391         }
392       }
393     }
394     catch(Exception JavaDoc e) { throw new UnexpectedException(e, StringOps.getStackTrace(e)); }
395
396     initLicense = true;
397     return LICENSE;
398   }
399
400   private static void wrapBorder(JComponent c, Border b) {
401     c.setBorder(new CompoundBorder(b,c.getBorder()));
402   }
403 }
404
Popular Tags