/*******************************************************************************
* Copyright (c) 2012 Pivotal Software, Inc.
* 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:
* Pivotal Software, Inc. - initial API and implementation
*******************************************************************************/
package org.springsource.ide.eclipse.commons.livexp.core;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.springsource.ide.eclipse.commons.livexp.util.ExceptionUtil;
/**
* A value representing the result of a wizard page validation.
*
* @author Kris De Volder
*/
public class ValidationResult {
public final String msg;
public int status;
private ValidationResult(int status, String msg) {
this.msg = msg;
this.status = status;
}
public boolean isOk() {
return status == IStatus.OK;
}
public static final ValidationResult OK = new ValidationResult(IStatus.OK, null);
public static ValidationResult error(String msg) {
return new ValidationResult(IStatus.ERROR, msg);
}
public static ValidationResult warning(String msg) {
return new ValidationResult(IStatus.WARNING, msg);
}
public static ValidationResult info(String msg) {
return new ValidationResult(IStatus.INFO, msg);
}
/**
* Create a copy of a ValidationResult replacing the message
*/
public ValidationResult withMessage(String newMsg) {
return new ValidationResult(this.status, newMsg);
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
//generated by eclipse
final int prime = 31;
int result = 1;
result = prime * result + ((msg == null) ? 0 : msg.hashCode());
result = prime * result + status;
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
//generated by eclipse
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ValidationResult other = (ValidationResult) obj;
if (msg == null) {
if (other.msg != null)
return false;
} else if (!msg.equals(other.msg))
return false;
if (status != other.status)
return false;
return true;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "ValidationResult [msg=" + msg + ", status=" + status(status) + "]";
}
private static String status(int s) {
switch (s) {
case IStatus.ERROR:
return "ERROR";
case IStatus.WARNING:
return "WARNING";
default:
return ""+s;
}
}
/**
* Convert the status code of this validation results into an IMessageProvider status code.
*/
public int getMessageProviderStatus() {
switch (status) {
case IStatus.OK:
return IMessageProvider.NONE;
case IStatus.CANCEL:
//There's no corresponding statis in IMessageProvider. Treat cancelation like an error.
return IMessageProvider.ERROR;
case IStatus.ERROR:
return IMessageProvider.ERROR;
case IStatus.INFO:
return IMessageProvider.INFORMATION;
case IStatus.WARNING:
return IMessageProvider.WARNING;
default:
//Shouldn't happen since all cases should be covered above... byt anyhow
return IMessageProvider.ERROR;
}
}
/**
* Convert an Eclipse IStatus into a ValidationResult
*/
public static ValidationResult from(IStatus status) {
if (status.isOK()) {
return ValidationResult.OK;
} else {
Throwable e = status.getException();
String msg;
if (e!=null) {
//produces more informative error messages (from the deepest cause rather than a generic message
// usually attached to the IStatuses from Eclipse.
msg = ExceptionUtil.getMessage(e);
} else {
msg = status.getMessage();
}
if (msg==null) {
msg = "Error";
}
return new ValidationResult(status.getSeverity(), msg);
}
}
}