001    /** =====================================================================       
002    *
003    *  File Name : $Id: PrintFaultDialog.java,v 1.6 2008/01/15 11:08:15 cb Exp $
004    *
005    *  Description
006    *  -----------
007    *
008    *  =====================================================================
009    *
010    *   @author : Craige Bevil 
011    *             Control Software Group
012    *             Isaac Newton Group of Telescopes
013    *
014    *  =====================================================================
015    *
016    *     Modification Log
017    *
018    *     Vers         Date        Author       Reason
019    *     ----         ----        ------       ------
020    *      1                       C.Bevil      First Release
021    *
022    *     Commissioning Notes
023    *     -------------------
024    *
025    *     None
026    *     
027    *  =====================================================================
028    *
029    *     @version   : $Id: PrintFaultDialog.java,v 1.6 2008/01/15 11:08:15 cb Exp $
030    *
031    *     @author    : $Author: cb $
032    *
033    *     Header     : $Header: /opt/INGsrc/src/CVS/softproj/FaultDatabase/src/FaultDatabase/FaultDatabase/src/GWTApplication/client/PrintFaultDialog.java,v 1.6 2008/01/15 11:08:15 cb Exp $
034    *
035    *     Log        : $Log: PrintFaultDialog.java,v $
036    *     Log        : Revision 1.6  2008/01/15 11:08:15  cb
037    *     Log        : Ran through PMD and sorted out the javadoc so that we could export the
038    *     Log        : javadoc to the javadoc repository.
039    *     Log        :
040    *     Log        : Revision 1.5  2007/12/12 15:26:11  cb
041    *     Log        : Added new javascript library which allows fancy message boxes to be
042    *     Log        : displayed.
043    *     Log        :
044    *     Log        : Revision 1.4  2007/10/19 08:51:57  cb
045    *     Log        : Updated so that the cookie storing the printer name does not expire at
046    *     Log        : the end of the browser session.
047    *     Log        :
048    *     Log        : Revision 1.3  2007/08/17 14:26:41  cb
049    *     Log        : Updated for lastest prototype incorporating a lot of Nikos comments
050    *     Log        :
051    *     Log        : Revision 1.2  2007/08/01 13:00:04  cb
052    *     Log        : First prototype after import
053    *     Log        :
054    *     Log        : Revision 1.1  2007/07/24 08:27:08  cb
055    *     Log        : First version
056    *     Log        :
057    *
058    * =====================================================================*/
059    
060    package GWTApplication.client;
061    
062    import com.google.gwt.user.client.*;
063    import com.google.gwt.user.client.rpc.*;
064    import com.google.gwt.user.client.ui.*;
065    import com.google.gwt.user.client.ui.DockPanel.*;
066    
067    import com.gwtext.client.core.EventObject;
068    import com.gwtext.client.util.Format;
069    import com.gwtext.client.widgets.MessageBox;
070    import com.gwtext.client.widgets.MessageBoxConfig;
071    
072    import com.google.gwt.user.client.ui.FlexTable.*;
073    import java.util.*;
074    
075    /**
076     * This class will allow the user to print a fault. The dialog box
077     * will allow the user to select a valid printer from a list provided
078     * by the servlet (basically the printers which the tomcat host has
079     * been configured for) and then instructs it to print the fault
080     * specified in the constructor when the user presses the submit
081     * button.
082     * @version $Id: PrintFaultDialog.java,v 1.6 2008/01/15 11:08:15 cb Exp $
083     * @author Craige Bevil 
084     */
085    
086    public class PrintFaultDialog extends FaultDBForm implements ClickListener {
087    
088        /**
089         * Name of the cookie in which we store the mode in which we want to print 
090         */
091        
092        final String PRINTMODECOOKIE = "PRINTORIENTATION";
093    
094        /**
095         * Used when storing in a cookie the orientation of the page to be printed. 
096         */
097        
098        final String LANDSCAPEMODE = "LANDSCAPE";
099    
100        /**
101         * Used when storing in a cookie the orientation of the page to be printed. 
102         */
103        
104        final String PORTRAITMODE = "PORTRAIT";
105        
106        /**
107         * Widget which is used by the user to select the printer
108         */
109        
110        final private LabelListBox printerSelectionList;
111    
112        /**
113         * This is the fault to be printed
114         */
115    
116        private int faultToPrint;
117        
118        /**
119         * This is the submit button widget 
120         */
121        
122        final private Button SubmitButton;
123    
124        /**
125         * This is the cancel button widget 
126         */
127        
128        final private Button CancelButton;
129        
130        /**
131         * Checkbox in which the user can select if he wants to print in
132         * landscape or portrait mode.
133         */
134        
135        final private CheckBox PrintModeCheckBox;
136    
137        /**
138         * This is the dialog box which will be displayed which will allow
139         * the user to select the printer
140         */
141        
142        DialogBox PrintDialog = new DialogBox(false);   
143                        
144        /**
145         * Constructor
146         */
147        
148        PrintFaultDialog (final FaultServiceAsync svc) throws  Exception {
149    
150            this.svc = svc;
151            
152            // If we have no printers then we cannot print 
153    
154            if (PrinterList == null || PrinterList.size() == 0) {
155                MessageBox.alert(internationalizationConstants.information(),"Unable to establish printer list");
156                throw new Exception();
157            }
158            
159            DockPanel panel = new DockPanel();
160            
161            printerSelectionList = new LabelListBox(LabelListBox.HORIZONTAL_ALIGNMENT);
162            
163            printerSelectionList.label.setText(internationalizationConstants.whichPrinterToUse());
164            printerSelectionList.setWidth("4cm","6cm");
165            
166            // Now add the printers to the list of printers.
167            
168            for (int i=0; i < PrinterList.size();i++) { 
169                printerSelectionList.listBox.addItem((String)PrinterList.get(i));
170            }
171            
172            panel.add(printerSelectionList,DockPanel.NORTH);
173            
174            // Now a button which will allow the user to select if he
175            // wants to print in landscape or portrait mode.
176    
177            final String printMode = Cookies.getCookie(PRINTMODECOOKIE);
178                    
179            PrintModeCheckBox = new CheckBox("&nbsp;&nbsp;" + internationalizationConstants.landscapeMode(),true);
180    
181            if (printMode.equals(PORTRAITMODE)) {
182                PrintModeCheckBox.setChecked(false);
183            } else {
184                PrintModeCheckBox.setChecked(true);             
185            }
186            
187            panel.add(PrintModeCheckBox,DockPanel.NORTH);
188            
189            // Now add the submit and cancel button 
190            
191            SubmitButton = new Button(internationalizationConstants.printFault(),this);
192            CancelButton = new Button(internationalizationConstants.cancel(),this);
193    
194            panel.add(SubmitButton,DockPanel.WEST);
195            panel.add(CancelButton,DockPanel.WEST);
196    
197            panel.setSpacing(20);
198    
199            // Now add the main panel to the dialog box
200    
201            PrintDialog.setWidget(panel);
202    
203            PrintDialog.setText(internationalizationConstants.printFault());
204    
205            // Get the name of the last printer which was used by the user
206            // and set the default printer accordingly
207    
208            final String defaultPrinter = Cookies.getCookie(FaultDatabaseConstants.FDBPRINTER);
209            
210            // Set the default option in the printer list to be that of
211            // the last printer which was selected by the user.
212    
213            printerSelectionList.setSelectedItem(defaultPrinter);
214                    
215        }   
216    
217    
218        /**
219         * This is the click handler which is called when the user calls
220         * presses one of the buttons on the print dialog. He can either
221         * press the print button or the cancel button.
222         * @param sender This is the widget which created the event. 
223         */
224        
225        public void onClick (Widget sender) {
226            
227            if (sender.equals(SubmitButton)) {
228    
229                // Get the name of the printer that the user has selected 
230                
231                final String printerName = printerSelectionList.getSelectedItemText();
232                
233                if (printerName == null) {
234                    MessageBox.alert(internationalizationConstants.information(),internationalizationConstants.youMustSelectAPrinterFromTheList());
235                    return;
236                }
237    
238                // Save the name the printer so the user
239                // has a default printer in the future
240                
241                Cookies.setCookie(FaultDatabaseConstants.FDBPRINTER,printerName,new Date (150,11,31));
242    
243                if (PrintModeCheckBox.isChecked()) {
244                    Cookies.setCookie(PRINTMODECOOKIE,LANDSCAPEMODE,new Date (150,11,31));                      
245                } else {
246                    Cookies.setCookie(PRINTMODECOOKIE,PORTRAITMODE,new Date (150,11,31));                                                   
247                }
248                        
249                // Get the name of the selected printer from the interface
250                
251                svc.printFault(faultToPrint,printerName,PrintModeCheckBox.isChecked(),new AsyncCallback() {
252                        
253                        /**
254                         * On success we remove the dialog from the
255                         * display and report that we were successful to
256                         * the user
257                         */
258                        
259                        public void onSuccess (final Object result) {
260                            PrintDialog.hide();
261                        }
262                        
263                        public void onFailure (Throwable ex)
264                        {
265                            MessageBox.alert(internationalizationConstants.information(),internationalizationConstants.unableToPrintFault(ex.getMessage()));
266                        }
267                    });
268                
269            } else if (sender.equals(CancelButton)) {
270                PrintDialog.hide();
271            }
272        }
273            
274        /**
275         * Displays the new dialog which will allow the user to print this
276         * fault with the fault specified.
277         *
278         * @param faultToPrint This is the id. of the fault which is to be
279         * printed when the user presses the submit button
280         */
281        
282        public void showPrintDialog (final int faultToPrint) {
283    
284            this.faultToPrint = faultToPrint;
285                    
286            PrintDialog.show();             
287            PrintDialog.center();
288        }
289    }
290