1 11 package org.eclipse.jdt.internal.debug.ui; 12 13 14 import java.util.List ; 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 75 public class DetailFormatterDialog extends StatusDialog implements ITypeProvider { 76 77 80 private DetailFormatter fDetailFormatter; 81 82 private Text fTypeNameText; 84 private JDISourceViewer fSnippetViewer; 85 private Button fCheckBox; 86 87 91 private boolean fTypeSearched; 92 93 96 private boolean fEditTypeName; 97 98 104 private IType fType; 105 106 109 private List fDefinedTypes; 110 111 114 private IHandlerActivation fHandlerActivation; 115 116 123 124 125 133 public DetailFormatterDialog(Shell parent, DetailFormatter detailFormatter, List definedTypes, boolean editDialog) { 134 this(parent, detailFormatter, definedTypes, true, editDialog); 135 } 136 137 146 public DetailFormatterDialog(Shell parent, DetailFormatter detailFormatter, List 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 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 labelText = null; 198 IBindingService bindingService = (IBindingService) workbench.getAdapter(IBindingService.class); 199 String binding = bindingService.getBestActiveBindingFormattedFor("org.eclipse.ui.edit.text.contentAssist.proposals"); if (binding != null) { 201 labelText = MessageFormat.format(DebugUIMessages.DetailFormatterDialog_17, new String [] { 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 IHandler handler = new AbstractHandler() { 215 public Object 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 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 273 private void checkValues() { 274 StatusInfo status= new StatusInfo(); 275 String 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 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 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 title= DebugUIMessages.DetailFormatterDialog_Select_type_6; 310 String 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 [] 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 333 private void findCorrespondingType() { 334 if (fTypeSearched) { 335 return; 336 } 337 fType= null; 338 fTypeSearched= true; 339 final String pattern= fTypeNameText.getText().trim().replace('$', '.'); 340 if (pattern == null || "".equals(pattern)) { 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 enclosingElement = match.getElement(); 353 if (!fFirst) { 354 return; 355 } 356 fFirst= false; 357 if (enclosingElement instanceof IType) { 358 fType= (IType) enclosingElement; 359 } 360 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 380 public IType getType() { 381 if (!fTypeSearched) { 382 findCorrespondingType(); 383 } 384 return fType; 385 } 386 387 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 |