package org.csstudio.util.swt.meter;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/** main() routine for testing the clock as an SWT app.
 *  @author Kay Kasemir
 */
public class MeterWidgetTest
{
    private static boolean run = true;
    
    private static void updateMeter(final MeterWidget meter)
    {
        if (meter.isDisposed())
            return;
        meter.setValue(-10.0 + 20.0 * Math.random());
        meter.getDisplay().timerExec(200, new Runnable()
        {
            public void run()
            {
                updateMeter(meter);
            }
        });
    }

    @SuppressWarnings("nls")
    public static void main(String[] args)
    {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setBounds(400, 100, 300, 350);
        GridLayout gl = new GridLayout();
        gl.numColumns = 1;
        shell.setLayout(gl);
        GridData gd;

        MeterWidget meter = new MeterWidget(shell, 0);
        gd = new GridData();
        gd.grabExcessHorizontalSpace = true;
        gd.grabExcessVerticalSpace = true;
        gd.horizontalAlignment = SWT.FILL;
        gd.verticalAlignment = SWT.FILL;
        meter.setLayoutData(gd);
        
        Button ok = new Button(shell, SWT.PUSH);
        gd = new GridData();
        gd.grabExcessHorizontalSpace = true;
        gd.horizontalAlignment = SWT.RIGHT;
        ok.setLayoutData(gd);
        ok.setText("Ok");
        ok.addSelectionListener(new SelectionAdapter()
        {
            @Override
            public void widgetSelected(SelectionEvent e)
            {
                run = false;
            }
        });        

        updateMeter(meter);
        
        shell.open();
        // Message loop left to the application
        while (run && !shell.isDisposed())
            if (!display.readAndDispatch())
                display.sleep();
        display.dispose(); // !
    }
}

@@ -351,16 +351,55 @@
 s.Shell;
+%0Aimport org.eclipse.swt.widgets.Slider;
 %0A%0A/** ma
@@ -555,361 +555,8 @@
    %0A
-    private static void updateMeter(final MeterWidget meter)%0A    %7B%0A        if (meter.isDisposed())%0A            return;%0A        meter.setValue(-10.0 + 20.0 * Math.random());%0A        meter.getDisplay().timerExec(200, new Runnable()%0A        %7B%0A            public void run()%0A            %7B%0A                updateMeter(meter);%0A            %7D%0A        %7D);%0A    %7D%0A%0A
     
@@ -608,16 +608,22 @@
 id main(
+final 
 String%5B%5D
@@ -891,16 +891,22 @@
         
+final 
 MeterWid
@@ -1179,32 +1179,716 @@
 a(gd);%0A        %0A
+        meter.configure(0, 0.0, 2.0, 8.0, 9.0, 10.0, 2);%0A        %0A        final Slider slider = new Slider(shell, SWT.HORIZONTAL);%0A        gd = new GridData();%0A        gd.grabExcessHorizontalSpace = true;%0A        gd.horizontalAlignment = SWT.FILL;%0A        slider.setLayoutData(gd);%0A        slider.setValues(10, 0, 21, 1, 1, 1);%0A        slider.addSelectionListener(new SelectionAdapter()%0A        %7B%0A            @Override%0A            public void widgetSelected(SelectionEvent e)%0A            %7B%0A                final double value = slider.getSelection()-10.0;%0A                System.out.println(%22Value: %22 + value);%0A                meter.setValue(value);%0A            %7D%0A        %7D);%0A        %0A
         Button o
@@ -2320,36 +2320,8 @@
   %0A%0A
-        updateMeter(meter);%0A
     

Test with slider to control the value