use of org.openide.util.io.ReaderInputStream in project gephi by gephi.
the class ImportControllerImpl method importFile.
public Container importFile(Reader reader, FileImporter importer, File file) {
// Create Container
final Container container = Lookup.getDefault().lookup(Container.Factory.class).newContainer();
// Report
Report report = new Report();
container.setReport(report);
if (importer instanceof FileImporter.FileAware) {
if (file == null) {
// There is no source file but the importer needs it, create temporary copy:
try {
file = TempDirUtils.createTempDir().createFile("file_copy");
try (FileOutputStream fos = new FileOutputStream(file)) {
FileUtil.copy(new ReaderInputStream(reader, "UTF-8"), fos);
}
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
((FileImporter.FileAware) importer).setFile(file);
} else {
importer.setReader(reader);
}
try {
if (importer.execute(container.getLoader())) {
if (importer.getReport() != null && importer.getReport() != report) {
report.append(importer.getReport());
}
report.close();
return container;
}
} catch (RuntimeException ex) {
throw ex;
} catch (Exception ex) {
throw new RuntimeException(ex);
} finally {
try {
reader.close();
} catch (IOException ex) {
// NOOP
}
}
return null;
}
use of org.openide.util.io.ReaderInputStream 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);
}
}
Aggregations