Java Code Examples for javax.swing.JFileChooser.getFileFilter()

The following are Jave code examples for showing how to use getFileFilter() of the javax.swing.JFileChooser class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: incubator-netbeans   File: BasicExportAction.java   View Source Code Vote up 4 votes
private SelectedFile selectExportTargetFile(final ExportProvider exportProvider) {
    File targetDir;
    String targetName;
    String defaultName = exportProvider.getViewName();

    // 1. let the user choose file or directory
    final JFileChooser chooser = getFileChooser();
    if (exportDir != null) {
        chooser.setCurrentDirectory(exportDir);
    }
    int result = chooser.showSaveDialog(WindowManager.getDefault().getRegistry().getActivated());
    if (result != JFileChooser.APPROVE_OPTION) {
        return null; // cancelled by the user
    }

    // 2. process both cases and extract file name and extension to use and set exported file type
    File file = chooser.getSelectedFile();
    String targetExt = null;
    FileFilter selectedFileFilter = chooser.getFileFilter();
    if (selectedFileFilter==null  // workaround for #227659
            ||  selectedFileFilter.getDescription().equals(Bundle.BasicExportAction_ExportDialogCSVFilter())) {
        targetExt=FILE_EXTENSION_CSV;
        exportedFileType=MODE_CSV;
    } else if (selectedFileFilter.getDescription().equals(Bundle.BasicExportAction_ExportDialogTXTFilter())) {
        targetExt=FILE_EXTENSION_TXT;
        exportedFileType=MODE_TXT;
    } else if (selectedFileFilter.getDescription().equals(Bundle.BasicExportAction_ExportDialogBINFilter())) {
        targetExt=FILE_EXTENSION_BIN;
        exportedFileType=MODE_BIN;
    }

    if (file.isDirectory()) { // save to selected directory under default name
        exportDir = file;
        targetDir = file;
        targetName = defaultName;
    } else { // save to selected file
        targetDir = fileChooser.getCurrentDirectory();
        String fName = file.getName();

        // divide the file name into name and extension
        if (fName.endsWith("."+targetExt)) {  // NOI18N
            int idx = fName.lastIndexOf('.'); // NOI18N
            targetName = fName.substring(0, idx);
        } else {            // no extension
            targetName=fName;
        }
    }

    // 3. set type of exported file and return a newly created FileObject

    return new BasicExportAction.SelectedFile(targetDir, targetName, targetExt);
}
 
Example 2
Project: incubator-netbeans   File: OpenFileAction.java   View Source Code Vote up 4 votes
/**
 * {@inheritDoc} Displays a file chooser dialog
 * and opens the selected files.
 */
@Override
public void actionPerformed(ActionEvent e) {
    if (running) {
        return;
    }
    try {
        running = true;
        JFileChooser chooser = prepareFileChooser();
        File[] files;
        try {
            if( Boolean.getBoolean("nb.native.filechooser") ) { //NOI18N
                String oldFileDialogProp = System.getProperty("apple.awt.fileDialogForDirectories"); //NOI18N
                System.setProperty("apple.awt.fileDialogForDirectories", "false"); //NOI18N
                FileDialog fileDialog = new FileDialog(WindowManager.getDefault().getMainWindow());
                fileDialog.setMode(FileDialog.LOAD);
                fileDialog.setDirectory(getCurrentDirectory().getAbsolutePath());
                fileDialog.setTitle(chooser.getDialogTitle());
                fileDialog.setVisible(true);
                if( null != oldFileDialogProp ) {
                    System.setProperty("apple.awt.fileDialogForDirectories", oldFileDialogProp); //NOI18N
                } else {
                    System.clearProperty("apple.awt.fileDialogForDirectories"); //NOI18N
                }

                if( fileDialog.getDirectory() != null && fileDialog.getFile() != null ) {
                    String selFile = fileDialog.getFile();
                    File dir = new File( fileDialog.getDirectory() );
                    files = new File[] { new File( dir, selFile ) };
                    currentDirectory = dir;
                } else {
                    throw new UserCancelException();
                }
            } else {
                files = chooseFilesToOpen(chooser);
                currentDirectory = chooser.getCurrentDirectory();
                if (chooser.getFileFilter() != null) { // #227187
                    currentFileFilter =
                            chooser.getFileFilter().getDescription();
                }
            }
        } catch (UserCancelException ex) {
            return;
        }
        for (int i = 0; i < files.length; i++) {
            OpenFile.openFile(files[i], -1);
        }
    } finally {
        running = false;
    }
}
 
Example 3
Project: AgentWorkbench   File: BasicGraphGui.java   View Source Code Vote up 4 votes
/**
 * Export the current graph as image by using a file selection dialog.
 */
private void exportAsImage() {

	String currentFolder = null;
	if (Application.getGlobalInfo() != null) {
		// --- Get the last selected folder of Agent.GUI ---
		currentFolder = Application.getGlobalInfo().getLastSelectedFolderAsString();
	}

	// --- Create instance of JFileChooser -----------------
	JFileChooser jfc = new JFileChooser();
	jfc.setMultiSelectionEnabled(false);
	jfc.setAcceptAllFileFilterUsed(false);

	// --- Add custom icons for file types. ----------------
	jfc.setFileView(new ImageFileView());
	// --- Add the preview pane. ---------------------------
	jfc.setAccessory(new ImagePreview(jfc));

	// --- Set the file filter -----------------------------
	String[] extensionsJPEG = { ImageUtils.jpg, ImageUtils.jpeg };

	ConfigurableFileFilter filterJPG = new ConfigurableFileFilter(extensionsJPEG, "JPEG - Image");
	ConfigurableFileFilter filterPNG = new ConfigurableFileFilter(ImageUtils.png, "PNG - File");
	ConfigurableFileFilter filterGIF = new ConfigurableFileFilter(ImageUtils.gif, "GIF - Image");

	jfc.addChoosableFileFilter(filterGIF);
	jfc.addChoosableFileFilter(filterJPG);
	jfc.addChoosableFileFilter(filterPNG);

	jfc.setFileFilter(filterPNG);

	// --- Maybe set the current directory -----------------
	if (currentFolder != null) {
		jfc.setCurrentDirectory(new File(currentFolder));
	}

	// === Show dialog and wait on user action =============
	int state = jfc.showSaveDialog(this);
	if (state == JFileChooser.APPROVE_OPTION) {

		ConfigurableFileFilter cff = (ConfigurableFileFilter) jfc.getFileFilter();
		String selectedExtension = cff.getFileExtension()[0];
		String mustExtension = "." + selectedExtension;

		File selectedFile = jfc.getSelectedFile();
		if (selectedFile != null) {
			String selectedPath = selectedFile.getAbsolutePath();
			if (selectedPath.endsWith(mustExtension) == false) {
				selectedPath = selectedPath + mustExtension;
			}
			// ---------------------------------------------
			// --- Export current display to image ---------
			// ---------------------------------------------
			this.exportAsImage(this.getVisualizationViewer(), selectedPath, selectedExtension);
			// ---------------------------------------------

			if (Application.getGlobalInfo() != null) {
				Application.getGlobalInfo().setLastSelectedFolder(jfc.getCurrentDirectory());
			}
		}
	} // end APPROVE_OPTION

}
 
Example 4
Project: AgentWorkbench   File: ChartEditorJPanel.java   View Source Code Vote up 4 votes
private void saveAsImage(){
	
	// --- Determine image size ----------------------------
	Integer imageWidth = Integer.parseInt(this.getTfImageWidth().getText());
	if (imageWidth==0) {
		JOptionPane.showMessageDialog(this, Language.translate("Bitte nur ganze Zahlen größer 0 eingeben!"), Language.translate("Fehlerhafte Eingabe"), JOptionPane.ERROR_MESSAGE);
		this.setImageDefaultValues();
		return;
	}
	Integer imageHeight = Integer.parseInt(this.getTfImageHeight().getText());
	if (imageHeight==0) {
		JOptionPane.showMessageDialog(this, Language.translate("Bitte nur ganze Zahlen größer 0 eingeben!"), Language.translate("Fehlerhafte Eingabe"), JOptionPane.ERROR_MESSAGE);
		this.setImageDefaultValues();
		return;
	}
	
	String currentFolder = null;
	if (Application.getGlobalInfo()!=null) {
		// --- Get the last selected folder of Agent.GUI ---
		currentFolder = Application.getGlobalInfo().getLastSelectedFolderAsString();
	}

	// --- Create instance of JFileChooser -----------------
	JFileChooser jfc = new JFileChooser();
	jfc.setMultiSelectionEnabled(false);
	jfc.setAcceptAllFileFilterUsed(false);

	// --- Add custom icons for file types. ----------------
	jfc.setFileView(new ImageFileView());
	// --- Add the preview pane. ---------------------------
	jfc.setAccessory(new ImagePreview(jfc));

	// --- Set the file filter -----------------------------
	String[] extensionsJPEG = {ImageUtils.jpg, ImageUtils.jpeg};

	ConfigurableFileFilter filterJPG = new ConfigurableFileFilter(extensionsJPEG, "JPEG - Image");
	ConfigurableFileFilter filterPNG = new ConfigurableFileFilter(ImageUtils.png, "PNG - File");
	ConfigurableFileFilter filterGIF = new ConfigurableFileFilter(ImageUtils.gif, "GIF - Image");

	jfc.addChoosableFileFilter(filterGIF);
	jfc.addChoosableFileFilter(filterJPG);
	jfc.addChoosableFileFilter(filterPNG);

	jfc.setFileFilter(filterPNG);

	// --- Maybe set the current directory -----------------
	if (currentFolder != null) {
		jfc.setCurrentDirectory(new File(currentFolder));
	}
	
	// === Show dialog and wait on user action =============
	int state = jfc.showSaveDialog(this);
	
	if (state == JFileChooser.APPROVE_OPTION) {
		ConfigurableFileFilter cff = (ConfigurableFileFilter) jfc.getFileFilter();
		String selectedExtension = cff.getFileExtension()[0];
		String mustExtension = "." + selectedExtension;

		File selectedFile = jfc.getSelectedFile();
		if (selectedFile != null) {
			String selectedPath = selectedFile.getAbsolutePath();
			if (selectedPath.endsWith(mustExtension) == false) {
				selectedPath = selectedPath + mustExtension;
			}
			
			BufferedImage image = this.exportChartAsImage(imageWidth, imageHeight);
			this.writeImageFile(image, selectedPath, selectedExtension);
			
			if (Application.getGlobalInfo() != null) {
				Application.getGlobalInfo().setLastSelectedFolder(jfc.getCurrentDirectory());
			}
		}
	}
}
 
Example 5
Project: jaer   File: AEPlayer.java   View Source Code Vote up 4 votes
/**
     * Called when user asks to open data file file dialog.
     */
    @Override
    public void openAEInputFileDialog() {
//        try{Thread.currentThread().sleep(200);}catch(InterruptedException e){}
        float oldScale = viewer.chipCanvas.getScale();
        fileChooser = new JFileChooser();
//            new TypeAheadSelector(fileChooser);
        //com.sun.java.plaf.windows.WindowsFileChooserUI;
//            fileChooser.addKeyListener(new KeyAdapter() {
//                public void keyTyped(KeyEvent e){
//                    System.out.println("keycode="+e.getKeyCode());
//                }
//            });
//            System.out.println("fileChooser.getUIClassID()="+fileChooser.getUIClassID());
//            KeyListener[] keyListeners=fileChooser.getKeyListeners();
        ChipDataFilePreview preview = new ChipDataFilePreview(fileChooser, viewer.getChip());
        // from book swing hacks
        new FileDeleter(fileChooser, preview);
        fileChooser.addPropertyChangeListener(preview);
        fileChooser.setAccessory(preview);
        String lastFilePath = AEViewer.prefs.get("AEViewer.lastFile", "");
        // get the last folder
        viewer.lastFile = new File(lastFilePath);
//            fileChooser.setFileFilter(datFileFilter);
        IndexFileFilter indexFileFilter = new IndexFileFilter();
        fileChooser.addChoosableFileFilter(indexFileFilter);
        DATFileFilter datFileFilter = new DATFileFilter();
        fileChooser.addChoosableFileFilter(datFileFilter);
        if (lastFilter == null) {
            fileChooser.setFileFilter(datFileFilter);
        } else {
            fileChooser.setFileFilter(lastFilter);
        }
        fileChooser.setCurrentDirectory(viewer.lastFile);
        // sets the working directory of the chooser
//            boolean wasPaused=isPaused();
        setPaused(true);
        try {
            int retValue = fileChooser.showOpenDialog(viewer);
            if (retValue == JFileChooser.APPROVE_OPTION) {
                lastFilter = fileChooser.getFileFilter();
                try {
                    viewer.lastFile = fileChooser.getSelectedFile();
                    if (viewer.lastFile != null) {
                        viewer.recentFiles.addFile(viewer.lastFile);
                    }
                    startPlayback(viewer.lastFile);
                } catch (IOException fnf) {
                    log.warning(fnf.toString());
                }
            } else {
                preview.showFile(null);
            }
        } catch (GLException e) {
            log.warning(e.toString());
            preview.showFile(null);
        }
        fileChooser = null;
//        viewer.chipCanvas.setScale(oldScale);
        // restore persistent scale so that we don't get tiny size on next startup
        setPaused(false);
    }