KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > search > internal > ui > OpenSearchDialogPageAction


1 /*******************************************************************************
2  * Copyright (c) 2006 Klaus Wenger, Wind River Systems 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  * Klaus Wenger - initial API and implementation
10  * Markus Schorn - cleanup and conversion to inner part of the search plugin.
11  *******************************************************************************/

12
13 package org.eclipse.search.internal.ui;
14
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.swt.widgets.Control;
19 import org.eclipse.swt.widgets.Menu;
20
21 import org.eclipse.jface.action.Action;
22 import org.eclipse.jface.action.ActionContributionItem;
23 import org.eclipse.jface.action.IAction;
24 import org.eclipse.jface.resource.ImageDescriptor;
25 import org.eclipse.jface.viewers.ISelection;
26
27 import org.eclipse.ui.IWorkbenchWindow;
28 import org.eclipse.ui.IWorkbenchWindowPulldownDelegate2;
29 import org.eclipse.ui.activities.WorkbenchActivityHelper;
30
31 public class OpenSearchDialogPageAction implements IWorkbenchWindowPulldownDelegate2 {
32     
33     private IWorkbenchWindow fWorkbenchWindow;
34     private Menu fMenu;
35     private OpenSearchDialogAction fOpenSearchDialogAction;
36
37     /*
38      * @see org.eclipse.ui.IWorkbenchWindowPulldownDelegate2#getMenu(org.eclipse.swt.widgets.Menu)
39      */

40     public Menu getMenu(Menu parent) {
41         fMenu= new Menu(parent);
42         fillMenu(fMenu);
43         return fMenu;
44     }
45     
46     /*
47      * @see org.eclipse.ui.IWorkbenchWindowPulldownDelegate#getMenu(org.eclipse.swt.widgets.Control)
48      */

49     public Menu getMenu(Control parent) {
50         fMenu= new Menu(parent);
51         fillMenu(fMenu);
52         return fMenu;
53     }
54     
55     /*
56      * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
57      */

58     public void dispose() {
59         fMenu= null;
60         if (fOpenSearchDialogAction != null) {
61             fOpenSearchDialogAction.dispose();
62         }
63     }
64     
65     /*
66      * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
67      */

68     public void init(IWorkbenchWindow window) {
69         fWorkbenchWindow= window;
70     }
71     
72     /*
73      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
74      */

75     public void run(IAction action) {
76         if (fOpenSearchDialogAction == null) {
77             fOpenSearchDialogAction= new OpenSearchDialogAction();
78         }
79         fOpenSearchDialogAction.run(action);
80     }
81     
82     /*
83      * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
84      */

85     public void selectionChanged(IAction action, ISelection sel) {
86         // Empty
87
}
88     
89     protected void fillMenu(Menu localMenu) {
90         List JavaDoc pageDescriptors= SearchPlugin.getDefault().getSearchPageDescriptors();
91         int accelerator= 1;
92         for (Iterator JavaDoc iter= pageDescriptors.iterator(); iter.hasNext();) {
93             SearchPageDescriptor desc= (SearchPageDescriptor) iter.next();
94             if (!WorkbenchActivityHelper.filterItem(desc)) {
95                 SearchPageAction action= new SearchPageAction(fWorkbenchWindow, desc);
96                 addToMenu(localMenu, action, accelerator++);
97             }
98         }
99     }
100     
101     protected void addToMenu(Menu localMenu, IAction action, int accelerator) {
102         StringBuffer JavaDoc label= new StringBuffer JavaDoc();
103         if (accelerator >= 0 && accelerator < 10) {
104             //add the numerical accelerator
105
label.append('&');
106             label.append(accelerator);
107             label.append(' ');
108         }
109         label.append(action.getText());
110         action.setText(label.toString());
111         ActionContributionItem item= new ActionContributionItem(action);
112         item.fill(localMenu, -1);
113     }
114
115     private static final class SearchPageAction extends Action {
116         private final OpenSearchDialogAction fOpenSearchDialogAction;
117         
118         public SearchPageAction(IWorkbenchWindow workbenchWindow, SearchPageDescriptor pageDescriptor) {
119             super();
120             fOpenSearchDialogAction= new OpenSearchDialogAction(workbenchWindow, pageDescriptor.getId());
121             init(pageDescriptor);
122         }
123
124         private void init(SearchPageDescriptor pageDesc) {
125             setText(pageDesc.getLabel());
126             setToolTipText(pageDesc.getLabel());
127             ImageDescriptor imageDescriptor= pageDesc.getImage();
128             if (imageDescriptor != null) {
129                 setImageDescriptor(imageDescriptor);
130             }
131         }
132         
133         public void run() {
134             fOpenSearchDialogAction.run(this);
135         }
136
137     }
138 }
139
Popular Tags