Skip to content

Commit

Permalink
Better handling of Show Find Bar binding.
Browse files Browse the repository at this point in the history
  • Loading branch information
sandipchitale committed Nov 24, 2009
1 parent 69bb118 commit a88e352
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 89 deletions.
17 changes: 1 addition & 16 deletions EditorFindBar/plugin.xml
Expand Up @@ -75,23 +75,8 @@
<extension
point="org.eclipse.ui.handlers">
<handler
class="editorfindbar.impl.ShowFindReplaceDialogHandler"
class="editorfindbar.impl.ShowFindBarHandler"
commandId="org.eclipse.ui.edit.find.bar">
<enabledWhen>
<with
variable="activeEditor">
<not>
<or>
<instanceof
value="editorfindbar.api.IFindBarDecorated">
</instanceof>
<adapt
type="editorfindbar.api.IFindBarDecorated">
</adapt>
</or>
</not>
</with>
</enabledWhen>
</handler>
</extension>
</plugin>
4 changes: 3 additions & 1 deletion EditorFindBar/src/editorfindbar/api/IFindBarDecorated.java
Expand Up @@ -7,4 +7,6 @@
* @author schitale
*
*/
public interface IFindBarDecorated {}
public interface IFindBarDecorated {
IFindBarDecorator getFindBarDecorator();
}
12 changes: 2 additions & 10 deletions EditorFindBar/src/editorfindbar/api/IFindBarDecorator.java
Expand Up @@ -37,15 +37,7 @@ public interface IFindBarDecorator {
void createFindBar(ISourceViewer sourceViewer);

/**
* This hooks up the actions and command handlers. This should be called from:
*
* <pre>
* protected void createActions() {
* super.createActions();
* getfindBarDecorator().createActions();
* }
* </pre>
*
* Shows Find Bar.
*/
void createActions();
void showFindBar();
}
80 changes: 27 additions & 53 deletions EditorFindBar/src/editorfindbar/impl/FindBarDecorator.java
Expand Up @@ -8,14 +8,11 @@
import java.util.regex.PatternSyntaxException;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.Command;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.NotEnabledException;
import org.eclipse.core.commands.NotHandledException;
import org.eclipse.core.commands.ParameterizedCommand;
import org.eclipse.core.commands.common.NotDefinedException;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.jface.text.IFindReplaceTarget;
import org.eclipse.jface.text.IFindReplaceTargetExtension;
Expand Down Expand Up @@ -47,7 +44,6 @@
import org.eclipse.ui.IWorkbenchCommandConstants;
import org.eclipse.ui.IWorkbenchPartSite;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.commands.ICommandService;
import org.eclipse.ui.contexts.IContextActivation;
import org.eclipse.ui.contexts.IContextService;
import org.eclipse.ui.handlers.IHandlerService;
Expand Down Expand Up @@ -248,14 +244,7 @@ public void widgetSelected(SelectionEvent e) {
public void widgetDefaultSelected(SelectionEvent e) {}
});

}

public void createActions() {
if (textEditor instanceof IFindBarDecorated || (textEditor.getAdapter(IFindBarDecorated.class) instanceof IFindBarDecorated)) {
textEditor.setAction(SHOW_FIND_BAR_COMMAND_ID, new ShowFindBarAction());

textEditor.setActionActivationCode(SHOW_FIND_BAR_COMMAND_ID, (char) 6, SWT.DEFAULT, SWT.MOD1);

// Activate handlers
IHandlerService handlerService = (IHandlerService) textEditor.getSite().getService(IHandlerService.class);
handlerService.activateHandler("org.eclipse.ui.edit.find.bar.hide", new HideFindBarHandler()); //$NON-NLS-1$
Expand All @@ -264,6 +253,32 @@ public void createActions() {
}
}

public void showFindBar() {
boolean wasExcluded = findBarGridData.exclude;
if (findBarGridData.exclude) {
findBarGridData.exclude = false;
composite.layout();
}
ISelection selection = sourceViewer.getSelectionProvider()
.getSelection();
if (selection instanceof ITextSelection) {
ITextSelection textSelection = (ITextSelection) selection;
String text = textSelection.getText();
if (text.indexOf("\n") == -1 && text.indexOf("\r") == -1) { //$NON-NLS-1$ //$NON-NLS-2$
setFindText(text, !wasExcluded);
}
}
if (wasExcluded) {
combo.addModifyListener(modifyListener);
}
adjustEnablement();
boolean comboHasFocus = combo.isFocusControl();
if (!comboHasFocus) {
combo.setFocus();
incrementalOffset = -1;
}
}

private Combo combo;
private Button caseSensitive;
private Button wholeWord;
Expand All @@ -280,8 +295,6 @@ public void createActions() {

private IContextActivation findBarContextActivation;

private static String SHOW_FIND_BAR_COMMAND_ID = "org.eclipse.ui.edit.find.bar"; //$NON-NLS-1$

private static final String EMPTY = ""; //$NON-NLS-1$

private ModifyListener modifyListener = new ModifyListener() {
Expand Down Expand Up @@ -311,16 +324,6 @@ public void modifyText(ModifyEvent e) {
showCountTotal();
}
};

private class ShowFindBarAction extends Action {
private ShowFindBarAction() {
setActionDefinitionId(SHOW_FIND_BAR_COMMAND_ID);
}

public void run() {
showFindBar();
}
}

private class HideFindBarHandler extends AbstractHandler {
public Object execute(ExecutionEvent event) throws ExecutionException {
Expand Down Expand Up @@ -381,32 +384,6 @@ private void hideFindBar() {
textEditor.setFocus();
}

private void showFindBar() {
boolean wasExcluded = findBarGridData.exclude;
if (findBarGridData.exclude) {
findBarGridData.exclude = false;
composite.layout();
}
ISelection selection = sourceViewer.getSelectionProvider()
.getSelection();
if (selection instanceof ITextSelection) {
ITextSelection textSelection = (ITextSelection) selection;
String text = textSelection.getText();
if (text.indexOf("\n") == -1 && text.indexOf("\r") == -1) { //$NON-NLS-1$ //$NON-NLS-2$
setFindText(text, !wasExcluded);
}
}
if (wasExcluded) {
combo.addModifyListener(modifyListener);
}
adjustEnablement();
boolean comboHasFocus = combo.isFocusControl();
if (!comboHasFocus) {
combo.setFocus();
incrementalOffset = -1;
}
}

private void findPrevious() {
find(false);
setFindText(combo.getText());
Expand Down Expand Up @@ -564,13 +541,10 @@ private void showCountTotal() {

private void showFindReplaceDialog() {
IWorkbenchPartSite site = textEditor.getSite();
ICommandService commandService = (ICommandService) site.getService(ICommandService.class);
Command findReplacecommand = commandService.getCommand(IWorkbenchCommandConstants.EDIT_FIND_AND_REPLACE);
IHandlerService handlerService = (IHandlerService) site.getService(IHandlerService.class);
if (handlerService != null) {
try {
handlerService.executeCommand(
new ParameterizedCommand(findReplacecommand, null), null);
handlerService.executeCommand(IWorkbenchCommandConstants.EDIT_FIND_AND_REPLACE, null);
} catch (ExecutionException e1) {
} catch (NotDefinedException e1) {
} catch (NotEnabledException e1) {
Expand Down
Expand Up @@ -11,27 +11,39 @@
import org.eclipse.ui.handlers.IHandlerService;
import org.eclipse.ui.texteditor.ITextEditor;

import editorfindbar.api.IFindBarDecorated;

/**
* This takes care of the case when the active textEdior is not a instance of <code>IFindBarDecorated</code> or
* does not adapt to <code>IFindBarDecorated</code>.
* This takes care of the case when the active textEdior is an instance of <code>IFindBarDecorated</code> or
* does adapt to <code>IFindBarDecorated</code>.
*
* @author schitale
*
*/
public class ShowFindReplaceDialogHandler extends AbstractHandler {
public class ShowFindBarHandler extends AbstractHandler {
public Object execute(ExecutionEvent event) throws ExecutionException {
Object applicationContext = event.getApplicationContext();
if (applicationContext instanceof IEvaluationContext) {
IEvaluationContext evaluationContext = (IEvaluationContext) applicationContext;
Object variable = evaluationContext.getVariable(ISources.ACTIVE_EDITOR_NAME);
if (variable instanceof ITextEditor) {
ITextEditor textEditor = (ITextEditor) variable;
IHandlerService handlerService = (IHandlerService) textEditor.getSite().getService(IHandlerService.class);
try {
handlerService.executeCommand(IWorkbenchCommandConstants.EDIT_FIND_AND_REPLACE, null);
} catch (NotDefinedException e) {
} catch (NotEnabledException e) {
} catch (NotHandledException e) {
IFindBarDecorated findBarDecorated = null;
if (textEditor instanceof IFindBarDecorated) {
findBarDecorated = (IFindBarDecorated) textEditor;
} else {
findBarDecorated = (IFindBarDecorated) textEditor.getAdapter(IFindBarDecorated.class);
}
if (findBarDecorated != null) {
findBarDecorated.getFindBarDecorator().showFindBar();
} else {
IHandlerService handlerService = (IHandlerService) textEditor.getSite().getService(IHandlerService.class);
try {
handlerService.executeCommand(IWorkbenchCommandConstants.EDIT_FIND_AND_REPLACE, null);
} catch (NotDefinedException e) {
} catch (NotEnabledException e) {
} catch (NotHandledException e) {
}
}
}
}
Expand Down

0 comments on commit a88e352

Please sign in to comment.