Package de.innovationgate.eclipse.wgadesigner.wizards

Source Code of de.innovationgate.eclipse.wgadesigner.wizards.NewWGADesignPage

/*******************************************************************************
* Copyright (c) 2009, 2010 Innovation Gate GmbH.
* 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
*
* Contributors:
*     Innovation Gate GmbH - initial API and implementation
******************************************************************************/
package de.innovationgate.eclipse.wgadesigner.wizards;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
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.events.SelectionListener;
import org.eclipse.swt.events.TypedEvent;
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.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbenchWizard;
import org.eclipse.ui.forms.HyperlinkGroup;
import org.eclipse.ui.forms.events.HyperlinkAdapter;
import org.eclipse.ui.forms.events.HyperlinkEvent;
import org.eclipse.ui.forms.widgets.ImageHyperlink;

import de.innovationgate.eclipse.utils.BeanListPreferencesStore;
import de.innovationgate.eclipse.utils.WorkbenchUtils;
import de.innovationgate.eclipse.utils.ui.DefaultedText;
import de.innovationgate.eclipse.wgadesigner.WGADesignerPlugin;
import de.innovationgate.eclipse.wgadesigner.models.DesignTemplate;
import de.innovationgate.eclipse.wgadesigner.natures.IncompatibleWGAConfigVersion;
import de.innovationgate.eclipse.wgadesigner.natures.WGARuntime;
import de.innovationgate.wga.config.Domain;
import de.innovationgate.wga.config.WGAConfiguration;

public class NewWGADesignPage extends WizardPage {

  private static final String DEFAULT_DBKEY = "<default is design name>";
  private static final String DEFAULT_TITLE = "<default is design name>";

  private Text _txtDesignName;
  private Combo _comboRuntimeList;
  private Combo _comboTemplateList;
  private ISelection _selection;
  private WGARuntime _selectedRuntime;
  private Map<String, DesignTemplate> _designTemplates = new HashMap<String, DesignTemplate>();
  private Map<String, Domain> _domains = new HashMap<String, Domain>();
  private Button _chkCreateContentStore;
  private Group _grpCreateContentStore;
  private Text _txtDatabaseKey;
  private Combo _comboDomain;
  private Text _txtTitle;
  private Composite _container;

  public NewWGADesignPage(ISelection selection) {
    super("WGA Application");
    _selection = selection;
    setTitle("WGA Application");
    setDescription("This wizard creates a new WGA Design and associate it with an WGA Runtime.");
    setPageComplete(false);
  }

  public void createControl(Composite parent) {
    initialize();

    _container = new Composite(parent, SWT.NULL);
    GridLayout layout = new GridLayout();
    layout.numColumns = 3;
    layout.verticalSpacing = 9;
    _container.setLayout(layout);

    new Label(_container, SWT.NULL).setText("Name:");
    _txtDesignName = new Text(_container, SWT.BORDER);
    GridData g = new GridData(GridData.FILL_HORIZONTAL);
    g.horizontalSpan = 2;
    _txtDesignName.setLayoutData(g);

    _txtDesignName.addModifyListener(new ModifyListener() {
      public void modifyText(ModifyEvent e) {
        dialogChanged(e);
      }
    });
   
    new Label(_container, SWT.NULL).setText("Runtime: ");
    _comboRuntimeList = new Combo(_container, SWT.READ_ONLY);
    updateRuntimeCombo();

    _comboRuntimeList.addModifyListener(new ModifyListener() {
      public void modifyText(ModifyEvent e) {
        _selectedRuntime = WGADesignerPlugin.getAllRuntimes().get(_comboRuntimeList.getText());
        updateDomainCombo(_selectedRuntime);
        dialogChanged(e);
      }
    });

    HyperlinkGroup linkGroup = new HyperlinkGroup(_container.getDisplay());
    ImageHyperlink linkCreateNewRuntime = new ImageHyperlink(_container, SWT.NONE);
    linkCreateNewRuntime.setImage(WGADesignerPlugin.getDefault().getImageRegistry().get(WGADesignerPlugin.IMAGE_WGA_RUNTIME_ADD));
    linkGroup.add(linkCreateNewRuntime);
    linkCreateNewRuntime.setText("create new WGA Runtime Project");
    linkCreateNewRuntime.addHyperlinkListener(new HyperlinkAdapter() {

      @Override
      public void linkActivated(HyperlinkEvent e) {
        try {
          IWorkbenchWizard wizard = WorkbenchUtils.openWizard(WGADesignerPlugin.getDefault().getWorkbench(), de.innovationgate.eclipse.wgadesigner.ResourceIDs.WIZARD_NEW_WGA_RUNTIME);
          if (wizard != null) {
            NewWGARuntime runtimeWizard = (NewWGARuntime) wizard;
            _selectedRuntime = runtimeWizard.getCreatedRuntime();
            updateRuntimeCombo();           
          }                   
        } catch (CoreException e1) {
          WorkbenchUtils.showErrorDialog(WGADesignerPlugin.getDefault(), getShell(), "Open runtime wizard failed", "Unable to open 'New Runtime' wizard.", e1);
        }
      }
    });
     
   
    new Label(_container, SWT.NULL).setText("Use template:");
    _comboTemplateList = new Combo(_container, SWT.READ_ONLY);

    ArrayList<String> designTemplateNames = new ArrayList<String>(_designTemplates.keySet());
    Collections.sort(designTemplateNames,  new Comparator<String>() {

      public int compare(String o1, String o2) {
        if (o1.equals("empty")) {
          return -1;
        } else if (o2.equals("empty")) {
          return 1;
        } else {
          return o1.compareTo(o2);
        }
       
      }
     
    });
    _comboTemplateList.setItems(designTemplateNames.toArray(new String[0]));
    _comboTemplateList.select(0);
    _comboTemplateList.addModifyListener(new ModifyListener() {
      public void modifyText(ModifyEvent e) {
        dialogChanged(e);
      }
    });
    GridData comboTemplateData = new GridData();
    comboTemplateData.horizontalSpan = 2;
    _comboTemplateList.setLayoutData(comboTemplateData);
   
    Label label = new Label(_container, SWT.NONE);
    label.setText("Register as new application:");
    _chkCreateContentStore = new Button(_container, SWT.CHECK);
    _chkCreateContentStore.setText("yes");
    _chkCreateContentStore.setSelection(true);
   
    _chkCreateContentStore.addSelectionListener(new SelectionAdapter() {

      @Override
      public void widgetSelected(SelectionEvent e) {
        super.widgetSelected(e);
        _grpCreateContentStore.setVisible(!_grpCreateContentStore.isVisible());
        dialogChanged(e);
      }
     
    });
   
    _grpCreateContentStore = new Group(_container, SWT.SHADOW_NONE);
    GridData grpData = new GridData(GridData.FILL_HORIZONTAL);
    grpData.horizontalSpan = 3;
    _grpCreateContentStore.setLayoutData(grpData);
    _grpCreateContentStore.setText("Contentstore options");
   
    _grpCreateContentStore.setLayout(new GridLayout(2, false));
    GridData innerGrpData = new GridData(GridData.FILL_HORIZONTAL);
    label = new Label(_grpCreateContentStore, SWT.NONE);
    label.setText("Database key:");
    _txtDatabaseKey = new Text(_grpCreateContentStore, SWT.BORDER);
    DefaultedText dflHandler = new DefaultedText(DEFAULT_DBKEY);
    dflHandler.apply(_txtDatabaseKey);
    _txtDatabaseKey.setLayoutData(GridDataFactory.copyData(innerGrpData));
    _txtDatabaseKey.addModifyListener(new ModifyListener() {

      public void modifyText(ModifyEvent e) {
        dialogChanged(e);
      }
     
    });
   
    label = new Label(_grpCreateContentStore, SWT.NONE);
    label.setText("Title:");
    _txtTitle = new Text(_grpCreateContentStore, SWT.BORDER);
    dflHandler = new DefaultedText(DEFAULT_TITLE);
    dflHandler.apply(_txtTitle);
    _txtTitle.setLayoutData(GridDataFactory.copyData(innerGrpData));
    _txtTitle.addModifyListener(new ModifyListener() {

      public void modifyText(ModifyEvent e) {
        dialogChanged(e);
      }
     
    });
   
    label = new Label(_grpCreateContentStore, SWT.NONE);
    label.setText("Domain:");
    _comboDomain = new Combo(_grpCreateContentStore, SWT.READ_ONLY);   
    updateDomainCombo(WGADesignerPlugin.getAllRuntimes().get(_comboRuntimeList.getText()));   
    _comboDomain.addSelectionListener(new SelectionListener() {

      public void widgetDefaultSelected(SelectionEvent e) {
        dialogChanged(e);       
      }

      public void widgetSelected(SelectionEvent e) {
        dialogChanged(e);       
      }
     
    });
   
    setControl(_container);
  }

  private void updateRuntimeCombo() {
    ArrayList<String> projectnames = new ArrayList<String>(WGADesignerPlugin.getAllRuntimes().keySet());
    Collections.sort(projectnames);
    _comboRuntimeList.setItems(projectnames.toArray(new String[0]));
    if (_selectedRuntime != null) {
      _comboRuntimeList.select(projectnames.indexOf(_selectedRuntime.getName()));
    } else {
      _comboRuntimeList.select(0);
    }
    _comboRuntimeList.computeSize(SWT.DEFAULT,SWT.DEFAULT,true);
    _comboRuntimeList.layout();
    _container.layout();
  }

  private void updateDomainCombo(WGARuntime runtime) {
    if (runtime != null) {
      _domains.clear();
      WGAConfiguration config;
      try {
        config = runtime.retrieveWGAConfig(false);
        if (config != null) {
          Iterator<Domain> domains = config.getDomains().iterator();
          while (domains.hasNext()) {
            Domain domain = domains.next();
            _domains.put(domain.getName(), domain);
          }
        }
      } catch (IncompatibleWGAConfigVersion e) {
      } catch (IOException e) {
      }
      List<String> domainItems = new ArrayList<String>();
      domainItems.addAll(_domains.keySet());
      Collections.sort(domainItems);
      String[] domainNames = domainItems.toArray(new String[0]);
      _comboDomain.setItems(domainNames);
      // compute index of default domain
      for (int i=0; i < domainNames.length; i++) {
        Domain domain = _domains.get(domainNames[i]);
        if (WGAConfiguration.UID_DEFAULT_DOMAIN.equals(domain.getUid())) {
          _comboDomain.select(i);
          break;
        }
      }
      _comboDomain.computeSize(SWT.DEFAULT,SWT.DEFAULT,true);
      _comboDomain.layout();
      _grpCreateContentStore.layout();
    }
  }

  private void initialize() {
    if (_selection instanceof IStructuredSelection) {
      IStructuredSelection treeSel = (IStructuredSelection) _selection;
      Object selectedElement = treeSel.getFirstElement();

      if (selectedElement instanceof IResource) {
        IProject selectedRuntimeProject = ((IResource)selectedElement).getProject();
        try {
          _selectedRuntime = (WGARuntime) selectedRuntimeProject.getNature(WGADesignerPlugin.NATURE_WGA_RUNTIME);
        } catch (CoreException e) {
          WGADesignerPlugin.getDefault().logError("This project does not exist. This project is not open. The project nature extension could not be found.", e);
        }
      }
    }
   
    BeanListPreferencesStore<DesignTemplate> templateStore = WGADesignerPlugin.getDefault().getDesginTemplateStore();
    templateStore.load();
    Iterator<DesignTemplate> DesignTemplateIterator = templateStore.getBeans().iterator();
    DesignTemplate desginTemplate;

    while (DesignTemplateIterator.hasNext()) {
      desginTemplate = DesignTemplateIterator.next();
      _designTemplates.put(desginTemplate.getName(), desginTemplate);
    }
  }
 

  public void dialogChanged(TypedEvent event) {
    applyStatus("", IStatus.OK);
    setPageComplete(true);
   
    if (_comboRuntimeList.getText() == null || _comboRuntimeList.getText().trim().equals("")) {
      applyStatus("Please select a WGA runtime.", IStatus.ERROR);     
      setPageComplete(false);
      return;
    }

   
    if (isEmpty(_comboTemplateList.getText())) {
      applyStatus("Please select a template.", IStatus.ERROR);     
      setPageComplete(false);
      return;
    }
   

    if (!isEmpty(_txtDesignName.getText())) {
      String designName = _txtDesignName.getText();
     
      // validate designname
      if (!WorkbenchUtils.isValidResourceName(designName)) {
        applyStatus("Design name contains illegal characters. Only alphanumeric ascii characters and '_', '-' are allowed.", IStatus.ERROR);
        setPageComplete(false);
        return;
      }
     
      if (getRuntime() != null && getRuntime().hasDesign(_txtDesignName.getText())) {
        applyStatus("Design '" + designName + "' already exists in runtime '" + getRuntime().getName() + "'.", IStatus.ERROR);
        setPageComplete(false);
        return;               
      }
    } else {
      applyStatus("Please define a design name.", IStatus.ERROR);
      setPageComplete(false);
      return;
    }
   
    if (_chkCreateContentStore.getSelection()) {
      // validate required content store fields
      if (!_txtDatabaseKey.getText().trim().equals(DEFAULT_DBKEY) && isEmpty(_txtDatabaseKey.getText())) {
        applyStatus("Please define a database key for the new application.", IStatus.ERROR);
        setPageComplete(false);
        return;
      } else if (!_txtDatabaseKey.getText().trim().equals(DEFAULT_DBKEY)) {
        if (!WorkbenchUtils.isValidResourceName(_txtDatabaseKey.getText())) {
          applyStatus("Database key name contains illegal characters. Only alphanumeric ascii characters and '_', '-' are allowed.", IStatus.ERROR);
          setPageComplete(false);
          return;
        }
      }
     
      if (!_txtTitle.getText().trim().equals(DEFAULT_TITLE) && isEmpty(_txtTitle.getText())) {
        applyStatus("Please define a title for the new application.", IStatus.ERROR);
        setPageComplete(false);
        return;
      }
     
      if (_comboDomain.getSelectionIndex() == -1) {
        applyStatus("Please select the domain for the new application.", IStatus.ERROR);
        setPageComplete(false);
        return;
      }
     
     
      // check if contentstore already exists
      if (_selectedRuntime != null) {
        WGAConfiguration config = null;
        try {
          config = _selectedRuntime.retrieveWGAConfig(false);
        } catch (IncompatibleWGAConfigVersion e) {
        } catch (IOException e) {
        }
        if (config != null) {
          if (config.hasContentDatabase(getDatabaseKey())) {
            applyStatus("A database with key '" + getDatabaseKey() + "' already exists in the specified WGA runtime.", IStatus.ERROR);
            setPageComplete(false);
            return;
          }
        }
      }     
    } else {
      applyStatus("The new design will not be registered as application. You will have to manually assign the design to a contentstore in order to use it.", IStatus.WARNING);
    }

  }

  private boolean isEmpty(String value) {
    if (value != null) {
      return value.length() == 0;
    } else {
      return true;
    }
  }

  private void applyToStatusLine(IStatus status) {
    String message = status.getMessage();

    if (message.length() == 0)
      message = null;
    switch (status.getSeverity()) {
      case IStatus.OK:
        setErrorMessage(null);
        setMessage(message);
        break;
      case IStatus.WARNING:
        setErrorMessage(null);
        setMessage(message, WizardPage.WARNING);
        break;
      case IStatus.INFO:
        setErrorMessage(null);
        setMessage(message, WizardPage.INFORMATION);
        break;
      default:
        setErrorMessage(message);
        setMessage(null);
        break;
    }
  }
 
    private void applyStatus(String message, int severity) {
      Status status = new Status(severity, WGADesignerPlugin.PLUGIN_ID, message);
      applyToStatusLine(status);
    }
 
  public String getDesignName() {
    return _txtDesignName.getText();
  }

  public WGARuntime getRuntime() {
    _selectedRuntime = WGADesignerPlugin.getAllRuntimes().get(_comboRuntimeList.getText());
    return _selectedRuntime;
  }

  public DesignTemplate getTemplate() {
    return _designTemplates.get(_comboTemplateList.getText());
  }

  public boolean isCreateContentStore() {
    return _chkCreateContentStore.getSelection();
  }
 
  public String getDatabaseKey() {
    if (_txtDatabaseKey.getText().equals(DEFAULT_DBKEY)) {
      return _txtDesignName.getText().trim().toLowerCase();
    } else {
      return _txtDatabaseKey.getText().trim().toLowerCase();
    }
  }
 
  public Domain getDomain() {   
    return _domains.get(_comboDomain.getText());   
  }

  public String getDatabaseTitle() {
    if (_txtTitle.getText().trim().equals(DEFAULT_TITLE)) {
      return null;
    } else {
      return _txtTitle.getText().trim();
    }
  }
}
TOP

Related Classes of de.innovationgate.eclipse.wgadesigner.wizards.NewWGADesignPage

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.