1
/*******************************************************************************
2
* Copyright (C) 2003-2006, 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.core.ui;
16
import java.util.Collection;
17
import java.util.Iterator;
18
import java.util.Vector;
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;
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;
43
* @author Guillaume Brocker
45
public class DefaultDoxygenFieldEditor extends FieldEditor {
48
* defines the version column index
50
private final int VERSION_COLUMN_INDEX = 0;
53
* defines the type column index
55
private final int TYPE_COLUMN_INDEX = 1;
58
* defines the description column index
60
private final int DESCRIPTION_COLUMN_INDEX = 2;
63
* the table control showing available doxygen wrappers
68
* the composite containing all buttons
70
private Composite buttons;
73
* the button that triggers the addition of a custom doxygen
78
* the button that triggers the edition of a custom doxygen
83
* the button that triggers the removal of a custom doxygen
85
private Button remove;
88
* the valid state of the field editor
90
private boolean valid = true;
94
* Implements a selection listener for the owned table control.
96
* It is reponsible to ensure that only one table item is selected at a time
97
* and it also fires value change notifications.
99
private class MySelectionListener implements SelectionListener {
102
* @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
104
public void widgetDefaultSelected(SelectionEvent e) {
109
* @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
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();
122
* Adds a new table item for the given doxygen instance
124
private void addItem( Doxygen doxygen ) {
125
TableItem item = new TableItem( table, 0 );
127
item.setData( doxygen );
133
* Retrieves the type of the given doxygen wrapper instance
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";
144
* Updates the given table item.
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();
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 );
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();
168
* Checkes the table item representing the given doxygen identifier.
170
private void checkItem( final String identifier ) {
171
TableItem[] items = table.getItems();
173
for( int i = 0; i < items.length; ++i ) {
174
Doxygen current = (Doxygen) items[i].getData();
176
items[i].setChecked( current.getIdentifier().equalsIgnoreCase( identifier ) );
182
* Retrieves checked items.
184
* @return the checked item or null if none
186
private TableItem[] getCheckedItems() {
188
assert( table != null );
190
Vector checked = new Vector();
191
TableItem[] items = table.getItems();
193
for( int i = 0; i < items.length; ++i ) {
194
if( items[i].getChecked() == true ) {
195
checked.add( items[i] );
198
return (TableItem[]) checked.toArray( new TableItem[0] );
203
* Process the click on the add button.
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;
210
dialog.setMessage( "Select a location containing doxygen." );
211
dialog.setText( "Add Custom Doxygen" );
212
directory = dialog.open();
214
// Creates the new wrapper.
215
if( directory != null ) {
216
addItem( new CustomDoxygen( directory ) );
222
* Process the click on the edit button
224
private void onEditSelected() {
226
assert( table != null );
229
// Retrieves the checked items
230
TableItem[] selected = table.getSelection();
233
// Retrieves the doxygen wrapper associated to the selected item
234
assert( selected.length == 1 );
235
Doxygen doxygen = (Doxygen) selected[0].getData();
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;
244
dialog.setMessage( "Select a new location containing doxygen." );
245
dialog.setText( "Edit Custom Doxygen" );
246
dialog.setFilterPath( customDoxygen.getLocation() );
247
directory = dialog.open();
249
// Creates the new wrapper.
250
if( directory != null ) {
251
customDoxygen.setLocation( directory );
252
updateItem( selected[0] );
258
* Process the click on the remove button
260
private void onRemoveSelected() {
262
assert( table != null );
264
table.remove( table.getSelectionIndex() );
270
* Process the selection of the given table item.
272
private void onItemSelected( TableItem item ) {
273
if( item.getChecked() == true ) {
274
TableItem[] checked = getCheckedItems();
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 );
283
// Selects the item that has been checked.
284
table.setSelection( table.indexOf(item) );
286
// TODO only supported in eclipse 3.2
287
// table.setSelection( item );
289
// Fires some notifications.
290
fireValueChanged( VALUE, null, null );
294
// Updates button states
295
final boolean enable = item.getData() instanceof CustomDoxygen;
297
edit.setEnabled( enable );
298
remove.setEnabled( enable );
301
// Refreshes the field validity.
307
* @see org.eclipse.jface.preference.FieldEditor#adjustForNumColumns(int)
309
protected void adjustForNumColumns(int numColumns) {
311
assert( table != null );
313
GridData tableData = (GridData) table.getLayoutData();
314
GridData buttonsData = (GridData) buttons.getLayoutData();
316
tableData.horizontalSpan = numColumns - 1;
317
buttonsData.horizontalSpan = 1;
322
* @see org.eclipse.jface.preference.FieldEditor#doFillIntoGrid(org.eclipse.swt.widgets.Composite, int)
324
protected void doFillIntoGrid(Composite parent, int numColumns) {
326
assert( table == null );
328
// Creates the combo controls containing all available doxygen wrappers.
329
GridData tableData = new GridData( GridData.FILL_BOTH );
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() );
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 );
340
versionColumn.setText( "Version" );
341
typeColumn.setText( "Type" );
342
descriptionColumn.setText( "Description" );
343
table.setHeaderVisible( true );
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 );
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 );
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 );
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 );
376
* @see org.eclipse.jface.preference.FieldEditor#doLoad()
378
protected void doLoad() {
379
// Adds default doxygen instance.
380
addItem( new DefaultDoxygen() );
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] );
389
if( doxygen != null ) {
393
Plugin.getDefault().logError( splitted[i] + ": invalid custom doxygen identifier found." );
398
// Adds bundled doxygens.
399
Collection bundled = BundledDoxygen.getAll();
400
Iterator i = bundled.iterator();
401
while( i.hasNext() ) {
402
addItem( (Doxygen) i.next() );
406
// Select the default doxygen wrapper
407
checkItem( getPreferenceStore().getString( IPreferences.DEFAULT_DOXYGEN ) );
412
* @see org.eclipse.jface.preference.FieldEditor#doLoadDefault()
414
protected void doLoadDefault() {
415
// Adds default doxygen instance.
416
addItem( new DefaultDoxygen() );
418
// Select the default doxygen wrapper
419
checkItem( getPreferenceStore().getDefaultString( IPreferences.DEFAULT_DOXYGEN ) );
424
* @see org.eclipse.jface.preference.FieldEditor#doStore()
426
protected void doStore() {
428
assert( table != null );
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();
437
if( itemData instanceof CustomDoxygen ) {
438
CustomDoxygen doxygen = (CustomDoxygen) itemData;
440
serialized = serialized.concat( doxygen.getIdentifier() );
441
serialized = serialized.concat( "\n" );
444
getPreferenceStore().setValue( IPreferences.CUSTOM_DOXYGENS, serialized );
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();
453
defaultDoxygen = doxygen.getIdentifier();
455
getPreferenceStore().setValue( IPreferences.DEFAULT_DOXYGEN, defaultDoxygen );
460
* @see org.eclipse.jface.preference.FieldEditor#refreshValidState()
462
protected void refreshValidState() {
463
TableItem[] checked = getCheckedItems();
464
boolean oldValid = valid;
465
boolean newValid = checked.length == 1;
467
// Updates validity and error message.
469
if( valid == false ) {
470
showErrorMessage( "Select the doxygen version to use." );
476
// Send some notifications.
477
if( newValid != oldValid ) {
478
fireStateChanged( IS_VALID, oldValid, newValid );
486
public DefaultDoxygenFieldEditor( Composite parent ) {
487
super( IPreferences.DEFAULT_DOXYGEN, "Doxygen:", parent );
492
* @see org.eclipse.jface.preference.FieldEditor#getNumberOfControls()
494
public int getNumberOfControls() {
500
* @see org.eclipse.jface.preference.FieldEditor#isValid()
502
public boolean isValid() {
508
* @see org.eclipse.jface.preference.FieldEditor#setFocus()
510
public void setFocus() {