use of org.eclipse.titanium.error.GUIErrorHandler in project titan.EclipsePlug-ins by eclipse.
the class GraphHandler method saveToImage.
/**
* Exports the graph set for this class to a PNG file
*
* @param path
* : The PNG file's path
* @param mode
* : The way of export, see {@link GraphHandler}
* <code>public static</code> fields for possible values (EXPORT_
* named fields)
* @param size
* : This parameter sets the size of the exported image in pixels
* @throws Exception
* on file handling error
*/
public void saveToImage(final String path, final ImageExportType mode) throws BadLayoutException {
if (layout == null || actVisualisator == null) {
throw new BadLayoutException("Either the layout or the visuaizer is not set (is null)", ErrorType.NO_OBJECT);
}
VisualizationViewer<NodeDescriptor, EdgeDescriptor> tempVisualisator = null;
Dimension size = null;
switch(mode) {
case EXPORT_SEEN_GRAPH:
{
tempVisualisator = actVisualisator;
size = actVisualisator.getPreferredSize();
}
break;
case EXPORT_WHOLE_GRAPH:
{
layout = actVisualisator.getGraphLayout();
if (size == null) {
size = new Dimension(layout.getSize().width, layout.getSize().height);
}
final Function<NodeDescriptor, Point2D> trf = new Function<NodeDescriptor, Point2D>() {
@Override
public Point2D apply(final NodeDescriptor v) {
return layout.apply(v);
}
};
tempVisualisator = new VisualizationViewer<NodeDescriptor, EdgeDescriptor>(new LayoutBuilder(g, Layouts.LAYOUT_STATIC, size).transformer(trf).build());
tempVisualisator.setPreferredSize(size);
tempVisualisator.setSize(size);
tempVisualisator.getRenderContext().setVertexLabelTransformer(NODE_LABELER);
final GraphRenderer<NodeDescriptor, EdgeDescriptor> rnd = new GraphRenderer<NodeDescriptor, EdgeDescriptor>(NODE_LABELER, tempVisualisator.getPickedVertexState(), tempVisualisator.getPickedEdgeState());
setNodeRenderer(rnd, tempVisualisator);
tempVisualisator.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);
tempVisualisator.setBackground(Color.white);
tempVisualisator.setDoubleBuffered(false);
}
break;
case EXPORT_SATELLITE:
{
tempVisualisator = satView;
size = tempVisualisator.getSize();
}
break;
default:
ErrorReporter.logError("Unexpected image export type " + mode);
return;
}
BufferedImage image;
final GUIErrorHandler errorHandler = new GUIErrorHandler();
try {
image = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
} catch (OutOfMemoryError e) {
final long needed = (long) size.width * (long) size.height * 4;
String temp;
if (needed < 1024) {
temp = needed + " bytes";
} else if (needed < 1024 * 1024) {
temp = needed / 1024 + " Kbytes";
} else {
temp = needed / 1024 / 1024 + " Mbytes";
}
final String errorText = "Could not save an image of " + size.width + "*" + size.height + " size as there was not enough free memory (" + temp + ")";
errorHandler.reportErrorMessage(errorText);
ErrorReporter.logExceptionStackTrace(errorText, e);
return;
}
final Graphics2D g2 = image.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
tempVisualisator.paint(g2);
g2.dispose();
try {
ImageIO.write(image, "png", new File(path));
} catch (IOException e) {
final String message = "Error while writing to file" + path;
ErrorReporter.logExceptionStackTrace(message, e);
errorHandler.reportException(message, e);
}
}
use of org.eclipse.titanium.error.GUIErrorHandler in project titan.EclipsePlug-ins by eclipse.
the class ComponentGraphAction method doOpenGraphForProject.
/**
* @param project the project whose graph is to be displayed
*/
private void doOpenGraphForProject(final IProject project) {
try {
// looking for a file inside the selected
// project
final IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IFile input = null;
IResource[] members = project.members();
for (final IResource res : members) {
if (res.getType() == IResource.FILE) {
input = (IFile) res;
break;
}
if (res.getType() == IResource.FOLDER) {
members = ((IFolder) res).members();
}
}
final IEditorPart editor = page.findEditor(new FileEditorInput(input));
if (editor instanceof ComponentGraphEditor) {
((ComponentGraphEditor) editor).refreshGraph();
} else {
page.openEditor(new FileEditorInput(input), ComponentGraphEditor.ID, true, IWorkbenchPage.MATCH_ID | IWorkbenchPage.MATCH_INPUT);
}
} catch (Exception exc) {
final ErrorHandler errorHandler = new GUIErrorHandler();
errorHandler.reportException("Error while parsing the project", exc);
}
}
Aggregations