/*******************************************************************************
* Copyright (c) 2006 IBM Corporation and others.
* 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:
* IBM Corporation - initial API and implementation
* IBM Research
*******************************************************************************/
package com.ibm.research.tagging.core.ui.wizards;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import com.ibm.research.tagging.core.ITag;
import com.ibm.research.tagging.core.IWaypoint;
import com.ibm.research.tagging.core.TagCorePlugin;
import com.ibm.research.tagging.core.ui.validation.InvalidNameValidator;
/**
*
* @author mdesmond
*
*/
public abstract class WaypointWizard extends Wizard implements INewWizard
{
private static final String INVALID_CHARACTERS_IN_TAGS_ERROR = "The tags applied to this waypoint contain invalid characters. Please check tags and try again.";
private String fTitle;
private WaypointPage fPage;
private IStructuredSelection fSelection;
public WaypointWizard(String title, WaypointPage page)
{
fTitle = title;
fPage = page;
}
public WaypointPage getPage()
{
return fPage;
}
/*
* (non-Javadoc)
* @see org.eclipse.jface.wizard.Wizard#addPages()
*/
public void addPages()
{
super.addPages();
addPage(fPage);
}
/*
* (non-Javadoc)
* @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench, org.eclipse.jface.viewers.IStructuredSelection)
*/
public void init(IWorkbench workbench, IStructuredSelection selection)
{
setWindowTitle(fTitle);
fSelection = selection;
}
public IStructuredSelection getSelection()
{
return fSelection;
}
/*
* (non-Javadoc)
* @see org.eclipse.jface.wizard.Wizard#performFinish()
*/
public boolean performFinish()
{
InvalidNameValidator validator = new InvalidNameValidator()
{
@Override
protected String getInvalidNameError()
{
return INVALID_CHARACTERS_IN_TAGS_ERROR;
}
};
String error = validator.isValid(fPage.getTagsText());
if(error != null)
{
MessageDialog.openError(getShell(),"Error", error);
return false;
}
IWaypoint waypoint = getWaypoint();
if (waypoint == null)
return false;
String[] tagNames = fPage.getTags();
TagCorePlugin.getDefault().getTagCore().getWaypointModel()
.addWaypoint(waypoint);
for (String tagName : tagNames) {
if (tagName.trim().length() > 0) {
ITag tag = TagCorePlugin.getDefault().getTagCore()
.getTagModel().addTag(tagName);
waypoint.addTag(tag);
}
}
return true;
}
/**
* returns waypoint generated by the wizard. used by performFinish()
* @return IWaypoint or null to abort performFinish()
*/
protected abstract IWaypoint getWaypoint();
}