KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > common > widgets > FileBrowserWidget


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20
21 package org.apache.directory.ldapstudio.browser.common.widgets;
22
23
24 import java.io.File JavaDoc;
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 /**
40  * The FileBrowserWidget provides a combo with a history of recently
41  * used files an a browse button to open the file browser.
42  *
43  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
44  * @version $Rev$, $Date$
45  */

46 public class FileBrowserWidget extends BrowserWidget
47 {
48
49     /** The Constant TYPE_OPEN is used to create a Open file dialog. */
50     public static final int TYPE_OPEN = SWT.OPEN;
51
52     /** The Constant TYPE_SAVE is used to create a Save file dialog. */
53     public static final int TYPE_SAVE = SWT.SAVE;
54
55     /** The combo with the history of recently used files */
56     private Combo fileCombo;
57
58     /** The button to launch the file browser */
59     private Button browseButton;
60
61     /** The title */
62     private String JavaDoc title;
63
64     /** File extensions used within the lauched file browser */
65     private String JavaDoc[] extensions;
66
67     /** The type */
68     private int type;
69
70
71     /**
72      * Creates a new instance of FileBrowserWidget.
73      *
74      * @param title The title
75      * @param extensions The valid file extensions
76      * @param type The type, one of {@link #TYPE_OPEN} or {@link #TYPE_SAVE}
77      */

78     public FileBrowserWidget( String JavaDoc title, String JavaDoc[] extensions, int type )
79     {
80         this.title = title;
81         this.extensions = extensions;
82         this.type = type;
83     }
84
85
86     /**
87      * Creates the widget.
88      *
89      * @param parent the parent
90      */

91     public void createWidget( final Composite parent )
92     {
93
94         // Combo
95
fileCombo = BaseWidgetUtils.createCombo( parent, new String JavaDoc[0], -1, 1 );
96         fileCombo.addModifyListener( new ModifyListener()
97         {
98             public void modifyText( ModifyEvent e )
99             {
100                 notifyListeners();
101             }
102         } );
103
104         // Button
105
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 JavaDoc file = new File JavaDoc( 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 JavaDoc returnedFileName = fileDialog.open();
132                 if ( returnedFileName != null )
133                 {
134                     fileCombo.setText( returnedFileName );
135                     File JavaDoc file2 = new File JavaDoc( returnedFileName );
136                     BrowserCommonActivator.getDefault().getDialogSettings().put(
137                         BrowserCommonConstants.DIALOGSETTING_KEY_RECENT_FILE_PATH, file2.getParent() );
138                 }
139             }
140         } );
141
142         // file history
143
String JavaDoc[] history = HistoryUtils.load( BrowserCommonConstants.DIALOGSETTING_KEY_FILE_HISTORY );
144         fileCombo.setItems( history );
145     }
146
147
148     /**
149      * Gets the filename.
150      *
151      * @return the filename
152      */

153     public String JavaDoc getFilename()
154     {
155         return fileCombo.getText();
156     }
157
158
159     /**
160      * Sets the filename.
161      *
162      * @param filename the filename
163      */

164     public void setFilename( String JavaDoc filename )
165     {
166         fileCombo.setText( filename );
167     }
168
169
170     /**
171      * Saves dialog settings.
172      */

173     public void saveDialogSettings()
174     {
175         HistoryUtils.save( BrowserCommonConstants.DIALOGSETTING_KEY_FILE_HISTORY, fileCombo.getText() );
176     }
177
178
179     /**
180      * Sets the focus.
181      */

182     public void setFocus()
183     {
184         fileCombo.setFocus();
185     }
186
187
188     /**
189      * Enables or disables the widget.
190      *
191      * @param b true to enable the widget, false otherwise
192      */

193     public void setEnabled( boolean b )
194     {
195         fileCombo.setEnabled( b );
196         browseButton.setEnabled( b );
197     }
198
199 }
200
Popular Tags