javauis/lcdui_qt/src/javax/microedition/lcdui/GaugeLayouter.java
changeset 21 2a9601315dfc
child 23 98ccebc37403
equal deleted inserted replaced
18:e8e63152f320 21:2a9601315dfc
       
     1 /*
       
     2 * Copyright (c) 2009 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 package javax.microedition.lcdui;
       
    18 
       
    19 import org.eclipse.swt.SWT;
       
    20 import org.eclipse.swt.events.SelectionEvent;
       
    21 import org.eclipse.swt.events.SelectionListener;
       
    22 import org.eclipse.swt.graphics.Point;
       
    23 import org.eclipse.swt.widgets.*;
       
    24 
       
    25 import org.eclipse.swt.layout.*;
       
    26 import org.eclipse.swt.internal.extension.Style;
       
    27 /**
       
    28  * Class for layouting gauges.
       
    29  */
       
    30 class GaugeLayouter extends ItemLayouter {
       
    31 
       
    32     /**
       
    33      * Key name for selection listener.
       
    34      */
       
    35     private static final String SELECTION_LISTENER = "selection";
       
    36 
       
    37     /**
       
    38      * Constructor.
       
    39      *
       
    40      * @param dflp DefaultFormLayoutPolicy
       
    41      */
       
    42     GaugeLayouter(DefaultFormLayoutPolicy dflp) {
       
    43         super(dflp);
       
    44     }
       
    45 
       
    46     /**
       
    47      * Creates the eSWT ProgressBar/Slider for this item.
       
    48      */
       
    49     Control eswtGetControl(Composite parent, Item item) {
       
    50         return eswtCreateControl(parent, item);
       
    51     }
       
    52 
       
    53     /**
       
    54      * Construts a gauge control surrounded with composite.
       
    55      */
       
    56     static Control eswtCreateControl(Composite parent, Item item) {
       
    57         Gauge gauge = (Gauge) item;
       
    58         // create an owning composite every time
       
    59         Composite comp = new Composite(parent, SWT.NONE);
       
    60 
       
    61         if (gauge.isInteractive()) {
       
    62         	  FormLayout layout = new FormLayout();
       
    63             layout.marginBottom = Style.pixelMetric(Style.QSTYLE_PM_LAYOUTBOTTOMMARGIN);
       
    64             layout.marginTop = Style.pixelMetric(Style.QSTYLE_PM_LAYOUTTOPMARGIN);
       
    65             layout.marginLeft = Style.pixelMetric(Style.QSTYLE_PM_LAYOUTLEFTMARGIN);
       
    66             layout.marginRight = Style.pixelMetric(Style.QSTYLE_PM_LAYOUTRIGHTMARGIN);
       
    67             layout.spacing = Style.pixelMetric(Style.QSTYLE_PM_LAYOUTVERTICALSPACING);
       
    68         	  comp.setLayout(layout);
       
    69 
       
    70         	  // Current Value - Mutable Value
       
    71             Label currentlabel = new Label(comp, SWT.WRAP);
       
    72             currentlabel.setText(Integer.toString( gauge.getValue()));
       
    73             currentlabel.setAlignment(SWT.LEAD);
       
    74 
       
    75        	    FormData currLabelData = new FormData();
       
    76             currLabelData.left = new FormAttachment(0);
       
    77         	  currentlabel.setLayoutData(currLabelData);
       
    78 
       
    79             Slider slider = new Slider(comp, SWT.HORIZONTAL);
       
    80             slider.setMinimum(0);
       
    81             slider.setMaximum(gauge.getMaxValue() + 1);
       
    82             slider.setSelection(gauge.getValue());
       
    83             slider.setIncrement(1);
       
    84             slider.setPageIncrement(1);
       
    85 
       
    86        	    FormData SliderLayoutData = new FormData();
       
    87             SliderLayoutData.right = new FormAttachment(100);
       
    88             SliderLayoutData.left = new FormAttachment(currentlabel);
       
    89         	  slider.setLayoutData(SliderLayoutData);
       
    90 
       
    91             // Min Value
       
    92             Label minlabel = new Label(comp, SWT.WRAP);
       
    93             minlabel.setText("0");
       
    94             minlabel.setAlignment(SWT.LEAD);
       
    95 
       
    96        	    FormData minLabelData = new FormData();
       
    97             minLabelData.left = new FormAttachment(slider, 0, SWT.LEFT);
       
    98             minLabelData.top =  new FormAttachment(slider);
       
    99         	  minlabel.setLayoutData(minLabelData);
       
   100 
       
   101             // Max Value
       
   102 						Label maxlabel = new Label(comp, SWT.WRAP);
       
   103             maxlabel.setText(Integer.toString(gauge.getMaxValue()) );
       
   104             maxlabel.setAlignment(SWT.LEAD);
       
   105 
       
   106        	    FormData maxLabelData = new FormData();
       
   107             maxLabelData.right = new FormAttachment(slider, 0, SWT.RIGHT);
       
   108             maxLabelData.top =  new FormAttachment(slider);
       
   109         	  maxlabel.setLayoutData(maxLabelData);
       
   110         }
       
   111         else {
       
   112             ProgressBar progressBar = null;
       
   113             if (gauge.isIndefinite()) {
       
   114                 switch (gauge.getValue()) {
       
   115                     case Gauge.CONTINUOUS_IDLE:
       
   116                         // TODO: eSWT support required
       
   117                         // Gauge like busy-state indicator with no activity:
       
   118                         progressBar = new ProgressBar(comp,
       
   119                                 SWT.HORIZONTAL | SWT.INDETERMINATE);
       
   120                         break;
       
   121                     case Gauge.CONTINUOUS_RUNNING:
       
   122                         // Gauge like busy-state indicator:
       
   123                         progressBar = new ProgressBar(comp,
       
   124                                 SWT.HORIZONTAL | SWT.INDETERMINATE);
       
   125                         break;
       
   126                     case Gauge.INCREMENTAL_IDLE:
       
   127                         // TODO: eSWT support required
       
   128                         // Gauge like INCREMENTAL_UPDATING, but no activity.
       
   129                         progressBar = new ProgressBar(comp,
       
   130                                 SWT.HORIZONTAL | SWT.INDETERMINATE);
       
   131                         break;
       
   132                     case Gauge.INCREMENTAL_UPDATING:
       
   133                         // TODO: eSWT support required
       
   134                         // Gauge which is updated on  calling Gauge.setValue().
       
   135                         progressBar = new ProgressBar(comp,
       
   136                                 SWT.HORIZONTAL | SWT.INDETERMINATE);
       
   137                         break;
       
   138                     default:
       
   139                         // This is error and will cause NullPointerException
       
   140                         // later in this method. Gauge-class takes care that
       
   141                         // this is never executed.
       
   142                         Logger.warning("Unexpected gauge value: " + gauge.getValue());
       
   143                         break;
       
   144                 }
       
   145             }
       
   146             else {
       
   147                 progressBar = new ProgressBar(comp, SWT.HORIZONTAL);
       
   148                 progressBar.setMaximum(gauge.getMaxValue() + 1);
       
   149                 progressBar.setSelection(gauge.getValue());
       
   150             }
       
   151         }
       
   152 
       
   153         return comp;
       
   154     }
       
   155 
       
   156     /**
       
   157      * Set the size of the layouted Control.
       
   158      */
       
   159     void eswtResizeControl(Item item, Control control, int width, int height) {
       
   160         super.eswtResizeControl(item, control, width, height);
       
   161         control.getParent().setSize(width, height);
       
   162         // This will move gauge to the center of the screen:
       
   163         control.setLocation((width - control.getBounds().width) / 2,
       
   164                 control.getLocation().y);
       
   165     }
       
   166 
       
   167     /**
       
   168      * Add listeners to Layouter specific control.
       
   169      */
       
   170     void eswtAddSpecificListeners(Item item, Control control) {
       
   171         super.eswtAddSpecificListeners(item, control);
       
   172         Gauge gauge = (Gauge) item;
       
   173         if (gauge.isInteractive()) {
       
   174             Slider slider = (Slider) control;
       
   175             SelectionListener listener = new GaugeSelectionListener(gauge);
       
   176             slider.addSelectionListener(listener);
       
   177             slider.setData(SELECTION_LISTENER, listener);
       
   178         }
       
   179     }
       
   180 
       
   181     /**
       
   182      * Remove listeners from Layouter specific control.
       
   183      */
       
   184     void eswtRemoveSpecificListeners(Item item, Control control) {
       
   185         super.eswtRemoveSpecificListeners(item, control);
       
   186         Gauge gauge = (Gauge) item;
       
   187         if (gauge.isInteractive()) {
       
   188             Slider slider = (Slider) control;
       
   189             SelectionListener listener = (SelectionListener) slider
       
   190                     .getData(SELECTION_LISTENER);
       
   191             if (listener != null) {
       
   192                 slider.removeSelectionListener(listener);
       
   193                 slider.setData(SELECTION_LISTENER, null);
       
   194             }
       
   195         }
       
   196     }
       
   197 
       
   198     /**
       
   199      * Returns if this eSWT control is Layouter specific.
       
   200      */
       
   201     boolean eswtIsSpecificControl(Item item, Control control) {
       
   202         if (((Gauge) item).isInteractive()) {
       
   203             return (control instanceof Slider);
       
   204         }
       
   205         else {
       
   206             return (control instanceof ProgressBar);
       
   207         }
       
   208     }
       
   209 
       
   210     /**
       
   211      * Updates the values of Gauge.
       
   212      */
       
   213     void eswtUpdateItem(Item item, Control control, int reason, Object param) {
       
   214         Gauge gauge = (Gauge) item;
       
   215         if (control instanceof ProgressBar) {
       
   216             ((ProgressBar) control).setMaximum(gauge.getMaxValue() + 1);
       
   217             ((ProgressBar) control).setSelection(gauge.getValue());
       
   218         }
       
   219         else if (control instanceof Slider) {
       
   220             ((Slider) control).setMaximum(gauge.getMaxValue() + 1);
       
   221             ((Slider) control).setSelection(gauge.getValue());
       
   222         }
       
   223     }
       
   224 
       
   225     /**
       
   226      * Returns true if that key was consumed by Gauge.
       
   227      */
       
   228     boolean eswtOfferKeyPressed(Item item, int key) {
       
   229         Gauge gauge = (Gauge) item;
       
   230         if (gauge.isInteractive()) {
       
   231             if (key == SWT.ARROW_RIGHT) {
       
   232                 gauge.internalSetValue(gauge.getValue() + 1);
       
   233                gauge.setLayout(gauge.internalGetLayout());
       
   234                 
       
   235             }
       
   236             else if(key == SWT.ARROW_LEFT) {
       
   237                gauge.internalSetValue(gauge.getValue() -1);
       
   238                gauge.setLayout(gauge.internalGetLayout());
       
   239             }
       
   240             return true;
       
   241         }
       
   242         return false;
       
   243     }
       
   244 
       
   245     /**
       
   246      * Returns the minimum area needed to display a Gauge.
       
   247      *
       
   248      * @param gauge Gauge object.
       
   249      * @return Minimum area needed to display Gauge.
       
   250      */
       
   251     static Point calculateMinimumBounds(final Gauge gauge) {
       
   252         final Point minSize = new Point(0, 0);
       
   253         ESWTUIThreadRunner.syncExec(new Runnable() {
       
   254             public void run() {
       
   255                 Control comp = eswtCreateControl(eswtGetStaticShell(), gauge);
       
   256                 minSize.x = getMaximumItemWidth(gauge);
       
   257                 minSize.y = ((Composite) comp).computeSize(minSize.x, SWT.DEFAULT).y;
       
   258                 applyMinMargins(gauge, minSize);
       
   259                 comp.dispose();
       
   260             }
       
   261         });
       
   262         return minSize;
       
   263     }
       
   264 
       
   265     /**
       
   266      * Returns the preferred area needed to display an Item.
       
   267      *
       
   268      * @param item Item.
       
   269      * @return Preferred area needed to display Item. x is width
       
   270      *      and y is height.
       
   271      */
       
   272     static Point calculatePreferredBounds(Item item) {
       
   273         final Point prefSize = new Point(0, 0);
       
   274         final Gauge gauge = (Gauge)item;
       
   275         ESWTUIThreadRunner.syncExec(new Runnable() {
       
   276             public void run() {
       
   277                 Control comp = eswtCreateControl(eswtGetStaticShell(), gauge);
       
   278                 prefSize.x = getMaximumItemWidth(gauge);
       
   279                 prefSize.y = ((Composite) comp).computeSize(prefSize.x, SWT.DEFAULT).y;
       
   280                 applyPrefMargins(gauge, prefSize);
       
   281                 comp.dispose();
       
   282             }
       
   283         });
       
   284         return prefSize;
       
   285     }
       
   286 
       
   287     /**
       
   288      * Class that receives events from slider and updates gauge's value.
       
   289      */
       
   290     class GaugeSelectionListener implements SelectionListener {
       
   291 
       
   292         private Gauge gauge;
       
   293 
       
   294         /**
       
   295          * Constructor.
       
   296          * @param gauge Gauge to be updated.
       
   297          */
       
   298         public GaugeSelectionListener(Gauge gauge) {
       
   299             this.gauge = gauge;
       
   300         }
       
   301 
       
   302         public void widgetDefaultSelected(SelectionEvent e) { }
       
   303 
       
   304         /**
       
   305          * Called by eSWT when Slider's value is changed.
       
   306          * Updates Gauge's value.
       
   307          */
       
   308         public void widgetSelected(SelectionEvent e) {
       
   309             int newValue = ((Slider) e.getSource()).getSelection();
       
   310             // Actions needed only if value is adjusted:
       
   311             if (newValue != gauge.getValue()) {
       
   312                 // set Gauge value
       
   313                 gauge.internalSetValue(newValue);
       
   314                 gauge.setLayout(gauge.internalGetLayout());
       
   315             }
       
   316         }
       
   317     }
       
   318 }