View Javadoc

1   package com.explosion.utilities.dialog;
2   
3   /*
4    * =============================================================================
5    * 
6    * Copyright 2004 Stephen Cowx
7    * 
8    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
9    * use this file except in compliance with the License. You may obtain a copy of
10   * the License at
11   * 
12   * http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   * 
20   * =============================================================================
21   */
22  
23  import java.awt.BorderLayout;
24  import java.awt.Canvas;
25  import java.awt.CardLayout;
26  import java.awt.Color;
27  import java.awt.Dimension;
28  import java.awt.Font;
29  import java.awt.Frame;
30  import java.awt.Graphics;
31  import java.awt.Image;
32  import java.awt.Toolkit;
33  import java.awt.event.ActionEvent;
34  import java.util.Collections;
35  import java.util.Enumeration;
36  import java.util.Iterator;
37  import java.util.Map;
38  import java.util.Vector;
39  
40  import javax.swing.JButton;
41  import javax.swing.JDialog;
42  import javax.swing.JPanel;
43  import javax.swing.JScrollPane;
44  import javax.swing.JTabbedPane;
45  import javax.swing.JTable;
46  import javax.swing.JToggleButton;
47  import javax.swing.table.DefaultTableModel;
48  import javax.swing.table.TableModel;
49  
50  import com.explosion.utilities.FileSystemUtils;
51  import com.explosion.utilities.GeneralConstants;
52  
53  /***
54   * @author Stephen Cowx Date created:@05-Apr-2003
55   */
56  
57  public class AboutDialog extends JDialog
58  {
59  
60      private DialogCanvas canvas;
61  
62      private CardLayout cardLayout;
63      
64      private JTabbedPane propertiesPane = new JTabbedPane();
65  
66      private JPanel controlPanel = new JPanel();
67  
68      private JPanel cardPanel = new JPanel();
69  
70      private JButton doneButton = new JButton("Ok");
71  
72      private JToggleButton propsButton = new JToggleButton("Show system properties");
73      
74      private Map propertiesMap;
75      
76      private Map systemPropertyMaps;
77      
78      /***
79       * Creates an about fialog displaying the application logo and the system properties by default
80       * You have the option of adding tabs to the system properties area by supplying a Map containing a set of values where the Key is the 
81       * name for the property set and the Value is a TableModel containing the property information ready for display
82       * @param aboutImage
83       * @param applicationName
84       * @param applicationVersion
85       * @param vendor
86       * @param author
87       * @param year
88       * @param vendorUrl
89       * @param systemPropertyMap can be null
90       * @param textColor
91       * @param owner
92       */
93      public AboutDialog(String aboutImage, String applicationName, String applicationVersion, String vendor, String author, String year, String vendorUrl, Color textColor,  Map propertiesMap ,Frame owner)
94      {
95          super(owner, "About " + applicationName, true);
96          try
97          {
98              canvas = new DialogCanvas(aboutImage, applicationName, applicationVersion, vendor, author, year, vendorUrl, textColor);
99              this.propertiesMap = propertiesMap;
100             init();
101         } catch (Exception e)
102         {
103             com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(e, null);
104         }
105     }
106 
107     private void init() throws Exception
108     {
109         controlPanel.add(doneButton);
110         controlPanel.add(propsButton);
111         doneButton.addActionListener(new java.awt.event.ActionListener()
112         {
113 
114             public void actionPerformed(ActionEvent e)
115             {
116                 doneButton_actionPerformed(e);
117             }
118         });
119         propsButton.addActionListener(new java.awt.event.ActionListener()
120         {
121 
122             public void actionPerformed(ActionEvent e)
123             {
124                 propsButton_actionPerformed(e);
125             }
126         });
127 
128         JScrollPane system = new JScrollPane();
129         system.getViewport().add(new JTable(getSystemProperties()));
130         propertiesPane.add("Environment", system);
131         
132         if (propertiesMap != null)
133         {
134           Iterator it = propertiesMap.keySet().iterator();
135           while (it.hasNext())
136           {
137               Object key = it.next();
138               JScrollPane propertiesScrollPane = new JScrollPane();
139               propertiesScrollPane.getViewport().add(new JTable((TableModel)propertiesMap.get(key)));
140               propertiesPane.add(key.toString(),propertiesScrollPane);
141           }
142         }
143         
144         this.getContentPane().add(canvas, BorderLayout.CENTER);
145         this.getContentPane().add(controlPanel, BorderLayout.SOUTH);
146 
147         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
148 
149         this.pack();
150         this.setResizable(false);
151         this.setLocation((int) screenSize.getWidth() / 2 - getWidth() / 2, (int) screenSize.getHeight() / 2 - getHeight() / 2);
152     }
153     
154     void doneButton_actionPerformed(ActionEvent e)
155     {
156         this.setVisible(false);
157         this.dispose();
158     }
159 
160     void propsButton_actionPerformed(ActionEvent e)
161     {
162         if (!propsButton.isSelected())
163         {
164             this.getContentPane().removeAll();
165             this.getContentPane().add(canvas, BorderLayout.CENTER);
166             this.getContentPane().add(controlPanel, BorderLayout.SOUTH);
167             propsButton.setText("Show system properties");
168         } else
169         {
170             this.getContentPane().removeAll();
171             this.getContentPane().add(propertiesPane, BorderLayout.CENTER);
172             this.getContentPane().add(controlPanel, BorderLayout.SOUTH);
173             propsButton.setText("Hide system properties");
174         }
175     }
176 
177     DefaultTableModel getSystemProperties() throws Exception
178     {
179         Vector keysVector = new Vector();
180         Enumeration keys = System.getProperties().keys();
181         while (keys.hasMoreElements())
182             keysVector.addElement(keys.nextElement());
183 
184         Collections.sort(keysVector);
185 
186         Vector rows = new Vector();
187         for (int i = 0; i < keysVector.size(); i++)
188         {
189             Vector columns = new Vector();
190             columns.addElement(keysVector.elementAt(i));
191             columns.addElement(System.getProperty((String) keysVector.elementAt(i)));
192             rows.addElement(columns);
193         }
194 
195         Vector columnNames = new Vector();
196         columnNames.addElement("Property");
197         columnNames.addElement("Value");
198 
199         return new DefaultTableModel(rows, columnNames);
200     }
201 
202 }
203 
204 class DialogCanvas extends Canvas
205 {
206 
207     private Image image;
208 
209     private Font font = GeneralConstants.DEFAULT_WINDOW_FONT;
210 
211     private Color textColor = Color.white;
212 
213     private String applicationName;
214 
215     private String applicationVersion;
216 
217     private String vendor;
218 
219     private String author;
220 
221     private String year;
222 
223     private String vendorUrl;
224 
225     public DialogCanvas(String imageName, String applicationName, String applicationVersion, String vendor, String author, String year, String vendorUrl, Color textColor)
226     {
227         try
228         {
229             image = FileSystemUtils.loadImage(imageName, this);
230             this.setSize(new Dimension(image.getWidth(null), image.getHeight(null)));
231 
232             this.applicationName = applicationName;
233             this.applicationVersion = applicationVersion;
234             this.vendor = vendor;
235             this.author = author;
236             this.year = year;
237             this.vendorUrl = vendorUrl;
238 
239             this.textColor = textColor;
240             this.setFont(font);
241             this.setBackground(Color.WHITE);
242         } catch (Exception e)
243         {
244             com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(e, null);
245         }
246     }
247 
248     public void update(Graphics g)
249     {
250         paint(g);
251     }
252 
253     public void paint(Graphics g)
254     {
255         g.drawImage(image, 0, 0, image.getWidth(null), image.getHeight(null), null);
256         g.setColor(textColor);
257         int xstart = 145;
258         int ystart = 30;
259         g.setFont(new Font(font.getName(), Font.BOLD, font.getSize()));
260         g.drawString(applicationName, xstart, ystart);
261         g.setFont(font);
262         g.drawString("Version: " + applicationVersion, xstart, ystart + 20);
263         g.drawString("Copyright: " + vendor + ", " + author + ", " + year, xstart, ystart + 40);
264         g.drawString("Visit: " + vendorUrl, xstart, ystart + 60);
265     }
266 
267 }