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

« back to all changes in this revision

Viewing changes to eclox.core/src/eclox/core/ui/DefaultDoxygenFieldEditor.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, 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.core.ui;
 
15
 
 
16
import java.util.Collection;
 
17
import java.util.Iterator;
 
18
import java.util.Vector;
 
19
 
 
20
import org.eclipse.jface.preference.FieldEditor;
 
21
import org.eclipse.swt.SWT;
 
22
import org.eclipse.swt.events.SelectionEvent;
 
23
import org.eclipse.swt.events.SelectionListener;
 
24
import org.eclipse.swt.layout.FillLayout;
 
25
import org.eclipse.swt.layout.GridData;
 
26
import org.eclipse.swt.widgets.Button;
 
27
import org.eclipse.swt.widgets.Composite;
 
28
import org.eclipse.swt.widgets.DirectoryDialog;
 
29
import org.eclipse.swt.widgets.Table;
 
30
import org.eclipse.swt.widgets.TableColumn;
 
31
import org.eclipse.swt.widgets.TableItem;
 
32
import org.eclipse.ui.ISharedImages;
 
33
import org.eclipse.ui.PlatformUI;
 
34
 
 
35
import eclox.core.IPreferences;
 
36
import eclox.core.Plugin;
 
37
import eclox.core.doxygen.BundledDoxygen;
 
38
import eclox.core.doxygen.CustomDoxygen;
 
39
import eclox.core.doxygen.DefaultDoxygen;
 
40
import eclox.core.doxygen.Doxygen;
 
41
 
 
42
/**
 
43
 * @author Guillaume Brocker
 
44
 */
 
45
public class DefaultDoxygenFieldEditor extends FieldEditor {
 
46
        
 
47
        /**
 
48
         * defines the version column index
 
49
         */
 
50
        private final int VERSION_COLUMN_INDEX = 0;
 
51
        
 
52
        /**
 
53
         * defines the type column index
 
54
         */
 
55
        private final int TYPE_COLUMN_INDEX = 1;
 
56
        
 
57
        /**
 
58
         * defines the description column index
 
59
         */
 
60
        private final int DESCRIPTION_COLUMN_INDEX = 2;
 
61
        
 
62
        /**
 
63
         * the table control showing available doxygen wrappers
 
64
         */
 
65
        private Table table;
 
66
        
 
67
        /**
 
68
         * the composite containing all buttons
 
69
         */
 
70
        private Composite buttons;
 
71
        
 
72
        /**
 
73
         * the button that triggers the addition of a custom doxygen
 
74
         */
 
75
        private Button add;
 
76
        
 
77
        /**
 
78
         * the button that triggers the edition of a custom doxygen
 
79
         */
 
80
        private Button edit;
 
81
        
 
82
        /**
 
83
         * the button that triggers the removal of a custom doxygen
 
84
         */
 
85
        private Button remove;
 
86
        
 
87
        /**
 
88
         * the valid state of the field editor
 
89
         */
 
90
        private boolean valid = true;
 
91
        
 
92
 
 
93
        /**
 
94
         * Implements a selection listener for the owned table control.
 
95
         * 
 
96
         * It is reponsible to ensure that only one table item is selected at a time
 
97
         * and it also fires value change notifications.
 
98
         */
 
99
        private class MySelectionListener implements SelectionListener {
 
100
 
 
101
                /**
 
102
                 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
 
103
                 */
 
104
                public void widgetDefaultSelected(SelectionEvent e) {
 
105
                        widgetSelected( e );
 
106
                }
 
107
 
 
108
                /**
 
109
                 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
 
110
                 */
 
111
                public void widgetSelected(SelectionEvent e) {
 
112
                        if              (e.item instanceof TableItem )  onItemSelected( (TableItem) e.item );
 
113
                        else if (e.getSource() == add )                 onAddSelected();
 
114
                        else if (e.getSource() == edit)                 onEditSelected();
 
115
                        else if (e.getSource() == remove)               onRemoveSelected();             
 
116
                }
 
117
                
 
118
        }
 
119
        
 
120
        
 
121
        /**
 
122
         * Adds a new table item for the given doxygen instance
 
123
         */
 
124
        private void addItem( Doxygen doxygen ) {
 
125
                TableItem       item = new TableItem( table, 0 );
 
126
                
 
127
                item.setData( doxygen );
 
128
                updateItem( item );
 
129
        }
 
130
        
 
131
        
 
132
        /**
 
133
         * Retrieves the type of the given doxygen wrapper instance
 
134
         */
 
135
        private static String getDoxygenType( final Doxygen doxygen ) {
 
136
                if              ( doxygen instanceof DefaultDoxygen     )       return "Default";
 
137
                else if ( doxygen instanceof CustomDoxygen      )       return "Custom";
 
138
                else if ( doxygen instanceof BundledDoxygen     )       return "Bundled";
 
139
                else                                                                                    return "Uknown";
 
140
        }
 
141
        
 
142
        
 
143
        /**
 
144
         * Updates the given table item.
 
145
         */
 
146
        private void updateItem( TableItem item ) {
 
147
                // Item data preparation.
 
148
                final Doxygen   doxygen         = (Doxygen) item.getData();
 
149
                final String    type            = getDoxygenType( doxygen );
 
150
                final String    version         = doxygen.getVersion();
 
151
                final String    description = doxygen.getDescription();
 
152
                
 
153
                // Updates the item properties.
 
154
                item.setImage( (version == null) ? PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJS_WARN_TSK) : null );
 
155
                item.setText( VERSION_COLUMN_INDEX, (version == null) ? "unknown" : version );
 
156
                item.setText( TYPE_COLUMN_INDEX, type );
 
157
                item.setText( DESCRIPTION_COLUMN_INDEX, description );
 
158
                
 
159
                // Updates the table layout.
 
160
                table.getColumn( VERSION_COLUMN_INDEX ).pack();
 
161
                table.getColumn( TYPE_COLUMN_INDEX ).pack();
 
162
                table.getColumn( DESCRIPTION_COLUMN_INDEX ).pack();
 
163
                table.layout();
 
164
        }
 
165
        
 
166
        
 
167
        /**
 
168
         * Checkes the table item representing the given doxygen identifier.
 
169
         */
 
170
        private void checkItem( final String identifier ) {
 
171
                TableItem[]     items = table.getItems();
 
172
                
 
173
                for( int i = 0; i < items.length; ++i ) {
 
174
                        Doxygen current = (Doxygen) items[i].getData();
 
175
                        
 
176
                        items[i].setChecked( current.getIdentifier().equalsIgnoreCase( identifier ) ); 
 
177
                }
 
178
        }
 
179
        
 
180
        
 
181
        /**
 
182
         * Retrieves checked items.
 
183
         * 
 
184
         * @return      the checked item or null if none
 
185
         */
 
186
        private TableItem[] getCheckedItems() {
 
187
                // Pre-condition
 
188
                assert( table != null );
 
189
                
 
190
                Vector          checked = new Vector();
 
191
                TableItem[]     items   = table.getItems();
 
192
                
 
193
                for( int i = 0; i < items.length; ++i ) {
 
194
                        if( items[i].getChecked() == true ) {
 
195
                                checked.add( items[i] );
 
196
                        }
 
197
                }
 
198
                return (TableItem[]) checked.toArray( new TableItem[0] );
 
199
        }
 
200
        
 
201
        
 
202
        /**
 
203
         * Process the click on the add button.
 
204
         */
 
205
        private void onAddSelected() {
 
206
                // Asks the user to select a location containing a doxygen binary.
 
207
                DirectoryDialog dialog          = new DirectoryDialog( getPage().getShell() );
 
208
                String                  directory       = null;
 
209
                
 
210
                dialog.setMessage( "Select a location containing doxygen." );
 
211
                dialog.setText( "Add Custom Doxygen" );
 
212
                directory = dialog.open();
 
213
                
 
214
                // Creates the new wrapper.
 
215
                if( directory != null ) {
 
216
                        addItem( new CustomDoxygen( directory ) );
 
217
                }
 
218
        }
 
219
        
 
220
        
 
221
        /**
 
222
         * Process the click on the edit button
 
223
         */
 
224
        private void onEditSelected() {
 
225
                // Pre-condition
 
226
                assert( table != null );
 
227
                
 
228
                
 
229
                // Retrieves the checked items
 
230
                TableItem[]     selected = table.getSelection();
 
231
                
 
232
                
 
233
                // Retrieves the doxygen wrapper associated to the selected item
 
234
                assert( selected.length == 1 );
 
235
                Doxygen doxygen = (Doxygen) selected[0].getData();
 
236
                
 
237
                
 
238
                // Asks the user to select a location containing a doxygen binary.
 
239
                assert( doxygen instanceof CustomDoxygen );
 
240
                CustomDoxygen   customDoxygen   = (CustomDoxygen) doxygen;      
 
241
                DirectoryDialog dialog                  = new DirectoryDialog( getPage().getShell() );
 
242
                String                  directory               = null;
 
243
                
 
244
                dialog.setMessage( "Select a new location containing doxygen." );
 
245
                dialog.setText( "Edit Custom Doxygen" );
 
246
                dialog.setFilterPath( customDoxygen.getLocation() );
 
247
                directory = dialog.open();
 
248
                
 
249
                // Creates the new wrapper.
 
250
                if( directory != null ) {
 
251
                        customDoxygen.setLocation( directory );
 
252
                        updateItem( selected[0] );
 
253
                }
 
254
        }
 
255
        
 
256
        
 
257
        /**
 
258
         * Process the click on the remove button
 
259
         */
 
260
        private void onRemoveSelected() {
 
261
                // Pre-condition
 
262
                assert( table != null );
 
263
                
 
264
                table.remove( table.getSelectionIndex() );
 
265
                refreshValidState();
 
266
        }
 
267
        
 
268
        
 
269
        /**
 
270
         * Process the selection of the given table item.
 
271
         */
 
272
        private void onItemSelected( TableItem item ) {
 
273
                if( item.getChecked() == true ) {
 
274
                        TableItem[]     checked = getCheckedItems();
 
275
                        
 
276
                        // Updates chekced items so that only one is chekced at the same time.
 
277
                        for( int i = 0; i < checked.length; ++i ) {
 
278
                                if( checked[i] != item ) {
 
279
                                        checked[i].setChecked( false );
 
280
                                }
 
281
                        }
 
282
                        
 
283
                        // Selects the item that has been checked.
 
284
                        table.setSelection( table.indexOf(item) );
 
285
                        
 
286
                        // TODO only supported in eclipse 3.2
 
287
                        // table.setSelection( item );
 
288
                        
 
289
                        // Fires some notifications.
 
290
                        fireValueChanged( VALUE, null, null );
 
291
                }
 
292
                
 
293
                
 
294
                // Updates button states
 
295
                final boolean   enable = item.getData() instanceof CustomDoxygen;
 
296
                
 
297
                edit.setEnabled( enable );
 
298
                remove.setEnabled( enable );
 
299
                
 
300
                
 
301
                // Refreshes the field validity.
 
302
                refreshValidState();
 
303
        }
 
304
        
 
305
 
 
306
        /**
 
307
         * @see org.eclipse.jface.preference.FieldEditor#adjustForNumColumns(int)
 
308
         */
 
309
        protected void adjustForNumColumns(int numColumns) {
 
310
                // Pre-condition
 
311
                assert( table != null );
 
312
                
 
313
                GridData        tableData       = (GridData) table.getLayoutData();
 
314
                GridData        buttonsData     = (GridData) buttons.getLayoutData();
 
315
                
 
316
                tableData.horizontalSpan = numColumns - 1;
 
317
                buttonsData.horizontalSpan = 1;
 
318
        }
 
319
 
 
320
        
 
321
        /**
 
322
         * @see org.eclipse.jface.preference.FieldEditor#doFillIntoGrid(org.eclipse.swt.widgets.Composite, int)
 
323
         */
 
324
        protected void doFillIntoGrid(Composite parent, int numColumns) {
 
325
                // Pre-condition
 
326
                assert( table == null );
 
327
                
 
328
                // Creates the combo controls containing all available doxygen wrappers.
 
329
                GridData        tableData = new GridData( GridData.FILL_BOTH );
 
330
                
 
331
                table = new Table( parent, SWT.SINGLE|SWT.CHECK|SWT.BORDER|SWT.FULL_SELECTION );
 
332
                tableData.horizontalSpan = numColumns - 1;
 
333
                table.setLayoutData( tableData );
 
334
                table.addSelectionListener( new MySelectionListener() );
 
335
                
 
336
                TableColumn     versionColumn           = new TableColumn( table, SWT.LEFT, VERSION_COLUMN_INDEX );
 
337
                TableColumn     typeColumn                      = new TableColumn( table, SWT.LEFT, TYPE_COLUMN_INDEX );
 
338
                TableColumn     descriptionColumn       = new TableColumn( table, SWT.LEFT, DESCRIPTION_COLUMN_INDEX );
 
339
                
 
340
                versionColumn.setText( "Version" );
 
341
                typeColumn.setText( "Type" );
 
342
                descriptionColumn.setText( "Description" );
 
343
                table.setHeaderVisible( true );
 
344
                
 
345
                
 
346
                // Creates the composite containing all buttons and located on the right side of the table.
 
347
                GridData        buttonsData             = new GridData( GridData.END );
 
348
                FillLayout      buttonsLayout   = new FillLayout( SWT.VERTICAL );
 
349
                
 
350
                buttons = new Composite( parent, SWT.NO_FOCUS );
 
351
                buttonsData.horizontalSpan = 1;
 
352
                buttonsData.horizontalAlignment = SWT.FILL;
 
353
                buttons.setLayoutData( buttonsData );
 
354
                buttonsLayout.spacing = 5;
 
355
                buttons.setLayout( buttonsLayout );
 
356
                
 
357
                
 
358
                
 
359
                // Creates the button controlling custom doyxgen wrappers.
 
360
                add             = new Button( buttons, SWT.PUSH );
 
361
                edit    = new Button( buttons, SWT.PUSH );
 
362
                remove  = new Button( buttons, SWT.PUSH );
 
363
                
 
364
                add.setText( "Add..." );
 
365
                add.addSelectionListener( new MySelectionListener() );
 
366
                edit.setText( "Edit..." );
 
367
                edit.addSelectionListener( new MySelectionListener() );
 
368
                edit.setEnabled( false );
 
369
                remove.setText( "Remove" );
 
370
                remove.addSelectionListener( new MySelectionListener() );
 
371
                remove.setEnabled( false );             
 
372
        }
 
373
 
 
374
        
 
375
        /**
 
376
         * @see org.eclipse.jface.preference.FieldEditor#doLoad()
 
377
         */
 
378
        protected void doLoad() {
 
379
                // Adds default doxygen instance.
 
380
                addItem( new DefaultDoxygen() );
 
381
                
 
382
                
 
383
                // Adds custom doxygens.
 
384
                final String    raw                     = getPreferenceStore().getString( IPreferences.CUSTOM_DOXYGENS );
 
385
                final String[]  splitted        = raw.split( "\n");
 
386
                for( int i = 0; i < splitted.length; ++i ) {
 
387
                        CustomDoxygen   doxygen = CustomDoxygen.createFromIdentifier( splitted[i] );
 
388
                        
 
389
                        if( doxygen != null ) {
 
390
                                addItem( doxygen );
 
391
                        }
 
392
                        else {
 
393
                                Plugin.getDefault().logError( splitted[i] + ": invalid custom doxygen identifier found." );
 
394
                        }
 
395
                }
 
396
                
 
397
                
 
398
                // Adds bundled doxygens.
 
399
                Collection      bundled = BundledDoxygen.getAll();
 
400
                Iterator        i               = bundled.iterator();
 
401
                while( i.hasNext() ) {
 
402
                        addItem( (Doxygen) i.next() );
 
403
                }
 
404
                
 
405
 
 
406
                // Select the default doxygen wrapper
 
407
                checkItem( getPreferenceStore().getString( IPreferences.DEFAULT_DOXYGEN ) );            
 
408
        }
 
409
 
 
410
        
 
411
        /**
 
412
         * @see org.eclipse.jface.preference.FieldEditor#doLoadDefault()
 
413
         */
 
414
        protected void doLoadDefault() {
 
415
                // Adds default doxygen instance.
 
416
                addItem( new DefaultDoxygen() );
 
417
 
 
418
                // Select the default doxygen wrapper
 
419
                checkItem( getPreferenceStore().getDefaultString( IPreferences.DEFAULT_DOXYGEN ) );             
 
420
        }
 
421
 
 
422
        
 
423
        /**
 
424
         * @see org.eclipse.jface.preference.FieldEditor#doStore()
 
425
         */
 
426
        protected void doStore() {
 
427
                // Pre-condition
 
428
                assert( table != null );
 
429
                
 
430
                
 
431
                // Saves all custom doxygen wrappers.
 
432
                TableItem[]     items           = table.getItems();
 
433
                String          serialized      = new String();
 
434
                for( int i = 0; i < items.length; ++i ) {
 
435
                        Object  itemData = items[i].getData();
 
436
                        
 
437
                        if( itemData instanceof CustomDoxygen ) {
 
438
                                CustomDoxygen   doxygen = (CustomDoxygen) itemData;
 
439
                                
 
440
                                serialized = serialized.concat( doxygen.getIdentifier() );
 
441
                                serialized = serialized.concat( "\n" );
 
442
                        }
 
443
                }
 
444
                getPreferenceStore().setValue( IPreferences.CUSTOM_DOXYGENS, serialized );
 
445
                
 
446
                
 
447
                // Saves the checked item.
 
448
                TableItem[]     checked                 = getCheckedItems();
 
449
                String          defaultDoxygen  = new String();
 
450
                if( checked.length == 1 ) {
 
451
                        Doxygen doxygen = (Doxygen) checked[0].getData();
 
452
                        
 
453
                        defaultDoxygen = doxygen.getIdentifier();
 
454
                }
 
455
                getPreferenceStore().setValue( IPreferences.DEFAULT_DOXYGEN, defaultDoxygen );
 
456
        }
 
457
        
 
458
        
 
459
        /**
 
460
         * @see org.eclipse.jface.preference.FieldEditor#refreshValidState()
 
461
         */
 
462
        protected void refreshValidState() {
 
463
                TableItem[]     checked         = getCheckedItems();
 
464
                boolean         oldValid        = valid;
 
465
                boolean         newValid        = checked.length == 1;
 
466
                
 
467
                // Updates validity and error message. 
 
468
                valid = newValid;
 
469
                if( valid == false ) {
 
470
                        showErrorMessage( "Select the doxygen version to use." );
 
471
                }
 
472
                else {
 
473
                        clearErrorMessage();
 
474
                }
 
475
                
 
476
                // Send some notifications.
 
477
                if( newValid != oldValid ) {
 
478
                        fireStateChanged( IS_VALID, oldValid, newValid );
 
479
                }
 
480
        }
 
481
        
 
482
        
 
483
        /**
 
484
         * Constructor
 
485
         */
 
486
        public DefaultDoxygenFieldEditor( Composite parent ) {
 
487
                super( IPreferences.DEFAULT_DOXYGEN, "Doxygen:", parent );
 
488
        }
 
489
 
 
490
        
 
491
        /**
 
492
         * @see org.eclipse.jface.preference.FieldEditor#getNumberOfControls()
 
493
         */
 
494
        public int getNumberOfControls() {
 
495
                return 2;
 
496
        }
 
497
 
 
498
 
 
499
        /**
 
500
         * @see org.eclipse.jface.preference.FieldEditor#isValid()
 
501
         */
 
502
        public boolean isValid() {
 
503
                return valid;
 
504
        }
 
505
 
 
506
 
 
507
        /**
 
508
         * @see org.eclipse.jface.preference.FieldEditor#setFocus()
 
509
         */
 
510
        public void setFocus() {
 
511
                super.setFocus();
 
512
                table.setFocus();               
 
513
        }
 
514
        
 
515
}