001 ///////////////////////////////////////////////// 002 //This file is part of Sears project. 003 //Subtitle Editor And Re-Synch 004 //A tool to easily modify and resynch movies subtitles. 005 ///////////////////////////////////////////////// 006 //This program is free software; 007 //you can redistribute it and/or modify it under the terms 008 //of the GNU General Public License 009 //as published by the Free Software Foundation; 010 //either version 2 of the License, or (at your option) any later version. 011 ///////////////////////////////////////////////// 012 //Sears project is availbale under sourceforge 013 //at adress: http://sourceforge.net/projects/sears/ 014 //Copyright (C) 2005 Booba Skaya 015 //Mail: booba.skaya@gmail.com 016 ///////////////////////////////////////////////// 017 018 package sears.tools; 019 020 import java.awt.FileDialog; 021 import java.awt.Frame; 022 import java.io.File; 023 import java.io.FilenameFilter; 024 import java.util.Locale; 025 026 import javax.swing.JFileChooser; 027 import javax.swing.JOptionPane; 028 import javax.swing.UIManager; 029 import javax.swing.filechooser.FileFilter; 030 031 032 /** 033 * This class gives methods for display an "open" dialog and a "save as" dialog. 034 * <br>An instance of this class will display (if the good method is called), 035 * <br>after test conditions a FileDialog or JFileChooser. 036 * <br> 037 * <br> <Strong>Example of use:</Strong> 038 * <Blockquote> DialogUtils du = new DialogUtils(); 039 * <br> <i> // set options: </i> 040 * <br> du.setConditions( true, true ); 041 * <br> du.setApproveButtonText( "Save as" ); 042 * <br> du.setOverwriteText(SearsResourceBundle.getResource( "Do you really want to overwrite this file ?" )); 043 * <br> du.setFileFilter(new FileFilter() { ... }); 044 * <br> <i> // we get back the file choosen </i> 045 * <br> File file = du.showSaveAsDialog(instance, "Save As", subtitleFile.getFile()); 046 * <br> <i> // if user cancel action, file is null: </i> 047 * <br> if (file != null){ 048 * <Blockquote> <i> // action you want </i> 049 * </Blockquote> } 050 * </Blockquote> 051 * 052 */ 053 public class DialogUtils { 054 //Conditions: 055 056 // by default conditions are set to false, we display a JFileChooser 057 private boolean isMacCondition = false; 058 private boolean lookAndFeelCondition = false; 059 060 // this condition should be necessary when you display FileDialog on Windows platform 061 // method setFilenameFilter() doesn't work on this OS. 062 private boolean isWindowsCondition = false; 063 064 private String approveButtonText = null; 065 066 private FileFilter filter = null; 067 068 private String overwriteText = null; 069 070 private Locale locale = null; 071 072 public DialogUtils(){ 073 //... 074 } 075 076 /** 077 * <br>This method set conditions for display the FileDialog instead of the JFileChooser. 078 * @param OSCondition if true, the class instance will test if app is running on a Mac platform 079 * @param lookAndFeelCondition if true, the class instance will test if app's look and feel is the same of the system default one 080 */ 081 public void setConditions(boolean OSCondition , boolean lookAndFeelCondition){ 082 if(OSCondition){ 083 // if we want to test if app is running on Mac, the windows condition is set to false: 084 setConditions(false, OSCondition, lookAndFeelCondition); 085 } else { 086 // if we don't want to test if the app is running on a Mac platform, 087 // we must test if the app is running on a Windows platform: 088 setConditions(true, true, lookAndFeelCondition); 089 } 090 } 091 092 private void setConditions(boolean windowsCondition, boolean macCondition, boolean lookAndFeelCondition){ 093 if(windowsCondition){ 094 this.isWindowsCondition = Utils.isWindowsPlatform; 095 } 096 if(macCondition){ 097 // true if app is running on a Mac OS platform, false if not 098 this.isMacCondition = Utils.isMacPlatform; 099 } 100 if(lookAndFeelCondition){ 101 // true if the app's look and feel is the same as the default system look and feel 102 this.lookAndFeelCondition = (UIManager.getLookAndFeel()).toString().contains((UIManager.getSystemLookAndFeelClassName())); 103 } 104 } 105 106 /** 107 * <br>this method set the approveButtonText needed by JFileChooser 108 * @param approveButtonText the text display in the approve button 109 */ 110 public void setApproveButtonText(String approveButtonText){ 111 this.approveButtonText = approveButtonText; 112 } 113 114 /** 115 * <br>this method defines the file filer needed by the dialog... 116 * @param filter the file filter 117 */ 118 public void setFileFilter(FileFilter filter){ 119 this.filter = filter; 120 } 121 122 /** 123 * <br>this method defines the overwrite text needed if instance choose a JFileChooser 124 * <br>for display the "save as" dialog 125 * @param overwriteText the overwrite text you want to display if user try to overwrite a file 126 */ 127 public void setOverwriteText(String overwriteText) { 128 this.overwriteText = overwriteText; 129 } 130 131 public void setLocale(Locale locale) { 132 this.locale = locale; 133 } 134 135 /** 136 * <br>this method returns the overwrite text needed by JFileChooser with the "save as" dialog" 137 * @return the overwrite text if it set, "!" if not 138 */ 139 private String getOverwriteText() { 140 String result = null; 141 if(overwriteText == null){ 142 result = "!"; 143 } else { 144 result = overwriteText; 145 } 146 return result; 147 } 148 149 /** 150 * <br>this method convert the filter if it's set to a FileNameFilter instance 151 * @return a FilenameFilter instance according to filter 152 */ 153 private FilenameFilter fileFilterToFilenameFilter(){ 154 FilenameFilter returnFilter = null; 155 if(filter != null){ 156 returnFilter = new FilenameFilter(){ 157 158 public boolean accept(File file, String name) { 159 return filter.accept(new File(name)); 160 } 161 162 }; 163 } 164 return returnFilter; 165 } 166 167 168 /** 169 * Method saveAsDialog 170 * <br>this method display a dialog for save as a file. 171 * <br>it displays a FileDialog if require condition is respected, a JfileChooser if not. 172 * <br>A file is return if user accepts to save its ile, null if not. 173 * @param parent the parent Frame 174 * @param title the title of the dialog 175 * @param openedFile the file which want to save 176 * @return the saved file (created or existing file) 177 */ 178 public File showSaveAsDialog(Frame parent, String title, File openedFile){ 179 // the return file: 180 File file = null; 181 182 if(isMacCondition && lookAndFeelCondition){ 183 FileDialog fileDialog = new FileDialog(parent, title, FileDialog.SAVE); 184 if(!isWindowsCondition && filter != null) { 185 // doesn't work on Microsoft Windows: 186 fileDialog.setFilenameFilter(fileFilterToFilenameFilter()); 187 } 188 189 if(locale != null) { 190 fileDialog.setLocale(locale); 191 } 192 193 fileDialog.setLocationRelativeTo(parent); 194 // set up the default file to save in... 195 fileDialog.setFile(openedFile.getName()); 196 // we display the file dialog: 197 fileDialog.setVisible(true); 198 // if user cancel the action, file return is null, 199 if(fileDialog.getFile() != null){ 200 // we get back the file selected by user 201 //if file already exists FileDialog manage it itself. 202 file = new File(fileDialog.getDirectory() + fileDialog.getFile()); 203 } 204 } else { 205 206 // we create a file chooser: 207 JFileChooser fileChooser = null; 208 if (openedFile != null) { 209 fileChooser = new JFileChooser(openedFile.getParent()); 210 fileChooser.setSelectedFile(openedFile); 211 } else { 212 fileChooser = new JFileChooser(); 213 } 214 // we personnalize it: 215 fileChooser.setDialogTitle(title); 216 if(approveButtonText != null) { 217 fileChooser.setApproveButtonText(approveButtonText); 218 } 219 220 if(filter != null) { 221 fileChooser.setFileFilter(filter); 222 } 223 224 // and display the dialog: 225 int returnValue = fileChooser.showSaveDialog(parent); 226 // if the user click OK: 227 if(returnValue == JFileChooser.APPROVE_OPTION){ 228 file = fileChooser.getSelectedFile(); 229 if(file.exists()){ 230 int result = JOptionPane.showConfirmDialog(fileChooser, file.getName() + " " + getOverwriteText(), "" , JOptionPane.YES_NO_OPTION); 231 // if user don't accept to overwrite the file: 232 if(result != JOptionPane.OK_OPTION){ 233 // set save to false: 234 file = null; 235 } 236 } 237 } 238 } 239 return file; 240 } 241 242 public File showOpenDialog(Frame parent, String title, File openedFile){ 243 File file = null; 244 if(isMacCondition && lookAndFeelCondition){ 245 FileDialog fileDialog = new FileDialog(parent, title, FileDialog.LOAD); 246 if(!isWindowsCondition && filter != null) { 247 // doesn't work on Microsoft Windows: 248 fileDialog.setFilenameFilter(fileFilterToFilenameFilter()); 249 } 250 251 if(locale != null) { 252 fileDialog.setLocale(locale); 253 } 254 255 if(openedFile != null){ 256 // we set the default opened directory: 257 fileDialog.setDirectory(openedFile.getParent()); 258 // we set the default selected file: 259 fileDialog.setFile(openedFile.getName()); 260 } 261 262 fileDialog.setVisible(true); 263 // fileDialog.setFile(null); 264 // if user cancel the action, file is null, 265 if(fileDialog.getFile() != null){ 266 // we get back the file selected by user 267 file = new File(fileDialog.getDirectory() + fileDialog.getFile()); 268 } 269 } else { 270 // create the file chooser: 271 JFileChooser fileChooser = null; 272 if (openedFile != null) { 273 fileChooser = new JFileChooser(openedFile.getPath()); 274 } else { 275 fileChooser = new JFileChooser(); 276 } 277 // we set a filter on extension file: 278 if (filter != null){ 279 fileChooser.setFileFilter(filter); 280 } 281 282 // we display the browser: 283 int result = fileChooser.showOpenDialog(parent); 284 if ( result == JFileChooser.APPROVE_OPTION) { 285 // set result if user approves selection: 286 file = fileChooser.getSelectedFile(); 287 } 288 } 289 290 return file; 291 } 292 }