/*******************************************************************************
* 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 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 de.innovationgate.eclipse.editors.Plugin;
import de.innovationgate.eclipse.editors.ResourceIDs;
import de.innovationgate.eclipse.utils.WorkbenchUtils;
import de.innovationgate.eclipse.utils.ui.SingleStructuredSelection;
import de.innovationgate.eclipse.utils.ui.ValidatedMultiPageWizard;
import de.innovationgate.eclipse.utils.ui.model.TMLFileMetadataModel;
import de.innovationgate.eclipse.utils.wga.WGADesignStructureHelper;
public class NewTMLModule extends ValidatedMultiPageWizard{
private NewTMLModulePage _tmlModulePage;
private IStructuredSelection _selection;
private NewTMLModuleFolderSelectionPage _tmlModuleSelectFolderPAge;
private boolean _tmlValidFolderSelected = false;
public NewTMLModule() {
super();
setWindowTitle("New TML module");
}
public boolean performFinish() {
IFile tmlFile = null;
try {
tmlFile = _tmlModulePage.getTmlFile();
boolean isDirectAccess = _tmlModulePage.getIsTmlFileDirectAccess();
boolean isCachable = _tmlModulePage.getIsTmlFileCachable();
String header = Plugin.getDefault().getHeaderFileMap().get(Plugin.HEADER_TML_MODUL).getHeaderForProject(tmlFile.getProject());
tmlFile.create(new ByteArrayInputStream(header.getBytes()), true, new NullProgressMonitor());
if(isCachable || isDirectAccess){
TMLFileMetadataModel model = new TMLFileMetadataModel(tmlFile);
model.setCachable(isCachable);
model.setDirectAccess(isDirectAccess);
model.saveChanges();
}
WorkbenchUtils.openEditor(Plugin.getDefault().getWorkbench(), tmlFile, ResourceIDs.EDITOR_TML);
try {
WorkbenchUtils.setNavigationViewSelection(new SingleStructuredSelection(tmlFile));
} catch (Exception e) {
}
return true;
} catch (CoreException e) {
Plugin.getDefault().logError("Can not create TML module at " + tmlFile.getLocation(), e);
return false;
} catch (IOException e) {
Plugin.getDefault().logError("Can not create metadata for TML module " + tmlFile.getLocation(), e);
return false;
}
}
//TODO put into abstract class ?
public void init(IWorkbench workbench, IStructuredSelection selection) {
_selection = selection;
if(selection.getFirstElement() instanceof IContainer){
IContainer container = (IContainer)selection.getFirstElement();
if(WGADesignStructureHelper.isValidTMLLocation(container)){
_tmlValidFolderSelected = true;
}
}else if(selection.getFirstElement() instanceof IFile){
IFile file = (IFile) selection.getFirstElement();
if(WGADesignStructureHelper.isValidTMLLocation(file.getParent())){
_tmlValidFolderSelected = true;
}
}
}
//TODO put into abstract class ?
public void addPages() {
if(!_tmlValidFolderSelected){
_tmlModuleSelectFolderPAge = new NewTMLModuleFolderSelectionPage("Create new TML module", "Please select the design folder where you want to create the new TML module.", _selection);
addPage(_tmlModuleSelectFolderPAge);
_tmlModulePage = new NewTMLModulePage("Create new TML module", "Please specify the properties for the new TML module.");
addPage(_tmlModulePage);
}else{
_tmlModulePage = new NewTMLModulePage("Create new TML module", "Please specify the properties for the new TML module.", _selection);
addPage(_tmlModulePage);
}
}
}