aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/ui/MarginChooser.java
blob: c2a5f71b2c287501c08803af566483e26d313631 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Eclipse Public License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.eclipse.org/org/documents/epl-v10.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.ide.eclipse.adt.internal.ui;

import com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart;
import com.android.ide.eclipse.adt.internal.sdk.AndroidTargetData;
import com.android.resources.ResourceType;

import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.SelectionStatusDialog;

/**
 * Dialog for choosing margins
 */
public class MarginChooser extends SelectionStatusDialog implements Listener {
    private GraphicalEditorPart mEditor;
    private AndroidTargetData mTargetData;
    private Text mLeftField;
    private Text mRightField;
    private Text mTopField;
    private Text mBottomField;
    private Text mAllField;
    private String mInitialAll;
    private String mInitialLeft;
    private String mInitialRight;
    private String mInitialTop;
    private String mInitialBottom;
    private Label mErrorLabel;
    private String[] mMargins;

    // Client data key for resource buttons pointing to the associated text field
    private final static String PROP_TEXTFIELD = "textField"; //$NON-NLS-1$

    /**
     * Constructs a new margin chooser dialog.
     *
     * @param parent parent widget
     * @param editor associated layout editor
     * @param targetData current SDK target
     * @param all current value for the all margins attribute
     * @param left current value for the left margin
     * @param right current value for the right margin
     * @param top current value for the top margin
     * @param bottom current value for the bottom margin
     */
    public MarginChooser(Shell parent, GraphicalEditorPart editor, AndroidTargetData targetData, String all,
            String left, String right, String top, String bottom) {
        super(parent);
        setTitle("Edit Margins");
        mEditor = editor;
        mTargetData = targetData;
        mInitialAll = all;
        mInitialLeft = left;
        mInitialRight = right;
        mInitialTop = top;
        mInitialBottom = bottom;
    }

    @SuppressWarnings("unused") // SWT constructors have side effects, "new Label" is not unused.
    @Override
    protected Control createDialogArea(Composite parent) {
        Composite container = new Composite(parent, SWT.NONE);
        container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

        container.setLayout(new GridLayout(3, false));

        Label allLabel = new Label(container, SWT.NONE);
        allLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
        allLabel.setText("All:");

        mAllField = new Text(container, SWT.BORDER | SWT.LEFT);
        mAllField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
        mAllField.setText(mInitialAll != null ? mInitialAll : ""); //$NON-NLS-1$

        Button allButton = new Button(container, SWT.NONE);
        allButton.setText("Resource...");
        allButton.setData(PROP_TEXTFIELD, mAllField);

        Label label = new Label(container, SWT.SEPARATOR | SWT.HORIZONTAL);
        label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 3, 1));

        Label leftLabel = new Label(container, SWT.NONE);
        leftLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
        leftLabel.setText("Left:");

        mLeftField = new Text(container, SWT.BORDER | SWT.LEFT);
        mLeftField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
        mLeftField.setText(mInitialLeft != null ? mInitialLeft : ""); //$NON-NLS-1$

        Button leftButton = new Button(container, SWT.NONE);
        leftButton.setText("Resource...");
        leftButton.setData(PROP_TEXTFIELD, mLeftField);

        Label rightLabel = new Label(container, SWT.NONE);
        rightLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
        rightLabel.setText("Right:");

        mRightField = new Text(container, SWT.BORDER | SWT.LEFT);
        mRightField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
        mRightField.setText(mInitialRight != null ? mInitialRight : ""); //$NON-NLS-1$

        Button rightButton = new Button(container, SWT.NONE);
        rightButton.setText("Resource...");
        rightButton.setData(PROP_TEXTFIELD, mRightField);

        Label topLabel = new Label(container, SWT.NONE);
        topLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
        topLabel.setText("Top:");

        mTopField = new Text(container, SWT.BORDER | SWT.LEFT);
        mTopField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
        mTopField.setText(mInitialTop != null ? mInitialTop : ""); //$NON-NLS-1$

        Button topButton = new Button(container, SWT.NONE);
        topButton.setText("Resource...");
        topButton.setData(PROP_TEXTFIELD, mTopField);

        Label bottomLabel = new Label(container, SWT.NONE);
        bottomLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
        bottomLabel.setText("Bottom:");

        mBottomField = new Text(container, SWT.BORDER | SWT.LEFT);
        mBottomField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
        mBottomField.setText(mInitialBottom != null ? mInitialBottom : ""); //$NON-NLS-1$

        Button bottomButton = new Button(container, SWT.NONE);
        bottomButton.setText("Resource...");
        bottomButton.setData(PROP_TEXTFIELD, mBottomField);

        allButton.addListener(SWT.Selection, this);
        leftButton.addListener(SWT.Selection, this);
        rightButton.addListener(SWT.Selection, this);
        topButton.addListener(SWT.Selection, this);
        bottomButton.addListener(SWT.Selection, this);

        mAllField.addListener(SWT.Modify, this);
        mLeftField.addListener(SWT.Modify, this);
        mRightField.addListener(SWT.Modify, this);
        mTopField.addListener(SWT.Modify, this);
        mBottomField.addListener(SWT.Modify, this);

        new Label(container, SWT.NONE);
        mErrorLabel = new Label(container, SWT.WRAP);
        mErrorLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));
        mErrorLabel.setForeground(parent.getDisplay().getSystemColor(SWT.COLOR_RED));
        return container;
    }

    @Override
    protected void computeResult() {
        mMargins = new String[] {
                mAllField.getText().trim(),
                mLeftField.getText().trim(), mRightField.getText().trim(),
                mTopField.getText().trim(), mBottomField.getText().trim()
        };
    }

    /**
     * Returns the margins in the order all, left, right, top, bottom
     *
     * @return the margins in the order all, left, right, top, bottom, never
     *         null
     */
    public String[] getMargins() {
        return mMargins;
    }

    @Override
    public void handleEvent(Event event) {
        if (event.type == SWT.Modify) {
            // Text field modification -- warn about non-dip numbers
            if (event.widget instanceof Text) {
                Text text = (Text) event.widget;
                String input = text.getText().trim();
                boolean isNumber = false;
                try {
                    if (Integer.parseInt(input) > 0) {
                        isNumber = true;
                    }
                } catch (NumberFormatException nufe) {
                    // Users are allowed to enter non-numbers here, not an error
                }
                if (isNumber) {
                    String message = String.format("Hint: Use \"%1$sdp\" instead", input);
                    mErrorLabel.setText(message);
                } else {
                    mErrorLabel.setText("");
                }
            }
        } else if (event.type == SWT.Selection) {
            // Button pressed - open resource chooser
            if (event.widget instanceof Button) {
                Button button = (Button) event.widget;
                Text text = (Text) button.getData(PROP_TEXTFIELD);

                // Open a resource chooser dialog for specified resource type.
                ResourceChooser chooser = ResourceChooser.create(mEditor, ResourceType.DIMEN)
                        .setCurrentResource(text.getText().trim());
                if (chooser.open() == Window.OK) {
                    text.setText(chooser.getCurrentResource());
                }
            }
        }
    }
}