@Override public void importFiles(FileObject[] fileObjects) { try { Map<ImporterUI, List<FileImporter>> importerUIs = new HashMap<ImporterUI, List<FileImporter>>(); List<FileImporter> importers = new ArrayList<FileImporter>(); for (FileObject fileObject : fileObjects) { 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; } importers.add(importer); ImporterUI ui = controller.getUI(importer); if (ui != null) { List<FileImporter> l = importerUIs.get(ui); if (l == null) { l = new ArrayList<FileImporter>(); importerUIs.put(ui, l); } l.add(importer); } // MRU MostRecentFiles mostRecentFiles = Lookup.getDefault().lookup(MostRecentFiles.class); mostRecentFiles.addFile(fileObject.getPath()); } 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(); ui.setup(entry.getValue().toArray(new FileImporter[0])); 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); } final List<Container> result = new ArrayList<Container>(); for (int i = 0; i < importers.size(); i++) { final FileImporter importer = importers.get(i); FileObject fileObject = fileObjects[i]; 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); result.add(container); } } catch (Exception ex) { throw new RuntimeException(ex); } } }, taskName, errorHandler); } executor.execute( null, new Runnable() { @Override public void run() { if (!result.isEmpty()) { finishImport(result.toArray(new Container[0])); } } }); } catch (Exception ex) { Logger.getLogger("").log(Level.WARNING, "", ex); } }
@Override public void importFile(final Reader reader, String importerName) { try { final FileImporter importer = controller.getFileImporter(importerName); 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; } ImporterUI ui = controller.getUI(importer); if (ui != null) { ui.setup(new FileImporter[] {importer}); String title = NbBundle.getMessage( DesktopImportControllerUI.class, "DesktopImportControllerUI.file.ui.dialog.title", ui.getDisplayName()); JPanel panel = ui.getPanel(); 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 final String containerSource = NbBundle.getMessage( DesktopImportControllerUI.class, "DesktopImportControllerUI.streamSource", importerName); String taskName = NbBundle.getMessage( DesktopImportControllerUI.class, "DesktopImportControllerUI.taskName", containerSource); executor.execute( task, new Runnable() { @Override public void run() { try { Container container = controller.importFile(reader, 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); } }
@Test public void testPNG() { try { // Init a project - and therefore a workspace ProjectController pc = Lookup.getDefault().lookup(ProjectController.class); pc.newProject(); Workspace workspace = pc.getCurrentWorkspace(); // Append container to graph structure String sample = "/org/gephi/desktop/welcome/samples/Les Miserables.gexf"; final InputStream stream = WelcomeTopComponent.class.getResourceAsStream(sample); ImportController importController = Lookup.getDefault().lookup(ImportController.class); FileImporter fileImporter = importController.getFileImporter(".gexf"); Container container = importController.importFile(stream, fileImporter); importController.process(container, new DefaultProcessor(), workspace); // Add self loop GraphController graphController = Lookup.getDefault().lookup(GraphController.class); Graph graph = graphController.getModel().getGraph(); Node node = graph.getNode(12); System.out.println("Self loop " + node.getNodeData().getLabel()); graph.addEdge(graphController.getModel().factory().newEdge(node, node, 31, true)); // Set label edges for (Edge edge : graphController.getModel().getGraph().getEdges()) { edge.getEdgeData().setLabel("Label test"); } // svg document // attribute ??? AttributeController attributeController = Lookup.getDefault().lookup(AttributeController.class); AttributeModel attributeModel = null; // preview PreviewController previewController = Lookup.getDefault().lookup(PreviewController.class); PreviewModel previewModel = previewController.getModel(workspace); PreviewProperties previewProperties = previewModel.getProperties(); previewController.refreshPreview(workspace); previewModel.getProperties().putValue("width", 1000); previewModel.getProperties().putValue("height", 1000); ProcessingTarget target = (ProcessingTarget) previewController.getRenderTarget(RenderTarget.PROCESSING_TARGET, workspace); previewProperties.putValue(LegendModel.LEGEND_PROPERTIES, new LegendModel(workspace)); LegendModel legendManager = previewProperties.getValue(LegendModel.LEGEND_PROPERTIES); Integer itemIndex = legendManager.getCurrentIndex(); // creating item Item item = addGroupsItem(itemIndex, graph, attributeModel); // Item item = addTextItem(itemIndex, graph, attributeModel); // add item LegendController.getInstance().addItemToLegendModel(workspace, item); // render previewController.refreshPreview(workspace); previewController.render(target); File fileToSave = new File("/Volumes/edubecks/edubecks/Untitled.png"); OutputStream fos = new FileOutputStream(fileToSave); Writer writer = new OutputStreamWriter(fos, "UTF-8"); // // saving // int width = 1000; // int height = 1000; // PGraphicsJava2D pg2 = (PGraphicsJava2D) target.getGraphics(); // BufferedImage img = new BufferedImage(width, height, // BufferedImage.TYPE_INT_ARGB); // // System.out.println("@Var: pg2.pixels: "+pg2.pixels); // img.setRGB(0, 0, width, height, pg2.pixels, 0, width); // ImageIO.write(img, "png", fileToSave); // stream.close(); PNGExporter pngExporter = new PNGExporter(); // pngExporter.setHeight(2000); // pngExporter.setWidth(2000); pngExporter.setWorkspace(workspace); pngExporter.setOutputStream(fos); pngExporter.execute(); } catch (Exception e) { Exceptions.printStackTrace(e); } }