KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > DetailFormatterDialog


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.debug.ui;
12
13
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.commands.AbstractHandler;
17 import org.eclipse.core.commands.ExecutionEvent;
18 import org.eclipse.core.commands.ExecutionException;
19 import org.eclipse.core.commands.IHandler;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.core.runtime.NullProgressMonitor;
23 import org.eclipse.core.runtime.OperationCanceledException;
24 import org.eclipse.jdt.core.IType;
25 import org.eclipse.jdt.core.JavaCore;
26 import org.eclipse.jdt.core.JavaModelException;
27 import org.eclipse.jdt.core.search.IJavaSearchConstants;
28 import org.eclipse.jdt.core.search.IJavaSearchScope;
29 import org.eclipse.jdt.core.search.SearchEngine;
30 import org.eclipse.jdt.core.search.SearchMatch;
31 import org.eclipse.jdt.core.search.SearchParticipant;
32 import org.eclipse.jdt.core.search.SearchPattern;
33 import org.eclipse.jdt.core.search.SearchRequestor;
34 import org.eclipse.jdt.internal.debug.ui.contentassist.DynamicTypeContext;
35 import org.eclipse.jdt.internal.debug.ui.contentassist.JavaDebugContentAssistProcessor;
36 import org.eclipse.jdt.internal.debug.ui.contentassist.DynamicTypeContext.ITypeProvider;
37 import org.eclipse.jdt.internal.debug.ui.display.DisplayViewerConfiguration;
38 import org.eclipse.jdt.ui.IJavaElementSearchConstants;
39 import org.eclipse.jdt.ui.JavaUI;
40 import org.eclipse.jdt.ui.text.IJavaPartitions;
41 import org.eclipse.jdt.ui.text.JavaTextTools;
42 import org.eclipse.jface.dialogs.IDialogConstants;
43 import org.eclipse.jface.dialogs.StatusDialog;
44 import org.eclipse.jface.text.Document;
45 import org.eclipse.jface.text.DocumentEvent;
46 import org.eclipse.jface.text.IDocument;
47 import org.eclipse.jface.text.IDocumentListener;
48 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
49 import org.eclipse.jface.text.source.ISourceViewer;
50 import org.eclipse.swt.SWT;
51 import org.eclipse.swt.events.ModifyEvent;
52 import org.eclipse.swt.events.ModifyListener;
53 import org.eclipse.swt.graphics.Font;
54 import org.eclipse.swt.layout.GridData;
55 import org.eclipse.swt.widgets.Button;
56 import org.eclipse.swt.widgets.Composite;
57 import org.eclipse.swt.widgets.Control;
58 import org.eclipse.swt.widgets.Event;
59 import org.eclipse.swt.widgets.Listener;
60 import org.eclipse.swt.widgets.Shell;
61 import org.eclipse.swt.widgets.Text;
62 import org.eclipse.ui.IWorkbench;
63 import org.eclipse.ui.PlatformUI;
64 import org.eclipse.ui.dialogs.SelectionDialog;
65 import org.eclipse.ui.handlers.IHandlerActivation;
66 import org.eclipse.ui.handlers.IHandlerService;
67 import org.eclipse.ui.keys.IBindingService;
68 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
69
70 import com.ibm.icu.text.MessageFormat;
71
72 /**
73  * Dialog for edit detail formatter.
74  */

75 public class DetailFormatterDialog extends StatusDialog implements ITypeProvider {
76     
77     /**
78      * The detail formatter to edit.
79      */

80     private DetailFormatter fDetailFormatter;
81
82     // widgets
83
private Text fTypeNameText;
84     private JDISourceViewer fSnippetViewer;
85     private Button fCheckBox;
86
87     /**
88      * Indicate if a search for a type with the given name
89      * have been already performed.
90      */

91     private boolean fTypeSearched;
92     
93     /**
94      * Indicate if the type can be modified.
95      */

96     private boolean fEditTypeName;
97
98     /**
99      * The type object which corresponds to the given name.
100      * If this field is <code>null</code> and <code>fTypeSearched</code> is
101      * <code>true</code>, that means there is no type with the given name in
102      * the workspace.
103      */

104     private IType fType;
105     
106     /**
107      * List of types that have detail formatters already defined.
108      */

109     private List JavaDoc fDefinedTypes;
110
111     /**
112      * Activation handler for content assist, must be deactivated on disposal.
113      */

114     private IHandlerActivation fHandlerActivation;
115     
116     /**
117      * DetailFormatterDialog constructor.
118      *
119      * @param detailFormatter the detail formatter to edit/add.
120      * @param editDialog flag which indicates if the dialog is used for
121      * edit an existing formatter, or for enter the info of a new one.
122      */

123     
124     
125     /**
126      * DetailFormatterDialog constructor. Creates a new dialog to create/edit a detail formatter.
127      *
128      * @param parent parent shell
129      * @param detailFormatter detail formatter to edit, not <code>null</code>
130      * @param definedTypes list of types with detail formatters already defined, or <code>null</code>
131      * @param editDialog whether the dialog is being used to edit a detail formatter
132      */

133     public DetailFormatterDialog(Shell parent, DetailFormatter detailFormatter, List JavaDoc definedTypes, boolean editDialog) {
134         this(parent, detailFormatter, definedTypes, true, editDialog);
135     }
136     
137     /**
138      * DetailFormatterDialog constructor. Creates a new dialog to create/edit a detail formatter.
139      *
140      * @param parent parent shell
141      * @param detailFormatter detail formatter to edit, not <code>null</code>
142      * @param definedTypes list of types with detail formatters already defined, or <code>null</code>
143      * @param editTypeName whether the user should be able to modify the type
144      * @param editDialog whether the dialog is being used to edit a detail formatter
145      */

146     public DetailFormatterDialog(Shell parent, DetailFormatter detailFormatter, List JavaDoc definedTypes, boolean editTypeName, boolean editDialog) {
147         super(parent);
148         fDetailFormatter= detailFormatter;
149         fTypeSearched= false;
150         setShellStyle(getShellStyle() | SWT.MAX | SWT.RESIZE);
151         if (editDialog) {
152             setTitle(DebugUIMessages.DetailFormatterDialog_Edit_Detail_Formatter_1);
153         } else {
154             setTitle(DebugUIMessages.DetailFormatterDialog_Add_Detail_Formatter_2);
155         }
156         fEditTypeName= editTypeName;
157         fDefinedTypes= definedTypes;
158     }
159     
160     /**
161      * Create the dialog area.
162      *
163      * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(Composite)
164      */

165     protected Control createDialogArea(Composite parent) {
166         IWorkbench workbench = PlatformUI.getWorkbench();
167         
168         workbench.getHelpSystem().setHelp(
169             parent,
170             IJavaDebugHelpContextIds.EDIT_DETAIL_FORMATTER_DIALOG);
171         
172         Font font = parent.getFont();
173         Composite container = (Composite)super.createDialogArea(parent);
174         
175         SWTFactory.createLabel(container, DebugUIMessages.DetailFormatterDialog_Qualified_type__name__2, 1);
176
177         Composite innerContainer = SWTFactory.createComposite(container, font, 2, 1, GridData.FILL_HORIZONTAL);
178         
179         fTypeNameText = SWTFactory.createSingleText(innerContainer, 1);
180         fTypeNameText.setEditable(fEditTypeName);
181         fTypeNameText.setText(fDetailFormatter.getTypeName());
182         fTypeNameText.addModifyListener(new ModifyListener() {
183             public void modifyText(ModifyEvent e) {
184                 fTypeSearched= false;
185                 checkValues();
186             }
187         });
188         
189         Button typeSearchButton = SWTFactory.createPushButton(innerContainer, DebugUIMessages.DetailFormatterDialog_Select__type_4, null);
190         typeSearchButton.setEnabled(fEditTypeName);
191         typeSearchButton.addListener(SWT.Selection, new Listener() {
192             public void handleEvent(Event e) {
193                 selectType();
194             }
195         });
196         
197         String JavaDoc labelText = null;
198         IBindingService bindingService = (IBindingService) workbench.getAdapter(IBindingService.class);
199         String JavaDoc binding = bindingService.getBestActiveBindingFormattedFor("org.eclipse.ui.edit.text.contentAssist.proposals"); //$NON-NLS-1$
200
if (binding != null) {
201             labelText = MessageFormat.format(DebugUIMessages.DetailFormatterDialog_17, new String JavaDoc[] { binding });
202         }
203         if (labelText == null) {
204             labelText = DebugUIMessages.DetailFormatterDialog_Detail_formatter__code_snippet__1;
205         }
206         
207         SWTFactory.createLabel(container, labelText, 1);
208
209         createSnippetViewer(container);
210         
211         fCheckBox = SWTFactory.createCheckButton(container, DebugUIMessages.DetailFormatterDialog__Enable_1, null, fDetailFormatter.isEnabled(), 1);
212        
213         // Set up content assist in the viewer
214
IHandler handler = new AbstractHandler() {
215             public Object JavaDoc execute(ExecutionEvent event) throws ExecutionException {
216                 if (fSnippetViewer.canDoOperation(ISourceViewer.CONTENTASSIST_PROPOSALS) && fSnippetViewer.getControl().isFocusControl()){
217                     findCorrespondingType();
218                     fSnippetViewer.doOperation(ISourceViewer.CONTENTASSIST_PROPOSALS);
219                 }
220                 return null;
221             }
222         };
223         IHandlerService handlerService = (IHandlerService) workbench.getAdapter(IHandlerService.class);
224         fHandlerActivation = handlerService.activateHandler(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS, handler);
225         
226         checkValues();
227         return container;
228     }
229
230     /**
231      * Creates the JDISourceViewer that displays the code snippet to the user.
232      *
233      * @param parent parent composite
234      */

235     private void createSnippetViewer(Composite parent) {
236         fSnippetViewer= new JDISourceViewer(parent, null, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL );
237         fSnippetViewer.setInput(this);
238     
239         JavaTextTools tools= JDIDebugUIPlugin.getDefault().getJavaTextTools();
240         IDocument document= new Document();
241         tools.setupJavaDocumentPartitioner(document, IJavaPartitions.JAVA_PARTITIONING);
242         fSnippetViewer.configure(new DisplayViewerConfiguration() {
243             public IContentAssistProcessor getContentAssistantProcessor() {
244                 return new JavaDebugContentAssistProcessor(new DynamicTypeContext(DetailFormatterDialog.this));
245             }
246         });
247         fSnippetViewer.setEditable(true);
248         fSnippetViewer.setDocument(document);
249         
250         Control control= fSnippetViewer.getControl();
251         GridData gd= new GridData(GridData.FILL_BOTH);
252         gd.heightHint= convertHeightInCharsToPixels(10);
253         gd.widthHint= convertWidthInCharsToPixels(80);
254         control.setLayoutData(gd);
255         document.set(fDetailFormatter.getSnippet());
256         
257         fSnippetViewer.getDocument().addDocumentListener(new IDocumentListener() {
258             public void documentAboutToBeChanged(DocumentEvent event) {
259             }
260             public void documentChanged(DocumentEvent event) {
261                 checkValues();
262             }
263         });
264         
265         if (fDetailFormatter.getTypeName().length() > 0) {
266             fSnippetViewer.getControl().setFocus();
267         }
268     }
269     
270     /**
271      * Check the field values and display a message in the status if needed.
272      */

273     private void checkValues() {
274         StatusInfo status= new StatusInfo();
275         String JavaDoc typeName= fTypeNameText.getText().trim();
276         if (typeName.length() == 0) {
277             status.setError(DebugUIMessages.DetailFormatterDialog_Qualified_type_name_must_not_be_empty__3);
278         } else if (fDefinedTypes != null && fDefinedTypes.contains(typeName)) {
279             status.setError(DebugUIMessages.DetailFormatterDialog_A_detail_formatter_is_already_defined_for_this_type_2);
280         } else if (fSnippetViewer.getDocument().get().trim().length() == 0) {
281             status.setError(DebugUIMessages.DetailFormatterDialog_Associated_code_must_not_be_empty_3);
282         } else if (fType == null && fTypeSearched) {
283             status.setWarning(DebugUIMessages.No_type_with_the_given_name_found_in_the_workspace__1);
284         }
285         updateStatus(status);
286     }
287
288     /**
289      * @see org.eclipse.jface.dialogs.Dialog#okPressed()
290      */

291     protected void okPressed() {
292         fDetailFormatter.setEnabled(fCheckBox.getSelection());
293         fDetailFormatter.setTypeName(fTypeNameText.getText().trim());
294         fDetailFormatter.setSnippet(fSnippetViewer.getDocument().get());
295         
296         super.okPressed();
297     }
298     
299     /**
300      * Open the 'select type' dialog, and set the user choice into the formatter.
301      */

302     private void selectType() {
303         Shell shell= getShell();
304         SelectionDialog dialog= null;
305         try {
306             dialog= JavaUI.createTypeDialog(shell, PlatformUI.getWorkbench().getProgressService(),
307                 SearchEngine.createWorkspaceScope(), IJavaElementSearchConstants.CONSIDER_ALL_TYPES, false, fTypeNameText.getText());
308         } catch (JavaModelException jme) {
309             String JavaDoc title= DebugUIMessages.DetailFormatterDialog_Select_type_6;
310             String JavaDoc message= DebugUIMessages.DetailFormatterDialog_Could_not_open_type_selection_dialog_for_detail_formatters_7;
311             ExceptionHandler.handle(jme, title, message);
312             return;
313         }
314     
315         dialog.setTitle(DebugUIMessages.DetailFormatterDialog_Select_type_8);
316         dialog.setMessage(DebugUIMessages.DetailFormatterDialog_Select_a_type_to_format_when_displaying_its_detail_9);
317         if (dialog.open() == IDialogConstants.CANCEL_ID) {
318             return;
319         }
320         
321         Object JavaDoc[] types= dialog.getResult();
322         if (types != null && types.length > 0) {
323             fType = (IType)types[0];
324             fTypeNameText.setText(fType.getFullyQualifiedName());
325             fTypeSearched = true;
326         }
327     }
328     
329     /**
330      * Use the Java search engine to find the type which corresponds
331      * to the given name.
332      */

333     private void findCorrespondingType() {
334         if (fTypeSearched) {
335             return;
336         }
337         fType= null;
338         fTypeSearched= true;
339         final String JavaDoc pattern= fTypeNameText.getText().trim().replace('$', '.');
340         if (pattern == null || "".equals(pattern)) { //$NON-NLS-1$
341
return;
342         }
343         final IProgressMonitor monitor = new NullProgressMonitor();
344         final SearchRequestor collector = new SearchRequestor() {
345             private boolean fFirst= true;
346             
347             public void endReporting() {
348                 checkValues();
349             }
350
351             public void acceptSearchMatch(SearchMatch match) throws CoreException {
352                 Object JavaDoc enclosingElement = match.getElement();
353                 if (!fFirst) {
354                     return;
355                 }
356                 fFirst= false;
357                 if (enclosingElement instanceof IType) {
358                     fType= (IType) enclosingElement;
359                 }
360                 // cancel once we have one match
361
monitor.setCanceled(true);
362             }
363         };
364         
365         SearchEngine engine= new SearchEngine(JavaCore.getWorkingCopies(null));
366         SearchPattern searchPattern = SearchPattern.createPattern(pattern, IJavaSearchConstants.TYPE, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
367         IJavaSearchScope scope= SearchEngine.createWorkspaceScope();
368         SearchParticipant[] participants = new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()};
369         try {
370             engine.search(searchPattern, participants, scope, collector, monitor);
371         } catch (CoreException e) {
372             JDIDebugUIPlugin.log(e);
373         } catch (OperationCanceledException e) {
374         }
375     }
376     
377     /**
378      * Return the type object which corresponds to the given name.
379      */

380     public IType getType() {
381         if (!fTypeSearched) {
382             findCorrespondingType();
383         }
384         return fType;
385     }
386
387     /* (non-Javadoc)
388      * @see org.eclipse.jface.window.Window#close()
389      */

390     public boolean close() {
391         IWorkbench workbench = PlatformUI.getWorkbench();
392         IHandlerService handlerService = (IHandlerService) workbench.getAdapter(IHandlerService.class);
393         handlerService.deactivateHandler(fHandlerActivation);
394         fSnippetViewer.dispose();
395         return super.close();
396     }
397
398 }
399
Popular Tags