blob: 83497afd5c04157ad70cfb39f1d4693080320fd6 [file] [log] [blame]
//------------------------------------------------------------------------------
// Copyright (c) 2005, 2006 IBM Corporation and others.
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// which accompanies this distribution, and is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// Contributors:
// IBM Corporation - initial implementation
//------------------------------------------------------------------------------
package org.eclipse.epf.richtext.actions;
import org.eclipse.epf.common.utils.XMLUtil;
import org.eclipse.epf.richtext.IRichText;
import org.eclipse.epf.richtext.RichTextCommand;
import org.eclipse.epf.richtext.RichTextImages;
import org.eclipse.epf.richtext.RichTextPlugin;
import org.eclipse.epf.richtext.RichTextResources;
import org.eclipse.epf.richtext.dialogs.FindReplaceDialog;
import org.eclipse.swt.widgets.Display;
/**
* Finds and replaces text in a Rich Text control.
*
* @author Kelvin Low
* @since 1.0
*/
public class FindReplaceAction extends RichTextAction {
/**
* Finds text sub action.
*/
public static final int FIND_TEXT = 1;
/**
* Replaces text sub action.
*/
public static final int REPLACE_TEXT = 2;
/**
* Replaces and finds text sub action.
*/
public static final int REPLACE_FIND_TEXT = 3;
/**
* Replaces all sub action.
*/
public static final int REPLACE_ALL_TEXT = 4;
/**
* Find match in a forward direction.
*/
public static final int FORWARD_MATCH = 1;
/**
* Find match in a backward direction.
*/
public static final int BACKWARD_MATCH = -1;
/**
* Whole word match.
*/
public static final int WHOLE_WORD_MATCH = 2;
/**
* Case sensitive match.
*/
public static final int CASE_SENSITIVE_MATCH = 4;
// Encoded single quote.
private static final String ENCODED_SINGLE_QUOTE = "%sq%"; //$NON-NLS-1$
private IRichText richText;
protected boolean foundMatch = false;
/**
* Creates a new instance.
*/
public FindReplaceAction() {
super();
setImage(RichTextImages.IMG_FIND_REPLACE);
setToolTipText(RichTextResources
.getString("RichText.findReplaceAction.toolTipText")); //$NON-NLS-1$
setEnabled(true);
}
/**
* Executes the action.
*
* @param richText
* A Rich Text control.
*/
public void execute(IRichText richText) {
this.richText = richText;
if (richText != null) {
try {
FindReplaceDialog dialog = new FindReplaceDialog(Display
.getCurrent().getActiveShell(), this);
dialog.open();
} catch (Exception e) {
RichTextPlugin.getDefault().getLogger().logError(e);
}
}
}
/**
* Returns true if a match is found.
*
* @return true if a match is found.
*/
public boolean getFoundMatch() {
return foundMatch;
}
/**
* Executes the action.
*
* @param subAction
* The sub action to execute.
* @param findText
* The find text.
* @param replaceText
* The replace text.
* @param matchDir
* The match direction. The value can either be
* <code>FIND_FORWARD</code> or <code>FIND_BACKWARD</code>.
* @param matchOptions
* The match options.
*
*/
public void run(int subAction, String findText, String replaceText,
int matchDir, int matchOptions) {
if (findText.indexOf("'") != -1) { //$NON-NLS-1$
findText = findText.replaceAll("'", ENCODED_SINGLE_QUOTE); //$NON-NLS-1$
}
if (replaceText.indexOf("'") != -1) { //$NON-NLS-1$
replaceText = replaceText.replaceAll("'", ENCODED_SINGLE_QUOTE); //$NON-NLS-1$
}
try {
foundMatch = false;
int status = 0;
switch (subAction) {
case FIND_TEXT:
status = richText.executeCommand(RichTextCommand.FIND_TEXT,
new String[] { findText,
"" + matchDir, "" + matchOptions }); //$NON-NLS-1$ //$NON-NLS-2$
break;
case REPLACE_TEXT:
status = richText.executeCommand(RichTextCommand.REPLACE_TEXT,
new String[] { replaceText,
"" + matchDir, "" + matchOptions }); //$NON-NLS-1$ //$NON-NLS-2$
break;
case REPLACE_FIND_TEXT:
richText.executeCommand(RichTextCommand.REPLACE_TEXT,
new String[] { replaceText,
"" + matchDir, "" + matchOptions }); //$NON-NLS-1$ //$NON-NLS-2$
status = richText.executeCommand(RichTextCommand.FIND_TEXT,
new String[] { findText,
"" + matchDir, "" + matchOptions }); //$NON-NLS-1$ //$NON-NLS-2$
break;
case REPLACE_ALL_TEXT:
richText.executeCommand(RichTextCommand.REPLACE_ALL_TEXT,
new String[] { escape(findText), escape(replaceText),
"" + matchOptions }); //$NON-NLS-1$
break;
}
if (status > 0)
foundMatch = true;
} catch (Exception e) {
RichTextPlugin.getDefault().getLogger().logError(e);
}
}
/**
* Escapes the given text.
*
* @param text
* Text to be escaped.
*/
private static String escape(String text) {
if (text == null || text.length() == 0)
return ""; //$NON-NLS-1$
StringBuffer sb = new StringBuffer();
int textSize = text.length();
for (int i = 0; i < textSize; i++) {
char ch = text.charAt(i);
switch (ch) {
case '<':
sb.append(XMLUtil.XML_LT);
break;
case '>':
sb.append(XMLUtil.XML_GT);
break;
case '&':
sb.append(XMLUtil.XML_AMP);
break;
default:
sb.append(ch);
break;
}
}
return sb.toString();
}
}