PageRenderTime 55ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/WrapInWizard.java

https://gitlab.com/Codeaurora/platform_sdk
Java | 267 lines | 203 code | 39 blank | 25 comment | 39 complexity | ca2e37a6578dd0dc09c7d780c6c3410c MD5 | raw file
  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.editors.layout.refactoring;
  17. import static com.android.ide.common.layout.LayoutConstants.FQCN_GESTURE_OVERLAY_VIEW;
  18. import static com.android.ide.common.layout.LayoutConstants.FQCN_LINEAR_LAYOUT;
  19. import static com.android.ide.common.layout.LayoutConstants.FQCN_RADIO_BUTTON;
  20. import static com.android.ide.common.layout.LayoutConstants.GESTURE_OVERLAY_VIEW;
  21. import static com.android.ide.common.layout.LayoutConstants.RADIO_GROUP;
  22. import static com.android.ide.eclipse.adt.internal.editors.layout.descriptors.LayoutDescriptors.VIEW_INCLUDE;
  23. import com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditor;
  24. import com.android.ide.eclipse.adt.internal.editors.layout.descriptors.ViewElementDescriptor;
  25. import com.android.ide.eclipse.adt.internal.editors.layout.gle2.CustomViewFinder;
  26. import com.android.ide.eclipse.adt.internal.editors.layout.gre.PaletteMetadataDescriptor;
  27. import com.android.ide.eclipse.adt.internal.editors.layout.gre.ViewMetadataRepository;
  28. import com.android.ide.eclipse.adt.internal.resources.ResourceNameValidator;
  29. import com.android.ide.eclipse.adt.internal.sdk.AndroidTargetData;
  30. import com.android.ide.eclipse.adt.internal.sdk.Sdk;
  31. import com.android.resources.ResourceType;
  32. import com.android.sdklib.IAndroidTarget;
  33. import com.android.util.Pair;
  34. import org.eclipse.core.resources.IProject;
  35. import org.eclipse.swt.SWT;
  36. import org.eclipse.swt.layout.GridData;
  37. import org.eclipse.swt.layout.GridLayout;
  38. import org.eclipse.swt.widgets.Combo;
  39. import org.eclipse.swt.widgets.Composite;
  40. import org.eclipse.swt.widgets.Label;
  41. import org.eclipse.swt.widgets.Text;
  42. import java.util.ArrayList;
  43. import java.util.Collections;
  44. import java.util.List;
  45. import java.util.Set;
  46. public class WrapInWizard extends VisualRefactoringWizard {
  47. private static final String SEPARATOR_LABEL =
  48. "----------------------------------------"; //$NON-NLS-1$
  49. public WrapInWizard(WrapInRefactoring ref, LayoutEditor editor) {
  50. super(ref, editor);
  51. setDefaultPageTitle("Wrap in Container");
  52. }
  53. @Override
  54. protected void addUserInputPages() {
  55. WrapInRefactoring ref = (WrapInRefactoring) getRefactoring();
  56. String oldType = ref.getOldType();
  57. addPage(new InputPage(mEditor.getProject(), oldType));
  58. }
  59. /** Wizard page which inputs parameters for the {@link WrapInRefactoring} operation */
  60. private static class InputPage extends VisualRefactoringInputPage {
  61. private final IProject mProject;
  62. private final String mOldType;
  63. private Text mIdText;
  64. private Combo mTypeCombo;
  65. private List<Pair<String, ViewElementDescriptor>> mClassNames;
  66. public InputPage(IProject project, String oldType) {
  67. super("WrapInInputPage"); //$NON-NLS-1$
  68. mProject = project;
  69. mOldType = oldType;
  70. }
  71. public void createControl(Composite parent) {
  72. Composite composite = new Composite(parent, SWT.NONE);
  73. composite.setLayout(new GridLayout(2, false));
  74. Label typeLabel = new Label(composite, SWT.NONE);
  75. typeLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
  76. typeLabel.setText("Type of Container:");
  77. mTypeCombo = new Combo(composite, SWT.READ_ONLY);
  78. mTypeCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
  79. mTypeCombo.addSelectionListener(mSelectionValidateListener);
  80. Label idLabel = new Label(composite, SWT.NONE);
  81. idLabel.setText("New Layout Id:");
  82. idLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
  83. mIdText = new Text(composite, SWT.BORDER);
  84. mIdText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
  85. mIdText.addModifyListener(mModifyValidateListener);
  86. Set<String> exclude = Collections.singleton(VIEW_INCLUDE);
  87. mClassNames = addLayouts(mProject, mOldType, mTypeCombo, exclude, true);
  88. mTypeCombo.select(0);
  89. setControl(composite);
  90. validatePage();
  91. mTypeCombo.setFocus();
  92. }
  93. @Override
  94. protected boolean validatePage() {
  95. boolean ok = true;
  96. String id = mIdText.getText().trim();
  97. if (id.length() == 0) {
  98. setErrorMessage("ID required");
  99. ok = false;
  100. } else {
  101. // ...but if you do, it has to be valid!
  102. ResourceNameValidator validator = ResourceNameValidator.create(false, mProject,
  103. ResourceType.ID);
  104. String message = validator.isValid(id);
  105. if (message != null) {
  106. setErrorMessage(message);
  107. ok = false;
  108. }
  109. }
  110. int selectionIndex = mTypeCombo.getSelectionIndex();
  111. String type = selectionIndex != -1 ? mClassNames.get(selectionIndex).getFirst() : null;
  112. if (type == null) {
  113. setErrorMessage("Select a container type");
  114. ok = false; // The user has chosen a separator
  115. }
  116. if (ok) {
  117. setErrorMessage(null);
  118. // Record state
  119. WrapInRefactoring refactoring =
  120. (WrapInRefactoring) getRefactoring();
  121. refactoring.setId(id);
  122. refactoring.setType(type);
  123. ViewElementDescriptor descriptor = mClassNames.get(selectionIndex).getSecond();
  124. if (descriptor instanceof PaletteMetadataDescriptor) {
  125. PaletteMetadataDescriptor paletteDescriptor =
  126. (PaletteMetadataDescriptor) descriptor;
  127. String initializedAttributes = paletteDescriptor.getInitializedAttributes();
  128. refactoring.setInitializedAttributes(initializedAttributes);
  129. } else {
  130. refactoring.setInitializedAttributes(null);
  131. }
  132. }
  133. setPageComplete(ok);
  134. return ok;
  135. }
  136. }
  137. static List<Pair<String, ViewElementDescriptor>> addLayouts(IProject project,
  138. String oldType, Combo combo,
  139. Set<String> exclude, boolean addGestureOverlay) {
  140. List<Pair<String, ViewElementDescriptor>> classNames =
  141. new ArrayList<Pair<String, ViewElementDescriptor>>();
  142. if (oldType != null && oldType.equals(FQCN_RADIO_BUTTON)) {
  143. combo.add(RADIO_GROUP);
  144. // NOT a fully qualified name since android widgets do not include the package
  145. classNames.add(Pair.of(RADIO_GROUP, (ViewElementDescriptor) null));
  146. combo.add(SEPARATOR_LABEL);
  147. classNames.add(Pair.<String,ViewElementDescriptor>of(null, null));
  148. }
  149. Pair<List<String>,List<String>> result = CustomViewFinder.findViews(project, true);
  150. List<String> customViews = result.getFirst();
  151. List<String> thirdPartyViews = result.getSecond();
  152. if (customViews.size() > 0) {
  153. for (String view : customViews) {
  154. combo.add(view);
  155. classNames.add(Pair.of(view, (ViewElementDescriptor) null));
  156. }
  157. combo.add(SEPARATOR_LABEL);
  158. classNames.add(Pair.<String,ViewElementDescriptor>of(null, null));
  159. }
  160. // Populate type combo
  161. Sdk currentSdk = Sdk.getCurrent();
  162. if (currentSdk != null) {
  163. IAndroidTarget target = currentSdk.getTarget(project);
  164. if (target != null) {
  165. AndroidTargetData targetData = currentSdk.getTargetData(target);
  166. if (targetData != null) {
  167. ViewMetadataRepository repository = ViewMetadataRepository.get();
  168. List<Pair<String,List<ViewElementDescriptor>>> entries =
  169. repository.getPaletteEntries(targetData, false, true);
  170. // Find the layout category - it contains LinearLayout
  171. List<ViewElementDescriptor> layoutDescriptors = null;
  172. search: for (Pair<String,List<ViewElementDescriptor>> pair : entries) {
  173. List<ViewElementDescriptor> list = pair.getSecond();
  174. for (ViewElementDescriptor d : list) {
  175. if (d.getFullClassName().equals(FQCN_LINEAR_LAYOUT)) {
  176. // Found - use this list
  177. layoutDescriptors = list;
  178. break search;
  179. }
  180. }
  181. }
  182. if (layoutDescriptors != null) {
  183. for (ViewElementDescriptor d : layoutDescriptors) {
  184. String className = d.getFullClassName();
  185. if (exclude == null || !exclude.contains(className)) {
  186. combo.add(d.getUiName());
  187. classNames.add(Pair.of(className, d));
  188. }
  189. }
  190. // SWT does not support separators in combo boxes
  191. combo.add(SEPARATOR_LABEL);
  192. classNames.add(null);
  193. if (thirdPartyViews.size() > 0) {
  194. for (String view : thirdPartyViews) {
  195. combo.add(view);
  196. classNames.add(Pair.of(view, (ViewElementDescriptor) null));
  197. }
  198. combo.add(SEPARATOR_LABEL);
  199. classNames.add(null);
  200. }
  201. if (addGestureOverlay) {
  202. combo.add(GESTURE_OVERLAY_VIEW);
  203. classNames.add(Pair.<String, ViewElementDescriptor> of(
  204. FQCN_GESTURE_OVERLAY_VIEW, null));
  205. combo.add(SEPARATOR_LABEL);
  206. classNames.add(Pair.<String,ViewElementDescriptor>of(null, null));
  207. }
  208. }
  209. // Now add ALL known layout descriptors in case the user has
  210. // a special case
  211. layoutDescriptors =
  212. targetData.getLayoutDescriptors().getLayoutDescriptors();
  213. for (ViewElementDescriptor d : layoutDescriptors) {
  214. String className = d.getFullClassName();
  215. if (exclude == null || !exclude.contains(className)) {
  216. combo.add(d.getUiName());
  217. classNames.add(Pair.of(className, d));
  218. }
  219. }
  220. }
  221. }
  222. } else {
  223. combo.add("SDK not initialized");
  224. classNames.add(Pair.<String,ViewElementDescriptor>of(null, null));
  225. }
  226. return classNames;
  227. }
  228. }