use of au.gov.asd.tac.constellation.plugins.PluginException in project constellation by constellation-app.
the class ToolsOverlay method overlay.
@Override
public void overlay() {
boolean leftMousePressed = renderer.mouseButton == PConstants.LEFT;
// draw tool overlay
renderer.noStroke();
renderer.fill(BACKGROUND_COLOUR);
renderer.rect(x, y, width, height);
float yOffset = y + MARGIN;
// update measure tool state
final float measureToolX = x + 60;
final float measureToolWidth = VALUE_BOX_MEDIUM_WIDTH + VALUE_BOX_SHORT_WIDTH;
final float measureSystemX = x + 60 + VALUE_BOX_MEDIUM_WIDTH + VALUE_BOX_SHORT_WIDTH + (PADDING * 2);
final float measureSystemWidth = VALUE_BOX_SHORT_WIDTH;
if (renderer.mouseX > measureToolX && renderer.mouseX < measureToolX + measureToolWidth && renderer.mouseY > yOffset && renderer.mouseY < yOffset + VALUE_BOX_HEIGHT) {
if (leftMousePressed && mouseLeftMeasureToolRegion && !drawActive) {
measureActive = !measureActive;
mouseLeftMeasureToolRegion = false;
}
} else {
mouseLeftMeasureToolRegion = true;
}
if (!measureActive && renderer.mouseX > measureSystemX && renderer.mouseX < measureSystemX + measureSystemWidth && renderer.mouseY > yOffset && renderer.mouseY < yOffset + VALUE_BOX_HEIGHT) {
if (leftMousePressed && mouseLeftMeasureSystemRegion) {
final MeasurementSystem[] measurementSystems = MeasurementSystem.values();
measureSystem = measurementSystems[(measureSystem.ordinal() + 1) % measurementSystems.length];
mouseLeftMeasureSystemRegion = false;
}
} else {
mouseLeftMeasureSystemRegion = true;
}
// draw measure tool
drawLabel("Measure", x + 60, yOffset);
drawValue(measureSystem.getDisplayText(), measureSystemX, yOffset, measureSystemWidth, false, false);
if (drawActive) {
final Location start = getDrawToolStart();
final Location end = getDrawToolEnd();
if (start != null && end != null) {
final float distance = measureSystem.getMeasureFunction().apply(start, end).floatValue();
drawValue(String.format("%s", PApplet.nf(distance, 1, 3)), measureToolX, yOffset, measureToolWidth, false, false);
} else {
drawValue(DISABLED, measureToolX, yOffset, measureToolWidth, false, false);
}
} else if (measureActive) {
final Location start = getMeasureToolStart();
final Location end = getMeasureToolEnd();
if (start != null && end != null) {
float distance = measureSystem.getMeasureFunction().apply(start, end).floatValue();
if (measurePath) {
for (int i = 1; i < measureVertices.size(); i++) {
final Location startVertex = measureVertices.get(i - 1);
final Location endVertex = measureVertices.get(i);
distance += measureSystem.getMeasureFunction().apply(startVertex, endVertex).floatValue();
}
}
drawValue(String.format("%s", PApplet.nf(distance, 1, 3)), measureToolX, yOffset, measureToolWidth, false, true);
} else {
drawValue("Enabled", measureToolX, yOffset, measureToolWidth, false, true);
}
} else {
drawValue(DISABLED, measureToolX, yOffset, measureToolWidth, false, false);
}
// update map based on measure tool state
if (mouseLeftMeasureToolRegion && measureActive) {
final int measureToolColor = renderer.color(255, 0, 0, 127);
renderer.stroke(measureToolColor);
if (measurePath && measureVertices.size() > 1) {
for (int i = 1; i < measureVertices.size(); i++) {
final ScreenPosition previousVertex = map.getScreenPosition(measureVertices.get(i - 1));
final ScreenPosition currentVertex = map.getScreenPosition(measureVertices.get(i));
renderer.strokeWeight(5);
renderer.point(previousVertex.x, previousVertex.y);
renderer.point(currentVertex.x, currentVertex.y);
renderer.strokeWeight(2);
renderer.line(previousVertex.x, previousVertex.y, currentVertex.x, currentVertex.y);
}
} else {
renderer.strokeWeight(5);
renderer.point(measureOriginX, measureOriginY);
}
if (measureOriginX != -1 && measureOriginY != -1 && measureDeltaX != -1 && measureDeltaY != -1) {
renderer.strokeWeight(5);
renderer.point(measureDeltaX, measureDeltaY);
renderer.strokeWeight(2);
renderer.line(measureOriginX, measureOriginY, measureDeltaX, measureDeltaY);
if (measureCircle) {
final float radius = (float) Math.sqrt(Math.pow((measureDeltaX - measureOriginX), 2) + Math.pow((measureDeltaY - measureOriginY), 2));
renderer.noFill();
renderer.strokeWeight(2);
renderer.ellipseMode(PConstants.RADIUS);
renderer.ellipse(measureOriginX, measureOriginY, radius, radius);
}
}
}
// draw separator
yOffset += VALUE_BOX_HEIGHT + PADDING * 2;
drawSeparator(yOffset);
yOffset += PADDING * 2;
// update draw tool state
final float drawToolX = x + 60;
final float drawToolWidth = VALUE_BOX_MEDIUM_WIDTH + VALUE_BOX_SHORT_WIDTH;
final float addToGraphX = x + 60 + VALUE_BOX_MEDIUM_WIDTH + VALUE_BOX_SHORT_WIDTH + (PADDING * 2);
final float addToGraphWidth = VALUE_BOX_SHORT_WIDTH;
if (renderer.mouseX > drawToolX && renderer.mouseX < drawToolX + drawToolWidth && renderer.mouseY > yOffset && renderer.mouseY < yOffset + VALUE_BOX_HEIGHT) {
if (leftMousePressed && mouseLeftDrawToolRegion && !measureActive) {
drawActive = !drawActive;
mouseLeftDrawToolRegion = false;
}
} else {
mouseLeftDrawToolRegion = true;
}
if (!drawActive && renderer.mouseX > addToGraphX && renderer.mouseX < addToGraphX + addToGraphWidth && renderer.mouseY > yOffset && renderer.mouseY < yOffset + VALUE_BOX_HEIGHT) {
if (leftMousePressed && mouseLeftAddToGraphRegion) {
try {
final Graph currentGraph = GraphManager.getDefault().getActiveGraph();
PluginExecution.withPlugin(MapViewPluginRegistry.COPY_CUSTOM_MARKERS_TO_GRAPH).executeNow(currentGraph);
final MarkerCache markerCache = renderer.getMarkerCache();
markerCache.getCustomMarkers().forEach(marker -> {
markerCache.get(marker).remove(GraphElement.NON_ELEMENT);
marker.setCustom(false);
});
renderer.updateMarkers(currentGraph, renderer.getMarkerState());
} catch (final PluginException ex) {
LOGGER.log(Level.SEVERE, "Error copying custom markers to graph", ex);
} catch (final InterruptedException ex) {
LOGGER.log(Level.SEVERE, "Error copying custom markers to graph", ex);
Thread.currentThread().interrupt();
}
mouseLeftAddToGraphRegion = false;
}
} else {
mouseLeftAddToGraphRegion = true;
}
// draw draw tool
drawLabel("Draw", x + 60, yOffset);
drawValue("+", addToGraphX, yOffset, addToGraphWidth, false, false);
if (drawActive) {
drawValue("Enabled", drawToolX, yOffset, drawToolWidth, false, true);
yOffset += VALUE_BOX_HEIGHT + (PADDING * 4);
final float drawDescriptionHeight = (VALUE_BOX_HEIGHT * 16) + (PADDING * 2) + 1;
renderer.noStroke();
renderer.fill(BACKGROUND_COLOUR);
renderer.rect(x, yOffset - 1, width, drawDescriptionHeight);
final String drawDescription = " > Click on the map to draw a point marker.\n" + " > Click on the map while holding shift to begin drawing a circle" + " marker, click again with or without shift to complete it.\n" + " > Click on the map while holding control to begin drawing a polygon" + " marker, continue clicking while holding control to draw edges," + " then release control and click once more to complete it.\n" + " > Click on a drawn marker to remove it.";
drawInfo(drawDescription, yOffset - (PADDING * 2), width - (MARGIN * 2) - (PADDING * 2), true);
} else {
drawValue(DISABLED, drawToolX, yOffset, drawToolWidth, false, false);
}
// update map based on draw tool state
if (mouseLeftDrawToolRegion && drawActive) {
final int polygonColor = renderer.color(0, 0, 0, 127);
renderer.stroke(polygonColor);
renderer.strokeJoin(PConstants.ROUND);
renderer.strokeCap(PConstants.ROUND);
if (drawPolygon && drawVertices.size() > 1) {
for (int i = 1; i < drawVertices.size(); i++) {
final ScreenPosition previousVertex = map.getScreenPosition(drawVertices.get(i - 1));
final ScreenPosition currentVertex = map.getScreenPosition(drawVertices.get(i));
renderer.strokeWeight(5);
renderer.point(previousVertex.x, previousVertex.y);
renderer.point(currentVertex.x, currentVertex.y);
renderer.strokeWeight(2);
renderer.line(previousVertex.x, previousVertex.y, currentVertex.x, currentVertex.y);
}
} else {
renderer.strokeWeight(5);
renderer.point(drawOriginX, drawOriginY);
}
if (drawOriginX != -1 && drawOriginY != -1 && drawDeltaX != -1 && drawDeltaY != -1) {
renderer.strokeWeight(5);
renderer.point(drawDeltaX, drawDeltaY);
renderer.strokeWeight(2);
renderer.line(drawOriginX, drawOriginY, drawDeltaX, drawDeltaY);
if (drawCircle) {
final float radius = (float) Math.sqrt(Math.pow((drawDeltaX - drawOriginX), 2) + Math.pow((drawDeltaY - drawOriginY), 2));
renderer.noFill();
renderer.strokeWeight(2);
renderer.ellipseMode(PConstants.RADIUS);
renderer.ellipse(drawOriginX, drawOriginY, radius, radius);
}
}
}
active = measureActive || drawActive;
}
use of au.gov.asd.tac.constellation.plugins.PluginException in project constellation by constellation-app.
the class CopyCustomMarkersToGraphPlugin method edit.
@Override
protected void edit(final GraphWriteMethods graph, final PluginInteraction interaction, final PluginParameters parameters) throws InterruptedException, PluginException {
final int vertexIdentifierAttributeId = VisualConcept.VertexAttribute.IDENTIFIER.ensure(graph);
final int vertexTypeAttributeId = AnalyticConcept.VertexAttribute.TYPE.ensure(graph);
final int vertexLatitudeAttributeId = SpatialConcept.VertexAttribute.LATITUDE.ensure(graph);
final int vertexLongitudeAttributeId = SpatialConcept.VertexAttribute.LONGITUDE.ensure(graph);
final int vertexPrecisionAttributeId = SpatialConcept.VertexAttribute.PRECISION.ensure(graph);
final int vertexShapeAttributeId = SpatialConcept.VertexAttribute.SHAPE.ensure(graph);
final MarkerCache markerCache = MarkerCache.getDefault();
for (final ConstellationAbstractMarker marker : markerCache.getCustomMarkers()) {
final String markerId = marker.getId() == null ? marker.toString() : marker.getId();
final int vertexId = graph.addVertex();
graph.setStringValue(vertexIdentifierAttributeId, vertexId, markerId);
graph.setObjectValue(vertexTypeAttributeId, vertexId, AnalyticConcept.VertexType.LOCATION);
graph.setFloatValue(vertexLatitudeAttributeId, vertexId, marker.getLocation().getLat());
graph.setFloatValue(vertexLongitudeAttributeId, vertexId, marker.getLocation().getLon());
graph.setFloatValue(vertexPrecisionAttributeId, vertexId, (float) marker.getRadius());
try {
final Shape.GeometryType geometryType;
if (marker instanceof ConstellationMultiMarker) {
geometryType = Shape.GeometryType.MULTI_POLYGON;
} else if (marker instanceof ConstellationPolygonMarker) {
geometryType = Shape.GeometryType.POLYGON;
} else if (marker instanceof ConstellationLineMarker) {
geometryType = Shape.GeometryType.LINE;
} else {
geometryType = Shape.GeometryType.POINT;
}
final List<Tuple<Double, Double>> coordinates = marker.getLocations().stream().map(location -> Tuple.create((double) location.getLon(), (double) location.getLat())).collect(Collectors.toList());
final String shape = Shape.generateShape(markerId, geometryType, coordinates);
graph.setStringValue(vertexShapeAttributeId, vertexId, shape);
} catch (final IOException ex) {
throw new PluginException(PluginNotificationLevel.ERROR, ex);
}
}
PluginExecution.withPlugin(VisualSchemaPluginRegistry.COMPLETE_SCHEMA).executeNow(graph);
PluginExecution.withPlugin(InteractiveGraphPluginRegistry.RESET_VIEW).executeNow(graph);
}
use of au.gov.asd.tac.constellation.plugins.PluginException in project constellation by constellation-app.
the class SimpleEditPlugin method run.
@Override
public final void run(final PluginGraphs graphs, final PluginInteraction interaction, final PluginParameters parameters) throws InterruptedException, PluginException, RuntimeException {
boolean inControlOfProgress = true;
final Graph graph = graphs.getGraph();
// graph no longer exists
if (graph == null) {
LOGGER.log(Level.WARNING, "Null graph not allowed in a {0}", SimpleEditPlugin.class.getSimpleName());
return;
}
// Make the graph appear busy
interaction.setBusy(graph.getId(), true);
try {
// Make the progress bar appear nondeterminent
interaction.setProgress(0, 0, WAITING_INTERACTION, true);
try {
boolean cancelled = false;
Object description = null;
WritableGraph writableGraph = graph.getWritableGraph(getName(), isSignificant(), this);
try {
interaction.setProgress(0, 0, "Editing...", true);
try {
description = describedEdit(writableGraph, interaction, parameters);
if (!"Editing...".equals(interaction.getCurrentMessage())) {
inControlOfProgress = false;
}
} catch (final Exception ex) {
cancelled = true;
throw ex;
}
} finally {
if (cancelled) {
writableGraph.rollBack();
} else {
writableGraph.commit(description);
}
}
} finally {
interaction.setProgress(2, 1, inControlOfProgress ? "Finished" : interaction.getCurrentMessage(), true);
}
} finally {
interaction.setBusy(graph.getId(), false);
}
}
use of au.gov.asd.tac.constellation.plugins.PluginException in project constellation by constellation-app.
the class ScriptingExecutePlugin method execute.
@Override
protected void execute(final PluginGraphs graphs, final PluginInteraction interaction, final PluginParameters parameters) throws InterruptedException, PluginException {
final Graph graph = graphs.getGraph();
final String script = parameters.getParameters().get(SCRIPT_PARAMETER_ID).getStringValue();
final boolean newOutput = parameters.getParameters().get(NEW_OUTPUT_PARAMETER_ID).getBooleanValue();
final String graphName = parameters.getParameters().get(GRAPH_NAME_PARAMETER_ID).getStringValue();
// configure the output pane
final ScriptingInterruptAction interruptor = new ScriptingInterruptAction();
final Action[] actions = new Action[] { interruptor };
final InputOutput io = IOProvider.getDefault().getIO(graphName, newOutput, actions, null);
if (!newOutput) {
try {
io.getOut().reset();
} catch (final IOException ex) {
LOGGER.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
}
}
io.select();
// configure scripting engine
final ScriptEngine engine = FACTORY.getEngineByMimeType(SyntaxConstants.SYNTAX_STYLE_PYTHON);
if (engine instanceof PyScriptEngine) {
// Add custom libs to the Jython sys.path.
final Preferences prefs = NbPreferences.forModule(ApplicationPreferenceKeys.class);
final String userDir = ApplicationPreferenceKeys.getUserDir(prefs);
final File pythonLib = new File(userDir, "PythonLib");
if (pythonLib.isDirectory()) {
final PySystemState state = Py.getSystemState();
state.path.append(new PyString(pythonLib.getPath()));
}
}
engine.getContext().setWriter(new InterruptibleWriter(io.getOut()));
engine.getContext().setErrorWriter(new InterruptibleWriter(io.getErr()));
// set scripting engine for graph
final SGraph sGraph = new SGraph(graph);
try {
sGraph.setEngine(engine);
// add custom objects to scripting engine
engine.getContext().setAttribute("graph", sGraph, ScriptContext.ENGINE_SCOPE);
Lookup.getDefault().lookupAll(ScriptingModule.class).iterator().forEachRemaining(module -> engine.getContext().setAttribute(module.getName(), module, ScriptContext.ENGINE_SCOPE));
try {
// Jython caches modules - force it to reload all modules in case the user has changed something in their local PythonLib.
if (engine instanceof PyScriptEngine) {
engine.eval("import sys\nsys.modules.clear()", engine.getContext());
}
engine.eval(script, engine.getContext());
} catch (final ScriptException ex) {
parameters.getParameters().get(OUTPUT_EXCEPTION_PARAMETER_ID).setObjectValue(ex);
ex.printStackTrace(io.getErr());
throw new PluginException(PluginNotificationLevel.ERROR, ex);
}
} finally {
// attempt to cleanup locks created by script
sGraph.cleanup();
}
}
use of au.gov.asd.tac.constellation.plugins.PluginException in project constellation by constellation-app.
the class ScriptingUtilities method executePlugin.
/**
* Lookup a plugin by name and execute it with custom parameter values.
*
* @param graph The graph on which to execute the plugin.
* @param pluginName The name of the plugin you wish to execute.
* @param pluginParameters A map of parameters to their values for use with
* the plugin you wish to execute.
*/
public void executePlugin(final SGraph graph, final String pluginName, final Map<String, String> pluginParameters) {
final Plugin plugin = PluginRegistry.get(pluginName);
final PluginParameters parameters = new PluginParameters();
parameters.appendParameters(plugin.createParameters());
try {
pluginParameters.forEach((parameterName, parameterValue) -> {
if (parameters.hasParameter(parameterName)) {
parameters.getParameters().get(parameterName).setStringValue(parameterValue);
}
});
PluginExecution.withPlugin(plugin).withParameters(parameters).executeNow(graph.getGraph());
} catch (final InterruptedException ex) {
LOGGER.log(Level.SEVERE, ex, () -> pluginName + " was interrupted");
Thread.currentThread().interrupt();
} catch (final PluginException ex) {
LOGGER.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
}
}
Aggregations