Search in sources :

Example 1 with NamedSelectionState

use of au.gov.asd.tac.constellation.views.namedselection.state.NamedSelectionState in project constellation by constellation-app.

the class ArrangeInBubbleTreeAction method actionPerformed.

@Override
public void actionPerformed(final ActionEvent e) {
    final Graph graph = context.getGraph();
    final ReadableGraph rg = graph.getReadableGraph();
    try {
        NamedSelectionState nsState = null;
        final int namedSelectionId = rg.getAttribute(GraphElementType.VERTEX, "named_selection");
        if (namedSelectionId != Graph.NOT_FOUND) {
            final int namedSelectionStateId = rg.getAttribute(GraphElementType.META, NamedSelectionState.ATTRIBUTE_NAME);
            if (namedSelectionStateId != Graph.NOT_FOUND) {
                nsState = rg.getObjectValue(namedSelectionStateId, 0);
                final SelectNamedSelectionPanel ssp = new SelectNamedSelectionPanel(nsState.getNamedSelections(), "Select a named selection to represent the tree roots.");
                final DialogDescriptor dd = new DialogDescriptor(ssp, Bundle.CTL_ArrangeInBubbleTreeAction());
                dd.setHelpCtx(new HelpCtx(HELP_LOCATION));
                final Object result = DialogDisplayer.getDefault().notify(dd);
                if (result == DialogDescriptor.OK_OPTION) {
                    final long selectionId = ssp.getNamedSelectionId();
                    if (selectionId != -1) {
                        final long mask = 1L << selectionId;
                        final Set<Integer> rootVxIds = new HashSet<>();
                        for (int position = 0; position < rg.getVertexCount(); position++) {
                            final int vxId = rg.getVertex(position);
                            final long selections = rg.getLongValue(namedSelectionId, vxId);
                            if ((selections & mask) != 0) {
                                rootVxIds.add(vxId);
                            }
                        }
                        PluginExecutor.startWith(ArrangementPluginRegistry.BUBBLE_TREE).set(ArrangeInBubbleTreePlugin.ROOTS_PARAMETER_ID, rootVxIds).set(ArrangeInBubbleTreePlugin.IS_MINIMAL_PARAMETER_ID, true).followedBy(InteractiveGraphPluginRegistry.RESET_VIEW).executeWriteLater(context.getGraph(), Bundle.CTL_ArrangeInBubbleTreeAction());
                    }
                }
            }
        }
        if (nsState == null) {
            NotifyDisplayer.display("There must be a named selection to specify the tree roots", NotifyDescriptor.WARNING_MESSAGE);
        }
    } finally {
        rg.release();
    }
}
Also used : ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) Graph(au.gov.asd.tac.constellation.graph.Graph) SelectNamedSelectionPanel(au.gov.asd.tac.constellation.views.namedselection.utilities.SelectNamedSelectionPanel) DialogDescriptor(org.openide.DialogDescriptor) HelpCtx(org.openide.util.HelpCtx) NamedSelectionState(au.gov.asd.tac.constellation.views.namedselection.state.NamedSelectionState) HashSet(java.util.HashSet)

Example 2 with NamedSelectionState

use of au.gov.asd.tac.constellation.views.namedselection.state.NamedSelectionState in project constellation by constellation-app.

the class NamedSelectionManager method saveStateToGraph.

/**
 * Helper method that saves the current Named Selection state to the graph.
 *
 * @return The state that was saved to the graph.
 */
@SuppressWarnings("unchecked")
private NamedSelectionState saveStateToGraph() {
    final Graph graph = graphNode.getGraph();
    final NamedSelectionState newState;
    if (state != null) {
        newState = new NamedSelectionState(state);
    } else {
        newState = new NamedSelectionState();
    }
    // Only write graph if it exists.
    if (graph != null) {
        // Write what we have to the graph:
        final NamedSelectionStatePlugin nssp = new NamedSelectionStatePlugin(newState);
        final Future<?> f = PluginExecution.withPlugin(nssp).interactively(true).executeLater(graph);
        try {
            f.get();
        } catch (final InterruptedException ex) {
            LOGGER.log(Level.SEVERE, "Saving Named Selection state was interrupted", ex);
            Thread.currentThread().interrupt();
        } catch (final ExecutionException ex) {
            LOGGER.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
        }
    }
    // Update the old state now:
    state = newState;
    return newState;
}
Also used : ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) Graph(au.gov.asd.tac.constellation.graph.Graph) NamedSelectionStatePlugin(au.gov.asd.tac.constellation.views.namedselection.state.NamedSelectionStatePlugin) ExecutionException(java.util.concurrent.ExecutionException) NamedSelectionState(au.gov.asd.tac.constellation.views.namedselection.state.NamedSelectionState)

Example 3 with NamedSelectionState

use of au.gov.asd.tac.constellation.views.namedselection.state.NamedSelectionState in project constellation by constellation-app.

the class ArrangeInHierarchyAction method actionPerformed.

@Override
public void actionPerformed(final ActionEvent e) {
    final Graph graph = context.getGraph();
    final ReadableGraph rg = graph.getReadableGraph();
    try {
        NamedSelectionState nsState = null;
        final int namedSelectionId = rg.getAttribute(GraphElementType.VERTEX, "named_selection");
        if (namedSelectionId != Graph.NOT_FOUND) {
            final int namedSelectionStateId = rg.getAttribute(GraphElementType.META, NamedSelectionState.ATTRIBUTE_NAME);
            if (namedSelectionStateId != Graph.NOT_FOUND) {
                nsState = rg.getObjectValue(namedSelectionStateId, 0);
                final SelectNamedSelectionPanel ssp = new SelectNamedSelectionPanel(nsState.getNamedSelections(), "Select a named selection to represent the top of the hierarchy.");
                final DialogDescriptor dd = new DialogDescriptor(ssp, Bundle.CTL_ArrangeInHierarchyAction());
                dd.setHelpCtx(new HelpCtx(HELP_LOCATION));
                final Object result = DialogDisplayer.getDefault().notify(dd);
                if (result == DialogDescriptor.OK_OPTION) {
                    final long selectionId = ssp.getNamedSelectionId();
                    if (selectionId != -1) {
                        final long mask = 1L << selectionId;
                        final Set<Integer> rootVxIds = new HashSet<>();
                        for (int position = 0; position < rg.getVertexCount(); position++) {
                            final int vxId = rg.getVertex(position);
                            final long selections = rg.getLongValue(namedSelectionId, vxId);
                            if ((selections & mask) != 0) {
                                rootVxIds.add(vxId);
                            }
                        }
                        PluginExecutor.startWith(ArrangementPluginRegistry.HIERARCHICAL).set(ArrangeInHierarchyPlugin.ROOTS_PARAMETER_ID, rootVxIds).followedBy(InteractiveGraphPluginRegistry.RESET_VIEW).executeWriteLater(context.getGraph(), Bundle.CTL_ArrangeInHierarchyAction());
                    }
                }
            }
        }
        if (nsState == null) {
            NotifyDisplayer.display("There must be a named selection to specify the tree roots", NotifyDescriptor.WARNING_MESSAGE);
        }
    } finally {
        rg.release();
    }
}
Also used : ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) Graph(au.gov.asd.tac.constellation.graph.Graph) SelectNamedSelectionPanel(au.gov.asd.tac.constellation.views.namedselection.utilities.SelectNamedSelectionPanel) DialogDescriptor(org.openide.DialogDescriptor) HelpCtx(org.openide.util.HelpCtx) NamedSelectionState(au.gov.asd.tac.constellation.views.namedselection.state.NamedSelectionState) HashSet(java.util.HashSet)

Example 4 with NamedSelectionState

use of au.gov.asd.tac.constellation.views.namedselection.state.NamedSelectionState in project constellation by constellation-app.

the class CopyToNewGraphPlugin method makeGraph.

/**
 * Created a new graph containing the selected elements of an existing
 * graph.
 *
 * @param original The graph to copy elements from.
 * @param newSchemaName If not null, create the new graph with this schema;
 * otherwise, use the schema of the original graph.
 * @param copyKeys If true, copy the keys.
 * @param copyAll If true, copy everything regardless of whether it is
 * selected or not.
 *
 * @return The new graph.
 *
 * @throws java.lang.InterruptedException if the process is canceled while
 * it is running.
 */
public static Graph makeGraph(final GraphReadMethods original, final String newSchemaName, final boolean copyKeys, final boolean copyAll) throws InterruptedException {
    final Schema schema = StringUtils.isNotBlank(newSchemaName) ? SchemaFactoryUtilities.getSchemaFactory(newSchemaName).createSchema() : original.getSchema();
    final Graph dualGraph = new DualGraph(schema == null ? null : schema.getFactory().createSchema());
    final WritableGraph graph = dualGraph.getWritableGraph("Make Graph", true);
    try {
        int vertexSelected = original.getAttribute(GraphElementType.VERTEX, VisualConcept.VertexAttribute.SELECTED.getName());
        int transactionSelected = original.getAttribute(GraphElementType.TRANSACTION, VisualConcept.TransactionAttribute.SELECTED.getName());
        int[] attributeTranslation = new int[1024];
        // Copy the attributes.
        for (GraphElementType type : GraphElementType.values()) {
            int attributeCount = original.getAttributeCount(type);
            for (int attributePosition = 0; attributePosition < attributeCount; attributePosition++) {
                int originalAttributeId = original.getAttribute(type, attributePosition);
                Attribute attribute = new GraphAttribute(original, originalAttributeId);
                int newAttributeId = graph.addAttribute(type, attribute.getAttributeType(), attribute.getName(), attribute.getDescription(), attribute.getDefaultValue(), null);
                if (originalAttributeId >= attributeTranslation.length) {
                    attributeTranslation = Arrays.copyOf(attributeTranslation, originalAttributeId * 2);
                }
                attributeTranslation[originalAttributeId] = newAttributeId;
                if (type == GraphElementType.GRAPH) {
                    graph.setObjectValue(newAttributeId, 0, original.getObjectValue(originalAttributeId, 0));
                }
            }
            if (copyKeys) {
                final int[] keyAttributes = original.getPrimaryKey(type);
                if (keyAttributes != null && keyAttributes.length > 0) {
                    for (int i = 0; i < keyAttributes.length; i++) {
                        keyAttributes[i] = attributeTranslation[keyAttributes[i]];
                    }
                    graph.setPrimaryKey(type, keyAttributes);
                }
            }
        }
        // Copy the named selection state.
        final int namedSelectionAttr = original.getAttribute(GraphElementType.META, NamedSelectionState.ATTRIBUTE_NAME);
        if (namedSelectionAttr != Graph.NOT_FOUND) {
            final Object possibleState = original.getObjectValue(namedSelectionAttr, 0);
            if (possibleState instanceof NamedSelectionState) {
                final NamedSelectionState state = new NamedSelectionState((NamedSelectionState) possibleState);
                graph.setObjectValue(attributeTranslation[namedSelectionAttr], 0, state);
            }
        }
        // Copy the vertices.
        int[] vertexTranslation = new int[original.getVertexCapacity()];
        for (int position = 0; position < original.getVertexCount(); position++) {
            int originalVertex = original.getVertex(position);
            if (copyAll || vertexSelected == Graph.NOT_FOUND || original.getBooleanValue(vertexSelected, originalVertex)) {
                int newVertex = graph.addVertex();
                vertexTranslation[originalVertex] = newVertex;
                for (int attributePosition = 0; attributePosition < original.getAttributeCount(GraphElementType.VERTEX); attributePosition++) {
                    int originalAttributeId = original.getAttribute(GraphElementType.VERTEX, attributePosition);
                    int newAttributeId = attributeTranslation[originalAttributeId];
                    graph.setObjectValue(newAttributeId, newVertex, original.getObjectValue(originalAttributeId, originalVertex));
                }
            }
        }
        // Copy the transactions.
        for (int position = 0; position < original.getTransactionCount(); position++) {
            int originalTransaction = original.getTransaction(position);
            if (!copyAll) {
                if (transactionSelected != Graph.NOT_FOUND && !original.getBooleanValue(transactionSelected, originalTransaction)) {
                    continue;
                }
                if (vertexSelected != Graph.NOT_FOUND) {
                    if (!original.getBooleanValue(vertexSelected, original.getTransactionSourceVertex(originalTransaction))) {
                        continue;
                    }
                    if (!original.getBooleanValue(vertexSelected, original.getTransactionDestinationVertex(originalTransaction))) {
                        continue;
                    }
                }
            }
            int sourceVertex = vertexTranslation[original.getTransactionSourceVertex(originalTransaction)];
            int destinationVertex = vertexTranslation[original.getTransactionDestinationVertex(originalTransaction)];
            boolean directed = original.getTransactionDirection(originalTransaction) < 2;
            int newTransaction = graph.addTransaction(sourceVertex, destinationVertex, directed);
            for (int attributePosition = 0; attributePosition < original.getAttributeCount(GraphElementType.TRANSACTION); attributePosition++) {
                int originalAttributeId = original.getAttribute(GraphElementType.TRANSACTION, attributePosition);
                int newAttributeId = attributeTranslation[originalAttributeId];
                graph.setObjectValue(newAttributeId, newTransaction, original.getObjectValue(originalAttributeId, originalTransaction));
            }
        }
    } finally {
        graph.commit();
    }
    return dualGraph;
}
Also used : WritableGraph(au.gov.asd.tac.constellation.graph.WritableGraph) Graph(au.gov.asd.tac.constellation.graph.Graph) DualGraph(au.gov.asd.tac.constellation.graph.locking.DualGraph) Attribute(au.gov.asd.tac.constellation.graph.Attribute) GraphAttribute(au.gov.asd.tac.constellation.graph.GraphAttribute) Schema(au.gov.asd.tac.constellation.graph.schema.Schema) GraphAttribute(au.gov.asd.tac.constellation.graph.GraphAttribute) DualGraph(au.gov.asd.tac.constellation.graph.locking.DualGraph) WritableGraph(au.gov.asd.tac.constellation.graph.WritableGraph) GraphElementType(au.gov.asd.tac.constellation.graph.GraphElementType) NamedSelectionState(au.gov.asd.tac.constellation.views.namedselection.state.NamedSelectionState)

Example 5 with NamedSelectionState

use of au.gov.asd.tac.constellation.views.namedselection.state.NamedSelectionState in project constellation by constellation-app.

the class NamedSelectionManager method readStateFromGraph.

/**
 * Queries the given graph (via the PluginFramework) for a saved
 * <code>NamedSelectionState</code>.
 *
 * @param graph The graph to retrieve the <code>NamedSelectionState</code>
 * from.
 *
 * @see NamedSelectionState
 * @see NamedSelectionStatePlugin
 */
@SuppressWarnings("unchecked")
private void readStateFromGraph(final GraphReadMethods graph) {
    // Ensure that the state gets set correctly under all circumstances.
    this.state = null;
    final int attrID = graph.getAttribute(GraphElementType.META, NamedSelectionState.ATTRIBUTE_NAME);
    if (attrID != Graph.NOT_FOUND) {
        final Object possibleState = graph.getObjectValue(attrID, 0);
        if (possibleState instanceof NamedSelectionState) {
            this.state = (NamedSelectionState) possibleState;
        }
    }
    if (this.state == null) {
        this.state = new NamedSelectionState();
    }
}
Also used : NamedSelectionState(au.gov.asd.tac.constellation.views.namedselection.state.NamedSelectionState)

Aggregations

NamedSelectionState (au.gov.asd.tac.constellation.views.namedselection.state.NamedSelectionState)5 Graph (au.gov.asd.tac.constellation.graph.Graph)4 ReadableGraph (au.gov.asd.tac.constellation.graph.ReadableGraph)3 SelectNamedSelectionPanel (au.gov.asd.tac.constellation.views.namedselection.utilities.SelectNamedSelectionPanel)2 HashSet (java.util.HashSet)2 DialogDescriptor (org.openide.DialogDescriptor)2 HelpCtx (org.openide.util.HelpCtx)2 Attribute (au.gov.asd.tac.constellation.graph.Attribute)1 GraphAttribute (au.gov.asd.tac.constellation.graph.GraphAttribute)1 GraphElementType (au.gov.asd.tac.constellation.graph.GraphElementType)1 WritableGraph (au.gov.asd.tac.constellation.graph.WritableGraph)1 DualGraph (au.gov.asd.tac.constellation.graph.locking.DualGraph)1 Schema (au.gov.asd.tac.constellation.graph.schema.Schema)1 NamedSelectionStatePlugin (au.gov.asd.tac.constellation.views.namedselection.state.NamedSelectionStatePlugin)1 ExecutionException (java.util.concurrent.ExecutionException)1