/*******************************************************************************
* 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.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
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.TypedEvent;
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 org.eclipse.swt.widgets.Text;
import de.innovationgate.eclipse.utils.BeanListPreferencesStore;
import de.innovationgate.eclipse.utils.WorkbenchUtils;
import de.innovationgate.eclipse.wgadesigner.WGADesignerPlugin;
import de.innovationgate.eclipse.wgadesigner.models.DesignTemplate;
public class NewExternalWGADesignPage extends WizardPage {
private Text _designName;
private Combo _comboTemplateList;
private Map<String, DesignTemplate> _designTemplates = new HashMap<String, DesignTemplate>();
public NewExternalWGADesignPage() {
super("New external WGA Design");
setTitle("New external WGA Design");
setDescription("This wizard creates a new external WGA Design.");
setPageComplete(false);
}
public void createControl(Composite parent) {
initialize();
Composite container = new Composite(parent, SWT.NULL);
GridLayout layout = new GridLayout();
layout.numColumns = 2;
layout.verticalSpacing = 9;
container.setLayout(layout);
new Label(container, SWT.NULL).setText("Design name:");
_designName = new Text(container, SWT.BORDER);
GridData g = new GridData(GridData.FILL_HORIZONTAL);
g.horizontalSpan = 1;
_designName.setLayoutData(g);
_designName.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
dialogChanged(e);
}
});
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);
}
});
setControl(container);
}
private void initialize() {
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) {
if (isEmpty(_comboTemplateList.getText())) {
applyStatus("Please choose a template for the new design.", IStatus.ERROR);
setPageComplete(false);
return;
}
if (!isEmpty(_designName.getText())) {
String designName = _designName.getText();
// validate design name
if (!WorkbenchUtils.isValidResourceName(designName)) {
applyStatus("Design name contains illegal characters. Only alphanumeric ascii characters and '_', '-' are allowed.", IStatus.ERROR);
setPageComplete(false);
return;
}
if (ResourcesPlugin.getWorkspace().getRoot().getProject(designName).exists()) {
applyStatus("A project with name '" + designName + "' already exists", IStatus.ERROR);
setPageComplete(false);
return;
}
} else {
applyStatus("Please define a design name.", IStatus.ERROR);
setPageComplete(false);
return;
}
applyStatus("", IStatus.OK);
setPageComplete(true);
}
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 _designName.getText();
}
public DesignTemplate getTemplate() {
return _designTemplates.get(_comboTemplateList.getText());
}
}