org.jfree.ui.ExtensionFileFilter Java Examples

The following examples show how to use org.jfree.ui.ExtensionFileFilter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractChartPanel.java    From rapidminer-studio with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Opens a file chooser and gives the user an opportunity to save the chart in PNG format.
 * 
 * @throws IOException
 *             if there is an I/O error.
 */

@Override
public void doSaveAs() throws IOException {

	JFileChooser fileChooser = new JFileChooser();
	fileChooser.setCurrentDirectory(this.defaultDirectoryForSaveAs);
	ExtensionFileFilter filter = new ExtensionFileFilter(localizationResources.getString("PNG_Image_Files"), ".png");
	fileChooser.addChoosableFileFilter(filter);

	int option = fileChooser.showSaveDialog(this);
	if (option == JFileChooser.APPROVE_OPTION) {
		String filename = fileChooser.getSelectedFile().getPath();
		if (isEnforceFileExtensions()) {
			if (!filename.endsWith(".png")) {
				filename = filename + ".png";
			}
		}
		ChartUtilities.saveChartAsPNG(new File(filename), this.chart, getWidth(), getHeight());
	}

}
 
Example #2
Source File: ChartPanel.java    From opensim-gui with Apache License 2.0 6 votes vote down vote up
/**
 * Opens a file chooser and gives the user an opportunity to save the chart
 * in PNG format.
 *
 * @throws IOException if there is an I/O error.
 */
public void doSaveAs() throws IOException {

    JFileChooser fileChooser = new JFileChooser();
    ExtensionFileFilter filter = new ExtensionFileFilter(
            localizationResources.getString("PNG_Image_Files"), ".png");
    fileChooser.addChoosableFileFilter(filter);

    int option = fileChooser.showSaveDialog(this);
    if (option == JFileChooser.APPROVE_OPTION) {
        String filename = fileChooser.getSelectedFile().getPath();
        if (isEnforceFileExtensions()) {
            if (!filename.endsWith(".png")) {
                filename = filename + ".png";
            }
        }
        ChartUtilities.saveChartAsPNG(new File(filename), this.chart, 
                getWidth(), getHeight());
    }

}
 
Example #3
Source File: AbstractFileSelectionAction.java    From ccu-historian with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Creates the file chooser.
 *
 * @return the initialized file chooser.
 */
protected JFileChooser createFileChooser() {
    final JFileChooser fc = new JFileChooser();
    fc.addChoosableFileFilter(
        new ExtensionFileFilter(getFileDescription(), getFileExtension())
    );
    fc.setMultiSelectionEnabled(false);
    fc.setCurrentDirectory(getCurrentDirectory());
    return fc;
}
 
Example #4
Source File: ChartPanel.java    From opensim-gui with Apache License 2.0 4 votes vote down vote up
private void createChartPrintPostScriptJob() {
// Use the pre-defined flavor for a Printable from an InputStream 
	DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;

		// Specify the type of the output stream 
	String psMimeType = DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType();

	// Locate factory which can export a GIF image stream as Postscript 
	StreamPrintServiceFactory[] factories =
	StreamPrintServiceFactory.lookupStreamPrintServiceFactories(
				flavor, psMimeType);
	if (factories.length == 0) {
		System.err.println("No suitable factories");
		System.exit(0); // FIXME too
	}
               // Obtain file name from user
               JFileChooser fileChooser = new JFileChooser();
               ExtensionFileFilter filter = new ExtensionFileFilter(
                       localizationResources.getString("PostScript_Files"), ".eps");
               fileChooser.addChoosableFileFilter(filter);
               String filename="";
               int option = fileChooser.showSaveDialog(this);
               if (option == JFileChooser.APPROVE_OPTION) {
                  filename = fileChooser.getSelectedFile().getPath();
                  if (isEnforceFileExtensions()) {
                     if (!filename.endsWith(".eps")) {
                        filename = filename + ".eps";
                     }
                  } else
                     return;
               }

	try {
		// Create a file for the exported postscript
		FileOutputStream fos = new FileOutputStream(filename);

		// Create a Stream printer for Postscript 
		StreamPrintService sps = factories[0].getPrintService(fos);

		// Create and call a Print Job 
		DocPrintJob pj = sps.createPrintJob();
		PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();

		Doc doc = new SimpleDoc(this, flavor, null);

		pj.print(doc, aset);
		fos.close();

	} catch (PrintException pe) { 
		System.err.println(pe);
	} catch (IOException ie) { 
		System.err.println(ie);
	}

  }
 
Example #5
Source File: ChartPanel.java    From opensim-gui with Apache License 2.0 4 votes vote down vote up
/**
 * Prints the chart on a single page.
 *
 * @param g  the graphics context.
 * @param pf  the page format to use.
 * @param pageIndex  the index of the page. If not <code>0</code>, nothing 
 *                   gets print.
 *
 * @return The result of printing.
 */
public int print(Graphics g, PageFormat pf, int pageIndex) {

   if (pageIndex != 0) {
      return NO_SUCH_PAGE;
   }
   /** this works but the curve is made of little pieces */
   Graphics2D g2 = (Graphics2D) g;
   double x = pf.getImageableX();
   double y = pf.getImageableY();
   double w = pf.getImageableWidth();
   double h = pf.getImageableHeight();
   this.chart.draw(g2, new Rectangle2D.Double(x, y, w, h), this.anchor,
           null);
   
   if (printToPrinter)  return PAGE_EXISTS;
   // The rest should be moved up to the export eps action listener so it's not done per page'
   // Show export dialog
   JFileChooser fileChooser = new JFileChooser();
   ExtensionFileFilter filter = new ExtensionFileFilter(
           localizationResources.getString("PostScript_Files"), ".eps");
   fileChooser.addChoosableFileFilter(filter);
   String filename="";
   int option = fileChooser.showSaveDialog(this);
   if (option == JFileChooser.APPROVE_OPTION) {
      filename = fileChooser.getSelectedFile().getPath();
      if (isEnforceFileExtensions()) {
         if (!filename.endsWith(".eps") || !filename.endsWith(".ps")) {
            filename = filename + ".eps";
         }
      } else
         return NO_SUCH_PAGE;
   }
    
   try {
      OutputStream out = new java.io.FileOutputStream(new File(filename));
      out = new java.io.BufferedOutputStream(out);

      //Instantiate the EPSDocumentGraphics2D instance
      EPSDocumentGraphics2D g2dps = new EPSDocumentGraphics2D(false);
      g2dps.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());
      //Set up the document size
      g2dps.setupDocument(out, (int)w, (int)h+200);

      //Paint a bounding box
      g2dps.drawRect((int)x, (int)y, (int)w, (int)h);
      this.chart.draw(g2dps, new Rectangle2D.Double(x, y, w, h), this.anchor,
              null);


      //A few rectangles rotated and with different color

      //Cleanup
      g2dps.finish();
      out.flush();
      out.close();
   }
   catch(java.io.IOException e){
      return NO_SUCH_PAGE;
   }
   return PAGE_EXISTS;
   
}