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