Package es.upm.dit.gsi.eclipse.jadex.adfmanager.wizards

Source Code of es.upm.dit.gsi.eclipse.jadex.adfmanager.wizards.AddCapabilityReferenceWizardPage

/*******************************************************************************
* Copyright (c) 2011 Grupo de Sistemas Inteligentes (GSI) - DIT UPM
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package es.upm.dit.gsi.eclipse.jadex.adfmanager.wizards;

import java.io.File;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.dialogs.IDialogPage;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
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.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
import org.eclipse.ui.dialogs.ISelectionStatusValidator;
import org.eclipse.ui.dialogs.ResourceSelectionDialog;
import org.eclipse.ui.model.BaseWorkbenchContentProvider;
import org.eclipse.ui.model.WorkbenchLabelProvider;

import es.upm.dit.gsi.jadex.customproject.natures.JadexProjectNature;
import org.eclipse.core.databinding.validation.ValidationStatus;


/**
* This page allows to get the following information in order to create a new reference to an existing capability
*  - Capability name
*  - Capability File
*   
@author Pablo Muñoz
*/

public class AddCapabilityReferenceWizardPage extends WizardPage {
  private Text capabilityNameText;
  private Text capabilityFileText;

  /**
   * Constructor for AgentWizardPage
   *
   * @param pageName
   */
  public AddCapabilityReferenceWizardPage(ISelection selection) {
    super("wizardPage");
    setTitle("Capability");
    setDescription("This wizard adds a reference to an existing capability in the selected Agent");
  }

  /**
   * @see IDialogPage#createControl(Composite)
   */
  public void createControl(Composite parent) {
    Composite container = new Composite(parent, SWT.NULL);
    GridLayout layout = new GridLayout();
    container.setLayout(layout);
    layout.numColumns = 3;
    layout.verticalSpacing = 9;
    PlatformUI.getWorkbench().getHelpSystem().setHelp(container, "es.upm.dit.gsi.eclipse.jadex.adfmanager.addCapabilityReferenceWizard");
   
    Label label = new Label(container, SWT.NULL);
    label.setText("Capability name:");
    capabilityNameText = new Text(container, SWT.BORDER | SWT.SINGLE);
    GridData gd = new GridData(GridData.FILL_HORIZONTAL);
    gd.horizontalSpan = 2;
    capabilityNameText.setLayoutData(gd);
    capabilityNameText.addModifyListener(new ModifyListener() {
      public void modifyText(ModifyEvent e) {
        dialogChanged();
      }
    });
    capabilityNameText.setToolTipText("choose a name of an existing capability");
   
   
    Label label2 = new Label(container, SWT.NULL);
    label2.setText("Capability definition file:");
    capabilityFileText = new Text(container, SWT.BORDER | SWT.SINGLE);
    gd = new GridData(GridData.FILL_HORIZONTAL);
    capabilityFileText.setLayoutData(gd);
    capabilityFileText.addModifyListener(new ModifyListener() {
      public void modifyText(ModifyEvent e) {
        dialogChanged();
      }
    });
    capabilityFileText.setToolTipText("click browse in order to select an existing capability file");
    Button button = new Button(container, SWT.PUSH);
    button.setText("Browse...");
    button.addSelectionListener(new SelectionAdapter(){
      public void widgetSelected(SelectionEvent e) {
        handleBrowse();
      }
    });
    initialize();
    dialogChanged();
    setControl(container);
  }

  /**
   * Tests if the current workbench selection is a suitable container to use.
   */
  private void initialize() {
  }

  /**
   * Ensures that the all text fields are set correctly.
   */
  private void dialogChanged() {
    File file = new File(getCapabilityFilePath());
    if(capabilityNameText.getText().length() == 0){
      updateStatus("capability name must be set");
      return;
    }
    if(capabilityFileText.getText().length() == 0){
      updateStatus("capability file must be set");
      return;
    }
    if(file != null && !file.exists()){
      updateStatus("The selected capability file doesn't exist");
      return;
    }
    if(!file.getName().endsWith("capability.xml")){
      updateStatus("Capability files must have the \"capability.xml\" extension");
      return;
    }
    updateStatus(null);
  }

  /*
   * This method is called in order to update the status message
   */
  private void updateStatus(String message) {
    setErrorMessage(message);
    setPageComplete(message == null);
  }
 
 
  /**
   *
   * @return the capability name
   */
  public String getCapabilityName() {
    return capabilityNameText.getText();
  }

  /**
   *
   * @return the path to the capability definition file
   */
  public String getCapabilityFilePath() {
    return capabilityFileText.getText();
  }

  private void handleBrowse() {   
    ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(
          getShell(), new WorkbenchLabelProvider(), new BaseWorkbenchContentProvider());
      dialog.setTitle("Select target Capability");
      dialog.setMessage("Select the target capability file:");
      dialog.setInput(ResourcesPlugin.getWorkspace().getRoot());
      dialog.setAllowMultiple(false);
      dialog.addFilter(new ViewerFilter() {       
        @Override
        public boolean select(Viewer viewer, Object parentElement, Object element) {
          if(element instanceof IProject){
            IProject p = (IProject) element;
            try{
              if(p.hasNature(JadexProjectNature.NATURE_ID)){
                return true;
              }
              else{
                return false;
              }
            }
            catch(CoreException e1){
              return false;
            }
          }
          else{
            if(element instanceof IFile){
              IFile file = (IFile) element;
              try{
                if(file.getName().endsWith("capability.xml")){
                  return true;
                }
                else{
                  return false;
                }
              }
              catch(Exception e1){
                return false;
              }
            }
           
          }
          return true;
        }
      });
      dialog.setValidator(new ISelectionStatusValidator() {
       
        @Override
        public IStatus validate(Object[] selection) {
          if(selection.length == 1){
            if(selection[0] instanceof IFile){
              IFile fileSel = (IFile) selection[0];
              if(fileSel.getName().endsWith("capability.xml")){
                return ValidationStatus.ok();
              }
            }
          }
          return ValidationStatus.error("Invalid selection");
        }
      });
    if (dialog.open() == ResourceSelectionDialog.OK) {
      Object[] result = dialog.getResult();
      IFile f = (IFile) result[0];
      capabilityFileText.setText(f.getRawLocation().toOSString());
      dialogChanged();
    }
  }
}
TOP

Related Classes of es.upm.dit.gsi.eclipse.jadex.adfmanager.wizards.AddCapabilityReferenceWizardPage

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.