javamanager/javainstaller/installerui/javasrc/com/nokia/mj/impl/installer/ui/eswt/ProgressView.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.KeyEvent;
       
    23 import org.eclipse.swt.events.KeyListener;
       
    24 import org.eclipse.swt.events.SelectionEvent;
       
    25 import org.eclipse.swt.events.SelectionListener;
       
    26 import org.eclipse.swt.graphics.Point;
       
    27 import org.eclipse.swt.graphics.Rectangle;
       
    28 import org.eclipse.swt.layout.GridData;
       
    29 import org.eclipse.swt.widgets.Button;
       
    30 import org.eclipse.swt.widgets.Composite;
       
    31 import org.eclipse.swt.widgets.Control;
       
    32 import org.eclipse.swt.widgets.Label;
       
    33 import org.eclipse.swt.widgets.ProgressBar;
       
    34 import org.eclipse.swt.widgets.Shell;
       
    35 
       
    36 /**
       
    37  * ProgressView displays a progress bar.
       
    38  * Value range for the progress bar is between 0 and 100,
       
    39  * unless the progress bar is created as indeterminate
       
    40  * in which case the value is ignored.
       
    41  */
       
    42 public class ProgressView extends ViewBase
       
    43 {
       
    44     private KeyListener iViewKeyListener = null;
       
    45     private KeyListener iSoftKeyListener = null;
       
    46     private Label iLabel = null;
       
    47     private String iMsg = null;
       
    48     private ProgressBar iProgressBar = null;
       
    49     private boolean iIndeterminate = false;
       
    50     private int iValue = 0;
       
    51     protected Button iEmptyCommand = null;
       
    52     protected Button iCancelCommand = null;
       
    53 
       
    54     /** Constructor */
       
    55     protected ProgressView()
       
    56     {
       
    57         super();
       
    58     }
       
    59 
       
    60     /** Constructor */
       
    61     protected ProgressView(InstallerUiEswt aInstaller, Composite aParent)
       
    62     {
       
    63         this(aInstaller, aParent, "");
       
    64     }
       
    65 
       
    66     /** Constructor */
       
    67     protected ProgressView(InstallerUiEswt aInstaller, Composite aParent, String aMsg)
       
    68     {
       
    69         this(aInstaller, aParent, aMsg, false);
       
    70     }
       
    71 
       
    72     /** Constructor */
       
    73     protected ProgressView(InstallerUiEswt aInstaller, Composite aParent, String aMsg,
       
    74                            boolean aIndeterminate)
       
    75     {
       
    76         super(aInstaller, aParent, 1);
       
    77         iMsg = aMsg;
       
    78         setTitle(iMsg);
       
    79         iLabel = createLabel(iMsg, SWT.WRAP);
       
    80 
       
    81         iIndeterminate = aIndeterminate;
       
    82         if (iIndeterminate)
       
    83         {
       
    84             iProgressBar = new ProgressBar
       
    85             (getComposite(), SWT.INDETERMINATE | SWT.HORIZONTAL);
       
    86         }
       
    87         else
       
    88         {
       
    89             iProgressBar = new ProgressBar
       
    90             (getComposite(), SWT.SMOOTH | SWT.HORIZONTAL);
       
    91             iProgressBar.setMinimum(0);
       
    92             iProgressBar.setMaximum(100);
       
    93             iProgressBar.setSelection(iValue);
       
    94         }
       
    95         iProgressBar.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
       
    96 
       
    97         // To add cancel command to all progress bars,
       
    98         // uncomment the following line.
       
    99         addCancelCommand();
       
   100     }
       
   101 
       
   102     /** Update text for this progress bar. */
       
   103     public void setText(String aText)
       
   104     {
       
   105         iMsg = aText;
       
   106         // UI updates must be executed in UI thread.
       
   107         getComposite().getDisplay().syncExec
       
   108         (new Runnable()
       
   109         {
       
   110             public void run()
       
   111             {
       
   112                 setTitle(iMsg);
       
   113                 iLabel.setText(iMsg);
       
   114                 updateSize();
       
   115             }
       
   116         });
       
   117     }
       
   118 
       
   119     /** Update value for this progress bar. */
       
   120     public void updateProgress(int aValue)
       
   121     {
       
   122         iValue = aValue;
       
   123         if (iIndeterminate)
       
   124         {
       
   125             // No need to update an indeterminate progress bar.
       
   126             return;
       
   127         }
       
   128         // UI updates must be executed in UI thread.
       
   129         getComposite().getDisplay().syncExec
       
   130         (new Runnable()
       
   131         {
       
   132             public void run()
       
   133             {
       
   134                 iProgressBar.setSelection(iValue);
       
   135             }
       
   136         });
       
   137     }
       
   138 
       
   139     /**
       
   140      * Adds a cancel command for this progress view.
       
   141      */
       
   142     protected void addCancelCommand()
       
   143     {
       
   144         if (iCancelCommand != null || isDisposed())
       
   145         {
       
   146             // Cancel command has already been added, do not add it anymore.
       
   147             return;
       
   148         }
       
   149         // UI updates must be executed in UI thread.
       
   150         getDisplay().syncExec
       
   151         (new Runnable()
       
   152         {
       
   153             public void run()
       
   154             {
       
   155                 iEmptyCommand = new Button
       
   156                 (getCommandComposite(), SWT.PUSH);
       
   157                 GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
       
   158                 gridData.horizontalSpan = 1;
       
   159                 iEmptyCommand.setLayoutData(gridData);
       
   160                 iEmptyCommand.setEnabled(false);
       
   161                 addSoftKeyListenerFor(iEmptyCommand);
       
   162 
       
   163                 iCancelCommand = new Button
       
   164                 (getCommandComposite(), SWT.PUSH);
       
   165                 gridData = new GridData(GridData.FILL_HORIZONTAL);
       
   166                 gridData.horizontalSpan = 1;
       
   167                 iCancelCommand.setLayoutData(gridData);
       
   168                 iCancelCommand.setText
       
   169                 (InstallerUiTexts.get(InstallerUiTexts.CANCEL));
       
   170                 iCancelCommand.addSelectionListener
       
   171                 (new SelectionListener()
       
   172                 {
       
   173                     public void widgetDefaultSelected
       
   174                     (SelectionEvent aEvent)
       
   175                     {
       
   176                         widgetSelected(aEvent);
       
   177                     }
       
   178                     public void widgetSelected
       
   179                     (SelectionEvent aEvent)
       
   180                     {
       
   181                         confirmCancel();
       
   182                     }
       
   183                 });
       
   184                 addSoftKeyListenerFor(iCancelCommand);
       
   185                 // Set the Cancel command to be the default command.
       
   186                 iCancelCommand.setFocus();
       
   187                 getShell().setDefaultButton(iCancelCommand);
       
   188 
       
   189                 // Add a KeyListener to handle selection key, LSK and RSK events.
       
   190                 addViewKeyListenerFor(getContainer());
       
   191             }
       
   192         });
       
   193     }
       
   194 
       
   195     /**
       
   196      * Removes cancel command for this progress view.
       
   197      */
       
   198     protected void removeCancelCommand()
       
   199     {
       
   200         if (iCancelCommand == null || isDisposed())
       
   201         {
       
   202             // Cancel command does not exist, do nothing.
       
   203             return;
       
   204         }
       
   205         // UI updates must be executed in UI thread.
       
   206         getDisplay().syncExec
       
   207         (new Runnable()
       
   208         {
       
   209             public void run()
       
   210             {
       
   211                 getShell().setDefaultButton(null);
       
   212                 iEmptyCommand.dispose();
       
   213                 iEmptyCommand = null;
       
   214                 iCancelCommand.dispose();
       
   215                 iCancelCommand = null;
       
   216             }
       
   217         });
       
   218     }
       
   219 
       
   220     /**
       
   221      * This method is called when user cancels the dialog.
       
   222      */
       
   223     protected void confirmCancel()
       
   224     {
       
   225         if (iCancelCommand == null)
       
   226         {
       
   227             // Ignore selection of a non-existing command.
       
   228             return;
       
   229         }
       
   230         if (iInstallerUi != null)
       
   231         {
       
   232             iInstallerUi.cancel();
       
   233         }
       
   234     }
       
   235 
       
   236     /**
       
   237      * Adds KeyListener for given control. This KeyListener handles
       
   238      * selection key, LSK and RSK.
       
   239      */
       
   240     protected void addViewKeyListenerFor(Control aControl)
       
   241     {
       
   242         if (aControl == null)
       
   243         {
       
   244             return;
       
   245         }
       
   246         if (iViewKeyListener == null)
       
   247         {
       
   248             iViewKeyListener = new KeyListener()
       
   249             {
       
   250                 public void keyPressed(KeyEvent aEvent)
       
   251                 {
       
   252                     switch (aEvent.keyCode)
       
   253                     {
       
   254                     case SWT.ARROW_UP: // up
       
   255                     case SWT.ARROW_DOWN: // down
       
   256                     case SWT.ARROW_LEFT: // left
       
   257                     case SWT.ARROW_RIGHT: // right
       
   258                         // When View has focus and user presses arrow
       
   259                         // keys, set focus to the default command button.
       
   260                         iCancelCommand.setFocus();
       
   261                         break;
       
   262                     case -5: // selection key
       
   263                         // When View has focus the selection key
       
   264                         // does nothing in progress bar view.
       
   265                         break;
       
   266                     }
       
   267                 }
       
   268                 public void keyReleased(KeyEvent aEvent)
       
   269                 {
       
   270                 }
       
   271             };
       
   272         }
       
   273         aControl.addKeyListener(iViewKeyListener);
       
   274         addSoftKeyListenerFor(aControl);
       
   275     }
       
   276 
       
   277     /**
       
   278      * Adds KeyListener for given control. This KeyListener handles
       
   279      * LSK and RSK.
       
   280      */
       
   281     protected void addSoftKeyListenerFor(Control aControl)
       
   282     {
       
   283         if (aControl == null)
       
   284         {
       
   285             return;
       
   286         }
       
   287         if (iSoftKeyListener == null)
       
   288         {
       
   289             iSoftKeyListener = new KeyListener()
       
   290             {
       
   291                 public void keyPressed(KeyEvent aEvent)
       
   292                 {
       
   293                     switch (aEvent.keyCode)
       
   294                     {
       
   295                     case -6: // LSK
       
   296                         // Empty command, do nothing.
       
   297                         break;
       
   298                     case -7: // RSK
       
   299                         confirmCancel();
       
   300                         break;
       
   301                     }
       
   302                 }
       
   303                 public void keyReleased(KeyEvent aEvent)
       
   304                 {
       
   305                 }
       
   306             };
       
   307         }
       
   308         aControl.addKeyListener(iSoftKeyListener);
       
   309     }
       
   310 }