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);
}
}
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);
}
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations