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

Quick Search    Search Deep

Source code: com/flexstor/common/gui/sendsettings/ChangeSendSettingsDialog.java


1   /*
2    * ChangeSendSettingsDialog.java
3    *
4    * Copyright $Date: 2003/08/19 04:06:47 $ FLEXSTOR.net Inc.
5    *
6    * This work is licensed for use and distribution under license terms found at
7    * http://www.flexstor.org/license.html
8    *
9    */
10  
11  package com.flexstor.common.gui.sendsettings;
12  
13  import java.awt.BorderLayout;
14  import java.awt.Checkbox;
15  import java.awt.Component;
16  import java.awt.Cursor;
17  import java.awt.Dimension;
18  import java.awt.Frame;
19  import java.awt.GridBagConstraints;
20  import java.awt.GridBagLayout;
21  import java.awt.Insets;
22  import java.awt.Label;
23  import java.awt.event.ActionEvent;
24  import java.awt.event.ActionListener;
25  import java.awt.event.TextEvent;
26  import java.awt.event.TextListener;
27  import java.util.ArrayList;
28  import java.util.Hashtable;
29  import java.util.Vector;
30  
31  import com.flexstor.common.awt.AWTUtil;
32  import com.flexstor.common.awt.FlexBorderPanel;
33  import com.flexstor.common.awt.FlexTabPanel;
34  import com.flexstor.common.awt.WindowId;
35  import com.flexstor.common.awt.dialogs.InputDialog;
36  import com.flexstor.common.awt.dialogs.MessageBox;
37  import com.flexstor.common.awt.dialogs.OptionDlg;
38  import com.flexstor.common.awt.event.FlexTextEvent;
39  import com.flexstor.common.awt.event.FlexTextListener;
40  import com.flexstor.common.awt.event.SettingsEvent;
41  import com.flexstor.common.awt.event.SettingsEventI;
42  import com.flexstor.common.awt.event.SettingsEventListener;
43  import com.flexstor.common.awt.field.ActionButton;
44  import com.flexstor.common.awt.field.FlexChoice;
45  import com.flexstor.common.awt.field.FlexTextField;
46  import com.flexstor.common.data.ejb.address.EmailAddressData;
47  import com.flexstor.common.data.ejb.address.FtpAddressData;
48  import com.flexstor.common.data.ejb.setting.SendSettingData;
49  import com.flexstor.common.exceptions.ejb.DuplicateRecordException;
50  import com.flexstor.common.gateway.exceptions.TransactionFailedException;
51  import com.flexstor.common.gui.addressbook.AddressBookModel;
52  import com.flexstor.common.gui.addressbook.AddressBookModelI;
53  import com.flexstor.common.gui.addressbook.AddressBookViewer;
54  import com.flexstor.common.gui.addressbook.SelectAddressDialog;
55  import com.flexstor.common.resources.Resources;
56  import com.flexstor.common.settings.SendSettingsModel;
57  import com.flexstor.common.settings.SettingsModelI;
58  import com.flexstor.common.settings.UserManager;
59  import com.flexstor.common.threadmgr.ThreadConsumerI;
60  import com.flexstor.common.threadmgr.ThreadManager;
61  import com.flexstor.common.util.Diagnostic;
62  import com.flexstor.common.util.FlexVector;
63  //import com.flexstor.common.util.ServerList;
64  
65  /**
66    * Main Dialog That allows user to Modify/Add/Delete Send Settings 
67   */
68  public final class ChangeSendSettingsDialog extends OptionDlg
69     implements TextListener,ActionListener,SettingsEventListener,FlexTextListener,ThreadConsumerI
70  
71  {
72     // constants indicate the position of tabs (zero-based)
73     public static final int TAB_GENERAL    = 0;
74     public static final int TAB_FTP        = 1;
75     public static final int TAB_EMAIL      = 2;
76     public static final int TAB_OTHER      = 3;
77     public static final int TAB_CONVERSION = 4;
78  
79     // current tab in dialog
80     private int currentTab = -1;
81     
82     // name for default seftting
83     private static String DEFAULT_NAME = Resources.get(5242);
84  
85     // name for temporary/ad hoc setting
86     private static String ADHOC_NAME   = Resources.get(5243);
87     
88     // Action id's for various buttons
89     private static final int ACTION_SAVE   = ACTION_CUSTOM + 1;
90     private static final int ACTION_SAVEAS = ACTION_CUSTOM + 2;
91     private static final int ACTION_DELETE = ACTION_CUSTOM + 3;
92     
93     // member variables
94     protected Dimension dimMinSize = new Dimension(380, 420);
95     // tab panel for different settings
96     protected FlexTabPanel                         pnlTabs       = new FlexTabPanel();
97     protected SendSettingsGeneralPanel         pnlGeneral    = new SendSettingsGeneralPanel(this);;
98     protected SendSettingsFtpPanel             pnlFtp        = new SendSettingsFtpPanel(this);
99     protected SendSettingsEmailPanel           pnlEMail      = new SendSettingsEmailPanel(this,false);
100    protected SendSettingsEmailPanel           pnlManual     = new SendSettingsEmailPanel(this,true);
101    protected SendSettingsConversionPanel      pnlConversion = new SendSettingsConversionPanel(this);
102 
103    protected FlexBorderPanel        pnlSetting    = new FlexBorderPanel();
104    protected FlexChoice             chSetting     = new FlexChoice();
105    protected Frame                fParent;        // parent frame
106    protected Hashtable            htSettings;     // list of settings
107    protected SendSettingData    currentSetting; // currently selected setting
108    protected SendSettingData    tempSetting = new SendSettingData("temp");    // current GUI contents
109    protected boolean              bChangeInProgress = false;
110    protected boolean              bSystemSettingSelected = false; // indicates that default or ad hoc is selected
111    protected String               sDefaultEMailAddress = "";
112    
113    // Model for SendSettings
114    protected SettingsModelI      settingsModel;
115    
116    // Model for Address Book
117    protected AddressBookModelI addBookModel    = null;
118    
119    // Vecot of selected settings 
120    protected Vector selectedSendSettings = null;
121    
122 
123     /**
124     * Creates a new dialog to modify send settings.
125     * @param fParent the parent frame
126     * @param currentSetting the initial setting, maybe null
127     * @param htSettings all available settings in a Hashtable
128     * @param settingsModel the instance which manages persistence for send settings
129     * @param selectedSettings all selected settings for current user/group
130     */
131    public ChangeSendSettingsDialog(Frame                  fParent,
132                                  SendSettingData          initialSetting,
133                                  Hashtable                  htSettings ,
134                                  SendSettingsModel        model,
135                                  Vector selectedSendSettings,AddressBookModelI addBookModel ) //PersistCollectionI  addressMgr,
136    {
137       super(fParent, OK_CANCEL_HELP);
138       this.fParent        = fParent;
139       this.currentSetting = initialSetting;
140       this.addBookModel = addBookModel;
141       this.settingsModel = model;
142       //if( htSettings != null && htSettings.size() == 0)
143       if( htSettings != null )
144         this.htSettings = htSettings;
145       else 
146         htSettings = populateSettings();
147       this.selectedSendSettings = selectedSendSettings;
148      
149       init();
150    }
151 
152    /**
153     * Creates a new dialog to modify send settings.
154     * @param fParent the parent frame
155     * @param currentSetting the initial setting, maybe null
156     * @param htSettings all available settings in a Hashtable
157     * @param model the instance which manages persistence for send settings
158     */
159    public ChangeSendSettingsDialog(Frame                 fParent,
160                                  SendSettingData     initialSetting,
161                                  Hashtable             htSettings,
162                                  SettingsModelI model,AddressBookModelI addBookModel )
163                                 
164    {
165       super(fParent, OK_CANCEL_HELP);
166       this.fParent        = fParent;
167       this.currentSetting = initialSetting;
168       this.addBookModel = addBookModel;
169       this.settingsModel = model;
170       //System.out.println("DBG:ChangeSettingsDialog:init:Settings "+htSettings);
171       if( htSettings != null )
172         this.htSettings = htSettings;
173       else 
174         htSettings = populateSettings();
175       init();
176    }
177 
178    /**
179     * Initialize the dialog
180     */
181    private void init()
182    {
183 
184       GridBagLayout      gbl = null;
185       GridBagConstraints gbc = new GridBagConstraints();
186 
187       // top panel with setting choice
188       gbl = new GridBagLayout();
189       pnlSetting.setLayout(gbl);
190       add(BorderLayout.NORTH, pnlSetting);
191       //try 
192       //{
193          pnlSetting.setBevelStyle(FlexBorderPanel.BEVEL_RAISED);
194          pnlSetting.setPaddingBottom(2);
195          pnlSetting.setPaddingTop(2);
196       //}
197       //catch(java.beans.PropertyVetoException e){}
198 
199       Label lblName = new Label(Resources.get(5272));
200       gbc = AWTUtil.getGbc(0,0,0,0,GridBagConstraints.CENTER,GridBagConstraints.BOTH);
201       gbc.insets = new Insets(4,0,0,4);
202       ((GridBagLayout)pnlSetting.getLayout()).setConstraints(lblName, gbc);
203       pnlSetting.add(lblName);
204 
205       gbc = AWTUtil.getGbc(1,0,1,1,GridBagConstraints.WEST,GridBagConstraints.BOTH);
206 
207       gbc.insets = new Insets(4,0,0,10);
208       ((GridBagLayout)pnlSetting.getLayout()).setConstraints(chSetting, gbc);
209       pnlSetting.add(chSetting);
210 
211       add(BorderLayout.CENTER, pnlTabs);
212 //  TODO:3PG fix or remove 
213 /*
214       pnlTabs.addTabPanel(Resources.get(5273), true, pnlGeneral, TAB_GENERAL);
215 
216       pnlTabs.addTabPanel(Resources.get(5274), false, pnlFtp, TAB_FTP);
217 
218       pnlTabs.addTabPanel(Resources.get(5275), false, pnlEMail, TAB_EMAIL);
219 
220       pnlTabs.addTabPanel(Resources.get(5276), false, pnlManual, TAB_OTHER);
221 
222       pnlTabs.addTabPanel(Resources.get(5277), true, pnlConversion, TAB_CONVERSION);
223 
224       try
225       {
226          pnlTabs.setCurrentPanelNdx(0);
227       }
228       catch(java.beans.PropertyVetoException e) { }
229       */
230       //add buttons to the button panel
231       addButtons(ACTION_SAVE,Resources.get(5295),0);
232       addButtons(ACTION_SAVEAS,Resources.get(5296),1);
233       addButtons(ACTION_DELETE,Resources.get(5209),2);
234       centerOn(fParent);
235       chSetting.addTextListener(this);
236       pack();
237    }
238    
239    /**
240      * Utility method to add Action Buttons
241     */ 
242    private void addButtons(int id,String label,int position)
243    {
244      ActionButton button  = new ActionButton(id);
245      button.setLabel(label);
246      vButtons.insertElementAt(button, position);
247    }
248 
249    // returns the window id for help
250    protected int getWindowId()
251    {
252       return WindowId.WNDID_SEND_CHG_SET;
253    }
254 
255    /**
256      * Method called after dialog created
257     */ 
258    public void addNotify()
259    {
260       super.addNotify();
261       setSize(dimMinSize);
262       // set defaults, after addNotify, otherwise property
263       // change events do not happen (Symantec)
264       if(htSettings == null)
265       {
266         htSettings = populateSettings();  
267       }
268       for(java.util.Enumeration e = htSettings.keys();e.hasMoreElements();)
269       {
270          chSetting.addItem((String)e.nextElement());
271          chSetting.validate();
272       }
273 
274       // add default setting
275       SendSettingData defaultSetting = new SendSettingData(DEFAULT_NAME);
276       defaultSetting.setSendType(SendSettingData.FTP);
277       htSettings.put(DEFAULT_NAME, defaultSetting);
278       chSetting.insert(DEFAULT_NAME, 0);
279 
280       // If no setting is preferred make default the selected setting
281       if (currentSetting == null)
282       {
283         currentSetting = (SendSettingData)htSettings.get(DEFAULT_NAME);
284       }
285 
286       // Remember Always to set the MailFrom for Email Panel
287       pnlEMail.setMailFrom(sDefaultEMailAddress);
288 
289       // Select to current setting 
290       selectSetting(currentSetting);
291       populateGUI(currentSetting);
292       bChangeInProgress = false;
293       configureButtons();
294       centerOn(null);
295    }
296 
297   /**
298    * Implementation for ActionListener.
299    * @param java.awt.event.ActionEvent
300    */
301    public void actionPerformed(java.awt.event.ActionEvent event)
302    {
303       ThreadManager.requestService(this,event);
304    }
305    
306   /**
307    * Implementation of ThreadConsumerI.To run all the database intesive methods 
308    * in different thread.
309    * 
310    * @param Object
311    */
312    public void processService(Object obj)
313    {
314       ActionEvent event = null;
315       
316       if(obj instanceof ActionEvent)
317         event = ( ActionEvent )obj;
318       else return;
319       
320       enableInput(false);
321       setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
322       
323       int actionId = Integer.parseInt(event.getActionCommand());
324  
325       if (actionId == ACTION_CANCEL)
326       {
327          setStatus(OptionDlg.ACTION_CANCEL);
328          if (bChangeInProgress)
329          {
330             if (!bSystemSettingSelected)
331             {
332               if (!confirmDiscard())
333               {
334                  enableInput(true);
335                  configureButtons();
336                  if(saveSetting())
337                     super.actionPerformed(event);
338                  else return;  
339               }
340               else // System setting selected
341               {
342                  enableInput(true);
343                  configureButtons();
344                  super.actionPerformed(event);
345               }
346             }  
347          }
348          super.actionPerformed(event);
349       }
350       else if (actionId == ACTION_OK)
351       {
352          setStatus(OptionDlg.ACTION_OK);
353 
354          // If system setting is not selected (not default, not ad hoc)
355          if (!bSystemSettingSelected )
356          {
357             if (bChangeInProgress)
358             {
359                // setting has changed, discard or cancel?
360                nStatus = MessageBox.showMessageDialog(fParent, 5297, MessageBox.OK_CANCEL, currentSetting.getName());
361                if (nStatus == ACTION_OK)
362                {
363                   // do not save changes, but use as ad hoc setting
364                   if ( !makeAdhocSetting() )
365                   {
366                     enableInput(true);
367                     configureButtons();
368                     return;
369                   }
370                   else // Return to previous state // cancel
371                   {
372                     bChangeInProgress = false;
373                   }
374                }
375                else
376                {
377                   // cancel
378                   enableInput(true);
379                   configureButtons();
380                   return;
381                }
382             }
383          }
384          else  // (default or ad_hoc)
385          {
386             // if default, make it an ad hoc setting
387             if (currentSetting.getName().equals(DEFAULT_NAME))
388             {
389                if( bChangeInProgress )
390                {
391                  nStatus = MessageBox.showMessageDialog(fParent, 5297, MessageBox.OK_CANCEL, currentSetting.getName());
392                  if (nStatus == ACTION_OK)
393                  {
394                    if ( !makeAdhocSetting() )
395                    {  
396                      enableInput(true);
397                      configureButtons();
398                      return;
399                    }
400                  }
401                  else // Cancel
402                  {
403                    enableInput(true);
404                    configureButtons();
405                    return;
406                  }
407                }//if change
408                else //if not changed
409                {
410                  enableInput(true);
411                  configureButtons();
412                }
413             }
414             else  //(Ad_hoc)
415             {
416               // just refresh the temporary data
417               getDataFromGUI(currentSetting);
418             }
419          }
420          super.actionPerformed(event);
421       }
422       else if (actionId == ACTION_SAVE) // Save Setting
423       {
424          bChangeInProgress = !saveSetting();
425       }
426       else if (actionId == ACTION_SAVEAS) // SaveAs Setting
427       {
428          boolean result = saveAsNewSetting();
429 
430          if (bChangeInProgress == true)
431               bChangeInProgress = !result;
432 
433       }
434       else if (actionId == ACTION_DELETE) // Delete Setting
435       {
436          // never delete default setting!
437          if (!bSystemSettingSelected)
438          {
439             // Delete? Yes/No
440             nStatus = MessageBox.showMessageDialog(fParent, 5298, MessageBox.YES_NO, currentSetting.getName());
441             if (nStatus == ACTION_YES)
442             {
443                try
444                {
445                   settingsModel.removeSetting( currentSetting );
446                   htSettings.remove(currentSetting.getName());
447                   chSetting.remove(currentSetting.getName());
448                   SendSettingData setting = (SendSettingData)htSettings.get(chSetting.getItem(0));
449                   selectSetting(setting);
450 //        TODO:3PG fix or remove 
451 /*
452                   try
453                   {
454                     pnlTabs.setCurrentPanelNdx(0);
455                   }
456                   catch(java.beans.PropertyVetoException e) { }
457  */
458                   bSystemSettingSelected = true;
459                   bChangeInProgress = false;
460                }
461                catch(Exception e)//TransactionFailedException
462                {
463                   MessageBox.showMessageDialog(fParent, 5299, MessageBox.OK, currentSetting.getName());
464                }
465             }  // nStatus == 1
466          }  // system setting?
467       }  // pbDelete
468       else if( actionId == SendSettingsTab.ACTION_EMAIL_TO ) // EmailTo button in EmailTab
469       {
470         if( currentTab == ChangeSendSettingsDialog.TAB_EMAIL )
471         {
472             String address = selectEMailAddress( pnlEMail.getMailTo() );
473             pnlEMail.setMailTo(address);
474         }
475         else if( currentTab == ChangeSendSettingsDialog.TAB_OTHER )
476         {
477             String address = selectEMailAddress( pnlManual.getMailTo() );
478             pnlManual.setMailTo(address);
479         }
480       }
481       else if( actionId == SendSettingsTab.ACTION_EMAIL_CC ) // EmailCc button in EmailTab
482       {
483         if( currentTab == ChangeSendSettingsDialog.TAB_EMAIL )
484         {
485            String address = selectEMailAddress( pnlEMail.getCc() );
486            pnlEMail.setCc(address);
487         }
488         else if( currentTab == ChangeSendSettingsDialog.TAB_OTHER )
489         {
490             String address = selectEMailAddress( pnlManual.getCc() );
491             pnlManual.setCc(address);
492             
493         }
494       }
495       else if( actionId == SendSettingsTab.ACTION_FTP ) // Ftp button in EmailTab
496       {
497         selectFtpAddress();
498       }
499       else if( actionId == SendSettingsTab.ACTION_BROWSE )
500       {
501         /*
502          try
503          {
504            BrowseResult result = ( new BrowseController( null,
505                                                              Resources.get( 6898 ),
506                                                              BrowseController.ACCESS_REMOTE,
507                                                              ServerList.getDNSServerList(),
508                                                              new BrowseFilter( BrowseFilter.DIRECTORY ),
509                                                              pnlManual.getDestination(),
510                                                              true) ).getResult();
511             if ( result != null )
512             {
513                pnlManual.setDestination(result.getPath());
514                bChangeInProgress = true;
515 
516                if ( result.isSetAsDefault() )
517                   Settings.addString( Settings.DEFAULT_SEND_OTHER_LOCATION, result.getPath() );
518             }//if
519          }//try
520          catch( BrowseAccessorException ex )
521          {
522           // Unable to browse check out location. %%1
523            MessageBox.showMessageDialog( fParent, 6897, MessageBox.OK, ex.getMessage() );
524          }
525          */
526       }//if
527       else
528       {
529          super.actionPerformed(event);
530       }
531       enableInput(true);
532       configureButtons();
533    } // End of ProcessService()
534 
535   /**
536    * Show email address selection list, which allows user to select email addresses.
537    * @param String address to which other addresses are to be added.
538    */
539    private String selectEMailAddress(String address)
540    {
541        //currently multiple mode is turned off. It is to be turn on.
542        SelectAddressDialog dlg = new SelectAddressDialog(fParent, addBookModel,AddressBookViewer.EMAIL, true);
543        //dlg.setLocation(txtTarget.getLocationOnScreen().x,txtTarget.getLocationOnScreen().y + txtTarget.getSize().height);
544        dlg.setVisible(true);
545        if (dlg.getStatus() == SelectAddressDialog.ACTION_OK)
546        {
547          StringBuffer sb = new StringBuffer( address );
548          FlexVector sel = dlg.getSelectedAddresses();
549          int i = 0;
550          for (i = 0; i < sel.size();i++)
551          {
552             if (sb.length() != 0) sb.append(", ");
553               sb.append(((EmailAddressData)sel.elementAt(i)).getAddress());
554          }
555          if( i > 0 )
556             bChangeInProgress = true;
557          return sb.toString();
558          //return ((EmailAddressData)sel.elementAt(0)).getAddress();
559        }
560        return address;
561    }
562 
563   /**
564    * Show ftp address selection list,which allows user to select a ftp address.
565    * 
566    */
567    private void selectFtpAddress()
568    {
569        SelectAddressDialog dlg = new SelectAddressDialog(fParent, addBookModel,AddressBookViewer.FTP, true);
570        
571        dlg.setVisible(true);
572        if (dlg.getStatus() == SelectAddressDialog.ACTION_OK)
573        {
574          bChangeInProgress = true;
575          pnlFtp.setAddress( (FtpAddressData)dlg.getSelectedAddresses().elementAt(0));
576        }
577    }
578 
579   /**
580    * Implementation for FlexTextListener.
581    */
582    public void textValueChangeBegin ( FlexTextEvent e )
583    {
584    }
585 
586   /**
587    * Implementation for FlexTextListener.This method will be called on selecting new setting
588    * on settings combobox, on entering any text on text fields email/ftp tabs.
589    * @param FlexTextEvent
590    */
591    public void textValueChangeEnd ( FlexTextEvent e )
592    {
593       Object object = e.getSource();
594       //is event comming from "Seting Name" ComboBox
595       if (object == chSetting)
596       {
597          if(chSetting.getSelectedItem().equalsIgnoreCase(currentSetting.getName()))
598            return ;
599 
600          if (!bSystemSettingSelected)
601          {
602            //is Setting Changed?
603            if ( bChangeInProgress )
604            {
605               if (!confirmDiscard())
606               {
607                  boolean result = saveSetting();
608                  if (!result)
609                  {
610                    if (bChangeInProgress == true)
611                      bChangeInProgress = !result;
612 
613                    // re-select choice to previous item
614                    chSetting.select(currentSetting.getName());
615                    return ;
616                  }
617 
618               }
619            }
620          }  
621          // update variables to new setting
622          SendSettingData setting = (SendSettingData)htSettings.get(chSetting.getSelectedItem());
623          selectSetting(setting);
624 
625             //Enable the tab depending upon send type of current selected setting
626          if( currentSetting.getSendType() == SendSettingData.FTP)
627          {
628             configureTab(true,false,false);
629          }
630          else if( currentSetting.getSendType() == SendSettingData.EMAIL)
631          {
632             configureTab(false,true,false);
633          }
634          else if( currentSetting.getSendType() == SendSettingData.MANUAL)
635          {
636             configureTab(false,false,true);
637          }
638          try
639          {
640             //enable the corresponding radio button on Send Via panel
641             pnlGeneral.setSendType(currentSetting.getSendType());
642             bChangeInProgress = false;
643          }
644          catch(java.beans.PropertyVetoException ex){}
645 
646       }
647       else if(object instanceof FlexTextField ) // Any text field for present
648       {
649         bChangeInProgress = true;
650       }
651       else if (object instanceof FlexChoice) // Conversion choice
652       {
653         bChangeInProgress = true;
654       }
655       configureButtons();
656    }
657 
658   /**
659    * Symantec's TabPanel returns a fixed size, so we must overwrite
660    * this method.
661    */
662    public Dimension getPreferredSize()
663    {
664       return dimMinSize;
665    }
666 
667    /**
668     * Sets a new preferred size.
669     * @param a dimension describing the new preferred size
670     */
671    public void setPreferredSize(Dimension dim)
672    {
673       this.dimMinSize = dim;
674    }
675 
676    public Dimension getMinimumSize()
677    {
678       return getPreferredSize();
679    }
680    
681    /**
682     * This will set the proper tab selected.
683     * If configureTab(true,false,false) is called, then ftp tab will be selected.
684     * @param ftp 
685     * @param email
686     * @param manual
687     * @return void
688    */
689    private void configureTab(boolean ftp,boolean email, boolean manual)
690    {
691      //try
692      //{
693         if(ftp)
694           currentTab = ChangeSendSettingsDialog.TAB_FTP;
695         else if(email)
696           currentTab = ChangeSendSettingsDialog.TAB_EMAIL;
697         else if(manual)
698           currentTab = ChangeSendSettingsDialog.TAB_OTHER;
699 //    TODO:3PG fix or remove 
700 /*
701         pnlTabs.enableTabPanel(ftp, pnlTabs.getPanelTabIndex(pnlFtp));
702         pnlTabs.enableTabPanel(email, pnlTabs.getPanelTabIndex(pnlEMail));
703         pnlTabs.enableTabPanel(manual, pnlTabs.getPanelTabIndex(pnlManual));
704 */
705     // }
706     // catch(java.beans.PropertyVetoException ex){}
707    }
708    
709    /**
710     * Get the current setting selected in dialog
711     * @param void
712     * @return SendSettingData current setting    
713     */
714    public SendSettingData getSetting()
715    {
716       return currentSetting;
717    }
718 
719   /**
720    * Populates GUI from a setting object.
721    */
722    private void populateGUI(SendSettingData setting)
723    {
724      //try
725     // {
726        // if send type changes, need to change the tabs correspondingly
727        // i.e. if ftp tab is selected and new send type is e-mail, switch to general tab
728      //TODO:3PG fix or remove Component pnl = pnlTabs.getTabPanel(pnlTabs.getCurrentPanelNdx());
729         if (setting.getSendType() != pnlGeneral.getSendType())
730         {
731       //TODO:3PG fix or remove if (pnl instanceof SendSettingsFtpPanel || pnl instanceof SendSettingsEmailPanel)
732       //TODO:3PG fix or remove pnlTabs.setCurrentPanelNdx(0);
733         }//if
734         pnlGeneral.populateGUI(setting);
735         pnlConversion.populateGUI(setting);
736 
737         int sendType = setting.getSendType();
738         //populate the gui corresponding to the send type of setting.
739         if(  sendType == SendSettingData.FTP )
740         {
741             pnlEMail.cleanUpFields();
742             pnlManual.cleanUpFields();
743             pnlFtp.populateGUI(setting);
744         }
745         else if( sendType == SendSettingData.EMAIL )
746         {
747             pnlFtp.cleanUpFields();
748             pnlManual.cleanUpFields();
749             pnlEMail.populateGUI(setting);
750         }
751         else if( sendType == SendSettingData.MANUAL )
752         {
753             pnlFtp.cleanUpFields();
754             pnlEMail.cleanUpFields();
755             pnlManual.populateGUI(setting);
756         }
757      //}
758 //     catch(java.beans.PropertyVetoException e)
759 //     {}
760    }
761 
762   /**
763    * Populates a setting object from the GUI.It gets the setting from GUI only for currently
764    * selected send type on Send Via Panel.
765    */
766    private void getDataFromGUI(SendSettingData setting)
767    {
768       //copy data fields from GUI into setting object
769       pnlGeneral.synchronizeData(setting);
770       pnlConversion.synchronizeData(setting);
771 
772       int sendType = pnlGeneral.getSendType();
773       if(  sendType == SendSettingData.FTP)
774          pnlFtp.synchronizeData(setting);
775       else if( sendType == SendSettingData.EMAIL)
776          pnlEMail.synchronizeData(setting);
777       else if( sendType == SendSettingData.MANUAL)
778          pnlManual.synchronizeData(setting);
779    }
780 
781 
782   /**
783    * Enables/disables buttons dependant on the current state of the dialog.
784    */
785    private void configureButtons()
786    {
787       //getComponentForId(ACTION_OK).setEnabled( bChangeInProgress );//canPerformDefaultAction());
788       getComponentForId(ACTION_SAVE).setEnabled(bChangeInProgress && !bSystemSettingSelected);
789       getComponentForId(ACTION_DELETE).setEnabled(!bSystemSettingSelected);
790    }
791 
792    /**
793    */ 
794    protected boolean canPerformDefaultAction()
795    {
796       if (tempSetting.getSendType() == SendSettingData.EMAIL
797       &&  getDefaultEMailAddress().trim().length() == 0)
798          return false;
799       else
800          return tempSetting.isValid();
801    }
802 
803    /**
804     * Asks the user if pending changes for a setting are to be discarded.
805     * If so, changes will be reflected in the temporay setting which will
806     * be selected automatically.
807     * Methods gets called when Cancel was pressed or the setting choice was changed.
808     * @return true if ok has been chosen, false if process is cancelled.
809     */
810    private boolean confirmDiscard()
811    {
812       boolean bResult = false;
813 
814       // setting has changed, discard or cancel?
815       if (ACTION_YES == MessageBox.showMessageDialog(fParent, 7022, MessageBox.YES_NO, currentSetting.getName()))
816       {
817          bResult = false;
818       }
819       else
820          bResult = true;
821 
822       return bResult;
823    }
824 
825 
826    //this will handle the text event generated from the text fields on viewer.
827    public void textValueChanged ( TextEvent e )
828    {
829       bChangeInProgress = true;
830       configureButtons();
831    }
832    
833    //implementation for SettingsEventListener
834    public void eventOccured(SettingsEvent e)
835    {
836      try
837      {
838       if(e.getEventType() == SettingsEvent.SEND_SETTINGS_EVENT )
839       {
840          Component c = e.getSourceComponent();
841          if( c instanceof Checkbox)
842          {
843            Checkbox b = (Checkbox)c;
844            //Check the radio button selected and set the corresponding tab enable
845            if(b.getLabel().equals(Resources.get(5261)))  //email radio button
846            {
847               bChangeInProgress = true;
848               configureTab(false,true,false);
849            }
850            else if( b.getLabel().equals(Resources.get(5260)) ) //ftp radio button
851            {
852               bChangeInProgress = true;
853               configureTab(true,false,false);
854            }
855            else if( b.getLabel().equals(Resources.get(5262)) )//manual radio button
856            {  bChangeInProgress = true;
857               configureTab(false,false,true);
858            }
859            //if state is changed then mark as change in progress.
860            if ( e.getEventId() == SettingsEventI.COMPONENT_STATE_CHANGED )
861            {
862               bChangeInProgress = true;
863               configureButtons();
864            }
865            configureButtons();
866 
867          }//if( c instanceof Checkbox)
868       }//if(e.getEventType() == SettingsEvent.SEND_SETTINGS_EVENT )
869      }
870      catch(Exception ex) {}
871    }//eventOccured
872 
873 
874 
875    /**
876     * Save the current setting selected in dialog
877     * @param void 
878     * @return boolean setting was saved successfully    
879     */
880    private boolean saveSetting()
881    {
882       //Validate the entries
883       if( !verify() )
884         return false;
885       // verify that it is not the default setting
886       if ( bSystemSettingSelected )
887       {
888          return saveAsNewSetting();
889       }
890       else
891       {
892          getDataFromGUI(currentSetting);
893          try
894          {
895             settingsModel.addSetting(currentSetting);
896             populateGUI(currentSetting);
897          }
898          catch(DuplicateRecordException x)
899          {
900             // should not happen because this is an update
901          }
902          catch(TransactionFailedException e)
903          {
904             // error from gateway
905             MessageBox.showMessageDialog(fParent, 5301, MessageBox.OK, currentSetting.getName());
906             return false;
907          }
908       }
909       return true;
910    }
911 
912 
913    /**
914     * SaveAs the current setting selected in dialog
915     * @param void
916     * @return boolean setting was savedAs successfully
917     */
918    private boolean saveAsNewSetting()
919    {
920       //Validate the entries
921       if( !verify() )
922         return false;
923 
924       String sNewName = null;
925       SendSettingData newSetting;
926 
927       //show dialog to query new name, ensure it is unique
928       InputDialog dlg = new InputDialog(fParent, Resources.get(5279), 30 );
929       dlg.setVisible(true);
930       if (dlg.getStatus() == ACTION_OK)
931       {  System.out.println(" Action OK ");
932          sNewName = dlg.getText().trim();
933          // no entry --> return
934          if (sNewName.length() == 0)
935            return false;
936 
937          if (sNewName.equalsIgnoreCase(DEFAULT_NAME) || sNewName.equalsIgnoreCase(ADHOC_NAME))
938          {
939            // Cannot use System Setting name " %%1 " for creating new setting.
940            MessageBox.showMessageDialog(fParent,7013,MessageBox.OK,sNewName);
941            if (!saveAsNewSetting()) return false;
942          }
943 
944 
945          // check for unique name
946          newSetting = (SendSettingData)htSettings.get(sNewName);
947          if (newSetting == null)
948          {
949             // newSetting does not exist, save new record
950             newSetting = new SendSettingData(sNewName);
951             getDataFromGUI(newSetting);
952 
953             try
954             {
955                settingsModel.addSetting(newSetting);
956                htSettings.put(sNewName, newSetting);
957                chSetting.add(sNewName);
958                selectSetting(newSetting);
959                bChangeInProgress = false;
960             }
961             catch(TransactionFailedException e)
962             {
963                // error saving to gateway
964                MessageBox.showMessageDialog(fParent, 5302, MessageBox.OK, newSetting.getName());
965                return false;
966             }
967             catch(DuplicateRecordException x)
968             {
969                // should not happen because duplicate name is checked above, and this is a new setting
970             }
971          }
972          else
973          {
974             // setting name not unique, overwrite setting?
975             nStatus = MessageBox.showMessageDialog(fParent, 5303, MessageBox.YES_NO, sNewName);
976             if (nStatus == ACTION_YES)
977             {
978                try
979                {
980                   getDataFromGUI(newSetting);
981                   settingsModel.updateSetting(newSetting);
982                   selectSetting(newSetting);
983                }
984                catch(DuplicateRecordException x)
985                {
986                   // should not happen because this is an update
987                }
988                catch(TransactionFailedException e)
989                {
990                   // error from gateway
991                   MessageBox.showMessageDialog(fParent, 5301, MessageBox.OK, newSetting.getName());
992                   return false;
993                }
994             }
995             else
996             {
997                //return false;
998                if (!saveAsNewSetting()) return false;
999             }
1000         }
1001      }
1002      else
1003      { System.out.println(" In cancel ");
1004        return false;
1005      }
1006      return false;
1007   } // end saveAsSetting
1008
1009  /**
1010   * Synchronizes choice, currentSetting and state variables updates GUI
1011   * @param  setting setting to be selected
1012   * @return void 
1013   */ 
1014   private void selectSetting(SendSettingData setting)
1015   {
1016      setTitle(Resources.get(5278, setting.getName()));
1017      chSetting.select(setting.getName());
1018      currentSetting = setting;
1019      bSystemSettingSelected = (setting.getName().equals(DEFAULT_NAME) || setting.getName().equals(ADHOC_NAME));
1020      populateGUI(currentSetting);
1021   }
1022   
1023   /**
1024    * Makes a Temporary Send Setting for user with Adhoc name
1025    * @param  void
1026    * @return boolean Temporary setting created successfully
1027    */
1028   private boolean makeAdhocSetting()
1029   {
1030      // verify for validity
1031      if( !verify() )
1032      {
1033        return false;
1034      }
1035      currentSetting = (SendSettingData)htSettings.get(ADHOC_NAME);
1036      if (currentSetting == null)
1037      {
1038         currentSetting = new SendSettingData(ADHOC_NAME);
1039         htSettings.put(ADHOC_NAME, currentSetting);
1040         chSetting.add(ADHOC_NAME);
1041         getDataFromGUI(currentSetting);
1042         selectSetting(currentSetting);
1043         populateGUI(currentSetting);
1044      }
1045      return true;
1046   }
1047   
1048   /**
1049    * Checks for validation in children tabs
1050    * @param  void
1051    * @return boolean 
1052    */
1053   public boolean verify()
1054   {
1055      int sendType = pnlGeneral.getSendType();
1056      if(sendType == SendSettingData.FTP)
1057        return pnlFtp.verify();
1058      else if(sendType == SendSettingData.EMAIL)
1059        return pnlEMail.verify();
1060      else if(sendType == SendSettingData.MANUAL)
1061        return pnlManual.verify();
1062      else return false;
1063   }
1064
1065   public void dispose()
1066   {
1067      htSettings.remove(DEFAULT_NAME);
1068      super.dispose();
1069   }
1070
1071   /**
1072    * Sets the default e-mail address for the e-mail tab,
1073    * required for e-mail send setting to be enabled.
1074    * @param sAddress the e-mail address
1075    * @return void 
1076    */
1077   public void setDefaultEMailAddress(String sAddress)
1078   {
1079      this.sDefaultEMailAddress = sAddress;
1080      pnlEMail.setMailFrom(sDefaultEMailAddress);
1081   }
1082
1083   /**
1084    * Sets the default e-mail address for the e-mail tab,
1085    * required for e-mail send setting to be enabled.
1086    * @param void
1087    * @return address
1088    */
1089   private String getDefaultEMailAddress()
1090   {
1091      return sDefaultEMailAddress;
1092   }
1093  
1094   private Hashtable populateSettings()
1095   {
1096     Hashtable htSettings = new Hashtable();
1097     try
1098     {
1099       ArrayList list = settingsModel.getSettings();
1100       java.util.Iterator iter = list.iterator();
1101       while(iter.hasNext())
1102       {
1103        SendSettingData newSetting = (SendSettingData)iter.next();
1104        htSettings.put(newSetting.getName(), newSetting);
1105       } 
1106       return htSettings;
1107     }
1108     catch(TransactionFailedException ex )
1109     {
1110        ex.printStackTrace();
1111        return null;
1112     }
1113   }
1114  
1115  
1116  
1117  /* public static  void main(String args[])
1118   {
1119    ///////////////////////////////////////////////////
1120     try
1121      {
1122
1123      java.util.Hashtable htSettings = new java.util.Hashtable();
1124
1125      com.flexstor.common.settings.Settings.registerDataSource ( com.flexstor.common.settings.Settings.RUNTIME, new com.flexstor.common.settings.datasource.RuntimeDataSource() );
1126      com.flexstor.common.settings.Settings.registerDataSource ( com.flexstor.common.settings.Settings.USER, new com.flexstor.common.settings.datasource.UserDataSource(new java.util.Hashtable()));
1127      try
1128      {
1129         Resources.registerDataSource( new FileDataSource( args[0]));//"E:\\MRroke\\FromRorke\\Aug16Build48\\build48a.tar\\resources\\Resources.en" ) );
1130         com.flexstor.common.resources.Resources.cacheAll();
1131         // Setup diagnostics
1132         Diagnostic.enableOutput ( true );
1133         Diagnostic.setTraceLevel ( 3 );
1134         Diagnostic.addTraceCategory ( Diagnostic.CAT_GATEWAY );
1135         Diagnostic.addTraceCategory ( Diagnostic.SETTINGS );
1136         Diagnostic.addOutputDevice ( System.out );
1137
1138         com.flexstor.common.gateway.InitGateway igw = new com.flexstor.common.gateway.InitGateway ( "JONAS",args[1], args[2],true );
1139         // initializing user
1140         com.flexstor.common.settings.UserManager um = com.flexstor.common.settings.UserManager.getInstance();
1141         um.init ( "demo1" );
1142         SendSettingsModel model = new SendSettingsModel();
1143         model.init("demo1",true);
1144         Vector v =new Vector(); 
1145         ArrayList l = model.getSettings() ;
1146         for(int i = 0; i<l.size() ; i++)
1147           v.addElement(l.get(i));
1148         
1149         Frame f = new Frame();
1150         ChangeSendSettingsDialog p = new ChangeSendSettingsDialog(f,null,new Hashtable(),model,v);
1151         p.show();
1152         System.out.println(f.getSize());
1153         System.out.println(" Created frame ");
1154         
1155      }
1156      catch ( Exception e ){ e.printStackTrace();}
1157      try{System.in.read();}catch(Exception e){}
1158      }catch(Exception e ){ e.printStackTrace();}
1159      //System.exit(0);
1160      ///////////////////////////////////////////////////////////
1161
1162  }*/
1163
1164
1165// stub for standalone testing
1166   public static void main(String[] args) throws Exception
1167   {
1168      java.util.Hashtable htSettings = new Hashtable();
1169      SendSettingsModel model = null;
1170      AddressBookModel addBookModel  =null ;
1171      Vector v = null;
1172
1173      com.flexstor.common.settings.Settings.registerDataSource ( com.flexstor.common.settings.Settings.RUNTIME, new com.flexstor.common.settings.datasource.RuntimeDataSource() );
1174      com.flexstor.common.settings.Settings.registerDataSource ( com.flexstor.common.settings.Settings.USER, new com.flexstor.common.settings.datasource.UserDataSource(new java.util.Hashtable()));
1175      try
1176      {
1177         //java.util.Hashtable hash = new com.flexstor.common.settings.datasource.ConfigSettingsLoader().loadSettings ( args[0] + ServicesI.CONFIG_DIR + '/' + ServicesI.PROPERTIES_FILE );
1178         com.flexstor.common.settings.Settings.registerDataSource ( com.flexstor.common.settings.Settings.CONFIG, new com.flexstor.common.settings.datasource.ConfigDataSource(new Hashtable()) );
1179         //com.flexstor.common.settings.Settings.addString ( com.flexstor.common.settings.Settings.CONFIG_DIRECTORY,  args[0] + ServicesI.CONFIG_DIR + '/' );
1180
1181
1182         com.flexstor.common.resources.Resources.registerDataSource( new com.flexstor.common.resources.FileDataSource( args[0]) );//+ "resources/Resources.en" ) );
1183         com.flexstor.common.resources.Resources.cacheAll();
1184         // Setup diagnostics
1185         Diagnostic.enableOutput ( true );
1186         Diagnostic.setTraceLevel ( 3 );
1187         Diagnostic.addTraceCategory ( Diagnostic.CAT_GATEWAY );
1188         Diagnostic.addTraceCategory ( Diagnostic.SETTINGS );
1189         Diagnostic.addOutputDevice ( System.out );
1190                                                            //save //load
1191         //Gateway.enableDebugging ( "D:/flexstordb/30/cso", false, true);
1192
1193         new com.flexstor.common.gateway.InitGateway ( "JONAS",args[1], args[2],true );
1194         // initializing user
1195         UserManager.init ( "demo1" );
1196         java.util.ArrayList list = com.flexstor.common.settings.SendSettingsManager.create(com.flexstor.common.settings.SendSettingsManager.USER).getItems();
1197         java.util.Iterator iter = list.iterator();
1198         while(iter.hasNext())
1199         {
1200            SendSettingData newSetting = (SendSettingData)iter.next();
1201            htSettings.put(newSetting.getName(), newSetting);
1202         }
1203
1204         //////
1205         model = new SendSettingsModel();
1206         model.init("demo1",true);
1207         v =new Vector(); 
1208         list = model.getSettings() ;
1209         iter = list.iterator();
1210         while(iter.hasNext())
1211         {
1212            SendSettingData newSetting = (SendSettingData)iter.next();
1213            //htSettings.put(newSetting.getName(), newSetting);
1214            v.addElement(newSetting);
1215         }
1216         addBookModel = new AddressBookModel("demo1",true);
1217         //for(int i = 0; i<l.size() ; i++)
1218           //v.addElement(l.get(i));
1219         
1220         //Frame f = new Frame();
1221         //ChangeSendSettingsDialog p = new ChangeSendSettingsDialog(f,null,new Hashtable(),model,v);
1222         //p.show();
1223         ///////
1224         //java.util.ArrayList list = com.flexstor.common.settings.SendSettingsManager.create(com.flexstor.common.settings.SendSettingsManager.USER).getItems();
1225         
1226
1227      }
1228      catch(com.flexstor.common.gateway.exceptions.TransactionFailedException e)
1229      {
1230         //System.out.println("Exception:"+e.getOrginalException());
1231      }
1232      catch(Exception e)
1233      {
1234         e.printStackTrace();
1235      }
1236
1237      // initialize here for stub testing, otherwise class initializer runs before resources are loaded
1238      DEFAULT_NAME = Resources.get(5242);
1239      ADHOC_NAME   = Resources.get(5243);
1240      
1241      
1242      
1243      ChangeSendSettingsDialog dlg = new ChangeSendSettingsDialog(new Frame(), null, htSettings,model,v,addBookModel);
1244      if(addBookModel.getDefaultEMailAddress() != null)
1245      dlg.setDefaultEMailAddress(addBookModel.getDefaultEMailAddress().getAddress());
1246      dlg.setVisible(true);
1247      //dlg.setDefaultEMailAddress("sender@firm.com");
1248      System.exit(0);
1249    }
1250  
1251}  // end of class