1
/*******************************************************************************
2
* Copyright (C) 2003, 2006, 2007, 2013, Guillaume Brocker
4
* All rights reserved. This program and the accompanying materials
5
* are made available under the terms of the Eclipse Public License v1.0
6
* which accompanies this distribution, and is available at
7
* http://www.eclipse.org/legal/epl-v10.html
10
* Guillaume Brocker - Initial API and implementation
12
******************************************************************************/
14
package eclox.ui.editor.editors;
18
import org.eclipse.core.runtime.IPath;
19
import org.eclipse.core.runtime.Path;
20
import org.eclipse.jface.dialogs.IDialogConstants;
21
import org.eclipse.jface.dialogs.IInputValidator;
22
import org.eclipse.jface.dialogs.InputDialog;
23
import org.eclipse.swt.widgets.Composite;
24
import org.eclipse.swt.widgets.DirectoryDialog;
25
import org.eclipse.swt.widgets.FileDialog;
26
import org.eclipse.swt.widgets.Shell;
28
import eclox.core.doxyfiles.Doxyfile;
29
import eclox.ui.IPreferences;
30
import eclox.ui.Plugin;
33
* Implements convenience methods for setting editors.
37
public final class Convenience {
40
* Implements a specialized input dialog that adds buttons for browsing
41
* workspace or file system for paths.
43
private static class PathInputDialog extends InputDialog {
46
* Implements an input validator for the input dialog used to edit value compunds.
48
private static class MyInputValidator implements IInputValidator {
50
public String isValid(String newText) {
51
if( newText.length() == 0 ) {
52
return "Empty value is not allowed.";
61
private final static int BROWSE_FILESYSTEM_FILE_ID = IDialogConstants.CLIENT_ID + 3;
63
private final static int BROWSE_FILESYSTEM_DIRECTORY_ID = IDialogConstants.CLIENT_ID + 4;
65
private Doxyfile doxyfile;
72
* @param shell the parent shell
73
* @param doxyfile the reference doxyfile
74
* @param initValue the initial value
75
* @param styles a combination of browse facility flags
77
public PathInputDialog( Shell shell, Doxyfile doxyfile, String initValue, int styles ) {
81
"Edit the path. You may use buttons bellow to browse for a path.",
83
new MyInputValidator() );
85
this.doxyfile = doxyfile;
89
protected void createButtonsForButtonBar(Composite parent) {
90
// Create additionnal buttons.
91
if( (styles & BROWSE_FILESYSTEM_FILE) != 0 )
93
createButton( parent, BROWSE_FILESYSTEM_FILE_ID, "Browse File...", false );
95
if( (styles & BROWSE_FILESYSTEM_DIRECTORY) != 0 )
97
createButton( parent, BROWSE_FILESYSTEM_DIRECTORY_ID, "Browse Directory...", false );
100
// Create default buttons.
101
super.createButtonsForButtonBar(parent);
104
protected void buttonPressed(int buttonId) {
105
// Retrieves the path if one of the browse buttons is pressed.
108
case BROWSE_FILESYSTEM_DIRECTORY_ID:
109
path = Convenience.browseFileSystemForDirectory( getShell(), doxyfile, getText().getText() );
112
case BROWSE_FILESYSTEM_FILE_ID:
113
path = Convenience.browseFileSystemForFile( getShell(), doxyfile, getText().getText() );
117
super.buttonPressed(buttonId);
120
// Updates the input text if a path has been retrieved.
122
getText().setText( path );
130
* flag that activates the facility to browse for a file system file
132
public static final int BROWSE_FILESYSTEM_FILE = 1;
135
* flag that activates the facility to browse for a file system directory
137
public static final int BROWSE_FILESYSTEM_DIRECTORY = 2;
140
* flag that activates the facility to browse for a workspace or file system path
142
public static final int BROWSE_ALL = BROWSE_FILESYSTEM_FILE|BROWSE_FILESYSTEM_DIRECTORY;
146
* @brief Retrieves a path from the user that will be relative to the given doxyfile.
148
* @param shell the parent shell
149
* @param doxyfile the reference doxyfile
150
* @param initPath the initial path (may relative to the given doxyfile or absolute)
151
* @param style a combination of flags to tell which browse facilities will be available
153
* @return a string containing the path entered by the user or null if none
155
public static String browserForPath( Shell shell, Doxyfile doxyfile, String initPath, int style ) {
156
PathInputDialog dialog = new PathInputDialog( shell, doxyfile, initPath, style );
158
if( dialog.open() == PathInputDialog.OK ) {
159
return dialog.getValue();
167
* Browses the file system for a file and retrieves a path in the file system that
168
* may be relative to the given doxyfile's path
170
* @param shell a shell that will be the parent of dialogs
171
* @param doxyfile a doxyfile that is the reference for relative paths
172
* @param path a string containing an initial path
174
* @return a string containing the browsed path, or null if none
176
public static String browseFileSystemForFile( Shell shell, Doxyfile doxyfile, String path )
178
// Retrieves the initial path.
179
IPath initialPath = new Path( path );
180
if( initialPath.isAbsolute() == false ) {
181
initialPath = doxyfile.getFile().getParent().getLocation().append( initialPath );
184
// If the initial path is not valid, use the doxyfile location as fall back.
185
File file = new File( initialPath.toOSString() );
186
if( file.exists() == false ) {
187
initialPath = doxyfile.getFile().getParent().getLocation();
190
// Displays the directory dialog to the user
191
FileDialog dialog = new FileDialog( shell );
192
String choosenPathString;
194
dialog.setText( "File System File Selection" );
195
dialog.setFilterPath( initialPath.toOSString() );
196
choosenPathString = dialog.open();
198
// Parses the result.
199
if( choosenPathString != null ) {
200
IPath choosenPath = Path.fromOSString( choosenPathString );
201
IPath finalPath = doxyfile.makePathRelative( choosenPath );
203
return finalPath.toOSString();
211
* Browses the file system for a directory and retrieves a path in the file system that
212
* may be relative to the given doxyfile's path.
214
* @param shell a shell that will be the parent of dialogs
215
* @param doxyfile a doxyfile that is the reference for relative paths
216
* @param path a string containing an initial path
218
* @return a string containing the browsed path, or null if none
220
public static String browseFileSystemForDirectory( Shell shell, Doxyfile doxyfile, String path ) {
221
// Retrieves the initial path.
222
IPath initialPath = new Path( path );
223
if( initialPath.isAbsolute() == false ) {
224
initialPath = doxyfile.getFile().getParent().getLocation().append( initialPath );
227
// If the initial path is not valid, use the doxyfile location as fall back.
228
File file = new File( initialPath.toOSString() );
229
if( file.exists() == false ) {
230
initialPath = doxyfile.getFile().getParent().getLocation();
233
// Displays the directory dialog to the user
234
DirectoryDialog dialog = new DirectoryDialog( shell );
235
String choosenPathString;
236
dialog.setText( "File System Directory Selection" );
237
dialog.setFilterPath( initialPath.toOSString() );
238
choosenPathString = dialog.open();
240
// Parses the result.
241
if( choosenPathString != null ) {
242
IPath choosenPath = Path.fromOSString( choosenPathString );
243
IPath finalPath = doxyfile.makePathRelative( choosenPath );
245
return finalPath.toOSString();
253
* Escapes the given value string. This will place back slashes before
254
* any backslash or double quote.
256
* @param value the value string to escape
258
* @return the escaped value string
262
public static String escapeValue( String value )
264
String result = value;
266
if( Plugin.getDefault().getPluginPreferences().getBoolean(IPreferences.HANDLE_ESCAPED_VALUES) == true ) {
267
result = replace( result, "\\", "\\\\" ); // Replaces all \ by \\
268
result = replace( result, "\"", "\\\"" ); // Replaces all " by \"
274
* Un-escapes the given value string. This will remove escape backslashes
275
* located before backslashes and double quotes.
277
* @param value the value string to un-escape
279
* @return the escaped value string
283
public static String unescapeValue( String value )
285
String result = value;
287
if( Plugin.getDefault().getPluginPreferences().getBoolean(IPreferences.HANDLE_ESCAPED_VALUES) == true ) {
288
result = replace( result, "\\\"", "\"" ); // Replaces all \" by "
289
result = replace( result, "\\\\", "\\" ); // Replaces all \\ by \
295
* In the given string, replaces all matching substring by the given replacement.
297
* @param string the string to update
298
* @param search the sub string to search and replace
299
* @param replace the string to place where search matches
301
* @return the resulting string
303
public static String replace( String string, String search, String replace ) {
304
StringBuffer buffer = new StringBuffer( string );
306
for( int i = buffer.indexOf(search); i != -1; i = buffer.indexOf(search, i+replace.length()) ) {
307
buffer = buffer.replace( i, i + search.length(), replace );
309
return buffer.toString();