/*******************************************************************************
* 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.wgadesigner.wizards;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.dialogs.IDialogPage;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.preference.PreferenceDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.jface.wizard.WizardPage;
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.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkingSet;
import org.eclipse.ui.IWorkingSetManager;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.PreferencesUtil;
import org.eclipse.ui.forms.HyperlinkGroup;
import org.eclipse.ui.forms.events.HyperlinkAdapter;
import org.eclipse.ui.forms.events.HyperlinkEvent;
import org.eclipse.ui.forms.widgets.ImageHyperlink;
import de.innovationgate.eclipse.utils.WorkbenchUtils;
import de.innovationgate.eclipse.wgadesigner.WGADesignerPlugin;
import de.innovationgate.eclipse.wgadesigner.dialogs.SelectWorkingSetDialog;
import de.innovationgate.eclipse.wgadesigner.editors.helpers.WGADeployment;
import de.innovationgate.utils.WGUtils;
public class NewWGARuntimePage extends WizardPage {
private Text _txtRuntimeName;
private Combo _comboWGADistribution;
private HashMap<String, WGADeployment> _wgaDistributionItems;
private Composite _container;
private Combo _selectWorkingSet;
private Label _lblworkingSets;
private static List<String> _workingSetSelection;
private Object _selection;
public NewWGARuntimePage(ISelection selection) {
super("WGA Runtime Definition");
setTitle("WGA Runtime Project");
setDescription("This wizard creates a new WGA Runtime Project in the workspace.");
_selection = ((TreeSelection)selection).getFirstElement();
//computeSelectedWorkingSet(_selection);
}
private String computeSelectedWorkingSet(Object selection) {
if (selection instanceof IWorkingSet){
IWorkingSet workingSet = (IWorkingSet) selection;
return workingSet.getName();
}
if (selection instanceof IResource){
IResource resource = (IResource) selection;
IProject project = resource.getProject();
IWorkingSet[] workingSets = PlatformUI.getWorkbench().getWorkingSetManager().getWorkingSets();
for(IWorkingSet currentSet : workingSets){
for(IAdaptable current : currentSet.getElements()){
if(current.equals(project)){
return currentSet.getName();
}
}
}
}
return null;
}
/**
* @see IDialogPage#createControl(Composite)
*/
@SuppressWarnings("unchecked")
public void createControl(Composite parent) {
if(_workingSetSelection==null){
_workingSetSelection = new ArrayList<String>();
}
_container = new Composite(parent, SWT.NULL);
GridLayout layout = new GridLayout();
_container.setLayout(layout);
layout.numColumns = 3;
layout.verticalSpacing = 9;
Label label = new Label(_container, SWT.NULL);
label.setText("&Runtime name:");
_txtRuntimeName = new Text(_container, SWT.BORDER | SWT.SINGLE);
GridData gd = new GridData(GridData.FILL_HORIZONTAL);
gd.horizontalSpan = 2;
_txtRuntimeName.setLayoutData(gd);
_txtRuntimeName.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
dialogChanged();
}
});
// join wga distribution download if present
Job[] jobs = Job.getJobManager().find(WGADesignerPlugin.JOB_FAMILY_WGA_DISTRIBUTION_DOWNLOAD);
if (jobs != null && jobs.length > 0) {
ProgressMonitorDialog dialog = new ProgressMonitorDialog(getShell());
IRunnableWithProgress runnable = new IRunnableWithProgress() {
public void run(IProgressMonitor arg0) throws InvocationTargetException, InterruptedException {
Job.getJobManager().join(WGADesignerPlugin.JOB_FAMILY_WGA_DISTRIBUTION_DOWNLOAD, arg0);
}
};
try {
dialog.run(true, true, runnable);
}
catch (InvocationTargetException e) {
}
catch (InterruptedException e) {
}
}
// init wga distributions
label = new Label(_container, SWT.NULL);
label.setText("WGA Distribution:");
_comboWGADistribution = new Combo(_container, SWT.READ_ONLY);
updateWGADistributions(false);
_comboWGADistribution.addSelectionListener(new SelectionListener() {
public void widgetDefaultSelected(SelectionEvent e) {
dialogChanged();
}
public void widgetSelected(SelectionEvent e) {
dialogChanged();
}
});
HyperlinkGroup linkGroup = new HyperlinkGroup(_container.getDisplay());
ImageHyperlink linkCreateNewRuntime = new ImageHyperlink(_container, SWT.NONE);
linkGroup.add(linkCreateNewRuntime);
linkCreateNewRuntime.setText("configure WGA distributions");
linkCreateNewRuntime.addHyperlinkListener(new HyperlinkAdapter() {
@Override
public void linkActivated(HyperlinkEvent e) {
PreferenceDialog pref = PreferencesUtil.createPreferenceDialogOn(new Shell(), WGADesignerPlugin.PREFERENCES_PAGE_WGA_DEPLOYMENTS, null, null);
if (pref != null) {
pref.open();
updateWGADistributions();
}
}
});
_lblworkingSets = new Label(_container, SWT.NULL);
_lblworkingSets.setText("Working set:");
_selectWorkingSet = new Combo(_container, SWT.READ_ONLY);
_selectWorkingSet.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
_lblworkingSets.setEnabled(true);
_selectWorkingSet.setEnabled(true);
IWorkingSetManager manager = PlatformUI.getWorkbench().getWorkingSetManager();
String[] ids = new String[manager.getAllWorkingSets().length];
int i = 0;
for (IWorkingSet c : manager.getAllWorkingSets()) {
ids[i] = c.getId();
i++;
}
IWorkingSet[] workingSets = PlatformUI.getWorkbench().getWorkingSetManager().getWorkingSets();
List<String> workingSetNames = new ArrayList<String>();
workingSetNames.add("");
int indexOfworkingSetNames=0;
int indexOfpreselected=0;
String selectedWorkingSetName = computeSelectedWorkingSet(_selection);
if(selectedWorkingSetName!=null){
for(IWorkingSet currentSet : workingSets){
indexOfworkingSetNames++;
workingSetNames.add(currentSet.getName());
if(selectedWorkingSetName.equals(currentSet.getName())){
indexOfpreselected = indexOfworkingSetNames;
}
}
}
_selectWorkingSet.setItems(workingSetNames.toArray(new String[0]));
_selectWorkingSet.select(indexOfpreselected);
setControl(_container);
setPageComplete(false);
}
public List<String> getSelectedWorkingSets() {
if(_selectWorkingSet!=null && _selectWorkingSet.isEnabled() && _selectWorkingSet.getSelectionIndex() != -1){
List<String> sets = WGUtils.deserializeCollection(_selectWorkingSet.getItem(_selectWorkingSet.getSelectionIndex()), ",");
if(sets.size()>0){
return sets;
}
}
return null;
}
private void updateWGADistributions() {
updateWGADistributions(true);
}
private void updateWGADistributions(boolean fireDialogChanged) {
_wgaDistributionItems = new HashMap<String, WGADeployment>();
Iterator<WGADeployment> deployments = WGADesignerPlugin.getDefault().getWGADeploymentManager().getDeployments().iterator();
while (deployments.hasNext()) {
WGADeployment deployment = deployments.next();
_wgaDistributionItems.put(deployment.toString(), deployment);
}
_comboWGADistribution.setItems(_wgaDistributionItems.keySet().toArray(new String[0]));
// select default deployment
List<String> items = Arrays.asList(_comboWGADistribution.getItems());
WGADeployment defaultDeployment = WGADesignerPlugin.getDefault().getWGADeploymentManager().getDefaultDeployment();
if (defaultDeployment != null) {
_comboWGADistribution.select(items.indexOf(defaultDeployment.toString()));
}
else {
_comboWGADistribution.select(0);
}
_comboWGADistribution.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
_comboWGADistribution.layout();
_container.layout();
if (fireDialogChanged) {
dialogChanged();
}
}
private void dialogChanged() {
String name = getRuntimeName();
File container = new File(ResourcesPlugin.getWorkspace().getRoot().getLocation().makeAbsolute().toFile().getAbsolutePath());
File targetFile = new File(ResourcesPlugin.getWorkspace().getRoot().getLocation().makeAbsolute().toFile().getAbsolutePath() + File.separator + name);
if (container == null || (!container.exists()) || (!container.isDirectory())) {
updateStatus("Container '" + container.getAbsolutePath() + "' does not exist.");
return;
}
if (!container.canWrite() || !container.canRead()) {
updateStatus("Container is not accessable due to filesystem permissions.");
return;
}
if (name.length() == 0) {
updateStatus("Please specify a name for the new WGA runtime.");
return;
}
if (!WorkbenchUtils.isValidResourceName(name)) {
updateStatus("WGA runtime name contains invalid characters. Only alphanumeric ascii characters and '_', '-' are allowed.");
return;
}
if (getWGADeployment() == null) {
updateStatus("Please specify a wga distribution.");
return;
}
if (ResourcesPlugin.getWorkspace().getRoot().getProject(name).exists()) {
updateStatus("A project with name '" + name + "' already exists");
return;
}
if (targetFile.exists()) {
updateStatus("a resource at '" + targetFile.getAbsolutePath() + "' already exists.");
return;
}
updateStatus(null);
}
private void updateStatus(String message) {
setErrorMessage(message);
setPageComplete(message == null);
}
public String getRuntimeName() {
return _txtRuntimeName.getText();
}
public WGADeployment getWGADeployment() {
if (_comboWGADistribution.getSelectionIndex() != -1) {
String selectedCombo = _comboWGADistribution.getItem(_comboWGADistribution.getSelectionIndex());
return _wgaDistributionItems.get(selectedCombo);
}
else {
return null;
}
}
}