/******************************************************************************* * Copyright (c) 2000, 2006 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ package org.eclipse.swt.widgets; import org.eclipse.swt.graphics.*; import org.eclipse.swt.internal.*; import org.eclipse.swt.internal.motif.*; import org.eclipse.swt.*; /** * Instances of this class allow the user to navigate * the file system and select or enter a file name. *
* Note: Only one of the styles SAVE and OPEN may be specified. *
* IMPORTANT: This class is intended to be subclassed only * within the SWT implementation. *
*/ public class FileDialog extends Dialog { int dialog; String [] filterNames = new String [0]; String [] filterExtensions = new String [0]; String [] fileNames = new String [0]; String fileName = ""; //$NON-NLS-1$ String filterPath = ""; //$NON-NLS-1$ String fullPath; int filterIndex = -1; boolean overwrite = false; static final String FILTER = "*"; //$NON-NLS-1$ static final char SEPARATOR = System.getProperty ("file.separator").charAt (0); //$NON-NLS-1$ /** * Constructs a new instance of this class given only its parent. * * @param parent a shell which will be the parent of the new instance * * @exception IllegalArgumentException
* The style value is either one of the style constants defined in
* class SWT
which is applicable to instances of this
* class, or must be built by bitwise OR'ing together
* (that is, using the int
"|" operator) two or more
* of those SWT
style constants. The class description
* lists the style constants that are applicable to the class.
* Style bits are also inherited from superclasses.
*
* This is an index into the FilterExtensions array and * the FilterNames array. *
* * @return index the file extension filter index * * @see #getFilterExtensions * @see #getFilterNames * * @since 3.4 */ public int getFilterIndex () { return filterIndex; } /** * Returns the names that describe the filter extensions * which the dialog will use to filter the files it shows. * * @return the list of filter names */ public String [] getFilterNames () { return filterNames; } /** * Returns the directory path that the dialog will use, or an empty * string if this is not set. File names in this path will appear * in the dialog, filtered according to the filter extensions. * * @return the directory path string * * @see #setFilterExtensions */ public String getFilterPath () { return filterPath; } /** * Returns the flag that the dialog will use to * determine whether to prompt the user for file * overwrite if the selected file already exists. * * @return true if the dialog will prompt for file overwrite, false otherwise * * @since 3.4 */ public boolean getOverwrite () { return overwrite; } int itemSelected (int widget, int client, int call) { /* this callback will only be hooked if the dialog style is MULTI */ int fileList = OS.XmFileSelectionBoxGetChild (dialog, OS.XmDIALOG_LIST); if (fileList == 0) return 0; int selectionText = OS.XmFileSelectionBoxGetChild (dialog, OS.XmDIALOG_TEXT); if (selectionText == 0) return 0; int [] argList = {OS.XmNselectedItems, 0, OS.XmNselectedItemCount, 0}; OS.XtGetValues (fileList, argList, argList.length / 2); int items = argList [1], itemCount = argList [3]; int ptr = 0; if (itemCount == 0) { int [] argList2 = {OS.XmNdirectory, 0}; OS.XtGetValues (dialog, argList2, argList2.length / 2); ptr = argList2 [1]; } else { int [] buffer = new int [1]; OS.memmove (buffer, items, 4); ptr = buffer [0]; } if (ptr == 0) return 0; Display display = parent.getDisplay (); int [] table = new int [] {display.tabMapping, display.crMapping}; int address = OS.XmStringUnparse ( ptr, null, OS.XmCHARSET_TEXT, OS.XmCHARSET_TEXT, table, table.length, OS.XmOUTPUT_ALL); if (itemCount == 0) OS.XmStringFree (ptr); if (address == 0) return 0; int length = OS.strlen (address); byte [] buffer = new byte [length + 1]; OS.memmove (buffer, address, length); OS.XtFree (address); OS.XmTextSetString (selectionText, buffer); OS.XmTextSetInsertionPosition (selectionText, OS.XmTextGetLastPosition (selectionText)); return 0; } int okPressed (int widget, int client, int call) { String fullPath = null, fileName = null; String [] fileNames = null; int [] argList = {OS.XmNdirSpec, 0, OS.XmNdirectory, 0}; OS.XtGetValues (dialog, argList, argList.length / 2); int xmString1 = argList [1]; Display display = parent.getDisplay (); int [] table = new int [] {display.tabMapping, display.crMapping}; int ptr = OS.XmStringUnparse ( xmString1, null, OS.XmCHARSET_TEXT, OS.XmCHARSET_TEXT, table, table.length, OS.XmOUTPUT_ALL); if (ptr != 0) { int length = OS.strlen (ptr); byte [] buffer = new byte [length]; OS.memmove (buffer, ptr, length); OS.XtFree (ptr); /* Use the character encoding for the default locale */ fullPath = new String (Converter.mbcsToWcs (null, buffer)).trim(); } OS.XmStringFree (xmString1); filterIndex = filterExtensions == null || filterExtensions.length == 0 ? -1 : 0; if ((style & SWT.MULTI) != 0) { int fileList = OS.XmFileSelectionBoxGetChild (dialog, OS.XmDIALOG_LIST); if (fileList == 0) return 0; int [] argList2 = {OS.XmNselectedItems, 0, OS.XmNselectedItemCount, 0}; OS.XtGetValues (fileList, argList2, argList2.length / 2); int items = argList2 [1], itemCount = argList2 [3]; int [] buffer1 = new int [1]; fileNames = new String [itemCount]; boolean match = false; for (int i = 0; i < itemCount; i++) { OS.memmove (buffer1, items, 4); ptr = buffer1 [0]; int address = OS.XmStringUnparse ( ptr, null, OS.XmCHARSET_TEXT, OS.XmCHARSET_TEXT, table, table.length, OS.XmOUTPUT_ALL); if (address != 0) { int length = OS.strlen (address); byte [] buffer = new byte [length]; OS.memmove (buffer, address, length); OS.XtFree (address); /* Use the character encoding for the default locale */ String fullFilename = new String (Converter.mbcsToWcs (null, buffer)); int index = fullFilename.lastIndexOf (SEPARATOR); fileNames [i] = fullFilename.substring (index + 1, fullFilename.length ()); if (fullFilename.equals (fullPath)) match = true; } items += 4; } if (match) { fileName = fileNames [0]; } else { /* The user has modified the text field such that it doesn't match any * of the selected files, so use this value instead */ int index = fullPath.lastIndexOf (SEPARATOR); fileName = fullPath.substring (index + 1, fullPath.length ()); fileNames = new String [] {fileName}; } } else { int index = fullPath.lastIndexOf (SEPARATOR); fileName = fullPath.substring (index + 1, fullPath.length ()); fileNames = new String [] {fileName}; } // if no file selected then go into the current directory if (fileName.equals("")) { //$NON-NLS-1$ int [] argList1 = {OS.XmNdirMask, 0}; OS.XtGetValues (dialog, argList1, argList1.length / 2); int directoryHandle = argList1[1]; int [] argList2 = {OS.XmNpattern,directoryHandle}; OS.XtSetValues (dialog, argList2, argList2.length / 2); OS.XmStringFree (directoryHandle); return 0; } int xmString2 = argList [3]; ptr = OS.XmStringUnparse ( xmString2, null, OS.XmCHARSET_TEXT, OS.XmCHARSET_TEXT, table, table.length, OS.XmOUTPUT_ALL); if (ptr != 0) { int length = OS.strlen (ptr); byte [] buffer = new byte [length]; OS.memmove (buffer, ptr, length); OS.XtFree (ptr); /* Use the character encoding for the default locale */ filterPath = new String (Converter.mbcsToWcs (null, buffer)); } OS.XmStringFree (xmString2); int length = filterPath.length (); if (length > 0) { if (filterPath.charAt (length - 1) == SEPARATOR) { filterPath = filterPath.substring (0, length - 1); } } this.fullPath = fullPath; this.fileName = fileName; this.fileNames = fileNames; OS.XtUnmanageChild (widget); return 0; } /** * Makes the dialog visible and brings it to the front * of the display. * * @return a string describing the absolute path of the first selected file, * or null if the dialog was cancelled or an error occurred * * @exception SWTException* The strings are platform specific. For example, on * Windows, an extension filter string is typically of * the form "*.extension", where "*.*" matches all files. *
* * @param extensions the file extension filter * * @see #setFilterNames to specify the user-friendly * names corresponding to the extensions */ public void setFilterExtensions (String [] extensions) { filterExtensions = extensions; } /** * Set the 0-based index of the file extension filter * which the dialog will use initially to filter the files * it shows to the argument. ** This is an index into the FilterExtensions array and * the FilterNames array. *
* * @param index the file extension filter index * * @see #setFilterExtensions * @see #setFilterNames * * @since 3.4 */ public void setFilterIndex (int index) { filterIndex = index; } /** * Sets the the names that describe the filter extensions * which the dialog will use to filter the files it shows * to the argument, which may be null. *
* Each name is a user-friendly short description shown for
* its corresponding filter. The names
array must
* be the same length as the extensions
array.
*
* Note that the path string is platform dependent. * For convenience, either '/' or '\' can be used * as a path separator. *
* * @param string the directory path * * @see #setFilterExtensions */ public void setFilterPath (String string) { filterPath = string; } /** * Sets the flag that the dialog will use to * determine whether to prompt the user for file * overwrite if the selected file already exists. * * @param overwrite true if the dialog will prompt for file overwrite, false otherwise * * @since 3.4 */ public void setOverwrite (boolean overwrite) { // this.overwrite = overwrite; } }