KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > dialogs > TypeSelectionComponent


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.dialogs;
12
13 import java.io.IOException JavaDoc;
14 import java.io.StringReader JavaDoc;
15 import java.io.StringWriter JavaDoc;
16
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.accessibility.AccessibleAdapter;
19 import org.eclipse.swt.accessibility.AccessibleEvent;
20 import org.eclipse.swt.custom.CLabel;
21 import org.eclipse.swt.custom.ViewForm;
22 import org.eclipse.swt.events.DisposeEvent;
23 import org.eclipse.swt.events.DisposeListener;
24 import org.eclipse.swt.events.KeyEvent;
25 import org.eclipse.swt.events.KeyListener;
26 import org.eclipse.swt.events.ModifyEvent;
27 import org.eclipse.swt.events.ModifyListener;
28 import org.eclipse.swt.events.SelectionAdapter;
29 import org.eclipse.swt.events.SelectionEvent;
30 import org.eclipse.swt.events.SelectionListener;
31 import org.eclipse.swt.events.TraverseEvent;
32 import org.eclipse.swt.events.TraverseListener;
33 import org.eclipse.swt.graphics.Font;
34 import org.eclipse.swt.graphics.Point;
35 import org.eclipse.swt.graphics.Rectangle;
36 import org.eclipse.swt.layout.GridData;
37 import org.eclipse.swt.layout.GridLayout;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.swt.widgets.Control;
40 import org.eclipse.swt.widgets.Label;
41 import org.eclipse.swt.widgets.Menu;
42 import org.eclipse.swt.widgets.Table;
43 import org.eclipse.swt.widgets.Text;
44 import org.eclipse.swt.widgets.ToolBar;
45 import org.eclipse.swt.widgets.ToolItem;
46
47 import org.eclipse.jface.action.Action;
48 import org.eclipse.jface.action.IAction;
49 import org.eclipse.jface.action.IMenuManager;
50 import org.eclipse.jface.action.MenuManager;
51 import org.eclipse.jface.dialogs.DialogSettings;
52 import org.eclipse.jface.dialogs.IDialogSettings;
53 import org.eclipse.jface.util.IPropertyChangeListener;
54 import org.eclipse.jface.util.PropertyChangeEvent;
55
56 import org.eclipse.ui.IMemento;
57 import org.eclipse.ui.IWorkingSet;
58 import org.eclipse.ui.WorkbenchException;
59 import org.eclipse.ui.XMLMemento;
60
61 import org.eclipse.jdt.core.search.IJavaSearchScope;
62 import org.eclipse.jdt.core.search.SearchEngine;
63 import org.eclipse.jdt.core.search.TypeNameMatch;
64
65 import org.eclipse.jdt.internal.corext.util.Strings;
66
67 import org.eclipse.jdt.ui.dialogs.ITypeSelectionComponent;
68 import org.eclipse.jdt.ui.dialogs.TypeSelectionExtension;
69
70 import org.eclipse.jdt.internal.ui.JavaPlugin;
71 import org.eclipse.jdt.internal.ui.JavaPluginImages;
72 import org.eclipse.jdt.internal.ui.JavaUIMessages;
73 import org.eclipse.jdt.internal.ui.search.JavaSearchScopeFactory;
74 import org.eclipse.jdt.internal.ui.util.PixelConverter;
75 import org.eclipse.jdt.internal.ui.util.SWTUtil;
76 import org.eclipse.jdt.internal.ui.util.TypeNameMatchLabelProvider;
77 import org.eclipse.jdt.internal.ui.workingsets.WorkingSetFilterActionGroup;
78
79 /**
80  * @deprecated use {@link FilteredTypesSelectionDialog}
81  */

82 public class TypeSelectionComponent extends Composite implements ITypeSelectionComponent {
83     
84     private IDialogSettings fSettings;
85     private boolean fMultipleSelection;
86     private ITitleLabel fTitleLabel;
87     
88     private ToolBar fToolBar;
89     private ToolItem fToolItem;
90     private MenuManager fMenuManager;
91     private WorkingSetFilterActionGroup fFilterActionGroup;
92     
93     private TypeSelectionExtension fTypeSelectionExtension;
94     private Text fFilter;
95     private String JavaDoc fInitialFilterText;
96     private IJavaSearchScope fScope;
97     private TypeInfoViewer fViewer;
98     private ViewForm fForm;
99     private CLabel fLabel;
100     
101     public static final int NONE= 0;
102     public static final int CARET_BEGINNING= 1;
103     public static final int FULL_SELECTION= 2;
104     
105     private static final String JavaDoc DIALOG_SETTINGS= "org.eclipse.jdt.internal.ui.dialogs.TypeSelectionComponent"; //$NON-NLS-1$
106
private static final String JavaDoc SHOW_STATUS_LINE= "show_status_line"; //$NON-NLS-1$
107
private static final String JavaDoc FULLY_QUALIFY_DUPLICATES= "fully_qualify_duplicates"; //$NON-NLS-1$
108
private static final String JavaDoc WORKINGS_SET_SETTINGS= "workingset_settings"; //$NON-NLS-1$
109

110     private class ToggleStatusLineAction extends Action {
111         public ToggleStatusLineAction() {
112             super(JavaUIMessages.TypeSelectionComponent_show_status_line_label, IAction.AS_CHECK_BOX);
113         }
114         public void run() {
115             if (fForm == null)
116                 return;
117             GridData gd= (GridData)fForm.getLayoutData();
118             boolean checked= isChecked();
119             gd.exclude= !checked;
120             fForm.setVisible(checked);
121             fSettings.put(SHOW_STATUS_LINE, checked);
122             TypeSelectionComponent.this.layout();
123         }
124     }
125     
126     private class FullyQualifyDuplicatesAction extends Action {
127         public FullyQualifyDuplicatesAction() {
128             super(JavaUIMessages.TypeSelectionComponent_fully_qualify_duplicates_label, IAction.AS_CHECK_BOX);
129         }
130         public void run() {
131             boolean checked= isChecked();
132             fViewer.setFullyQualifyDuplicates(checked, true);
133             fSettings.put(FULLY_QUALIFY_DUPLICATES, checked);
134         }
135     }
136     
137     /**
138      * Special interface to access a title lable in
139      * a generic fashion.
140      */

141     public interface ITitleLabel {
142         /**
143          * Sets the title to the given text
144          *
145          * @param text the title text
146          */

147         public void setText(String JavaDoc text);
148     }
149     
150     public TypeSelectionComponent(Composite parent, int style, String JavaDoc message, boolean multi,
151             IJavaSearchScope scope, int elementKind, String JavaDoc initialFilter, ITitleLabel titleLabel,
152             TypeSelectionExtension extension) {
153         super(parent, style);
154         setFont(parent.getFont());
155         fMultipleSelection= multi;
156         fScope= scope;
157         fInitialFilterText= initialFilter;
158         fTitleLabel= titleLabel;
159         fTypeSelectionExtension= extension;
160         IDialogSettings settings= JavaPlugin.getDefault().getDialogSettings();
161         fSettings= settings.getSection(DIALOG_SETTINGS);
162         if (fSettings == null) {
163             fSettings= new DialogSettings(DIALOG_SETTINGS);
164             settings.addSection(fSettings);
165         }
166         if (fSettings.get(SHOW_STATUS_LINE) == null) {
167             fSettings.put(SHOW_STATUS_LINE, true);
168         }
169         createContent(message, elementKind);
170     }
171     
172     public void triggerSearch() {
173         fViewer.forceSearch();
174     }
175     
176     public TypeNameMatch[] getSelection() {
177         return fViewer.getSelection();
178     }
179     
180     public IJavaSearchScope getScope() {
181         return fScope;
182     }
183     
184     private void createContent(final String JavaDoc message, int elementKind) {
185         GridLayout layout= new GridLayout();
186         layout.numColumns= 2;
187         layout.marginWidth= 0; layout.marginHeight= 0;
188         setLayout(layout);
189         Font font= getFont();
190         
191         Control header= createHeader(this, font, message);
192         GridData gd= new GridData(GridData.FILL_HORIZONTAL);
193         gd.horizontalSpan= 2;
194         header.setLayoutData(gd);
195         
196         fFilter= new Text(this, SWT.BORDER | SWT.FLAT);
197         fFilter.setFont(font);
198         if (fInitialFilterText != null) {
199             fFilter.setText(fInitialFilterText);
200         }
201         gd= new GridData(GridData.FILL_HORIZONTAL);
202         gd.horizontalSpan= 2;
203         fFilter.setLayoutData(gd);
204         fFilter.addModifyListener(new ModifyListener() {
205             public void modifyText(ModifyEvent e) {
206                 patternChanged((Text)e.widget);
207             }
208         });
209         fFilter.addKeyListener(new KeyListener() {
210             public void keyReleased(KeyEvent e) {
211             }
212             public void keyPressed(KeyEvent e) {
213                 if (e.keyCode == SWT.ARROW_DOWN) {
214                     fViewer.setFocus();
215                 }
216             }
217         });
218         fFilter.getAccessible().addAccessibleListener(new AccessibleAdapter() {
219             public void getName(AccessibleEvent e) {
220                 e.result= Strings.removeMnemonicIndicator(message);
221             }
222         });
223         TextFieldNavigationHandler.install(fFilter);
224         
225         Label label= new Label(this, SWT.NONE);
226         label.setFont(font);
227         label.setText(JavaUIMessages.TypeSelectionComponent_label);
228         label.addTraverseListener(new TraverseListener() {
229             public void keyTraversed(TraverseEvent e) {
230                 if (e.detail == SWT.TRAVERSE_MNEMONIC && e.doit) {
231                     e.detail= SWT.TRAVERSE_NONE;
232                     fViewer.setFocus();
233                 }
234             }
235         });
236         label= new Label(this, SWT.RIGHT);
237         label.setFont(font);
238         gd= new GridData(GridData.FILL_HORIZONTAL);
239         label.setLayoutData(gd);
240         fViewer= new TypeInfoViewer(this, fMultipleSelection ? SWT.MULTI : SWT.NONE, label,
241             fScope, elementKind, fInitialFilterText,
242             fTypeSelectionExtension != null ? fTypeSelectionExtension.getFilterExtension() : null,
243             fTypeSelectionExtension != null ? fTypeSelectionExtension.getImageProvider() : null);
244         gd= new GridData(GridData.FILL_BOTH);
245         final Table table= fViewer.getTable();
246         PixelConverter converter= new PixelConverter(table);
247         gd.widthHint= converter.convertWidthInCharsToPixels(70);
248         gd.heightHint= SWTUtil.getTableHeightHint(table, 10);
249         gd.horizontalSpan= 2;
250         table.setLayoutData(gd);
251         table.getAccessible().addAccessibleListener(new AccessibleAdapter() {
252             public void getName(AccessibleEvent e) {
253                 if (table.getSelectionCount() == 0) {
254                     e.result= Strings.removeMnemonicIndicator(JavaUIMessages.TypeSelectionComponent_label);
255                 }
256             }
257         });
258         fViewer.setFullyQualifyDuplicates(fSettings.getBoolean(FULLY_QUALIFY_DUPLICATES), false);
259         if (fTypeSelectionExtension != null) {
260             Control addition= fTypeSelectionExtension.createContentArea(this);
261             if (addition != null) {
262                 addition.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
263             }
264         }
265         if (!fMultipleSelection) {
266             fForm= new ViewForm(this, SWT.BORDER | SWT.FLAT);
267             fForm.setFont(font);
268             gd= new GridData(GridData.FILL_HORIZONTAL);
269             gd.horizontalSpan= 2;
270             boolean showStatusLine= fSettings.getBoolean(SHOW_STATUS_LINE);
271             gd.exclude= !showStatusLine;
272             fForm.setVisible(showStatusLine);
273             fForm.setLayoutData(gd);
274             fLabel= new CLabel(fForm, SWT.FLAT);
275             fLabel.setFont(fForm.getFont());
276             fForm.setContent(fLabel);
277             table.addSelectionListener(new SelectionAdapter() {
278                 private TypeNameMatchLabelProvider fLabelProvider= new TypeNameMatchLabelProvider(
279                     TypeNameMatchLabelProvider.SHOW_TYPE_CONTAINER_ONLY + TypeNameMatchLabelProvider.SHOW_ROOT_POSTFIX);
280                 public void widgetSelected(SelectionEvent event) {
281                     TypeNameMatch[] selection= fViewer.getSelection();
282                     if (selection.length != 1) {
283                         fLabel.setText(""); //$NON-NLS-1$
284
fLabel.setImage(null);
285                     } else {
286                         TypeNameMatch type= selection[0];
287                         fLabel.setText(fViewer.getLabelProvider().getQualificationText(type));
288                         fLabel.setImage(fLabelProvider.getImage(type));
289                     }
290                 }
291             });
292         }
293         addDisposeListener(new DisposeListener() {
294             public void widgetDisposed(DisposeEvent event) {
295                 disposeComponent();
296             }
297         });
298         if (fTypeSelectionExtension != null) {
299             fTypeSelectionExtension.initialize(this);
300         }
301     }
302
303     public void addSelectionListener(SelectionListener listener) {
304         fViewer.getTable().addSelectionListener(listener);
305     }
306     
307     public void populate(int selectionMode) {
308         if (fInitialFilterText != null) {
309             switch(selectionMode) {
310                 case CARET_BEGINNING:
311                     fFilter.setSelection(0, 0);
312                     break;
313                 case FULL_SELECTION:
314                     fFilter.setSelection(0, fInitialFilterText.length());
315                     break;
316             }
317         }
318         fFilter.setFocus();
319         fViewer.startup();
320     }
321     
322     private void patternChanged(Text text) {
323         fViewer.setSearchPattern(text.getText());
324     }
325     
326     private Control createHeader(Composite parent, Font font, String JavaDoc message) {
327         Composite header= new Composite(parent, SWT.NONE);
328         GridLayout layout= new GridLayout();
329         layout.numColumns= 2;
330         layout.marginWidth= 0; layout.marginHeight= 0;
331         header.setLayout(layout);
332         header.setFont(font);
333         Label label= new Label(header, SWT.NONE);
334         label.setText(message);
335         label.setFont(font);
336         label.addTraverseListener(new TraverseListener() {
337             public void keyTraversed(TraverseEvent e) {
338                 if (e.detail == SWT.TRAVERSE_MNEMONIC && e.doit) {
339                     e.detail= SWT.TRAVERSE_NONE;
340                     fFilter.setFocus();
341                 }
342             }
343         });
344         GridData gd= new GridData(GridData.FILL_HORIZONTAL);
345         label.setLayoutData(gd);
346         
347         createViewMenu(header);
348         return header;
349     }
350     
351     private void createViewMenu(Composite parent) {
352         fToolBar= new ToolBar(parent, SWT.FLAT);
353         fToolItem= new ToolItem(fToolBar, SWT.PUSH, 0);
354
355         GridData data= new GridData();
356         data.horizontalAlignment= GridData.END;
357         fToolBar.setLayoutData(data);
358
359         fToolItem.setImage(JavaPluginImages.get(JavaPluginImages.IMG_ELCL_VIEW_MENU));
360         fToolItem.setDisabledImage(JavaPluginImages.get(JavaPluginImages.IMG_DLCL_VIEW_MENU));
361         fToolItem.setToolTipText(JavaUIMessages.TypeSelectionComponent_menu);
362         fToolItem.addSelectionListener(new SelectionAdapter() {
363             public void widgetSelected(SelectionEvent e) {
364                 showViewMenu();
365             }
366         });
367         
368         fMenuManager= new MenuManager();
369         fillViewMenu(fMenuManager);
370
371         // ICommandService commandService= (ICommandService)PlatformUI.getWorkbench().getAdapter(ICommandService.class);
372
// IHandlerService handlerService= (IHandlerService)PlatformUI.getWorkbench().getAdapter(IHandlerService.class);
373
}
374     
375     private void showViewMenu() {
376         Menu menu = fMenuManager.createContextMenu(getShell());
377         Rectangle bounds = fToolItem.getBounds();
378         Point topLeft = new Point(bounds.x, bounds.y + bounds.height);
379         topLeft = fToolBar.toDisplay(topLeft);
380         menu.setLocation(topLeft.x, topLeft.y);
381         menu.setVisible(true);
382     }
383     
384     private void fillViewMenu(IMenuManager viewMenu) {
385         if (!fMultipleSelection) {
386             ToggleStatusLineAction showStatusLineAction= new ToggleStatusLineAction();
387             showStatusLineAction.setChecked(fSettings.getBoolean(SHOW_STATUS_LINE));
388             viewMenu.add(showStatusLineAction);
389         }
390         FullyQualifyDuplicatesAction fullyQualifyDuplicatesAction= new FullyQualifyDuplicatesAction();
391         fullyQualifyDuplicatesAction.setChecked(fSettings.getBoolean(FULLY_QUALIFY_DUPLICATES));
392         viewMenu.add(fullyQualifyDuplicatesAction);
393         if (fScope == null) {
394             fFilterActionGroup= new WorkingSetFilterActionGroup(getShell(),
395                 JavaPlugin.getActivePage(),
396                 new IPropertyChangeListener() {
397                     public void propertyChange(PropertyChangeEvent event) {
398                         IWorkingSet ws= (IWorkingSet)event.getNewValue();
399                         if (ws == null || (ws.isAggregateWorkingSet() && ws.isEmpty())) {
400                             fScope= SearchEngine.createWorkspaceScope();
401                             fTitleLabel.setText(null);
402                         } else {
403                             fScope= JavaSearchScopeFactory.getInstance().createJavaSearchScope(ws, true);
404                             fTitleLabel.setText(ws.getLabel());
405                         }
406                         fViewer.setSearchScope(fScope, true);
407                     }
408                 });
409             String JavaDoc setting= fSettings.get(WORKINGS_SET_SETTINGS);
410             if (setting != null) {
411                 try {
412                     IMemento memento= XMLMemento.createReadRoot(new StringReader JavaDoc(setting));
413                     fFilterActionGroup.restoreState(memento);
414                 } catch (WorkbenchException e) {
415                 }
416             }
417             IWorkingSet ws= fFilterActionGroup.getWorkingSet();
418             if (ws == null || (ws.isAggregateWorkingSet() && ws.isEmpty())) {
419                 fScope= SearchEngine.createWorkspaceScope();
420                 fTitleLabel.setText(null);
421             } else {
422                 fScope= JavaSearchScopeFactory.getInstance().createJavaSearchScope(ws, true);
423                 fTitleLabel.setText(ws.getLabel());
424             }
425             fFilterActionGroup.fillViewMenu(viewMenu);
426         }
427     }
428
429     private void disposeComponent() {
430         if (fFilterActionGroup != null) {
431             XMLMemento memento= XMLMemento.createWriteRoot("workingSet"); //$NON-NLS-1$
432
fFilterActionGroup.saveState(memento);
433             fFilterActionGroup.dispose();
434             StringWriter JavaDoc writer= new StringWriter JavaDoc();
435             try {
436                 memento.save(writer);
437                 fSettings.put(WORKINGS_SET_SETTINGS, writer.getBuffer().toString());
438             } catch (IOException JavaDoc e) {
439                 // don't do anything. Simply don't store the settings
440
}
441         }
442     }
443 }
444
Popular Tags