MenuDetectListener
interface.
+ *
+ * @param listener the listener which should be notified
+ */
+ public abstract void addMenuDetectListener(MenuDetectListener listener);
+
+ /** Sets the receiver's pop up menu to the argument. */
+ public abstract void setMenu(Menu menu);
+
+ /** Cuts the selected text. */
+ public abstract void cut();
+
+ /** Copies the selected text. */
+ public abstract void copy();
+
+ /** Pastes text from clipboard. */
+ public abstract void paste();
+
+ /** Selects all the text in the receiver. */
+ public abstract void selectAll();
+
+ /**
+ * Returns the caret position relative to the start of the text.
+ *
+ * Indexing is zero based. + * + * @return the caret position relative to the start of the text. + */ + public abstract int getCaretPosition(); + + /** + * Sets the caret position. + * + * @param position set caret offset, relative to the first character in the text. + */ + public abstract void setCaretPosition(int position); + + /** + * Gets the number of characters. + * + * @return number of characters in the widget + */ + public abstract int getCharCount(); + + /** + * @return The caret line number, starting from 1. + */ + public int getLineNumber() { + String text = getText(); + if (StringUtils.isEmpty(text)) { + return 1; + } + + int rowNumber = 1; + int textPosition = getCaretPosition(); + while (textPosition > 0) { + if (text.charAt(textPosition - 1) == '\n') { + rowNumber++; + } + textPosition--; + } + + return rowNumber; + } + + /** + * @return The caret column number, starting from 1. + */ + public int getColumnNumber() { + String text = getText(); + if (StringUtils.isEmpty(text)) { + return 1; + } - public abstract int getColumnNumber(); + int columnNumber = 1; + int textPosition = getCaretPosition(); + while (textPosition > 0 + && text.charAt(textPosition - 1) != '\n' + && text.charAt(textPosition - 1) != '\r') { + textPosition--; + columnNumber++; + } + + return columnNumber; + } + /** + * Returns the widget text. + * + *
The text for a text widget is the characters in the widget, or an empty string if this has + * never been set. + * + * @return the widget text + */ public abstract String getText(); + /** Sets the contents of the receiver to the given string. */ public abstract void setText(String text); - public abstract String getSelectionText(); + /** + * Returns the number of selected characters. + * + * @return the number of selected characters. + */ + public abstract int getSelectionCount(); - public abstract int getCaretOffset(); + /** Gets the selected text, or an empty string if there is no current selection. */ + public abstract String getSelectionText(); + /** + * Inserts a string. + * + *
The old selection is replaced with the new text. + * + * @param string the string + */ public abstract void insert(String strInsert); - public abstract void setSelection(int iStart, int length); + /** + * Sets the selection. + * + *
Indexing is zero based. The range of a selection is from 0..N where N is the number of + * characters in the widget. + * + * @param start new caret position + */ + public abstract void setSelection(int start); - public abstract int getSelectionCount(); + /** + * Sets the selection to the range specified by the given start and end indices. + * + *
Indexing is zero based. The range of a selection is from 0..N where N is the number of + * characters in the widget. + * + *
+ * + * @param start the start of the range + * @param end the end of the range + */ + public abstract void setSelection(int start, int end); + + /** + * Returns the editable state. + * + * @return whether or not the receiver is editable + */ + public abstract boolean isEditable(); + + /** + * Sets the editable state. + * + * @param editable the new editable state + */ + public abstract void setEditable(boolean editable); + + /** + * Check if something is stored inside the Clipboard. + * + * @return false if no text is available inside the Clipboard + */ + protected boolean checkPaste() { + try { + Clipboard clipboard = new Clipboard(getParent().getDisplay()); + String text = (String) clipboard.getContents(TextTransfer.getInstance()); + if (text != null && text.length() > 0) { + return true; + } else { + return false; + } + } catch (Exception e) { + return false; + } + } + + protected void undo() {} + + protected void redo() {} + + protected boolean isSupportUnoRedo() { + return false; + } + + protected void buildingStyledTextMenu(Menu popupMenu) { + + if (isSupportUnoRedo()) { + final MenuItem undoItem = new MenuItem(popupMenu, SWT.PUSH); + undoItem.setText( + OsHelper.customizeMenuitemText(BaseMessages.getString(PKG, "WidgetDialog.Styled.Undo"))); + undoItem.setImage( + GuiResource.getInstance() + .getImage("ui/images/undo.svg", ConstUi.SMALL_ICON_SIZE, ConstUi.SMALL_ICON_SIZE)); + undoItem.addListener(SWT.Selection, event -> undo()); + + final MenuItem redoItem = new MenuItem(popupMenu, SWT.PUSH); + redoItem.setText( + OsHelper.customizeMenuitemText(BaseMessages.getString(PKG, "WidgetDialog.Styled.Redo"))); + redoItem.setImage( + GuiResource.getInstance() + .getImage("ui/images/redo.svg", ConstUi.SMALL_ICON_SIZE, ConstUi.SMALL_ICON_SIZE)); + redoItem.addListener(SWT.Selection, event -> redo()); + + new MenuItem(popupMenu, SWT.SEPARATOR); + } + + final MenuItem cutItem = new MenuItem(popupMenu, SWT.PUSH); + cutItem.setText( + OsHelper.customizeMenuitemText(BaseMessages.getString(PKG, "WidgetDialog.Styled.Cut"))); + cutItem.setImage( + GuiResource.getInstance() + .getImage("ui/images/cut.svg", ConstUi.SMALL_ICON_SIZE, ConstUi.SMALL_ICON_SIZE)); + cutItem.addListener(SWT.Selection, event -> cut()); + + final MenuItem copyItem = new MenuItem(popupMenu, SWT.PUSH); + copyItem.setText( + OsHelper.customizeMenuitemText(BaseMessages.getString(PKG, "WidgetDialog.Styled.Copy"))); + copyItem.setImage( + GuiResource.getInstance() + .getImage("ui/images/copy.svg", ConstUi.SMALL_ICON_SIZE, ConstUi.SMALL_ICON_SIZE)); + copyItem.addListener(SWT.Selection, event -> copy()); + + final MenuItem pasteItem = new MenuItem(popupMenu, SWT.PUSH); + pasteItem.setText( + OsHelper.customizeMenuitemText(BaseMessages.getString(PKG, "WidgetDialog.Styled.Paste"))); + pasteItem.setImage( + GuiResource.getInstance() + .getImage("ui/images/paste.svg", ConstUi.SMALL_ICON_SIZE, ConstUi.SMALL_ICON_SIZE)); + pasteItem.addListener(SWT.Selection, event -> paste()); + + new MenuItem(popupMenu, SWT.SEPARATOR); + + final MenuItem selectAllItem = new MenuItem(popupMenu, SWT.PUSH); + selectAllItem.setText( + OsHelper.customizeMenuitemText( + BaseMessages.getString(PKG, "WidgetDialog.Styled.SelectAll"))); + selectAllItem.setImage( + GuiResource.getInstance() + .getImage( + "ui/images/select-all.svg", ConstUi.SMALL_ICON_SIZE, ConstUi.SMALL_ICON_SIZE)); + selectAllItem.addListener(SWT.Selection, event -> selectAll()); + + addListener( + SWT.KeyDown, + event -> { + if (isSupportUnoRedo() + && event.keyCode == 'z' + && (event.stateMask & SWT.MOD1) != 0 + && (event.stateMask & SWT.MOD2) != 0) { + redo(); + } else if (isSupportUnoRedo() + && event.keyCode == 'z' + && (event.stateMask & SWT.MOD1) != 0) { + undo(); + } else if (event.keyCode == 'a' && (event.stateMask & SWT.MOD1) != 0) { + selectAll(); + } else if (event.keyCode == 'f' && (event.stateMask & SWT.MOD1) != 0) { + // TODO: implement FIND + // find(); + } else if (event.keyCode == 'h' && (event.stateMask & SWT.MOD1) != 0) { + // TODO: implement FIND AND REPLACE + // findAndReplace(); + } + }); + + addMenuDetectListener( + event -> { + pasteItem.setEnabled(checkPaste()); + if (getSelectionCount() > 0) { + cutItem.setEnabled(true); + copyItem.setEnabled(true); + } else { + cutItem.setEnabled(false); + copyItem.setEnabled(false); + } + }); + + setMenu(popupMenu); + } } diff --git a/ui/src/main/resources/org/apache/hop/ui/core/widget/messages/messages_en_US.properties b/ui/src/main/resources/org/apache/hop/ui/core/widget/messages/messages_en_US.properties index ebd000af38..f85bda653d 100644 --- a/ui/src/main/resources/org/apache/hop/ui/core/widget/messages/messages_en_US.properties +++ b/ui/src/main/resources/org/apache/hop/ui/core/widget/messages/messages_en_US.properties @@ -93,4 +93,6 @@ TextVar.VariableValue.Message=The value of variable ''{0}'' is \: \n\n\t{1}\n\n WidgetDialog.Styled.Copy=Copy\tCtrl+C WidgetDialog.Styled.Cut=Cut\tCtrl+X WidgetDialog.Styled.Paste=Paste\tCtrl+V +WidgetDialog.Styled.Redo=Redo\tCtrl+Shift+Z WidgetDialog.Styled.SelectAll=Select &All\tCtrl+A +WidgetDialog.Styled.Undo=Undo\tCtrl+Z