javauis/lcdui_qt/src/javax/microedition/lcdui/GaugeLayouter.java
changeset 69 773449708c84
parent 61 bf7ee68962da
equal deleted inserted replaced
61:bf7ee68962da 69:773449708c84
    18 
    18 
    19 import org.eclipse.swt.SWT;
    19 import org.eclipse.swt.SWT;
    20 import org.eclipse.swt.events.SelectionEvent;
    20 import org.eclipse.swt.events.SelectionEvent;
    21 import org.eclipse.swt.events.SelectionListener;
    21 import org.eclipse.swt.events.SelectionListener;
    22 import org.eclipse.swt.graphics.Point;
    22 import org.eclipse.swt.graphics.Point;
       
    23 import org.eclipse.swt.graphics.Rectangle;
    23 import org.eclipse.swt.widgets.*;
    24 import org.eclipse.swt.widgets.*;
    24 
       
    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 import org.eclipse.ercp.swt.mobile.CaptionedControl;
       
    28 
       
    29 
    27 /**
    30 /**
    28  * Class for layouting gauges.
    31  * Class for layouting gauges.
    29  */
    32  */
    30 class GaugeLayouter extends ItemLayouter
    33 class GaugeLayouter extends ItemLayouter
    31 {
    34 {
    32 
    35 
    33     /**
    36     /**
    34      * Key name for selection listener.
    37      * Key name for selection listener.
    35      */
    38      */
    36     private static final String SELECTION_LISTENER = "selection";
    39     private static final String SELECTION_LISTENER = "SelectionListener";
       
    40     private static final String MAX_LABEL_DATA = "MaxLabel";
       
    41     private static final String CURRENT_LABEL_DATA = "CurrentLabel";
       
    42 
    37 
    43 
    38     /**
    44     /**
    39      * Constructor.
    45      * Constructor.
    40      *
    46      *
    41      * @param aFormLayouter FormLayouter used for layouting.
    47      * @param aFormLayouter FormLayouter used for layouting.
    59     static Control eswtCreateControl(Composite parent, Item item)
    65     static Control eswtCreateControl(Composite parent, Item item)
    60     {
    66     {
    61         Gauge gauge = (Gauge) item;
    67         Gauge gauge = (Gauge) item;
    62         // create an owning composite every time
    68         // create an owning composite every time
    63         Composite comp = new Composite(parent, SWT.NONE);
    69         Composite comp = new Composite(parent, SWT.NONE);
       
    70 		FormLayout layout = new FormLayout();
       
    71 		comp.setLayout(layout);
    64 
    72 
    65         if(gauge.isInteractive())
    73         if(gauge.isInteractive())
    66         {
    74         {
    67             FormLayout layout = new FormLayout();
    75             //Current Value Label
    68             layout.marginBottom = Style.pixelMetric(Style.QSTYLE_PM_LAYOUTBOTTOMMARGIN);
       
    69             layout.marginTop = Style.pixelMetric(Style.QSTYLE_PM_LAYOUTTOPMARGIN);
       
    70             layout.marginLeft = Style.pixelMetric(Style.QSTYLE_PM_LAYOUTLEFTMARGIN);
       
    71             layout.marginRight = Style.pixelMetric(Style.QSTYLE_PM_LAYOUTRIGHTMARGIN);
       
    72             layout.spacing = Style.pixelMetric(Style.QSTYLE_PM_LAYOUTVERTICALSPACING);
       
    73             comp.setLayout(layout);
       
    74 
       
    75             // Current Value - Mutable Value
       
    76             Label currentlabel = new Label(comp, SWT.WRAP);
    76             Label currentlabel = new Label(comp, SWT.WRAP);
    77             currentlabel.setText(Integer.toString(gauge.getValue()));
    77             currentlabel.setText(Integer.toString(gauge.getValue()));
    78             currentlabel.setAlignment(SWT.LEAD);
    78             currentlabel.setAlignment(SWT.LEAD);
    79 
    79 			currentlabel.setData(CURRENT_LABEL_DATA, CURRENT_LABEL_DATA);
       
    80 			//Current Value Label Data
    80             FormData currLabelData = new FormData();
    81             FormData currLabelData = new FormData();
    81             currLabelData.left = new FormAttachment(0);
    82             currLabelData.left = new FormAttachment(0);
    82             currentlabel.setLayoutData(currLabelData);
    83             currentlabel.setLayoutData(currLabelData);
    83 
    84 			//Slider
    84             Slider slider = new Slider(comp, SWT.HORIZONTAL);
    85             Slider slider = new Slider(comp, SWT.HORIZONTAL);
    85             slider.setMinimum(0);
    86             slider.setMinimum(0);
    86             slider.setMaximum(gauge.getMaxValue() + 1);
    87             slider.setMaximum(gauge.getMaxValue());
    87             slider.setSelection(gauge.getValue());
    88             slider.setSelection(gauge.getValue());
    88             slider.setIncrement(1);
    89             slider.setIncrement(1);
    89             slider.setPageIncrement(1);
    90             slider.setPageIncrement(1);
    90 
    91 			//Slider Data
    91             FormData SliderLayoutData = new FormData();
    92             FormData sliderLayoutData = new FormData();
    92             SliderLayoutData.right = new FormAttachment(100);
    93             sliderLayoutData.right = new FormAttachment(100);
    93             SliderLayoutData.left = new FormAttachment(currentlabel);
    94             sliderLayoutData.left = new FormAttachment(currentlabel);
    94             slider.setLayoutData(SliderLayoutData);
    95             slider.setLayoutData(sliderLayoutData);
    95 
    96             //Min Value Label
    96             // Min Value
       
    97             Label minlabel = new Label(comp, SWT.WRAP);
    97             Label minlabel = new Label(comp, SWT.WRAP);
    98             minlabel.setText("0");
    98             minlabel.setText("0");
    99             minlabel.setAlignment(SWT.LEAD);
    99             minlabel.setAlignment(SWT.LEAD);
   100 
   100 			//Min Value Label Data
   101             FormData minLabelData = new FormData();
   101             FormData minLabelData = new FormData();
   102             minLabelData.left = new FormAttachment(slider, 0, SWT.LEFT);
   102             minLabelData.left = new FormAttachment(slider, 0, SWT.LEFT);
   103             minLabelData.top =  new FormAttachment(slider);
   103             minLabelData.top =  new FormAttachment(slider);
   104             minlabel.setLayoutData(minLabelData);
   104             minlabel.setLayoutData(minLabelData);
   105 
   105             //Max Value Label
   106             // Max Value
       
   107             Label maxlabel = new Label(comp, SWT.WRAP);
   106             Label maxlabel = new Label(comp, SWT.WRAP);
   108             maxlabel.setText(Integer.toString(gauge.getMaxValue()));
   107             maxlabel.setText(Integer.toString(gauge.getMaxValue()));
   109             maxlabel.setAlignment(SWT.LEAD);
   108             maxlabel.setAlignment(SWT.LEAD);
   110 
   109 			maxlabel.setData(MAX_LABEL_DATA, MAX_LABEL_DATA);
       
   110 			//Max Value Label Data
   111             FormData maxLabelData = new FormData();
   111             FormData maxLabelData = new FormData();
   112             maxLabelData.right = new FormAttachment(slider, 0, SWT.RIGHT);
   112             maxLabelData.right = new FormAttachment(slider, 0, SWT.RIGHT);
   113             maxLabelData.top =  new FormAttachment(slider);
   113             maxLabelData.top =  new FormAttachment(slider);
   114             maxlabel.setLayoutData(maxLabelData);
   114             maxlabel.setLayoutData(maxLabelData);
   115         }
   115         }
   125                     // Gauge like busy-state indicator with no activity:
   125                     // Gauge like busy-state indicator with no activity:
   126                     progressBar = new ProgressBar(comp,
   126                     progressBar = new ProgressBar(comp,
   127                                                   SWT.HORIZONTAL | SWT.INDETERMINATE);
   127                                                   SWT.HORIZONTAL | SWT.INDETERMINATE);
   128                     break;
   128                     break;
   129                 case Gauge.CONTINUOUS_RUNNING:
   129                 case Gauge.CONTINUOUS_RUNNING:
   130                     // Gauge like busy-state indicator:
   130                     // Gauge like busy-state indicator with continuous activity:
   131                     progressBar = new ProgressBar(comp,
   131                     progressBar = new ProgressBar(comp,
   132                                                   SWT.HORIZONTAL | SWT.INDETERMINATE);
   132                                                   SWT.HORIZONTAL | SWT.INDETERMINATE);
   133                     break;
   133                     break;
   134                 case Gauge.INCREMENTAL_IDLE:
   134                 case Gauge.INCREMENTAL_IDLE:
   135                     // TODO: eSWT support required
   135                     // TODO: eSWT support required
   136                     // Gauge like INCREMENTAL_UPDATING, but no activity.
   136                     // Gauge like icremental updating, but no activity.
   137                     progressBar = new ProgressBar(comp,
   137                     progressBar = new ProgressBar(comp,
   138                                                   SWT.HORIZONTAL | SWT.INDETERMINATE);
   138                                                   SWT.HORIZONTAL | SWT.INDETERMINATE);
   139                     break;
   139                     break;
   140                 case Gauge.INCREMENTAL_UPDATING:
   140                 case Gauge.INCREMENTAL_UPDATING:
   141                     // TODO: eSWT support required
   141                     // TODO: eSWT support required
   142                     // Gauge which is updated on  calling Gauge.setValue().
   142                     //Gauge like icremental updating, with indefinite activity.
   143                     progressBar = new ProgressBar(comp,
   143                     progressBar = new ProgressBar(comp,
   144                                                   SWT.HORIZONTAL | SWT.INDETERMINATE);
   144                                                   SWT.HORIZONTAL | SWT.INDETERMINATE);
   145                     break;
   145                     break;
   146                 default:
   146                 default:
   147                     // This is error and will cause NullPointerException
   147                     // This is invalid case and should never occur, as Gauge should take care of it.
   148                     // later in this method. Gauge-class takes care that
       
   149                     // this is never executed.
       
   150                     Logger.warning("Unexpected gauge value: " + gauge.getValue());
   148                     Logger.warning("Unexpected gauge value: " + gauge.getValue());
   151                     break;
   149                     break;
   152                 }
   150                 }
   153             }
   151             }
   154             else
   152             else
   155             {
   153             {
   156                 progressBar = new ProgressBar(comp, SWT.HORIZONTAL);
   154                 progressBar = new ProgressBar(comp, SWT.HORIZONTAL);
   157                 progressBar.setMaximum(gauge.getMaxValue() + 1);
   155                 progressBar.setMaximum(gauge.getMaxValue());
   158                 progressBar.setSelection(gauge.getValue());
   156                 progressBar.setSelection(gauge.getValue());
   159             }
   157             }
   160         }
   158 
   161 
   159 			if(progressBar != null)
   162         return comp;
   160 			{
   163     }
   161 				FormData progressBarData = new FormData();
       
   162 				progressBarData.left = new FormAttachment(0);
       
   163 				progressBarData.right = new FormAttachment(100);
       
   164 				progressBar.setLayoutData(progressBarData);
       
   165 			}
       
   166         }
       
   167 		parent.pack();
       
   168         return parent;
       
   169     }
       
   170 
       
   171     /**
       
   172      * returns the matching data Label of Gauge control.
       
   173      */
       
   174 	Label eswtGetDataLabel(Control control, String data)
       
   175 	{
       
   176         Control ret = null;
       
   177 		
       
   178         if(control instanceof Label)
       
   179         {
       
   180 			if(data.equals((String)control.getData(data)))
       
   181             ret = control;
       
   182         }
       
   183         else if(control instanceof Composite)
       
   184         {
       
   185             Control[] children = ((Composite) control).getChildren();
       
   186             for(int i = 0; i < children.length; i++)
       
   187             {
       
   188                 Control result = eswtGetDataLabel(children[i], data);
       
   189                 if(result != null)
       
   190                 {
       
   191                     ret = result;
       
   192                     break;
       
   193                 }
       
   194             }
       
   195         }
       
   196         return (Label)ret;
       
   197 	}
   164 
   198 
   165     /**
   199     /**
   166      * Set the size of the layouted Control.
   200      * Set the size of the layouted Control.
   167      */
   201      */
   168     void eswtResizeControl(Item item, Control control, int width, int height)
   202     void eswtResizeControl(Item item, Control control, int width, int height)
   169     {
   203     {
   170         super.eswtResizeControl(item, control, width, height);
   204 		if((control instanceof Slider) || (control instanceof ProgressBar))
   171         control.getParent().setSize(width, height);
   205 		{
   172         // This will move gauge to the center of the screen:
   206 			Control comp = control.getParent();
   173         control.setLocation((width - control.getBounds().width) / 2,
   207 			Control capt = comp.getParent();
   174                             control.getLocation().y);
   208 			comp.pack();
       
   209 			Point size = comp.computeSize(width, comp.getBounds().height);
       
   210 			comp.setSize(size.x, size.y);
       
   211 			Point loc = comp.getLocation();
       
   212 			if((height-size.y) != 0)
       
   213 			{
       
   214 				comp.setLocation(loc.x, loc.y + (height - size.y)/2);
       
   215 			}
       
   216 		}
   175     }
   217     }
   176 
   218 
   177     /**
   219     /**
   178      * Add listeners to Layouter specific control.
   220      * Add listeners to Layouter specific control.
   179      */
   221      */
   180     void eswtAddSpecificListeners(Item item, Control control)
   222     void eswtAddSpecificListeners(Item item, Control control)
   181     {
   223     {
   182         super.eswtAddSpecificListeners(item, control);
   224     	if(control instanceof Slider)
       
   225    		{
       
   226 	        Slider slider = (Slider)control;
       
   227 			SelectionListener listener = (SelectionListener)slider.getData(SELECTION_LISTENER);
       
   228 	        if(listener == null)
       
   229 	        {
       
   230 		        super.eswtAddSpecificListeners(item, control);
       
   231 				Gauge gauge = (Gauge)item;
       
   232 		        listener = new GaugeSelectionListener(gauge);
       
   233 				slider.addSelectionListener(listener);
       
   234 				slider.setData(SELECTION_LISTENER, listener);
       
   235 	        }
       
   236     	}
       
   237     }
       
   238 
       
   239     /**
       
   240      * Remove listeners from Layouter specific control.
       
   241      */
       
   242     void eswtRemoveSpecificListeners(Item item, Control control)
       
   243     {
       
   244     	if(control instanceof Slider)
       
   245    		{
       
   246 	        Slider slider = (Slider)control;
       
   247 			SelectionListener listener = (SelectionListener)slider.getData(SELECTION_LISTENER);
       
   248 	        if(listener != null)
       
   249 	        {
       
   250 		        super.eswtRemoveSpecificListeners(item, control);
       
   251 				slider.removeSelectionListener(listener);
       
   252                 slider.setData(SELECTION_LISTENER, null);
       
   253 	        }
       
   254     	}
       
   255     }
       
   256 
       
   257     /**
       
   258      * Returns if this eSWT control is Layouter specific.
       
   259      */
       
   260     boolean eswtIsSpecificControl(Item item, Control control)
       
   261     {
       
   262         return((control instanceof Slider) || (control instanceof ProgressBar));
       
   263     }
       
   264 
       
   265     /**
       
   266      * Updates the values of Gauge.
       
   267      */
       
   268     void eswtUpdateItem(Item item, Control control, int aReason, Object param)
       
   269     {
       
   270 		if(!(control instanceof  CaptionedControl))
       
   271 		{
       
   272 			return;
       
   273 		}
       
   274 		
       
   275 		Gauge gauge = (Gauge)item;
       
   276 		int reason = aReason & Item.UPDATE_SIZE_MASK;
       
   277 		
       
   278 		switch(reason)
       
   279 		{
       
   280 		case Item.UPDATE_NONE:
       
   281 			break;
       
   282 		
       
   283 		case Item.UPDATE_LABEL:
       
   284 		{
       
   285 			String label = gauge.getLabel();
       
   286 			if(label == null)
       
   287 			{
       
   288 				label = "";
       
   289 			}
       
   290 		
       
   291 			((CaptionedControl)control).setText(label);
       
   292 			control.pack();
       
   293 			break;
       
   294 		}
       
   295 		
       
   296 		case Gauge.UPDATE_VALUE:
       
   297 		{
       
   298 			//Setting the value to the control irrespective of the reason,
       
   299 			//as trade of for reason filtration.
       
   300 			Control ctrl = eswtFindSpecificControl(gauge, control);
       
   301 		
       
   302 			if(ctrl instanceof Slider)
       
   303 			{
       
   304 				Slider slider = (Slider)ctrl;
       
   305 				slider.setSelection(gauge.getValue());
       
   306 			}
       
   307 			else if(ctrl instanceof ProgressBar)
       
   308 			{
       
   309 				ProgressBar progressbar = (ProgressBar)ctrl;
       
   310 				progressbar.setSelection(gauge.getValue());
       
   311 			}
       
   312 
       
   313 			Label currLabel = eswtGetDataLabel(control, CURRENT_LABEL_DATA);
       
   314 			if(currLabel != null)
       
   315 			{
       
   316 				currLabel.setText(Integer.toString(gauge.getValue()));
       
   317 				currLabel.getParent().layout();
       
   318 			}
       
   319 			break;
       
   320 		}
       
   321 		
       
   322 		case Gauge.UPDATE_MAXVALUE:
       
   323 		{
       
   324 			Control ctrl = eswtFindSpecificControl(gauge, control);
       
   325 		
       
   326 			if(ctrl instanceof Slider)
       
   327 			{
       
   328 				Slider slider = (Slider)ctrl;
       
   329 				slider.setMaximum(gauge.getMaxValue());
       
   330 			}
       
   331 			else if(ctrl instanceof ProgressBar)
       
   332 			{
       
   333 				ProgressBar progressbar = (ProgressBar)ctrl;
       
   334 				progressbar.setMaximum(gauge.getMaxValue());
       
   335 			}
       
   336 
       
   337 			Label maxLabel = eswtGetDataLabel(control, MAX_LABEL_DATA);
       
   338 			if(maxLabel != null)
       
   339 			{
       
   340 				maxLabel.setText(Integer.toString(gauge.getMaxValue()));			
       
   341 				maxLabel.getParent().layout();
       
   342 			}
       
   343 
       
   344 			Label currLabel = eswtGetDataLabel(control, CURRENT_LABEL_DATA);
       
   345 			if(currLabel != null)
       
   346 			{
       
   347 				currLabel.setText(Integer.toString(gauge.getValue()));
       
   348 			}
       
   349 			break;
       
   350 		}
       
   351 		
       
   352 		default:
       
   353 		{
       
   354 			break;
       
   355 		}
       
   356 		}
       
   357 
       
   358     }
       
   359 
       
   360     /**
       
   361      * Returns true if that key was consumed by Gauge.
       
   362      */
       
   363     boolean eswtOfferKeyPressed(Item item, int key)
       
   364     {
   183         Gauge gauge = (Gauge) item;
   365         Gauge gauge = (Gauge) item;
   184         if(gauge.isInteractive())
   366         if(gauge.isInteractive())
   185         {
   367         {
   186             Slider slider = (Slider) control;
       
   187             SelectionListener listener = new GaugeSelectionListener(gauge);
       
   188             slider.addSelectionListener(listener);
       
   189             slider.setData(SELECTION_LISTENER, listener);
       
   190         }
       
   191     }
       
   192 
       
   193     /**
       
   194      * Remove listeners from Layouter specific control.
       
   195      */
       
   196     void eswtRemoveSpecificListeners(Item item, Control control)
       
   197     {
       
   198         super.eswtRemoveSpecificListeners(item, control);
       
   199         Gauge gauge = (Gauge) item;
       
   200         if(gauge.isInteractive())
       
   201         {
       
   202             Slider slider = (Slider) control;
       
   203             SelectionListener listener = (SelectionListener) slider
       
   204                                          .getData(SELECTION_LISTENER);
       
   205             if(listener != null)
       
   206             {
       
   207                 slider.removeSelectionListener(listener);
       
   208                 slider.setData(SELECTION_LISTENER, null);
       
   209             }
       
   210         }
       
   211     }
       
   212 
       
   213     /**
       
   214      * Returns if this eSWT control is Layouter specific.
       
   215      */
       
   216     boolean eswtIsSpecificControl(Item item, Control control)
       
   217     {
       
   218         if(((Gauge) item).isInteractive())
       
   219         {
       
   220             return (control instanceof Slider);
       
   221         }
       
   222         else
       
   223         {
       
   224             return (control instanceof ProgressBar);
       
   225         }
       
   226     }
       
   227 
       
   228     /**
       
   229      * Updates the values of Gauge.
       
   230      */
       
   231     void eswtUpdateItem(Item item, Control control, int reason, Object param)
       
   232     {
       
   233         Gauge gauge = (Gauge) item;
       
   234         if(control instanceof ProgressBar)
       
   235         {
       
   236             ((ProgressBar) control).setMaximum(gauge.getMaxValue() + 1);
       
   237             ((ProgressBar) control).setSelection(gauge.getValue());
       
   238         }
       
   239         else if(control instanceof Slider)
       
   240         {
       
   241             ((Slider) control).setMaximum(gauge.getMaxValue() + 1);
       
   242             ((Slider) control).setSelection(gauge.getValue());
       
   243         }
       
   244     }
       
   245 
       
   246     /**
       
   247      * Returns true if that key was consumed by Gauge.
       
   248      */
       
   249     boolean eswtOfferKeyPressed(Item item, int key)
       
   250     {
       
   251         Gauge gauge = (Gauge) item;
       
   252         if(gauge.isInteractive())
       
   253         {
       
   254             if(key == SWT.ARROW_RIGHT)
   368             if(key == SWT.ARROW_RIGHT)
   255             {
   369             {
   256                 gauge.internalSetValue(gauge.getValue() + 1);
   370             	if(gauge.getValue() != gauge.getMaxValue())
   257                 gauge.setLayout(gauge.internalGetLayout());
   371 				{
       
   372 	                gauge.internalSetValue(gauge.getValue() + 1);
       
   373 	                gauge.setLayout(gauge.internalGetLayout());
       
   374 					return true;
       
   375             	}
   258 
   376 
   259             }
   377             }
   260             else if(key == SWT.ARROW_LEFT)
   378             else if(key == SWT.ARROW_LEFT)
   261             {
   379             {
   262                 gauge.internalSetValue(gauge.getValue() -1);
   380             	if(gauge.getValue() > 0)
   263                 gauge.setLayout(gauge.internalGetLayout());
   381             	{
   264             }
   382 	                gauge.internalSetValue(gauge.getValue() - 1);
   265             return true;
   383 	                gauge.setLayout(gauge.internalGetLayout());
       
   384 					return true;
       
   385             	}
       
   386             }
   266         }
   387         }
   267         return false;
   388         return false;
   268     }
   389     }
   269 
   390 
   270     /**
   391     /**
   273      * @param gauge Gauge object.
   394      * @param gauge Gauge object.
   274      * @return Minimum area needed to display Gauge.
   395      * @return Minimum area needed to display Gauge.
   275      */
   396      */
   276     static Point calculateMinimumBounds(final Gauge gauge)
   397     static Point calculateMinimumBounds(final Gauge gauge)
   277     {
   398     {
   278         final Point minSize = new Point(0, 0);
   399 	    final Point minSize = new Point(0, 0);
   279         ESWTUIThreadRunner.syncExec(new Runnable()
   400         ESWTUIThreadRunner.syncExec(new Runnable()
   280         {
   401         {
   281             public void run()
   402             public void run()
   282             {
   403             {
   283                 Control comp = eswtCreateControl(eswtGetStaticShell(), gauge);
   404 		        CaptionedControl captioned = new CaptionedControl(eswtGetStaticShell(), SWT.VERTICAL);
   284                 minSize.x = getMaximumItemWidth(gauge);
   405 		        if(gauge.hasLabel())
   285                 minSize.y = ((Composite) comp).computeSize(minSize.x, SWT.DEFAULT).y;
   406 		        {
   286                 applyMinMargins(gauge, minSize);
   407 			        captioned.setText(MIN_TEXT);
   287                 comp.dispose();
   408 				}
       
   409 		        eswtCreateControl(captioned, gauge);
       
   410 				int maxWidth = getMaximumItemWidth(gauge);
       
   411 		        Point size = captioned.computeSize(maxWidth, SWT.DEFAULT);
       
   412 		        captioned.dispose();
       
   413 		        minSize.x = size.x;
       
   414 		        minSize.y = size.y;
   288             }
   415             }
   289         });
   416         });
   290         return minSize;
   417 		return minSize;
   291     }
   418     }
   292 
   419 
   293     /**
   420     /**
   294      * Returns the preferred area needed to display an Item.
   421      * Returns the preferred area needed to display an Item.
   295      *
   422      *
   296      * @param item Item.
   423      * @param item Item.
   297      * @return Preferred area needed to display Item. x is width
   424      * @return Preferred area needed to display Item. x is width
   298      *      and y is height.
   425      *      and y is height.
   299      */
   426      */
   300     static Point calculatePreferredBounds(Item item)
   427     static Point calculatePreferredBounds(final Gauge gauge)
   301     {
   428     {
   302         final Point prefSize = new Point(0, 0);
   429 	    final Point prefSize = new Point(0, 0);
   303         final Gauge gauge = (Gauge)item;
   430 	    ESWTUIThreadRunner.syncExec(new Runnable()
   304         ESWTUIThreadRunner.syncExec(new Runnable()
   431 	    {
   305         {
   432 	        public void run()
   306             public void run()
   433 	        {
   307             {
   434 		        CaptionedControl captioned = new CaptionedControl(eswtGetStaticShell(), SWT.VERTICAL);
   308                 Control comp = eswtCreateControl(eswtGetStaticShell(), gauge);
   435 		        if(gauge.hasLabel())
   309                 prefSize.x = getMaximumItemWidth(gauge);
   436 		        {
   310                 prefSize.y = ((Composite) comp).computeSize(prefSize.x, SWT.DEFAULT).y;
   437 			        captioned.setText(gauge.getLabel());
   311                 applyPrefMargins(gauge, prefSize);
   438 				}
   312                 comp.dispose();
   439 		        eswtCreateControl(captioned, gauge);
   313             }
   440 				int maxWidth = getMaximumItemWidth(gauge);
   314         });
   441 		        Point size = captioned.computeSize(maxWidth, SWT.DEFAULT);
   315         return prefSize;
   442 		        captioned.dispose();
       
   443 		        prefSize.x = size.x;
       
   444 		        prefSize.y = size.y;
       
   445 	        }
       
   446 	    });
       
   447 		return prefSize;
   316     }
   448     }
   317 
   449 
   318     /**
   450     /**
   319      * Class that receives events from slider and updates gauge's value.
   451      * Class that receives events from slider and updates gauge's value.
   320      */
   452      */
   321     class GaugeSelectionListener implements SelectionListener
   453     class GaugeSelectionListener implements SelectionListener
   322     {
   454     {
   323 
       
   324         private Gauge gauge;
   455         private Gauge gauge;
   325 
   456 
   326         /**
   457         /**
   327          * Constructor.
   458          * Constructor.
   328          * @param gauge Gauge to be updated.
   459          * @param gauge Gauge to be updated.
   330         public GaugeSelectionListener(Gauge gauge)
   461         public GaugeSelectionListener(Gauge gauge)
   331         {
   462         {
   332             this.gauge = gauge;
   463             this.gauge = gauge;
   333         }
   464         }
   334 
   465 
   335         public void widgetDefaultSelected(SelectionEvent e) { }
   466         public void widgetDefaultSelected(SelectionEvent e)
       
   467 		{
       
   468 			Logger.method(this, "widgetDefaultSelected");
       
   469 		}
   336 
   470 
   337         /**
   471         /**
   338          * Called by eSWT when Slider's value is changed.
   472          * Called by eSWT when Slider's value is changed.
   339          * Updates Gauge's value.
   473          * Updates Gauge's value.
   340          */
   474          */
   341         public void widgetSelected(SelectionEvent e)
   475         public void widgetSelected(SelectionEvent e)
   342         {
   476         {
       
   477         	//Implement for other Gauge types with Commands
   343             int newValue = ((Slider) e.getSource()).getSelection();
   478             int newValue = ((Slider) e.getSource()).getSelection();
       
   479 
   344             // Actions needed only if value is adjusted:
   480             // Actions needed only if value is adjusted:
   345             if(newValue != gauge.getValue())
   481             if(newValue != gauge.getValue())
   346             {
   482             {
   347                 // set Gauge value
   483                 // set Gauge value
   348                 gauge.internalSetValue(newValue);
   484                 gauge.internalSetValue(newValue);