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.common.resources.ResourceRepository;
     19 import com.android.ide.eclipse.adt.internal.resources.manager.ProjectResources;
     20 import com.android.ide.eclipse.adt.internal.resources.manager.ResourceManager;
     21 import com.android.ide.eclipse.adt.internal.sdk.AndroidTargetData;
     22 import com.android.resources.ResourceType;
     23 
     24 import org.eclipse.core.resources.IProject;
     25 import org.eclipse.jface.window.Window;
     26 import org.eclipse.swt.SWT;
     27 import org.eclipse.swt.layout.GridData;
     28 import org.eclipse.swt.layout.GridLayout;
     29 import org.eclipse.swt.widgets.Button;
     30 import org.eclipse.swt.widgets.Composite;
     31 import org.eclipse.swt.widgets.Control;
     32 import org.eclipse.swt.widgets.Event;
     33 import org.eclipse.swt.widgets.Label;
     34 import org.eclipse.swt.widgets.Listener;
     35 import org.eclipse.swt.widgets.Shell;
     36 import org.eclipse.swt.widgets.Text;
     37 import org.eclipse.ui.dialogs.SelectionStatusDialog;
     38 
     39 public class MarginChooser extends SelectionStatusDialog implements Listener {
     40     private IProject mProject;
     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     public MarginChooser(Shell parent, IProject project, AndroidTargetData targetData, String all,
     59             String left, String right, String top, String bottom) {
     60         super(parent);
     61         setTitle("Edit Margins");
     62         mProject = project;
     63         mTargetData = targetData;
     64         mInitialAll = all;
     65         mInitialLeft = left;
     66         mInitialRight = right;
     67         mInitialTop = top;
     68         mInitialBottom = bottom;
     69     }
     70 
     71     @Override
     72     protected Control createDialogArea(Composite parent) {
     73         Composite container = new Composite(parent, SWT.NONE);
     74         container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
     75 
     76         container.setLayout(new GridLayout(3, false));
     77 
     78         Label allLabel = new Label(container, SWT.NONE);
     79         allLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
     80         allLabel.setText("All:");
     81 
     82         mAllField = new Text(container, SWT.BORDER | SWT.LEFT);
     83         mAllField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
     84         mAllField.setText(mInitialAll != null ? mInitialAll : ""); //$NON-NLS-1$
     85 
     86         Button allButton = new Button(container, SWT.NONE);
     87         allButton.setText("Resource...");
     88         allButton.setData(PROP_TEXTFIELD, mAllField);
     89 
     90         Label label = new Label(container, SWT.SEPARATOR | SWT.HORIZONTAL);
     91         label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 3, 1));
     92 
     93         Label leftLabel = new Label(container, SWT.NONE);
     94         leftLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
     95         leftLabel.setText("Left:");
     96 
     97         mLeftField = new Text(container, SWT.BORDER | SWT.LEFT);
     98         mLeftField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
     99         mLeftField.setText(mInitialLeft != null ? mInitialLeft : ""); //$NON-NLS-1$
    100 
    101         Button leftButton = new Button(container, SWT.NONE);
    102         leftButton.setText("Resource...");
    103         leftButton.setData(PROP_TEXTFIELD, mLeftField);
    104 
    105         Label rightLabel = new Label(container, SWT.NONE);
    106         rightLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
    107         rightLabel.setText("Right:");
    108 
    109         mRightField = new Text(container, SWT.BORDER | SWT.LEFT);
    110         mRightField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
    111         mRightField.setText(mInitialRight != null ? mInitialRight : ""); //$NON-NLS-1$
    112 
    113         Button rightButton = new Button(container, SWT.NONE);
    114         rightButton.setText("Resource...");
    115         rightButton.setData(PROP_TEXTFIELD, mRightField);
    116 
    117         Label topLabel = new Label(container, SWT.NONE);
    118         topLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
    119         topLabel.setText("Top:");
    120 
    121         mTopField = new Text(container, SWT.BORDER | SWT.LEFT);
    122         mTopField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
    123         mTopField.setText(mInitialTop != null ? mInitialTop : ""); //$NON-NLS-1$
    124 
    125         Button topButton = new Button(container, SWT.NONE);
    126         topButton.setText("Resource...");
    127         topButton.setData(PROP_TEXTFIELD, mTopField);
    128 
    129         Label bottomLabel = new Label(container, SWT.NONE);
    130         bottomLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
    131         bottomLabel.setText("Bottom:");
    132 
    133         mBottomField = new Text(container, SWT.BORDER | SWT.LEFT);
    134         mBottomField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
    135         mBottomField.setText(mInitialBottom != null ? mInitialBottom : ""); //$NON-NLS-1$
    136 
    137         Button bottomButton = new Button(container, SWT.NONE);
    138         bottomButton.setText("Resource...");
    139         bottomButton.setData(PROP_TEXTFIELD, mBottomField);
    140 
    141         allButton.addListener(SWT.Selection, this);
    142         leftButton.addListener(SWT.Selection, this);
    143         rightButton.addListener(SWT.Selection, this);
    144         topButton.addListener(SWT.Selection, this);
    145         bottomButton.addListener(SWT.Selection, this);
    146 
    147         mAllField.addListener(SWT.Modify, this);
    148         mLeftField.addListener(SWT.Modify, this);
    149         mRightField.addListener(SWT.Modify, this);
    150         mTopField.addListener(SWT.Modify, this);
    151         mBottomField.addListener(SWT.Modify, this);
    152 
    153         new Label(container, SWT.NONE);
    154         mErrorLabel = new Label(container, SWT.WRAP);
    155         mErrorLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));
    156         mErrorLabel.setForeground(parent.getDisplay().getSystemColor(SWT.COLOR_RED));
    157         return container;
    158     }
    159 
    160     @Override
    161     protected void computeResult() {
    162         mMargins = new String[] {
    163                 mAllField.getText().trim(),
    164                 mLeftField.getText().trim(), mRightField.getText().trim(),
    165                 mTopField.getText().trim(), mBottomField.getText().trim()
    166         };
    167     }
    168 
    169     public String[] getMargins() {
    170         return mMargins;
    171     }
    172 
    173     public void handleEvent(Event event) {
    174         if (event.type == SWT.Modify) {
    175             // Text field modification -- warn about non-dip numbers
    176             if (event.widget instanceof Text) {
    177                 Text text = (Text) event.widget;
    178                 String input = text.getText().trim();
    179                 boolean isNumber = false;
    180                 try {
    181                     if (Integer.parseInt(input) > 0) {
    182                         isNumber = true;
    183                     }
    184                 } catch (NumberFormatException nufe) {
    185                     // Users are allowed to enter non-numbers here, not an error
    186                 }
    187                 if (isNumber) {
    188                     String message = String.format("Hint: Use \"%1$sdp\" instead", input);
    189                     mErrorLabel.setText(message);
    190                 } else {
    191                     mErrorLabel.setText("");
    192                 }
    193             }
    194         } else if (event.type == SWT.Selection) {
    195             // Button pressed - open resource chooser
    196             if (event.widget instanceof Button) {
    197                 Button button = (Button) event.widget;
    198 
    199                 // Open a resource chooser dialog for specified resource type.
    200                 ProjectResources projectRepository = ResourceManager.getInstance()
    201                         .getProjectResources(mProject);
    202                 ResourceRepository frameworkRepository = mTargetData.getFrameworkResources();
    203                 ResourceChooser dlg = new ResourceChooser(mProject, ResourceType.DIMEN,
    204                         projectRepository, frameworkRepository, getShell());
    205                 Text text = (Text) button.getData(PROP_TEXTFIELD);
    206                 dlg.setCurrentResource(text.getText().trim());
    207                 if (dlg.open() == Window.OK) {
    208                     text.setText(dlg.getCurrentResource());
    209                 }
    210             }
    211         }
    212     }
    213 }
    214