nitind | 102e60a | 2005-09-15 12:42:42 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright (c) 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 API and implementation |
| 10 | * |
| 11 | *******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.wst.sse.ui.internal.search; |
| 14 | |
| 15 | import java.util.Iterator; |
| 16 | import java.util.List; |
| 17 | |
| 18 | import org.eclipse.core.resources.IFile; |
| 19 | import org.eclipse.jface.action.IAction; |
| 20 | import org.eclipse.jface.text.BadLocationException; |
| 21 | import org.eclipse.jface.text.IDocument; |
| 22 | import org.eclipse.jface.text.ITextSelection; |
| 23 | import org.eclipse.jface.text.ITypedRegion; |
| 24 | import org.eclipse.jface.viewers.ISelection; |
| 25 | import org.eclipse.swt.widgets.Event; |
| 26 | import org.eclipse.ui.IActionDelegate2; |
| 27 | import org.eclipse.ui.IEditorActionDelegate; |
| 28 | import org.eclipse.ui.IEditorPart; |
| 29 | import org.eclipse.ui.IFileEditorInput; |
| 30 | import org.eclipse.ui.IViewActionDelegate; |
| 31 | import org.eclipse.ui.IViewPart; |
| 32 | import org.eclipse.ui.texteditor.ITextEditor; |
| 33 | import org.eclipse.wst.sse.ui.internal.SSEUIMessages; |
| 34 | import org.eclipse.wst.sse.ui.internal.util.PlatformStatusLineUtil; |
| 35 | |
| 36 | /** |
| 37 | * Performs the appropriate FindOccurrencesProcessor action call based on |
| 38 | * selection. Clients can add processors for different partitions via |
| 39 | * <code>getProcessors</code> |
| 40 | * |
| 41 | */ |
| 42 | abstract public class FindOccurrencesActionDelegate implements IEditorActionDelegate, IActionDelegate2, IViewActionDelegate { |
| 43 | private IEditorPart fEditor; |
| 44 | |
| 45 | public void setActiveEditor(IAction action, IEditorPart targetEditor) { |
| 46 | fEditor = targetEditor; |
| 47 | } |
| 48 | |
| 49 | public void dispose() { |
| 50 | // nulling out just in case |
| 51 | fEditor = null; |
| 52 | } |
| 53 | |
| 54 | public void init(IAction action) { |
| 55 | if (action != null) { |
| 56 | action.setText(SSEUIMessages.FindOccurrences_label); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | public void runWithEvent(IAction action, Event event) { |
| 61 | run(action); |
| 62 | } |
| 63 | |
| 64 | public void run(IAction action) { |
| 65 | boolean okay = false; |
| 66 | if (fEditor instanceof ITextEditor) { |
| 67 | ITextEditor textEditor = (ITextEditor) fEditor; |
| 68 | IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput()); |
| 69 | if (document != null) { |
| 70 | ITextSelection textSelection = getTextSelection(textEditor); |
| 71 | FindOccurrencesProcessor findOccurrenceProcessor = getProcessorForCurrentSelection(document, textSelection); |
| 72 | if (findOccurrenceProcessor != null) { |
| 73 | if (textEditor.getEditorInput() instanceof IFileEditorInput) { |
| 74 | IFile file = ((IFileEditorInput) textEditor.getEditorInput()).getFile(); |
| 75 | okay = findOccurrenceProcessor.findOccurrences(document, textSelection, file); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | if (okay) { |
| 81 | // clear status message |
| 82 | PlatformStatusLineUtil.clearStatusLine(); |
| 83 | } |
| 84 | else { |
| 85 | String errorMessage = SSEUIMessages.FindOccurrencesActionProvider_0; //$NON-NLS-1$ |
| 86 | PlatformStatusLineUtil.displayErrorMessage(errorMessage); |
| 87 | PlatformStatusLineUtil.addOneTimeClearListener(); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | public void init(IViewPart view) { |
| 92 | // do nothing |
| 93 | } |
| 94 | |
| 95 | public void selectionChanged(IAction action, ISelection selection) { |
| 96 | // clear status message |
| 97 | PlatformStatusLineUtil.clearStatusLine(); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Get the appropriate find occurrences processor |
| 102 | * |
| 103 | * @param document - |
| 104 | * assumes not null |
| 105 | * @param textSelection |
| 106 | * @return |
| 107 | */ |
| 108 | private FindOccurrencesProcessor getProcessorForCurrentSelection(IDocument document, ITextSelection textSelection) { |
| 109 | // check if we have an action that's enabled on the current partition |
| 110 | ITypedRegion tr = getPartition(document, textSelection); |
| 111 | String partition = tr != null ? tr.getType() : ""; //$NON-NLS-1$ |
| 112 | |
| 113 | Iterator it = getProcessors().iterator(); |
| 114 | FindOccurrencesProcessor action = null; |
| 115 | while (it.hasNext()) { |
| 116 | action = (FindOccurrencesProcessor) it.next(); |
| 117 | // we just choose the first action that can handle the partition |
| 118 | if (action.enabledForParitition(partition)) |
| 119 | return action; |
| 120 | } |
| 121 | return null; |
| 122 | } |
| 123 | |
| 124 | private ITypedRegion getPartition(IDocument document, ITextSelection textSelection) { |
| 125 | ITypedRegion region = null; |
| 126 | if (textSelection != null) { |
| 127 | try { |
| 128 | region = document.getPartition(textSelection.getOffset()); |
| 129 | } |
| 130 | catch (BadLocationException e) { |
| 131 | region = null; |
| 132 | } |
| 133 | } |
| 134 | return region; |
| 135 | } |
| 136 | |
| 137 | private ITextSelection getTextSelection(ITextEditor textEditor) { |
| 138 | ITextSelection textSelection = null; |
| 139 | ISelection selection = textEditor.getSelectionProvider().getSelection(); |
| 140 | if (selection instanceof ITextSelection && !selection.isEmpty()) { |
| 141 | textSelection = (ITextSelection) selection; |
| 142 | } |
| 143 | return textSelection; |
| 144 | } |
| 145 | |
| 146 | abstract protected List getProcessors(); |
| 147 | } |