Java Code Examples for javax.swing.filechooser.FileFilter#accept()

The following examples show how to use javax.swing.filechooser.FileFilter#accept() . 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: JFileChooser.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * Sets the current file filter. The file filter is used by the
 * file chooser to filter out files from the user's view.
 *
 * @param filter the new current file filter to use
 * @see #getFileFilter
 */
@BeanProperty(preferred = true, description
        = "Sets the File Filter used to filter out files of type.")
public void setFileFilter(FileFilter filter) {
    FileFilter oldValue = fileFilter;
    fileFilter = filter;
    if (filter != null) {
        if (isMultiSelectionEnabled() && selectedFiles != null && selectedFiles.length > 0) {
            Vector<File> fList = new Vector<File>();
            boolean failed = false;
            for (File file : selectedFiles) {
                if (filter.accept(file)) {
                    fList.add(file);
                } else {
                    failed = true;
                }
            }
            if (failed) {
                setSelectedFiles((fList.size() == 0) ? null : fList.toArray(new File[fList.size()]));
            }
        } else if (selectedFile != null && !filter.accept(selectedFile)) {
            setSelectedFile(null);
        }
    }
    firePropertyChange(FILE_FILTER_CHANGED_PROPERTY, oldValue, fileFilter);
}
 
Example 2
Source File: JFileChooser.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Sets the current file filter. The file filter is used by the
 * file chooser to filter out files from the user's view.
 *
 * @param filter the new current file filter to use
 * @see #getFileFilter
 */
@BeanProperty(preferred = true, description
        = "Sets the File Filter used to filter out files of type.")
public void setFileFilter(FileFilter filter) {
    FileFilter oldValue = fileFilter;
    fileFilter = filter;
    if (filter != null) {
        if (isMultiSelectionEnabled() && selectedFiles != null && selectedFiles.length > 0) {
            Vector<File> fList = new Vector<File>();
            boolean failed = false;
            for (File file : selectedFiles) {
                if (filter.accept(file)) {
                    fList.add(file);
                } else {
                    failed = true;
                }
            }
            if (failed) {
                setSelectedFiles((fList.size() == 0) ? null : fList.toArray(new File[fList.size()]));
            }
        } else if (selectedFile != null && !filter.accept(selectedFile)) {
            setSelectedFile(null);
        }
    }
    firePropertyChange(FILE_FILTER_CHANGED_PROPERTY, oldValue, fileFilter);
}
 
Example 3
Source File: MiscUtil.java    From energy2d with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * If the user does not input the extension specified by the file filter, automatically augment the file name with the specified extension.
 */
public static String fileNameAutoExtend(FileFilter filter, File file) {
	if (filter == null)
		return file.getAbsolutePath();
	String description = filter.getDescription().toLowerCase();
	String extension = getExtensionInLowerCase(file);
	String filename = file.getAbsolutePath();
	if (extension != null) {
		if (!filter.accept(file)) {
			filename = file.getAbsolutePath().concat(".").concat(description);
		}
	} else {
		filename = file.getAbsolutePath().concat(".").concat(description);
	}
	return filename;
}
 
Example 4
Source File: Openfile.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
private boolean openSnapshot(final File file, final List<SnapshotCategory> snapshots) {
    if (file.isFile()) {
        for (SnapshotCategory s : snapshots) {
            FileFilter filter = s.getFileFilter();

            if (filter.accept(file)) {
                s.openSnapshot(file);
                return true;
            }
        }
    }
    return false;
}
 
Example 5
Source File: MaskFormActions.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
private void importMasks() {
    final JFileChooser fileChooser = new SnapFileChooser();
    fileChooser.setDialogTitle("Import Masks from file");
    final FileFilter bmdFilter = new SnapFileFilter("BITMASK_DEFINITION_FILE", ".bmd",
                                                    "Bitmask definition files (*.bmd)");
    fileChooser.addChoosableFileFilter(bmdFilter);
    final FileFilter bmdxFilter = new SnapFileFilter("BITMASK_DEFINITION_FILE_XML", ".bmdx",
                                                     "Bitmask definition xml files (*.bmdx)");
    fileChooser.addChoosableFileFilter(bmdxFilter);
    final FileFilter xmlFilter = new SnapFileFilter("XML", ".xml", "XML files (*.xml)");
    fileChooser.setFileFilter(xmlFilter);
    fileChooser.setCurrentDirectory(getDirectory());
    fileChooser.setMultiSelectionEnabled(true);
    if (fileChooser.showOpenDialog(SwingUtilities.getWindowAncestor(maskTopComponent)) == JFileChooser.APPROVE_OPTION) {
        final File file = fileChooser.getSelectedFile();
        if (file != null) {
            setDirectory(file.getAbsoluteFile().getParentFile());
            if (file.canRead()) {
                if (bmdFilter.accept(file)) {
                    importMaskFromBmd(file);
                } else if (bmdxFilter.accept(file)) {
                    importMasksFromBmdx(file);
                } else {
                    importMasksFromXml(file);
                }

            }
        }
    }
}
 
Example 6
Source File: RegexFileChooser.java    From wandora with GNU General Public License v3.0 5 votes vote down vote up
public static java.io.FileFilter ioFileFilter(final FileFilter ff){
    return new java.io.FileFilter(){
        public boolean accept(File pathname){
            return ff.accept(pathname);
        }
    };
}
 
Example 7
Source File: ComboFileFilter.java    From scifio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/** Accepts files with the proper filename prefix. */
@Override
public boolean accept(final File file) {
	for (final FileFilter filter : fileFilters) {
		if (filter.accept(file)) return true;
	}
	return false;
}
 
Example 8
Source File: MainForm.java    From zxpoly with GNU General Public License v3.0 4 votes vote down vote up
private void menuFileLoadSnapshotActionPerformed(ActionEvent evt) {
  stepSemaphor.lock();
  try {
    this.turnZxKeyboardOff();
    if (AppOptions.getInstance().isTestRomActive()) {
      final JHtmlLabel label = new JHtmlLabel(
          "<html><body>ZX-Spectrum 128 ROM is required to load snapshots.<br>Go to menu <b><i><a href=\"rom\">File->Options</i></b></i> and choose ROM 128.</body></html>");
      label.addLinkListener((source, link) -> {
        if ("rom".equals(link)) {
          SwingUtilities.windowForComponent(source).setVisible(false);
          SwingUtilities.invokeLater(() -> menuFileOptions.doClick());
        }
      });
      JOptionPane.showMessageDialog(MainForm.this, label, "ZX-Spectrum ROM 128 image is required",
          JOptionPane.WARNING_MESSAGE);
      return;
    }

    final FileFilter formatZ80 = new FormatZ80();
    final FileFilter formatSNA = new FormatSNA();
    final FileFilter formatZXP = new FormatZXP();
    final FileFilter formatSpec256 = new FormatSpec256();

    final FileFilter formatAll = new FileFilter() {
      @Override
      public boolean accept(File f) {
        return formatZ80.accept(f)
            || formatSpec256.accept(f)
            || formatSNA.accept(f)
            || formatZXP.accept(f);
      }

      @Override
      public String getDescription() {
        return "All snapshots (*.z80, *.sna, *.zip, *.zxp)";
      }
    };

    final AtomicReference<FileFilter> theFilter = new AtomicReference<>();
    final File selected =
        chooseFileForOpen("Select snapshot", this.lastSnapshotFolder, theFilter, formatAll,
            formatZ80, formatSpec256, formatSNA, formatZXP);

    if (selected != null) {
      this.board.forceResetAllCpu();
      this.board.resetIoDevices();

      this.lastSnapshotFolder = selected.getParentFile();
      try {
        if (theFilter.get() == formatAll) {
          if (formatZ80.accept(selected)) {
            theFilter.set(formatZ80);
          } else if (formatSNA.accept(selected)) {
            theFilter.set(formatSNA);
          } else if (formatZXP.accept(selected)) {
            theFilter.set(formatZXP);
          } else {
            theFilter.set(formatSpec256);
          }
        }

        final Snapshot selectedFilter = (Snapshot) theFilter.get();
        LOGGER.log(Level.INFO, "Loading snapshot " + selectedFilter.getName());
        selectedFilter.loadFromArray(selected, this.board, this.board.getVideoController(),
            FileUtils.readFileToByteArray(selected));
        this.menuOptionsZX128Mode.setState(this.board.getBoardMode() != BoardMode.ZXPOLY);
      } catch (Exception ex) {
        ex.printStackTrace();
        LOGGER.log(Level.WARNING, "Can't read snapshot file [" + ex.getMessage() + ']', ex);
        JOptionPane.showMessageDialog(this, "Can't read snapshot file [" + ex.getMessage() + ']',
            "Error", JOptionPane.ERROR_MESSAGE);
      }
    }
  } finally {
    this.turnZxKeyboardOn();
    stepSemaphor.unlock();
  }
}
 
Example 9
Source File: TFrame.java    From tracker with GNU General Public License v3.0 4 votes vote down vote up
/**
  * Loads an object with data from an XMLControl.
  *
  * @param control the control
  * @param obj the object
  * @return the loaded object
  */
 public Object loadObject(XMLControl control, Object obj) {
 	TFrame frame = (TFrame) obj;
 	FileFilter videoFilter = new VideoFileFilter();
   String[][] tabs = (String[][])control.getObject("tabs"); //$NON-NLS-1$
String base = control.getString("basepath"); //$NON-NLS-1$
File dataFile = null;
   if (tabs!=null) {
   	boolean prev = TrackerIO.loadInSeparateThread;
   	TrackerIO.loadInSeparateThread = false;
   	for (String[] next: tabs) {
   		File file = null;
   		Resource res = null;
   		if (base!=null) {
   			file = new File(base, next[1]); // next[1] is relative path
   			res = ResourceLoader.getResource(file.getPath());
   		}
   		if (res==null) {
   			file = new File(XML.getUserDirectory(), next[1]);
   			res = ResourceLoader.getResource(file.getPath());
   		}
   		if (res==null && next[0]!=null) {
   			file = new File(next[0]); // next[0] is absolute path
   			res = ResourceLoader.getResource(file.getPath());
   		}
   		if (res==null) {
         int i = JOptionPane.showConfirmDialog(frame, "\""+next[1]+"\" "                                       //$NON-NLS-1$ //$NON-NLS-2$
             +MediaRes.getString("VideoClip.Dialog.VideoNotFound.Message"),                                  //$NON-NLS-1$
             TrackerRes.getString("TFrame.Dialog.FileNotFound.Title"),                                   //$NON-NLS-1$
             JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
         if(i==JOptionPane.YES_OPTION) {
           TrackerIO.getChooser().setSelectedFile(file);
           java.io.File[] files = TrackerIO.getChooserFiles("open");                                         //$NON-NLS-1$
           if(files!=null) {
             file = files[0];
           }
           else continue;
         }
         else continue;
   		}
   		if (res!=null && !videoFilter.accept(file)) {
   			if (dataFile==null) dataFile = file;
   			TrackerIO.open(file, frame);
   		}
   	}
   	TrackerIO.loadInSeparateThread = prev;
   }
   int n = frame.getTab(dataFile);
   OSPLog.finest("selecting first tabset tab at index "+n); //$NON-NLS-1$
   frame.setSelectedTab(n);
   return obj;
 }