MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: GUIKit FileDialog Widget

  • To: mathgroup at smc.vnet.net
  • Subject: [mg50198] Re: [mg50187] GUIKit FileDialog Widget
  • From: Jeff Adams <jeffa at wolfram.com>
  • Date: Fri, 20 Aug 2004 04:57:39 -0400 (EDT)
  • References: <200408191028.GAA22906@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

On Aug 19, 2004, at 5:28 AM, Peter Dickof wrote:

> When I bring up this widget it starts with a display of the file
> structure starting in a specific directory (My Network Data) and
> allows files of type "All files". I wish to use Mathematic/GUIKit to
> 1) change the initial directory to something of my choice.
> 2) provide a file filter string *.gif to restrict files displayed and
> selected.
>
> How can this be done, preferably without delving into Java (about
> which I know even less than GUIKit)?
>
> I am running Mathematica 5.0.1.0, GUIKit 1.0.0 on windows XP.
>
> Peter Dickof

Hi Peter,

The initial directory of the file chooser can be changed before it is 
displayed
to the user by setting the "currentDirectory" property of the chooser
to a Widget["File"] instance. The following example shows a slight 
modification
of the existing example open file dialog code which includes changing 
the
current directory to the $TopDirectory of the Mathematica installation:

GUIResolve[
   Script[
     Widget["FileDialog", Name -> "openFileDialog"];
     SetPropertyValue[{"openFileDialog", "multiSelectionEnabled"}, 
False];
     SetPropertyValue[{"openFileDialog", "fileSelectionMode"},
       PropertyValue[{"openFileDialog", "FILES_ONLY"}]];

     SetPropertyValue[{"openFileDialog", "currentDirectory"},
       Widget["File", InitialArguments -> {Script[ $TopDirectory]}]];

     returnValue = InvokeMethod[{"openFileDialog", "showOpenDialog"}, 
Null];
     If[returnValue === PropertyValue[{"openFileDialog", 
"APPROVE_OPTION"}],
       PropertyValue[{PropertyValue[{"openFileDialog", "selectedFile"}], 
"path"}],
       Null
       ]
     ]
   ]

Unfortunately the answer to your second question about filtering the 
files
displayed is a little more complicated.
It was an oversight to not include with GUIKit an abstraction of the 
way Java handles
file filtering on the file dialogs.

The good news is that I have just added such a Widget["FileFilter"] 
object to GUIKit so that
the next release will include an easy way of implementing file filters.
The bad news is that to implement this with the current GUIKit 1.0.0 
release you
would need to include your own Java class that handles the file 
filtering code.

You mention that you would rather not delve into Java to implement 
this, and I can
understand that, but perhaps for the benefit of others on the list who 
might be interested
in how this would be done I thought I'd outline the steps to make this 
happen
with GUIKit 1.0.0:

Implementing file filtering with the file dialogs:

The Widget["FileDialog"] widget has a "fileFilter" property which can 
be set to
any subclass of the Java class javax.swing.filechooser.FileFilter.
Alternatively you can also call InvokeMethod[{"openFileDialog", 
"addChoosableFileFilter"}, filter]
to add multiple file filters to the dialog that your user can choose 
from in the popdown menu.

One needs to implement, compile, and add the resulting .class file to 
any
  Mathematica AddOn Java/ subdirectory
so that GUIKit and JLink can find this compiled Java class.

The following source code for the class 
com.wolfram.guikit.swing.GUIKitFileFilter
is the basis for Widget["FileFilter"] that will be included in a future 
release of GUIKit:

----------
/*
  * @(#)GUIKitFileFilter.java
  *
  * Copyright (c) 2004 Wolfram Research Inc., All Rights Reserved.
  */
package com.wolfram.guikit.swing;

import java.io.File;
import javax.swing.filechooser.FileFilter;

/**
  * GUIKitFileFilter
  */
public class GUIKitFileFilter extends FileFilter {

   private String description = "Unnamed file filter";
   private String[] extensions = null;

	public GUIKitFileFilter() {
		super();
		}

   public boolean accept(File f) {
     if (f.isDirectory()) {
       return true;
       }
     String extension = getExtension(f);
     if (extension != null && extensions != null) {
       for (int i = 0; i < extensions.length; ++i) {
         if (extension.equalsIgnoreCase(extensions[i])) return true;
         }
       }
     return false;
     }

   public String getDescription() {return description;}
   public void setDescription(String d) {description = d;}

   public String[] getExtensions() {return extensions;}
   public void setExtensions(String[] ex) {extensions = ex;}

   /*
    * Get the extension of a file.
    */
   public static String getExtension(File f) {
     String ext = null;
     String s = f.getName();
     int i = s.lastIndexOf('.');
     if (i > 0 &&  i < s.length() - 1) {
       ext = s.substring(i+1).toLowerCase();
       }
     return ext;
     }

   }
----------

Once you compile and include this class (or any variant of it you 
prefer)
  in your distribution you can create a widget instance of it
setting its description and extensions properties like so:

Widget["class:com.wolfram.guikit.swing.GUIKitFileFilter", {
   "description" -> "GIF files", "extensions" -> {"gif"} }]

You could then include this with the previous file dialog code like so:

GUIResolve[
   Script[
     Widget["FileDialog", Name -> "openFileDialog"];
     SetPropertyValue[{"openFileDialog", "multiSelectionEnabled"}, 
False];
     SetPropertyValue[{"openFileDialog", "fileSelectionMode"},
       PropertyValue[{"openFileDialog", "FILES_ONLY"}]];

     SetPropertyValue[{"openFileDialog", "currentDirectory"},
       Widget["File", InitialArguments -> {Script[ $TopDirectory]}]];
     SetPropertyValue[{"openFileDialog", "fileFilter"},
       Widget["class:com.wolfram.guikit.swing.GUIKitFileFilter", {
         "description" -> "GIF Files", "extensions" -> {"gif"}}] ];

     returnValue = InvokeMethod[{"openFileDialog", "showOpenDialog"}, 
Null];
     If[returnValue === PropertyValue[{"openFileDialog", 
"APPROVE_OPTION"}],
       PropertyValue[{PropertyValue[{"openFileDialog", "selectedFile"}], 
"path"}],
       Null
       ]
     ]
   ]

to provide filtered file selection only on files with the extension 
"gif"

I hope some of this information proves useful to you,

Jeff Adams
Wolfram Research


  • Prev by Date: Re: Re: FindMinimum and the minimum-radius circle
  • Next by Date: 2005 Geonetry Calendars
  • Previous by thread: GUIKit FileDialog Widget
  • Next by thread: Re: Plotting a contour plot with cylindrical co-ordinates