All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.swt.widgets.FileDialog Maven / Gradle / Ivy

Go to download

SWT is an open source widget toolkit for Java designed to provide efficient, portable access to the user-interface facilities of the operating systems on which it is implemented.

The newest version!
/*******************************************************************************
 * Copyright (c) 2000, 2012 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.internal.*;
import org.eclipse.swt.internal.win32.*;
import org.eclipse.swt.*;

/**
 * Instances of this class allow the user to navigate
 * the file system and select or enter a file name.
 * 
*
Styles:
*
SAVE, OPEN, MULTI
*
Events:
*
(none)
*
*

* Note: Only one of the styles SAVE and OPEN may be specified. *

* IMPORTANT: This class is not intended to be subclassed. *

* * @see FileDialog snippets * @see SWT Example: ControlExample, Dialog tab * @see Sample code and further information * @noextend This class is not intended to be subclassed by clients. */ public class FileDialog extends Dialog { String [] filterNames = new String [0]; String [] filterExtensions = new String [0]; String [] fileNames = new String [0]; String filterPath = "", fileName = ""; int filterIndex = 0; boolean overwrite = false; static final String FILTER = "*.*"; static int BUFFER_SIZE = 1024 * 32; static boolean USE_HOOK = true; static { /* * Feature in Vista. When OFN_ENABLEHOOK is set in the * save or open file dialog, Vista uses the old XP look * and feel. OFN_ENABLEHOOK is used to grow the file * name buffer in a multi-select file dialog. The fix * is to only use OFN_ENABLEHOOK when the buffer has * overrun. */ if (!OS.IsWinCE && OS.WIN32_VERSION >= OS.VERSION (6, 0)) { USE_HOOK = false; } } /** * 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
    *
  • ERROR_NULL_ARGUMENT - if the parent is null
  • *
* @exception SWTException
    *
  • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent
  • *
  • ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass
  • *
*/ public FileDialog (Shell parent) { this (parent, SWT.APPLICATION_MODAL); } /** * Constructs a new instance of this class given its parent * and a style value describing its behavior and appearance. *

* 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. *

* * @param parent a shell which will be the parent of the new instance * @param style the style of dialog to construct * * @exception IllegalArgumentException
    *
  • ERROR_NULL_ARGUMENT - if the parent is null
  • *
* @exception SWTException
    *
  • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent
  • *
  • ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass
  • *
* * @see SWT#SAVE * @see SWT#OPEN * @see SWT#MULTI */ public FileDialog (Shell parent, int style) { super (parent, checkStyle (parent, style)); checkSubclass (); } /** * Returns the path of the first file that was * selected in the dialog relative to the filter path, or an * empty string if no such file has been selected. * * @return the relative path of the file */ public String getFileName () { return fileName; } /** * Returns a (possibly empty) array with the paths of all files * that were selected in the dialog relative to the filter path. * * @return the relative paths of the files */ public String [] getFileNames () { return fileNames; } /** * Returns the file extensions which the dialog will * use to filter the files it shows. * * @return the file extensions filter */ public String [] getFilterExtensions () { return filterExtensions; } /** * Get the 0-based index of the file extension filter * which was selected by the user, or -1 if no filter * was selected. *

* 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 /*long*/ OFNHookProc (int /*long*/ hdlg, int /*long*/ uiMsg, int /*long*/ wParam, int /*long*/ lParam) { switch ((int)/*64*/uiMsg) { case OS.WM_NOTIFY: OFNOTIFY ofn = new OFNOTIFY (); OS.MoveMemory (ofn, lParam, OFNOTIFY.sizeof); if (ofn.code == OS.CDN_SELCHANGE) { int lResult = (int)/*64*/OS.SendMessage (ofn.hwndFrom, OS.CDM_GETSPEC, 0, 0); if (lResult > 0) { lResult += OS.MAX_PATH; OPENFILENAME lpofn = new OPENFILENAME (); OS.MoveMemory (lpofn, ofn.lpOFN, OPENFILENAME.sizeof); if (lpofn.nMaxFile < lResult) { int /*long*/ hHeap = OS.GetProcessHeap (); int /*long*/ lpstrFile = OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, lResult * TCHAR.sizeof); if (lpstrFile != 0) { if (lpofn.lpstrFile != 0) OS.HeapFree (hHeap, 0, lpofn.lpstrFile); lpofn.lpstrFile = lpstrFile; lpofn.nMaxFile = lResult; OS.MoveMemory (ofn.lpOFN, lpofn, OPENFILENAME.sizeof); } } } } break; } 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
    *
  • ERROR_WIDGET_DISPOSED - if the dialog has been disposed
  • *
  • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the dialog
  • *
*/ public String open () { int /*long*/ hHeap = OS.GetProcessHeap (); /* Get the owner HWND for the dialog */ int /*long*/ hwndOwner = parent.handle; int /*long*/ hwndParent = parent.handle; /* * Feature in Windows. There is no API to set the orientation of a * file dialog. It is always inherited from the parent. The fix is * to create a hidden parent and set the orientation in the hidden * parent for the dialog to inherit. */ boolean enabled = false; if (!OS.IsWinCE && OS.WIN32_VERSION >= OS.VERSION(4, 10)) { int dialogOrientation = style & (SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT); int parentOrientation = parent.style & (SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT); if (dialogOrientation != parentOrientation) { int exStyle = OS.WS_EX_NOINHERITLAYOUT; if (dialogOrientation == SWT.RIGHT_TO_LEFT) exStyle |= OS.WS_EX_LAYOUTRTL; hwndOwner = OS.CreateWindowEx ( exStyle, Shell.DialogClass, null, 0, OS.CW_USEDEFAULT, 0, OS.CW_USEDEFAULT, 0, hwndParent, 0, OS.GetModuleHandle (null), null); enabled = OS.IsWindowEnabled (hwndParent); if (enabled) OS.EnableWindow (hwndParent, false); } } /* Convert the title and copy it into lpstrTitle */ if (title == null) title = ""; /* Use the character encoding for the default locale */ TCHAR buffer3 = new TCHAR (0, title, true); int byteCount3 = buffer3.length () * TCHAR.sizeof; int /*long*/ lpstrTitle = OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, byteCount3); OS.MoveMemory (lpstrTitle, buffer3, byteCount3); /* Compute filters and copy into lpstrFilter */ String strFilter = ""; if (filterNames == null) filterNames = new String [0]; if (filterExtensions == null) filterExtensions = new String [0]; for (int i=0; i 0) { /* Use the character encoding for the default locale */ TCHAR prefix = new TCHAR (0, nFileOffset - 1); int byteCount2 = prefix.length () * TCHAR.sizeof; OS.MoveMemory (prefix, lpstrFile, byteCount2); filterPath = prefix.toString (0, prefix.length ()); /* * Get each file from the buffer. Files are delimited * by a NULL character with 2 NULL characters at the end. */ int count = 0; fileNames = new String [(style & SWT.MULTI) != 0 ? 4 : 1]; int start = nFileOffset; do { int end = start; while (end < buffer.length () && buffer.tcharAt (end) != 0) end++; String string = buffer.toString (start, end - start); start = end; if (count == fileNames.length) { String [] newFileNames = new String [fileNames.length + 4]; System.arraycopy (fileNames, 0, newFileNames, 0, fileNames.length); fileNames = newFileNames; } fileNames [count++] = string; if ((style & SWT.MULTI) == 0) break; start++; } while (start < buffer.length () && buffer.tcharAt (start) != 0); if (fileNames.length > 0) fileName = fileNames [0]; String separator = ""; int length = filterPath.length (); if (length > 0 && filterPath.charAt (length - 1) != '\\') { separator = "\\"; } fullPath = filterPath + separator + fileName; if (count < fileNames.length) { String [] newFileNames = new String [count]; System.arraycopy (fileNames, 0, newFileNames, 0, count); fileNames = newFileNames; } } filterIndex = struct.nFilterIndex - 1; } /* Free the memory that was allocated. */ OS.HeapFree (hHeap, 0, lpstrFile); OS.HeapFree (hHeap, 0, lpstrFilter); OS.HeapFree (hHeap, 0, lpstrInitialDir); OS.HeapFree (hHeap, 0, lpstrTitle); if (lpstrDefExt != 0) OS.HeapFree (hHeap, 0, lpstrDefExt); /* Destroy the BIDI orientation window */ if (hwndParent != hwndOwner) { if (enabled) OS.EnableWindow (hwndParent, true); OS.SetActiveWindow (hwndParent); OS.DestroyWindow (hwndOwner); } /* * This code is intentionally commented. On some * platforms, the owner window is repainted right * away when a dialog window exits. This behavior * is currently unspecified. */ // if (hwndOwner != 0) OS.UpdateWindow (hwndOwner); /* Answer the full path or null */ return fullPath; } /** * Set the initial filename which the dialog will * select by default when opened to the argument, * which may be null. The name will be prefixed with * the filter path when one is supplied. * * @param string the file name */ public void setFileName (String string) { fileName = string; } /** * Set the file extensions which the dialog will * use to filter the files it shows to the argument, * which may be null. *

* The strings are platform specific. For example, on * some platforms, an extension filter string is typically * of the form "*.extension", where "*.*" matches all files. * For filters with multiple extensions, use semicolon as * a separator, e.g. "*.jpg;*.png". *

* * @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 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. *

* * @param names the list of filter names, or null for no filter names * * @see #setFilterExtensions */ public void setFilterNames (String [] names) { filterNames = names; } /** * Sets the directory path that the dialog will use * to the argument, which may be null. File names in this * path will appear in the dialog, filtered according * to the filter extensions. If the string is null, * then the operating system's default filter path * will be used. *

* 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; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy