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 
17 package com.android.ide.eclipse.adt.internal.assetstudio;
18 
19 import com.android.ide.eclipse.adt.internal.project.ProjectChooserHelper;
20 import com.android.ide.eclipse.adt.internal.project.ProjectChooserHelper.ProjectCombo;
21 import com.android.ide.eclipse.adt.internal.resources.ResourceNameValidator;
22 import com.android.resources.ResourceFolderType;
23 
24 import org.eclipse.jface.dialogs.IMessageProvider;
25 import org.eclipse.jface.wizard.WizardPage;
26 import org.eclipse.swt.SWT;
27 import org.eclipse.swt.dnd.Clipboard;
28 import org.eclipse.swt.dnd.TextTransfer;
29 import org.eclipse.swt.dnd.Transfer;
30 import org.eclipse.swt.events.ModifyEvent;
31 import org.eclipse.swt.events.ModifyListener;
32 import org.eclipse.swt.events.SelectionEvent;
33 import org.eclipse.swt.events.SelectionListener;
34 import org.eclipse.swt.layout.GridData;
35 import org.eclipse.swt.layout.GridLayout;
36 import org.eclipse.swt.widgets.Button;
37 import org.eclipse.swt.widgets.Composite;
38 import org.eclipse.swt.widgets.Label;
39 import org.eclipse.swt.widgets.Text;
40 
41 /** Page for choosing the type of asset to create, as well as the target project */
42 public class ChooseAssetTypePage extends WizardPage implements SelectionListener, ModifyListener {
43     private final CreateAssetSetWizardState mValues;
44     private ProjectCombo mProjectButton;
45     private Button mClipboardButton;
46     private Text mNameText;
47     private boolean mNameModified;
48     private Label mResourceName;
49 
50     /**
51      * Create the wizard.
52      */
ChooseAssetTypePage(CreateAssetSetWizardState values)53     public ChooseAssetTypePage(CreateAssetSetWizardState values) {
54         super("chooseAssetTypePage");
55         mValues = values;
56         setTitle("Choose Icon Set Type");
57         setDescription("Select the type of icon set to create:");
58     }
59 
60     /**
61      * Create contents of the wizard.
62      *
63      * @param parent the parent composite
64      */
65     @Override
createControl(Composite parent)66     public void createControl(Composite parent) {
67         Composite container = new Composite(parent, SWT.NULL);
68 
69         setControl(container);
70         container.setLayout(new GridLayout(3, false));
71 
72         for (AssetType type : AssetType.values()) {
73             Button button = new Button(container, SWT.RADIO);
74             button.setData(type);
75             button.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 3, 1));
76             button.setSelection(type == mValues.type);
77             button.setText(type.getDisplayName());
78             button.addSelectionListener(this);
79         }
80 
81         Label separator = new Label(container, SWT.SEPARATOR | SWT.HORIZONTAL);
82         GridData gdSeparator = new GridData(SWT.FILL, SWT.CENTER, false, false, 3, 1);
83         gdSeparator.heightHint = 20;
84         separator.setLayoutData(gdSeparator);
85 
86         Label projectLabel = new Label(container, SWT.NONE);
87         projectLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
88         projectLabel.setText("Project:");
89 
90         ProjectChooserHelper helper =
91                 new ProjectChooserHelper(getShell(), null /* filter */);
92         mProjectButton = new ProjectCombo(helper, container, mValues.project);
93         mProjectButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
94         mProjectButton.addSelectionListener(this);
95 
96         Label assetLabel = new Label(container, SWT.NONE);
97         assetLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
98         assetLabel.setText("Icon Name:");
99 
100         mNameText = new Text(container, SWT.BORDER);
101         mNameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
102         mNameText.addModifyListener(this);
103 
104         Label resourceLabel = new Label(container, SWT.NONE);
105         resourceLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
106         resourceLabel.setText("Resource:");
107 
108         mResourceName = new Label(container, SWT.NONE);
109         mResourceName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
110 
111         mClipboardButton = new Button(container, SWT.FLAT);
112         mClipboardButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
113         mClipboardButton.setText("Copy Name to Clipboard");
114 
115         mClipboardButton.addSelectionListener(this);
116 
117         updateAssetType();
118         validatePage();
119         parent.getDisplay().asyncExec(new Runnable() {
120             @Override
121             public void run() {
122                 mNameText.setFocus();
123             }
124         });
125     }
126 
updateAssetType()127     private void updateAssetType() {
128         if (!mNameModified) {
129             // Default name suggestion, possibly as a suffix, e.g. "ic_menu_<name>"
130             String replace = "name";
131             String suggestedName = String.format(mValues.type.getDefaultNameFormat(), replace);
132             mNameText.setText(suggestedName);
133             mValues.outputName = suggestedName;
134 
135             updateResourceLabel();
136             mNameModified = false;
137             int start = suggestedName.indexOf(replace);
138             if (start != -1) {
139                 mNameText.setSelection(start, start + replace.length());
140             } else {
141                 mNameText.selectAll();
142             }
143         } else {
144             mNameText.selectAll();
145         }
146     }
147 
updateResourceLabel()148     private void updateResourceLabel() {
149         mResourceName.setText("@drawable/" + getOutputName()); //$NON-NLS-1$
150     }
151 
152     @Override
canFlipToNextPage()153     public boolean canFlipToNextPage() {
154         return mValues.project != null;
155     }
156 
157     @Override
widgetSelected(SelectionEvent e)158     public void widgetSelected(SelectionEvent e) {
159         Object source = e.getSource();
160         if (source == mProjectButton) {
161             mValues.project = mProjectButton.getSelectedProject();
162             validatePage();
163         } else if (source == mClipboardButton) {
164             Clipboard clipboard = new Clipboard(getShell().getDisplay());
165             TextTransfer textTransfer = TextTransfer.getInstance();
166             clipboard.setContents(
167                     new Object[] { mResourceName.getText() },
168                     new Transfer[] { textTransfer });
169             clipboard.dispose();
170         } else if (source instanceof Button) {
171             // User selected a different asset type to be created
172             Object data = ((Button) source).getData();
173             if (data instanceof AssetType) {
174                 mValues.type = (AssetType) data;
175                 CreateAssetSetWizardState.sLastType = mValues.type;
176                 updateAssetType();
177             }
178         }
179     }
180 
181     @Override
widgetDefaultSelected(SelectionEvent e)182     public void widgetDefaultSelected(SelectionEvent e) {
183     }
184 
185     @Override
modifyText(ModifyEvent e)186     public void modifyText(ModifyEvent e) {
187         Object source = e.getSource();
188         if (source == mNameText) {
189             mNameModified = true;
190             mValues.outputName = mNameText.getText().trim();
191             updateResourceLabel();
192         }
193 
194         validatePage();
195     }
196 
getOutputName()197     private String getOutputName() {
198         return mNameText.getText().trim();
199     }
200 
validatePage()201     private void validatePage() {
202         String error = null;
203 
204         if (mValues.project == null) {
205             error = "Please select an Android project.";
206         } else {
207             String outputName = getOutputName();
208             if (outputName == null || outputName.length() == 0) {
209                 error = "Please enter a name";
210             } else {
211                 ResourceNameValidator validator =
212                         ResourceNameValidator.create(true, ResourceFolderType.DRAWABLE);
213                 error = validator.isValid(outputName);
214             }
215         }
216 
217         setPageComplete(error == null);
218         if (error != null) {
219             setMessage(error, IMessageProvider.ERROR);
220         } else {
221             setErrorMessage(null);
222             setMessage(null);
223         }
224     }
225 }
226