/** * Method that exports the current view to a file using a specified file type. * * @param component Drawable that will be exported. * @param mimeType File format as MIME type string. * @param file File to export to. * @param documentBounds Document boundary rectangle */ private void export(Drawable component, String mimeType, File file, Rectangle2D documentBounds) { FileOutputStream destination; try { destination = new FileOutputStream(file); } catch (FileNotFoundException ex) { // TODO Auto-generated catch block ex.printStackTrace(); return; } DrawableWriter writer = DrawableWriterFactory.getInstance().get(mimeType); try { writer.write( component, destination, documentBounds.getX(), documentBounds.getY(), documentBounds.getWidth(), documentBounds.getHeight()); } catch (IOException ex) { // TODO Auto-generated catch block ex.printStackTrace(); } finally { try { destination.close(); } catch (IOException ex2) { // TODO Auto-generated catch block ex2.printStackTrace(); } } }
/** * Creates a new panel instance and initializes it with a drawable component. * * @param drawable Drawable component. */ @SuppressWarnings("serial") public InteractivePanel(Drawable drawable) { super(drawable); printerJob = PrinterJob.getPrinterJob(); printerJob.setPrintable(this); List<IOCapabilities> exportFormats = DrawableWriterFactory.getInstance().getCapabilities(); exportImageChooser = new ExportChooser(true, exportFormats); exportImageChooser.setDialogTitle( Messages.getString("InteractivePanel.exportImageTitle")); // $NON-NLS-1$ actions = new ActionMap(); actions.put( "zoomIn", new AbstractAction( Messages.getString( // $NON-NLS-1$ "InteractivePanel.zoomIn")) { //$NON-NLS-1$ public void actionPerformed(ActionEvent e) { zoom(popupMenuPos, 1); } }); actions.put( "zoomOut", new AbstractAction( Messages.getString( // $NON-NLS-1$ "InteractivePanel.zoomOut")) { //$NON-NLS-1$ public void actionPerformed(ActionEvent e) { zoom(popupMenuPos, -1); } }); actions.put( "resetView", new AbstractAction( Messages.getString( // $NON-NLS-1$ "InteractivePanel.resetView")) { //$NON-NLS-1$ public void actionPerformed(ActionEvent e) { resetZoom(popupMenuPos); } }); actions.put( "exportImage", new AbstractAction( Messages.getString( // $NON-NLS-1$ "InteractivePanel.exportImage")) { //$NON-NLS-1$ public void actionPerformed(ActionEvent e) { int ret = exportImageChooser.showSaveDialog(InteractivePanel.this); // Clear artifacts of the file chooser repaint(); // If the user aborted we can stop if (ret != JFileChooser.APPROVE_OPTION) { return; } // If the user didn't select a file we can stop File file = exportImageChooser.getSelectedFile(); if (file == null) { return; } // If the selected an existing file we ask for permission // to overwrite it else if (file.exists()) { int retOverwrite = JOptionPane.showConfirmDialog( InteractivePanel.this, Messages.getString("InteractivePanel.exportExistsWarning"), // $NON-NLS-1$ Messages.getString("InteractivePanel.warning"), // $NON-NLS-1$ JOptionPane.YES_NO_OPTION); // Clear artifacts of the confirm dialog repaint(); if (retOverwrite == JOptionPane.NO_OPTION) { return; } } // Export current view to the selected file Drawable d = getDrawable(); ExportDialog ed = new ExportDialog(InteractivePanel.this, d); ed.setVisible(true); if (!ed.getUserAction().equals(ExportDialog.UserAction.APPROVE)) { return; } DrawableWriterFilter filter = (DrawableWriterFilter) exportImageChooser.getFileFilter(); export(d, filter.getWriterCapabilities().getMimeType(), file, ed.getDocumentBounds()); } }); actions.put( "print", new AbstractAction( Messages.getString( // $NON-NLS-1$ "InteractivePanel.print")) { //$NON-NLS-1$ public void actionPerformed(ActionEvent e) { if (printerJob.printDialog()) { try { printerJob.print(); } catch (PrinterException ex) { // TODO Show error dialog ex.printStackTrace(); } } } }); popupMenuEnabled = true; addMouseListener(new PopupListener()); setZoomable(true); setPannable(true); }