/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jkiss.dbeaver.ui.controls;
import org.eclipse.jface.text.IFindReplaceTarget;
import org.eclipse.jface.text.IFindReplaceTargetExtension;
import org.eclipse.jface.text.IFindReplaceTargetExtension3;
import org.eclipse.jface.text.IRegion;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.jkiss.dbeaver.Log;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
/**
* Find/Replace target for StyledText
*/
public class StyledTextFindReplaceTarget implements IFindReplaceTarget, IFindReplaceTargetExtension, IFindReplaceTargetExtension3 {
private static final Log log = Log.getLog(StyledTextFindReplaceTarget.class);
private final StyledText text;
private Pattern searchPattern;
private Color scopeHighlightColor;
private boolean replaceAll;
public StyledTextFindReplaceTarget(StyledText text) {
this.text = text;
}
@Override
public boolean canPerformFind()
{
return true;
}
@Override
public int findAndSelect(int widgetOffset, String findString, boolean searchForward, boolean caseSensitive, boolean wholeWord)
{
return findAndSelect(widgetOffset, findString, searchForward, caseSensitive, wholeWord, false);
}
@Override
public Point getSelection()
{
return text.getSelection();
}
@Override
public String getSelectionText()
{
return text.getSelectionText();
}
@Override
public boolean isEditable()
{
return text.getEditable();
}
@Override
public void replaceSelection(String text)
{
replaceSelection(text, false);
}
@Override
public void beginSession()
{
}
@Override
public void endSession()
{
}
@Override
public IRegion getScope()
{
return null;
}
@Override
public void setScope(IRegion scope)
{
}
@Override
public Point getLineSelection()
{
return getSelection();
}
@Override
public void setSelection(int offset, int length)
{
text.setSelection(offset, length);
}
@Override
public void setScopeHighlightColor(Color color)
{
this.scopeHighlightColor = color;
}
@Override
public void setReplaceAllMode(boolean replaceAll)
{
this.replaceAll = replaceAll;
}
@Override
public int findAndSelect(int offset, String findString, boolean searchForward, boolean caseSensitive, boolean wholeWord, boolean regExSearch)
{
searchPattern = null;
Pattern findPattern;
if (regExSearch) {
try {
findPattern = Pattern.compile(findString, caseSensitive ? 0 : Pattern.CASE_INSENSITIVE);
} catch (PatternSyntaxException e) {
log.warn("Bad regex pattern: " + findString);
return -1;
}
} else {
findPattern = Pattern.compile(Pattern.quote(findString), caseSensitive ? 0 : Pattern.CASE_INSENSITIVE);
}
if (offset == -1) {
offset = 0;
}
int textLength = text.getCharCount();
if (textLength <= 0 || offset >= textLength) {
return -1;
}
String searchIn = text.getText();
Matcher matcher = findPattern.matcher(searchIn);
if (matcher.find(offset)) {
text.setSelection(matcher.start(), matcher.end());
return matcher.start();
}
return -1;
}
@Override
public void replaceSelection(String str, boolean regExReplace)
{
/*
GridPos selection = (GridPos) owner.getSelection().getFirstElement();
if (selection == null) {
return;
}
GridCell cell = owner.getSpreadsheet().posToCell(selection);
if (cell == null) {
return;
}
String oldValue = owner.getSpreadsheet().getContentProvider().getCellText(cell.col, cell.row);
String newValue = str;
if (searchPattern != null) {
newValue = searchPattern.matcher(oldValue).replaceAll(newValue);
}
boolean recordMode = owner.getController().isRecordMode();
final DBDAttributeBinding attr = (DBDAttributeBinding)(recordMode ? cell.row : cell.col);
final ResultSetRow row = (ResultSetRow)(recordMode ? cell.col : cell.row);
owner.getController().getModel().updateCellValue(attr, row, newValue);
owner.getController().updatePanelsContent();
*/
}
}