/*******************************************************************************
* 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.utils.ui;
import java.util.List;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.WizardPage;
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 de.innovationgate.eclipse.utils.Activator;
public abstract class ValidatedWizardPage extends WizardPage implements ModifyListener, SelectionListener, ISelectionChangedListener {
protected ValidatedWizardPage(String pageName) {
super(pageName);
}
public ValidatedWizardPage(String pageName, String title, ImageDescriptor titleImage) {
super(pageName, title, titleImage);
}
/**
* called on each page change
* @return
*/
public abstract List<IStatus> validate();
@Override
public IWizardPage getNextPage() {
System.out.println("nextpage called on " + getName());
return super.getNextPage();
}
/**
* called after page has been validated successfully
*/
public abstract void computeResponse();
public void modifyText(ModifyEvent e) {
performValidation();
}
protected void performValidation() {
//TODO only set page incomplte if there is a ERROR status in list
// info warning should net set the page inclomplete
List<IStatus> status = validate();
if (status != null && !status.isEmpty()) {
applyToStatusLine(status.get(0));
setPageComplete(false);
return;
}
applyToStatusLine(new Status(Status.OK, Activator.PLUGIN_ID, ""));
computeResponse();
setPageComplete(true);
}
public void show(){};
private void applyToStatusLine(IStatus status) {
String message = status.getMessage();
if (status.getException() != null) {
Activator.getDefault().logError(message, status.getException());
message += " See error log for details.";
}
if (message.length() == 0)
message = null;
switch (status.getSeverity()) {
case IStatus.OK:
setErrorMessage(null);
setMessage(message);
break;
case IStatus.WARNING:
setErrorMessage(null);
setMessage(message, WizardPage.WARNING);
break;
case IStatus.INFO:
setErrorMessage(null);
setMessage(message, WizardPage.INFORMATION);
break;
default:
setErrorMessage(message);
setMessage(null);
break;
}
}
public void widgetDefaultSelected(SelectionEvent e) {
performValidation();
}
public void widgetSelected(SelectionEvent e) {
performValidation();
}
public void selectionChanged(SelectionChangedEvent event) {
performValidation();
}
@Override
public void setVisible(boolean visible) {
if (visible) {
show();
}
super.setVisible(visible);
}
}