/*******************************************************************************
* 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.editors.design.wizards;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import de.innovationgate.eclipse.editors.Plugin;
import de.innovationgate.eclipse.utils.ui.ValidatedWizardPage;
import de.innovationgate.eclipse.utils.wga.WGADesignStructureHelper;
public class CreateNewCMLayoutPage extends ValidatedWizardPage {
private static final String DEFAULT_OUTERLAYOUT_NAME = "layout";
private static final String DEFAULT_INNERLAYOUT_NAME = "standard";
private Text _txtOuterLayoutName;
private Text _txtInnerLayoutName;
public CreateNewCMLayoutPage() {
super("CMLayoutPage");
setTitle("Create new CM layout");
setDescription("Please specify names for the outer- and innerlayout.");
}
@Override
public void computeResponse() {
}
public String getOuterLayoutName() {
return _txtOuterLayoutName.getText();
}
public String getInnerLayoutName() {
return _txtInnerLayoutName.getText();
}
@Override
public List<IStatus> validate() {
List<IStatus> messages = new ArrayList<IStatus>();
String outerLayoutName = _txtOuterLayoutName.getText();
if (outerLayoutName == null || outerLayoutName.trim().equals("")) {
messages.add(new Status(Status.ERROR, Plugin.PLUGIN_ID, "Please define a name for the outerlayout."));
} else if (!WGADesignStructureHelper.isValidModuleName(outerLayoutName)) {
messages.add(new Status(Status.ERROR, Plugin.PLUGIN_ID, "The name for the outerlayout contains invalid characters. Only alphanumeric ascii characters and '.', '_', '-' and '$' are allowed."));
}
String innerLayoutName = _txtInnerLayoutName.getText();
if (innerLayoutName == null || innerLayoutName.trim().equals("")) {
messages.add(new Status(Status.ERROR, Plugin.PLUGIN_ID, "Please define a name for the innerlayout."));
} else if (!WGADesignStructureHelper.isValidModuleName(innerLayoutName)) {
messages.add(new Status(Status.ERROR, Plugin.PLUGIN_ID, "The name for the innerlayout contains invalid characters. Only alphanumeric ascii characters and '.', '_', '-' and '$' are allowed."));
}
// check if the resources already exist
IContainer designRoot = null;
if (getPreviousPage() instanceof DesignFolderSelectionPage) {
designRoot = (IContainer)((DesignFolderSelectionPage)getPreviousPage()).getSelectedResource();
}
if (designRoot == null) {
messages.add(new Status(Status.ERROR, Plugin.PLUGIN_ID, "No design folder selected."));
} else {
WGADesignStructureHelper helper = new WGADesignStructureHelper(designRoot);
IFolder html = helper.getTmlRoot().getFolder("html");
if (html.exists()) {
IFile outerLayoutFile = html.getFile(new Path(getOuterLayoutName() + ".tml"));
if (outerLayoutFile.exists()) {
messages.add(new Status(Status.ERROR, Plugin.PLUGIN_ID, "A module with name '" + outerLayoutName + "' already exists in '" + outerLayoutFile.getParent().getFullPath().toString() + "'."));
}
IFile innerLayoutFile = html.getFile(new Path("inner").append(getInnerLayoutName() + ".tml"));
if (innerLayoutFile.exists()) {
messages.add(new Status(Status.ERROR, Plugin.PLUGIN_ID, "A module with name '" + innerLayoutName + "' already exists in '" + innerLayoutFile.getParent().getFullPath().toString() + "'."));
}
}
}
return messages;
}
public void createControl(Composite parent) {
Composite container = new Composite(parent, SWT.NULL);
GridLayout layout = new GridLayout();
layout.numColumns = 2;
container.setLayout(layout);
new Label(container, SWT.NONE).setText("Module name for the outerlayout:");
_txtOuterLayoutName = new Text(container, SWT.BORDER);
_txtOuterLayoutName.setText(DEFAULT_OUTERLAYOUT_NAME);
_txtOuterLayoutName.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
_txtOuterLayoutName.addModifyListener(this);
new Label(container, SWT.NONE).setText("Module name for the innerlayout:");
_txtInnerLayoutName = new Text(container, SWT.BORDER);
_txtInnerLayoutName.setText(DEFAULT_INNERLAYOUT_NAME);
_txtInnerLayoutName.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
_txtInnerLayoutName.addModifyListener(this);
setControl(container);
}
@Override
public void show() {
performValidation();
}
}