/*******************************************************************************
* 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.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.swt.SWT;
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.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import de.innovationgate.eclipse.editors.Plugin;
import de.innovationgate.eclipse.utils.ui.FolderSelectionPage;
import de.innovationgate.eclipse.utils.ui.ValidatedWizardPage;
import de.innovationgate.eclipse.utils.wga.WGADesignStructureHelper;
public class NewScriptModulePage extends ValidatedWizardPage {
private IContainer _selectedContainer;
private FolderSelectionPage _selectionPage;
private Text _moduleName;
private Text _txtTargetLocation;
private Text _typeLable;
private IFile _scriptFile;
private Group _tmlscriptOptionsGrp;
private List<Button> _scriptOptionsButtons = new ArrayList<Button>();
public static ArrayList<String> _scriptTypes = new ArrayList<String>();
public static Map<String, String> _defaultTMLScripts = new HashMap<String, String>();
private static String DEFAULT_HDB_LISTENER = "create hdblistener";
private static String DEFAULT_MASTER_ACTION = "create master action";
private static String EMPTY = "new file(blank)";
static {
_scriptTypes.add("css");
_scriptTypes.add("js");
_scriptTypes.add("tmlscript");
_scriptTypes.add("vbs");
_scriptTypes.add("xml");
_defaultTMLScripts.put(EMPTY, null);
_defaultTMLScripts.put(DEFAULT_HDB_LISTENER, "/resources/templates/defaultHDBListener.tmlscript");
_defaultTMLScripts.put(DEFAULT_MASTER_ACTION, "/resources/templates/defaultMasterAction.tmlscript");
}
public NewScriptModulePage(String title, String description) {
super(title);
setTitle(title);
setPageComplete(false);
setDescription(description);
}
public NewScriptModulePage(String title, String description, IStructuredSelection selection) {
super(title);
setTitle(title);
setPageComplete(false);
setDescription(description);
if (selection != null && selection.getFirstElement() instanceof IResource) {
IResource selectedResource = (IResource) selection.getFirstElement();
if (selectedResource instanceof IContainer) {
_selectedContainer = (IContainer) selection.getFirstElement();
}
if(selectedResource instanceof IFile){
_selectedContainer = ((IFile) selection.getFirstElement()).getParent();
}
}
}
@Override
public void show() {
IWizardPage prevPage = getWizard().getPreviousPage(this);
if(prevPage != null){
if (prevPage instanceof FolderSelectionPage) {
_selectionPage = (FolderSelectionPage) prevPage;
}
if (_selectionPage.getSelectedResource() instanceof IContainer) {
_selectedContainer = (IContainer) _selectionPage.getSelectedResource();
}
if (_selectedContainer != null) {
_typeLable.setText(WGADesignStructureHelper.getScriptTypeOf(_selectedContainer));
}
if (WGADesignStructureHelper.getScriptTypeOf(_selectedContainer).equals("tmlscript")) {
_tmlscriptOptionsGrp.setVisible(true);
} else {
_tmlscriptOptionsGrp.setVisible(false);
}
_txtTargetLocation.setText(_selectedContainer.getFullPath().toString());
}
performValidation();
}
@Override
public void computeResponse() {
// TODO Auto-generated method stub
}
@Override
public List<IStatus> validate() {
List<IStatus> list = new ArrayList<IStatus>();
String nameOfmodelToCreate = _moduleName.getText();
if (nameOfmodelToCreate.equals("")) {
list.add(new Status(Status.ERROR, Plugin.PLUGIN_ID, "No name given."));
} else if (!WGADesignStructureHelper.isValidModuleName(nameOfmodelToCreate)) {
list.add(new Status(Status.ERROR, Plugin.PLUGIN_ID, "Invalid chars in modulename."));
} else {
if(nameOfmodelToCreate.contains(".")){
String suffix = nameOfmodelToCreate.substring(nameOfmodelToCreate.lastIndexOf(".")+1,nameOfmodelToCreate.length());
if(suffix.equals(_typeLable.getText())){
nameOfmodelToCreate = nameOfmodelToCreate.substring(0, nameOfmodelToCreate.lastIndexOf("."));
}
}
IFolder targetContainer = _selectedContainer.getFolder(new Path(nameOfmodelToCreate));
IFile targetFile = _selectedContainer.getFile(new Path(nameOfmodelToCreate + "." + _typeLable.getText()));
if (targetContainer.exists()) {
list.add(new Status(Status.ERROR, Plugin.PLUGIN_ID, "There is already a folder named \"" + nameOfmodelToCreate + "\" below \"" + _selectedContainer.getName() + "\"."));
} else if (targetFile.exists()) {
list.add(new Status(Status.ERROR, Plugin.PLUGIN_ID, "There is already a file named \"" + nameOfmodelToCreate + "\" below \"" + _selectedContainer.getName() + "\"."));
} else {
_scriptFile = targetFile;
}
}
return list;
}
public void createControl(Composite parent) {
Composite container = new Composite(parent, SWT.NULL);
GridLayout layout = new GridLayout();
layout.numColumns = 2;
container.setLayout(layout);
GridData txtStyle = new GridData(GridData.FILL_HORIZONTAL | SWT.LEFT);
Display display = getWizard().getContainer().getShell().getDisplay();
new Label(container, SWT.NONE).setText("Module name: ");
_moduleName = new Text(container, SWT.BORDER);
_moduleName.addModifyListener(this);
_moduleName.setLayoutData(txtStyle);
new Label(container, SWT.None).setText("Target location: ");
_txtTargetLocation = new Text(container, SWT.READ_ONLY);
_txtTargetLocation.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
GridData innerLayout = new GridData(GridData.FILL_HORIZONTAL);
_txtTargetLocation.setLayoutData(innerLayout);
if(_selectedContainer!=null){
_txtTargetLocation.setText(_selectedContainer.getFullPath().toString());
}
new Label(container, SWT.NONE).setText("Script type: ");
_typeLable = new Text(container, SWT.READ_ONLY);
_typeLable.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
_tmlscriptOptionsGrp = new Group(container, SWT.None);
_tmlscriptOptionsGrp.setText("tmlscript options:");
GridLayout groupLayout = new GridLayout();
groupLayout.numColumns = 1;
_tmlscriptOptionsGrp.setLayout(groupLayout);
GridData groupLayoutData = new GridData(GridData.FILL_HORIZONTAL);
groupLayoutData.horizontalSpan = 3;
_tmlscriptOptionsGrp.setLayoutData(groupLayoutData);
for (String current : _defaultTMLScripts.keySet()) {
Button currrentButton = new Button(_tmlscriptOptionsGrp, SWT.RADIO);
currrentButton.setText(current);
if (current.equals(EMPTY)) {
currrentButton.setSelection(true);
}
_scriptOptionsButtons.add(currrentButton);
}
if (_selectedContainer != null) {
_typeLable.setText(WGADesignStructureHelper.getScriptTypeOf(_selectedContainer));
if (WGADesignStructureHelper.getScriptTypeOf(_selectedContainer).equals("tmlscript")) {
_tmlscriptOptionsGrp.setVisible(true);
} else {
_tmlscriptOptionsGrp.setVisible(false);
}
}
setControl(container);
}
public IFile getTargetFile() {
return _scriptFile;
}
public String getCopyFromFile() {
for (Button current : _scriptOptionsButtons) {
if (current.getSelection()) {
return _defaultTMLScripts.get(current.getText());
}
}
return null;
}
}