1 20 21 package org.apache.directory.ldapstudio.browser.common.widgets; 22 23 24 import java.io.File ; 25 26 import org.apache.directory.ldapstudio.browser.common.BrowserCommonActivator; 27 import org.apache.directory.ldapstudio.browser.common.BrowserCommonConstants; 28 import org.eclipse.swt.SWT; 29 import org.eclipse.swt.events.ModifyEvent; 30 import org.eclipse.swt.events.ModifyListener; 31 import org.eclipse.swt.events.SelectionAdapter; 32 import org.eclipse.swt.events.SelectionEvent; 33 import org.eclipse.swt.widgets.Button; 34 import org.eclipse.swt.widgets.Combo; 35 import org.eclipse.swt.widgets.Composite; 36 import org.eclipse.swt.widgets.FileDialog; 37 38 39 46 public class FileBrowserWidget extends BrowserWidget 47 { 48 49 50 public static final int TYPE_OPEN = SWT.OPEN; 51 52 53 public static final int TYPE_SAVE = SWT.SAVE; 54 55 56 private Combo fileCombo; 57 58 59 private Button browseButton; 60 61 62 private String title; 63 64 65 private String [] extensions; 66 67 68 private int type; 69 70 71 78 public FileBrowserWidget( String title, String [] extensions, int type ) 79 { 80 this.title = title; 81 this.extensions = extensions; 82 this.type = type; 83 } 84 85 86 91 public void createWidget( final Composite parent ) 92 { 93 94 fileCombo = BaseWidgetUtils.createCombo( parent, new String [0], -1, 1 ); 96 fileCombo.addModifyListener( new ModifyListener() 97 { 98 public void modifyText( ModifyEvent e ) 99 { 100 notifyListeners(); 101 } 102 } ); 103 104 browseButton = BaseWidgetUtils.createButton( parent, "Bro&wse...", 1 ); 106 browseButton.addSelectionListener( new SelectionAdapter() 107 { 108 public void widgetSelected( SelectionEvent event ) 109 { 110 FileDialog fileDialog = new FileDialog( parent.getShell(), type ); 111 fileDialog.setText( title ); 112 113 fileDialog.setFilterExtensions( extensions ); 114 115 File file = new File ( fileCombo.getText() ); 116 if ( file.isFile() ) 117 { 118 fileDialog.setFilterPath( file.getParent() ); 119 fileDialog.setFileName( file.getName() ); 120 } 121 else if ( file.isDirectory() ) 122 { 123 fileDialog.setFilterPath( file.getPath() ); 124 } 125 else 126 { 127 fileDialog.setFilterPath( BrowserCommonActivator.getDefault().getDialogSettings().get( 128 BrowserCommonConstants.DIALOGSETTING_KEY_RECENT_FILE_PATH ) ); 129 } 130 131 String returnedFileName = fileDialog.open(); 132 if ( returnedFileName != null ) 133 { 134 fileCombo.setText( returnedFileName ); 135 File file2 = new File ( returnedFileName ); 136 BrowserCommonActivator.getDefault().getDialogSettings().put( 137 BrowserCommonConstants.DIALOGSETTING_KEY_RECENT_FILE_PATH, file2.getParent() ); 138 } 139 } 140 } ); 141 142 String [] history = HistoryUtils.load( BrowserCommonConstants.DIALOGSETTING_KEY_FILE_HISTORY ); 144 fileCombo.setItems( history ); 145 } 146 147 148 153 public String getFilename() 154 { 155 return fileCombo.getText(); 156 } 157 158 159 164 public void setFilename( String filename ) 165 { 166 fileCombo.setText( filename ); 167 } 168 169 170 173 public void saveDialogSettings() 174 { 175 HistoryUtils.save( BrowserCommonConstants.DIALOGSETTING_KEY_FILE_HISTORY, fileCombo.getText() ); 176 } 177 178 179 182 public void setFocus() 183 { 184 fileCombo.setFocus(); 185 } 186 187 188 193 public void setEnabled( boolean b ) 194 { 195 fileCombo.setEnabled( b ); 196 browseButton.setEnabled( b ); 197 } 198 199 } 200 | Popular Tags |