blob: 315951cd65a63e29c1f0ff49827dd2ea647e9560 [file] [log] [blame]
/**
*
* Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation
*/
package org.eclipse.osbp.wizard.ui.basic;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.util.HashSet;
import java.util.Set;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkingSet;
import org.eclipse.ui.dialogs.WorkingSetGroup;
import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
import org.eclipse.ui.internal.ide.dialogs.ProjectContentsLocationArea;
import org.eclipse.xtext.ui.wizard.IProjectCreator;
import org.eclipse.xtext.ui.wizard.IProjectInfo;
import com.axelerator.studio.rc.util.ui.ImageCombo;
public abstract class BasicNewProjectWizard extends org.eclipse.xtext.ui.wizard.XtextNewProjectWizard {
public final CommonProjectWizardTranslator translator;
public BasicNewProjectWizard(IProjectCreator creator, CommonProjectWizardTranslator translator) {
super(creator);
this.translator = translator;
// try {
// ImageDescriptor desc = IDEWorkbenchPlugin.imageDescriptorFromPlugin(
// "org.eclipse.osbp.wizard.ui",
// "/icons/osbp-logo-156x66.png");//$NON-NLS-1$
// setDefaultPageImageDescriptor(desc);
// }
// catch (Exception e) {} // NOP
}
@Override
protected void doFinish(final IProjectInfo projectInfo, final IProgressMonitor monitor) {
try {
getCreator().setProjectInfo(projectInfo);
getCreator().run(monitor);
// fileOpener.selectAndReveal(creator.getResult());
// fileOpener.openFileToEdit(getShell(), creator.getResult());
}
catch (final InvocationTargetException e) {
// logger.error(e.getMessage(), e);
}
catch (final InterruptedException e) {
// cancelled by user, ok
return;
}
}
/**
* Use this method to add pages to the wizard.
* The one-time generated version of this class will add a default new project page to the wizard.
*/
public abstract void addPages();
@Override
public void createPageControls(Composite pageContainer) {
super.createPageControls(pageContainer);
addListenerToComponents();
}
/**
* Use this method to read the project settings from the wizard pages and feed them into the project info class.
*/
@Override
protected abstract IProjectInfo getProjectInfo();
public BasicProjectInfo projectInfo;
protected String initialProjectFieldValue;
// widgets
protected Text vendor;
public ImageCombo topLevelDomain;
public Text companyDomain;
public Text projectNameField;
public Combo masterTemplate;
public Text businessBundle;
public Text bundleName;
public Set<Listener> projectNameChanges = new HashSet<Listener>();
public Listener nameModifyListener = new Listener() {
public void handleEvent(Event e) {
setLocationForSelection();
IWizardPage current = BasicNewProjectWizard.this.getContainer().getCurrentPage();
if (current instanceof WizardPage) {
((WizardPage) current).setPageComplete(validatePage((WizardPage) current));
}
}
};
/**
* Set the location to the default location if we are set to useDefaults.
*/
protected void setLocationForSelection() {
locationArea.updateProjectName(getProjectNameFieldValue());
}
/**
* Returns the current project name as entered by the user, or its anticipated
* initial value.
*
* @return the project name, its anticipated initial value, or <code>null</code>
* if no project name is known
*/
public String getProjectName() {
if (projectNameField == null) {
return initialProjectFieldValue;
}
return getProjectNameFieldValue();
}
/**
* Returns the value of the project name field
* with leading and trailing spaces removed.
*
* @return the project name in the field
*/
protected String getProjectNameFieldValue() {
if (projectNameField == null) {
return ""; //$NON-NLS-1$
}
return projectNameField.getText().trim();
}
protected Listener vendorChanges = new Listener() {
public void handleEvent(Event e) {
projectInfo.setVendor(vendor.getText());
}
};
protected Listener masterTemplateChanges = new Listener() {
public void handleEvent(Event e) {
projectInfo.setMasterTemplate(masterTemplate.getItem(masterTemplate.getSelectionIndex()));
businessBundle.setText(projectInfo.getBusinessBundle());
}
};
protected ProjectContentsLocationArea locationArea;
protected WorkingSetGroup workingSetGroup;
/**
* Create a working set group for this page. This method can only be called
* once.
*
* @param composite
* the composite in which to create the group
* @param selection
* the current workbench selection
* @param supportedWorkingSetTypes
* an array of working set type IDs that will restrict what types
* of working sets can be chosen in this group
* @return the created group. If this method has been called previously the
* original group will be returned.
* @since 3.4
*/
public WorkingSetGroup createWorkingSetGroup(Composite composite,
IStructuredSelection selection, String[] supportedWorkingSetTypes) {
if (workingSetGroup != null)
return workingSetGroup;
workingSetGroup = new WorkingSetGroup(composite, selection,
supportedWorkingSetTypes);
return workingSetGroup;
}
protected void addListenerToComponents() {
if (vendor != null) {
vendor.addListener(SWT.Modify, vendorChanges);
}
for (Listener projectNameChange : projectNameChanges) {
if (topLevelDomain != null) {
topLevelDomain.addListener(SWT.Selection, projectNameChange);
}
if (companyDomain != null) {
companyDomain.addListener(SWT.Modify, projectNameChange);
}
}
if (masterTemplate != null) {
masterTemplate.addListener(SWT.Selection, masterTemplateChanges);
}
}
/**
* Returns the current project location path as entered by
* the user, or its anticipated initial value.
* Note that if the default has been returned the path
* in a project description used to create a project
* should not be set.
*
* @return the project location path or its anticipated initial value.
*/
public IPath getLocationPath() {
return new Path(locationArea.getProjectLocation());
}
/**
/**
* Returns the current project location URI as entered by
* the user, or <code>null</code> if a valid project location
* has not been entered.
*
* @return the project location URI, or <code>null</code>
* @since 3.2
*/
public URI getLocationURI() {
return locationArea.getProjectLocationURI();
}
/**
* Creates a project resource handle for the current project name field
* value. The project handle is created relative to the workspace root.
* <p>
* This method does not create the project resource; this is the
* responsibility of <code>IProject::create</code> invoked by the new
* project resource wizard.
* </p>
*
* @return the new project resource handle
*/
public IProject getProjectHandle() {
return ResourcesPlugin.getWorkspace().getRoot().getProject(
getProjectName());
}
/**
* Sets the initial project name that this page will use when
* created. The name is ignored if the createControl(Composite)
* method has already been called. Leading and trailing spaces
* in the name are ignored.
* Providing the name of an existing project will not necessarily
* cause the wizard to warn the user. Callers of this method
* should first check if the project name passed already exists
* in the workspace.
*
* @param name initial project name for this page
*
* @see IWorkspace#validateName(String, int)
*
*/
public void setInitialProjectName(String name) {
if (name == null) {
initialProjectFieldValue = null;
} else {
initialProjectFieldValue = name.trim();
if(locationArea != null) {
locationArea.updateProjectName(name.trim());
}
}
}
/**
* Returns whether this page's controls currently all contain valid
* values.
*
* @return <code>true</code> if all controls are valid, and
* <code>false</code> if at least one is invalid
*/
protected boolean validatePage(WizardPage wizardPage) {
if (wizardPage instanceof BasicNewProjectEclipsePage) {
IWorkspace workspace = IDEWorkbenchPlugin.getPluginWorkspace();
String projectFieldContents = getProjectNameFieldValue();
if (projectFieldContents.equals("")) { //$NON-NLS-1$
wizardPage.setErrorMessage(null);
wizardPage.setMessage(IDEWorkbenchMessages.WizardNewProjectCreationPage_projectNameEmpty);
return false;
}
IStatus nameStatus = workspace.validateName(projectFieldContents,
IResource.PROJECT);
if (!nameStatus.isOK()) {
wizardPage.setErrorMessage(nameStatus.getMessage());
return false;
}
IProject handle = getProjectHandle();
if (handle.exists()) {
wizardPage.setErrorMessage(IDEWorkbenchMessages.WizardNewProjectCreationPage_projectExistsMessage);
return false;
}
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(
getProjectNameFieldValue());
locationArea.setExistingProject(project);
String validLocationMessage = locationArea.checkValidLocation();
if (validLocationMessage != null) { // there is no destination location given
wizardPage.setErrorMessage(validLocationMessage);
return false;
}
}
wizardPage.setErrorMessage(null);
wizardPage.setMessage(null);
return true;
}
/**
* Return the selected working sets, if any. If this page is not configured
* to interact with working sets this will be an empty array.
*
* @return the selected working sets
* @since 3.4
*/
public IWorkingSet[] getSelectedWorkingSets() {
return workingSetGroup == null ? new IWorkingSet[0] : workingSetGroup
.getSelectedWorkingSets();
}
}