javauis/lcdui_qt/src/javax/microedition/lcdui/GaugeLayouter.java
changeset 69 773449708c84
parent 61 bf7ee68962da
--- a/javauis/lcdui_qt/src/javax/microedition/lcdui/GaugeLayouter.java	Wed Aug 18 09:43:15 2010 +0300
+++ b/javauis/lcdui_qt/src/javax/microedition/lcdui/GaugeLayouter.java	Thu Sep 02 20:20:40 2010 +0300
@@ -20,10 +20,13 @@
 import org.eclipse.swt.events.SelectionEvent;
 import org.eclipse.swt.events.SelectionListener;
 import org.eclipse.swt.graphics.Point;
+import org.eclipse.swt.graphics.Rectangle;
 import org.eclipse.swt.widgets.*;
-
 import org.eclipse.swt.layout.*;
 import org.eclipse.swt.internal.extension.Style;
+import org.eclipse.ercp.swt.mobile.CaptionedControl;
+
+
 /**
  * Class for layouting gauges.
  */
@@ -33,7 +36,10 @@
     /**
      * Key name for selection listener.
      */
-    private static final String SELECTION_LISTENER = "selection";
+    private static final String SELECTION_LISTENER = "SelectionListener";
+    private static final String MAX_LABEL_DATA = "MaxLabel";
+    private static final String CURRENT_LABEL_DATA = "CurrentLabel";
+
 
     /**
      * Constructor.
@@ -61,53 +67,47 @@
         Gauge gauge = (Gauge) item;
         // create an owning composite every time
         Composite comp = new Composite(parent, SWT.NONE);
+		FormLayout layout = new FormLayout();
+		comp.setLayout(layout);
 
         if(gauge.isInteractive())
         {
-            FormLayout layout = new FormLayout();
-            layout.marginBottom = Style.pixelMetric(Style.QSTYLE_PM_LAYOUTBOTTOMMARGIN);
-            layout.marginTop = Style.pixelMetric(Style.QSTYLE_PM_LAYOUTTOPMARGIN);
-            layout.marginLeft = Style.pixelMetric(Style.QSTYLE_PM_LAYOUTLEFTMARGIN);
-            layout.marginRight = Style.pixelMetric(Style.QSTYLE_PM_LAYOUTRIGHTMARGIN);
-            layout.spacing = Style.pixelMetric(Style.QSTYLE_PM_LAYOUTVERTICALSPACING);
-            comp.setLayout(layout);
-
-            // Current Value - Mutable Value
+            //Current Value Label
             Label currentlabel = new Label(comp, SWT.WRAP);
             currentlabel.setText(Integer.toString(gauge.getValue()));
             currentlabel.setAlignment(SWT.LEAD);
-
+			currentlabel.setData(CURRENT_LABEL_DATA, CURRENT_LABEL_DATA);
+			//Current Value Label Data
             FormData currLabelData = new FormData();
             currLabelData.left = new FormAttachment(0);
             currentlabel.setLayoutData(currLabelData);
-
+			//Slider
             Slider slider = new Slider(comp, SWT.HORIZONTAL);
             slider.setMinimum(0);
-            slider.setMaximum(gauge.getMaxValue() + 1);
+            slider.setMaximum(gauge.getMaxValue());
             slider.setSelection(gauge.getValue());
             slider.setIncrement(1);
             slider.setPageIncrement(1);
-
-            FormData SliderLayoutData = new FormData();
-            SliderLayoutData.right = new FormAttachment(100);
-            SliderLayoutData.left = new FormAttachment(currentlabel);
-            slider.setLayoutData(SliderLayoutData);
-
-            // Min Value
+			//Slider Data
+            FormData sliderLayoutData = new FormData();
+            sliderLayoutData.right = new FormAttachment(100);
+            sliderLayoutData.left = new FormAttachment(currentlabel);
+            slider.setLayoutData(sliderLayoutData);
+            //Min Value Label
             Label minlabel = new Label(comp, SWT.WRAP);
             minlabel.setText("0");
             minlabel.setAlignment(SWT.LEAD);
-
+			//Min Value Label Data
             FormData minLabelData = new FormData();
             minLabelData.left = new FormAttachment(slider, 0, SWT.LEFT);
             minLabelData.top =  new FormAttachment(slider);
             minlabel.setLayoutData(minLabelData);
-
-            // Max Value
+            //Max Value Label
             Label maxlabel = new Label(comp, SWT.WRAP);
             maxlabel.setText(Integer.toString(gauge.getMaxValue()));
             maxlabel.setAlignment(SWT.LEAD);
-
+			maxlabel.setData(MAX_LABEL_DATA, MAX_LABEL_DATA);
+			//Max Value Label Data
             FormData maxLabelData = new FormData();
             maxLabelData.right = new FormAttachment(slider, 0, SWT.RIGHT);
             maxLabelData.top =  new FormAttachment(slider);
@@ -127,26 +127,24 @@
                                                   SWT.HORIZONTAL | SWT.INDETERMINATE);
                     break;
                 case Gauge.CONTINUOUS_RUNNING:
-                    // Gauge like busy-state indicator:
+                    // Gauge like busy-state indicator with continuous activity:
                     progressBar = new ProgressBar(comp,
                                                   SWT.HORIZONTAL | SWT.INDETERMINATE);
                     break;
                 case Gauge.INCREMENTAL_IDLE:
                     // TODO: eSWT support required
-                    // Gauge like INCREMENTAL_UPDATING, but no activity.
+                    // Gauge like icremental updating, but no activity.
                     progressBar = new ProgressBar(comp,
                                                   SWT.HORIZONTAL | SWT.INDETERMINATE);
                     break;
                 case Gauge.INCREMENTAL_UPDATING:
                     // TODO: eSWT support required
-                    // Gauge which is updated on  calling Gauge.setValue().
+                    //Gauge like icremental updating, with indefinite activity.
                     progressBar = new ProgressBar(comp,
                                                   SWT.HORIZONTAL | SWT.INDETERMINATE);
                     break;
                 default:
-                    // This is error and will cause NullPointerException
-                    // later in this method. Gauge-class takes care that
-                    // this is never executed.
+                    // This is invalid case and should never occur, as Gauge should take care of it.
                     Logger.warning("Unexpected gauge value: " + gauge.getValue());
                     break;
                 }
@@ -154,24 +152,68 @@
             else
             {
                 progressBar = new ProgressBar(comp, SWT.HORIZONTAL);
-                progressBar.setMaximum(gauge.getMaxValue() + 1);
+                progressBar.setMaximum(gauge.getMaxValue());
                 progressBar.setSelection(gauge.getValue());
             }
+
+			if(progressBar != null)
+			{
+				FormData progressBarData = new FormData();
+				progressBarData.left = new FormAttachment(0);
+				progressBarData.right = new FormAttachment(100);
+				progressBar.setLayoutData(progressBarData);
+			}
+        }
+		parent.pack();
+        return parent;
+    }
+
+    /**
+     * returns the matching data Label of Gauge control.
+     */
+	Label eswtGetDataLabel(Control control, String data)
+	{
+        Control ret = null;
+		
+        if(control instanceof Label)
+        {
+			if(data.equals((String)control.getData(data)))
+            ret = control;
+        }
+        else if(control instanceof Composite)
+        {
+            Control[] children = ((Composite) control).getChildren();
+            for(int i = 0; i < children.length; i++)
+            {
+                Control result = eswtGetDataLabel(children[i], data);
+                if(result != null)
+                {
+                    ret = result;
+                    break;
+                }
+            }
         }
-
-        return comp;
-    }
+        return (Label)ret;
+	}
 
     /**
      * Set the size of the layouted Control.
      */
     void eswtResizeControl(Item item, Control control, int width, int height)
     {
-        super.eswtResizeControl(item, control, width, height);
-        control.getParent().setSize(width, height);
-        // This will move gauge to the center of the screen:
-        control.setLocation((width - control.getBounds().width) / 2,
-                            control.getLocation().y);
+		if((control instanceof Slider) || (control instanceof ProgressBar))
+		{
+			Control comp = control.getParent();
+			Control capt = comp.getParent();
+			comp.pack();
+			Point size = comp.computeSize(width, comp.getBounds().height);
+			comp.setSize(size.x, size.y);
+			Point loc = comp.getLocation();
+			if((height-size.y) != 0)
+			{
+				comp.setLocation(loc.x, loc.y + (height - size.y)/2);
+			}
+		}
     }
 
     /**
@@ -179,15 +221,19 @@
      */
     void eswtAddSpecificListeners(Item item, Control control)
     {
-        super.eswtAddSpecificListeners(item, control);
-        Gauge gauge = (Gauge) item;
-        if(gauge.isInteractive())
-        {
-            Slider slider = (Slider) control;
-            SelectionListener listener = new GaugeSelectionListener(gauge);
-            slider.addSelectionListener(listener);
-            slider.setData(SELECTION_LISTENER, listener);
-        }
+    	if(control instanceof Slider)
+   		{
+	        Slider slider = (Slider)control;
+			SelectionListener listener = (SelectionListener)slider.getData(SELECTION_LISTENER);
+	        if(listener == null)
+	        {
+		        super.eswtAddSpecificListeners(item, control);
+				Gauge gauge = (Gauge)item;
+		        listener = new GaugeSelectionListener(gauge);
+				slider.addSelectionListener(listener);
+				slider.setData(SELECTION_LISTENER, listener);
+	        }
+    	}
     }
 
     /**
@@ -195,19 +241,17 @@
      */
     void eswtRemoveSpecificListeners(Item item, Control control)
     {
-        super.eswtRemoveSpecificListeners(item, control);
-        Gauge gauge = (Gauge) item;
-        if(gauge.isInteractive())
-        {
-            Slider slider = (Slider) control;
-            SelectionListener listener = (SelectionListener) slider
-                                         .getData(SELECTION_LISTENER);
-            if(listener != null)
-            {
-                slider.removeSelectionListener(listener);
+    	if(control instanceof Slider)
+   		{
+	        Slider slider = (Slider)control;
+			SelectionListener listener = (SelectionListener)slider.getData(SELECTION_LISTENER);
+	        if(listener != null)
+	        {
+		        super.eswtRemoveSpecificListeners(item, control);
+				slider.removeSelectionListener(listener);
                 slider.setData(SELECTION_LISTENER, null);
-            }
-        }
+	        }
+    	}
     }
 
     /**
@@ -215,32 +259,102 @@
      */
     boolean eswtIsSpecificControl(Item item, Control control)
     {
-        if(((Gauge) item).isInteractive())
-        {
-            return (control instanceof Slider);
-        }
-        else
-        {
-            return (control instanceof ProgressBar);
-        }
+        return((control instanceof Slider) || (control instanceof ProgressBar));
     }
 
     /**
      * Updates the values of Gauge.
      */
-    void eswtUpdateItem(Item item, Control control, int reason, Object param)
+    void eswtUpdateItem(Item item, Control control, int aReason, Object param)
     {
-        Gauge gauge = (Gauge) item;
-        if(control instanceof ProgressBar)
-        {
-            ((ProgressBar) control).setMaximum(gauge.getMaxValue() + 1);
-            ((ProgressBar) control).setSelection(gauge.getValue());
-        }
-        else if(control instanceof Slider)
-        {
-            ((Slider) control).setMaximum(gauge.getMaxValue() + 1);
-            ((Slider) control).setSelection(gauge.getValue());
-        }
+		if(!(control instanceof  CaptionedControl))
+		{
+			return;
+		}
+		
+		Gauge gauge = (Gauge)item;
+		int reason = aReason & Item.UPDATE_SIZE_MASK;
+		
+		switch(reason)
+		{
+		case Item.UPDATE_NONE:
+			break;
+		
+		case Item.UPDATE_LABEL:
+		{
+			String label = gauge.getLabel();
+			if(label == null)
+			{
+				label = "";
+			}
+		
+			((CaptionedControl)control).setText(label);
+			control.pack();
+			break;
+		}
+		
+		case Gauge.UPDATE_VALUE:
+		{
+			//Setting the value to the control irrespective of the reason,
+			//as trade of for reason filtration.
+			Control ctrl = eswtFindSpecificControl(gauge, control);
+		
+			if(ctrl instanceof Slider)
+			{
+				Slider slider = (Slider)ctrl;
+				slider.setSelection(gauge.getValue());
+			}
+			else if(ctrl instanceof ProgressBar)
+			{
+				ProgressBar progressbar = (ProgressBar)ctrl;
+				progressbar.setSelection(gauge.getValue());
+			}
+
+			Label currLabel = eswtGetDataLabel(control, CURRENT_LABEL_DATA);
+			if(currLabel != null)
+			{
+				currLabel.setText(Integer.toString(gauge.getValue()));
+				currLabel.getParent().layout();
+			}
+			break;
+		}
+		
+		case Gauge.UPDATE_MAXVALUE:
+		{
+			Control ctrl = eswtFindSpecificControl(gauge, control);
+		
+			if(ctrl instanceof Slider)
+			{
+				Slider slider = (Slider)ctrl;
+				slider.setMaximum(gauge.getMaxValue());
+			}
+			else if(ctrl instanceof ProgressBar)
+			{
+				ProgressBar progressbar = (ProgressBar)ctrl;
+				progressbar.setMaximum(gauge.getMaxValue());
+			}
+
+			Label maxLabel = eswtGetDataLabel(control, MAX_LABEL_DATA);
+			if(maxLabel != null)
+			{
+				maxLabel.setText(Integer.toString(gauge.getMaxValue()));			
+				maxLabel.getParent().layout();
+			}
+
+			Label currLabel = eswtGetDataLabel(control, CURRENT_LABEL_DATA);
+			if(currLabel != null)
+			{
+				currLabel.setText(Integer.toString(gauge.getValue()));
+			}
+			break;
+		}
+		
+		default:
+		{
+			break;
+		}
+		}
+
     }
 
     /**
@@ -253,16 +367,23 @@
         {
             if(key == SWT.ARROW_RIGHT)
             {
-                gauge.internalSetValue(gauge.getValue() + 1);
-                gauge.setLayout(gauge.internalGetLayout());
+            	if(gauge.getValue() != gauge.getMaxValue())
+				{
+	                gauge.internalSetValue(gauge.getValue() + 1);
+	                gauge.setLayout(gauge.internalGetLayout());
+					return true;
+            	}
 
             }
             else if(key == SWT.ARROW_LEFT)
             {
-                gauge.internalSetValue(gauge.getValue() -1);
-                gauge.setLayout(gauge.internalGetLayout());
+            	if(gauge.getValue() > 0)
+            	{
+	                gauge.internalSetValue(gauge.getValue() - 1);
+	                gauge.setLayout(gauge.internalGetLayout());
+					return true;
+            	}
             }
-            return true;
         }
         return false;
     }
@@ -275,19 +396,25 @@
      */
     static Point calculateMinimumBounds(final Gauge gauge)
     {
-        final Point minSize = new Point(0, 0);
+	    final Point minSize = new Point(0, 0);
         ESWTUIThreadRunner.syncExec(new Runnable()
         {
             public void run()
             {
-                Control comp = eswtCreateControl(eswtGetStaticShell(), gauge);
-                minSize.x = getMaximumItemWidth(gauge);
-                minSize.y = ((Composite) comp).computeSize(minSize.x, SWT.DEFAULT).y;
-                applyMinMargins(gauge, minSize);
-                comp.dispose();
+		        CaptionedControl captioned = new CaptionedControl(eswtGetStaticShell(), SWT.VERTICAL);
+		        if(gauge.hasLabel())
+		        {
+			        captioned.setText(MIN_TEXT);
+				}
+		        eswtCreateControl(captioned, gauge);
+				int maxWidth = getMaximumItemWidth(gauge);
+		        Point size = captioned.computeSize(maxWidth, SWT.DEFAULT);
+		        captioned.dispose();
+		        minSize.x = size.x;
+		        minSize.y = size.y;
             }
         });
-        return minSize;
+		return minSize;
     }
 
     /**
@@ -297,22 +424,27 @@
      * @return Preferred area needed to display Item. x is width
      *      and y is height.
      */
-    static Point calculatePreferredBounds(Item item)
+    static Point calculatePreferredBounds(final Gauge gauge)
     {
-        final Point prefSize = new Point(0, 0);
-        final Gauge gauge = (Gauge)item;
-        ESWTUIThreadRunner.syncExec(new Runnable()
-        {
-            public void run()
-            {
-                Control comp = eswtCreateControl(eswtGetStaticShell(), gauge);
-                prefSize.x = getMaximumItemWidth(gauge);
-                prefSize.y = ((Composite) comp).computeSize(prefSize.x, SWT.DEFAULT).y;
-                applyPrefMargins(gauge, prefSize);
-                comp.dispose();
-            }
-        });
-        return prefSize;
+	    final Point prefSize = new Point(0, 0);
+	    ESWTUIThreadRunner.syncExec(new Runnable()
+	    {
+	        public void run()
+	        {
+		        CaptionedControl captioned = new CaptionedControl(eswtGetStaticShell(), SWT.VERTICAL);
+		        if(gauge.hasLabel())
+		        {
+			        captioned.setText(gauge.getLabel());
+				}
+		        eswtCreateControl(captioned, gauge);
+				int maxWidth = getMaximumItemWidth(gauge);
+		        Point size = captioned.computeSize(maxWidth, SWT.DEFAULT);
+		        captioned.dispose();
+		        prefSize.x = size.x;
+		        prefSize.y = size.y;
+	        }
+	    });
+		return prefSize;
     }
 
     /**
@@ -320,7 +452,6 @@
      */
     class GaugeSelectionListener implements SelectionListener
     {
-
         private Gauge gauge;
 
         /**
@@ -332,7 +463,10 @@
             this.gauge = gauge;
         }
 
-        public void widgetDefaultSelected(SelectionEvent e) { }
+        public void widgetDefaultSelected(SelectionEvent e)
+		{
+			Logger.method(this, "widgetDefaultSelected");
+		}
 
         /**
          * Called by eSWT when Slider's value is changed.
@@ -340,7 +474,9 @@
          */
         public void widgetSelected(SelectionEvent e)
         {
+        	//Implement for other Gauge types with Commands
             int newValue = ((Slider) e.getSource()).getSelection();
+
             // Actions needed only if value is adjusted:
             if(newValue != gauge.getValue())
             {