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<>();
int cannotDelete;
ArrayList<DirectoryNode> nodes2Remove = new ArrayList<>(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 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");
}
// Safe filename
String filename = DigestUtils.sha1Hex(preset.getName());
FileObject presetFile = folder.getFileObject(filename, "xml");
if (presetFile == null) {
presetFile = folder.createData(filename, "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
try (OutputStream outputStream = presetFile.getOutputStream()) {
Source source = new DOMSource(document);
Result result = new StreamResult(outputStream);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.transform(source, result);
}
} catch (Exception ex) {
Exceptions.printStackTrace(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) {
Exceptions.printStackTrace(ex);
}
}
}
}
}
use of org.openide.filesystems.FileObject in project gephi by gephi.
the class DesktopImportControllerUI method importFiles.
private void importFiles(final Reader[] readers, final FileImporter[] importers, FileObject[] fileObjects) {
try {
File[] files = new File[readers.length];
Map<ImporterUI, List<FileImporter>> importerUIs = new HashMap<>();
for (int i = 0; i < importers.length; i++) {
FileImporter importer = importers[i];
ImporterUI ui = controller.getUI(importer);
if (ui != null) {
List<FileImporter> l = importerUIs.get(ui);
if (l == null) {
l = new ArrayList<>();
importerUIs.put(ui, l);
}
l.add(importer);
}
if (importer instanceof FileImporter.FileAware) {
try (Reader reader = readers[i]) {
File file = null;
if (fileObjects != null) {
file = FileUtil.toFile(fileObjects[i]);
}
if (file == null) {
// There is no source file but the importer needs it, create temporary copy:
String fileName = "tmp_file" + 1;
String charset = "UTF-8";
if (fileObjects != null && fileObjects[i] != null) {
// Netbeans FileUtil.toFile bug returning null?? Try to recover:
fileName = fileObjects[i].getNameExt();
CharsetToolkit charsetToolkit = new CharsetToolkit(fileObjects[i].getInputStream());
charset = charsetToolkit.getCharset().name();
}
file = TempDirUtils.createTempDir().createFile(fileName);
try (FileOutputStream fos = new FileOutputStream(file)) {
FileUtil.copy(new ReaderInputStream(reader, charset), fos);
}
}
files[i] = file;
((FileImporter.FileAware) importer).setFile(file);
}
}
}
for (Map.Entry<ImporterUI, List<FileImporter>> entry : importerUIs.entrySet()) {
ImporterUI ui = entry.getKey();
String title = NbBundle.getMessage(DesktopImportControllerUI.class, "DesktopImportControllerUI.file.ui.dialog.title", ui.getDisplayName());
JPanel panel = ui.getPanel();
FileImporter[] fi = (FileImporter[]) entry.getValue().toArray((FileImporter[]) Array.newInstance(entry.getValue().get(0).getClass(), 0));
ui.setup(fi);
if (panel != null) {
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;
}
}
if (ui instanceof ImporterUI.WithWizard) {
boolean finishedOk = showWizard(ui, ((ImporterUI.WithWizard) ui).getWizardDescriptor());
if (!finishedOk) {
ui.unsetup(false);
return;
}
}
ui.unsetup(true);
}
final List<Container> results = new ArrayList<>();
for (int i = 0; i < importers.length; i++) {
doImport(results, readers[i], files[i], importers[i]);
}
executor.execute(null, new Runnable() {
@Override
public void run() {
if (!results.isEmpty()) {
finishImport(results.toArray(new Container[0]));
}
}
});
} catch (Exception ex) {
Exceptions.printStackTrace(ex);
}
}
use of org.openide.filesystems.FileObject in project gephi by gephi.
the class CommandLineProcessor method process.
@Override
public void process(Env env, Map values) throws CommandException {
List<String> filenameList = new ArrayList<>();
Object obj = values.get(openOption);
if (obj != null) {
filenameList.addAll(Arrays.asList((String[]) obj));
}
obj = values.get(openOption2);
if (obj != null) {
filenameList.addAll(Arrays.asList((String[]) obj));
}
try {
for (int i = 0; i < filenameList.size(); i++) {
File file = new File(filenameList.get(i));
if (!file.isAbsolute()) {
file = new File(env.getCurrentDirectory(), filenameList.get(i));
}
FileObject fileObject = FileUtil.toFileObject(file);
if (!file.exists()) {
NotifyDescriptor.Message msg = new NotifyDescriptor.Message(NbBundle.getMessage(CommandLineProcessor.class, "CommandLineProcessor.fileNotFound", file.getName()), NotifyDescriptor.WARNING_MESSAGE);
DialogDisplayer.getDefault().notify(msg);
return;
}
if (fileObject.hasExt(GEPHI_EXTENSION)) {
ProjectControllerUI pc = Lookup.getDefault().lookup(ProjectControllerUI.class);
try {
pc.openProject(file);
} catch (Exception ew) {
Exceptions.printStackTrace(ew);
NotifyDescriptor.Message msg = new NotifyDescriptor.Message(NbBundle.getMessage(CommandLineProcessor.class, "CommandLineProcessor.openGephiError"), NotifyDescriptor.WARNING_MESSAGE);
DialogDisplayer.getDefault().notify(msg);
}
return;
} else {
ImportControllerUI importController = Lookup.getDefault().lookup(ImportControllerUI.class);
if (importController.getImportController().isFileSupported(FileUtil.toFile(fileObject))) {
importController.importFile(fileObject);
} else {
NotifyDescriptor.Message msg = new NotifyDescriptor.Message(NbBundle.getMessage(CommandLineProcessor.class, "CommandLineProcessor.fileNotSupported"), NotifyDescriptor.WARNING_MESSAGE);
DialogDisplayer.getDefault().notify(msg);
}
}
}
} catch (OutOfMemoryError ex) {
System.gc();
NotifyDescriptor nd = new NotifyDescriptor.Message(MEMORY_ERROR, NotifyDescriptor.ERROR_MESSAGE);
DialogDisplayer.getDefault().notify(nd);
} catch (Exception ex) {
Exceptions.printStackTrace(ex);
NotifyDescriptor nd = new NotifyDescriptor.Message("CommandLineParsing " + ex.getMessage(), NotifyDescriptor.ERROR_MESSAGE);
DialogDisplayer.getDefault().notify(nd);
}
}
Aggregations