Search in sources :

Example 1 with HandleIoProgress

use of au.gov.asd.tac.constellation.utilities.gui.HandleIoProgress in project constellation by constellation-app.

the class LoadTemplatePlugin method loadTemplate.

private void loadTemplate(final File loadFile, final String templateName) throws PluginException {
    try {
        final Graph graph = new GraphJsonReader().readGraphZip(loadFile, new HandleIoProgress("Loading Template..."));
        GraphOpener.getDefault().openGraph(graph, templateName);
    } catch (GraphParseException | IOException ex) {
        throw new PluginException(this, PluginNotificationLevel.ERROR, "Failed to open template", ex);
    }
}
Also used : Graph(au.gov.asd.tac.constellation.graph.Graph) HandleIoProgress(au.gov.asd.tac.constellation.utilities.gui.HandleIoProgress) PluginException(au.gov.asd.tac.constellation.plugins.PluginException) GraphParseException(au.gov.asd.tac.constellation.graph.file.io.GraphParseException) IOException(java.io.IOException) GraphJsonReader(au.gov.asd.tac.constellation.graph.file.io.GraphJsonReader)

Example 2 with HandleIoProgress

use of au.gov.asd.tac.constellation.utilities.gui.HandleIoProgress in project constellation by constellation-app.

the class AutosaveGraphPlugin method execute.

@Override
public void execute(final PluginGraphs graphs, final PluginInteraction interaction, final PluginParameters parameters) throws InterruptedException, PluginException {
    final Graph graph = graphs.getGraph();
    final String graphId = graph.getId();
    final GraphNode gnode = GraphNode.getGraphNode(graphId);
    // The user might have deleted the graph, so check first.
    if (gnode != null) {
        interaction.setProgress(-1, -1, "Autosaving: " + graphId, true);
        // We don't want to hold the user up while we're reading from a graph they might be using.
        // Make a copy of the graph so that we can release the read lock as soon as possible.
        GraphReadMethods copy;
        ReadableGraph rg = graph.getReadableGraph();
        try {
            copy = rg.copy();
        } finally {
            rg.release();
        }
        interaction.setProgress(1, 0, "Finished", true);
        final File saveDir = AutosaveUtilities.getAutosaveDir();
        try {
            final String gname = graph.getId() + FileExtensionConstants.STAR;
            StatusDisplayer.getDefault().setStatusText(String.format("Auto saving %s as %s at %s...", graphId, gname, new Date()));
            final File saveFile = new File(saveDir, gname);
            new GraphJsonWriter().writeGraphToZip(copy, saveFile.getPath(), new HandleIoProgress("Autosaving..."));
            ConstellationLoggerHelper.exportPropertyBuilder(this, GraphRecordStoreUtilities.getVertices(copy, false, false, false).getAll(GraphRecordStoreUtilities.SOURCE + VisualConcept.VertexAttribute.LABEL), saveFile, ConstellationLoggerHelper.SUCCESS);
            final Properties p = new Properties();
            p.setProperty(AutosaveUtilities.ID, graph.getId());
            p.setProperty(AutosaveUtilities.NAME, gnode.getName());
            p.setProperty(AutosaveUtilities.PATH, gnode.getDataObject().getPrimaryFile().getPath());
            p.setProperty(AutosaveUtilities.UNSAVED, Boolean.toString(gnode.getDataObject().isInMemory()));
            p.setProperty(AutosaveUtilities.DT, ZonedDateTime.now().format(TemporalFormatting.ZONED_DATE_TIME_FORMATTER));
            try (OutputStream s = new FileOutputStream(new File(saveDir, gname + "_auto"))) {
                p.store(s, null);
            }
        } catch (IOException ex) {
            LOGGER.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
        }
    }
}
Also used : ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) HandleIoProgress(au.gov.asd.tac.constellation.utilities.gui.HandleIoProgress) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) GraphNode(au.gov.asd.tac.constellation.graph.node.GraphNode) IOException(java.io.IOException) Properties(java.util.Properties) Date(java.util.Date) GraphReadMethods(au.gov.asd.tac.constellation.graph.GraphReadMethods) ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) Graph(au.gov.asd.tac.constellation.graph.Graph) GraphJsonWriter(au.gov.asd.tac.constellation.graph.file.io.GraphJsonWriter) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 3 with HandleIoProgress

use of au.gov.asd.tac.constellation.utilities.gui.HandleIoProgress in project constellation by constellation-app.

the class AutosaveStartup method run.

@Override
public void run() {
    synchronized (String.class) {
        // Look for existing autosaved in-memory graphs.
        final File[] saveFiles = AutosaveUtilities.getAutosaves(FileExtensionConstants.STAR_AUTOSAVE);
        final long now = new Date().getTime();
        for (final File f : saveFiles) {
            try {
                final Properties props = new Properties();
                try (InputStream in = new FileInputStream(f)) {
                    props.load(in);
                }
                final String dtprop = props.getProperty(AutosaveUtilities.DT);
                final String name = props.getProperty(AutosaveUtilities.NAME);
                final boolean unsaved = "true".equals(props.getProperty(AutosaveUtilities.UNSAVED));
                if (dtprop != null) {
                    if (unsaved) {
                        final String msg = String.format("Graph %s (autosaved at %s).%nDo you want to recover it?", name != null ? name : "<unknown>", dtprop);
                        final NotifyDescriptor nd = new NotifyDescriptor.Confirmation(msg, "Autosaved graph", NotifyDescriptor.YES_NO_OPTION, NotifyDescriptor.QUESTION_MESSAGE);
                        if (DialogDisplayer.getDefault().notify(nd) == NotifyDescriptor.YES_OPTION) {
                            // Load the autosaved graph away from the EDT.
                            new Thread() {

                                @Override
                                public void run() {
                                    setName(AUTOSAVE_THREAD_NAME);
                                    final String loading = String.format("Loading autosaved graph %s", name);
                                    try {
                                        // Remove the "_auto" from the end and load the matching graph.
                                        String path = f.getPath();
                                        path = path.substring(0, path.length() - 5);
                                        final Graph g = new GraphJsonReader().readGraphZip(new File(path), new HandleIoProgress(loading));
                                        GraphOpener.getDefault().openGraph(g, name, false);
                                        AutosaveUtilities.deleteAutosave(f);
                                    } catch (GraphParseException | IOException ex) {
                                        LOGGER.log(Level.WARNING, "Error loading graph", ex);
                                        NotifyDisplayer.display("Error loading graph: " + ex.getMessage(), NotifyDescriptor.ERROR_MESSAGE);
                                    }
                                }
                            }.start();
                        } else {
                            // If the user doesn't want it we get rid of it.
                            AutosaveUtilities.deleteAutosave(f);
                        }
                    } else if (now - f.lastModified() > PURGE_PERIOD_MS) {
                        // This autosave is old enough to be purged; the user won't remember the details of the graph.
                        AutosaveUtilities.deleteAutosave(f);
                    } else {
                    // Do nothing
                    }
                } else {
                    // Some information about this autosave is missing so get rid of it.
                    AutosaveUtilities.deleteAutosave(f);
                }
            } catch (IOException ex) {
                LOGGER.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
            }
        }
        // Start autosaving in the background.
        Autosaver.schedule(0);
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) HandleIoProgress(au.gov.asd.tac.constellation.utilities.gui.HandleIoProgress) IOException(java.io.IOException) Properties(java.util.Properties) Date(java.util.Date) FileInputStream(java.io.FileInputStream) GraphJsonReader(au.gov.asd.tac.constellation.graph.file.io.GraphJsonReader) NotifyDescriptor(org.openide.NotifyDescriptor) Graph(au.gov.asd.tac.constellation.graph.Graph) File(java.io.File)

Example 4 with HandleIoProgress

use of au.gov.asd.tac.constellation.utilities.gui.HandleIoProgress in project constellation by constellation-app.

the class SaveTemplatePlugin method saveTemplate.

private void saveTemplate(final GraphReadMethods graph, final String templateName) throws PluginException {
    final Preferences prefs = NbPreferences.forModule(ApplicationPreferenceKeys.class);
    final String userDir = ApplicationPreferenceKeys.getUserDir(prefs);
    final File templateDir = new File(userDir, TEMPLATE_DIR);
    final File oldTemplate = new File(templateDir, NewSchemaGraphAction.getTemplateNames().get(templateName) + "/" + templateName);
    if (oldTemplate.exists()) {
        final boolean oldTemplateIsDeleted = oldTemplate.delete();
        if (!oldTemplateIsDeleted) {
        // TODO: Handle case where file not successfully deleted
        }
    }
    if (!templateDir.exists()) {
        templateDir.mkdir();
    }
    if (!templateDir.isDirectory()) {
        final String msg = String.format("Can't create template directory '%s'.", templateDir);
        final NotifyDescriptor nd = new NotifyDescriptor.Message(msg, NotifyDescriptor.ERROR_MESSAGE);
        DialogDisplayer.getDefault().notify(nd);
        return;
    }
    final File schemaDir = new File(templateDir, graph.getSchema().getFactory().getName());
    if (!schemaDir.exists()) {
        schemaDir.mkdir();
    }
    if (!schemaDir.isDirectory()) {
        final String msg = String.format("Can't create template directory '%s'.", schemaDir);
        final NotifyDescriptor nd = new NotifyDescriptor.Message(msg, NotifyDescriptor.ERROR_MESSAGE);
        DialogDisplayer.getDefault().notify(nd);
        return;
    }
    final File saveFile = new File(schemaDir, templateName);
    try {
        new GraphJsonWriter().writeTemplateToZip(graph, saveFile.getPath(), new HandleIoProgress("Saving Template..."));
    } catch (IOException ex) {
        throw new PluginException(this, PluginNotificationLevel.ERROR, "Failed to save template", ex);
    }
}
Also used : NotifyDescriptor(org.openide.NotifyDescriptor) GraphJsonWriter(au.gov.asd.tac.constellation.graph.file.io.GraphJsonWriter) HandleIoProgress(au.gov.asd.tac.constellation.utilities.gui.HandleIoProgress) PluginException(au.gov.asd.tac.constellation.plugins.PluginException) IOException(java.io.IOException) Preferences(java.util.prefs.Preferences) NbPreferences(org.openide.util.NbPreferences) File(java.io.File)

Example 5 with HandleIoProgress

use of au.gov.asd.tac.constellation.utilities.gui.HandleIoProgress in project constellation by constellation-app.

the class ExportToJsonPlugin method read.

@Override
public void read(final GraphReadMethods rg, final PluginInteraction interaction, final PluginParameters parameters) throws InterruptedException, PluginException {
    final String filename = parameters.getParameters().get(FILE_NAME_PARAMETER_ID).getStringValue();
    try {
        new GraphJsonWriter().writeGraphFile(rg, filename, new HandleIoProgress("Exporting..."));
        ConstellationLoggerHelper.exportPropertyBuilder(this, GraphRecordStoreUtilities.getVertices(rg, false, false, false).getAll(GraphRecordStoreUtilities.SOURCE + VisualConcept.VertexAttribute.LABEL), new File(filename), ConstellationLoggerHelper.SUCCESS);
    } catch (final IOException ex) {
        LOGGER.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
    }
}
Also used : GraphJsonWriter(au.gov.asd.tac.constellation.graph.file.io.GraphJsonWriter) HandleIoProgress(au.gov.asd.tac.constellation.utilities.gui.HandleIoProgress) IOException(java.io.IOException) File(java.io.File)

Aggregations

HandleIoProgress (au.gov.asd.tac.constellation.utilities.gui.HandleIoProgress)7 Graph (au.gov.asd.tac.constellation.graph.Graph)5 File (java.io.File)5 IOException (java.io.IOException)5 GraphJsonReader (au.gov.asd.tac.constellation.graph.file.io.GraphJsonReader)3 GraphJsonWriter (au.gov.asd.tac.constellation.graph.file.io.GraphJsonWriter)3 ReadableGraph (au.gov.asd.tac.constellation.graph.ReadableGraph)2 GraphParseException (au.gov.asd.tac.constellation.graph.file.io.GraphParseException)2 PluginException (au.gov.asd.tac.constellation.plugins.PluginException)2 RestServiceException (au.gov.asd.tac.constellation.webserver.restapi.RestServiceException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 Date (java.util.Date)2 Properties (java.util.Properties)2 NotifyDescriptor (org.openide.NotifyDescriptor)2 GraphReadMethods (au.gov.asd.tac.constellation.graph.GraphReadMethods)1 GraphNode (au.gov.asd.tac.constellation.graph.node.GraphNode)1 GraphRecordStore (au.gov.asd.tac.constellation.graph.processing.GraphRecordStore)1 IoProgress (au.gov.asd.tac.constellation.utilities.gui.IoProgress)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1