aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/ChangeViewWizard.java
blob: 0ac7106b3c4ed79e133ecbcf7f3d67e5f2f094e4 (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
/*
 * 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.editors.layout.refactoring;

import static com.android.SdkConstants.REQUEST_FOCUS;
import static com.android.SdkConstants.VIEW_FRAGMENT;
import static com.android.SdkConstants.VIEW_INCLUDE;

import com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditorDelegate;
import com.android.ide.eclipse.adt.internal.editors.layout.descriptors.ViewElementDescriptor;
import com.android.ide.eclipse.adt.internal.editors.layout.gle2.CustomViewFinder;
import com.android.ide.eclipse.adt.internal.editors.layout.gre.ViewMetadataRepository;
import com.android.ide.eclipse.adt.internal.sdk.AndroidTargetData;
import com.android.ide.eclipse.adt.internal.sdk.Sdk;
import com.android.sdklib.IAndroidTarget;
import com.android.utils.Pair;

import org.eclipse.core.resources.IProject;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

import java.util.ArrayList;
import java.util.List;

class ChangeViewWizard extends VisualRefactoringWizard {
    private static final String SEPARATOR_LABEL =
        "----------------------------------------"; //$NON-NLS-1$

    public ChangeViewWizard(ChangeViewRefactoring ref, LayoutEditorDelegate editor) {
        super(ref, editor);
        setDefaultPageTitle("Change Widget Type");
    }

    @Override
    protected void addUserInputPages() {
        ChangeViewRefactoring ref = (ChangeViewRefactoring) getRefactoring();
        List<String> oldTypes = ref.getOldTypes();
        String oldType = null;
        for (String type : oldTypes) {
            if (oldType == null) {
                oldType = type;
            } else if (!oldType.equals(type)) {
                // If the types differ, don't offer related categories
                oldType = null;
                break;
            }
        }
        addPage(new InputPage(mDelegate.getEditor().getProject(), oldType));
    }

    /** Wizard page which inputs parameters for the {@link ChangeViewRefactoring} operation */
    private static class InputPage extends VisualRefactoringInputPage {
        private final IProject mProject;
        private Combo mTypeCombo;
        private final String mOldType;
        private List<String> mClassNames;

        public InputPage(IProject project, String oldType) {
            super("ChangeViewInputPage");  //$NON-NLS-1$
            mProject = project;
            mOldType = oldType;
        }

        @Override
        public void createControl(Composite parent) {
            Composite composite = new Composite(parent, SWT.NONE);
            composite.setLayout(new GridLayout(2, false));

            Label typeLabel = new Label(composite, SWT.NONE);
            typeLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
            typeLabel.setText("New Widget Type:");

            mTypeCombo = new Combo(composite, SWT.READ_ONLY);
            mTypeCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
            mTypeCombo.addSelectionListener(mSelectionValidateListener);

            mClassNames = getWidgetTypes(mOldType, mTypeCombo);
            mTypeCombo.select(0);

            setControl(composite);
            validatePage();

            mTypeCombo.setFocus();
        }

        private List<String> getWidgetTypes(String oldType, Combo combo) {
            List<String> classNames = new ArrayList<String>();

            // Populate type combo
            Sdk currentSdk = Sdk.getCurrent();
            if (currentSdk != null) {
                IAndroidTarget target = currentSdk.getTarget(mProject);
                if (target != null) {
                    // Try to pick "related" widgets to the one you have selected.
                    // For example, for an AnalogClock, display DigitalClock first.
                    // For a Text, offer EditText, AutoComplete, etc.
                    if (oldType != null) {
                        ViewMetadataRepository repository = ViewMetadataRepository.get();
                        List<String> relatedTo = repository.getRelatedTo(oldType);
                        if (relatedTo.size() > 0) {
                            for (String className : relatedTo) {
                                String base = className.substring(className.lastIndexOf('.') + 1);
                                combo.add(base);
                                classNames.add(className);
                            }
                            combo.add(SEPARATOR_LABEL);
                            classNames.add(null);
                        }
                    }

                    Pair<List<String>,List<String>> result =
                        CustomViewFinder.findViews(mProject, false);
                    List<String> customViews = result.getFirst();
                    List<String> thirdPartyViews = result.getSecond();
                    if (customViews.size() > 0) {
                        for (String view : customViews) {
                            combo.add(view);
                            classNames.add(view);
                        }
                        combo.add(SEPARATOR_LABEL);
                        classNames.add(null);
                    }

                    if (thirdPartyViews.size() > 0) {
                        for (String view : thirdPartyViews) {
                            combo.add(view);
                            classNames.add(view);
                        }
                        combo.add(SEPARATOR_LABEL);
                        classNames.add(null);
                    }

                    AndroidTargetData targetData = currentSdk.getTargetData(target);
                    if (targetData != null) {
                        // Now add ALL known layout descriptors in case the user has
                        // a special case
                        List<ViewElementDescriptor> descriptors =
                            targetData.getLayoutDescriptors().getViewDescriptors();
                        for (ViewElementDescriptor d : descriptors) {
                            String className = d.getFullClassName();
                            if (className.equals(VIEW_INCLUDE)
                                    || className.equals(VIEW_FRAGMENT)
                                    || className.equals(REQUEST_FOCUS)) {
                                continue;
                            }
                            combo.add(d.getUiName());
                            classNames.add(className);

                        }
                    }
                }
            } else {
                combo.add("SDK not initialized");
                classNames.add(null);
            }

            return classNames;
        }

        @Override
        protected boolean validatePage() {
            boolean ok = true;
            int selectionIndex = mTypeCombo.getSelectionIndex();
            String type = selectionIndex != -1 ? mClassNames.get(selectionIndex) : null;
            if (type == null) {
                setErrorMessage("Select a widget type to convert to");
                ok = false; // The user has chosen a separator
            } else {
                setErrorMessage(null);
            }

            // Record state
            ChangeViewRefactoring refactoring =
                (ChangeViewRefactoring) getRefactoring();
            refactoring.setType(type);

            setPageComplete(ok);
            return ok;
        }
    }
}