use of org.openide.filesystems.FileObject in project gephi by gephi.
the class DesktopImportControllerUI method importFile.
@Override
public void importFile(FileObject fileObject) {
try {
final FileImporter importer = controller.getFileImporter(FileUtil.toFile(fileObject));
if (importer == null) {
NotifyDescriptor.Message msg = new NotifyDescriptor.Message(NbBundle.getMessage(getClass(), "DesktopImportControllerUI.error_no_matching_file_importer"), NotifyDescriptor.WARNING_MESSAGE);
DialogDisplayer.getDefault().notify(msg);
return;
}
//MRU
MostRecentFiles mostRecentFiles = Lookup.getDefault().lookup(MostRecentFiles.class);
mostRecentFiles.addFile(fileObject.getPath());
ImporterUI ui = controller.getUI(importer);
if (ui != null) {
String title = NbBundle.getMessage(DesktopImportControllerUI.class, "DesktopImportControllerUI.file.ui.dialog.title", ui.getDisplayName());
JPanel panel = ui.getPanel();
FileImporter[] fi = (FileImporter[]) Array.newInstance(importer.getClass(), 1);
fi[0] = importer;
ui.setup(fi);
final DialogDescriptor dd = new DialogDescriptor(panel, title);
if (panel instanceof ValidationPanel) {
ValidationPanel vp = (ValidationPanel) panel;
vp.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
dd.setValid(!((ValidationPanel) e.getSource()).isProblem());
}
});
}
Object result = DialogDisplayer.getDefault().notify(dd);
if (!result.equals(NotifyDescriptor.OK_OPTION)) {
ui.unsetup(false);
return;
}
ui.unsetup(true);
}
LongTask task = null;
if (importer instanceof LongTask) {
task = (LongTask) importer;
}
//Execute task
fileObject = getArchivedFile(fileObject);
final String containerSource = fileObject.getNameExt();
final InputStream stream = fileObject.getInputStream();
String taskName = NbBundle.getMessage(DesktopImportControllerUI.class, "DesktopImportControllerUI.taskName", containerSource);
executor.execute(task, new Runnable() {
@Override
public void run() {
try {
Container container = controller.importFile(stream, importer);
if (container != null) {
container.setSource(containerSource);
finishImport(container);
}
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
}, taskName, errorHandler);
} catch (Exception ex) {
Logger.getLogger("").log(Level.WARNING, "", ex);
}
}
use of org.openide.filesystems.FileObject in project gephi by gephi.
the class PresetUtils method loadPresets.
private void loadPresets() {
FileObject folder = FileUtil.getConfigFile("previewpresets");
if (folder != null) {
for (FileObject child : folder.getChildren()) {
if (child.isValid() && child.hasExt("xml")) {
try {
InputStream stream = child.getInputStream();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(stream);
PreviewPreset preset = readXML(document);
addPreset(preset);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
}
use of org.openide.filesystems.FileObject in project gephi by gephi.
the class PresetUtils method savePreset.
public void savePreset(PreviewPreset preset) {
int exist = -1;
for (int i = 0; i < presets.size(); i++) {
PreviewPreset p = presets.get(i);
if (p.getName().equals(preset.getName())) {
exist = i;
break;
}
}
if (exist == -1) {
addPreset(preset);
} else {
presets.set(exist, preset);
}
try {
//Create file if dont exist
FileObject folder = FileUtil.getConfigFile("previewpresets");
if (folder == null) {
folder = FileUtil.getConfigRoot().createFolder("previewpresets");
}
FileObject presetFile = folder.getFileObject(preset.getName(), "xml");
if (presetFile == null) {
presetFile = folder.createData(preset.getName(), "xml");
}
//Create doc
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = factory.newDocumentBuilder();
final Document document = documentBuilder.newDocument();
document.setXmlVersion("1.0");
document.setXmlStandalone(true);
//Write doc
writeXML(document, preset);
//Write XML file
Source source = new DOMSource(document);
Result result = new StreamResult(FileUtil.toFile(presetFile));
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.transform(source, result);
} catch (Exception e) {
e.printStackTrace();
}
}
use of org.openide.filesystems.FileObject in project gephi by gephi.
the class DirectoryChooserUI method deleteAction.
// remove multiple directories
private void deleteAction() {
// fixed #97079 to be able to delete one or more folders
final TreePath[] nodePath = tree.getSelectionPaths();
if (nodePath == null) {
return;
}
String message = "";
if (nodePath.length == 1) {
File file = ((DirectoryNode) nodePath[0].getLastPathComponent()).getFile();
// Don't do anything if it's a special file
if (!canWrite(file)) {
return;
}
message = MessageFormat.format(getBundle().getString("MSG_Delete"), file.getName());
} else {
message = MessageFormat.format(getBundle().getString("MSG_Delete_Multiple"), nodePath.length);
}
int answer = JOptionPane.showConfirmDialog(fileChooser, message, getBundle().getString("MSG_Confirm"), JOptionPane.YES_NO_OPTION);
if (answer == JOptionPane.YES_OPTION) {
RequestProcessor.getDefault().post(new Runnable() {
DirectoryNode node;
ArrayList<File> list = new ArrayList<File>();
int cannotDelete;
ArrayList<DirectoryNode> nodes2Remove = new ArrayList<DirectoryNode>(nodePath.length);
@Override
public void run() {
if (!EventQueue.isDispatchThread()) {
// first pass, out of EQ thread, deletes files
setCursor(fileChooser, Cursor.WAIT_CURSOR);
cannotDelete = 0;
for (int i = 0; i < nodePath.length; i++) {
DirectoryNode nodeToDelete = (DirectoryNode) nodePath[i].getLastPathComponent();
try {
FileObject fo = FileUtil.toFileObject(nodeToDelete.getFile());
fo.delete();
nodes2Remove.add(nodeToDelete);
} catch (IOException ignore) {
cannotDelete++;
if (canWrite(nodeToDelete.getFile())) {
list.add(nodeToDelete.getFile());
}
}
}
// send to second pass
EventQueue.invokeLater(this);
} else {
// second pass, in EQ thread
for (DirectoryNode curNode : nodes2Remove) {
model.removeNodeFromParent(curNode);
}
setCursor(fileChooser, Cursor.DEFAULT_CURSOR);
if (cannotDelete > 0) {
String message = "";
if (cannotDelete == 1) {
message = cannotDelete + " " + getBundle().getString("MSG_Sing_Delete");
} else {
message = cannotDelete + " " + getBundle().getString("MSG_Plur_Delete");
}
setSelected((File[]) list.toArray(new File[list.size()]));
JOptionPane.showConfirmDialog(fileChooser, message, getBundle().getString("MSG_Confirm"), JOptionPane.OK_OPTION);
} else {
setSelected(new File[] { null });
setFileName("");
}
}
}
});
}
}
use of org.openide.filesystems.FileObject in project gephi by gephi.
the class SaveTask method run.
@Override
public void run() {
Progress.start(progressTicket);
Progress.setDisplayName(progressTicket, NbBundle.getMessage(SaveTask.class, "SaveTask.name"));
File writeFile = null;
try {
String tempFileName = file.getName() + "_temp" + System.currentTimeMillis();
writeFile = new File(file.getParent(), tempFileName);
FileOutputStream outputStream = null;
ZipOutputStream zipOut = null;
BufferedOutputStream bos = null;
DataOutputStream dos = null;
try {
//Stream
int zipLevel = NbPreferences.forModule(SaveTask.class).getInt(ZIP_LEVEL_PREFERENCE, 9);
outputStream = new FileOutputStream(writeFile);
zipOut = new ZipOutputStream(outputStream);
zipOut.setLevel(zipLevel);
bos = new BufferedOutputStream(zipOut);
dos = new DataOutputStream(bos);
//Providers and workspace
Collection<WorkspacePersistenceProvider> providers = PersistenceProviderUtils.getPersistenceProviders();
Workspace[] workspaces = project.getLookup().lookup(WorkspaceProviderImpl.class).getWorkspaces();
//Setup progress
Progress.switchToDeterminate(progressTicket, 1 + (1 + providers.size()) * workspaces.length);
//Write Project
writeProject(dos, zipOut);
Progress.progress(progressTicket);
//Write Workspace files
for (Workspace ws : workspaces) {
writeWorkspace(ws, dos, zipOut);
Progress.progress(progressTicket);
for (WorkspacePersistenceProvider provider : providers) {
if (provider instanceof WorkspaceXMLPersistenceProvider) {
writeWorkspaceChildrenXML(ws, (WorkspaceXMLPersistenceProvider) provider, dos, zipOut);
} else if (provider instanceof WorkspaceBytesPersistenceProvider) {
writeWorkspaceChildrenBytes(ws, (WorkspaceBytesPersistenceProvider) provider, dos, zipOut);
}
Progress.progress(progressTicket);
if (cancel) {
break;
}
}
if (cancel) {
break;
}
}
Progress.switchToIndeterminate(progressTicket);
zipOut.finish();
} finally {
if (dos != null) {
try {
dos.close();
} catch (IOException ex1) {
}
}
if (bos != null) {
try {
bos.close();
} catch (IOException ex1) {
}
}
if (zipOut != null) {
try {
zipOut.close();
} catch (IOException ex1) {
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException ex1) {
}
}
}
Progress.finish(progressTicket);
//Rename file
if (!cancel && writeFile.exists()) {
//Delete original file
if (file.exists()) {
file.delete();
}
FileObject tempFileObject = FileUtil.toFileObject(writeFile);
FileLock lock = tempFileObject.lock();
tempFileObject.rename(lock, getFileNameWithoutExt(file), getFileExtension(file));
lock.releaseLock();
}
} catch (Exception ex) {
if (ex instanceof GephiFormatException) {
throw (GephiFormatException) ex;
}
throw new GephiFormatException(SaveTask.class, ex);
} finally {
if (writeFile != null && writeFile.exists()) {
FileObject tempFileObject = FileUtil.toFileObject(writeFile);
try {
tempFileObject.delete();
} catch (IOException ex) {
}
}
}
Progress.finish(progressTicket);
}
Aggregations