/*
* JBoss, Home of Professional Open Source.
*
* See the LEGAL.txt file distributed with this work for information regarding copyright ownership and licensing.
*
* See the AUTHORS.txt file distributed with this work for a full listing of individual contributors.
*/
package org.teiid.designer.vdb.ui.build;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IMarkerResolution;
import org.teiid.designer.runtime.spi.ITeiidVdb;
import org.teiid.designer.ui.common.viewsupport.UiBusyIndicator;
import org.teiid.designer.ui.util.ErrorHandler;
import org.teiid.designer.vdb.Vdb;
import org.teiid.designer.vdb.XmiVdb;
import org.teiid.designer.vdb.ui.Messages;
/**
*
*/
public class VdbMigrateXsdFilesMarkerResolution implements IMarkerResolution {
/**
* {@inheritDoc}
*
* @see org.eclipse.ui.IMarkerResolution#getLabel()
*/
@Override
public String getLabel() {
return Messages.migrateXsdFilesFromModelsToSchemasLabel;
}
/**
* @param resource the resource being checked (never <code>null</code>)
* @return <code>true</code> if resource is a VDB file
*/
private boolean isVdbFile(IResource resource) {
return ((resource.getType() == IResource.FILE) && ITeiidVdb.VDB_EXTENSION.equals(resource.getFileExtension()) && resource.exists());
}
/**
* {@inheritDoc}
*
* @see org.eclipse.ui.IMarkerResolution#run(org.eclipse.core.resources.IMarker)
*/
@Override
public void run(IMarker marker) {
IResource resource = marker.getResource();
// Fix the Marked Model Resource
if (! isVdbFile(resource))
return;
final IFile theVdbFile = (IFile)resource;
final Exception[] theException = new Exception[1];
UiBusyIndicator.showWhile(Display.getDefault(), new Runnable() {
@Override
public void run() {
try {
fixVdb(theVdbFile);
} catch (Exception ex) {
theException[0] = ex;
}
}
});
if (theException[0] != null)
ErrorHandler.toExceptionDialog(theException[0]);
}
void fixVdb(IFile theVdb) throws Exception {
NullProgressMonitor monitor = new NullProgressMonitor();
Vdb vdb = new XmiVdb(theVdb, false);
vdb.save();
try {
theVdb.refreshLocal(IResource.DEPTH_ZERO, monitor);
} catch (CoreException ex) {
// No need to log this
}
}
}