GUIFactory.java
001 /*
002  * This class is part of DocWhatsUp, a bug profiling and -reporting lib.
003  
004  * Copyright (C)
005  
006  * This library is free software; you can redistribute it and/or modify it
007  * under the terms of the GNU General Public License as published by the
008  * Free Software Foundation; either version 2 of the License, or (at your
009  * option) any later version.
010  
011  * This program is distributed in the hope that it will be useful, but
012  * WITHOUT ANY WARRANTY; without even the implied warranty of
013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
014  * Public License for more details.
015  *  
016  * You should have received a copy of the GNU General Public License along
017  * with this program; if not, write to the Free Software Foundation, Inc.,
018  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019  
020  * EXTENSION:
021  * Linking DocWhatsUp statically or dynamically with other modules is making a
022  * combined work based on DocWhatsUp.  Thus, the terms and conditions of the 
023  * GNU General Public License cover the whole combination.
024  *
025  * As a special exception, the copyright holder of DocWhatsUp give you
026  * permission to link DocWhatsUp with independent modules that communicate with
027  * DocWhatsUp solely through the DocWhatsUp.java interface, regardless of the 
028  * license terms of these independent modules, and to copy and distribute the
029  * resulting combined work under terms of your choice, provided that
030  * every copy of the combined work is accompanied by a complete copy of
031  * the source code of DocWhatsUp (the version of DocWhatsUp used to produce the
032  * combined work), being distributed under the terms of the GNU General
033  * Public License plus this exception.  An independent module is a module
034  * which is not derived from or based on DocWhatsUp.
035 
036  * Note that people who make modified versions of DocWhatsUp are not obligated
037  * to grant this special exception for their modified versions; it is
038  * their choice whether to do so.  The GNU General Public License gives
039  * permission to release a modified version without this exception; this
040  * exception also makes it possible to release a modified version which
041  * carries forward this exception.
042  
043  * Author: Philipp Bartsch; codeshaker@gmx.net
044  */
045 
046 package org.shaker.dwu;
047 
048 import java.awt.Color;
049 import java.awt.Dimension;
050 import java.awt.Font;
051 import java.awt.event.ActionListener;
052 import java.awt.event.FocusListener;
053 
054 import javax.swing.BorderFactory;
055 import javax.swing.ImageIcon;
056 import javax.swing.JButton;
057 import javax.swing.JComponent;
058 import javax.swing.JEditorPane;
059 import javax.swing.JLabel;
060 import javax.swing.JScrollPane;
061 import javax.swing.JTextArea;
062 import javax.swing.JTextField;
063 import javax.swing.border.Border;
064 import javax.swing.border.EtchedBorder;
065 import javax.swing.border.SoftBevelBorder;
066 import javax.swing.border.TitledBorder;
067 
068 
069 /**
070  * This pure static class delivers standardized widgets.
071  
072  @author <A HREF="mailto:codeshaker@gmx.net">
073  *            Philipp Bartsch (codeshaker@gmx.net)</A>
074  *            <A HREF="../../../../gpl.txt">GPL License</A>
075  */
076 final class GUIFactory {
077 
078     /**Standard background gray color object*/
079     static final Color          GRAY       = new JLabel().getBackground();
080     /**DWU`s blue color object*/    
081     static final Color          BLUE       = new Color(100149237);
082     /**DWU`s red color object*/
083     static final Color          RED        = Color.red.darker();
084     /**Black color object*/
085     static final Color          BLACK      = Color.black;
086     /**White color object*/
087     static final Color          WHITE      = Color.white;
088 
089     //===============
090 
091     /**Arial 10 plain*/
092             static final Font          ARIAL_10P  = new Font("Arial",
093                                                            Font.PLAIN,
094                                                            10);
095     /**Arial 10 bold*/
096             static final Font          ARIAL_10B  = new Font("Arial",
097                                                            Font.BOLD,
098                                                            10);
099     /**Arial 9 bold*/
100             static final Font          ARIAL_9B   = new Font("Arial",
101                                                            Font.BOLD,
102                                                            9);
103     /**Arial 9 plain*/
104             static final Font          ARIAL_9P   = new Font("Arial",
105                                                            Font.PLAIN,
106                                                            9);
107     /**Courier 10 plain (monospace font)*/
108     private static final Font          COURIER_10 = new Font("Courier",
109                                                            Font.PLAIN,
110                                                            10);
111 
112     //===============
113 
114     /**100 x 18 Dimesion object*/
115     private static final Dimension D100x18  = new Dimension(100,
116                                                             18);
117     /**250 x 300 Dimesion object*/
118     private static final Dimension D250x300 = new Dimension(250,
119                                                             300);
120     /**Simple black line border*/
121             static final Border       BLACKLN  = BorderFactory
122                                               .createLineBorder(BLACK);
123     /**Lowered etched border*/
124             static final Border       ETCHED   = new EtchedBorder(
125                                                     EtchedBorder.LOWERED);
126     /**Lowered soft bevel border*/
127             static final Border       LOW         = new SoftBevelBorder(
128                                                     SoftBevelBorder.LOWERED);
129     /**Empty border (5,2,5,2)*/
130     private static final Border    EMPTY1   = BorderFactory.createEmptyBorder(5,
131                                                                               2,
132                                                                               5,
133                                                                              2);
134     /**Empty border (2,2,2,2)*/                                                                   
135             static final Border    EMPTY2     = BorderFactory.createEmptyBorder(2,
136                                                                               2,
137                                                                               2,
138                                                                              2);
139     //===============
140     /** */
141     static final ProgressKit PROGRESSKIT = new ProgressKit ()
142         
143     /**
144      * Invisible constructor. This class is a factory, so it`s not meant to be
145      * instantiated.
146      */
147     private GUIFactory () {}
148     
149     /**
150      * Creates a standardized multiline textarea.
151      
152      @param dictKey        the dictionary key of the shown text
153      @param editable        indicates wheter it is editable
154      @param wordWrap        indicates wheter to wrap whole words
155      @return JTextArea    the textarea
156      */
157     static final JTextArea textArea (final String  dictKey,
158                                      final boolean editable,
159                                      final boolean wordWrap) {
160         final JTextArea area = new JTextArea (ToolBox.localize(dictKey));
161         //
162         if (!editable) {
163             area.setBackground(GRAY);
164             area.setEditable(false);                
165         }
166         area.setLineWrap(true);        
167         area.setWrapStyleWord(wordWrap);        
168         area.setCaretPosition(0);
169         area.setFont(ARIAL_10P);
170         area.setBorder(EMPTY1);    
171         
172         return area; 
173     }
174     
175     /**
176      * Creates a standardized button.
177      
178      @param dictKey       the dictionary key of the shown text
179      @param listener        an actionlistener
180      @param command        an actioncommand
181      @param iconName        an iconname
182      @return JButton        the button
183      */
184     static final JButton button (final String            dictKey,
185                                  final ActionListener     listener,
186                                  final String             command,
187                                  final String            iconName) {
188         final JButton btn = new JButton (ToolBox.localize(dictKey));
189         
190         btn.setPreferredSize(D100x18);
191         btn.setBorder(ETCHED);
192         if (iconName.length() 0)
193             btn.setIcon(GUIFactory.getIcon(iconName));
194         btn.setToolTipText(ToolBox.localize(dictKey + "_tt"));
195         btn.setFont(GUIFactory.ARIAL_10P);
196         btn.setActionCommand(command);
197         btn.addActionListener(listener);
198         
199         return btn; 
200     }
201     
202     /**
203      * Returns a label with requested text.
204      *
205      *
206      @param dictKey       the language dictionary key for the label text
207      @param courierFont   flag indicates wheter to use courier (or arial)
208      @param rightAlign    flag indicates wheter to align right (or left)
209      @return                the label
210      */
211     static final JLabel label (final String  dictKey,
212                                final boolean courierFont,
213                                final boolean rightAlign) {
214         final JLabel label = new JLabel ("  " + ToolBox.localize(dictKey),
215                                          (rightAlign
216                                            ? JLabel.RIGHT
217                                               : JLabel.LEFT));
218         if (!courierFont) {
219             label.setFont(ARIAL_10P);
220             label.setBackground(BLUE);
221         else 
222             label.setFont(COURIER_10);    
223         label.setOpaque(true);
224         label.setForeground(BLACK);
225         return label;
226     }
227     
228     /**
229      * Creates a standardized text/html editorpane with gray background.
230      
231      @return JEditorPane    the editorpane
232      */
233     static final JEditorPane editorPane () {
234         final JEditorPane pane = new JEditorPane("text/plain",
235                                                        "");
236         pane.setBackground(GRAY);
237         return pane;                                                   
238     }
239 
240     /**
241      * Wraps the given component with a scrollbar.
242      *
243      @param component component to wrap
244      @param border    the requested border
245      @return          the scrollpane
246      */
247     static final JScrollPane scrollWrap (final JComponent component,
248                                          final Border      border) {
249         final JScrollPane scroller = new JScrollPane(component);
250         scroller.setPreferredSize(GUIFactory.D250x300);
251         scroller.setBorder(border);
252         
253         return scroller;                         
254     }
255         
256     /**
257      * Creates a standardized textfield.
258      *     
259      @param dictKey        the dictionary key of the shown text
260      @param flsn            an focuslistener or null
261      @return JTextField    the textfield
262      */
263     static final JTextField textField (final String        dictKey,
264                                        final FocusListener flsn) {
265         
266         final JTextField field = new JTextField (ToolBox.localize(dictKey));
267         field.setFont(GUIFactory.ARIAL_10P);
268         field.setColumns(5);
269         if (flsn != null)
270             field.addFocusListener(flsn);
271         
272         return field;
273                                                            
274     }
275     
276     /**
277      * Creates a standardized titled, etched border.
278      
279      @param    dictKey         the dictionary key
280      @return   TitledBorder     the border
281      */
282     static TitledBorder titledEBorder (final String  dictKey) {
283         return BorderFactory.createTitledBorder(ETCHED,
284                                                  ToolBox.localize(dictKey),
285                                                 TitledBorder
286                                                     .DEFAULT_JUSTIFICATION,
287                                                 TitledBorder
288                                                     .DEFAULT_POSITION,
289                                                 ARIAL_9B);
290     }
291     
292     /**
293      * Creates a standardized titled, softbevel (lowered) border.
294      
295      @param    dictKey         the dictionary key
296      @return   TitledBorder     the border
297      */
298     static TitledBorder titledLBorder (final String  dictKey) {
299         return BorderFactory.createTitledBorder(LOW,
300                                                 ToolBox.localize(dictKey),
301                                                 TitledBorder
302                                                     .DEFAULT_JUSTIFICATION,
303                                                 TitledBorder
304                                                     .DEFAULT_POSITION,
305                                                 ARIAL_9B);
306     }
307 
308     /**
309      * Returns an ImageIcon. The method returns an icon from the jar file.
310      
311      @param name            icons filename
312      @return ImageIcon    the ImageIcon
313      */
314     static ImageIcon getIcon (final String name) {
315         try {    
316             ImageIcon image = new ImageIcon(ClassLoader.getSystemClassLoader()
317                                             .getResource("icon/" + name));
318             return image;                                         
319         catch (Exception e) {
320             // requested icon not found; can`t happen
321             ToolBox.logException(e);
322             return null;
323         }
324     }
325     
326     /**
327      * Shows a JWindow with an embedded progressbar. Always called when submit
328      * bug reports visually.
329      *
330      @param messageCount the number of stored bugprofiles
331      */
332     static void showProgressWindow (final int messageCount) {
333         PROGRESSKIT.setProgressMax (messageCount);
334         PROGRESSKIT.setLabel(ToolBox.localize("submitting_messages")
335                              ": " + messageCount);
336         PROGRESSKIT.show();        
337     }
338     
339     /**
340      * Disposes the progress window.
341      */
342     static void killProgressWindow () {        
343         PROGRESSKIT.dispose();
344     }    
345 }