javamanager/javainstaller/installerui/javasrc/com/nokia/mj/impl/installer/ui/eswt/ConfirmationViewBase.java
branchRCL_3
changeset 19 04becd199f91
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 package com.nokia.mj.impl.installer.ui.eswt;
       
    20 
       
    21 import org.eclipse.swt.SWT;
       
    22 import org.eclipse.swt.events.DisposeEvent;
       
    23 import org.eclipse.swt.events.DisposeListener;
       
    24 import org.eclipse.swt.events.FocusEvent;
       
    25 import org.eclipse.swt.events.FocusListener;
       
    26 import org.eclipse.swt.events.KeyEvent;
       
    27 import org.eclipse.swt.events.KeyListener;
       
    28 import org.eclipse.swt.events.SelectionEvent;
       
    29 import org.eclipse.swt.events.SelectionListener;
       
    30 import org.eclipse.swt.graphics.Point;
       
    31 import org.eclipse.swt.graphics.Rectangle;
       
    32 import org.eclipse.swt.layout.GridData;
       
    33 import org.eclipse.swt.widgets.Composite;
       
    34 import org.eclipse.swt.widgets.Control;
       
    35 import org.eclipse.swt.widgets.Button;
       
    36 
       
    37 /**
       
    38  * Base class for confirmation views.
       
    39  */
       
    40 abstract public class ConfirmationViewBase extends ViewBase
       
    41 {
       
    42     protected Button iOkCommand = null;
       
    43     protected Button iCancelCommand = null;
       
    44     private KeyListener iViewKeyListener = null;
       
    45     private KeyListener iSoftKeyListener = null;
       
    46     private String iOkText = InstallerUiTexts.get(InstallerUiTexts.OK);
       
    47     private String iCancelText = InstallerUiTexts.get(InstallerUiTexts.CANCEL);
       
    48     private boolean iCancelled = false;
       
    49     private boolean iUserHasAnswered = false;
       
    50     /** Object for thread synchronization. */
       
    51     private Object iSynchObject = new Object();
       
    52 
       
    53     /** Constructor */
       
    54     protected ConfirmationViewBase()
       
    55     {
       
    56         super();
       
    57     }
       
    58 
       
    59     /** Constructor */
       
    60     protected ConfirmationViewBase(InstallerUiEswt aInstaller, Composite aParent, int aColumns)
       
    61     {
       
    62         super(aInstaller, aParent, aColumns);
       
    63     }
       
    64 
       
    65     /**
       
    66      * Set user visible command names for OK and Cancel commands.
       
    67      * If either parameter is null, then default value is used.
       
    68      */
       
    69     public void setCommands(String aOkText, String aCancelText)
       
    70     {
       
    71         iOkText = aOkText;
       
    72         iCancelText = aCancelText;
       
    73     }
       
    74 
       
    75     /**
       
    76      * Synchoronous method for asking user confirmation.
       
    77      * This method will call createView() before opening the view
       
    78      * and getDataFromView() after user has answered.
       
    79      */
       
    80     public boolean confirm()
       
    81     {
       
    82         if (isDisposed())
       
    83         {
       
    84             return false;
       
    85         }
       
    86         // Open the dialog from the UI thread.
       
    87         getComposite().getDisplay().syncExec
       
    88         (new Runnable()
       
    89         {
       
    90             public void run()
       
    91             {
       
    92                 if (iOkCommand == null && iCancelCommand == null)
       
    93                 {
       
    94                     createView();
       
    95                     createCommands();
       
    96                     setDefaultCommand();
       
    97                 }
       
    98                 setVisible(true);
       
    99             }
       
   100         });
       
   101         // The UI thread must not be blocked. Let's wait for the answer
       
   102         // in the client thread.
       
   103         synchronized (iSynchObject)
       
   104         {
       
   105             try
       
   106             {
       
   107                 iSynchObject.wait();
       
   108             }
       
   109             catch (InterruptedException ie)
       
   110             {
       
   111                 // Ignore silently.
       
   112             }
       
   113         }
       
   114         if (commandDisposed())
       
   115         {
       
   116             // Widget got disposed, no confirmation available.
       
   117             return false;
       
   118         }
       
   119         // Hide the dialog.
       
   120         getComposite().getDisplay().syncExec
       
   121         (new Runnable()
       
   122         {
       
   123             public void run()
       
   124             {
       
   125                 if (commandDisposed())
       
   126                 {
       
   127                     return;
       
   128                 }
       
   129                 getDataFromView();
       
   130                 setVisible(false);
       
   131             }
       
   132         });
       
   133         // And return the result to the client.
       
   134         boolean result = true;
       
   135         if (iCancelled)
       
   136         {
       
   137             result = false;
       
   138         }
       
   139         return result;
       
   140     }
       
   141 
       
   142     /**
       
   143      * Returns true if user has answered to this dialog.
       
   144      */
       
   145     public boolean userHasAnswered()
       
   146     {
       
   147         return iUserHasAnswered;
       
   148     }
       
   149 
       
   150     /**
       
   151      * This method is called once before view is opened.
       
   152      * Inheriting class must implement this method.
       
   153      */
       
   154     abstract protected void createView();
       
   155 
       
   156     /**
       
   157      * This method is called after user has answered
       
   158      * to confirmation.
       
   159      * Inheriting class must implement this method.
       
   160      */
       
   161     abstract protected void getDataFromView();
       
   162 
       
   163     /**
       
   164      * Creates the commands for confirmation dialogs.
       
   165      */
       
   166     private void createCommands()
       
   167     {
       
   168         if (iOkText == null && iCancelText == null)
       
   169         {
       
   170             return;
       
   171         }
       
   172 
       
   173         iOkCommand = new Button(getCommandComposite(), SWT.PUSH);
       
   174         GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
       
   175         gridData.horizontalSpan = 1;
       
   176         iOkCommand.setLayoutData(gridData);
       
   177         iOkCommand.addDisposeListener(new DisposeListener()
       
   178         {
       
   179             public void widgetDisposed(DisposeEvent aEvent)
       
   180             {
       
   181                 confirmCancel();
       
   182             }
       
   183         });
       
   184         iOkCommand.addSelectionListener(new SelectionListener()
       
   185         {
       
   186             public void widgetDefaultSelected(SelectionEvent aEvent)
       
   187             {
       
   188                 widgetSelected(aEvent);
       
   189             }
       
   190             public void widgetSelected(SelectionEvent aEvent)
       
   191             {
       
   192                 confirmOk();
       
   193             }
       
   194         });
       
   195         iOkCommand.addFocusListener(new FocusListener()
       
   196         {
       
   197             public void focusGained(FocusEvent aEvent)
       
   198             {
       
   199                 okCommandFocused(aEvent);
       
   200             }
       
   201             public void focusLost(FocusEvent aEvent)
       
   202             {
       
   203                 // nop
       
   204             }
       
   205         });
       
   206         if (iOkText != null)
       
   207         {
       
   208             iOkCommand.setText(iOkText);
       
   209         }
       
   210         else
       
   211         {
       
   212             iOkCommand.setEnabled(false);
       
   213         }
       
   214         addSoftKeyListenerFor(iOkCommand);
       
   215 
       
   216         iCancelCommand = new Button(getCommandComposite(), SWT.PUSH);
       
   217         gridData = new GridData(GridData.FILL_HORIZONTAL);
       
   218         gridData.horizontalSpan = 1;
       
   219         iCancelCommand.setLayoutData(gridData);
       
   220         iCancelCommand.addDisposeListener(new DisposeListener()
       
   221         {
       
   222             public void widgetDisposed(DisposeEvent aEvent)
       
   223             {
       
   224                 confirmCancel();
       
   225             }
       
   226         });
       
   227         iCancelCommand.addSelectionListener(new SelectionListener()
       
   228         {
       
   229             public void widgetDefaultSelected(SelectionEvent aEvent)
       
   230             {
       
   231                 widgetSelected(aEvent);
       
   232             }
       
   233             public void widgetSelected(SelectionEvent aEvent)
       
   234             {
       
   235                 confirmCancel();
       
   236             }
       
   237         });
       
   238         iCancelCommand.addFocusListener(new FocusListener()
       
   239         {
       
   240             public void focusGained(FocusEvent aEvent)
       
   241             {
       
   242                 cancelCommandFocused(aEvent);
       
   243             }
       
   244             public void focusLost(FocusEvent aEvent)
       
   245             {
       
   246                 // nop
       
   247             }
       
   248         });
       
   249         if (iCancelText != null)
       
   250         {
       
   251             iCancelCommand.setText(iCancelText);
       
   252         }
       
   253         else
       
   254         {
       
   255             iCancelCommand.setEnabled(false);
       
   256         }
       
   257         addSoftKeyListenerFor(iCancelCommand);
       
   258 
       
   259         // Add a KeyListener to handle selection key, LSK and RSK events.
       
   260         addViewKeyListenerFor(getContainer());
       
   261     }
       
   262 
       
   263     /**
       
   264      * This method is called when user accepts the dialog.
       
   265      */
       
   266     protected void confirmOk()
       
   267     {
       
   268         synchronized (iSynchObject)
       
   269         {
       
   270             iUserHasAnswered = true;
       
   271             iCancelled = false;
       
   272             iSynchObject.notify();
       
   273         }
       
   274     }
       
   275 
       
   276     /**
       
   277      * This method is called when user cancels the dialog.
       
   278      */
       
   279     protected void confirmCancel()
       
   280     {
       
   281         synchronized (iSynchObject)
       
   282         {
       
   283             iUserHasAnswered = true;
       
   284             iCancelled = true;
       
   285             iSynchObject.notify();
       
   286         }
       
   287     }
       
   288 
       
   289     /**
       
   290      * Called after view and commands have been created. Subclasses
       
   291      * can overrride this method to set focus to their own default
       
   292      * commands.
       
   293      */
       
   294     protected void setDefaultCommand()
       
   295     {
       
   296         if (iOkText != null)
       
   297         {
       
   298             iOkCommand.setFocus();
       
   299             getShell().setDefaultButton(iOkCommand);
       
   300         }
       
   301         else
       
   302         {
       
   303             iCancelCommand.setFocus();
       
   304             getShell().setDefaultButton(iCancelCommand);
       
   305         }
       
   306     }
       
   307 
       
   308     /**
       
   309      * Adds KeyListener for given control. This KeyListener handles
       
   310      * selection key, LSK and RSK.
       
   311      */
       
   312     protected void addViewKeyListenerFor(Control aControl)
       
   313     {
       
   314         if (aControl == null)
       
   315         {
       
   316             return;
       
   317         }
       
   318         if (iViewKeyListener == null)
       
   319         {
       
   320             iViewKeyListener = new KeyListener()
       
   321             {
       
   322                 public void keyPressed(KeyEvent aEvent)
       
   323                 {
       
   324                     switch (aEvent.keyCode)
       
   325                     {
       
   326                     case SWT.ARROW_UP: // up
       
   327                     case SWT.ARROW_DOWN: // down
       
   328                     case SWT.ARROW_LEFT: // left
       
   329                     case SWT.ARROW_RIGHT: // right
       
   330                         // When View has focus and user presses arrow
       
   331                         // keys, set focus to the default command button.
       
   332                         if (iOkText != null)
       
   333                         {
       
   334                             iOkCommand.setFocus();
       
   335                         }
       
   336                         else
       
   337                         {
       
   338                             iCancelCommand.setFocus();
       
   339                         }
       
   340                         break;
       
   341                     case -5: // selection key
       
   342                         selectionKeyPressed(aEvent);
       
   343                         break;
       
   344                     }
       
   345                 }
       
   346                 public void keyReleased(KeyEvent aEvent)
       
   347                 {
       
   348                 }
       
   349             };
       
   350         }
       
   351         aControl.addKeyListener(iViewKeyListener);
       
   352         addSoftKeyListenerFor(aControl);
       
   353     }
       
   354 
       
   355     /**
       
   356      * Adds KeyListener for given control. This KeyListener handles
       
   357      * LSK and RSK.
       
   358      */
       
   359     protected void addSoftKeyListenerFor(Control aControl)
       
   360     {
       
   361         if (aControl == null)
       
   362         {
       
   363             return;
       
   364         }
       
   365         if (iSoftKeyListener == null)
       
   366         {
       
   367             iSoftKeyListener = new KeyListener()
       
   368             {
       
   369                 public void keyPressed(KeyEvent aEvent)
       
   370                 {
       
   371                     switch (aEvent.keyCode)
       
   372                     {
       
   373                     case -6: // LSK
       
   374                         lskPressed(aEvent);
       
   375                         break;
       
   376                     case -7: // RSK
       
   377                         rskPressed(aEvent);
       
   378                         break;
       
   379                     }
       
   380                 }
       
   381                 public void keyReleased(KeyEvent aEvent)
       
   382                 {
       
   383                 }
       
   384             };
       
   385         }
       
   386         aControl.addKeyListener(iSoftKeyListener);
       
   387     }
       
   388 
       
   389     /**
       
   390      * Called when user presses selection key for the Shell.
       
   391      */
       
   392     protected void selectionKeyPressed(KeyEvent aEvent)
       
   393     {
       
   394         if (iOkText != null)
       
   395         {
       
   396             confirmOk();
       
   397         }
       
   398         else
       
   399         {
       
   400             confirmCancel();
       
   401         }
       
   402     }
       
   403 
       
   404     /**
       
   405      * Called when user presses left soft key.
       
   406      */
       
   407     protected void lskPressed(KeyEvent aEvent)
       
   408     {
       
   409         if (iOkText != null)
       
   410         {
       
   411             confirmOk();
       
   412         }
       
   413     }
       
   414 
       
   415     /**
       
   416      * Called when user presses right soft key.
       
   417      */
       
   418     protected void rskPressed(KeyEvent aEvent)
       
   419     {
       
   420         if (iCancelText != null)
       
   421         {
       
   422             confirmCancel();
       
   423         }
       
   424     }
       
   425 
       
   426     /**
       
   427      * Called when the OK command gets focus.
       
   428      */
       
   429     protected void okCommandFocused(FocusEvent aEvent)
       
   430     {
       
   431     }
       
   432 
       
   433     /**
       
   434      * Called when the Cancel command gets focus.
       
   435      */
       
   436     protected void cancelCommandFocused(FocusEvent aEvent)
       
   437     {
       
   438     }
       
   439 
       
   440     /**
       
   441      * Returns true if a command from this view has been disposed.
       
   442      */
       
   443     private boolean commandDisposed()
       
   444     {
       
   445         if ((iOkCommand != null && iOkCommand.isDisposed()) ||
       
   446                 (iCancelCommand != null && iCancelCommand.isDisposed()))
       
   447         {
       
   448             return true;
       
   449         }
       
   450         return false;
       
   451     }
       
   452 }