KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > preferences > CodeAssistAdvancedConfigurationBlock


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.preferences;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collections JavaDoc;
15 import java.util.Comparator JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.eclipse.core.commands.Command;
22 import org.eclipse.core.commands.CommandManager;
23 import org.eclipse.core.commands.IParameter;
24 import org.eclipse.core.commands.Parameterization;
25 import org.eclipse.core.commands.ParameterizedCommand;
26 import org.eclipse.core.commands.common.NotDefinedException;
27 import org.eclipse.core.commands.contexts.ContextManager;
28
29 import org.eclipse.core.runtime.Assert;
30
31 import org.eclipse.swt.SWT;
32 import org.eclipse.swt.events.SelectionAdapter;
33 import org.eclipse.swt.events.SelectionEvent;
34 import org.eclipse.swt.graphics.GC;
35 import org.eclipse.swt.graphics.Image;
36 import org.eclipse.swt.layout.GridData;
37 import org.eclipse.swt.layout.GridLayout;
38 import org.eclipse.swt.widgets.Button;
39 import org.eclipse.swt.widgets.Composite;
40 import org.eclipse.swt.widgets.Control;
41 import org.eclipse.swt.widgets.Label;
42 import org.eclipse.swt.widgets.Link;
43 import org.eclipse.swt.widgets.Table;
44 import org.eclipse.swt.widgets.TableColumn;
45
46 import org.eclipse.jface.bindings.BindingManager;
47 import org.eclipse.jface.bindings.Scheme;
48 import org.eclipse.jface.bindings.TriggerSequence;
49 import org.eclipse.jface.resource.ImageDescriptor;
50 import org.eclipse.jface.resource.JFaceResources;
51 import org.eclipse.jface.viewers.ArrayContentProvider;
52 import org.eclipse.jface.viewers.CheckStateChangedEvent;
53 import org.eclipse.jface.viewers.CheckboxTableViewer;
54 import org.eclipse.jface.viewers.ICheckStateListener;
55 import org.eclipse.jface.viewers.IStructuredSelection;
56 import org.eclipse.jface.viewers.ITableLabelProvider;
57 import org.eclipse.jface.viewers.LabelProvider;
58 import org.eclipse.jface.viewers.ViewerComparator;
59
60 import org.eclipse.ui.PlatformUI;
61 import org.eclipse.ui.commands.ICommandService;
62 import org.eclipse.ui.dialogs.PreferencesUtil;
63 import org.eclipse.ui.keys.IBindingService;
64 import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer;
65 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
66
67 import org.eclipse.jdt.core.JavaCore;
68
69 import org.eclipse.jdt.internal.corext.util.Messages;
70
71 import org.eclipse.jdt.ui.PreferenceConstants;
72
73 import org.eclipse.jdt.internal.ui.JavaPlugin;
74 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
75 import org.eclipse.jdt.internal.ui.text.java.CompletionProposalCategory;
76 import org.eclipse.jdt.internal.ui.text.java.CompletionProposalComputerRegistry;
77 import org.eclipse.jdt.internal.ui.util.PixelConverter;
78 import org.eclipse.jdt.internal.ui.util.SWTUtil;
79 import org.eclipse.jdt.internal.ui.wizards.IStatusChangeListener;
80
81 /**
82  *
83  * @since 3.2
84  */

85 final class CodeAssistAdvancedConfigurationBlock extends OptionsConfigurationBlock {
86     
87     private static final Key PREF_EXCLUDED_CATEGORIES= getJDTUIKey(PreferenceConstants.CODEASSIST_EXCLUDED_CATEGORIES);
88     private static final Key PREF_CATEGORY_ORDER= getJDTUIKey(PreferenceConstants.CODEASSIST_CATEGORY_ORDER);
89     private static final Key PREF_CODEASSIST_TIMEOUT_FOR_PARAMETER_NAME_FROM_ATTACHED_JAVADOC= getJDTCoreKey(JavaCore.TIMEOUT_FOR_PARAMETER_NAME_FROM_ATTACHED_JAVADOC);
90     
91     private static Key[] getAllKeys() {
92         return new Key[] {
93                 PREF_EXCLUDED_CATEGORIES,
94                 PREF_CATEGORY_ORDER,
95                 PREF_CODEASSIST_TIMEOUT_FOR_PARAMETER_NAME_FROM_ATTACHED_JAVADOC
96         };
97     }
98
99     private final class DefaultTableLabelProvider extends LabelProvider implements ITableLabelProvider {
100
101         /*
102          * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang.Object, int)
103          */

104         public Image getColumnImage(Object JavaDoc element, int columnIndex) {
105             if (columnIndex == 0)
106                 return ((ModelElement) element).getImage();
107             return null;
108         }
109
110         /*
111          * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object, int)
112          */

113         public String JavaDoc getColumnText(Object JavaDoc element, int columnIndex) {
114             switch (columnIndex) {
115                 case 0:
116                     return ((ModelElement) element).getName();
117                 case 1:
118                     return ((ModelElement) element).getKeybindingAsString();
119                 default:
120                     Assert.isTrue(false);
121                     return null;
122             }
123         }
124         
125         /*
126          * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
127          */

128         public String JavaDoc getText(Object JavaDoc element) {
129             return getColumnText(element, 0); // needed to make the sorter work
130
}
131     }
132
133     private final class SeparateTableLabelProvider extends LabelProvider implements ITableLabelProvider {
134         
135         /*
136          * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang.Object, int)
137          */

138         public Image getColumnImage(Object JavaDoc element, int columnIndex) {
139             if (columnIndex == 0)
140                 return ((ModelElement) element).getImage();
141             return null;
142         }
143         
144         /*
145          * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object, int)
146          */

147         public String JavaDoc getColumnText(Object JavaDoc element, int columnIndex) {
148             switch (columnIndex) {
149                 case 0:
150                     return ((ModelElement) element).getName();
151                 default:
152                     Assert.isTrue(false);
153                 return null;
154             }
155         }
156     }
157
158     private final Comparator JavaDoc fCategoryComparator= new Comparator JavaDoc() {
159         private int getRank(Object JavaDoc o) {
160             return ((ModelElement) o).getRank();
161         }
162
163         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
164             return getRank(o1) - getRank(o2);
165         }
166     };
167     
168     private final class PreferenceModel {
169         private static final int LIMIT= 0xffff;
170         private static final String JavaDoc COLON= ":"; //$NON-NLS-1$
171
private static final String JavaDoc SEPARATOR= "\0"; //$NON-NLS-1$
172

173         private final List JavaDoc fElements;
174         /**
175          * The read-only list of elements.
176          */

177         final List JavaDoc elements;
178         
179         public PreferenceModel(CompletionProposalComputerRegistry registry) {
180             List JavaDoc categories= registry.getProposalCategories();
181             fElements= new ArrayList JavaDoc();
182             for (Iterator JavaDoc it= categories.iterator(); it.hasNext();) {
183                 CompletionProposalCategory category= (CompletionProposalCategory) it.next();
184                 if (category.hasComputers()) {
185                     fElements.add(new ModelElement(category, this));
186                 }
187             }
188             Collections.sort(fElements, fCategoryComparator);
189             elements= Collections.unmodifiableList(fElements);
190         }
191         
192         public void moveUp(ModelElement category) {
193             int index= fElements.indexOf(category);
194             if (index > 0) {
195                 Object JavaDoc item= fElements.remove(index);
196                 fElements.add(index - 1, item);
197                 writeOrderPreference(null, false);
198             }
199         }
200
201         public void moveDown(ModelElement category) {
202             int index= fElements.indexOf(category);
203             if (index < fElements.size() - 1) {
204                 Object JavaDoc item= fElements.remove(index);
205                 fElements.add(index + 1, item);
206                 writeOrderPreference(null, false);
207             }
208         }
209
210         private void writeInclusionPreference(ModelElement changed, boolean isInDefaultCategory) {
211             StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
212             for (Iterator JavaDoc it= fElements.iterator(); it.hasNext();) {
213                 ModelElement item= (ModelElement) it.next();
214                 boolean included= changed == item ? isInDefaultCategory : item.isInDefaultCategory();
215                 if (!included)
216                     buf.append(item.getId() + SEPARATOR);
217             }
218             
219             String JavaDoc newValue= buf.toString();
220             String JavaDoc oldValue= setValue(PREF_EXCLUDED_CATEGORIES, newValue);
221             validateSettings(PREF_EXCLUDED_CATEGORIES, oldValue, newValue);
222         }
223         
224         private void writeOrderPreference(ModelElement changed, boolean isSeparate) {
225             StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
226             int i= 0;
227             for (Iterator JavaDoc it= fElements.iterator(); it.hasNext(); i++) {
228                 ModelElement item= (ModelElement) it.next();
229                 boolean separate= changed == item ? isSeparate : item.isSeparateCommand();
230                 int rank= separate ? i : i + LIMIT;
231                 buf.append(item.getId() + COLON + rank + SEPARATOR);
232             }
233             
234             String JavaDoc newValue= buf.toString();
235             String JavaDoc oldValue= setValue(PREF_CATEGORY_ORDER, newValue);
236             validateSettings(PREF_CATEGORY_ORDER, oldValue, newValue);
237         }
238         
239
240         private boolean readInclusionPreference(CompletionProposalCategory cat) {
241             String JavaDoc[] ids= getTokens(getValue(PREF_EXCLUDED_CATEGORIES), SEPARATOR);
242             for (int i= 0; i < ids.length; i++) {
243                 if (ids[i].equals(cat.getId()))
244                     return false;
245             }
246             return true;
247         }
248         
249         private int readOrderPreference(CompletionProposalCategory cat) {
250             String JavaDoc[] sortOrderIds= getTokens(getValue(PREF_CATEGORY_ORDER), SEPARATOR);
251             for (int i= 0; i < sortOrderIds.length; i++) {
252                 String JavaDoc[] idAndRank= getTokens(sortOrderIds[i], COLON);
253                 if (idAndRank[0].equals(cat.getId()))
254                     return Integer.parseInt(idAndRank[1]);
255             }
256             return LIMIT + 1;
257         }
258
259         public void update() {
260             Collections.sort(fElements, fCategoryComparator);
261         }
262     }
263     
264     private final class ModelElement {
265         private final CompletionProposalCategory fCategory;
266         private final Command fCommand;
267         private final IParameter fParam;
268         private final PreferenceModel fPreferenceModel;
269         
270         ModelElement(CompletionProposalCategory category, PreferenceModel model) {
271             fCategory= category;
272             ICommandService commandSvc= (ICommandService) PlatformUI.getWorkbench().getAdapter(ICommandService.class);
273             fCommand= commandSvc.getCommand("org.eclipse.jdt.ui.specific_content_assist.command"); //$NON-NLS-1$
274
IParameter type;
275             try {
276                 type= fCommand.getParameters()[0];
277             } catch (NotDefinedException x) {
278                 Assert.isTrue(false);
279                 type= null;
280             }
281             fParam= type;
282             fPreferenceModel= model;
283         }
284         Image getImage() {
285             return CodeAssistAdvancedConfigurationBlock.this.getImage(fCategory.getImageDescriptor());
286         }
287         String JavaDoc getName() {
288             return fCategory.getDisplayName();
289         }
290         String JavaDoc getKeybindingAsString() {
291             final Parameterization[] params= { new Parameterization(fParam, fCategory.getId()) };
292             final ParameterizedCommand pCmd= new ParameterizedCommand(fCommand, params);
293             String JavaDoc key= getKeyboardShortcut(pCmd);
294             return key;
295         }
296         boolean isInDefaultCategory() {
297             return fPreferenceModel.readInclusionPreference(fCategory);
298         }
299         void setInDefaultCategory(boolean included) {
300             if (included != isInDefaultCategory())
301                 fPreferenceModel.writeInclusionPreference(this, included);
302         }
303         String JavaDoc getId() {
304             return fCategory.getId();
305         }
306         int getRank() {
307             int rank= getInternalRank();
308             if (rank > PreferenceModel.LIMIT)
309                 return rank - PreferenceModel.LIMIT;
310             return rank;
311         }
312         void moveUp() {
313             fPreferenceModel.moveUp(this);
314         }
315         void moveDown() {
316             fPreferenceModel.moveDown(this);
317         }
318         private int getInternalRank() {
319             return fPreferenceModel.readOrderPreference(fCategory);
320         }
321         boolean isSeparateCommand() {
322             return getInternalRank() < PreferenceModel.LIMIT;
323         }
324         
325         void setSeparateCommand(boolean separate) {
326             if (separate != isSeparateCommand())
327                 fPreferenceModel.writeOrderPreference(this, separate);
328         }
329         
330         void update() {
331             fCategory.setIncluded(isInDefaultCategory());
332             int rank= getInternalRank();
333             fCategory.setSortOrder(rank);
334             fCategory.setSeparateCommand(rank < PreferenceModel.LIMIT);
335         }
336     }
337     
338     /** element type: {@link ModelElement}. */
339     private final PreferenceModel fModel;
340     private final Map JavaDoc fImages= new HashMap JavaDoc();
341
342     private CheckboxTableViewer fDefaultViewer;
343     private CheckboxTableViewer fSeparateViewer;
344     private Button fUpButton;
345     private Button fDownButton;
346     
347     CodeAssistAdvancedConfigurationBlock(IStatusChangeListener statusListener, IWorkbenchPreferenceContainer container) {
348         super(statusListener, null, getAllKeys(), container);
349         fModel= new PreferenceModel(CompletionProposalComputerRegistry.getDefault());
350     }
351
352     /*
353      * @see org.eclipse.jdt.internal.ui.preferences.OptionsConfigurationBlock#createContents(org.eclipse.swt.widgets.Composite)
354      */

355     protected Control createContents(Composite parent) {
356         
357         ScrolledPageContent scrolled= new ScrolledPageContent(parent, SWT.H_SCROLL | SWT.V_SCROLL);
358         
359         scrolled.setExpandHorizontal(true);
360         scrolled.setExpandVertical(true);
361         
362         Composite composite= new Composite(scrolled, SWT.NONE);
363         int columns= 2;
364         GridLayout layout= new GridLayout(columns, false);
365         layout.marginWidth= 0;
366         layout.marginHeight= 0;
367         composite.setLayout(layout);
368         
369         
370         createDefaultLabel(composite, columns);
371         createDefaultViewer(composite, columns);
372         createKeysLink(composite, columns);
373         
374         createFiller(composite, columns);
375         
376         createSeparateLabel(composite, columns);
377         createSeparateSection(composite);
378         
379         createFiller(composite, columns);
380         
381         createParameterTimeoutControl(composite, columns);
382         
383         updateControls();
384         if (fModel.elements.size() > 0) {
385             fDefaultViewer.getTable().select(0);
386             fSeparateViewer.getTable().select(0);
387             handleTableSelection();
388         }
389         
390         scrolled.setContent(composite);
391         scrolled.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
392         return scrolled;
393     }
394     
395     private void createDefaultLabel(Composite composite, int h_span) {
396         final ICommandService commandSvc= (ICommandService) PlatformUI.getWorkbench().getAdapter(ICommandService.class);
397         final Command command= commandSvc.getCommand(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
398         ParameterizedCommand pCmd= new ParameterizedCommand(command, null);
399         String JavaDoc key= getKeyboardShortcut(pCmd);
400         if (key == null)
401             key= PreferencesMessages.CodeAssistAdvancedConfigurationBlock_no_shortcut;
402
403         PixelConverter pixelConverter= new PixelConverter(composite);
404         int width= pixelConverter.convertWidthInCharsToPixels(40);
405         
406         Label label= new Label(composite, SWT.NONE | SWT.WRAP);
407         label.setText(Messages.format(PreferencesMessages.CodeAssistAdvancedConfigurationBlock_page_description, new Object JavaDoc[] { key }));
408         GridData gd= new GridData(GridData.FILL, GridData.FILL, true, false, h_span, 1);
409         gd.widthHint= width;
410         label.setLayoutData(gd);
411         
412         createFiller(composite, h_span);
413
414         label= new Label(composite, SWT.NONE | SWT.WRAP);
415         label.setText(PreferencesMessages.CodeAssistAdvancedConfigurationBlock_default_table_description);
416         gd= new GridData(GridData.FILL, GridData.FILL, true, false, h_span, 1);
417         gd.widthHint= width;
418         label.setLayoutData(gd);
419     }
420
421     private void createDefaultViewer(Composite composite, int h_span) {
422         fDefaultViewer= CheckboxTableViewer.newCheckList(composite, SWT.SINGLE | SWT.BORDER);
423         Table table= fDefaultViewer.getTable();
424         table.setHeaderVisible(true);
425         table.setLinesVisible(false);
426         table.setLayoutData(new GridData(GridData.FILL, GridData.BEGINNING, false, false, h_span, 1));
427         
428         TableColumn nameColumn= new TableColumn(table, SWT.NONE);
429         nameColumn.setText(PreferencesMessages.CodeAssistAdvancedConfigurationBlock_default_table_category_column_title);
430         nameColumn.setResizable(false);
431         TableColumn keyColumn= new TableColumn(table, SWT.NONE);
432         keyColumn.setText(PreferencesMessages.CodeAssistAdvancedConfigurationBlock_default_table_keybinding_column_title);
433         keyColumn.setResizable(false);
434         
435         fDefaultViewer.addCheckStateListener(new ICheckStateListener() {
436             public void checkStateChanged(CheckStateChangedEvent event) {
437                 boolean checked= event.getChecked();
438                 ModelElement element= (ModelElement) event.getElement();
439                 element.setInDefaultCategory(checked);
440             }
441         });
442         
443         fDefaultViewer.setContentProvider(new ArrayContentProvider());
444         
445         DefaultTableLabelProvider labelProvider= new DefaultTableLabelProvider();
446         fDefaultViewer.setLabelProvider(labelProvider);
447         fDefaultViewer.setInput(fModel.elements);
448         fDefaultViewer.setComparator(new ViewerComparator()); // sort alphabetically
449

450         final int ICON_AND_CHECKBOX_WITH= 50;
451         final int HEADER_MARGIN= 20;
452         int minNameWidth= computeWidth(table, nameColumn.getText()) + HEADER_MARGIN;
453         int minKeyWidth= computeWidth(table, keyColumn.getText()) + HEADER_MARGIN;
454         for (int i= 0; i < fModel.elements.size(); i++) {
455             minNameWidth= Math.max(minNameWidth, computeWidth(table, labelProvider.getColumnText(fModel.elements.get(i), 0)) + ICON_AND_CHECKBOX_WITH);
456             minKeyWidth= Math.max(minKeyWidth, computeWidth(table, labelProvider.getColumnText(fModel.elements.get(i), 1)));
457         }
458         
459         nameColumn.setWidth(minNameWidth);
460         keyColumn.setWidth(minKeyWidth);
461     }
462     
463     private void createKeysLink(Composite composite, int h_span) {
464         Link link= new Link(composite, SWT.NONE | SWT.WRAP);
465         link.setText(PreferencesMessages.CodeAssistAdvancedConfigurationBlock_key_binding_hint);
466         link.addSelectionListener(new SelectionAdapter() {
467             public void widgetSelected(SelectionEvent e) {
468                 PreferencesUtil.createPreferenceDialogOn(getShell(), e.text, null, null);
469             }
470         });
471         
472         PixelConverter pixelConverter= new PixelConverter(composite);
473         int width= pixelConverter.convertWidthInCharsToPixels(40);
474
475         // limit the size of the Link as it would take all it can get
476
GridData gd= new GridData(GridData.FILL, GridData.FILL, false, false, h_span, 1);
477         gd.widthHint= width;
478         link.setLayoutData(gd);
479     }
480
481     private void createFiller(Composite composite, int h_span) {
482         Label filler= new Label(composite, SWT.NONE);
483         filler.setVisible(false);
484         filler.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, h_span, 1));
485     }
486
487     private void createSeparateLabel(Composite composite, int h_span) {
488         PixelConverter pixelConverter= new PixelConverter(composite);
489         int width= pixelConverter.convertWidthInCharsToPixels(40);
490         
491         Label label= new Label(composite, SWT.NONE | SWT.WRAP);
492         label.setText(PreferencesMessages.CodeAssistAdvancedConfigurationBlock_separate_table_description);
493         GridData gd= new GridData(GridData.FILL, GridData.FILL, false, false, h_span, 1);
494         gd.widthHint= width;
495         label.setLayoutData(gd);
496     }
497     
498     private void createSeparateSection(Composite composite) {
499         createSeparateViewer(composite);
500         createButtonList(composite);
501     }
502
503     private void createSeparateViewer(Composite composite) {
504         fSeparateViewer= CheckboxTableViewer.newCheckList(composite, SWT.SINGLE | SWT.BORDER);
505         Table table= fSeparateViewer.getTable();
506         table.setHeaderVisible(false);
507         table.setLinesVisible(false);
508         table.setLayoutData(new GridData(GridData.FILL, GridData.BEGINNING, true, false, 1, 1));
509         
510         TableColumn nameColumn= new TableColumn(table, SWT.NONE);
511         nameColumn.setText(PreferencesMessages.CodeAssistAdvancedConfigurationBlock_separate_table_category_column_title);
512         nameColumn.setResizable(false);
513         
514         fSeparateViewer.setContentProvider(new ArrayContentProvider());
515         
516         ITableLabelProvider labelProvider= new SeparateTableLabelProvider();
517         fSeparateViewer.setLabelProvider(labelProvider);
518         fSeparateViewer.setInput(fModel.elements);
519         
520         final int ICON_AND_CHECKBOX_WITH= 50;
521         final int HEADER_MARGIN= 20;
522         int minNameWidth= computeWidth(table, nameColumn.getText()) + HEADER_MARGIN;
523         for (int i= 0; i < fModel.elements.size(); i++) {
524             minNameWidth= Math.max(minNameWidth, computeWidth(table, labelProvider.getColumnText(fModel.elements.get(i), 0)) + ICON_AND_CHECKBOX_WITH);
525         }
526         
527         nameColumn.setWidth(minNameWidth);
528         
529         fSeparateViewer.addCheckStateListener(new ICheckStateListener() {
530             public void checkStateChanged(CheckStateChangedEvent event) {
531                 boolean checked= event.getChecked();
532                 ModelElement element= (ModelElement) event.getElement();
533                 element.setSeparateCommand(checked);
534             }
535         });
536         
537         table.addSelectionListener(new SelectionAdapter() {
538             public void widgetSelected(SelectionEvent e) {
539                 handleTableSelection();
540             }
541         });
542         
543     }
544     
545     private void createButtonList(Composite parent) {
546         Composite composite= new Composite(parent, SWT.NONE);
547         composite.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false));
548         
549         GridLayout layout= new GridLayout();
550         layout.marginWidth= 0;
551         layout.marginHeight= 0;
552         composite.setLayout(layout);
553         
554         fUpButton= new Button(composite, SWT.PUSH | SWT.CENTER);
555         fUpButton.setText(PreferencesMessages.CodeAssistAdvancedConfigurationBlock_Up);
556         fUpButton.addSelectionListener(new SelectionAdapter() {
557             public void widgetSelected(SelectionEvent e) {
558                 int index= getSelectionIndex();
559                 if (index != -1) {
560                     ((ModelElement) fModel.elements.get(index)).moveUp();
561                     fSeparateViewer.refresh();
562                     handleTableSelection();
563                 }
564             }
565         });
566         fUpButton.setLayoutData(new GridData());
567         SWTUtil.setButtonDimensionHint(fUpButton);
568         
569         fDownButton= new Button(composite, SWT.PUSH | SWT.CENTER);
570         fDownButton.setText(PreferencesMessages.CodeAssistAdvancedConfigurationBlock_Down);
571         fDownButton.addSelectionListener(new SelectionAdapter() {
572             public void widgetSelected(SelectionEvent e) {
573                 int index= getSelectionIndex();
574                 if (index != -1) {
575                     ((ModelElement) fModel.elements.get(index)).moveDown();
576                     fSeparateViewer.refresh();
577                     handleTableSelection();
578                 }
579             }
580         });
581         fDownButton.setLayoutData(new GridData());
582         SWTUtil.setButtonDimensionHint(fDownButton);
583     }
584
585     private void createParameterTimeoutControl(Composite composite, int h_span) {
586         Composite timeoutComposite= new Composite(composite, SWT.NONE);
587         GridLayout layout= new GridLayout(4, false);
588         layout.marginWidth= 0;
589         layout.marginHeight= 0;
590         timeoutComposite.setLayout(layout);
591         GridData gd= new GridData(GridData.FILL, GridData.FILL, true, false, h_span, 1);
592         timeoutComposite.setLayoutData(gd);
593         
594         PixelConverter pixelConverter= new PixelConverter(composite);
595         String JavaDoc str= PreferencesMessages.CodeAssistAdvancedConfigurationBlock_parameterNameFromAttachedJavadoc_timeout;
596         addTextField(timeoutComposite, str, PREF_CODEASSIST_TIMEOUT_FOR_PARAMETER_NAME_FROM_ATTACHED_JAVADOC, 0, pixelConverter.convertWidthInCharsToPixels(7));
597         
598         Label ms= new Label(timeoutComposite, SWT.NONE);
599         gd= new GridData();
600         ms.setLayoutData(gd);
601         ms.setText(PreferencesMessages.CodeAssistAdvancedConfigurationBlock_parameterNameFromAttachedJavadoc_timeout_ms);
602         
603     }
604
605     private void handleTableSelection() {
606         ModelElement item= getSelectedItem();
607         if (item != null) {
608             int index= getSelectionIndex();
609             fUpButton.setEnabled(index > 0);
610             fDownButton.setEnabled(index < fModel.elements.size() - 1);
611         } else {
612             fUpButton.setEnabled(false);
613             fDownButton.setEnabled(false);
614         }
615     }
616     
617     private ModelElement getSelectedItem() {
618         return (ModelElement) ((IStructuredSelection) fSeparateViewer.getSelection()).getFirstElement();
619     }
620     
621     private int getSelectionIndex() {
622         return fSeparateViewer.getTable().getSelectionIndex();
623     }
624     
625     /*
626      * @see org.eclipse.jdt.internal.ui.preferences.OptionsConfigurationBlock#updateControls()
627      */

628     protected void updateControls() {
629         super.updateControls();
630
631         fModel.update();
632         updateCheckedState();
633         fDefaultViewer.refresh();
634         fSeparateViewer.refresh();
635         handleTableSelection();
636     }
637     
638     private void updateCheckedState() {
639         final int size= fModel.elements.size();
640         List JavaDoc defaultChecked= new ArrayList JavaDoc(size);
641         List JavaDoc separateChecked= new ArrayList JavaDoc(size);
642
643         for (Iterator JavaDoc it= fModel.elements.iterator(); it.hasNext();) {
644             ModelElement element= (ModelElement) it.next();
645             if (element.isInDefaultCategory())
646                 defaultChecked.add(element);
647             if (element.isSeparateCommand())
648                 separateChecked.add(element);
649         }
650
651         fDefaultViewer.setCheckedElements(defaultChecked.toArray(new Object JavaDoc[defaultChecked.size()]));
652         fSeparateViewer.setCheckedElements(separateChecked.toArray(new Object JavaDoc[separateChecked.size()]));
653     }
654
655     /*
656      * @see org.eclipse.jdt.internal.ui.preferences.OptionsConfigurationBlock#processChanges(org.eclipse.ui.preferences.IWorkbenchPreferenceContainer)
657      */

658     protected boolean processChanges(IWorkbenchPreferenceContainer container) {
659         for (Iterator JavaDoc it= fModel.elements.iterator(); it.hasNext();) {
660             ModelElement item= (ModelElement) it.next();
661             item.update();
662         }
663         
664         return super.processChanges(container);
665     }
666
667     /*
668      * @see org.eclipse.jdt.internal.ui.preferences.OptionsConfigurationBlock#validateSettings(org.eclipse.jdt.internal.ui.preferences.OptionsConfigurationBlock.Key, java.lang.String, java.lang.String)
669      */

670     protected void validateSettings(Key changedKey, String JavaDoc oldValue, String JavaDoc newValue) {
671         if (changedKey == PREF_CODEASSIST_TIMEOUT_FOR_PARAMETER_NAME_FROM_ATTACHED_JAVADOC) {
672             final StatusInfo status= new StatusInfo();
673             if (newValue.length() == 0)
674                 status.setError(PreferencesMessages.CodeAssistAdvancedConfigurationBlock_parameterNameFromAttachedJavadoc_timeout_emptyInput);
675             else {
676                 try {
677                     int number= Integer.parseInt(newValue);
678                     int min= 0;
679                     int max= 5000;
680                     if (number < min || number > max) {
681                         String JavaDoc msgFormat= PreferencesMessages.CodeAssistAdvancedConfigurationBlock_parameterNameFromAttachedJavadoc_timeout_invalidRange;
682                         String JavaDoc msg= Messages.format(msgFormat, new Object JavaDoc[] {new Integer JavaDoc(min), new Integer JavaDoc(max)});
683                         status.setError(msg);
684                     }
685                 } catch (NumberFormatException JavaDoc ex) {
686                     String JavaDoc msgFormat= PreferencesMessages.CodeAssistAdvancedConfigurationBlock_parameterNameFromAttachedJavadoc_timeout_invalidInput;
687                     String JavaDoc msg= Messages.format(msgFormat, newValue);
688                     status.setError(msg);
689                 }
690             }
691             fContext.statusChanged(status);
692         }
693     }
694
695     /*
696      * @see org.eclipse.jdt.internal.ui.preferences.OptionsConfigurationBlock#getFullBuildDialogStrings(boolean)
697      */

698     protected String JavaDoc[] getFullBuildDialogStrings(boolean workspaceSettings) {
699         // no builds triggered by our settings
700
return null;
701     }
702     
703     /*
704      * @see org.eclipse.jdt.internal.ui.preferences.OptionsConfigurationBlock#dispose()
705      */

706     public void dispose() {
707         for (Iterator JavaDoc it= fImages.values().iterator(); it.hasNext();) {
708             Image image= (Image) it.next();
709             image.dispose();
710         }
711         
712         super.dispose();
713     }
714
715     private int computeWidth(Control control, String JavaDoc name) {
716         if (name == null)
717             return 0;
718         GC gc= new GC(control);
719         try {
720             gc.setFont(JFaceResources.getDialogFont());
721             return gc.stringExtent(name).x + 10;
722         } finally {
723             gc.dispose();
724         }
725     }
726
727     private static BindingManager fgLocalBindingManager;
728     static {
729         fgLocalBindingManager= new BindingManager(new ContextManager(), new CommandManager());
730         final IBindingService bindingService= (IBindingService)PlatformUI.getWorkbench().getService(IBindingService.class);
731         final Scheme[] definedSchemes= bindingService.getDefinedSchemes();
732         if (definedSchemes != null) {
733             try {
734                 for (int i = 0; i < definedSchemes.length; i++) {
735                     final Scheme scheme= definedSchemes[i];
736                     final Scheme copy= fgLocalBindingManager.getScheme(scheme.getId());
737                     copy.define(scheme.getName(), scheme.getDescription(), scheme.getParentId());
738                 }
739             } catch (final NotDefinedException e) {
740                 JavaPlugin.log(e);
741             }
742         }
743         fgLocalBindingManager.setLocale(bindingService.getLocale());
744         fgLocalBindingManager.setPlatform(bindingService.getPlatform());
745     }
746
747     private static String JavaDoc getKeyboardShortcut(ParameterizedCommand command) {
748         IBindingService bindingService= (IBindingService) PlatformUI.getWorkbench().getAdapter(IBindingService.class);
749         fgLocalBindingManager.setBindings(bindingService.getBindings());
750         try {
751             Scheme activeScheme= bindingService.getActiveScheme();
752             if (activeScheme != null)
753                 fgLocalBindingManager.setActiveScheme(activeScheme);
754         } catch (NotDefinedException e) {
755             JavaPlugin.log(e);
756         }
757         
758         TriggerSequence[] bindings= fgLocalBindingManager.getActiveBindingsDisregardingContextFor(command);
759         if (bindings.length > 0)
760             return bindings[0].format();
761         return null;
762     }
763
764     private Image getImage(ImageDescriptor imgDesc) {
765         if (imgDesc == null)
766             return null;
767         
768         Image img= (Image) fImages.get(imgDesc);
769         if (img == null) {
770             img= imgDesc.createImage(false);
771             fImages.put(imgDesc, img);
772         }
773         return img;
774     }
775     
776 }
777
Popular Tags