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.Window;
  45: import java.awt.event.ComponentEvent;
  46: import java.awt.peer.FileDialogPeer;
  47: import java.io.File;
  48: import java.io.FilenameFilter;
  49: 
  50: public class GtkFileDialogPeer extends GtkDialogPeer implements FileDialogPeer
  51: {
  52:   static final String FS = System.getProperty("file.separator");
  53:   
  54:   private String currentFile = null;
  55:   private String currentDirectory = null;
  56:   private FilenameFilter filter;
  57: 
  58:   native void create (GtkContainerPeer parent, int mode);
  59:   native void connectSignals ();
  60:   native void nativeSetFile (String file);
  61:   public native String nativeGetDirectory();
  62:   public native void nativeSetDirectory(String directory);
  63:   native void nativeSetFilenameFilter (FilenameFilter filter);
  64: 
  65:   public void create()
  66:   {
  67:     create((GtkContainerPeer) awtComponent.getParent().getPeer(),
  68:            ((FileDialog) awtComponent).getMode());
  69: 
  70:     FileDialog fd = (FileDialog) awtComponent;
  71: 
  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:       {
 121:         nativeSetFile (fileName);
 122:       }
 123:     else
 124:       {
 125:         nativeSetFile (nativeGetDirectory() + FS + fileName);
 126:       }
 127:   }
 128: 
 129:   public void setDirectory (String directory)
 130:   {
 131:     /* If nothing changed so nothing.  This usually happens because
 132:        the only way we have to set the directory in FileDialog is by
 133:        calling its setDirectory which will call us back. */
 134:     if ((directory == null && currentDirectory == null)
 135:         || (directory != null && directory.equals (currentDirectory)))
 136:       return;
 137: 
 138:     if (directory == null || directory.equals (""))
 139:       {
 140:         currentDirectory = FS;
 141:         nativeSetFile (FS);
 142:     return;
 143:       }
 144:       
 145:     currentDirectory = directory;
 146:     nativeSetDirectory (directory);
 147:   }
 148: 
 149:   public void setFilenameFilter (FilenameFilter filter)
 150:   {
 151:     this.filter = filter;
 152:     nativeSetFilenameFilter(filter);
 153:   }
 154: 
 155:   /* This method interacts with the native callback function of the
 156:      same name.  The native function will extract the filename from the
 157:      GtkFileFilterInfo object and send it to this method, which will
 158:      in turn call the filter's accept() method and give back the return
 159:      value. */
 160:   // called back by native side: filename_filter_cb
 161:   boolean filenameFilterCallback (String fullname) {
 162:     String filename = fullname.substring(fullname.lastIndexOf(FS) + 1);
 163:     String dirname = fullname.substring(0, fullname.lastIndexOf(FS));
 164:     File dir = new File(dirname);
 165:     return filter.accept(dir, filename);
 166:   }
 167: 
 168:   public Graphics getGraphics ()
 169:   {
 170:     // GtkFileDialog will repaint by itself
 171:     return null;
 172:   }
 173: 
 174:   // called back by native side: handle_response_cb
 175:   // only called from the GTK thread
 176:   void gtkHideFileDialog () 
 177:   {
 178:     // hide calls back the peer's setVisible method, so locking is a
 179:     // problem.
 180:     ((Dialog) awtComponent).hide();
 181:   }
 182:   
 183:   // called back by native side: handle_response_cb
 184:   void gtkDisposeFileDialog () 
 185:   {
 186:     ((Dialog) awtComponent).dispose();
 187:   }
 188: 
 189:   // Callback to set the file and directory values when the user is finished
 190:   // with the dialog.
 191:   // called back by native side: handle_response_cb
 192:   void gtkSetFilename (String fileName)
 193:   {
 194:     FileDialog fd = (FileDialog) awtWidget;
 195:     if (fileName == null)
 196:       {
 197:         currentFile = null;
 198:         fd.setFile(null);
 199:         return;
 200:       }
 201: 
 202:     int sepIndex = fileName.lastIndexOf (FS);
 203:     if (sepIndex < 0)
 204:       {
 205:         /* This should never happen on Unix (all paths start with '/') */
 206:     currentFile = fileName;
 207:       }
 208:     else
 209:       {
 210:         if (fileName.length() > (sepIndex + 1))
 211:       {
 212:         String fn = fileName.substring (sepIndex + 1);
 213:         currentFile = fn;
 214:       }
 215:     else
 216:       {
 217:             currentFile = null;
 218:       }
 219: 
 220:         String dn = fileName.substring (0, sepIndex + 1);
 221:         currentDirectory = dn;
 222:         fd.setDirectory(dn);
 223:       }
 224: 
 225:     fd.setFile (currentFile);
 226:   }
 227: }