/*******************************************************************************
* 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.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.editors.text.EditorsUI;
import de.innovationgate.eclipse.editors.Plugin;
import de.innovationgate.eclipse.editors.ResourceIDs;
import de.innovationgate.eclipse.utils.WorkbenchUtils;
import de.innovationgate.eclipse.utils.ui.ValidatedMultiPageWizard;
import de.innovationgate.eclipse.utils.wga.WGADesignStructureHelper;
public class NewScriptModule extends ValidatedMultiPageWizard {
private IStructuredSelection _selection;
private boolean _scriptValidFolderSelected = false;
private NewScriptModuleFolderSelectionPage _folderSelectionPage;
private NewScriptModulePage _modulePage;
private final static Map<String,String> _fileExtToHeader = new HashMap<String, String>();
static {
_fileExtToHeader.put("css", Plugin.HEADER_CSS_MODUL);
_fileExtToHeader.put("js", Plugin.HEADER_JS_MODUL);
_fileExtToHeader.put("tmlscript", Plugin.HEADER_TMLSCRIPT_MODUL);
_fileExtToHeader.put("vbs", Plugin.HEADER_VBS_MODUL);
_fileExtToHeader.put("xml", Plugin.HEADER_XML_MODUL);
}
public NewScriptModule() {
super();
setWindowTitle("New script module");
}
@Override
public void addPages() {
if (!_scriptValidFolderSelected) {
_folderSelectionPage = new NewScriptModuleFolderSelectionPage("New script module", "Please select the design folder where you want to create the new script module", _selection);
addPage(_folderSelectionPage);
_modulePage = new NewScriptModulePage("New script module", "Please specify the properties for the new script module.");
addPage(_modulePage);
} else {
_modulePage = new NewScriptModulePage("New script module", "Please specify the properties for the new script module.", _selection);
addPage(_modulePage);
}
}
@Override
public boolean performFinish() {
IFile target = _modulePage.getTargetFile();
String header = Plugin.getDefault().getHeaderFileMap().get(_fileExtToHeader.get(target.getFileExtension())).getHeaderForProject(target.getProject());
if (_modulePage.getCopyFromFile() != null) {
try {
target.create(new ByteArrayInputStream((header + "\n").getBytes()), true, new NullProgressMonitor());
target.appendContents(Plugin.getDefault().getResourceAsStream(_modulePage.getCopyFromFile()), true, false, new NullProgressMonitor());
} catch (CoreException e) {
Plugin.getDefault().logError("Unable to create file " + target.getLocation(), e);
return false;
} catch (IOException e) {
Plugin.getDefault().logError("Unable to cpoy from template in file " + target.getLocation(), e);
return false;
}
}else{
try {
target.create(new ByteArrayInputStream(header.getBytes()), true, new NullProgressMonitor());
} catch (CoreException e) {
Plugin.getDefault().logError("Unable to create file " + target.getLocation(), e);
return false;
}
}
try {
if(target.getFileExtension().equals("tmlscript") || target.getFileExtension().equals("js") ){
WorkbenchUtils.openEditor(Plugin.getDefault().getWorkbench(), target, ResourceIDs.EDITOR_TMLSCRIPT);
}else{
WorkbenchUtils.openEditor(Plugin.getDefault().getWorkbench(), target, EditorsUI.DEFAULT_TEXT_EDITOR_ID);
}
} catch (PartInitException e) {
Plugin.getDefault().logWarning("Unable to open editor for file " + target.getLocation(), e);
}
return true;
}
public void init(IWorkbench workbench, IStructuredSelection selection) {
_selection = selection;
if (selection.getFirstElement() instanceof IContainer) {
IContainer container = (IContainer) selection.getFirstElement();
if (WGADesignStructureHelper.isValidScriptLocation(container)) {
_scriptValidFolderSelected = true;
}
} else if (selection.getFirstElement() instanceof IFile) {
IFile file = (IFile) selection.getFirstElement();
if (WGADesignStructureHelper.isValidScriptLocation(file.getParent())) {
_scriptValidFolderSelected = true;
}
}
}
}