RenameTypeInputPage.java :  » Workflow-Engines » osbl-1_0 » gui » wizards » Java Open Source

Java Open Source » Workflow Engines » osbl 1_0 
osbl 1_0 » gui » wizards » RenameTypeInputPage.java
/**
 * 
 */
package gui.wizards;

import gui.RefactoringPlugin;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.jdt.core.JavaConventions;
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.ltk.ui.refactoring.UserInputWizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import core.RefactorInfo;

/** <p>the input page for the Rename Type refactoring, where users can
 * control the effects of the refactoring; to be shown in the wizard.</p>
 *
 * <p>We let the user enter the new name for the property, and we let her 
 * decide whether other property files in the bundle should be affected, and
 * whether the operation is supposed to span the entire workspace or only 
 * the current project.</p>
 * 
 * @author sh
 */
public class RenameTypeInputPage extends UserInputWizardPage {
  
  private static final String DS_KEY = RenameTypeInputPage.class.getName();
  private static final String DS_UPDATE_BUNDLE = "UPDATE_BUNDLE";
  private static final String DS_ALL_PROJECTS  = "ALL_PROJECTS";
  
  private final RefactorInfo info;
    
  private IDialogSettings dialogSettings;  
  private Text txtVarName;
  private Combo comboNewType;

  public static final String[] TYPES = new String[] { "boolean",
                            "byte",
                            "char",
                            "String",
                            "short",
                            "int",
                            "long",
                            "float",
                            "double"};
  
  public RenameTypeInputPage(final RefactorInfo info) {
    super(RenameTypeInputPage.class.getName());
      this.info = info;
      //initDialogSettings();
  }
  
  /**
   * interface methods of UserInputWizardPage
   */
  public void createControl(final Composite parent) {
    Composite composite = createRootComposite(parent);
      setControl(composite);
            
      createLblNewType(composite);
      createComboNewType(composite);
      createLblVarName(composite);
      createTxtVarName(composite);
      
      validateType();
  }
    
  /**
   * UI creation method
   */
  private Composite createRootComposite(final Composite parent) {
      Composite result = new Composite(parent, SWT.NONE);
      GridLayout gridLayout = new GridLayout(2, false);
      gridLayout.marginWidth = 10;
      gridLayout.marginHeight = 10;
      result.setLayout(gridLayout);
      initializeDialogUnits(result);
      Dialog.applyDialogFont(result);
      return result;
  }
  
  /**
   * UI creation method
   */
  private void createLblNewType(final Composite composite) {
    Label lblNewName = new Label(composite, SWT.NONE);
      lblNewName.setText("New type");
  }
  
  /**
   * UI creation method
   */
  private void createComboNewType(Composite composite) {
      comboNewType = new Combo(composite, SWT.SIMPLE);
      comboNewType.setItems(TYPES);
      
      String oldType = info.getOldName();
      for (int i = 0; i < TYPES.length; i++) {
      String element = TYPES[i];
      if (element.contains(oldType)) {
        comboNewType.select(i);    
      }
    }
      
      comboNewType.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
      comboNewType.addKeyListener(new KeyAdapter() {
        public void keyReleased(final KeyEvent e) {
          info.setNewName(comboNewType.getText());
          validateType();
        }
      });
      comboNewType.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        info.setNewName(comboNewType.getText());
        validateType();
      }
      });
  }
  
  /**
   * UI creation method
   */
  private void createLblVarName(final Composite composite) {
    Label lblNewName = new Label(composite, SWT.NONE);
      lblNewName.setText("name");
  }
  
  /**
   * UI creation method
   */
  private void createTxtVarName(Composite composite) {
      txtVarName = new Text(composite, SWT.BORDER);
      txtVarName.setText(info.getOldVarName());
      txtVarName.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
      txtVarName.selectAll();
      txtVarName.addKeyListener(new KeyAdapter() {
        public void keyReleased(final KeyEvent e) {
          info.setNewVarName(txtVarName.getText());
          validateFieldName();
        }
      });
  }
  
  /**
   * helper method
   */  
  private void initDialogSettings() {
    IDialogSettings ds = RefactoringPlugin.getDefault().getDialogSettings();
      dialogSettings = ds.getSection(DS_KEY);
      if (dialogSettings == null) {
        dialogSettings = ds.addNewSection(DS_KEY);
        // init default values
        dialogSettings.put(DS_UPDATE_BUNDLE, true);
        dialogSettings.put(DS_ALL_PROJECTS, false);
      }
  }
      
  /**
   * helper method
   */
  private void validateType() {
    String typeTxt = comboNewType.getText();
    
    // check if the selected value exists in the default type collection
    boolean contained = false;
    for (int j = 0; j < TYPES.length; j++) {
      String type = TYPES[j];
      if (type.equals(typeTxt)) contained = true;
    }
    
    // if not, check if the current value is valid
    if (!contained) {
      IStatus status = JavaConventions.validateJavaTypeName(typeTxt, CompilerOptions.VERSION_1_3,CompilerOptions.VERSION_1_3);
      if (status.isOK()) {
        setPageComplete(true);
          setMessage(null);
      }
      else if(status.ERROR == IStatus.ERROR) {
        setPageComplete(false);
        setMessage(status.getMessage());
      }  
    }
  }
  
  /**
   * helper method
   */
  private void validateFieldName() {
    String nameTxt = txtVarName.getText();
    IStatus fieldStatus = JavaConventions.validateFieldName(nameTxt, CompilerOptions.VERSION_1_3,CompilerOptions.VERSION_1_3);
    
    if(fieldStatus.isOK() && nameTxt.length() > 0) {
      setPageComplete(true);
      setMessage(null);
    }
    else if(fieldStatus.ERROR == IStatus.ERROR) {
      setPageComplete(false);
      setMessage(fieldStatus.getMessage());
    }
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.