use of org.eclipse.swt.events.VerifyEvent in project archi by archimatetool.
the class LabelDirectEditManager method initCellEditor.
/**
* @see org.eclipse.gef.tools.DirectEditManager#initCellEditor()
*/
@Override
protected void initCellEditor() {
super.initCellEditor();
// Single text control strips CRLFs
setNormalised();
/**
* Changes the size of the editor control to reflect the changed text
*/
verifyListener = new VerifyListener() {
@Override
public void verifyText(VerifyEvent event) {
String oldText = getTextControl().getText();
String leftText = oldText.substring(0, event.start);
String rightText = oldText.substring(event.end, oldText.length());
GC gc = new GC(getTextControl());
Point size = gc.textExtent(leftText + event.text + rightText);
gc.dispose();
if (size.x != 0) {
size = getTextControl().computeSize(size.x, SWT.DEFAULT);
} else {
// just make it square
size.x = size.y;
}
getCellEditor().getControl().setSize(size.x, size.y);
}
};
getTextControl().addVerifyListener(verifyListener);
// set the initial value of the text
getCellEditor().setValue(initialText);
IFigure figure = (getEditPart()).getFigure();
getTextControl().setFont(figure.getFont());
}
use of org.eclipse.swt.events.VerifyEvent in project archi by archimatetool.
the class StyledTextControl method handleVerifyKey.
private void handleVerifyKey(VerifyEvent event) {
// On Ctrl/Command + Enter we send out a SWT.DefaultSelection event instead
if (((event.stateMask & SWT.MODIFIER_MASK) == SWT.MOD1 || (event.stateMask & SWT.MODIFIER_MASK) == SWT.CTRL) && (event.keyCode == SWT.CR || event.keyCode == SWT.KEYPAD_CR)) {
// Consume the key press
event.doit = false;
// Fire a SWT.DefaultSelection event
Event e = new Event();
e.type = SWT.DefaultSelection;
e.widget = fStyledText;
e.display = fStyledText.getDisplay();
e.time = (int) System.currentTimeMillis();
fStyledText.notifyListeners(SWT.DefaultSelection, e);
}
}
use of org.eclipse.swt.events.VerifyEvent in project translationstudio8 by heartsome.
the class SegmentViewer method initListeners.
private void initListeners() {
StyledText styledText = getTextWidget();
// 去掉默认的复制、粘贴键绑定,以实现在复制、粘贴前对标记的处理
styledText.setKeyBinding('V' | SWT.MOD1, SWT.NULL);
styledText.setKeyBinding(SWT.INSERT | SWT.MOD2, SWT.NULL);
styledText.setKeyBinding('C' | SWT.MOD1, SWT.NULL);
styledText.setKeyBinding(SWT.INSERT | SWT.MOD1, SWT.NULL);
getDocument().addDocumentListener(new IDocumentListener() {
public void documentChanged(DocumentEvent e) {
}
public void documentAboutToBeChanged(DocumentEvent event) {
String method = belongToUndoOrRedo();
if (event.getLength() > 0 && method != null) {
try {
String text = event.getDocument().get(event.getOffset(), event.getLength());
Matcher matcher = PATTERN.matcher(text);
List<InnerTag> cache = new ArrayList<InnerTag>();
while (matcher.find()) {
String placeHolder = matcher.group();
InnerTag innerTag = InnerTagUtil.getInnerTag(getInnerTagCacheList(), placeHolder);
if (innerTag != null && innerTag.isVisible()) {
innerTag.setVisible(false);
if ("undo".equals(method) && innerTag.getInnerTagBean().isWrongTag()) {
cache.add(innerTag);
}
}
}
for (InnerTag t : cache) {
innerTagCacheList.remove(t);
t.dispose();
}
} catch (BadLocationException e) {
e.printStackTrace();
}
}
}
/**
* 是否属于撤销或者重做的操作。
* @return ;
*/
private String belongToUndoOrRedo() {
IUndoManager undoManager = getUndoManager();
if (undoManager != null) {
StackTraceElement[] stackTraceElements = new Throwable().getStackTrace();
for (StackTraceElement stackTraceElement : stackTraceElements) {
if (undoManager.getClass().getName().equals(stackTraceElement.getClassName())) {
String methodName = stackTraceElement.getMethodName();
if ("undo".equals(methodName) || "redo".equals(methodName)) {
return methodName;
}
}
}
}
return null;
}
});
/**
* 处理在显示非打印隐藏字符的情况光标移动问题。兼容非打印字符替换符号
*/
styledText.addKeyListener(new KeyListener() {
public void keyReleased(KeyEvent e) {
if (!XliffEditorParameter.getInstance().isShowNonpirnttingCharacter()) {
return;
}
if (e.stateMask == SWT.NONE && (e.keyCode == SWT.ARROW_UP || e.keyCode == SWT.ARROW_DOWN)) {
StyledText styledText = (StyledText) e.widget;
int offset = styledText.getCaretOffset();
if (offset < 1 || offset >= styledText.getCharCount()) {
return;
}
char c = styledText.getText().charAt(offset);
char _c = styledText.getText().charAt(offset - 1);
if (c == '\n' && (_c == Constants.LINE_SEPARATOR_CHARACTER)) {
styledText.setCaretOffset(offset - 1);
}
}
}
public void keyPressed(KeyEvent e) {
if (!XliffEditorParameter.getInstance().isShowNonpirnttingCharacter()) {
return;
}
if (e.stateMask == SWT.NONE && (e.keyCode == SWT.ARROW_LEFT || e.keyCode == SWT.ARROW_RIGHT)) {
StyledText styledText = (StyledText) e.widget;
int offset = styledText.getCaretOffset();
if (offset < 1 || offset >= styledText.getCharCount()) {
return;
}
char c = styledText.getText().charAt(offset);
char _c = styledText.getText().charAt(offset - 1);
if ((c == '' && (_c == Constants.TAB_CHARACTER || _c == Constants.SPACE_CHARACTER)) || (c == '\n' && (_c == Constants.LINE_SEPARATOR_CHARACTER))) {
if (e.keyCode == SWT.ARROW_LEFT) {
styledText.setCaretOffset(offset - 1);
} else if (e.keyCode == SWT.ARROW_RIGHT) {
styledText.setCaretOffset(offset + 1);
}
}
} else if (e.stateMask == SWT.CTRL && (e.keyCode == SWT.ARROW_LEFT || e.keyCode == SWT.ARROW_RIGHT)) {
// 单独对 ctrl + right ,ctrl + left 换行的处理
StyledText styledText = (StyledText) e.widget;
int offset = styledText.getCaretOffset();
char c = styledText.getText().charAt(offset);
if (offset < 1 || offset >= styledText.getCharCount()) {
return;
}
char _c = styledText.getText().charAt(offset - 1);
if (c == '\n' && (_c == Constants.LINE_SEPARATOR_CHARACTER)) {
if (e.keyCode == SWT.ARROW_LEFT) {
styledText.setCaretOffset(offset - 1);
} else if (e.keyCode == SWT.ARROW_RIGHT) {
styledText.setCaretOffset(offset + 1);
}
}
} else if ((e.stateMask == SWT.SHIFT || e.stateMask == (SWT.SHIFT | SWT.CTRL)) && (e.keyCode == SWT.ARROW_LEFT || e.keyCode == SWT.ARROW_RIGHT)) {
StyledText styledText = (StyledText) e.widget;
int offset = styledText.getCaretOffset();
char c = styledText.getText().charAt(offset);
if (offset < 1 || offset >= styledText.getCharCount()) {
return;
}
char _c = styledText.getText().charAt(offset - 1);
if ((c == '' && (_c == Constants.TAB_CHARACTER || _c == Constants.SPACE_CHARACTER)) || (c == '\n' && (_c == Constants.LINE_SEPARATOR_CHARACTER))) {
if (e.keyCode == SWT.ARROW_LEFT) {
styledText.invokeAction(ST.SELECT_COLUMN_PREVIOUS);
} else if (e.keyCode == SWT.ARROW_RIGHT) {
styledText.invokeAction(ST.SELECT_COLUMN_NEXT);
}
}
} else if ((e.stateMask == SWT.SHIFT) && (e.keyCode == SWT.ARROW_UP || e.keyCode == SWT.ARROW_DOWN)) {
StyledText styledText = (StyledText) e.widget;
int offset = styledText.getCaretOffset();
char c = styledText.getText().charAt(offset);
if (offset < 1 || offset >= styledText.getCharCount()) {
return;
}
char _c = styledText.getText().charAt(offset - 1);
if (c == '\n' && (_c == Constants.LINE_SEPARATOR_CHARACTER)) {
if (e.keyCode == SWT.ARROW_UP) {
styledText.invokeAction(ST.SELECT_COLUMN_PREVIOUS);
} else if (e.keyCode == SWT.ARROW_DOWN) {
styledText.invokeAction(ST.SELECT_COLUMN_NEXT);
}
}
}
}
});
/**
* 处理在显示非打印隐藏字符的情况光标移动问题。兼容非打印字符替换符号
*/
styledText.addMouseListener(new MouseListener() {
public void mouseUp(MouseEvent e) {
}
public void mouseDown(MouseEvent e) {
if (!XliffEditorParameter.getInstance().isShowNonpirnttingCharacter()) {
return;
}
StyledText styledText = (StyledText) e.widget;
int offset = styledText.getCaretOffset();
if (offset < 1 || offset >= styledText.getCharCount()) {
return;
}
// hidden character
char c = styledText.getText().charAt(offset);
// display character
char _c = styledText.getText().charAt(offset - 1);
if ((_c == Constants.LINE_SEPARATOR_CHARACTER || _c == Constants.TAB_CHARACTER || _c == Constants.SPACE_CHARACTER) && (c == '\n' || c == '')) {
styledText.setCaretOffset(offset + 1);
}
}
public void mouseDoubleClick(MouseEvent e) {
}
});
/**
* 选择内容时对非打印字符的处理
*/
styledText.addMouseMoveListener(new MouseMoveListener() {
public void mouseMove(MouseEvent e) {
StyledText styledText = (StyledText) e.widget;
int offset = styledText.getCaretOffset();
if (offset < 1 || offset >= styledText.getCharCount()) {
return;
}
char c = styledText.getText().charAt(offset);
char _c = styledText.getText().charAt(offset - 1);
if ((c == '' && (_c == Constants.TAB_CHARACTER || _c == Constants.SPACE_CHARACTER)) || (c == '\n' && (_c == Constants.LINE_SEPARATOR_CHARACTER))) {
int caretOffset = styledText.getCaretOffset();
Point p = styledText.getSelection();
if (caretOffset == p.x) {
styledText.invokeAction(ST.SELECT_COLUMN_PREVIOUS);
} else if (caretOffset == p.y) {
styledText.invokeAction(ST.SELECT_COLUMN_NEXT);
}
}
}
});
styledText.addVerifyListener(new VerifyListener() {
public void verifyText(final VerifyEvent e) {
String t = e.text;
if ((e.start == e.end) || (e.start != e.end && !e.text.equals(""))) {
// 添加内容时
if (XliffEditorParameter.getInstance().isShowNonpirnttingCharacter()) {
t = t.replace("\t", Constants.TAB_CHARACTER + "").replace(" ", Constants.SPACE_CHARACTER + "");
t = t.replace(System.getProperty("line.separator"), "\n");
StringBuffer bf = new StringBuffer(t);
int i = bf.indexOf("\n");
if (i != -1) {
if (i == 0) {
bf.insert(i, Constants.LINE_SEPARATOR_CHARACTER);
} else if (i != 0 && bf.charAt(i - 1) != Constants.LINE_SEPARATOR_CHARACTER) {
bf.insert(i, Constants.LINE_SEPARATOR_CHARACTER);
}
i = bf.indexOf("\n", i + 1);
}
e.text = bf.toString();
}
return;
}
final StyledText styledText = (StyledText) e.widget;
final String text = styledText.getText(e.start, e.end - 1);
final Matcher matcher = PATTERN.matcher(text);
if (matcher.find()) {
// 被删除的部分中存在标记的的情况,进行特殊处理。
if (isSource()) {
e.doit = false;
setToolTipMessage(Messages.getString("innertag.SegmentViewer.msg1"));
return;
}
matcher.reset();
styledText.getDisplay().syncExec(new Runnable() {
public void run() {
deleteInnerTagInPairs(e, matcher);
}
});
}
if (XliffEditorParameter.getInstance().isShowNonpirnttingCharacter()) {
if (text.length() == 1 && (text.equals("\n") || text.indexOf('') != -1)) {
char c = styledText.getText().charAt(e.start - 1);
if (c == Constants.LINE_SEPARATOR_CHARACTER || c == Constants.SPACE_CHARACTER || c == Constants.TAB_CHARACTER) {
styledText.replaceTextRange(e.start - 1, 2, "");
e.doit = false;
}
} else if (text.length() == 1 && (text.indexOf(Constants.LINE_SEPARATOR_CHARACTER) != -1 || text.indexOf(Constants.TAB_CHARACTER) != -1 || text.indexOf(Constants.TAB_CHARACTER) != -1)) {
char c = styledText.getText().charAt(e.start + 1);
if (c == '\n' || c == '') {
styledText.replaceTextRange(e.start, 2, "");
e.doit = false;
}
}
}
}
/**
* 成对删除内部标记
*/
private void deleteInnerTagInPairs(final VerifyEvent e, Matcher matcher) {
StyledText styledText = (StyledText) e.widget;
// 记录被删除的标记的索引。
ArrayList<Integer> tagIndexes = new ArrayList<Integer>();
while (matcher.find()) {
String placeHolder = matcher.group();
InnerTag innerTag = InnerTagUtil.getInnerTag(SegmentViewer.this.getInnerTagCacheList(), placeHolder);
if (innerTag != null && innerTag.isVisible()) {
innerTag.setVisible(false);
// 保存成对标记中未完全删除的标记索引
TagType tagType = innerTag.getInnerTagBean().getType();
if (tagType == TagType.START || tagType == TagType.END) {
// 处理成对标记的成对删除
// 标记索引
Integer tagIndex = Integer.valueOf(innerTag.getInnerTagBean().getIndex());
if (tagIndexes.contains(tagIndex)) {
// 如果已经包含此索引,说明成对标记的2个部分都已经删除。
tagIndexes.remove(tagIndex);
} else {
// 如果未包含此索引,则说明只删除了一个部分(开始或结束)的标记。
tagIndexes.add(tagIndex);
}
}
}
}
if (!tagIndexes.isEmpty()) {
// 存在未删除的情况。
getUndoManager().beginCompoundChange();
// 上一步已经修改,取消修改操作。
e.doit = false;
// 替换改动内容
styledText.getContent().replaceTextRange(e.start, e.end - e.start, e.text);
for (int i = 0; i < errorTagStart; i++) {
// 删除成对标记中未被删除的部分。
InnerTag innerTag = innerTagCacheList.get(i);
if (innerTag != null && innerTag.isVisible()) {
if (tagIndexes.contains(innerTag.getInnerTagBean().getIndex())) {
innerTag.setVisible(false);
String placeHolder = placeHolderBuilder.getPlaceHolder(null, i);
int start = -1;
if ((start = styledText.getText().indexOf(placeHolder)) != -1) {
styledText.getContent().replaceTextRange(start, placeHolder.length(), "");
}
tagIndexes.remove(Integer.valueOf(innerTag.getInnerTagBean().getIndex()));
if (tagIndexes.isEmpty()) {
break;
}
}
}
}
getUndoManager().endCompoundChange();
/**
* 通知更新主菜单(actionBar)中“撤销重做”等菜单项的状态,参见
* net.heartsome.cat.ts.ui.xliffeditor.nattable.editor.XLIFFEditorActionHandler
*/
styledText.notifyListeners(SWT.Selection, null);
}
}
});
/**
* 重绘时,将当前文本段中存在的内部标记对应的控件显示出来
*/
styledText.addPaintObjectListener(new PaintObjectListener() {
public void paintObject(PaintObjectEvent event) {
StyleRange styleRange = event.style;
if (styleRange != null) {
String text = ((StyledText) event.widget).getText();
int end = styleRange.start + styleRange.length;
if (text.length() < end) {
return;
}
String styledString = text.substring(styleRange.start, end);
Matcher matcher = PATTERN.matcher(styledString);
if (matcher.matches()) {
InnerTag tag = InnerTagUtil.getInnerTag(SegmentViewer.this.getInnerTagCacheList(), styledString);
if (tag != null) {
if (!tag.isVisible()) {
tag.setVisible(true);
}
// int y = event.y + event.ascent - styleRange.metrics.ascent;
int lineHeight = getTextWidget().getLineHeight();
// innerTag.setLocation(event.x + SEGMENT_LINE_SPACING / 2, event.y + SEGMENT_LINE_SPACING /
// 2 /* 行距的一半 */);
int y = event.y + lineHeight / 2 - tag.getBounds().height / 2;
tag.setLocation(event.x + SEGMENT_LINE_SPACING, y);
}
}
}
}
});
/**
* 鼠标移动时,清除错误消息。
*/
styledText.addMouseMoveListener(new MouseMoveListener() {
public void mouseMove(MouseEvent e) {
if (getTextWidget().getToolTipText() != null && getTextWidget().getToolTipText().length() > 0) {
setToolTipMessage("");
}
}
});
// 处理修改内容时,需要非打印字符添加样式。
styledText.addListener(SWT.Modify, new Listener() {
public void handleEvent(Event event) {
if (!XliffEditorParameter.getInstance().isShowNonpirnttingCharacter()) {
return;
}
String s = event.text;
Matcher matcher = Constants.NONPRINTING_PATTERN.matcher(s);
TextStyle style = new TextStyle(null, GUIHelper.getColor(new RGB(100, 100, 100)), null);
List<StyleRange> ranges = new ArrayList<StyleRange>();
while (matcher.find()) {
int start = event.start + matcher.start();
StyleRange range = new StyleRange(style);
range.start = start;
range.length = 1;
ranges.add(range);
}
for (StyleRange range : ranges) {
getTextWidget().setStyleRange(range);
}
}
});
styledText.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
for (InnerTag tag : innerTagCacheList) {
if (tag.isSelected()) {
tag.setSelected(false);
tag.redraw();
}
}
String styledString = getTextWidget().getSelectionText();
Matcher matcher = PATTERN.matcher(styledString);
while (matcher.find()) {
String s = matcher.group();
InnerTag tag = InnerTagUtil.getInnerTag(SegmentViewer.this.getInnerTagCacheList(), s);
if (tag != null) {
tag.setSelected(true);
tag.redraw();
}
}
}
});
}
use of org.eclipse.swt.events.VerifyEvent in project translationstudio8 by heartsome.
the class SetSplitPointByWordNumDialog method createSegCmp.
/**
* 创建片段
* @param bean
*/
private void createSegCmp(SegPointBean bean) {
final Composite curSegCmp = new Composite(segParentCmp, SWT.NONE);
GridDataFactory.fillDefaults().grab(true, false).applyTo(curSegCmp);
GridLayoutFactory.fillDefaults().margins(0, 0).numColumns(5).applyTo(curSegCmp);
// 第一列,一个label
Label segLbl = new Label(curSegCmp, SWT.NONE);
segLbl.setText(Messages.getString("SetSplitPointByWordNumDialog.segLbl"));
Label segIndexLbl = new Label(curSegCmp, SWT.LEFT);
segIndexLbl.setText("");
GridDataFactory.swtDefaults().align(SWT.LEFT, SWT.CENTER).hint(20, SWT.DEFAULT).applyTo(segIndexLbl);
final Text wordNumTxt = new Text(curSegCmp, SWT.BORDER);
GridDataFactory.swtDefaults().hint(60, SWT.DEFAULT).applyTo(wordNumTxt);
wordNumTxt.setText(bean.getWordNumber() + "");
wordNumTxt.addVerifyListener(new VerifyListener() {
public void verifyText(VerifyEvent e) {
String addText = e.text;
if (addText.length() > 0) {
e.doit = addText.matches("\\d+");
}
}
});
Button addBtn = new Button(curSegCmp, SWT.NONE);
addBtn.setImage(addImg);
Button deleteBtn = new Button(curSegCmp, SWT.NONE);
deleteBtn.setImage(deleteImg);
addBtn.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
int addedWordNum = 0;
for (Control control : segParentCmp.getChildren()) {
if (control instanceof Composite) {
Control childControl = ((Composite) control).getChildren()[2];
if (childControl instanceof Text) {
addedWordNum += Integer.parseInt(((Text) childControl).getText());
}
}
}
SegPointBean addBean = new SegPointBean((totalWordNum - addedWordNum) > 0 ? (totalWordNum - addedWordNum) : 0);
// 开始创建片断列表
createSegCmp(addBean);
wordNumberScroll.setMinSize(segParentCmp.computeSize(SWT.DEFAULT, SWT.DEFAULT));
segParentCmp.layout();
wordNumberScroll.layout();
}
});
deleteBtn.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
if (e.getSource() instanceof Button) {
Composite parentCmp = ((Button) e.getSource()).getParent();
parentCmp.dispose();
wordNumberScroll.setMinSize(segParentCmp.computeSize(SWT.DEFAULT, SWT.DEFAULT));
segParentCmp.layout();
wordNumberScroll.layout();
refreshSegListArea();
}
}
});
refreshSegListArea();
}
use of org.eclipse.swt.events.VerifyEvent in project cubrid-manager by CUBRID.
the class SQLEditorComposite method addListener.
/**
* Add listener for text
*/
private void addListener() {
text.addModifyListener(new ModifyListener() {
IAction undoAction = ActionManager.getInstance().getAction(UndoAction.ID);
IAction redoAction = ActionManager.getInstance().getAction(RedoAction.ID);
public void modifyText(ModifyEvent event) {
setDirty(true);
if (!undoAction.isEnabled()) {
FocusAction.changeActionStatus(undoAction, text);
}
if (!redoAction.isEnabled()) {
FocusAction.changeActionStatus(redoAction, text);
}
ServerInfo serverInfo = queryEditor == null || queryEditor.getSelectedServer() == null ? null : queryEditor.getSelectedServer().getServerInfo();
boolean isLowerCase = QueryOptions.getKeywordLowercase(serverInfo);
boolean isNoAutoUpperCase = QueryOptions.getNoAutoUppercaseKeyword(serverInfo);
if (!isLowerCase && !isNoAutoUpperCase) {
autoReplaceKeyword();
}
}
// FIXME extract method?
// replace keyword to upper case automatically
public void autoReplaceKeyword() {
if (pendingCompletionsListener) {
return;
}
int pos = text.getCaretOffset() - 1;
if (pos <= 0) {
return;
}
String currentKey = text.getText(pos, pos);
if (currentKey == null || currentKey.length() <= 0) {
return;
}
char cur = currentKey.charAt(0);
if (cur != ' ' && cur != '(' && cur != '\t' && cur != '\n' && cur != '\r' && cur != ',') {
return;
}
pos--;
if (pos < 0) {
return;
}
int spos = pos - 20;
if (spos < 0) {
spos = 0;
}
String txt = text.getText(spos, pos);
spos = pos + 1;
for (int i = txt.length() - 1; i >= 0; i--) {
char c = txt.charAt(i);
if (c == ' ' || c == '\t' || c == '\n' || c == '(') {
break;
}
spos--;
}
int epos = pos;
if (spos < 0 || epos < 0 || spos > epos) {
return;
}
String currentKeyword = text.getText(spos, epos);
if (currentKeyword == null) {
return;
}
int len = currentKeyword.length();
for (int i = 0; i < QuerySyntax.KEYWORDS_AUTO_UPPER.length; i++) {
String keyword = QuerySyntax.KEYWORDS_AUTO_UPPER[i];
if (keyword.equalsIgnoreCase(currentKeyword)) {
pendingCompletionsListener = true;
text.replaceTextRange(spos, len, keyword.toUpperCase());
pendingCompletionsListener = false;
break;
}
}
}
});
text.addVerifyKeyListener(new VerifyKeyListener() {
public void verifyKey(VerifyEvent event) {
if ((event.stateMask & SWT.SHIFT) != 0 && event.keyCode == SWT.TAB) {
event.doit = false;
return;
}
if (((event.stateMask & SWT.CTRL) != 0 || (event.stateMask & SWT.COMMAND) != 0) && (event.keyCode == SWT.CR || event.keyCode == SWT.KEYPAD_CR)) {
event.doit = false;
return;
}
if (((event.stateMask & SWT.CTRL) != 0 || (event.stateMask & SWT.COMMAND) != 0) && (event.stateMask & SWT.ALT) != 0) {
event.doit = false;
return;
}
if (event.keyCode == SWT.TAB) {
event.doit = false;
} else {
event.doit = true;
}
}
});
text.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent event) {
if ((event.stateMask & SWT.COMMAND) != 0) {
//for Mac
if ((event.stateMask & SWT.SHIFT) != 0) {
if (event.keyCode == SWT.CR || event.keyCode == SWT.KEYPAD_CR) {
queryEditor.runQuery(false);
return;
}
}
}
if (event.keyCode == SWT.F5 || (event.stateMask & SWT.CTRL) != 0 && event.keyCode == 'e') {
queryEditor.runQuery(false);
} else if (event.keyCode == SWT.F6 || (event.stateMask & SWT.CTRL) != 0 && event.keyCode == 'l') {
queryEditor.runQuery(true);
} else if (event.keyCode == SWT.F7) {
queryEditor.getCombinedQueryComposite().showQueryHistory();
} else if (event.keyCode == SWT.F8) {
queryEditor.runMultiQuery();
} else if (event.keyCode == SWT.F1) {
ActionManager.getInstance().getAction(HelpDocumentAction.ID).run();
} else if (event.keyCode == SWT.F3) {
if ((event.stateMask & SWT.SHIFT) == 0) {
TextEditorFindReplaceMediator.findNext();
} else {
TextEditorFindReplaceMediator.findPrevious();
}
} else if (event.keyCode == SWT.F9) {
queryEditor.setTuningModeButton(!queryEditor.isTuningModeButton());
} else if (event.keyCode == SWT.F11) {
queryEditor.getCombinedQueryComposite().rotateQueryPlanDisplayMode();
} else if ((event.stateMask & SWT.CTRL) == 0 && (event.stateMask & SWT.SHIFT) == 0 && (event.stateMask & SWT.ALT) == 0 && event.keyCode == SWT.ESC) {
int cursorOffset = text.getCaretOffset();
text.setSelectionRange(cursorOffset, 0);
} else if ((event.stateMask & SWT.CTRL) != 0 && (event.stateMask & SWT.SHIFT) == 0 && (event.stateMask & SWT.ALT) == 0 && event.keyCode == ',') {
new QuickBuilderDialog(getShell(), SWT.NONE).open();
} else if ((event.stateMask & SWT.CTRL) != 0 && event.keyCode == ' ') {
contentAssistant.showPossibleCompletions();
} else if ((event.stateMask & SWT.CTRL) != 0 && event.keyCode == 'r') {
recentlyUsedSQLcontentAssistant.showPossibleCompletions();
} else if ((event.stateMask & SWT.CTRL) != 0 && event.keyCode == 'd') {
deleteRow();
} else if ((event.stateMask & SWT.CTRL) != 0 && (event.stateMask & SWT.ALT) != 0 && event.keyCode == SWT.ARROW_DOWN) {
duplicateRow();
} else if ((event.stateMask & SWT.CTRL) != 0 && (event.stateMask & SWT.SHIFT) != 0 && event.keyCode == 't') {
//add sql tab
queryEditor.addEditorTab();
} else if (((event.stateMask & SWT.CTRL) != 0 && (event.stateMask & SWT.SHIFT) != 0) && (event.keyCode == 'f' || event.keyCode == 'F')) {
format();
} else if ((event.stateMask & SWT.ALT) != 0 && event.keyCode == '/') {
contentAssist();
} else if ((event.stateMask & SWT.CTRL) != 0 && event.keyCode == 'o') {
// Open a SQL file...
ActionManager.getInstance().getAction(QueryOpenAction.ID).run();
} else if ((event.stateMask & SWT.CTRL) != 0 && event.keyCode == 's') {
// Save SQL to a file...
try {
save();
} catch (IOException e) {
MessageDialog.openError(getShell(), Messages.error, Messages.errCanNotSaveASQLFile);
}
} else if ((event.stateMask & SWT.CTRL) != 0 && (event.stateMask & SWT.SHIFT) == 0) {
if (event.keyCode == '/') {
inputComment(false, false);
} else if (event.keyCode == 'z') {
event.doit = false;
undo();
} else if (event.keyCode == 'y') {
redo();
} else if (event.keyCode == 'f' || event.keyCode == 'h') {
find();
} else if ((event.stateMask & SWT.ALT) != 0 && (event.keyCode == SWT.CR || event.keyCode == SWT.KEYPAD_CR)) {
// NOPMD
queryEditor.runQueryPlanInCursorLine();
} else if (event.keyCode == SWT.CR || event.keyCode == SWT.KEYPAD_CR) {
queryEditor.runQueryInCursorLine();
} else if (event.keyCode == 'g') {
gotoLine();
}
} else if ((event.stateMask & SWT.CTRL) != 0 && (event.stateMask & SWT.SHIFT) != 0) {
if ((event.stateMask & SWT.ALT) != 0 && (event.keyCode == SWT.CR || event.keyCode == SWT.KEYPAD_CR)) {
// NOPMD
queryEditor.runQuery(true);
} else if (event.keyCode == SWT.CR || event.keyCode == SWT.KEYPAD_CR) {
queryEditor.runQuery(false);
} else if (event.keyCode == 'x') {
toUpperCase();
} else if (event.keyCode == 'y') {
toLowerCase();
}
} else if ((event.stateMask & SWT.SHIFT) == 0 && event.keyCode == SWT.TAB) {
indent();
} else if ((event.stateMask & SWT.SHIFT) != 0 && event.keyCode == SWT.TAB) {
unindent();
}
if (SQLContentAssistProcessor.isShowProposal(event.character)) {
contentAssistant.showPossibleCompletions();
useCompletions = true;
} else if ((event.character >= 'A' && event.character <= 'Z') || (event.character >= 'a' && event.character <= 'z')) {
if (useCompletions) {
contentAssistant.showPossibleCompletions();
}
useCompletions = false;
} else if (event.character == ' ' || event.character == '\t' || event.keyCode == SWT.KEYPAD_CR || event.keyCode == SWT.CR || event.keyCode == SWT.BS || (text.getText().trim().length() < 1)) {
useCompletions = true;
}
// ctrl + alt + 1 ~ 9 : change editor tab
if ((event.stateMask & SWT.CTRL) != 0 && (event.stateMask & SWT.ALT) != 0 && (event.keyCode >= '1' && event.keyCode <= '9')) {
IWorkbenchPage page = LayoutUtil.getActivePage();
if (page != null) {
int index = event.keyCode - '1';
IEditorReference[] refs = page.getEditorReferences();
for (int i = 0, selected = 0; i < refs.length; i++) {
IEditorReference ref = refs[i];
if (QueryEditorPart.ID.equals(ref.getId())) {
if (index == selected) {
IEditorPart part = ref.getEditor(true);
page.activate(part);
}
selected++;
}
}
}
} else if ((event.stateMask & SWT.CTRL) != 0 && (event.keyCode >= '1' && event.keyCode <= '9')) {
// ctrl + 1 ~ 9 : change middle tab
ITabSelection selector = queryEditor.getCombinedQueryComposite();
selector.select(event.keyCode - '1', -1);
} else if ((event.stateMask & SWT.ALT) != 0 && (event.keyCode >= '1' && event.keyCode <= '9')) {
// alt + 1 ~ 9 : change bottom tab
ITabSelection selector = queryEditor.getCombinedQueryComposite();
selector.select(-1, event.keyCode - '1');
}
}
});
text.addSelectionListener(new SelectionAdapter() {
/**
* @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
* @param event an event containing information about the selection
*/
public void widgetSelected(SelectionEvent event) {
IAction copyAction = ActionManager.getInstance().getAction(CopyAction.ID);
copyAction.setEnabled(false);
IAction cutAction = ActionManager.getInstance().getAction(CutAction.ID);
cutAction.setEnabled(false);
IAction reformatColumnsAliasAction = ActionManager.getInstance().getAction(ReformatColumnsAliasAction.ID);
reformatColumnsAliasAction.setEnabled(false);
// show schema info view with a selected text
IAction showSchemaAction = ActionManager.getInstance().getAction(ShowSchemaAction.ID);
showSchemaAction.setEnabled(false);
if (event.getSource() instanceof StyledText) {
StyledText stext = (StyledText) event.getSource();
if (stext != null && stext.getSelectionText() != null && stext.getSelectionText().length() > 0) {
copyAction.setEnabled(true);
cutAction.setEnabled(true);
reformatColumnsAliasAction.setEnabled(true);
CubridDatabase db = queryEditor.getSelectedDatabase();
if (DatabaseNavigatorMenu.SELF_DATABASE_ID.equals(db.getId()) && ApplicationType.CUBRID_MANAGER.equals(PerspectiveManager.getInstance().getCurrentMode())) {
showSchemaAction.setEnabled(false);
} else {
showSchemaAction.setEnabled(true);
}
}
}
}
});
text.addFocusListener(new FocusAdapter() {
public void focusGained(FocusEvent event) {
ActionManager.getInstance().changeFocusProvider(text);
}
});
TextEditorFindReplaceMediator editorDialogMediator = new TextEditorFindReplaceMediator();
text.addFocusListener(editorDialogMediator);
}
Aggregations