Home | History | Annotate | Download | only in ui
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Eclipse Public License, Version 1.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.eclipse.org/org/documents/epl-v10.php
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.android.ide.eclipse.adt.internal.ui;
     17 
     18 import com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart;
     19 import com.android.ide.eclipse.adt.internal.sdk.AndroidTargetData;
     20 import com.android.resources.ResourceType;
     21 
     22 import org.eclipse.jface.window.Window;
     23 import org.eclipse.swt.SWT;
     24 import org.eclipse.swt.layout.GridData;
     25 import org.eclipse.swt.layout.GridLayout;
     26 import org.eclipse.swt.widgets.Button;
     27 import org.eclipse.swt.widgets.Composite;
     28 import org.eclipse.swt.widgets.Control;
     29 import org.eclipse.swt.widgets.Event;
     30 import org.eclipse.swt.widgets.Label;
     31 import org.eclipse.swt.widgets.Listener;
     32 import org.eclipse.swt.widgets.Shell;
     33 import org.eclipse.swt.widgets.Text;
     34 import org.eclipse.ui.dialogs.SelectionStatusDialog;
     35 
     36 /**
     37  * Dialog for choosing margins
     38  */
     39 public class MarginChooser extends SelectionStatusDialog implements Listener {
     40     private GraphicalEditorPart mEditor;
     41     private AndroidTargetData mTargetData;
     42     private Text mLeftField;
     43     private Text mRightField;
     44     private Text mTopField;
     45     private Text mBottomField;
     46     private Text mAllField;
     47     private String mInitialAll;
     48     private String mInitialLeft;
     49     private String mInitialRight;
     50     private String mInitialTop;
     51     private String mInitialBottom;
     52     private Label mErrorLabel;
     53     private String[] mMargins;
     54 
     55     // Client data key for resource buttons pointing to the associated text field
     56     private final static String PROP_TEXTFIELD = "textField"; //$NON-NLS-1$
     57 
     58     /**
     59      * Constructs a new margin chooser dialog.
     60      *
     61      * @param parent parent widget
     62      * @param editor associated layout editor
     63      * @param targetData current SDK target
     64      * @param all current value for the all margins attribute
     65      * @param left current value for the left margin
     66      * @param right current value for the right margin
     67      * @param top current value for the top margin
     68      * @param bottom current value for the bottom margin
     69      */
     70     public MarginChooser(Shell parent, GraphicalEditorPart editor, AndroidTargetData targetData, String all,
     71             String left, String right, String top, String bottom) {
     72         super(parent);
     73         setTitle("Edit Margins");
     74         mEditor = editor;
     75         mTargetData = targetData;
     76         mInitialAll = all;
     77         mInitialLeft = left;
     78         mInitialRight = right;
     79         mInitialTop = top;
     80         mInitialBottom = bottom;
     81     }
     82 
     83     @SuppressWarnings("unused") // SWT constructors have side effects, "new Label" is not unused.
     84     @Override
     85     protected Control createDialogArea(Composite parent) {
     86         Composite container = new Composite(parent, SWT.NONE);
     87         container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
     88 
     89         container.setLayout(new GridLayout(3, false));
     90 
     91         Label allLabel = new Label(container, SWT.NONE);
     92         allLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
     93         allLabel.setText("All:");
     94 
     95         mAllField = new Text(container, SWT.BORDER | SWT.LEFT);
     96         mAllField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
     97         mAllField.setText(mInitialAll != null ? mInitialAll : ""); //$NON-NLS-1$
     98 
     99         Button allButton = new Button(container, SWT.NONE);
    100         allButton.setText("Resource...");
    101         allButton.setData(PROP_TEXTFIELD, mAllField);
    102 
    103         Label label = new Label(container, SWT.SEPARATOR | SWT.HORIZONTAL);
    104         label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 3, 1));
    105 
    106         Label leftLabel = new Label(container, SWT.NONE);
    107         leftLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
    108         leftLabel.setText("Left:");
    109 
    110         mLeftField = new Text(container, SWT.BORDER | SWT.LEFT);
    111         mLeftField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
    112         mLeftField.setText(mInitialLeft != null ? mInitialLeft : ""); //$NON-NLS-1$
    113 
    114         Button leftButton = new Button(container, SWT.NONE);
    115         leftButton.setText("Resource...");
    116         leftButton.setData(PROP_TEXTFIELD, mLeftField);
    117 
    118         Label rightLabel = new Label(container, SWT.NONE);
    119         rightLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
    120         rightLabel.setText("Right:");
    121 
    122         mRightField = new Text(container, SWT.BORDER | SWT.LEFT);
    123         mRightField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
    124         mRightField.setText(mInitialRight != null ? mInitialRight : ""); //$NON-NLS-1$
    125 
    126         Button rightButton = new Button(container, SWT.NONE);
    127         rightButton.setText("Resource...");
    128         rightButton.setData(PROP_TEXTFIELD, mRightField);
    129 
    130         Label topLabel = new Label(container, SWT.NONE);
    131         topLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
    132         topLabel.setText("Top:");
    133 
    134         mTopField = new Text(container, SWT.BORDER | SWT.LEFT);
    135         mTopField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
    136         mTopField.setText(mInitialTop != null ? mInitialTop : ""); //$NON-NLS-1$
    137 
    138         Button topButton = new Button(container, SWT.NONE);
    139         topButton.setText("Resource...");
    140         topButton.setData(PROP_TEXTFIELD, mTopField);
    141 
    142         Label bottomLabel = new Label(container, SWT.NONE);
    143         bottomLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
    144         bottomLabel.setText("Bottom:");
    145 
    146         mBottomField = new Text(container, SWT.BORDER | SWT.LEFT);
    147         mBottomField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
    148         mBottomField.setText(mInitialBottom != null ? mInitialBottom : ""); //$NON-NLS-1$
    149 
    150         Button bottomButton = new Button(container, SWT.NONE);
    151         bottomButton.setText("Resource...");
    152         bottomButton.setData(PROP_TEXTFIELD, mBottomField);
    153 
    154         allButton.addListener(SWT.Selection, this);
    155         leftButton.addListener(SWT.Selection, this);
    156         rightButton.addListener(SWT.Selection, this);
    157         topButton.addListener(SWT.Selection, this);
    158         bottomButton.addListener(SWT.Selection, this);
    159 
    160         mAllField.addListener(SWT.Modify, this);
    161         mLeftField.addListener(SWT.Modify, this);
    162         mRightField.addListener(SWT.Modify, this);
    163         mTopField.addListener(SWT.Modify, this);
    164         mBottomField.addListener(SWT.Modify, this);
    165 
    166         new Label(container, SWT.NONE);
    167         mErrorLabel = new Label(container, SWT.WRAP);
    168         mErrorLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));
    169         mErrorLabel.setForeground(parent.getDisplay().getSystemColor(SWT.COLOR_RED));
    170         return container;
    171     }
    172 
    173     @Override
    174     protected void computeResult() {
    175         mMargins = new String[] {
    176                 mAllField.getText().trim(),
    177                 mLeftField.getText().trim(), mRightField.getText().trim(),
    178                 mTopField.getText().trim(), mBottomField.getText().trim()
    179         };
    180     }
    181 
    182     /**
    183      * Returns the margins in the order all, left, right, top, bottom
    184      *
    185      * @return the margins in the order all, left, right, top, bottom, never
    186      *         null
    187      */
    188     public String[] getMargins() {
    189         return mMargins;
    190     }
    191 
    192     @Override
    193     public void handleEvent(Event event) {
    194         if (event.type == SWT.Modify) {
    195             // Text field modification -- warn about non-dip numbers
    196             if (event.widget instanceof Text) {
    197                 Text text = (Text) event.widget;
    198                 String input = text.getText().trim();
    199                 boolean isNumber = false;
    200                 try {
    201                     if (Integer.parseInt(input) > 0) {
    202                         isNumber = true;
    203                     }
    204                 } catch (NumberFormatException nufe) {
    205                     // Users are allowed to enter non-numbers here, not an error
    206                 }
    207                 if (isNumber) {
    208                     String message = String.format("Hint: Use \"%1$sdp\" instead", input);
    209                     mErrorLabel.setText(message);
    210                 } else {
    211                     mErrorLabel.setText("");
    212                 }
    213             }
    214         } else if (event.type == SWT.Selection) {
    215             // Button pressed - open resource chooser
    216             if (event.widget instanceof Button) {
    217                 Button button = (Button) event.widget;
    218                 Text text = (Text) button.getData(PROP_TEXTFIELD);
    219 
    220                 // Open a resource chooser dialog for specified resource type.
    221                 ResourceChooser chooser = ResourceChooser.create(mEditor, ResourceType.DIMEN)
    222                         .setCurrentResource(text.getText().trim());
    223                 if (chooser.open() == Window.OK) {
    224                     text.setText(chooser.getCurrentResource());
    225                 }
    226             }
    227         }
    228     }
    229 }
    230