Home | History | Annotate | Download | only in refactoring
      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.editors.layout.refactoring;
     18 
     19 import com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditorDelegate;
     20 import com.android.ide.eclipse.adt.internal.resources.ResourceNameValidator;
     21 import com.android.resources.ResourceType;
     22 
     23 import org.eclipse.core.resources.IFile;
     24 import org.eclipse.core.resources.IProject;
     25 import org.eclipse.swt.SWT;
     26 import org.eclipse.swt.layout.GridData;
     27 import org.eclipse.swt.layout.GridLayout;
     28 import org.eclipse.swt.widgets.Button;
     29 import org.eclipse.swt.widgets.Composite;
     30 import org.eclipse.swt.widgets.Label;
     31 import org.eclipse.swt.widgets.Text;
     32 
     33 class ExtractIncludeWizard extends VisualRefactoringWizard {
     34     public ExtractIncludeWizard(ExtractIncludeRefactoring ref, LayoutEditorDelegate editor) {
     35         super(ref, editor);
     36         setDefaultPageTitle(ref.getName());
     37     }
     38 
     39     @Override
     40     protected void addUserInputPages() {
     41         ExtractIncludeRefactoring ref = (ExtractIncludeRefactoring) getRefactoring();
     42         String initialName = ref.getInitialName();
     43         IFile sourceFile = ref.getSourceFile();
     44         addPage(new InputPage(mDelegate.getEditor().getProject(), sourceFile, initialName));
     45     }
     46 
     47     /** Wizard page which inputs parameters for the {@link ExtractIncludeRefactoring} operation */
     48     private static class InputPage extends VisualRefactoringInputPage {
     49         private final IProject mProject;
     50         private final IFile mSourceFile;
     51         private final String mSuggestedName;
     52         private Text mNameText;
     53         private Button mReplaceAllOccurrences;
     54 
     55         public InputPage(IProject project, IFile sourceFile, String suggestedName) {
     56             super("ExtractIncludeInputPage");
     57             mProject = project;
     58             mSourceFile = sourceFile;
     59             mSuggestedName = suggestedName;
     60         }
     61 
     62         @Override
     63         public void createControl(Composite parent) {
     64             Composite composite = new Composite(parent, SWT.NONE);
     65             composite.setLayout(new GridLayout(2, false));
     66 
     67             Label nameLabel = new Label(composite, SWT.NONE);
     68             nameLabel.setText("New Layout Name:");
     69             nameLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
     70 
     71             mNameText = new Text(composite, SWT.BORDER);
     72             mNameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
     73             mNameText.addModifyListener(mModifyValidateListener);
     74 
     75             mReplaceAllOccurrences = new Button(composite, SWT.CHECK);
     76             mReplaceAllOccurrences.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER,
     77                     false, false, 2, 1));
     78             mReplaceAllOccurrences.setText(
     79                     "Replace occurrences in all layouts with include to new layout");
     80             mReplaceAllOccurrences.setEnabled(true);
     81             mReplaceAllOccurrences.setSelection(true);
     82             mReplaceAllOccurrences.addSelectionListener(mSelectionValidateListener);
     83 
     84             // Initialize UI:
     85             if (mSuggestedName != null) {
     86                 mNameText.setText(mSuggestedName);
     87             }
     88 
     89             setControl(composite);
     90             validatePage();
     91         }
     92 
     93         @Override
     94         protected boolean validatePage() {
     95             boolean ok = true;
     96 
     97             String text = mNameText.getText().trim();
     98 
     99             if (text.length() == 0) {
    100                 setErrorMessage("Provide a name for the new layout");
    101                 ok = false;
    102             } else {
    103                 ResourceNameValidator validator = ResourceNameValidator.create(false, mProject,
    104                         ResourceType.LAYOUT);
    105                 String message = validator.isValid(text);
    106                 if (message != null) {
    107                     setErrorMessage(message);
    108                     ok = false;
    109                 }
    110             }
    111 
    112             if (ok) {
    113                 setErrorMessage(null);
    114 
    115                 // Record state
    116                 ExtractIncludeRefactoring refactoring =
    117                     (ExtractIncludeRefactoring) getRefactoring();
    118                 refactoring.setLayoutName(text);
    119                 refactoring.setReplaceOccurrences(mReplaceAllOccurrences.getSelection());
    120             }
    121 
    122             setPageComplete(ok);
    123             return ok;
    124         }
    125     }
    126 }
    127