KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > actions > FindVariableDialog


1 /*******************************************************************************
2  * Copyright (c) 2004, 2005 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 implementation
10  *******************************************************************************/

11 package org.eclipse.debug.internal.ui.actions;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.debug.core.DebugException;
17 import org.eclipse.debug.core.model.IValue;
18 import org.eclipse.debug.core.model.IVariable;
19 import org.eclipse.debug.internal.ui.DebugUIPlugin;
20 import org.eclipse.debug.internal.ui.views.DebugViewDecoratingLabelProvider;
21 import org.eclipse.debug.internal.ui.views.DebugViewInterimLabelProvider;
22 import org.eclipse.debug.internal.ui.views.DebugViewLabelDecorator;
23 import org.eclipse.debug.internal.ui.views.variables.VariablesView;
24 import org.eclipse.debug.internal.ui.views.variables.VariablesViewer;
25 import org.eclipse.debug.ui.IDebugModelPresentation;
26 import org.eclipse.debug.ui.IValueDetailListener;
27 import org.eclipse.jface.dialogs.Dialog;
28 import org.eclipse.jface.dialogs.IDialogConstants;
29 import org.eclipse.jface.viewers.ILabelProvider;
30 import org.eclipse.jface.viewers.ILabelProviderListener;
31 import org.eclipse.jface.viewers.ISelectionChangedListener;
32 import org.eclipse.jface.viewers.IStructuredContentProvider;
33 import org.eclipse.jface.viewers.IStructuredSelection;
34 import org.eclipse.jface.viewers.SelectionChangedEvent;
35 import org.eclipse.jface.viewers.StructuredSelection;
36 import org.eclipse.jface.viewers.TableViewer;
37 import org.eclipse.jface.viewers.Viewer;
38 import org.eclipse.jface.viewers.ViewerFilter;
39 import org.eclipse.swt.SWT;
40 import org.eclipse.swt.events.ModifyEvent;
41 import org.eclipse.swt.events.ModifyListener;
42 import org.eclipse.swt.graphics.Image;
43 import org.eclipse.swt.layout.GridData;
44 import org.eclipse.swt.widgets.Composite;
45 import org.eclipse.swt.widgets.Control;
46 import org.eclipse.swt.widgets.Label;
47 import org.eclipse.swt.widgets.Shell;
48 import org.eclipse.swt.widgets.Table;
49 import org.eclipse.swt.widgets.Text;
50 import org.eclipse.swt.widgets.TreeItem;
51 import org.eclipse.ui.IEditorInput;
52 import org.eclipse.ui.internal.ide.StringMatcher;
53
54 public class FindVariableDialog extends Dialog {
55     
56     private TableViewer fViewer;
57     private VariablesView fView;
58     private Text fText;
59     private StringMatcher fMatcher;
60     private int fTextLength;
61     
62     private class FindVariableContentProvider implements IStructuredContentProvider {
63         public void dispose() {
64         }
65         public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
66         }
67         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
68             return getVariables(((VariablesViewer) fView.getViewer()).getTree().getItems());
69         }
70         private IVariable[] getVariables(TreeItem[] items) {
71             List JavaDoc variables= new ArrayList JavaDoc();
72             getVariables(items, variables);
73             return (IVariable[]) variables.toArray(new IVariable[variables.size()]);
74         }
75         private void getVariables(TreeItem[] items, List JavaDoc variables) {
76             for (int i = 0; i < items.length; i++) {
77                 Object JavaDoc data = items[i].getData();
78                 if (data instanceof IVariable) {
79                     variables.add(data);
80                 }
81                 getVariables(items[i].getItems(), variables);
82             }
83         }
84     }
85     
86     private class FindVariableFilter extends ViewerFilter {
87         public boolean select(Viewer viewer, Object JavaDoc parentElement, Object JavaDoc element) {
88             if (fMatcher == null) {
89                 return true;
90             }
91             try {
92                 String JavaDoc name= ((IVariable) element).getName();
93                 fMatcher.match(name, 0, fTextLength - 1);
94                 return fMatcher.match(name);
95             } catch (DebugException e) {
96             }
97             return false;
98         }
99     }
100     
101     private class FindVariableModelPresentation implements IDebugModelPresentation {
102         public Image getImage(Object JavaDoc element) {
103             IVariable variable= (IVariable) element;
104             IDebugModelPresentation presentation= fView.getPresentation(variable.getModelIdentifier());
105             return presentation.getImage(element);
106         }
107         public String JavaDoc getText(Object JavaDoc element) {
108             try {
109                 return ((IVariable) element).getName();
110             } catch (DebugException e) {
111                 DebugUIPlugin.log(e.getStatus());
112             }
113             return ActionMessages.FindVariableDialog_0; //$NON-NLS-1$
114
}
115         public void setAttribute(String JavaDoc attribute, Object JavaDoc value) {}
116         public void computeDetail(IValue value, IValueDetailListener listener) {}
117         public void addListener(ILabelProviderListener listener) {}
118         public void dispose() {}
119         public boolean isLabelProperty(Object JavaDoc element, String JavaDoc property) {
120             return false;
121         }
122         public void removeListener(ILabelProviderListener listener) {}
123         public IEditorInput getEditorInput(Object JavaDoc element) {
124             return null;
125         }
126         public String JavaDoc getEditorId(IEditorInput input, Object JavaDoc element) {
127             return null;
128         }
129     }
130
131     protected FindVariableDialog(Shell parentShell, VariablesView view) {
132         super(parentShell);
133         fView= view;
134     }
135
136     protected Control createDialogArea(Composite parent) {
137         Composite composite= (Composite) super.createDialogArea(parent);
138         
139         Label label= new Label(composite, SWT.NONE);
140         label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
141         label.setFont(parent.getFont());
142         label.setText(ActionMessages.FindVariableDialog_1); //$NON-NLS-1$
143

144         fText= new Text(composite, SWT.SINGLE | SWT.BORDER);
145         fText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
146         fText.setFont(parent.getFont());
147         fText.addModifyListener(new ModifyListener() {
148             public void modifyText(ModifyEvent e) {
149                 textModified();
150             }
151         });
152         
153         label= new Label(composite, SWT.NONE);
154         label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
155         label.setFont(parent.getFont());
156         label.setText(ActionMessages.FindVariableDialog_2); //$NON-NLS-1$
157

158         fViewer= new TableViewer(composite, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
159         Table table = fViewer.getTable();
160         GridData gridData = new GridData(GridData.FILL_BOTH);
161         gridData.heightHint= 200;
162         table.setLayoutData(gridData);
163         table.setFont(parent.getFont());
164         fViewer.setContentProvider(new FindVariableContentProvider());
165         fViewer.addFilter(new FindVariableFilter());
166         fViewer.setInput(new Object JavaDoc());
167         FindVariableModelPresentation presentation = new FindVariableModelPresentation();
168         ILabelProvider provider= new DebugViewDecoratingLabelProvider(fViewer, new DebugViewInterimLabelProvider(presentation), new DebugViewLabelDecorator(presentation));
169         fViewer.setLabelProvider(provider);
170         fViewer.addPostSelectionChangedListener(new ISelectionChangedListener() {
171             public void selectionChanged(SelectionChangedEvent event) {
172                 FindVariableDialog.this.selectionChanged();
173             }
174         });
175         
176         return composite;
177     }
178     
179     private void textModified() {
180         StringBuffer JavaDoc text = new StringBuffer JavaDoc(fText.getText());
181         if (text.length() == 0 || text.charAt(text.length() - 1) != '*') {
182             text.append("*"); //$NON-NLS-1$
183
}
184         fMatcher= new StringMatcher(text.toString(), true, false);
185         fTextLength= text.length();
186         fViewer.refresh(false);
187         if (((IStructuredSelection) fViewer.getSelection()).isEmpty()) {
188             Table table= fViewer.getTable();
189             if (table.getItemCount() > 0) {
190                 fViewer.setSelection(new StructuredSelection(table.getItem(0).getData()));
191                 selectionChanged();
192             }
193         }
194     }
195     
196     private void selectionChanged() {
197         fView.getViewer().setSelection(fViewer.getSelection(), true);
198     }
199
200     protected void createButtonsForButtonBar(Composite parent) {
201         createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
202                 true);
203     }
204
205     protected void configureShell(Shell newShell) {
206         newShell.setText(ActionMessages.FindVariableDialog_3); //$NON-NLS-1$
207
super.configureShell(newShell);
208     }
209
210 }
211
Popular Tags