~ubuntu-branches/ubuntu/utopic/eclipse-eclox/utopic

« back to all changes in this revision

Viewing changes to eclox.ui/src/eclox/ui/editor/editors/Convenience.java

  • Committer: Package Import Robot
  • Author(s): Graham Inggs
  • Date: 2013-07-07 20:33:10 UTC
  • Revision ID: package-import@ubuntu.com-20130707203310-a44yw80gqtc2s9ob
Tags: upstream-0.10.0
ImportĀ upstreamĀ versionĀ 0.10.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************************************************
 
2
 * Copyright (C) 2003, 2006, 2007, 2013, Guillaume Brocker
 
3
 * 
 
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
 
8
 *
 
9
 * Contributors:
 
10
 *     Guillaume Brocker - Initial API and implementation
 
11
 *
 
12
 ******************************************************************************/ 
 
13
 
 
14
package eclox.ui.editor.editors;
 
15
 
 
16
import java.io.File;
 
17
 
 
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;
 
27
 
 
28
import eclox.core.doxyfiles.Doxyfile;
 
29
import eclox.ui.IPreferences;
 
30
import eclox.ui.Plugin;
 
31
 
 
32
/**
 
33
 * Implements convenience methods for setting editors.
 
34
 * 
 
35
 * @author gbrocker
 
36
 */
 
37
public final class Convenience {
 
38
        
 
39
        /**
 
40
         * Implements a specialized input dialog that adds buttons for browsing
 
41
         * workspace or file system for paths. 
 
42
         */
 
43
        private static class PathInputDialog extends InputDialog {
 
44
 
 
45
                /**
 
46
                 * Implements an input validator for the input dialog used to edit value compunds.
 
47
                 */
 
48
                private static class MyInputValidator implements IInputValidator {
 
49
 
 
50
                        public String isValid(String newText) {
 
51
                                if( newText.length() == 0 ) {
 
52
                                        return "Empty value is not allowed.";
 
53
                                }
 
54
                                else {
 
55
                                        return null;
 
56
                                }
 
57
                        }
 
58
                        
 
59
                }
 
60
 
 
61
                private final static int BROWSE_FILESYSTEM_FILE_ID = IDialogConstants.CLIENT_ID + 3;
 
62
                
 
63
                private final static int BROWSE_FILESYSTEM_DIRECTORY_ID = IDialogConstants.CLIENT_ID + 4;
 
64
                
 
65
                private Doxyfile doxyfile;
 
66
                
 
67
                private int styles;
 
68
                
 
69
                /**
 
70
                 * Constructor
 
71
                 * 
 
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
 
76
                 */
 
77
                public PathInputDialog( Shell shell, Doxyfile doxyfile, String initValue, int styles ) {
 
78
                        super(
 
79
                                        shell,
 
80
                                        "Value Edition",
 
81
                                        "Edit the path. You may use buttons bellow to browse for a path.",
 
82
                                        initValue,
 
83
                                        new MyInputValidator() );
 
84
                        
 
85
                        this.doxyfile = doxyfile;
 
86
                        this.styles = styles;
 
87
                }
 
88
                
 
89
                protected void createButtonsForButtonBar(Composite parent) {
 
90
                        // Create additionnal buttons.
 
91
                        if( (styles & BROWSE_FILESYSTEM_FILE) != 0 )
 
92
                        {
 
93
                                createButton( parent, BROWSE_FILESYSTEM_FILE_ID, "Browse File...", false );
 
94
                        }
 
95
                        if( (styles & BROWSE_FILESYSTEM_DIRECTORY) != 0 )
 
96
                        {
 
97
                                createButton( parent, BROWSE_FILESYSTEM_DIRECTORY_ID, "Browse Directory...", false );
 
98
                        }
 
99
                        
 
100
                        // Create default buttons.
 
101
                        super.createButtonsForButtonBar(parent);
 
102
                }
 
103
 
 
104
                protected void buttonPressed(int buttonId) {
 
105
                        // Retrieves the path if one of the browse buttons is pressed.
 
106
                        String  path = null;
 
107
                        switch( buttonId ) {
 
108
                                case BROWSE_FILESYSTEM_DIRECTORY_ID:
 
109
                                        path = Convenience.browseFileSystemForDirectory( getShell(), doxyfile, getText().getText() );
 
110
                                        break;
 
111
                                
 
112
                                case BROWSE_FILESYSTEM_FILE_ID:
 
113
                                        path = Convenience.browseFileSystemForFile( getShell(), doxyfile, getText().getText() );
 
114
                                        break;
 
115
                                
 
116
                                default:
 
117
                                        super.buttonPressed(buttonId);
 
118
                        }
 
119
 
 
120
                        // Updates the input text if a path has been retrieved.
 
121
                        if( path != null ) {
 
122
                                getText().setText( path );
 
123
                        }
 
124
                }
 
125
                
 
126
        }
 
127
 
 
128
        
 
129
        /**
 
130
         * flag that activates the facility to browse for a file system file
 
131
         */
 
132
        public static final int BROWSE_FILESYSTEM_FILE = 1;
 
133
        
 
134
        /**
 
135
         * flag that activates the facility to browse for a file system directory
 
136
         */
 
137
        public static final int BROWSE_FILESYSTEM_DIRECTORY = 2;
 
138
        
 
139
        /**
 
140
         * flag that activates the facility to browse for a workspace or file system path
 
141
         */
 
142
        public static final int BROWSE_ALL = BROWSE_FILESYSTEM_FILE|BROWSE_FILESYSTEM_DIRECTORY;
 
143
        
 
144
        
 
145
        /**
 
146
         * @brief       Retrieves a path from the user that will be relative to the given doxyfile.
 
147
         * 
 
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
 
152
         * 
 
153
         * @return      a string containing the path entered by the user or null if none
 
154
         */
 
155
        public static String browserForPath( Shell shell, Doxyfile doxyfile, String initPath, int style ) {
 
156
                PathInputDialog dialog =  new PathInputDialog( shell, doxyfile, initPath, style );
 
157
                
 
158
                if( dialog.open() == PathInputDialog.OK ) {
 
159
                        return dialog.getValue();
 
160
                }
 
161
                else {
 
162
                        return null;
 
163
                }
 
164
        }
 
165
        
 
166
        /**
 
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
 
169
         * 
 
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
 
173
         * 
 
174
         * @return      a string containing the browsed path, or null if none
 
175
         */
 
176
        public static String browseFileSystemForFile( Shell shell, Doxyfile doxyfile, String path )
 
177
        {
 
178
                // Retrieves the initial path.
 
179
                IPath   initialPath = new Path( path );
 
180
                if( initialPath.isAbsolute() == false ) {
 
181
                        initialPath = doxyfile.getFile().getParent().getLocation().append( initialPath );
 
182
                }
 
183
                
 
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();
 
188
                }
 
189
 
 
190
                // Displays the directory dialog to the user
 
191
                FileDialog              dialog = new FileDialog( shell );
 
192
                String                  choosenPathString;
 
193
                
 
194
                dialog.setText( "File System File Selection" );
 
195
                dialog.setFilterPath( initialPath.toOSString() );
 
196
                choosenPathString = dialog.open();
 
197
                
 
198
                // Parses the result.
 
199
                if( choosenPathString != null ) {
 
200
                        IPath   choosenPath = Path.fromOSString( choosenPathString );
 
201
                        IPath   finalPath = doxyfile.makePathRelative( choosenPath );
 
202
                        
 
203
                        return finalPath.toOSString();
 
204
                }
 
205
                else {
 
206
                        return null;
 
207
                }
 
208
        }
 
209
 
 
210
        /**
 
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.
 
213
         * 
 
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
 
217
         * 
 
218
         * @return      a string containing the browsed path, or null if none
 
219
         */
 
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 );
 
225
                }
 
226
                
 
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();
 
231
                }
 
232
                
 
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();
 
239
                
 
240
                // Parses the result.
 
241
                if( choosenPathString != null ) {
 
242
                        IPath   choosenPath = Path.fromOSString( choosenPathString );
 
243
                        IPath   finalPath = doxyfile.makePathRelative( choosenPath );
 
244
                        
 
245
                        return finalPath.toOSString();
 
246
                }
 
247
                else {
 
248
                        return null;
 
249
                }
 
250
        }
 
251
        
 
252
        /**
 
253
         * Escapes the given value string. This will place back slashes before
 
254
         * any backslash or double quote.
 
255
         * 
 
256
         * @param       value   the value string to escape
 
257
         * 
 
258
         * @return      the escaped value string
 
259
         * 
 
260
         * @see         unescapeValue
 
261
         */
 
262
        public static String escapeValue( String value )
 
263
        {
 
264
                String  result = value;
 
265
                
 
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 \"
 
269
                }
 
270
                return result;
 
271
        }
 
272
        
 
273
        /**
 
274
         * Un-escapes the given value string. This will remove escape backslashes 
 
275
         * located before backslashes and double quotes.
 
276
         * 
 
277
         * @param       value   the value string to un-escape
 
278
         * 
 
279
         * @return      the escaped value string
 
280
         * 
 
281
         * @see         escapeValue
 
282
         */
 
283
        public static String unescapeValue( String value )
 
284
        {
 
285
                String  result = value;
 
286
                
 
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 \
 
290
                }
 
291
                return result;
 
292
        }
 
293
 
 
294
        /**
 
295
         * In the given string, replaces all matching substring by the given replacement.
 
296
         * 
 
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
 
300
         * 
 
301
         * @return      the resulting string
 
302
         */
 
303
        public static String replace( String string, String search, String replace ) {
 
304
                StringBuffer    buffer = new StringBuffer( string );
 
305
                
 
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 );
 
308
                }
 
309
                return buffer.toString();
 
310
        }
 
311
}