Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/javathis/utilities/ui/JTOrnateAboutBox.java


1   /**
2    * JTUtilities - The Pure Java Utilities
3    * Copyright(c) 2002 by Rodney S. Foley
4    * <pre>
5    * This library is free software; you can redistribute it and/or modify it under
6    * the terms of the GNU Lesser General Public License as published by the Free
7    * Software Foundation; either version 2.1 of the License, or (at your option)
8    * any later version.
9    *
10   * This library is distributed in the hope that it will be useful, but WITHOUT
11   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   * FOR A PARTICULAR PURPOSE.  See the GNU Library General Public License for
13   * more details.
14   *
15   * You should have received a copy of the GNU Library General Public License
16   * along with this library; if not, write to:
17   *
18   * Free Software Foundation, Inc.
19   * 59 Temple Place, Suite 330
20   * Boston, MA  02111-1307, USA
21   *
22   * or download it from:  http://www.fsf.org/licenses/licenses.html#LGPL
23   * </pre>
24   */
25  package com.javathis.utilities.ui;
26  
27  import com.javathis.utilities.*;
28  import java.awt.*;
29  import java.awt.event.*;
30  import java.io.*;
31  import java.net.*;
32  import java.util.*;
33  import javax.swing.*;
34  
35  /**
36   * Provides a Windows XP style About box that can be used consistenly across
37   * multiple applications.
38   * <p>
39   * It has Four areas. A banner at the top, a icon at the left, text information
40   * at the right, and the button bar at the bottom.
41   * <pre>
42   *      _________________________________________________________
43   *      |O|___________________________________________________|X|
44   *      |                                                       |
45   *      |                 B A N N E R  A R E A                  |
46   *      |_______________________________________________________|
47   *      |               |                                       |
48   *      |     +----+    |  N A M E                              |
49   *      |     |ICON|    |  V E R S I O N                        |
50   *      |     +----+    |  COPYRIGHT  (1)                       |
51   *      |               |             (2)                       |
52   *      |               |             (3)                       |
53   *      |               |  L I C E N S E  C O M M E N T         |
54   *      |               |  L I C E N S E  D E T A I L S ''''''' |
55   *      |               |  '                                  ' |
56   *      |               |  '                                  ' |
57   *      |               |  '                                  ' |
58   *      |_______________|__'__________________________________'_|
59   *      |                                               [ OK ]  |
60   *      |_______________________________________________________|
61   * </pre>
62   * <br>
63   * Requirements:
64   * <p>
65   * Banner cannot be larger than 414 x 76.<br>
66   * Icon must be 32 x 32.<br>
67   * A IllegalArgumentException will be thrown is the images are not in compliance.
68   * <p>
69   * Limitations:
70   * <p>
71   * Name, Version, and License Comment fields are single lines that can contain
72   * between 30 - 50 characters depending on the charcters used.<br>
73   * Copyright field is multilined.  It will word wrap if needed.  Recommended that
74   * the string include returns and or tabs where needed for formating.  It only
75   * can hold 3 lines, all other lines will not be shown.<br>
76   * License Details is a multiline, scroll when needed, field.
77   * <p>
78   * Note:  If information is not provided for a field/area it will be set to it's
79   * default value.  Passing a null value will reset the field/area to it's
80   * default value.  If you want an area to be displayed as empty, use an empty
81   * string for the field.  Exceptions to this are the Banner, and Icon areas.
82   * An image is required for these areas.  If you do not want to show an image
83   * in the banner or icon area please use the methods noBanner(boolean),
84   * and/or noIcon(boolean).  The corresponding area will be blank.
85   */
86  public class JTOrnateAboutBox extends JDialog
87  {
88      protected static final ResourceBundle MAIN_RESOURCE_BUNDLE = ResourceBundle.getBundle("com/javathis/utilities/properties/About");
89  
90      private JPanel bannerPanel = new JPanel();
91      private JPanel mainPanel = new JPanel();
92      private JPanel buttonPanel = new JPanel();
93      private JButton okButton = new JButton();
94      private JPanel iconPanel = new JPanel();
95      private BorderLayout borderLayout1 = new BorderLayout();
96      private JLabel bannerLabel = new JLabel();
97      private GridBagLayout gridBagLayout1 = new GridBagLayout();
98      private JLabel iconLabel = new JLabel();
99      private GridBagLayout gridBagLayout2 = new GridBagLayout();
100     private JLabel nameLabel = new JLabel();
101     private JLabel versionLabel = new JLabel();
102     private JTextPane copyrightTextPane = new JTextPane();
103     private JLabel licenseCommentLabel = new JLabel();
104     private JTextPane licenseTextPane = new JTextPane();
105     private JScrollPane licenseScrollPane = new JScrollPane(licenseTextPane, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
106 
107     private Font aboutFont = new Font( nameLabel.getFont().getName(),  nameLabel.getFont().getStyle(), 11);
108 
109     private static final String DEFAULT_TITLE           = MAIN_RESOURCE_BUNDLE.getString("title");
110     private static final String DEFAULT_NAME            = MAIN_RESOURCE_BUNDLE.getString("name");
111     private static final String DEFAULT_VERSION         = MAIN_RESOURCE_BUNDLE.getString("version");
112     private static final String DEFAULT_COPYRIGHT       = MAIN_RESOURCE_BUNDLE.getString("copyright");
113     private static final String DEFAULT_LICENSE_COMMENT = MAIN_RESOURCE_BUNDLE.getString("licenseComment");
114     private static final String DEFAULT_LICENSE_DETAILS = MAIN_RESOURCE_BUNDLE.getString("licenseDetails");
115 
116     private static final URL DEFAULT_BANNER_URL = ClassLoader.getSystemResource("com/javathis/utilities/images/JavaThisLogo-Med.gif");
117     private static final URL DEFAULT_ICON_URL   = ClassLoader.getSystemResource("com/javathis/utilities/images/JavaThisIcon32.gif");
118     private static final URL EMPTY_BANNER       = ClassLoader.getSystemResource("com/javathis/utilities/images/emptyBanner.gif");
119     private static final URL EMPTY_ICON         = ClassLoader.getSystemResource("com/javathis/utilities/images/emptyIcon32.gif");
120 
121     private String name             = null;
122     private String version          = null;
123     private String copyright        = null;
124     private String licenseComment   = null;
125     private String licenseDetails   = null;
126 
127     private File bannerFile = null;
128     private File iconFile   = null;
129 
130     private boolean noBanner    = false;
131     private boolean noIcon      = false;
132 
133     private static final Dimension BANNER_SIZE          = new Dimension(414, 76);
134     private static final Dimension ICON_SIZE            = new Dimension(32, 32);
135     private static final Dimension COPYRIGHT_SIZE       = new Dimension(100, 44);
136     private static final Dimension DEFAULT_DIALOG_SIZE  = new Dimension(420, 350);
137 
138     /**
139      * Creates a <code>JTOrnateAboutBox</code> with a JavaThis banner and icon.
140      * The text information will be filled with details related to JavaThis
141      * Utitlities library.  It will have no owner.  Is always model.
142      */
143     public JTOrnateAboutBox()
144     {
145         super();
146         super.setTitle(DEFAULT_TITLE);
147         super.setModal(true);
148 
149         jtInit(DEFAULT_NAME, DEFAULT_VERSION, DEFAULT_COPYRIGHT, DEFAULT_LICENSE_COMMENT, DEFAULT_LICENSE_DETAILS, null, null);
150 
151     }
152 
153     /**
154      * Creates a <code>JTOrnateAboutBox</code> with a JavaThis banner and icon.
155      * The text information will be filled with details related to JavaThis
156      * Utitlities library.  It will have the provided {@link JFrame} as its owner,
157      * the with the provided title. Is always model.
158      */
159     public JTOrnateAboutBox(JFrame owner, String title)
160     {
161         super(owner, title, true);
162 
163         jtInit(DEFAULT_NAME, DEFAULT_VERSION, DEFAULT_COPYRIGHT, DEFAULT_LICENSE_COMMENT, DEFAULT_LICENSE_DETAILS, null, null);
164 
165     }
166 
167     /**
168      * Creates a <code>JTOrnateAboutBox</code> with a JavaThis banner and icon.
169      * The text information will be filled with details related to JavaThis
170      * Utitlities library.  It will have the provided {@link JDialog} as its owner,
171      * the with the provided title. Is always model.
172      */
173     public JTOrnateAboutBox(JDialog owner, String title)
174     {
175         super(owner, title, true);
176 
177         jtInit(DEFAULT_NAME, DEFAULT_VERSION, DEFAULT_COPYRIGHT, DEFAULT_LICENSE_COMMENT, DEFAULT_LICENSE_DETAILS, null, null);
178 
179     }
180 
181     /**
182      * Creates a <code>JTOrnateAboutBox</code> with the provided banner and icon.
183      * The text information will be filled with the provided details, and it will
184      * have the provided {@link JFrame} as its owner, the with the provided
185      * title. Is always model.
186      */
187     public JTOrnateAboutBox(JFrame owner, String title, String name, String version, String copyright, String licenseComment, String licenseDetails, File banner, File icon)
188     {
189         super(owner, title, true);
190 
191         jtInit(name, version, copyright, licenseComment, licenseDetails, banner, icon);
192     }
193 
194     /**
195      * Creates a <code>JTOrnateAboutBox</code> with the provided banner and icon.
196      * The text information will be filled with the provided details, and it will
197      * have the provided {@link JDialog} as its owner, the with the provided
198      * title. Is always model.
199      */
200     public JTOrnateAboutBox(JDialog owner, String title, String name, String version, String copyright, String licenseComment, String licenseDetails, File banner, File icon)
201     {
202         super(owner, title, true);
203 
204         jtInit(name, version, copyright, licenseComment, licenseDetails, banner, icon);
205     }
206 
207     /**
208      * Overridded to prevent modifications
209      *
210      * @param isModal
211      */
212     public void setModal(boolean isModal) {}
213 
214     /**
215      * Overridded to prevent modifications
216      *
217      * @param menu
218      */
219     public void setJMenuBar(JMenuBar menu) {}
220 
221     /**
222      * Overridded to prevent modifications
223      *
224      * @param manager
225      */
226     public void setLayout(LayoutManager manager)
227     {
228         String caller = JTUtilities.whoCalledMe();
229 
230         if (caller.startsWith("java.awt.Window"))
231             super.setLayout(manager);
232     }
233 
234     /**
235      * Provide the information to fill the name field.
236      * <p>
237      * Note: It only accepts one line, any excess will not be showed and a
238      * ellipsis will be shown at the end of the line.
239      *
240      * @param name
241      */
242     public void setName(String name)
243     {
244         String oldValue = this.name;
245 
246         this.name = name;
247         nameLabel.setText(name);
248 
249         firePropertyChange("name", oldValue, name);
250     }
251 
252     public String getName()
253     {
254         return name;
255     }
256 
257     /**
258      * Provide the information to fill the version field.
259      * <p>
260      * Note: It only accepts one line, any excess will not be showed and a
261      * ellipsis will be shown at the end of the line.
262      *
263      * @param version
264      */
265     public void setVersion(String version)
266     {
267         String oldValue = this.version;
268 
269         this.version = version;
270         versionLabel.setText(version);
271 
272         firePropertyChange("version", oldValue, version);
273     }
274 
275     public String getVersion()
276     {
277         return version;
278     }
279 
280     /**
281      * Provide the information to fill the copyright field.
282      * <p>
283      * Note: It only accepts one line, any excess will not be showed and a
284      * ellipsis will be shown at the end of the line.
285      *
286      * @param copyright
287      */
288     public void setCopyright(String copyright)
289     {
290         String oldValue = this.copyright;
291 
292         this.copyright = copyright;
293         copyrightTextPane.setText(copyright);
294 
295         firePropertyChange("copyright", oldValue, copyright);
296     }
297 
298     public String getCopyright()
299     {
300         return copyright;
301     }
302 
303     /**
304      * Provide the information to fill the license comment field.
305      * <p>
306      * Note:  It will word wrap and show only 3 lines maximum.
307      *
308      * @param licenseComment
309      */
310     public void setLicenseComment(String licenseComment)
311     {
312         String oldValue = this.licenseComment;
313 
314         this.licenseComment = licenseComment;
315         licenseCommentLabel.setText(licenseComment);
316 
317         firePropertyChange("licenseComment", oldValue, licenseComment);
318     }
319 
320     public String getLicenseComment()
321     {
322         return licenseComment;
323     }
324 
325     /**
326      * Provide the information to fill the license details field.
327      * <p>
328      * Note:  It will word wrap and provide a vertical scroll bar if needed.
329      *
330      * @param licenseDetails
331      */
332     public void setLicenseDetails(String licenseDetails)
333     {
334         String oldValue = this.licenseDetails;
335 
336         this.licenseDetails = licenseDetails;
337         licenseTextPane.setText(licenseDetails);
338 
339         firePropertyChange("licenseDetails", oldValue, licenseDetails);
340     }
341 
342     public String getLicenseDetails()
343     {
344         return licenseDetails;
345     }
346 
347     /**
348      * Sets the banner at the top of the about box with the provided file.
349      * <p>
350      * Note:  The {@link File} must point to a valid image.  That image must be
351      * not larger than 414 x 76 pixels.  If image is invalid in anyway an
352      * {@link IllegalArgumentException} will be thrown.  If the file is null or
353      * does not exist the default JavaThis banner will be displayed.  If you pass
354      * <code>true</code> to the {@link #noBanner} then no banner will be displayed
355      * and the area will be left blank, and unusable.
356      *
357      * @see     #noBanner
358      * @param   banner
359      * @throws  IllegalArgumentException
360      */
361     public void setBanner(File banner)
362     {
363         if (!noBanner)
364         {
365             if (banner != null && banner.exists())
366             {
367                 File oldValue =  bannerFile != null ? new File(bannerFile.getAbsolutePath()) : null;
368 
369                 bannerFile = new File(banner.getAbsolutePath());
370 
371                 ImageIcon tempIcon = JTUtilities.getImageIcon(bannerFile.getAbsolutePath());
372 
373                 if (tempIcon != null && tempIcon.getIconWidth() <= BANNER_SIZE.width && tempIcon.getIconHeight() <= BANNER_SIZE.height)
374                 {
375                     bannerLabel.setIcon(tempIcon);
376 
377                      firePropertyChange("banner", oldValue, bannerFile);
378                 }
379                 else
380                     throw new IllegalArgumentException(bannerFile.toString());
381             }
382             else
383             {
384                 File oldValue = bannerFile != null ? new File(bannerFile.getAbsolutePath()) : null;
385 
386                 bannerFile = new File(JTUtilities.fixURLFileString(DEFAULT_BANNER_URL.getFile()));
387 
388                 bannerLabel.setIcon(JTUtilities.getImageIcon(bannerFile.getAbsolutePath()));
389 
390                 firePropertyChange("banner", oldValue, bannerFile);
391             }
392         }
393     }
394 
395     /**
396      * Sets the icon at the left of the about box with the provided file.
397      * <p>
398      * Note:  The {@link File} must point to a valid image.  That image must be
399      * 32 x 32 pixels.  If image is invalid in anyway an
400      * {@link IllegalArgumentException} will be thrown.  If the file is null or
401      * does not exist the default JavaThis icon will be displayed.  If you pass
402      * <code>true</code> to the {@link #noIcon} then no icon will be displayed
403      * and the area will be left blank, and unusable.
404      *
405      * @see     #noIcon
406      * @param   icon
407      * @throws  IllegalArgumentException
408      */
409     public void setIcon(File icon)
410     {
411         if (!noIcon)
412         {
413             if (icon != null && icon.exists())
414             {
415                 File oldValue = iconFile != null ? new File(iconFile.getAbsolutePath()) : null;
416 
417                 iconFile = new File(icon.getAbsolutePath());
418 
419                 ImageIcon tempIcon = JTUtilities.getImageIcon(iconFile.getAbsolutePath());
420 
421                 if (tempIcon != null && tempIcon.getIconWidth() == ICON_SIZE.width && tempIcon.getIconHeight() == ICON_SIZE.height)
422                 {
423                     iconLabel.setIcon(tempIcon);
424 
425                     firePropertyChange("icon", oldValue, iconFile);
426                 }
427                 else
428                     throw new IllegalArgumentException(iconFile.toString());
429             }
430             else
431             {
432                 File oldValue = iconFile != null ? new File(iconFile.getAbsolutePath()) : null;
433 
434                 iconFile = new File(JTUtilities.fixURLFileString(DEFAULT_ICON_URL.getFile()));
435 
436                 iconLabel.setIcon(JTUtilities.getImageIcon(iconFile.getAbsolutePath()));
437 
438                 firePropertyChange("icon", oldValue, iconFile);
439             }
440         }
441     }
442 
443     /**
444      * Used to disable the showing of a banner.
445      *
446      * @param noBanner
447      */
448     public void noBanner(boolean noBanner)
449     {
450         this.noBanner = noBanner;
451 
452         if (noBanner)
453         {
454             File oldValue = bannerFile != null ? new File(bannerFile.getAbsolutePath()) : null;
455 
456             bannerFile = new File(JTUtilities.fixURLFileString(EMPTY_BANNER.getFile()));
457 
458             bannerLabel.setIcon(JTUtilities.getImageIcon(bannerFile.getAbsolutePath()));
459 
460             firePropertyChange("banner", oldValue, bannerFile);
461         }
462     }
463 
464     public boolean isNoBanner()
465     {
466         return noBanner   ;
467     }
468 
469     /**
470      * Used to disable the showing of a icon.
471      *
472      * @param noIcon
473      */
474     public void noIcon(boolean noIcon)
475     {
476         this.noIcon = noIcon;
477 
478         if (noIcon)
479         {
480             File oldValue = iconFile != null ? new File(iconFile.getAbsolutePath()) : null;
481 
482             iconFile = new File(JTUtilities.fixURLFileString(EMPTY_ICON.getFile()));
483 
484             iconLabel.setIcon(JTUtilities.getImageIcon(iconFile.getAbsolutePath()));
485 
486             firePropertyChange("banner", oldValue, iconFile);
487         }
488     }
489 
490     public boolean isNoIcon()
491     {
492         return noIcon   ;
493     }
494 
495     private void jtInit(String name, String version, String copyright, String licenseComment, String licenseDetails, File banner, File icon)
496     {
497         setSize(DEFAULT_DIALOG_SIZE);
498         setResizable(false);
499         setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
500 
501         okButton.setText(JTUtilities.COMMON_RESOURCE_BUNDLE.getString("ok.Text"));
502 
503         setName(name);
504         setVersion(version);
505         setCopyright(copyright);
506         setLicenseComment(licenseComment);
507         setLicenseDetails(licenseDetails);
508         setBanner(banner);
509         setIcon(icon);
510 
511         registerEvents();
512         guiInit();
513     }
514 
515     private void registerEvents()
516     {
517         okButton.addActionListener(new ActionListener()
518         {
519             public void actionPerformed(ActionEvent event)
520             {
521                 hide();
522             }
523         });
524     }
525 
526     private void guiInit()
527     {
528         getContentPane().setLayout(new BorderLayout());
529         bannerPanel.setLayout(new BorderLayout());
530         iconPanel.setLayout(new GridBagLayout());
531         mainPanel.setLayout(new GridBagLayout());
532         buttonPanel.setLayout(new JTCommandButtonLayout());
533 
534         iconLabel.setHorizontalAlignment(SwingConstants.CENTER);
535         iconLabel.setHorizontalTextPosition(SwingConstants.CENTER);
536 
537         bannerLabel.setHorizontalAlignment(SwingConstants.CENTER);
538         bannerLabel.setHorizontalTextPosition(SwingConstants.CENTER);
539 
540         copyrightTextPane.setDisabledTextColor(nameLabel.getForeground());
541         copyrightTextPane.setBackground(nameLabel.getBackground());
542         copyrightTextPane.setForeground(nameLabel.getForeground());
543 
544         licenseTextPane.setDisabledTextColor(nameLabel.getForeground());
545         licenseTextPane.setBackground(nameLabel.getBackground());
546         licenseTextPane.setForeground(nameLabel.getForeground());
547 
548         nameLabel.setFont(aboutFont);
549         versionLabel.setFont(aboutFont);
550         copyrightTextPane.setFont(aboutFont);
551         licenseCommentLabel.setFont(aboutFont);
552         licenseTextPane.setFont(aboutFont);
553 
554         licenseScrollPane.setBorder(BorderFactory.createEmptyBorder());
555 
556 
557         copyrightTextPane.setEnabled(false);
558         licenseTextPane.setEnabled(false);
559 
560         copyrightTextPane.setEditable(false);
561         licenseTextPane.setEditable(false);
562 
563         copyrightTextPane.setMargin(new Insets(0, 0, 0, 0));
564         licenseTextPane.setMargin(new Insets(0, 0, 0, 0));
565 
566         getContentPane().add(bannerPanel, BorderLayout.NORTH);
567         getContentPane().add(mainPanel,  BorderLayout.CENTER);
568         getContentPane().add(buttonPanel, BorderLayout.SOUTH);
569 
570         bannerPanel.add(bannerLabel,  BorderLayout.CENTER);
571         buttonPanel.add(okButton, null);
572 
573         bannerLabel.setMinimumSize(BANNER_SIZE);
574         bannerLabel.setMaximumSize(BANNER_SIZE);
575         bannerLabel.setPreferredSize(BANNER_SIZE);
576 
577         copyrightTextPane.setMinimumSize(COPYRIGHT_SIZE);
578         copyrightTextPane.setMaximumSize(COPYRIGHT_SIZE);
579         copyrightTextPane.setPreferredSize(COPYRIGHT_SIZE);
580 
581         getContentPane().add(iconPanel,  BorderLayout.WEST);
582         iconPanel.add(iconLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 1.0
583             ,GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(10, 35, 0, 0), 0, 0));
584         mainPanel.add(nameLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
585             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(10, 40, 1, 6), 0, 0));
586         mainPanel.add(versionLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
587             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(1, 40, 1, 6), 0, 0));
588         mainPanel.add(copyrightTextPane, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0
589             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(1, 40, 1, 6), 0, 0));
590         mainPanel.add(licenseCommentLabel, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0
591             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(1, 40, 1, 6), 0, 0));
592         mainPanel.add(licenseScrollPane,  new GridBagConstraints(0, 4, 1, 1, 1.0, 1.0
593             ,GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(1, 40, 1, 6), 0, 0));
594     }
595 
596     /**
597      * Overridden to center tje about box relative to it's owner before each
598      * showing.
599      */
600     public void show()
601     {
602         JTUtilities.centerRelativeFrom(getOwner(), this);
603 
604         super.show();
605     }
606 
607     /**
608      * Used for simple testing of about box using default settings.
609      *
610      * @param args
611      */
612     public static void main(String[] args)
613     {
614         try
615         {
616             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
617         }
618         catch(Exception e)
619         {
620             e.printStackTrace();
621             JTUtilities.showErrorMessage(null, e.getMessage(), e.getClass().toString());
622         }
623 
624         JTOrnateAboutBox dialog = new JTOrnateAboutBox();
625 
626         //dialog.noBanner(true);
627         //dialog.noIcon(true);
628 
629         dialog.show();
630     }
631 }