blob: 03774f1e259b3fd365f5b99eae177bf198b90beb [file] [log] [blame]
package org.eclipse.ogee.core.wizard.pages;
import java.io.File;
import java.net.URI;
import java.net.URL;
import java.util.Iterator;
import java.util.List;
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.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.ogee.core.Activator;
import org.eclipse.ogee.core.extensions.service.implementation.ServiceImplementationException;
import org.eclipse.ogee.core.nls.messages.FrameworkMessages;
import org.eclipse.ogee.core.wizard.common.CommonTargetRuntime;
import org.eclipse.ogee.core.wizard.common.CommonWizardBusinessData;
import org.eclipse.ogee.core.wizard.common.CommonWizardObject;
import org.eclipse.ogee.core.wizard.common.CommonWizardStructureData;
import org.eclipse.ogee.core.wizard.controller.WizardControllerException;
import org.eclipse.ogee.core.wizard.pages.help.HelpConstants;
import org.eclipse.ogee.utils.ProjectUtils;
import org.eclipse.ogee.utils.logger.Logger;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkingSet;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
import org.eclipse.ui.dialogs.ISelectionStatusValidator;
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.ui.internal.ide.dialogs.ProjectContentsLocationArea.IErrorMessageReporter;
import org.eclipse.ui.model.BaseWorkbenchContentProvider;
import org.eclipse.ui.model.WorkbenchLabelProvider;
/**
* Represents the page for the OData Service Implementation wizard.
*/
public class CommonServiceImplementationProjectPage extends WizardPage
{
public static final String PAGE_NAME = "CommonServiceImplementationProjectPage"; //$NON-NLS-1$
private static final String ORG_ECLIPSE_OGEE_CORE = "org.eclipse.ogee.core"; //$NON-NLS-1$
private static final String ORG_ECLIPSE_UI_HELP = "org.eclipse.ui.help"; //$NON-NLS-1$
private Composite inputGroup;
private Label projectNameLabel;
private Text projectNameText;
private Label pathLabel;
private Text pathText;
private Label serviceTypeLabel;
private Combo serviceTypeCombo;
private Combo versionTypeCombo;
private Label serviceNameLabel;
private Text serviceNameText;
public static final ImageDescriptor imageDescriptor;
private Composite container;
private Button projectBrowseButton;
private Button serviceInvisibleButton;
private CommonWizardBusinessData businessData = null;
private CommonWizardStructureData structureData = null;
private List<CommonTargetRuntime> listRuntimes = null;
private ProjectContentsLocationArea locationArea;
private boolean isValidated = false;
private WorkingSetGroup workingSetGroup;
static
{
URL imageUrl = Activator.getDefault().getBundle().getEntry(WizardPagesConstants.WIZARD_PAGE_BANNER_ICON);
imageDescriptor = ImageDescriptor.createFromURL(imageUrl);
}
public CommonServiceImplementationProjectPage(CommonWizardObject wizardObject)
{
super(PAGE_NAME);
this.businessData = wizardObject.getBusinessData();
this.structureData = wizardObject.getStructureData();
if (null != structureData.getPageTitle())
{
setTitle(structureData.getPageTitle());
}
else
{
setTitle(FrameworkMessages.ServiceImplementationProjectPage_0);
}
if (null != structureData.getPageDescription())
{
setDescription(structureData.getPageDescription());
}
else
{
setDescription(FrameworkMessages.ServiceImplementationWizard_5);
}
setPageComplete(false);
}
@Override
public void createControl(Composite parent)
{
try
{
this.container = new Composite(parent, SWT.NULL);
GridLayout gl_container = new GridLayout(1, false);
this.container.setLayout(gl_container);
GridData gridData = new GridData(SWT.LEFT, SWT.CENTER, true, true, 1, 1);
gridData.grabExcessHorizontalSpace = true;
this.container.setLayoutData(gridData);
createInputGroup(this.container);
setControl(this.container);
}
catch (Exception e)
{
setMessage(e.getLocalizedMessage(), ERROR);
}
}
/**
* 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;
}
/**
* /** Creates input group
*
* @param parent
*/
private void createInputGroup(Composite parent)
{
Composite projectInputGroup = new Composite(parent, SWT.NONE);
GridLayout gl_projectInputGroup = new GridLayout(3, false);
gl_projectInputGroup.marginWidth = 2;
projectInputGroup.setLayout(gl_projectInputGroup);
GridData gridData2 = new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1);
gridData2.grabExcessHorizontalSpace = true;
// projectInputGroup.setText(FrameworkMessages.ServiceImplementationProjectPage_LocationDetails);
projectInputGroup.setLayoutData(gridData2);
/*
* Composite projectInputGroup = new Composite(parent, SWT.NONE);
* GridLayout layout = new GridLayout(); layout.numColumns = 2;
* projectInputGroup.setLayout(layout);
* projectInputGroup.setLayoutData(new
* GridData(GridData.FILL_HORIZONTAL));
*/
// create project name label
this.projectNameLabel = new Label(projectInputGroup, SWT.NONE);
GridData projectNameLabelGridData = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
projectNameLabelGridData.widthHint = 90;
this.projectNameLabel.setLayoutData(projectNameLabelGridData);
this.projectNameLabel.setText(FrameworkMessages.ServiceImplementationProjectPage_3);
locationArea = new ProjectContentsLocationArea(getErrorReporter(), parent);
// create project name text
if (structureData.isBrowseButtonEnabled())
{
this.projectNameText = new Text(projectInputGroup, SWT.BORDER | SWT.READ_ONLY);
}
else
{
this.projectNameText = new Text(projectInputGroup, SWT.BORDER);
}
this.projectNameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
this.projectNameText.setFocus();
this.projectNameText.addModifyListener(new ModifyListener()
{
@Override
public void modifyText(ModifyEvent arg0)
{
String projectName = getProjectName();
businessData.setProjectName(projectName);
try
{
validatePage();
}
catch (Exception e)
{
Logger.getFrameworkLogger().logError(e);
}
}
});
// create project file browse button
this.projectBrowseButton = new Button(projectInputGroup, SWT.NONE);
this.projectBrowseButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
this.projectBrowseButton.setText(FrameworkMessages.BrowseButton);
if (structureData.isBrowseButtonEnabled())
{
this.projectBrowseButton.setVisible(true);
}
else
{
this.projectBrowseButton.setVisible(false);
}
this.projectBrowseButton.addSelectionListener(new SelectionListener()
{
@Override
public void widgetSelected(SelectionEvent arg0)
{
ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(getShell(),
new WorkbenchLabelProvider(), new BaseWorkbenchContentProvider());
dialog.setTitle(FrameworkMessages.ServiceImplementationProjectPage_5);
dialog.setMessage(FrameworkMessages.ServiceImplementationProjectPage_6);
dialog.setInput(ResourcesPlugin.getWorkspace().getRoot());
dialog.setHelpAvailable(false);
dialog.setValidator(new ISelectionStatusValidator()
{
@Override
public IStatus validate(Object[] args)
{
return new Status(IStatus.OK, Activator.PLUGIN_ID, null);
}
});
int returnCode = dialog.open();
if (returnCode == ElementTreeSelectionDialog.OK)
{
Object[] result = dialog.getResult();
if (result != null && result.length >= 1)
{
IProject projectFile = (IProject) result[0];
IPath odataFilePath = projectFile.getRawLocation();
businessData.setOdataFilePath(odataFilePath);
setProjectFilePath(projectFile);
}
}
}
@Override
public void widgetDefaultSelected(SelectionEvent arg0)
{
// do nothing
}
});
if (this.structureData.isServicePagePathRequired())
{
this.pathLabel = new Label(projectInputGroup, SWT.NONE);
this.pathLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
this.pathLabel.setText(FrameworkMessages.ServiceImplementationProjectPage_Path);
this.pathText = new Text(projectInputGroup, SWT.BORDER | SWT.READ_ONLY);
this.pathText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
this.pathText.setText(businessData.getProjectFilePath());
new Label(projectInputGroup, SWT.NONE);
}
// create files group
this.inputGroup = new Composite(parent, SWT.NONE);
GridLayout gl_inputGroup = new GridLayout(3, false);
gl_inputGroup.marginWidth = 2;
this.inputGroup.setLayout(gl_inputGroup);
GridData gridData = new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1);
gridData.grabExcessHorizontalSpace = true;
this.inputGroup.setLayoutData(gridData);
// create target runtime section if list of runtimes is not empty
listRuntimes = this.structureData.getTargetRuntimes();
if (null != listRuntimes)
{
Group targetRuntimeGroup = new Group(this.inputGroup, SWT.NONE);
GridLayout gl_targetRuntimeGroup = new GridLayout(3, false);
gl_targetRuntimeGroup.marginWidth = 2;
targetRuntimeGroup.setLayout(gl_targetRuntimeGroup);
GridData gridDataTargetRuntime = new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1);
gridDataTargetRuntime.grabExcessHorizontalSpace = true;
targetRuntimeGroup.setText(FrameworkMessages.ServiceImplementationProjectPage_TargetRuntime);
targetRuntimeGroup.setLayoutData(gridDataTargetRuntime);
this.serviceTypeLabel = new Label(targetRuntimeGroup, SWT.NONE);
GridData serviceLabelGridData = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
serviceLabelGridData.widthHint = 90;
this.serviceTypeLabel.setLayoutData(serviceLabelGridData);
this.serviceTypeLabel.setText(FrameworkMessages.ServiceImplementationProjectPage_Server);
this.versionTypeCombo = new Combo(targetRuntimeGroup, SWT.DROP_DOWN | SWT.BORDER | SWT.SINGLE
| SWT.READ_ONLY);
Iterator<CommonTargetRuntime> iterator = listRuntimes.iterator();
while (iterator.hasNext())
{
this.versionTypeCombo.add(iterator.next().getName());
}
this.versionTypeCombo.select(0);
this.versionTypeCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
for (int i = 0; i < listRuntimes.size(); i++)
{
if (this.versionTypeCombo.getText().equalsIgnoreCase(listRuntimes.get(i).getName()))
{
businessData.setVersionID(listRuntimes.get(i).getId());
}
}
this.versionTypeCombo.addModifyListener(new ModifyListener()
{
@Override
public void modifyText(ModifyEvent e)
{
for (int i = 0; i < listRuntimes.size(); i++)
{
if (versionTypeCombo.getText().equalsIgnoreCase(listRuntimes.get(i).getName()))
{
businessData.setVersionID(listRuntimes.get(i).getId());
}
}
}
});
this.serviceInvisibleButton = new Button(targetRuntimeGroup, SWT.NONE);
this.serviceInvisibleButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
this.serviceInvisibleButton.setText(FrameworkMessages.BrowseButton);
this.serviceInvisibleButton.setVisible(false);
}
// create service details section if either of the type or name is
// selected to be shown
if (structureData.isServicePageShowServiceDetailsServiceName()
|| structureData.isServicePageShowServiceDetailsVersion())
{
Group serviceDetailsGroup = new Group(this.inputGroup, SWT.NONE);
GridLayout gl_serviceDetailsGroup = new GridLayout(3, false);
gl_serviceDetailsGroup.marginWidth = 2;
serviceDetailsGroup.setLayout(gl_serviceDetailsGroup);
GridData gridData3 = new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1);
gridData3.grabExcessHorizontalSpace = true;
serviceDetailsGroup.setText(FrameworkMessages.ServiceImplementationProjectPage_ServiceDetails);
serviceDetailsGroup.setLayoutData(gridData3);
if (structureData.isServicePageShowServiceDetailsVersion())
{
this.serviceTypeLabel = new Label(serviceDetailsGroup, SWT.NONE);
GridData serviceTypeLabelGridData = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
serviceTypeLabelGridData.widthHint = 90;
this.serviceTypeLabel.setLayoutData(serviceTypeLabelGridData);
this.serviceTypeLabel.setText(FrameworkMessages.ServiceImplementationProjectPage_ServiceType);
this.serviceTypeCombo = new Combo(serviceDetailsGroup, SWT.DROP_DOWN | SWT.BORDER | SWT.SINGLE
| SWT.READ_ONLY);
this.serviceTypeCombo.add(FrameworkMessages.ServiceImplementationProjectPage_ODataVersion);
if (null != structureData.getServiceType())
{
if (structureData.getServiceType().equalsIgnoreCase(WizardPagesConstants.ODATA_V2))
{
this.serviceTypeCombo.select(0);
}
}
else
{
this.serviceTypeCombo.select(0);
}
this.serviceTypeCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
this.serviceInvisibleButton = new Button(serviceDetailsGroup, SWT.NONE);
this.serviceInvisibleButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
this.serviceInvisibleButton.setText(FrameworkMessages.BrowseButton);
this.serviceInvisibleButton.setVisible(false);
}
if (structureData.isServicePageShowServiceDetailsServiceName())
{
this.serviceNameLabel = new Label(serviceDetailsGroup, SWT.NONE);
GridData serviceNameLabelGridData = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
serviceNameLabelGridData.widthHint = 90;
this.serviceNameLabel.setLayoutData(serviceNameLabelGridData);
this.serviceNameLabel.setText(FrameworkMessages.ServiceImplementationProjectPage_ServiceName);
this.serviceNameText = new Text(serviceDetailsGroup, SWT.BORDER);
this.serviceNameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
this.serviceInvisibleButton = new Button(serviceDetailsGroup, SWT.NONE);
this.serviceInvisibleButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
this.serviceInvisibleButton.setText(FrameworkMessages.BrowseButton);
this.serviceInvisibleButton.setVisible(false);
}
}
createWorkingSetGroup(inputGroup, this.structureData.getSelection(), new String[]
{"org.eclipse.jdt.ui.JavaWorkingSetPage", "org.eclipse.pde.ui.pluginWorkingSet",
"org.eclipse.ui.resourceWorkingSetPage"});
}
/**
* Get an error reporter for the receiver.
*
* @return IErrorMessageReporter
*/
private IErrorMessageReporter getErrorReporter()
{
return new IErrorMessageReporter()
{
/*
* (non-Javadoc)
*
* @see
* org.eclipse.ui.internal.ide.dialogs.ProjectContentsLocationArea
* .IErrorMessageReporter#reportError(java.lang.String)
*/
public void reportError(String errorMessage, boolean infoOnly)
{
if (infoOnly)
{
setMessage(errorMessage, IStatus.INFO);
setErrorMessage(null);
}
else
{
setErrorMessage(errorMessage);
validatePageForWorkspace();
}
boolean valid = errorMessage == null;
if (valid)
{
valid = validatePageForWorkspace();
}
// setPageComplete(valid);
}
};
}
/*
* Setting the project file path
*/
private void setProjectFilePath(IProject projectFile)
{
String projectFilePath = projectFile.getFullPath().toOSString();
int sepIndex = projectFilePath.indexOf(File.separator);
projectFilePath = projectFilePath.substring(sepIndex + 1);
String[] projectFileName = projectFilePath.split("\\\\");
if (null != projectFileName[0])
{
projectNameText.setText(projectFileName[0]);
}
}
/**
* @return - the service name.
*/
private String getProjectName()
{
if (projectNameText == null)
{
return ""; //$NON-NLS-1$
}
return this.projectNameText.getText().trim();
}
/**
* 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());
}
/**
*
* @throws ServiceImplementationException
* @throws WizardControllerException
*/
private void validatePage() throws ServiceImplementationException, WizardControllerException
{
isValidated = false;
String projectName = getProjectName();
if (projectName == null || projectName.isEmpty())
{
setMessage(FrameworkMessages.ServiceImplementationProjectPage_13, ERROR);
setPageComplete(false);
return;
}
if (ProjectUtils.projectExists(projectName) && !structureData.isBrowseButtonEnabled())
{
setMessage(FrameworkMessages.ServiceImplementationProjectPage_1, ERROR);
setPageComplete(false);
return;
}
IStatus nameStatus = ResourcesPlugin.getWorkspace().validateName(projectName, IResource.PROJECT);
if (nameStatus.getSeverity() == IStatus.ERROR)
{
setMessage(nameStatus.getMessage(), ERROR);
setPageComplete(false);
return;
}
setMessage(null);
isValidated = true;
setPageComplete(false);
}
/**
* 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 validatePageForWorkspace()
{
IWorkspace workspace = IDEWorkbenchPlugin.getPluginWorkspace();
isValidated = false;
String projectFieldContents = getProjectName();
if (projectFieldContents.equals("")) { //$NON-NLS-1$ setErrorMessage(null);
setMessage(IDEWorkbenchMessages.WizardNewProjectCreationPage_projectNameEmpty);
setPageComplete(false);
return false;
}
IStatus nameStatus = workspace.validateName(projectFieldContents, IResource.PROJECT);
if (!nameStatus.isOK())
{
setErrorMessage(nameStatus.getMessage());
setPageComplete(false);
return false;
}
IProject handle = getProjectHandle();
if (handle.exists())
{
setErrorMessage(IDEWorkbenchMessages.WizardNewProjectCreationPage_projectExistsMessage);
setPageComplete(false);
return false;
}
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(getProjectName());
locationArea.setExistingProject(project);
String validLocationMessage = locationArea.checkValidLocation();
if (validLocationMessage != null)
{ // there is no destination location given
setErrorMessage(validLocationMessage);
setPageComplete(false);
return false;
}
setErrorMessage(null);
setMessage(null);
isValidated = true;
setPageComplete(false);
return true;
}
@Override
public void setVisible(boolean visible)
{
if (visible)
{
// set the matching help context to the page when it's visible
PlatformUI.getWorkbench().getHelpSystem().setHelp(getShell(), HelpConstants.SERVICE_IMPLEMENTATION_HELP);
}
super.setVisible(visible);
}
/**
* Returns the useDefaults.
*
* @return boolean
*/
public boolean useDefaults()
{
return locationArea.isDefault();
}
/**
* 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();
}
@Override
public boolean canFlipToNextPage()
{
if (isValidated)
{
return true;
}
else
{
return false;
}
}
@Override
public void dispose()
{
super.dispose();
if (null != inputGroup)
{
inputGroup = null;
}
if (null != projectNameLabel)
{
projectNameLabel = null;
}
if (null != projectNameText)
{
projectNameText = null;
}
if (null != pathLabel)
{
pathLabel = null;
}
if (null != pathText)
{
pathText = null;
}
if (null != serviceTypeLabel)
{
serviceTypeLabel = null;
}
if (null != serviceTypeCombo)
{
serviceTypeCombo = null;
}
if (null != versionTypeCombo)
{
versionTypeCombo = null;
}
if (null != serviceNameLabel)
{
serviceNameLabel = null;
}
if (null != serviceNameText)
{
serviceNameText = null;
}
if (null != container)
{
container = null;
}
if (null != projectBrowseButton)
{
projectBrowseButton = null;
}
if (null != serviceInvisibleButton)
{
serviceInvisibleButton = null;
}
if (null != locationArea)
{
locationArea = null;
}
if (null != workingSetGroup)
{
workingSetGroup = null;
}
if (null != businessData)
{
businessData = null;
}
if (null != structureData)
{
structureData = null;
}
}
@Override
public void performHelp()
{
getShell().setData(ORG_ECLIPSE_UI_HELP, ORG_ECLIPSE_OGEE_CORE + HelpConstants.SERVICE_IMPLEMENTATION_HELP);
}
}