Source for gnu.java.awt.peer.gtk.GtkFileDialogPeer

   1: /* GtkFileDialogPeer.java -- Implements FileDialogPeer with GTK
   2:    Copyright (C) 1998, 1999, 2002, 2004, 2005  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package gnu.java.awt.peer.gtk;
  40: 
  41: import java.awt.Dialog;
  42: import java.awt.FileDialog;
  43: import java.awt.Graphics;
  44: import java.awt.event.PaintEvent;
  45: import java.awt.peer.FileDialogPeer;
  46: import java.io.File;
  47: import java.io.FilenameFilter;
  48: 
  49: public class GtkFileDialogPeer extends GtkDialogPeer implements FileDialogPeer
  50: {
  51:   static final String FS = System.getProperty("file.separator");
  52:   
  53:   private String currentFile = null;
  54:   private String currentDirectory = null;
  55:   private FilenameFilter filter;
  56: 
  57:   native void create (GtkContainerPeer parent, int mode);
  58:   native void connectSignals ();
  59:   native void nativeSetFile (String file);
  60:   public native String nativeGetDirectory();
  61:   public native void nativeSetDirectory(String directory);
  62:   native void nativeSetFilenameFilter (FilenameFilter filter);
  63: 
  64:   public void create()
  65:   {
  66:     create((GtkContainerPeer) awtComponent.getParent().getPeer(),
  67:            ((FileDialog) awtComponent).getMode());
  68: 
  69:     FileDialog fd = (FileDialog) awtComponent;
  70:     
  71:     nativeSetDirectory(System.getProperty("user.dir"));
  72:     setDirectory(fd.getDirectory());
  73:     setFile(fd.getFile());
  74: 
  75:     FilenameFilter filter = fd.getFilenameFilter();
  76:     if (filter != null)
  77:       setFilenameFilter(filter);
  78:   }
  79: 
  80:   public GtkFileDialogPeer (FileDialog fd)
  81:   {
  82:     super (fd);
  83:   }
  84: 
  85:   void setComponentBounds ()
  86:   {
  87:     if (awtComponent.getHeight () == 0
  88:         && awtComponent.getWidth () == 0)
  89:       {
  90:         int[] dims = new int[2];
  91:         gtkWidgetGetPreferredDimensions (dims);
  92: 
  93:         if (dims[0] != awtComponent.getWidth()
  94:             || dims[1] != awtComponent.getHeight())
  95:           awtComponent.setSize(dims[0], dims[1]);
  96:       }
  97:     super.setComponentBounds ();
  98:   }
  99: 
 100:   public void setFile (String fileName)
 101:   {
 102:     /* If nothing changed do nothing.  This usually happens because
 103:        the only way we have to set the file name in FileDialog is by
 104:        calling its SetFile which will call us back. */
 105:     if ((fileName == null && currentFile == null)
 106:         || (fileName != null && fileName.equals (currentFile)))
 107:       return;
 108: 
 109:     if (fileName == null || fileName.equals (""))
 110:       {
 111:         currentFile = "";
 112:         nativeSetFile ("");
 113:         return;
 114:       }
 115: 
 116:     // GtkFileChooser requires absolute filenames. If the given filename
 117:     // is not absolute, let's construct it based on current directory.
 118:     currentFile = fileName;
 119:     if (fileName.indexOf(FS) == 0)
 120:       nativeSetFile(fileName);
 121:     else
 122:       nativeSetFile(nativeGetDirectory() + FS + fileName);
 123:   }
 124: 
 125:   public void setDirectory (String directory)
 126:   {
 127:     /* If nothing changed so nothing.  This usually happens because
 128:        the only way we have to set the directory in FileDialog is by
 129:        calling its setDirectory which will call us back. */
 130:     if ((directory == null && currentDirectory == null)
 131:         || (directory != null && directory.equals(currentDirectory)))
 132:       return;
 133: 
 134:     if (directory == null || directory.equals(""))
 135:       {
 136:         currentDirectory = FS;
 137:         nativeSetDirectory(FS);
 138:         return;
 139:       }
 140:     
 141:     // GtkFileChooser requires absolute directory names. If the given directory
 142:     // name is not absolute, construct it based on current directory if it is not
 143:     // null. Otherwise, use FS.
 144:     currentDirectory = directory;
 145:     if (directory.indexOf(FS) == 0)
 146:       nativeSetDirectory(directory);
 147:     else
 148:       nativeSetDirectory(nativeGetDirectory() + FS + directory);
 149:   }
 150: 
 151:   public void setFilenameFilter (FilenameFilter filter)
 152:   {
 153:     this.filter = filter;
 154:     nativeSetFilenameFilter(filter);
 155:   }
 156: 
 157:   /* This method interacts with the native callback function of the
 158:      same name.  The native function will extract the filename from the
 159:      GtkFileFilterInfo object and send it to this method, which will
 160:      in turn call the filter's accept() method and give back the return
 161:      value. */
 162:   // called back by native side: filename_filter_cb
 163:   boolean filenameFilterCallback (String fullname) {
 164:     String filename = fullname.substring(fullname.lastIndexOf(FS) + 1);
 165:     String dirname = fullname.substring(0, fullname.lastIndexOf(FS));
 166:     File dir = new File(dirname);
 167:     return filter.accept(dir, filename);
 168:   }
 169: 
 170:   // Sun does not call FileDialog.update.
 171:   protected void updateComponent (PaintEvent event)
 172:   {
 173:     // Override GtkComponetPeer.updateComponent to do nothing.
 174:   }
 175: 
 176:   // called back by native side: handle_response_cb
 177:   // only called from the GTK thread
 178:   void gtkHideFileDialog () 
 179:   {
 180:     // hide calls back the peer's setVisible method, so locking is a
 181:     // problem.
 182:     ((Dialog) awtComponent).hide();
 183:   }
 184:   
 185:   // called back by native side: handle_response_cb
 186:   void gtkDisposeFileDialog () 
 187:   {
 188:     ((Dialog) awtComponent).dispose();
 189:   }
 190: 
 191:   // Callback to set the file and directory values when the user is finished
 192:   // with the dialog.
 193:   // called back by native side: handle_response_cb
 194:   void gtkSetFilename (String fileName)
 195:   {
 196:     FileDialog fd = (FileDialog) awtWidget;
 197:     if (fileName == null)
 198:       {
 199:         currentFile = null;
 200:         fd.setFile(null);
 201:         return;
 202:       }
 203: 
 204:     int sepIndex = fileName.lastIndexOf (FS);
 205:     if (sepIndex < 0)
 206:       {
 207:         /* This should never happen on Unix (all paths start with '/') */
 208:     currentFile = fileName;
 209:       }
 210:     else
 211:       {
 212:         if (fileName.length() > (sepIndex + 1))
 213:       {
 214:         String fn = fileName.substring (sepIndex + 1);
 215:         currentFile = fn;
 216:       }
 217:     else
 218:       {
 219:             currentFile = null;
 220:       }
 221: 
 222:         String dn = fileName.substring (0, sepIndex + 1);
 223:         currentDirectory = dn;
 224:         fd.setDirectory(dn);
 225:       }
 226: 
 227:     fd.setFile (currentFile);
 228:   }
 229: }