BlackBoard.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.BorderLayout;
049 import java.awt.Dialog;
050 import java.awt.Frame;
051 import java.awt.GridLayout;
052 import java.awt.Window;
053 import java.awt.event.ActionEvent;
054 import java.awt.event.ActionListener;
055 
056 import javax.swing.BoxLayout;
057 import javax.swing.JButton;
058 import javax.swing.JLabel;
059 import javax.swing.JPanel;
060 
061 
062 /**
063  * This class provides a embeddable panel that can be used to view
064  * statistics like 
065  <ul>
066  <li>number of stored profiles</li>
067  <li>number of submitted profiles</li> 
068  <li>current mail configuration</li> 
069  </ul>
070  * Furthermore, you can send queued profiles and start the wizard to
071  * reconfigure the settings.<br> In opposite to DocWhatsUp dialogs, you need no
072  * occured error, so it can be created&amp;embedded whereever and
073  * whenever it`s needed.
074  
075  @author <A HREF="mailto:codeshaker@gmx.net">
076  *            Philipp Bartsch (codeshaker@gmx.net)</A>
077  *            <A HREF="../../../../gpl.txt">GPL License</A>
078  */
079 public final class      BlackBoard
080              extends    JPanel
081              implements ActionListener {
082     
083     /**ActionCommand: Start the wizard*/
084     private static  final     String      SHOW_WIZARD    = "SHOW_WIZARD";
085     /**ActionCommand: Send all reports*/
086     private static  final     String      SEND_ALL    = "SEND_ALL";
087     /**A reference to the settings*/
088     private static  final   Settings     SETTINGS    = DocWhatsUp.SETTINGS;
089     
090     /**The label of the statusbar*/
091     private final   JLabel      statusLabel = new JLabel(" ");
092     /**The statusbar itself*/
093     private final   JLabel      statusMsg   = new JLabel(" ");
094     /**The parental Component*/
095     private final   Window     parent;
096     /**Properties Panel*/          
097     private final   JPanel   propView     = new JPanel(new BorderLayout(3,3));
098     /**BugProfile`s Panel*/
099     private final   JPanel   profView      = new JPanel(new BorderLayout(3,3));
100     /**Send button*/
101     private final   JButton  sendBtn     = GUIFactory.button("sendall_btn",
102                                                                 this,
103                                                                 SEND_ALL,
104                                                                 "send.png");
105     /**Wizard starter button*/                                                                
106     private final   JButton     wizardBtn   = GUIFactory.button("startwiz_btn",
107                                                                 this,
108                                                                 SHOW_WIZARD,
109                                                                 "wizard.png");                                                                                                                                    
110     
111     /**
112      * Creates an ready-to-use BlackBoard instance. Only DWUFactory calls this
113      * constructor.
114      *
115      *@param parentalContainer the container (JFrame or JDialog) that holds this
116      *                           panel
117      */
118     BlackBoard (final Window parentalContainer) {
119         parent = parentalContainer;
120         setLayout(new BoxLayout(this, 
121                                 BoxLayout.Y_AXIS));
122         //
123         initStatusLabels();
124         //
125         add(initDescriptionPanel());
126         add(initConfigPanel());
127         add(initSubmissionPanel());    
128         //    
129                                     
130     }
131     
132     /**
133      * Inits a general information panel about DocWhatsUp.
134      *
135      @return the description panel
136      */ 
137     private JPanel initDescriptionPanel () {
138         final JPanel descriptionPanel = new JPanel (new BorderLayout(22));
139         descriptionPanel.setBorder(GUIFactory.titledEBorder("brd_consl_help"));
140         descriptionPanel.add(GUIFactory.textArea("console_help",
141                                                  false,
142                                                  true),
143                              BorderLayout.CENTER);        
144         return descriptionPanel;
145     }
146     
147     /**
148      * Inits a configuration information panel and displays the current
149      * mail configuration based on .default.settings or .custom.settings.
150      *
151      @return the config panel
152       */
153     private JPanel initConfigPanel () {
154         final JPanel configPanel = new JPanel (new BorderLayout(22));        
155         //
156         configPanel.setBorder(GUIFactory.titledLBorder("brd_props_view"));
157         initPropertiesSummary();
158         configPanel.add(propView,
159                         BorderLayout.NORTH);    
160         //                                      
161         configPanel.add(wizardBtn,
162                           BorderLayout.SOUTH);    
163         //                         
164         return configPanel;
165     }
166     
167     /**
168      * Inits the propView panel, that visualises the mail config settings.
169      * It is called whenever the wizard, started within this console,
170      * has changed something.
171      */
172     private void initPropertiesSummary () {        
173         propView.removeAll();
174         // if the wizard has never been started, the send button must be 
175         // disabled. furthermore the wizard button is displayed prominently.
176         if (Settings.isConfigurated()) {
177             wizardBtn.setForeground(GUIFactory.BLACK);
178             sendBtn.setEnabled(DocWhatsUp.getQueueCount() 0);
179         else {
180             wizardBtn.setForeground(GUIFactory.RED);
181             sendBtn.setEnabled(false);            
182         }
183         
184         // assemble some info labels
185         final JPanel labelPanel = new JPanel (new GridLayout(4,0,3,3));
186         final JPanel valuePanel = new JPanel (new GridLayout(4,0,3,3));
187 
188         //
189         labelPanel.add(GUIFactory.label("mailaddress",
190                                         false,
191                                         true));
192         valuePanel.add(GUIFactory.label(SETTINGS.getSender() == null
193                                         "?"
194                                         : SETTINGS.getSender().toString(),
195                                           true,
196                                           false))
197         //                  
198         labelPanel.add(GUIFactory.label("username",
199                                           false,
200                                           true));
201         valuePanel.add(GUIFactory.label(SETTINGS.hasUser()
202                                            ? SETTINGS.getUser()
203                                            "?",
204                                           true,
205                                           false))
206         //                  
207         labelPanel.add(GUIFactory.label("popserver",
208                                           false,
209                                           true));
210         valuePanel.add(GUIFactory.label(SETTINGS.hasPOPServer()
211                                           ? SETTINGS.getPOPServer()
212                                             ":" + SETTINGS.getPOPPort()
213                                           "?",
214                                           true,
215                                           false));
216         //
217         labelPanel.add(GUIFactory.label("smtpserver",
218                                           false,
219                                           true));
220 
221         valuePanel.add(GUIFactory.label(SETTINGS.hasSMTPServer()
222                                           ? SETTINGS.getSMTPServer()
223                                            ":" + SETTINGS.getSMTPPort()
224                                           "?",
225                                           true,
226                                           false));  
227         propView.add(labelPanel, 
228                      BorderLayout.WEST);
229         propView.add(valuePanel, 
230                      BorderLayout.CENTER);
231         propView.revalidate();                     
232     }
233     
234     /**
235      * Inits a submission information panel. Just displays the size of the 
236      * Profile queue and the size of the submission property in
237      * submitted.properties. Includes a size statement in kb.
238      *
239      @return the submission overview panel
240       */
241     private JPanel initSubmissionPanel () {
242         final JPanel submissionPanel = new JPanel (new BorderLayout(22));        
243         //
244         submissionPanel.setBorder(GUIFactory.titledLBorder("brd_send_view"));
245         initSubmissionSummary();
246         submissionPanel.add(profView,
247                             BorderLayout.NORTH);    
248         //                                      
249         submissionPanel.add(sendBtn,
250                             BorderLayout.SOUTH);    
251         //                         
252         sendBtn.setEnabled(DocWhatsUp.getQueueCount() 0);
253         return submissionPanel;
254         
255     }
256     
257     /**
258      * Inits the status bar labels.
259      */
260     private void initStatusLabels () {
261         statusLabel.setFont(GUIFactory.ARIAL_10B);
262         statusMsg.setFont(GUIFactory.ARIAL_10B);
263         statusLabel.setOpaque(true);
264         statusMsg.setOpaque(true);    
265         statusLabel.setHorizontalAlignment(JLabel.RIGHT);
266         statusLabel.setForeground(GUIFactory.BLACK);
267     }
268     
269     /**
270      * Inits the sendView panel, that visualises all submission informations.
271      * It is called whenever the MailEngine, started by the BlackBoard,
272      * has send something.
273      */
274     private void initSubmissionSummary () {
275         profView.removeAll();
276         
277         final JPanel panel1 = new JPanel (new GridLayout(3,0,3,3));
278         final JPanel panel2 = new JPanel (new GridLayout(3,0,3,3));
279         
280         panel1.add(GUIFactory.label("queue_count",
281                                     false,
282                                     true));
283         panel2.add(GUIFactory.label(toTransferSize(DocWhatsUp.getQueueCount()),
284                                     true,
285                                     false))
286                                           
287         panel1.add(GUIFactory.label("submit_count",
288                                     false,
289                                     true));
290         panel2.add(GUIFactory.label(toTransferSize(SETTINGS.getTransferCount()),
291                                     true,
292                                     false))
293         
294         panel1.add(statusLabel);
295         panel2.add(statusMsg)
296         
297         profView.add(panel1, 
298                      BorderLayout.WEST);
299         profView.add(panel2, 
300                      BorderLayout.CENTER);
301         profView.revalidate();
302     }    
303 
304     /**
305      * Resets the embedded status panel.
306      
307      @param dictKey     the message dictionary key
308      @param isError   is true, the label has a red foreground
309      */
310     private void resetStatus (final String   dictKey,
311                               final boolean  isError) {
312         if (dictKey.length() 0) {
313             statusLabel.setText("Message");
314             statusLabel.setBackground(GUIFactory.BLUE);
315             statusMsg.setForeground((isError
316                                      ? GUIFactory.RED
317                                      : GUIFactory.BLACK));
318             statusMsg.setText("   " + ToolBox.localize(dictKey));
319         else {    
320             statusLabel.setText("");
321             statusLabel.setBackground(GUIFactory.GRAY);
322             statusMsg.setText (" ");
323         }        
324     }
325     
326     /**
327      * Returns the estimated amount in kb of a given Bug Profiles count. This 
328      * value depends on the format and amount, so it`s estimates an amount of 
329      * 3 Kbyte per mail (an average between text and html).
330      
331      @param  count  the number of profiles
332      @return String estimated transfer amount in Kbyte
333      */
334     private static String toTransferSize (final int count) {
335         return count + (count == 0
336                         ""
337                         " (" + count*" KB)");
338     }    
339     
340     /**
341      * Handles SHOW_WIZARD and SEND_ALL ActionEvents.
342      *
343      @param ae the event
344      @see #SHOW_WIZARD show configuration wizard command
345      @see #SEND_ALL    send all reports command
346      */
347     public final void actionPerformed(final ActionEvent ae) {
348         final String command = ae.getActionCommand();
349         
350         if (command.equals(SEND_ALL)) {            
351             if (!SETTINGS.hasPassword()
352                 && SETTINGS.containsKey("mail.pop")
353                 && SETTINGS.getProperty("mail.pop").length() 0) {
354                 AuthDialog dialog;
355                 if (parent instanceof Frame)
356                     dialog = new AuthDialog((Frameparent);
357                 else                                                
358                     dialog = new AuthDialog((Dialogparent);
359                 String pass;
360                 if ((pass = dialog.getPassword()) == null)
361                     return;
362                 else
363                     SETTINGS.setProperty("mail.pass"
364                                          pass);
365             }
366             final String state = MailEngine.submit(this,
367                                                    DocWhatsUp.getQueuedBugs());
368             // everything worked fine
369             if (state.length() == 0) {
370                 // reset message label
371                 resetStatus(state,
372                             false);
373                 // reset overview panel
374                 initSubmissionSummary();
375             // submission failed
376             else {                
377                 // print error message
378                 resetStatus(state,
379                             true);                      
380             }
381             // forget given password
382             SETTINGS.removePassword();            
383                 
384         else if (command.equals(SHOW_WIZARD)) {
385             SetupDialog wizard;
386             if (parent instanceof Frame)
387                 wizard = new SetupDialog ((Frameparent);
388             else                                                
389                 wizard = new SetupDialog ((Dialogparent);
390             wizard.show();
391             initPropertiesSummary();    
392         }
393     }
394 }