KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > editors > text > HyperlinkDetectorsConfigurationBlock


1 /*******************************************************************************
2  * Copyright (c) 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
12 package org.eclipse.ui.internal.editors.text;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collections JavaDoc;
16 import java.util.Comparator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.StringTokenizer JavaDoc;
19
20 import com.ibm.icu.text.Collator;
21
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.events.KeyEvent;
24 import org.eclipse.swt.events.KeyListener;
25 import org.eclipse.swt.events.ModifyEvent;
26 import org.eclipse.swt.events.ModifyListener;
27 import org.eclipse.swt.events.SelectionAdapter;
28 import org.eclipse.swt.events.SelectionEvent;
29 import org.eclipse.swt.events.SelectionListener;
30 import org.eclipse.swt.graphics.Image;
31 import org.eclipse.swt.graphics.Point;
32 import org.eclipse.swt.layout.GridData;
33 import org.eclipse.swt.layout.GridLayout;
34 import org.eclipse.swt.widgets.Button;
35 import org.eclipse.swt.widgets.Composite;
36 import org.eclipse.swt.widgets.Control;
37 import org.eclipse.swt.widgets.Label;
38 import org.eclipse.swt.widgets.Table;
39 import org.eclipse.swt.widgets.TableColumn;
40 import org.eclipse.swt.widgets.Text;
41
42 import org.eclipse.core.runtime.Assert;
43 import org.eclipse.core.runtime.IStatus;
44
45 import org.eclipse.jface.action.Action;
46 import org.eclipse.jface.dialogs.IMessageProvider;
47 import org.eclipse.jface.layout.TableColumnLayout;
48 import org.eclipse.jface.preference.PreferencePage;
49 import org.eclipse.jface.viewers.CheckStateChangedEvent;
50 import org.eclipse.jface.viewers.CheckboxTableViewer;
51 import org.eclipse.jface.viewers.ColumnLayoutData;
52 import org.eclipse.jface.viewers.ColumnWeightData;
53 import org.eclipse.jface.viewers.ICheckStateListener;
54 import org.eclipse.jface.viewers.ILabelProviderListener;
55 import org.eclipse.jface.viewers.IStructuredContentProvider;
56 import org.eclipse.jface.viewers.IStructuredSelection;
57 import org.eclipse.jface.viewers.ITableLabelProvider;
58 import org.eclipse.jface.viewers.Viewer;
59
60 import org.eclipse.ui.editors.text.EditorsUI;
61
62 import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants;
63 import org.eclipse.ui.texteditor.HyperlinkDetectorDescriptor;
64 import org.eclipse.ui.texteditor.HyperlinkDetectorTargetDescriptor;
65
66
67 /**
68  * Configures hyperlink detector preferences.
69  *
70  * @since 3.3
71  */

72 class HyperlinkDetectorsConfigurationBlock implements IPreferenceConfigurationBlock {
73     
74     
75     private static final class ListItem {
76         final String JavaDoc id;
77         final String JavaDoc name;
78         final String JavaDoc description;
79         final String JavaDoc targetId;
80         final String JavaDoc targetName;
81         final String JavaDoc targetDescription;
82         String JavaDoc stateMask;
83         String JavaDoc modifierKeys= ""; //$NON-NLS-1$
84

85         ListItem(String JavaDoc id, String JavaDoc name, String JavaDoc description, String JavaDoc targetId, String JavaDoc targetName, String JavaDoc targetDescription, String JavaDoc modifierKeys) {
86             this.id= id;
87             this.name= name;
88             this.description= description;
89             this.targetId= targetId;
90             this.targetName= targetName;
91             this.targetDescription= targetDescription;
92             this.modifierKeys= modifierKeys;
93         }
94     }
95
96     
97     private static final class ItemContentProvider implements IStructuredContentProvider {
98
99         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
100             return (ListItem[]) inputElement;
101         }
102
103         public void dispose() {
104         }
105
106         public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
107         }
108     }
109     
110
111     private final class ItemLabelProvider implements ITableLabelProvider {
112         
113         public Image getImage(Object JavaDoc element) {
114             return null;
115         }
116
117         /*
118          * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang.Object, int)
119          */

120         public Image getColumnImage(Object JavaDoc element, int columnIndex) {
121             return null;
122         }
123
124         /*
125          * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object, int)
126          */

127         public String JavaDoc getColumnText(Object JavaDoc element, int columnIndex) {
128             switch (columnIndex) {
129             case 0:
130                 return ((ListItem) element).name;
131             case 1:
132                 String JavaDoc text= ((ListItem) element).modifierKeys;
133                 if (text == null)
134                     return fHyperlinkDefaultKeyModifierText.getText();
135                 return text;
136             case 2:
137                 return ((ListItem) element).targetName;
138             default:
139                 Assert.isLegal(false);
140             }
141             return null; // cannot happen
142
}
143
144         /*
145          * @see org.eclipse.jface.viewers.IBaseLabelProvider#addListener(org.eclipse.jface.viewers.ILabelProviderListener)
146          */

147         public void addListener(ILabelProviderListener listener) {
148         }
149
150         /*
151          * @see org.eclipse.jface.viewers.IBaseLabelProvider#dispose()
152          */

153         public void dispose() {
154         }
155
156         /*
157          * @see org.eclipse.jface.viewers.IBaseLabelProvider#isLabelProperty(java.lang.Object, java.lang.String)
158          */

159         public boolean isLabelProperty(Object JavaDoc element, String JavaDoc property) {
160             return false;
161         }
162
163         /*
164          * @see org.eclipse.jface.viewers.IBaseLabelProvider#removeListener(org.eclipse.jface.viewers.ILabelProviderListener)
165          */

166         public void removeListener(ILabelProviderListener listener) {
167         }
168     }
169
170     
171     private static final String JavaDoc MODIFIER_DELIMITER= TextEditorMessages.HyperlinkKeyModifier_delimiter;
172
173
174     private OverlayPreferenceStore fStore;
175
176     private CheckboxTableViewer fHyperlinkDetectorsViewer;
177     private ListItem[] fListModel;
178     private final HyperlinkDetectorDescriptor[] fHyperlinkDetectorDescriptors;
179     
180     private Text fHyperlinkDefaultKeyModifierText;
181     private Text fHyperlinkKeyModifierText;
182     private Button fHyperlinksEnabledCheckBox;
183     private StatusInfo fHyperlinkKeyModifierStatus;
184
185     private PreferencePage fPreferencePage;
186
187
188
189     public HyperlinkDetectorsConfigurationBlock(PreferencePage preferencePage, OverlayPreferenceStore store) {
190         Assert.isNotNull(store);
191         Assert.isNotNull(preferencePage);
192         fStore= store;
193         fPreferencePage= preferencePage;
194         fHyperlinkDetectorDescriptors= EditorsUI.getHyperlinkDetectorRegistry().getHyperlinkDetectorDescriptors();
195         fStore.addKeys(createOverlayStoreKeys());
196     }
197
198     private OverlayPreferenceStore.OverlayKey[] createOverlayStoreKeys() {
199         ArrayList JavaDoc overlayKeys= new ArrayList JavaDoc();
200         
201         for (int i= 0; i < fHyperlinkDetectorDescriptors.length; i++) {
202             overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, fHyperlinkDetectorDescriptors[i].getId()));
203             overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.INT, fHyperlinkDetectorDescriptors[i].getId() + HyperlinkDetectorDescriptor.STATE_MASK_POSTFIX));
204         }
205         
206         overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_HYPERLINK_KEY_MODIFIER));
207         overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_HYPERLINKS_ENABLED));
208
209         OverlayPreferenceStore.OverlayKey[] keys= new OverlayPreferenceStore.OverlayKey[overlayKeys.size()];
210         overlayKeys.toArray(keys);
211         return keys;
212     }
213
214     /*
215      * @see org.eclipse.ui.internal.editors.text.IPreferenceConfigurationBlock#createControl(org.eclipse.swt.widgets.Composite)
216      */

217     public Control createControl(Composite parent) {
218
219         PixelConverter pixelConverter= new PixelConverter(parent);
220
221         Composite composite= new Composite(parent, SWT.NONE);
222         GridLayout layout= new GridLayout();
223         layout.numColumns= 2;
224         layout.marginHeight= 0;
225         layout.marginWidth= 0;
226         composite.setLayout(layout);
227         
228         addFiller(composite, 2);
229
230         String JavaDoc label= TextEditorMessages.HyperlinksEnabled_label;
231         fHyperlinksEnabledCheckBox= addCheckBox(composite, label, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_HYPERLINKS_ENABLED, 0);
232         fHyperlinksEnabledCheckBox.addSelectionListener(new SelectionListener() {
233             public void widgetSelected(SelectionEvent e) {
234                 boolean state= fHyperlinksEnabledCheckBox.getSelection();
235                 fHyperlinkDefaultKeyModifierText.setEnabled(state);
236                 fHyperlinkKeyModifierText.setEnabled(state && getSelectedItem() != null);
237                 fHyperlinkDetectorsViewer.getTable().setEnabled(state);
238                 handleHyperlinkKeyModifierModified();
239             }
240             public void widgetDefaultSelected(SelectionEvent e) {
241             }
242         });
243
244         // Text field for default modifier string
245
label= TextEditorMessages.HyperlinkDefaultKeyModifier_label;
246         fHyperlinkDefaultKeyModifierText= (Text)addTextField(composite, label, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_HYPERLINK_KEY_MODIFIER, 15, 20, pixelConverter)[1];
247
248         fHyperlinkDefaultKeyModifierText.addKeyListener(new KeyListener() {
249             private boolean isModifierCandidate;
250             public void keyPressed(KeyEvent e) {
251                 isModifierCandidate= e.keyCode > 0 && e.character == 0 && e.stateMask == 0;
252             }
253
254             public void keyReleased(KeyEvent e) {
255                 if (isModifierCandidate && e.stateMask > 0 && e.stateMask == e.stateMask && e.character == 0) {// && e.time -time < 1000) {
256
String JavaDoc modifierString= fHyperlinkDefaultKeyModifierText.getText();
257                     Point selection= fHyperlinkDefaultKeyModifierText.getSelection();
258                     int i= selection.x - 1;
259                     while (i > -1 && Character.isWhitespace(modifierString.charAt(i))) {
260                         i--;
261                     }
262                     boolean needsPrefixDelimiter= i > -1 && !String.valueOf(modifierString.charAt(i)).equals(MODIFIER_DELIMITER);
263
264                     i= selection.y;
265                     while (i < modifierString.length() && Character.isWhitespace(modifierString.charAt(i))) {
266                         i++;
267                     }
268                     boolean needsPostfixDelimiter= i < modifierString.length() && !String.valueOf(modifierString.charAt(i)).equals(MODIFIER_DELIMITER);
269
270                     String JavaDoc insertString;
271
272                     if (needsPrefixDelimiter && needsPostfixDelimiter)
273                         insertString= NLSUtility.format(TextEditorMessages.HyperlinkKeyModifier_insertDelimiterAndModifierAndDelimiter, Action.findModifierString(e.stateMask));
274                     else if (needsPrefixDelimiter)
275                         insertString= NLSUtility.format(TextEditorMessages.HyperlinkKeyModifier_insertDelimiterAndModifier, Action.findModifierString(e.stateMask));
276                     else if (needsPostfixDelimiter)
277                         insertString= NLSUtility.format(TextEditorMessages.HyperlinkKeyModifier_insertModifierAndDelimiter, Action.findModifierString(e.stateMask));
278                     else
279                         insertString= Action.findModifierString(e.stateMask);
280
281                     fHyperlinkDefaultKeyModifierText.insert(insertString);
282                 }
283             }
284         });
285
286         fHyperlinkDefaultKeyModifierText.addModifyListener(new ModifyListener() {
287             public void modifyText(ModifyEvent e) {
288                 handleHyperlinkDefaultKeyModifierModified();
289             }
290         });
291         
292         addFiller(composite, 2);
293
294         Composite editorComposite= new Composite(composite, SWT.NONE);
295         GridData gd;
296         
297         // XXX: workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=178097
298
if ("gtk".equals(SWT.getPlatform())) //$NON-NLS-1$
299
gd= new GridData(SWT.FILL, SWT.FILL, true, true);
300             else
301                 gd= new GridData(SWT.FILL, SWT.TOP, true, false);
302         
303         gd.horizontalSpan= 2;
304         gd.horizontalIndent= 20;
305         editorComposite.setLayoutData(gd);
306         TableColumnLayout tableColumnlayout= new TableColumnLayout();
307         editorComposite.setLayout(tableColumnlayout);
308         
309         // Hyperlink detector table
310
Table hyperlinkDetectorTable= new Table(editorComposite, SWT.H_SCROLL | SWT.V_SCROLL | SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION | SWT.CHECK);
311         hyperlinkDetectorTable.setHeaderVisible(true);
312         hyperlinkDetectorTable.setLinesVisible(true);
313         hyperlinkDetectorTable.setFont(parent.getFont());
314         
315         hyperlinkDetectorTable.addSelectionListener(new SelectionListener() {
316             public void widgetSelected(SelectionEvent e) {
317                 handleListSelection();
318             }
319             public void widgetDefaultSelected(SelectionEvent e) {
320             }
321         });
322         
323         ColumnLayoutData columnLayoutData= new ColumnWeightData(1);
324         
325         TableColumn nameColumn= new TableColumn(hyperlinkDetectorTable, SWT.NONE, 0);
326         nameColumn.setText(TextEditorMessages.HyperlinkDetectorTable_nameColumn);
327         tableColumnlayout.setColumnData(nameColumn, columnLayoutData);
328         
329         TableColumn modifierKeysColumn= new TableColumn(hyperlinkDetectorTable, SWT.NONE, 1);
330         modifierKeysColumn.setText(TextEditorMessages.HyperlinkDetectorTable_modifierKeysColumn);
331         tableColumnlayout.setColumnData(modifierKeysColumn, columnLayoutData);
332         
333         TableColumn targetNameColumn= new TableColumn(hyperlinkDetectorTable, SWT.NONE, 2);
334         targetNameColumn.setText(TextEditorMessages.HyperlinkDetectorTable_targetNameColumn);
335         tableColumnlayout.setColumnData(targetNameColumn, columnLayoutData);
336
337         fHyperlinkDetectorsViewer= new CheckboxTableViewer(hyperlinkDetectorTable);
338         fHyperlinkDetectorsViewer.setUseHashlookup(true);
339         
340         fHyperlinkDetectorsViewer.addCheckStateListener(new ICheckStateListener() {
341             /*
342              * @see org.eclipse.jface.viewers.ICheckStateListener#checkStateChanged(org.eclipse.jface.viewers.CheckStateChangedEvent)
343              */

344             public void checkStateChanged(CheckStateChangedEvent event) {
345                 String JavaDoc id= ((ListItem)event.getElement()).id;
346                 if (id == null)
347                     return;
348                 fStore.setValue(id, !event.getChecked());
349             }
350         });
351         
352         fHyperlinkDetectorsViewer.setLabelProvider(new ItemLabelProvider());
353         fHyperlinkDetectorsViewer.setContentProvider(new ItemContentProvider());
354         gd= new GridData(SWT.FILL, SWT.FILL, true , false);
355         gd.heightHint= pixelConverter.convertHeightInCharsToPixels(10);
356         fHyperlinkDetectorsViewer.getControl().setLayoutData(gd);
357         
358 // addFiller(composite, 2);
359

360         // Text field for modifier string
361
label= TextEditorMessages.HyperlinkKeyModifier_label;
362         fHyperlinkKeyModifierText= (Text)addTextField(composite, label, null, 15, 20, pixelConverter)[1];
363
364         fHyperlinkKeyModifierText.addKeyListener(new KeyListener() {
365             private boolean isModifierCandidate;
366             public void keyPressed(KeyEvent e) {
367                 isModifierCandidate= e.keyCode > 0 && e.character == 0 && e.stateMask == 0;
368             }
369
370             public void keyReleased(KeyEvent e) {
371                 if (isModifierCandidate && e.stateMask > 0 && e.stateMask == e.stateMask && e.character == 0) {// && e.time -time < 1000) {
372
String JavaDoc modifierString= fHyperlinkKeyModifierText.getText();
373                     Point selection= fHyperlinkKeyModifierText.getSelection();
374                     int i= selection.x - 1;
375                     while (i > -1 && Character.isWhitespace(modifierString.charAt(i))) {
376                         i--;
377                     }
378                     boolean needsPrefixDelimiter= i > -1 && !String.valueOf(modifierString.charAt(i)).equals(MODIFIER_DELIMITER);
379
380                     i= selection.y;
381                     while (i < modifierString.length() && Character.isWhitespace(modifierString.charAt(i))) {
382                         i++;
383                     }
384                     boolean needsPostfixDelimiter= i < modifierString.length() && !String.valueOf(modifierString.charAt(i)).equals(MODIFIER_DELIMITER);
385
386                     String JavaDoc insertString;
387
388                     if (needsPrefixDelimiter && needsPostfixDelimiter)
389                         insertString= NLSUtility.format(TextEditorMessages.HyperlinkKeyModifier_insertDelimiterAndModifierAndDelimiter, Action.findModifierString(e.stateMask));
390                     else if (needsPrefixDelimiter)
391                         insertString= NLSUtility.format(TextEditorMessages.HyperlinkKeyModifier_insertDelimiterAndModifier, Action.findModifierString(e.stateMask));
392                     else if (needsPostfixDelimiter)
393                         insertString= NLSUtility.format(TextEditorMessages.HyperlinkKeyModifier_insertModifierAndDelimiter, Action.findModifierString(e.stateMask));
394                     else
395                         insertString= Action.findModifierString(e.stateMask);
396
397                     fHyperlinkKeyModifierText.insert(insertString);
398                 }
399             }
400         });
401
402         fHyperlinkKeyModifierText.addModifyListener(new ModifyListener() {
403             public void modifyText(ModifyEvent e) {
404                 handleHyperlinkKeyModifierModified();
405             }
406         });
407         
408         return composite;
409     }
410     
411     private void addFiller(Composite composite, int horizontalSpan) {
412         PixelConverter pixelConverter= new PixelConverter(composite);
413         Label filler= new Label(composite, SWT.LEFT );
414         GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL);
415         gd.horizontalSpan= horizontalSpan;
416         gd.heightHint= pixelConverter.convertHeightInCharsToPixels(1) / 2;
417         filler.setLayoutData(gd);
418     }
419
420     private Button addCheckBox(Composite composite, String JavaDoc label, final String JavaDoc key, int indentation) {
421         final Button checkBox= new Button(composite, SWT.CHECK);
422         checkBox.setText(label);
423
424         GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
425         gd.horizontalIndent= indentation;
426         gd.horizontalSpan= 2;
427         checkBox.setLayoutData(gd);
428         checkBox.addSelectionListener(new SelectionAdapter() {
429             public void widgetSelected(SelectionEvent e) {
430                 boolean value= checkBox.getSelection();
431                 fStore.setValue(key, value);
432             }
433         });
434
435         return checkBox;
436     }
437
438     private Control[] addTextField(Composite composite, String JavaDoc label, final String JavaDoc key, int textLimit, int indentation, PixelConverter pixelConverter) {
439         Label labelControl= new Label(composite, SWT.NONE);
440         labelControl.setText(label);
441         GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
442         gd.horizontalIndent= indentation;
443         labelControl.setLayoutData(gd);
444
445         final Text textControl= new Text(composite, SWT.BORDER | SWT.SINGLE);
446         gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
447         gd.widthHint= pixelConverter.convertWidthInCharsToPixels(textLimit + 1);
448         textControl.setLayoutData(gd);
449         textControl.setTextLimit(textLimit);
450
451         textControl.addModifyListener(new ModifyListener() {
452             public void modifyText(ModifyEvent e) {
453                 String JavaDoc value= textControl.getText();
454                 if (key != null)
455                     fStore.setValue(key, value);
456             }
457         });
458
459         return new Control[] {labelControl, textControl};
460     }
461
462     private Object JavaDoc[] getCheckedItems() {
463         List JavaDoc result= new ArrayList JavaDoc();
464         for (int i= 0; i < fListModel.length; i++)
465             if (!fStore.getBoolean(fListModel[i].id))
466                 result.add(fListModel[i]);
467         return result.toArray();
468     }
469
470     /*
471      * @see org.eclipse.ui.internal.editors.text.IPreferenceConfigurationBlock#canPerformOk()
472      */

473     public boolean canPerformOk() {
474         return true;
475     }
476
477     /*
478      * @see PreferencePage#performOk()
479      */

480     public void performOk() {
481         fStore.setValue(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_HYPERLINK_KEY_MODIFIER_MASK, computeStateMask(fHyperlinkKeyModifierText.getText()));
482     }
483
484     /*
485      * @see PreferencePage#performDefaults()
486      */

487     public void performDefaults() {
488         fStore.loadDefaults();
489         initialize();
490     }
491
492     private void handleListSelection() {
493         ListItem item= getSelectedItem();
494         if (item == null) {
495             fHyperlinkKeyModifierText.setEnabled(false);
496             return;
497         }
498         fHyperlinkKeyModifierText.setEnabled(fHyperlinkDetectorsViewer.getChecked(item));
499         String JavaDoc text= item.modifierKeys;
500         if (text == null)
501             text= fHyperlinkDefaultKeyModifierText.getText();
502         fHyperlinkKeyModifierText.setText(text);
503         
504     }
505
506     public void initialize() {
507         String JavaDoc modifierString= fStore.getString(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_HYPERLINK_KEY_MODIFIER);
508         if (computeStateMask(modifierString) == -1) {
509             // Fix possible illegal modifier string
510
int stateMask= fStore.getInt(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_HYPERLINK_KEY_MODIFIER_MASK);
511             if (stateMask == -1)
512                 fHyperlinkDefaultKeyModifierText.setText(""); //$NON-NLS-1$
513
else
514                 fHyperlinkDefaultKeyModifierText.setText(getModifierString(stateMask));
515         } else
516             fHyperlinkDefaultKeyModifierText.setText(modifierString);
517         boolean isEnabled= fStore.getBoolean(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_HYPERLINKS_ENABLED);
518         fHyperlinksEnabledCheckBox.setSelection(isEnabled);
519         fHyperlinkKeyModifierText.setEnabled(isEnabled);
520         fHyperlinkDefaultKeyModifierText.setEnabled(isEnabled);
521         
522         fListModel= createListModel();
523         fHyperlinkDetectorsViewer.setInput(fListModel);
524         fHyperlinkDetectorsViewer.setAllChecked(false);
525         fHyperlinkDetectorsViewer.setCheckedElements(getCheckedItems());
526         fHyperlinkDetectorsViewer.getTable().setEnabled(isEnabled);
527
528         fHyperlinkKeyModifierText.setText(""); //$NON-NLS-1$
529

530         handleListSelection();
531     }
532
533     private ListItem[] createListModel() {
534         ArrayList JavaDoc listModelItems= new ArrayList JavaDoc();
535         for (int i= 0; i < fHyperlinkDetectorDescriptors.length; i++) {
536             HyperlinkDetectorDescriptor desc= fHyperlinkDetectorDescriptors[i];
537             HyperlinkDetectorTargetDescriptor target= desc.getTarget();
538             
539             int stateMask= fStore.getInt(desc.getId() + HyperlinkDetectorDescriptor.STATE_MASK_POSTFIX);
540             String JavaDoc modifierKeys= getModifierString(stateMask);
541             
542             listModelItems.add(new ListItem(
543                     desc.getId(), desc.getName(), desc.getDescription(), desc.getTargetId(), target.getName(), target.getDescription(), modifierKeys));
544         }
545
546         Comparator JavaDoc comparator= new Comparator JavaDoc() {
547             /*
548              * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
549              */

550             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
551                 if (!(o2 instanceof ListItem))
552                     return -1;
553                 if (!(o1 instanceof ListItem))
554                     return 1;
555
556                 String JavaDoc label1= ((ListItem)o1).name;
557                 String JavaDoc label2= ((ListItem)o2).name;
558
559                 return Collator.getInstance().compare(label1, label2);
560
561             }
562         };
563         Collections.sort(listModelItems, comparator);
564
565         ListItem[] items= new ListItem[listModelItems.size()];
566         listModelItems.toArray(items);
567         return items;
568     }
569
570     private ListItem getSelectedItem() {
571         return (ListItem)((IStructuredSelection) fHyperlinkDetectorsViewer.getSelection()).getFirstElement();
572     }
573
574     private void handleHyperlinkKeyModifierModified() {
575         String JavaDoc modifiers= fHyperlinkKeyModifierText.getText();
576         
577         int stateMask= computeStateMask(modifiers);
578
579         if (fHyperlinksEnabledCheckBox.getSelection() && (stateMask == -1 || (stateMask & SWT.SHIFT) != 0)) {
580             if (stateMask == -1)
581                 fHyperlinkKeyModifierStatus= new StatusInfo(IStatus.ERROR, NLSUtility.format(TextEditorMessages.HyperlinkKeyModifier_error_modifierIsNotValid, modifiers));
582             else
583                 fHyperlinkKeyModifierStatus= new StatusInfo(IStatus.ERROR, TextEditorMessages.HyperlinkKeyModifier_error_shiftIsDisabled);
584             applyToStatusLine(getHyperlinkKeyModifierStatus());
585             fPreferencePage.setValid(getHyperlinkKeyModifierStatus().isOK());
586         } else {
587             ListItem item= getSelectedItem();
588             if (item != null) {
589                 if (item.modifierKeys != null || !modifiers.equalsIgnoreCase(fHyperlinkDefaultKeyModifierText.getText()))
590                     item.modifierKeys= modifiers;
591                 fHyperlinkDetectorsViewer.refresh(getSelectedItem());
592                 fStore.setValue(item.id + HyperlinkDetectorDescriptor.STATE_MASK_POSTFIX, stateMask);
593             }
594             fHyperlinkKeyModifierStatus= new StatusInfo();
595             fPreferencePage.setValid(true);
596             applyToStatusLine(fHyperlinkKeyModifierStatus);
597         }
598     }
599     
600     private void handleHyperlinkDefaultKeyModifierModified() {
601         String JavaDoc modifiers= fHyperlinkDefaultKeyModifierText.getText();
602         
603         int stateMask= computeStateMask(modifiers);
604         
605         if (fHyperlinksEnabledCheckBox.getSelection() && (stateMask == -1 || (stateMask & SWT.SHIFT) != 0)) {
606             if (stateMask == -1)
607                 fHyperlinkKeyModifierStatus= new StatusInfo(IStatus.ERROR, NLSUtility.format(TextEditorMessages.HyperlinkKeyModifier_error_modifierIsNotValid, modifiers));
608             else
609                 fHyperlinkKeyModifierStatus= new StatusInfo(IStatus.ERROR, TextEditorMessages.HyperlinkKeyModifier_error_shiftIsDisabled);
610             applyToStatusLine(getHyperlinkKeyModifierStatus());
611             fPreferencePage.setValid(getHyperlinkKeyModifierStatus().isOK());
612         } else {
613             fStore.setValue(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_HYPERLINK_KEY_MODIFIER, modifiers);
614             fHyperlinkKeyModifierStatus= new StatusInfo();
615             fPreferencePage.setValid(true);
616             applyToStatusLine(fHyperlinkKeyModifierStatus);
617             fHyperlinkDetectorsViewer.refresh();
618             handleListSelection();
619         }
620     }
621
622     private IStatus getHyperlinkKeyModifierStatus() {
623         if (fHyperlinkKeyModifierStatus == null)
624         fHyperlinkKeyModifierStatus= new StatusInfo();
625         return fHyperlinkKeyModifierStatus;
626     }
627
628     /**
629      * Computes the state mask for the given modifier string.
630      *
631      * @param modifiers the string with the modifiers, separated by '+', '-', ';', ',' or '.'
632      * @return the state mask or -1 if the input is invalid
633      */

634     private static final int computeStateMask(String JavaDoc modifiers) {
635         if (modifiers == null)
636             return -1;
637
638         if (modifiers.length() == 0)
639             return SWT.NONE;
640
641         int stateMask= 0;
642         StringTokenizer JavaDoc modifierTokenizer= new StringTokenizer JavaDoc(modifiers, ",;.:+-* "); //$NON-NLS-1$
643
while (modifierTokenizer.hasMoreTokens()) {
644             int modifier= findLocalizedModifier(modifierTokenizer.nextToken());
645             if (modifier == 0 || (stateMask & modifier) == modifier)
646                 return -1;
647             stateMask= stateMask | modifier;
648         }
649         return stateMask;
650     }
651
652     /**
653      * Maps the localized modifier name to a code in the same
654      * manner as #findModifier.
655      *
656      * @param modifierName the modifier name
657      * @return the SWT modifier bit, or <code>0</code> if no match was found
658      */

659     private static final int findLocalizedModifier(String JavaDoc modifierName) {
660         if (modifierName == null)
661             return 0;
662
663         if (modifierName.equalsIgnoreCase(Action.findModifierString(SWT.CTRL)))
664             return SWT.CTRL;
665         if (modifierName.equalsIgnoreCase(Action.findModifierString(SWT.SHIFT)))
666             return SWT.SHIFT;
667         if (modifierName.equalsIgnoreCase(Action.findModifierString(SWT.ALT)))
668             return SWT.ALT;
669         if (modifierName.equalsIgnoreCase(Action.findModifierString(SWT.COMMAND)))
670             return SWT.COMMAND;
671
672         return 0;
673     }
674
675     /**
676      * Returns the modifier string for the given SWT modifier
677      * modifier bits.
678      *
679      * @param stateMask the SWT modifier bits
680      * @return the modifier string
681      */

682     private static final String JavaDoc getModifierString(int stateMask) {
683         if (stateMask == -1)
684             return null;
685         
686         String JavaDoc modifierString= ""; //$NON-NLS-1$
687
if ((stateMask & SWT.CTRL) == SWT.CTRL)
688             modifierString= appendModifierString(modifierString, SWT.CTRL);
689         if ((stateMask & SWT.ALT) == SWT.ALT)
690             modifierString= appendModifierString(modifierString, SWT.ALT);
691         if ((stateMask & SWT.SHIFT) == SWT.SHIFT)
692             modifierString= appendModifierString(modifierString, SWT.SHIFT);
693         if ((stateMask & SWT.COMMAND) == SWT.COMMAND)
694             modifierString= appendModifierString(modifierString, SWT.COMMAND);
695
696         return modifierString;
697     }
698
699     /**
700      * Appends to modifier string of the given SWT modifier bit
701      * to the given modifierString.
702      *
703      * @param modifierString the modifier string
704      * @param modifier an int with SWT modifier bit
705      * @return the concatenated modifier string
706      */

707     private static final String JavaDoc appendModifierString(String JavaDoc modifierString, int modifier) {
708         if (modifierString == null)
709             modifierString= ""; //$NON-NLS-1$
710
String JavaDoc newModifierString= Action.findModifierString(modifier);
711         if (modifierString.length() == 0)
712             return newModifierString;
713         return NLSUtility.format(TextEditorMessages.HyperlinkKeyModifier_concatModifierStrings, new String JavaDoc[] {modifierString, newModifierString});
714     }
715
716     /**
717      * Applies the status to the status line of a dialog page.
718      *
719      * @param status the status
720      */

721     private void applyToStatusLine(IStatus status) {
722         String JavaDoc message= status.getMessage();
723         switch (status.getSeverity()) {
724             case IStatus.OK:
725                 fPreferencePage.setMessage(message, IMessageProvider.NONE);
726                 fPreferencePage.setErrorMessage(null);
727                 break;
728             case IStatus.WARNING:
729                 fPreferencePage.setMessage(message, IMessageProvider.WARNING);
730                 fPreferencePage.setErrorMessage(null);
731                 break;
732             case IStatus.INFO:
733                 fPreferencePage.setMessage(message, IMessageProvider.INFORMATION);
734                 fPreferencePage.setErrorMessage(null);
735                 break;
736             default:
737                 if (message.length() == 0) {
738                     message= null;
739                 }
740                 fPreferencePage.setMessage(null);
741                 fPreferencePage.setErrorMessage(message);
742                 break;
743         }
744     }
745
746     /*
747      * @see org.eclipse.ui.internal.editors.text.IPreferenceConfigurationBlock#dispose()
748      */

749     public void dispose() {
750     }
751
752 }
753
Popular Tags