1 11 package org.eclipse.jdt.internal.ui.actions; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.eclipse.core.runtime.Assert; 17 import org.eclipse.core.runtime.CoreException; 18 19 import org.eclipse.swt.events.MenuAdapter; 20 import org.eclipse.swt.events.MenuEvent; 21 import org.eclipse.swt.graphics.Point; 22 import org.eclipse.swt.widgets.Control; 23 import org.eclipse.swt.widgets.Menu; 24 import org.eclipse.swt.widgets.MenuItem; 25 import org.eclipse.swt.widgets.Shell; 26 27 import org.eclipse.jface.action.Action; 28 import org.eclipse.jface.action.ActionContributionItem; 29 import org.eclipse.jface.action.IAction; 30 import org.eclipse.jface.action.IMenuManager; 31 import org.eclipse.jface.action.Separator; 32 import org.eclipse.jface.viewers.ISelection; 33 import org.eclipse.jface.viewers.ISelectionProvider; 34 35 import org.eclipse.jface.text.DocumentEvent; 36 import org.eclipse.jface.text.IDocument; 37 import org.eclipse.jface.text.IEditingSupport; 38 import org.eclipse.jface.text.IEditingSupportRegistry; 39 import org.eclipse.jface.text.IRegion; 40 import org.eclipse.jface.text.IRewriteTarget; 41 import org.eclipse.jface.text.ITextSelection; 42 import org.eclipse.jface.text.ITextViewer; 43 import org.eclipse.jface.text.ITextViewerExtension; 44 import org.eclipse.jface.text.contentassist.ICompletionProposal; 45 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension2; 46 47 import org.eclipse.ui.IPartListener; 48 import org.eclipse.ui.IPartService; 49 import org.eclipse.ui.IWorkbenchPart; 50 import org.eclipse.ui.IWorkbenchWindow; 51 import org.eclipse.ui.IWorkbenchWindowPulldownDelegate2; 52 import org.eclipse.ui.dialogs.PreferencesUtil; 53 54 import org.eclipse.jdt.core.ICompilationUnit; 55 56 import org.eclipse.jdt.ui.JavaUI; 57 import org.eclipse.jdt.ui.actions.IJavaEditorActionDefinitionIds; 58 import org.eclipse.jdt.ui.actions.SurroundWithTryCatchAction; 59 import org.eclipse.jdt.ui.text.java.IInvocationContext; 60 import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal; 61 62 import org.eclipse.jdt.internal.ui.JavaPlugin; 63 import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor; 64 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; 65 import org.eclipse.jdt.internal.ui.text.correction.AssistContext; 66 import org.eclipse.jdt.internal.ui.text.correction.QuickTemplateProcessor; 67 68 public class SurroundWithTemplateMenuAction implements IWorkbenchWindowPulldownDelegate2 { 69 70 public static final String SURROUND_WITH_QUICK_MENU_ACTION_ID= "org.eclipse.jdt.ui.edit.text.java.surround.with.quickMenu"; 73 private static final String JAVA_TEMPLATE_PREFERENCE_PAGE_ID= "org.eclipse.jdt.ui.preferences.JavaTemplatePreferencePage"; 75 private static final String TEMPLATE_GROUP= "templateGroup"; 77 private static final String CONFIG_GROUP= "configGroup"; 79 private static class ConfigureTemplatesAction extends Action { 80 81 public ConfigureTemplatesAction() { 82 super(ActionMessages.SurroundWithTemplateMenuAction_ConfigureTemplatesActionName); 83 } 84 85 88 public void run() { 89 PreferencesUtil.createPreferenceDialogOn(getShell(), JAVA_TEMPLATE_PREFERENCE_PAGE_ID, new String [] {JAVA_TEMPLATE_PREFERENCE_PAGE_ID}, null).open(); 90 } 91 92 private Shell getShell() { 93 return JavaPlugin.getActiveWorkbenchWindow().getShell(); 94 } 95 } 96 97 private static Action NONE_APPLICABLE_ACTION= new Action(ActionMessages.SurroundWithTemplateMenuAction_NoneApplicable) { 98 public void run() { 99 } 101 public boolean isEnabled() { 102 return false; 103 } 104 }; 105 106 private Menu fMenu; 107 private IPartService fPartService; 108 private IPartListener fPartListener= new IPartListener() { 109 110 public void partActivated(IWorkbenchPart part) { 111 } 112 113 public void partBroughtToTop(IWorkbenchPart part) { 114 } 115 116 public void partClosed(IWorkbenchPart part) { 117 } 118 119 public void partDeactivated(IWorkbenchPart part) { 120 disposeMenuItems(); 121 } 122 123 public void partOpened(IWorkbenchPart part) { 124 } 125 126 }; 127 128 protected void disposeMenuItems() { 129 if (fMenu == null || fMenu.isDisposed()) { 130 return; 131 } 132 MenuItem[] items = fMenu.getItems(); 133 for (int i=0; i < items.length; i++) { 134 MenuItem menuItem= items[i]; 135 if (!menuItem.isDisposed()) { 136 menuItem.dispose(); 137 } 138 } 139 } 140 141 144 public Menu getMenu(Menu parent) { 145 setMenu(new Menu(parent)); 146 fillMenu(fMenu); 147 initMenu(); 148 return fMenu; 149 } 150 151 154 public Menu getMenu(Control parent) { 155 setMenu(new Menu(parent)); 156 fillMenu(fMenu); 157 initMenu(); 158 return fMenu; 159 } 160 161 public static void fillMenu(IMenuManager menu, CompilationUnitEditor editor, SurroundWithTryCatchAction surroundWithTryCatchAction) { 162 IAction[] actions= getTemplateActions(editor); 163 surroundWithTryCatchAction.update(editor.getSelectionProvider().getSelection()); 164 165 if (actions == null && !surroundWithTryCatchAction.isEnabled()) { 166 menu.add(NONE_APPLICABLE_ACTION); 167 } else { 168 menu.add(surroundWithTryCatchAction); 169 menu.add(new Separator(TEMPLATE_GROUP)); 170 171 if (actions == null) { 172 menu.add(NONE_APPLICABLE_ACTION); 173 } else { 174 for (int i= 0; i < actions.length; i++) { 175 menu.add(actions[i]); 176 } 177 } 178 } 179 180 menu.add(new Separator(CONFIG_GROUP)); 181 menu.add(new ConfigureTemplatesAction()); 182 } 183 184 187 public void dispose() { 188 if (fPartService != null) { 189 fPartService.removePartListener(fPartListener); 190 fPartService= null; 191 } 192 setMenu(null); 193 } 194 195 198 public void init(IWorkbenchWindow window) { 199 if (fPartService != null) { 200 fPartService.removePartListener(fPartListener); 201 fPartService= null; 202 } 203 204 if (window != null) { 205 IPartService partService= window.getPartService(); 206 if (partService != null) { 207 fPartService= partService; 208 partService.addPartListener(fPartListener); 209 } 210 } 211 } 212 213 216 public void run(IAction action) { 217 IWorkbenchPart activePart= JavaPlugin.getActivePage().getActivePart(); 218 if (!(activePart instanceof CompilationUnitEditor)) 219 return; 220 221 final CompilationUnitEditor editor= (CompilationUnitEditor)activePart; 222 223 (new JDTQuickMenuAction(editor, SURROUND_WITH_QUICK_MENU_ACTION_ID) { 224 protected void fillMenu(IMenuManager menu) { 225 SurroundWithTryCatchAction surroundWithTryCatch= createSurroundWithTryCatchAction(editor); 226 SurroundWithTemplateMenuAction.fillMenu(menu, editor, surroundWithTryCatch); 227 } 228 }).run(); 229 } 230 231 234 public void selectionChanged(IAction action, ISelection selection) { 235 } 237 238 241 protected void fillMenu(Menu menu) { 242 243 IWorkbenchPart activePart= JavaPlugin.getActivePage().getActivePart(); 244 if (!(activePart instanceof CompilationUnitEditor)) { 245 ActionContributionItem item= new ActionContributionItem(NONE_APPLICABLE_ACTION); 246 item.fill(menu, -1); 247 return; 248 } 249 250 CompilationUnitEditor editor= (CompilationUnitEditor)activePart; 251 252 IAction[] actions= getTemplateActions(editor); 253 254 SurroundWithTryCatchAction surroundAction= createSurroundWithTryCatchAction(editor); 255 ActionContributionItem surroundItem= new ActionContributionItem(surroundAction); 256 surroundItem.fill(menu, -1); 257 258 Separator templateGroup= new Separator(TEMPLATE_GROUP); 259 templateGroup.fill(menu, -1); 260 261 if (actions == null || actions.length == 0) { 262 ActionContributionItem item= new ActionContributionItem(NONE_APPLICABLE_ACTION); 263 item.fill(menu, -1); 264 } else { 265 for (int i= 0; i < actions.length; i++) { 266 ActionContributionItem item= new ActionContributionItem(actions[i]); 267 item.fill(menu, -1); 268 } 269 } 270 271 Separator configGroup= new Separator(CONFIG_GROUP); 272 configGroup.fill(menu, -1); 273 274 ActionContributionItem configAction= new ActionContributionItem(new ConfigureTemplatesAction()); 275 configAction.fill(menu, -1); 276 277 } 278 279 private static SurroundWithTryCatchAction createSurroundWithTryCatchAction(CompilationUnitEditor editor) { 280 SurroundWithTryCatchAction result= new SurroundWithTryCatchAction(editor); 281 result.setText(ActionMessages.SurroundWithTemplateMenuAction_SurroundWithTryCatchActionName); 282 result.setActionDefinitionId(IJavaEditorActionDefinitionIds.SURROUND_WITH_TRY_CATCH); 283 editor.setAction("SurroundWithTryCatch", result); return result; 285 } 286 287 protected void initMenu() { 288 fMenu.addMenuListener(new MenuAdapter() { 289 public void menuShown(MenuEvent e) { 290 Menu m = (Menu)e.widget; 291 MenuItem[] items = m.getItems(); 292 for (int i=0; i < items.length; i++) { 293 items[i].dispose(); 294 } 295 fillMenu(m); 296 } 297 }); 298 } 299 300 private void setMenu(Menu menu) { 301 if (fMenu != null) { 302 fMenu.dispose(); 303 } 304 fMenu = menu; 305 } 306 307 private static IAction[] getTemplateActions(JavaEditor editor) { 308 ISelectionProvider selectionProvider= editor.getSelectionProvider(); 309 if (selectionProvider == null) 310 return null; 311 312 ISelection selection= selectionProvider.getSelection(); 313 if (!(selection instanceof ITextSelection)) 314 return null; 315 316 ITextSelection textSelection= (ITextSelection)selection; 317 if (textSelection.getLength() == 0) 318 return null; 319 320 ICompilationUnit cu= JavaUI.getWorkingCopyManager().getWorkingCopy(editor.getEditorInput()); 321 if (cu == null) 322 return null; 323 324 QuickTemplateProcessor quickTemplateProcessor= new QuickTemplateProcessor(); 325 IInvocationContext context= new AssistContext(cu, textSelection.getOffset(), textSelection.getLength()); 326 327 try { 328 IJavaCompletionProposal[] proposals= quickTemplateProcessor.getAssists(context, null); 329 if (proposals == null || proposals.length == 0) 330 return null; 331 332 return getActionsFromProposals(proposals, context.getSelectionOffset(), editor.getViewer()); 333 } catch (CoreException e) { 334 JavaPlugin.log(e); 335 } 336 return null; 337 } 338 339 private static IAction[] getActionsFromProposals(IJavaCompletionProposal[] proposals, final int offset, final ITextViewer viewer) { 340 List result= new ArrayList (); 341 342 for (int i= 0, j= 1; i < proposals.length; i++) { 343 if (proposals[i] instanceof ICompletionProposalExtension2) { 344 final IJavaCompletionProposal proposal= proposals[i]; 345 346 StringBuffer actionName= new StringBuffer (); 347 if (j<10) { 348 actionName.append('&').append(j).append(' '); 349 } 350 actionName.append(proposals[i].getDisplayString()); 351 352 Action action= new Action(actionName.toString()) { 353 356 public void run() { 357 applyProposal(proposal, viewer, (char)0, 0, offset); 358 } 359 }; 360 361 result.add(action); 362 j++; 363 } 364 } 365 if (result.size() == 0) 366 return null; 367 368 return (IAction[])result.toArray(new IAction[result.size()]); 369 } 370 371 private static void applyProposal(ICompletionProposal proposal, ITextViewer viewer, char trigger, int stateMask, final int offset) { 372 Assert.isTrue(proposal instanceof ICompletionProposalExtension2); 373 374 IRewriteTarget target= null; 375 IEditingSupportRegistry registry= null; 376 IEditingSupport helper= new IEditingSupport() { 377 378 public boolean isOriginator(DocumentEvent event, IRegion focus) { 379 return focus.getOffset() <= offset && focus.getOffset() + focus.getLength() >= offset; 380 } 381 382 public boolean ownsFocusShell() { 383 return false; 384 } 385 386 }; 387 388 try { 389 IDocument document= viewer.getDocument(); 390 391 if (viewer instanceof ITextViewerExtension) { 392 ITextViewerExtension extension= (ITextViewerExtension) viewer; 393 target= extension.getRewriteTarget(); 394 } 395 396 if (target != null) 397 target.beginCompoundChange(); 398 399 if (viewer instanceof IEditingSupportRegistry) { 400 registry= (IEditingSupportRegistry) viewer; 401 registry.register(helper); 402 } 403 404 ((ICompletionProposalExtension2)proposal).apply(viewer, trigger, stateMask, offset); 405 406 Point selection= proposal.getSelection(document); 407 if (selection != null) { 408 viewer.setSelectedRange(selection.x, selection.y); 409 viewer.revealRange(selection.x, selection.y); 410 } 411 } finally { 412 if (target != null) 413 target.endCompoundChange(); 414 415 if (registry != null) 416 registry.unregister(helper); 417 } 418 } 419 } 420 | Popular Tags |