Java tutorial
/* The MIT License (MIT) Copyright (c) 2015 Bogdan Rechi This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ package bogdan.rechi.swt.commons; import org.apache.commons.io.FilenameUtils; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.DirectoryDialog; import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.Shell; import bogdan.rechi.commons.Files; import bogdan.rechi.commons.Platform; /** * Files operations dialogs. * * @author Bogdan Rechi */ public class SwtFiles { private static String _initialFolder = ""; // ============================================================================= /** * Browse for folder. * * @param parent * Parent shell. * @param initialFolder * Folder to start the browsing from, or null to use the internal mechanism. * @param title * Browsing window title. * * @return Selected folder or null. */ public static String browseForFolder(Shell parent, String initialFolder, String title) { DirectoryDialog dlg = new DirectoryDialog(parent == null ? SwtGeneral.CurrentApplicationShell : parent, SWT.OPEN); dlg.setFilterPath(initialFolder == null ? _initialFolder : initialFolder); dlg.setText(title); String dir = dlg.open(); if (dir != null) _initialFolder = dir; return dir; } /** * Browse for folder. * * @param initialFolder * Folder to start the browsing from, or null to use the internal mechanism. * @param title * Browsing window title. * * @return Selected folder or null. */ public static String browseForFolder(String initialFolder, String title) { return browseForFolder(null, initialFolder, title); } /** * Browse for files. * * @param parent * Parent shell. * @param initialFolder * Folder to start the browsing from, or null to use the internal mechanism. * @param initialFile * Initially selected file. * @param title * Browsing window title. * @param filters * Files' names filters. * @param filterNames * Filter names. * @param style * Must contain SWT.MULTI or SWT.SINGLE. * * @return Selected files or null. */ public static String[] browseForFiles(Shell parent, String initialFolder, String initialFile, String title, String[] filters, String[] filterNames, int style) { FileDialog dlg = new FileDialog(parent == null ? SwtGeneral.CurrentApplicationShell : parent, style); dlg.setText(title); if (Files.exists(initialFolder)) if (!Files.isFolder(initialFolder)) { if (initialFile == null) initialFile = FilenameUtils.getName(initialFolder); initialFolder = Files.getParent(initialFolder); } if (initialFile != null) dlg.setFileName(initialFile); dlg.setFilterPath(initialFolder == null ? _initialFolder : initialFolder); dlg.setFilterExtensions(filters); dlg.setFilterNames(filterNames); if (dlg.open() != null) { String[] files = (style == SWT.MULTI ? dlg.getFileNames() : new String[] { dlg.getFileName() }); _initialFolder = dlg.getFilterPath(); for (int i = 0; i < files.length; i++) files[i] = _initialFolder + Files.FILE_SEPARATOR + files[i]; if (Platform.SYSTEM_IS_LINUX && (style & SWT.SAVE) != 0) { String[] extSplit = filters[dlg.getFilterIndex()].split("\\."); if (extSplit.length == 2) if (!files[0].endsWith("." + extSplit[1])) files[0] = files[0] + "." + extSplit[1]; } return files; } return null; } /** * Browse for files. * * @param initialFolder * Folder to start the browsing from, or null to use the internal mechanism. * @param initialFile * Initially selected file. * @param title * Browsing window title. * @param filters * Files' names filters. * @param filterNames * Filter names. * @param style * Must contain SWT.MULTI or SWT.SINGLE. * * @return Selected files or null. */ public static String[] browseForFiles(String initialFolder, String initialFile, String title, String[] filters, String[] filterNames, int style) { return browseForFiles(null, initialFolder, initialFile, title, filters, filterNames, style); } /** * Browse for files. * * @param parent * Parent shell. * @param initialFolder * Folder to start the browsing from, or null to use the internal mechanism. * @param initialFile * Initially selected file. * @param title * Browsing window title. * @param filters * Files' names filters. * @param style * Must contain SWT.MULTI or SWT.SINGLE. * * @return Selected files or null. */ public static String[] browseForFiles(Shell parent, String initialFolder, String initialFile, String title, String[] filters, int style) { return browseForFiles(parent, initialFolder, initialFile, title, filters, filters, style); } /** * Browse for files. * * @param initialFolder * Folder to start the browsing from, or null to use the internal mechanism. * @param initialFile * Initially selected file. * @param title * Browsing window title. * @param filters * Files' names filters. * @param style * Must contain SWT.MULTI or SWT.SINGLE. * * @return Selected files or null. */ public static String[] browseForFiles(String initialFolder, String initialFile, String title, String[] filters, int style) { return browseForFiles(null, initialFolder, initialFile, title, filters, filters, style); } }