KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.eclipse.jdt.internal.ui.preferences;
12
13 import java.util.Arrays JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.runtime.Assert;
17 import org.eclipse.core.runtime.IStatus;
18
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.graphics.Image;
21 import org.eclipse.swt.graphics.Point;
22 import org.eclipse.swt.layout.GridData;
23 import org.eclipse.swt.layout.GridLayout;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.Control;
26 import org.eclipse.swt.widgets.Label;
27 import org.eclipse.swt.widgets.Shell;
28 import org.eclipse.swt.widgets.Text;
29
30 import org.eclipse.jface.dialogs.Dialog;
31 import org.eclipse.jface.dialogs.StatusDialog;
32 import org.eclipse.jface.operation.IRunnableContext;
33 import org.eclipse.jface.viewers.LabelProvider;
34 import org.eclipse.jface.viewers.ViewerComparator;
35 import org.eclipse.jface.window.Window;
36
37 import org.eclipse.ui.PlatformUI;
38 import org.eclipse.ui.dialogs.SelectionDialog;
39 import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer;
40
41 import org.eclipse.jdt.core.IType;
42 import org.eclipse.jdt.core.JavaConventions;
43 import org.eclipse.jdt.core.JavaCore;
44 import org.eclipse.jdt.core.JavaModelException;
45 import org.eclipse.jdt.core.search.IJavaSearchScope;
46 import org.eclipse.jdt.core.search.SearchEngine;
47
48 import org.eclipse.jdt.ui.IJavaElementSearchConstants;
49 import org.eclipse.jdt.ui.JavaUI;
50 import org.eclipse.jdt.ui.PreferenceConstants;
51
52 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
53 import org.eclipse.jdt.internal.ui.JavaPluginImages;
54 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
55 import org.eclipse.jdt.internal.ui.dialogs.TextFieldNavigationHandler;
56 import org.eclipse.jdt.internal.ui.util.BusyIndicatorRunnableContext;
57 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
58 import org.eclipse.jdt.internal.ui.util.PixelConverter;
59 import org.eclipse.jdt.internal.ui.viewsupport.JavaElementImageProvider;
60 import org.eclipse.jdt.internal.ui.wizards.IStatusChangeListener;
61 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
62 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
63 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IListAdapter;
64 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IStringButtonAdapter;
65 import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil;
66 import org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField;
67 import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringButtonDialogField;
68
69
70 /**
71  * Configures the Content Assist > Static Members preference page.
72  *
73  * @since 3.3
74  */

75 class CodeAssistFavoritesConfigurationBlock extends OptionsConfigurationBlock {
76     
77     
78     private static class FavoriteStaticMemberInputDialog extends StatusDialog {
79         
80         private class StringButtonAdapter implements IDialogFieldListener, IStringButtonAdapter {
81             /*
82              * @see IDialogFieldListener#dialogFieldChanged(DialogField)
83              */

84             public void dialogFieldChanged(DialogField field) {
85                 doDialogFieldChanged(field);
86             }
87
88             /*
89              * @see IStringButtonAdapter#changeControlPressed(DialogField)
90              */

91             public void changeControlPressed(DialogField field) {
92                 doBrowseTypes();
93             }
94         }
95         
96         private StringButtonDialogField fNameDialogField;
97         private List JavaDoc fExistingEntries;
98         private final boolean fIsEditingMember;
99             
100         public FavoriteStaticMemberInputDialog(Shell parent, List JavaDoc existingEntries, boolean isEditingMember) {
101             super(parent);
102             fIsEditingMember= isEditingMember;
103             setShellStyle(getShellStyle() | SWT.RESIZE);
104             
105             fExistingEntries= existingEntries;
106             
107             String JavaDoc label, title;
108             if (isEditingMember) {
109                 title= PreferencesMessages.FavoriteStaticMemberInputDialog_member_title;
110                 label= PreferencesMessages.FavoriteStaticMemberInputDialog_member_labelText;
111             } else {
112                 title= PreferencesMessages.FavoriteStaticMemberInputDialog_type_title;
113                 label= PreferencesMessages.FavoriteStaticMemberInputDialog_type_labelText;
114             }
115             setTitle(title);
116
117             StringButtonAdapter adapter= new StringButtonAdapter();
118
119             fNameDialogField= new StringButtonDialogField(adapter);
120             fNameDialogField.setLabelText(label);
121             fNameDialogField.setButtonLabel(PreferencesMessages.FavoriteStaticMemberInputDialog_browse_button);
122             fNameDialogField.setDialogFieldListener(adapter);
123             fNameDialogField.setText(""); //$NON-NLS-1$
124
}
125             
126         public void setInitialSelection(String JavaDoc editedEntry) {
127             Assert.isNotNull(editedEntry);
128             if (editedEntry.length() == 0)
129                 fNameDialogField.setText(""); //$NON-NLS-1$
130
else
131                 fNameDialogField.setText(editedEntry);
132         }
133         
134         public String JavaDoc getResult() {
135             String JavaDoc val= fNameDialogField.getText();
136             if (!fIsEditingMember)
137                 val= val + WILDCARD;
138             return val;
139         }
140             
141         protected Control createDialogArea(Composite parent) {
142             Composite composite= (Composite) super.createDialogArea(parent);
143             initializeDialogUnits(parent);
144             
145             GridLayout layout= (GridLayout) composite.getLayout();
146             layout.numColumns= 2;
147             
148             fNameDialogField.doFillIntoGrid(composite, 3);
149             
150             fNameDialogField.getChangeControl(null).setVisible(!fIsEditingMember);
151             
152             LayoutUtil.setHorizontalSpan(fNameDialogField.getLabelControl(null), 2);
153             
154             int fieldWidthHint= convertWidthInCharsToPixels(60);
155             Text text= fNameDialogField.getTextControl(null);
156             LayoutUtil.setWidthHint(text, fieldWidthHint);
157             LayoutUtil.setHorizontalGrabbing(text);
158             LayoutUtil.setHorizontalSpan(text, fIsEditingMember ? 2 : 1);
159             TextFieldNavigationHandler.install(text);
160             
161             DialogField.createEmptySpace(composite, 1);
162             
163             fNameDialogField.postSetFocusOnDialogField(parent.getDisplay());
164             
165             
166             applyDialogFont(composite);
167             return composite;
168         }
169         
170         private void doBrowseTypes() {
171             IRunnableContext context= new BusyIndicatorRunnableContext();
172             IJavaSearchScope scope= SearchEngine.createWorkspaceScope();
173             int style= IJavaElementSearchConstants.CONSIDER_ALL_TYPES;
174             try {
175                 SelectionDialog dialog= JavaUI.createTypeDialog(getShell(), context, scope, style, false, fNameDialogField.getText());
176                 dialog.setTitle(PreferencesMessages.FavoriteStaticMemberInputDialog_ChooseTypeDialog_title);
177                 dialog.setMessage(PreferencesMessages.FavoriteStaticMemberInputDialog_ChooseTypeDialog_description);
178                 if (dialog.open() == Window.OK) {
179                     IType res= (IType) dialog.getResult()[0];
180                     fNameDialogField.setText(res.getFullyQualifiedName('.'));
181                 }
182             } catch (JavaModelException e) {
183                 ExceptionHandler.handle(e, getShell(), PreferencesMessages.FavoriteStaticMemberInputDialog_ChooseTypeDialog_title, PreferencesMessages.FavoriteStaticMemberInputDialog_ChooseTypeDialog_error_message);
184             }
185         }
186         
187         void doDialogFieldChanged(DialogField field) {
188             doValidation();
189         }
190         
191         private void doValidation() {
192             StatusInfo status= new StatusInfo();
193             String JavaDoc newText= fNameDialogField.getText();
194             if (newText.length() == 0) {
195                 status.setError(""); //$NON-NLS-1$
196
} else {
197                 IStatus val= JavaConventions.validateJavaTypeName(newText, JavaCore.VERSION_1_3, JavaCore.VERSION_1_3);
198                 if (val.matches(IStatus.ERROR)) {
199                     if (fIsEditingMember)
200                         status.setError(PreferencesMessages.FavoriteStaticMemberInputDialog_error_invalidMemberName);
201                     else
202                         status.setError(PreferencesMessages.FavoriteStaticMemberInputDialog_error_invalidTypeName);
203                 } else {
204                     if (doesExist(newText, fIsEditingMember)) {
205                         status.setError(PreferencesMessages.FavoriteStaticMemberInputDialog_error_entryExists);
206                     }
207                 }
208             }
209             updateStatus(status);
210         }
211         
212         private boolean doesExist(String JavaDoc name, boolean isStatic) {
213             for (int i= 0; i < fExistingEntries.size(); i++) {
214                 String JavaDoc entry= (String JavaDoc) fExistingEntries.get(i);
215                 if (name.equals(entry)) {
216                     return true;
217                 }
218             }
219             return false;
220         }
221         
222
223         /*
224          * @see org.eclipse.jface.window.Window#configureShell(Shell)
225          */

226         protected void configureShell(Shell newShell) {
227             super.configureShell(newShell);
228             PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell, IJavaHelpContextIds.JAVA_EDITOR_PREFERENCE_PAGE);
229         }
230
231     }
232
233     
234     private static class ListLabelProvider extends LabelProvider {
235         
236         public final Image MEMBER_ICON;
237         private final Image CLASS_ICON;
238
239         public ListLabelProvider() {
240             MEMBER_ICON= JavaElementImageProvider.getDecoratedImage(JavaPluginImages.DESC_MISC_PUBLIC, 0, JavaElementImageProvider.SMALL_SIZE);
241             CLASS_ICON= JavaElementImageProvider.getDecoratedImage(JavaPluginImages.DESC_OBJS_CLASS, 0, JavaElementImageProvider.SMALL_SIZE);
242         }
243         
244         public Image getImage(Object JavaDoc element) {
245             return ((String JavaDoc)element).endsWith(WILDCARD) ? CLASS_ICON : MEMBER_ICON;
246         }
247
248         public String JavaDoc getText(Object JavaDoc element) {
249             return (String JavaDoc)element;
250         }
251     }
252     
253     
254     private class ListAdapter implements IListAdapter, IDialogFieldListener {
255
256         private boolean canEdit(ListDialogField field) {
257             List JavaDoc selected= field.getSelectedElements();
258             return selected.size() == 1;
259         }
260
261         public void customButtonPressed(ListDialogField field, int index) {
262             doButtonPressed(index);
263         }
264
265         public void selectionChanged(ListDialogField field) {
266             fList.enableButton(IDX_EDIT, canEdit(field));
267         }
268
269         public void dialogFieldChanged(DialogField field) {
270             doDialogFieldChanged(field);
271         }
272         
273         public void doubleClicked(ListDialogField field) {
274             if (canEdit(field)) {
275                 doButtonPressed(IDX_EDIT);
276             }
277         }
278     }
279     
280     
281     private static final Key PREF_CODEASSIST_FAVORITE_STATIC_MEMBERS= getJDTUIKey(PreferenceConstants.CODEASSIST_FAVORITE_STATIC_MEMBERS);
282     
283     private static final String JavaDoc WILDCARD= ".*"; //$NON-NLS-1$
284

285     private static final int IDX_NEW_TYPE= 0;
286     private static final int IDX_NEW_MEMBER= 1;
287     private static final int IDX_EDIT= 2;
288     private static final int IDX_REMOVE= 3;
289
290     private ListDialogField fList;
291     
292
293     private static Key[] getAllKeys() {
294         return new Key[] {
295                 PREF_CODEASSIST_FAVORITE_STATIC_MEMBERS
296         };
297     }
298     
299
300     public CodeAssistFavoritesConfigurationBlock(IStatusChangeListener statusListener, IWorkbenchPreferenceContainer workbenchcontainer) {
301         super(statusListener, null, getAllKeys(), workbenchcontainer);
302     }
303
304     protected Control createContents(Composite parent) {
305         ScrolledPageContent scrolled= new ScrolledPageContent(parent, SWT.H_SCROLL | SWT.V_SCROLL);
306         scrolled.setExpandHorizontal(true);
307         scrolled.setExpandVertical(true);
308         
309         Composite control= new Composite(scrolled, SWT.NONE);
310         GridLayout layout= new GridLayout();
311         layout.numColumns= 2;
312         layout.marginWidth= 0;
313         layout.marginHeight= 0;
314         control.setLayout(layout);
315         
316         createFavoriteList(control);
317         
318         initialize();
319         
320         scrolled.setContent(control);
321         final Point size= control.computeSize(SWT.DEFAULT, SWT.DEFAULT);
322         scrolled.setMinSize(size.x, size.y);
323
324         Dialog.applyDialogFont(scrolled);
325         
326         return scrolled;
327     }
328     
329     private void createFavoriteList(Composite parent) {
330         String JavaDoc[] buttonLabels= new String JavaDoc[] {
331                 PreferencesMessages.CodeAssistStaticMembersConfigurationBlock_newType_button,
332                 PreferencesMessages.CodeAssistStaticMembersConfigurationBlock_newMember_button,
333                 PreferencesMessages.CodeAssistStaticMembersConfigurationBlock_edit_button,
334                 PreferencesMessages.CodeAssistStaticMembersConfigurationBlock_remove_button
335         };
336
337         ListAdapter adapter= new ListAdapter();
338
339         fList= new ListDialogField(adapter, buttonLabels, new ListLabelProvider());
340         fList.setDialogFieldListener(adapter);
341         fList.setLabelText(PreferencesMessages.CodeAssistStaticMembersConfigurationBlock_description);
342         fList.setRemoveButtonIndex(IDX_REMOVE);
343         fList.enableButton(IDX_EDIT, false);
344         fList.setViewerComparator(new ViewerComparator());
345         
346         PixelConverter pixelConverter= new PixelConverter(parent);
347         
348         fList.doFillIntoGrid(parent, 3);
349         LayoutUtil.setHorizontalSpan(fList.getLabelControl(null), 2);
350         LayoutUtil.setWidthHint(fList.getLabelControl(null), pixelConverter.convertWidthInCharsToPixels(60));
351         LayoutUtil.setHorizontalGrabbing(fList.getListControl(null));
352         
353         Control listControl= fList.getListControl(null);
354         GridData gd= (GridData)listControl.getLayoutData();
355         gd.verticalAlignment= GridData.BEGINNING;
356         gd.heightHint= pixelConverter.convertHeightInCharsToPixels(7);
357     }
358     
359     public void initialize() {
360         initializeFields();
361     }
362
363     private void initializeFields() {
364         fList.setElements(Arrays.asList(getFavoriteStaticMembersPreference()));
365     }
366     
367     public void performDefaults() {
368         super.performDefaults();
369         initializeFields();
370     }
371     
372     protected String JavaDoc[] getFullBuildDialogStrings(boolean workspaceSettings) {
373         return null;
374     }
375     
376     protected void validateSettings(Key key, String JavaDoc oldValue, String JavaDoc newValue) {
377         // no validation
378
}
379
380     protected void setControlEnabled(Key key, boolean enabled) {
381         Control control= getControl(key);
382         control.setEnabled(enabled);
383         Label label= (Label) fLabels.get(control);
384         if (label != null)
385             label.setEnabled(enabled);
386     }
387
388     private Control getControl(Key key) {
389         for (int i= fComboBoxes.size() - 1; i >= 0; i--) {
390             Control curr= (Control) fComboBoxes.get(i);
391             ControlData data= (ControlData) curr.getData();
392             if (key.equals(data.getKey())) {
393                 return curr;
394             }
395         }
396         for (int i= fCheckBoxes.size() - 1; i >= 0; i--) {
397             Control curr= (Control) fCheckBoxes.get(i);
398             ControlData data= (ControlData) curr.getData();
399             if (key.equals(data.getKey())) {
400                 return curr;
401             }
402         }
403         for (int i= fTextBoxes.size() - 1; i >= 0; i--) {
404             Control curr= (Control) fTextBoxes.get(i);
405             Key currKey= (Key) curr.getData();
406             if (key.equals(currKey)) {
407                 return curr;
408             }
409         }
410         return null;
411     }
412     
413     private void doButtonPressed(int index) {
414         if (index == IDX_NEW_TYPE || index == IDX_NEW_MEMBER) { // add new
415
List JavaDoc existing= fList.getElements();
416             FavoriteStaticMemberInputDialog dialog= new FavoriteStaticMemberInputDialog(getShell(), existing, index == IDX_NEW_MEMBER);
417             if (dialog.open() == Window.OK) {
418                 fList.addElement(dialog.getResult());
419             }
420         } else if (index == IDX_EDIT) { // edit
421
List JavaDoc selected= fList.getSelectedElements();
422             if (selected.isEmpty())
423                 return;
424
425             String JavaDoc editedEntry= (String JavaDoc)selected.get(0);
426             
427             List JavaDoc existing= fList.getElements();
428             existing.remove(editedEntry);
429             boolean isType= editedEntry.endsWith(WILDCARD);
430             FavoriteStaticMemberInputDialog dialog= new FavoriteStaticMemberInputDialog(getShell(), existing, !isType);
431             if (isType)
432                 dialog.setInitialSelection(editedEntry.substring(0, editedEntry.length() - 2));
433             else
434                 dialog.setInitialSelection(editedEntry);
435             
436             if (dialog.open() == Window.OK) {
437                 fList.replaceElement(editedEntry, dialog.getResult());
438             }
439         }
440     }
441     
442     protected final void doDialogFieldChanged(DialogField field) {
443         // set values in working copy
444
if (field == fList)
445             setValue(PREF_CODEASSIST_FAVORITE_STATIC_MEMBERS, serializeFavorites(fList.getElements()));
446     }
447
448     private String JavaDoc[] getFavoriteStaticMembersPreference() {
449         String JavaDoc str= getValue(PREF_CODEASSIST_FAVORITE_STATIC_MEMBERS);
450         if (str != null && str.length() > 0)
451             return deserializeFavorites(str);
452         return new String JavaDoc[0];
453     }
454
455     private static String JavaDoc[] deserializeFavorites(String JavaDoc str) {
456         return str.split(";"); //$NON-NLS-1$
457
}
458     
459     private static String JavaDoc serializeFavorites(List JavaDoc favorites) {
460         int size= favorites.size();
461         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
462         for (int i= 0; i < size; i++) {
463             buf.append((String JavaDoc)favorites.get(i));
464             if (i < size -1)
465                 buf.append(';');
466         }
467         return buf.toString();
468     }
469
470 }
471
Popular Tags