FileDocCategorySizeDatePackage
CoordinateControls.javaAPI DocAndroid 1.5 API9184Wed May 06 22:41:08 BST 2009com.android.ddmuilib.location

CoordinateControls

public final class CoordinateControls extends Object
Encapsulation of controls handling a location coordinate in decimal and sexagesimal.

This handle the conversion between both modes automatically by using a {@link ModifyListener} on all the {@link Text} widgets.

To get/set the coordinate, use {@link #setValue(double)} and {@link #getValue()} (preceded by a call to {@link #isValueValid()})

Fields Summary
private double
mValue
private boolean
mValueValidity
private org.eclipse.swt.widgets.Text
mDecimalText
private org.eclipse.swt.widgets.Text
mSexagesimalDegreeText
private org.eclipse.swt.widgets.Text
mSexagesimalMinuteText
private org.eclipse.swt.widgets.Text
mSexagesimalSecondText
private int
mManualTextChange
Internal flag to prevent {@link ModifyEvent} to be sent when {@link Text#setText(String)} is called. This is an int instead of a boolean to act as a counter.
private org.eclipse.swt.events.ModifyListener
mSexagesimalListener
ModifyListener for the 3 {@link Text} controls of the sexagesimal mode.
Constructors Summary
Methods Summary
public voidcreateDecimalText(org.eclipse.swt.widgets.Composite parent)
Creates the {@link Text} control for the decimal display of the coordinate.

The control is expected to be placed in a Composite using a {@link GridLayout}.

param
parent The {@link Composite} parent of the control.

    
                                            
        
        mDecimalText = createTextControl(parent, "-199.999999", new ModifyListener() {
            public void modifyText(ModifyEvent event) {
                if (mManualTextChange > 0) {
                    return;
                }
                try {
                    mValue = Double.parseDouble(mDecimalText.getText());
                    setValueIntoSexagesimalControl(mValue);
                    mValueValidity = true;
                } catch (NumberFormatException e) {
                    // wrong format empty the sexagesimal controls.
                    mValueValidity = false;
                    resetSexagesimalControls();
                }
            }
        });
    
public voidcreateSexagesimalDegreeText(org.eclipse.swt.widgets.Composite parent)
Creates the {@link Text} control for the "degree" display of the coordinate in sexagesimal mode.

The control is expected to be placed in a Composite using a {@link GridLayout}.

param
parent The {@link Composite} parent of the control.

        mSexagesimalDegreeText = createTextControl(parent, "-199", mSexagesimalListener); //$NON-NLS-1$
    
public voidcreateSexagesimalMinuteText(org.eclipse.swt.widgets.Composite parent)
Creates the {@link Text} control for the "minute" display of the coordinate in sexagesimal mode.

The control is expected to be placed in a Composite using a {@link GridLayout}.

param
parent The {@link Composite} parent of the control.

        mSexagesimalMinuteText = createTextControl(parent, "99", mSexagesimalListener); //$NON-NLS-1$
    
public voidcreateSexagesimalSecondText(org.eclipse.swt.widgets.Composite parent)
Creates the {@link Text} control for the "second" display of the coordinate in sexagesimal mode.

The control is expected to be placed in a Composite using a {@link GridLayout}.

param
parent The {@link Composite} parent of the control.

        mSexagesimalSecondText = createTextControl(parent, "99.999", mSexagesimalListener); //$NON-NLS-1$
    
private org.eclipse.swt.widgets.TextcreateTextControl(org.eclipse.swt.widgets.Composite parent, java.lang.String defaultString, org.eclipse.swt.events.ModifyListener listener)
Creates a {@link Text} with a given parent, default string and a {@link ModifyListener}

param
parent the parent {@link Composite}.
param
defaultString the default string to be used to compute the {@link Text} control size hint.
param
listener the {@link ModifyListener} to be called when the {@link Text} control is modified.

        // create the control
        Text text = new Text(parent, SWT.BORDER | SWT.LEFT | SWT.SINGLE);
        
        // add the standard listener to it.
        text.addModifyListener(listener);
        
        // compute its size/
        mManualTextChange++;
        text.setText(defaultString);
        text.pack();
        Point size = text.computeSize(SWT.DEFAULT, SWT.DEFAULT);
        text.setText(""); //$NON-NLS-1$
        mManualTextChange--;
        
        GridData gridData = new GridData();
        gridData.widthHint = size.x;
        text.setLayoutData(gridData);
        
        return text;
    
public doublegetValue()
Returns the current value set in the control(s).

This value can be erroneous, and a check with {@link #isValueValid()} should be performed before any call to this method.

        return mValue;
    
private doublegetValueFromSexagesimalControls()

        double degrees = Double.parseDouble(mSexagesimalDegreeText.getText());
        double minutes = Double.parseDouble(mSexagesimalMinuteText.getText());
        double seconds = Double.parseDouble(mSexagesimalSecondText.getText());
        
        boolean isPositive = (degrees >= 0.);
        degrees = Math.abs(degrees);

        double value = degrees + minutes / 60. + seconds / 3600.; 
        return isPositive ? value : - value;
    
public booleanisValueValid()
Returns whether the value in the control(s) is valid.

        return mValueValidity;
    
private voidresetDecimalControls()

        mManualTextChange++;
        mDecimalText.setText(""); //$NON-NLS-1$
        mManualTextChange--;
    
private voidresetSexagesimalControls()

        mManualTextChange++;
        mSexagesimalDegreeText.setText(""); //$NON-NLS-1$
        mSexagesimalMinuteText.setText(""); //$NON-NLS-1$
        mSexagesimalSecondText.setText(""); //$NON-NLS-1$
        mManualTextChange--;
    
public voidsetEnabled(boolean enabled)
Enables or disables all the {@link Text} controls.

param
enabled the enabled state.

        mDecimalText.setEnabled(enabled);
        mSexagesimalDegreeText.setEnabled(enabled);
        mSexagesimalMinuteText.setEnabled(enabled);
        mSexagesimalSecondText.setEnabled(enabled);
    
public voidsetValue(double value)
Sets the coordinate into the {@link Text} controls.

param
value the coordinate value to set.

        mValue = value;
        mValueValidity = true;
        setValueIntoDecimalControl(value);
        setValueIntoSexagesimalControl(value);
    
private voidsetValueIntoDecimalControl(double value)

        mManualTextChange++;
        mDecimalText.setText(String.format("%.6f", value));
        mManualTextChange--;
    
private voidsetValueIntoSexagesimalControl(double value)

        // get the sign and make the number positive no matter what.
        boolean isPositive = (value >= 0.);
        value = Math.abs(value);
        
        // get the degree
        double degrees = Math.floor(value);
        
        // get the minutes
        double minutes = Math.floor((value - degrees) * 60.);
        
        // get the seconds.
        double seconds = (value - degrees) * 3600. - minutes * 60.;
        
        mManualTextChange++;
        mSexagesimalDegreeText.setText(
                Integer.toString(isPositive ? (int)degrees : (int)- degrees));
        mSexagesimalMinuteText.setText(Integer.toString((int)minutes));
        mSexagesimalSecondText.setText(String.format("%.3f", seconds)); //$NON-NLS-1$
        mManualTextChange--;