Class FileFilter

All Implemented Interfaces:
Buildable, Proxy

@Generated("org.javagi.JavaGI") public class FileFilter extends Filter implements Buildable

Filters files by name or mime type.

GtkFileFilter can be used to restrict the files being shown in a file chooser. Files can be filtered based on their name (with addPattern(String) or addSuffix(String)) or on their mime type (with addMimeType(String)).

Filtering by mime types handles aliasing and subclassing of mime types; e.g. a filter for text/plain also matches a file with mime type application/rtf, since application/rtf is a subclass of text/plain. Note that GtkFileFilter allows wildcards for the subtype of a mime type, so you can e.g. filter for image/\*.

Normally, file filters are used by adding them to a file chooser (see FileDialog.setFilters(ListModel)), but it is also possible to manually use a file filter on any FilterListModel containing GFileInfo objects.

GtkFileFilter as GtkBuildable

The GtkFileFilter implementation of the GtkBuildable interface supports adding rules using the <mime-types> and <patterns> and <suffixes> elements and listing the rules within. Specifying a <mime-type> or <pattern> or <suffix> has the same effect as as calling addMimeType(String) or addPattern(String) or addSuffix(String).

An example of a UI definition fragment specifying GtkFileFilter rules:

<object class="GtkFileFilter">
  <property name="name" translatable="yes">Text and Images</property>
  <mime-types>
    <mime-type>text/plain</mime-type>
    <mime-type>image/ *</mime-type>
  </mime-types>
  <patterns>
    <pattern>*.txt</pattern>
  </patterns>
  <suffixes>
    <suffix>png</suffix>
  </suffixes>
</object>
  • Constructor Details

    • FileFilter

      public FileFilter(MemorySegment address)
      Create a FileFilter instance for the provided memory address.
      Parameters:
      address - the memory address of the native object
    • FileFilter

      public FileFilter()
      Create a new FileFilter.
  • Method Details

    • getType

      public static @Nullable Type getType()
      Get the GType of the FileFilter class.
      Returns:
      the GType
    • getMemoryLayout

      public static MemoryLayout getMemoryLayout()
      The memory layout of the native struct.
      Returns:
      the memory layout
    • asParent

      protected FileFilter asParent()
      Return this instance as if it were its parent type. Comparable to the Java super keyword, but ensures the parent typeclass is also used in native code.
      Overrides:
      asParent in class Filter
      Returns:
      the instance as if it were its parent type
    • fromGvariant

      public static FileFilter fromGvariant(Variant variant)

      Deserialize a file filter from a GVariant.

      The variant must be in the format produced by toGvariant().

      Parameters:
      variant - an a{sv} GVariant
      Returns:
      a new GtkFileFilter object
    • addMimeType

      public void addMimeType(String mimeType)
      Adds a rule allowing a given mime type.
      Parameters:
      mimeType - name of a MIME type
    • addMimeTypes

      public void addMimeTypes(@Nullable String @Nullable [] mimeTypes)

      Adds a rule allowing a given array of mime types. It can for example be used with Gly.Loader.get_mime_types.

      This is equivalent to calling addMimeType(String) for all the supported mime types.

      Parameters:
      mimeTypes - a null-terminated array of mime types
      Since:
      4.22
    • addPattern

      public void addPattern(String pattern)

      Adds a rule allowing a shell style glob pattern.

      Note that it depends on the platform whether pattern matching ignores case or not. On Windows, it does, on other platforms, it doesn't.

      Parameters:
      pattern - a shell style glob pattern
    • addPixbufFormats

      @Deprecated public void addPixbufFormats()
      Deprecated.
      Use the api of your image loading framework (e.g. glycin) to enumerate supported formats

      Adds a rule allowing image files in the formats supported by GdkPixbuf.

      This is equivalent to calling addMimeType(String) for all the supported mime types.

    • addSuffix

      public void addSuffix(String suffix)

      Adds a suffix match rule to a filter.

      This is similar to adding a match for the pattern "*.suffix"

      An exaple to filter files with the suffix ".sub":

      gtk_file_filter_add_suffix (filter, "sub");
      

      Filters with multiple dots are allowed.

      In contrast to pattern matches, suffix matches are always case-insensitive.

      Parameters:
      suffix - filename suffix to match
      Since:
      4.4
    • getAttributes

      public String[] getAttributes()

      Gets the attributes that need to be filled in for the GFileInfo passed to this filter.

      This function will not typically be used by applications; it is intended for use in file chooser implementation.

      Returns:
      the attributes
    • getName

      public @Nullable String getName()

      Gets the human-readable name for the filter.

      See setName(String).

      Returns:
      the human-readable name of the filter
    • setName

      public void setName(@Nullable String name)

      Sets a human-readable name of the filter.

      This is the string that will be displayed in the user interface if there is a selectable list of filters.

      Parameters:
      name - the human-readable name for the filter
    • toGvariant

      public Variant toGvariant()
      Serialize a file filter to an a{sv} variant.
      Returns:
      a new, floating, GVariant
    • builder

      public static FileFilter.Builder<? extends FileFilter.Builder> builder()
      A FileFilter.Builder object constructs a FileFilter with the specified properties. Use the various set...() methods to set properties, and finish construction with FileFilter.Builder.build().
      Returns:
      the builder object