Skip to content

Commit

Permalink
Implement Undo/Redo for StyledText apache#4708
Browse files Browse the repository at this point in the history
Fix Variable insertion apache#4591
If StyledText widget, position the variables list under the caret
Fix StyledText "disabled" look with dark mode
Add key CTRL+A for select all in TextComposite
Use TextComposite in ActionEvalDialog
Reduce code duplication
  • Loading branch information
nadment committed Dec 15, 2024
1 parent 8f9dcb2 commit ee38ced
Show file tree
Hide file tree
Showing 19 changed files with 878 additions and 1,469 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@
import org.apache.hop.ui.core.PropsUi;
import org.apache.hop.ui.core.dialog.BaseDialog;
import org.apache.hop.ui.core.dialog.MessageBox;
import org.apache.hop.ui.core.widget.JavaScriptStyledTextComp;
import org.apache.hop.ui.core.widget.StyledTextComp;
import org.apache.hop.ui.core.widget.TextComposite;
import org.apache.hop.ui.pipeline.transform.BaseTransformDialog;
import org.apache.hop.ui.util.EnvironmentUtils;
import org.apache.hop.ui.workflow.action.ActionDialog;
import org.apache.hop.ui.workflow.dialog.WorkflowDialog;
import org.apache.hop.workflow.WorkflowMeta;
Expand All @@ -52,7 +55,7 @@ public class ActionEvalDialog extends ActionDialog {

private Text wName;

private StyledTextComp wScript;
private TextComposite wScript;

private Label wlPosition;

Expand Down Expand Up @@ -133,16 +136,30 @@ public IAction open() {
fdlScript.left = new FormAttachment(0, 0);
fdlScript.top = new FormAttachment(wName, margin);
wlScript.setLayoutData(fdlScript);
wScript =
new StyledTextComp(
action, shell, SWT.MULTI | SWT.LEFT | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);

if (EnvironmentUtils.getInstance().isWeb()) {
wScript =
new StyledTextComp(
action,
shell,
SWT.MULTI | SWT.LEFT | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL,
false);
} else {
wScript =
new JavaScriptStyledTextComp(
action,
shell,
SWT.MULTI | SWT.LEFT | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL,
false);
wScript.addLineStyleListener();
}
wScript.setText(BaseMessages.getString(PKG, "ActionEval.Script.Default"));
PropsUi.setLook(wScript, Props.WIDGET_STYLE_FIXED);
wScript.addModifyListener(lsMod);
FormData fdScript = new FormData();
fdScript.left = new FormAttachment(0, 0);
fdScript.top = new FormAttachment(wlScript, margin);
fdScript.right = new FormAttachment(100, -10);
fdScript.right = new FormAttachment(100, 0);
fdScript.bottom = new FormAttachment(wlPosition, -margin);
wScript.setLayoutData(fdScript);
wScript.addModifyListener(arg0 -> setPosition());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ public IAction open() {
FormData fdSql = new FormData();
fdSql.left = new FormAttachment(0, 0);
fdSql.top = new FormAttachment(wlSql, margin);
fdSql.right = new FormAttachment(100, -20);
fdSql.right = new FormAttachment(100, -margin);
fdSql.bottom = new FormAttachment(wlPosition, -margin);
wSql.setLayoutData(fdSql);
wSql.addListener(SWT.Modify, e -> setPosition());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ ActionSQL.NoDatabaseConnection=No database connection is defined.
ActionSQL.NoSQLFileSpecified=Please specify SQL filename\!
ActionSQL.Position.Label=Line {0} Column {1}
ActionSQL.Script.Label=SQL Script\:
ActionSQL.SendOneStatement.Label=Send SQL as single statement?
ActionSQL.SendOneStatement.Label=Send SQL as single statement
ActionSQL.SendOneStatement.Tooltip=Consider SQL as one running statement\nOtherwise ';' will be taken as statement end.
ActionSQL.SQLFileExists=SQL file [{0}] exists
ActionSQL.SQLFileNotExist=We can not find file [{0}]\!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ public void widgetSelected(SelectionEvent e) {
FormData fdSql = new FormData();
fdSql.left = new FormAttachment(0, 0);
fdSql.top = new FormAttachment(wbSqlTable, margin);
fdSql.right = new FormAttachment(100, -10);
fdSql.right = new FormAttachment(100, 0);
fdSql.bottom = new FormAttachment(wlPosition, -margin);
wSql.setLayoutData(fdSql);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1626,7 +1626,7 @@ private void treeDblClick(Event event) {
if (item.getParentItem().equals(wTreeClassesItem)) {
setActiveCtab(item.getText());
} else if (!item.getData().equals("Snippit")) {
int iStart = wScript.getCaretOffset();
int iStart = wScript.getCaretPosition();
int selCount = wScript.getSelectionCount(); // this selection
// will be replaced
// by wScript.insert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1596,8 +1596,7 @@ private void treeDblClick(Event event) {
if (item.getParentItem().equals(wTreeScriptsItem)) {
setActiveCtab(item.getText());
} else if (!item.getData().equals(CONST_FUNCTION)) {
// int iStart = wScript.getTextWidget().getCaretOffset();
int iStart = wScript.getCaretOffset();
int iStart = wScript.getCaretPosition();
int selCount =
wScript.getSelectionCount(); // this selection will be replaced by wScript.insert
iStart =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import org.apache.hop.ui.core.gui.GuiResource;
import org.apache.hop.ui.core.widget.ColumnInfo;
import org.apache.hop.ui.core.widget.ComboVar;
import org.apache.hop.ui.core.widget.ControlSpaceKeyAdapter;
import org.apache.hop.ui.core.widget.PasswordTextVar;
import org.apache.hop.ui.core.widget.StyledTextComp;
import org.apache.hop.ui.core.widget.TableView;
Expand Down Expand Up @@ -744,7 +743,8 @@ public void focusGained(org.eclipse.swt.events.FocusEvent e) {
new StyledTextComp(
variables,
wSearchGroup,
SWT.MULTI | SWT.LEFT | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
SWT.MULTI | SWT.LEFT | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL,
true);
wFilterString.setToolTipText(
BaseMessages.getString(PKG, "LdapInputDialog.FilterString.Tooltip"));
PropsUi.setLook(wFilterString);
Expand All @@ -755,7 +755,6 @@ public void focusGained(org.eclipse.swt.events.FocusEvent e) {
fdFilterString.right = new FormAttachment(100, -2 * margin);
fdFilterString.bottom = new FormAttachment(100, -margin);
wFilterString.setLayoutData(fdFilterString);
wFilterString.addKeyListener(new ControlSpaceKeyAdapter(variables, wFilterString));

FormData fdSearchGroup = new FormData();
fdSearchGroup.left = new FormAttachment(0, margin);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public void mouseUp(MouseEvent e) {
FormData fdlReadField = new FormData();
fdlReadField.left = new FormAttachment(middle, margin);
fdlReadField.right = new FormAttachment(middle * 2, -margin);
fdlReadField.bottom = new FormAttachment(wOk, -3 * margin);
fdlReadField.bottom = new FormAttachment(wOk, -margin);
wlReadField.setLayoutData(fdlReadField);
wReadField = new Text(shell, SWT.SINGLE | SWT.LEFT | SWT.BORDER);
PropsUi.setLook(wReadField);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ public void widgetSelected(SelectionEvent arg0) {
FormData fdSql = new FormData();
fdSql.left = new FormAttachment(0, 0);
fdSql.top = new FormAttachment(wbTable, margin);
fdSql.right = new FormAttachment(100, -2 * margin);
fdSql.right = new FormAttachment(100, -margin);
fdSql.bottom = new FormAttachment(wlPosition, -margin);
wSql.setLayoutData(fdSql);
wSql.addModifyListener(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public String open() {
FormData fdLogMessage = new FormData();
fdLogMessage.left = new FormAttachment(middle, 0);
fdLogMessage.top = new FormAttachment(wLimitRowsNumber, margin);
fdLogMessage.right = new FormAttachment(100, -2 * margin);
fdLogMessage.right = new FormAttachment(100, -margin);
fdLogMessage.height = (int) (125 * props.getZoomFactor());
wLogMessage.setLayoutData(fdLogMessage);
wLogMessage.addListener(SWT.Modify, lsModify);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,9 @@
import org.apache.hop.ui.core.gui.GuiResource;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormLayout;
Expand All @@ -55,8 +52,6 @@ public class ControlSpaceKeyAdapter extends KeyAdapter {

private static final Class<?> PKG = ControlSpaceKeyAdapter.class;

private static final PropsUi props = PropsUi.getInstance();

private final IGetCaretPosition getCaretPositionInterface;

private final IInsertText insertTextInterface;
Expand Down Expand Up @@ -95,31 +90,31 @@ public ControlSpaceKeyAdapter(
* in chinese window, Ctrl-SPACE is reversed by system for input chinese character. use
* Ctrl-ALT-SPACE instead.
*
* @param e the keyevent
* @param event the keyevent
* @return true when ctrl-SPACE is pressed
*/
private boolean isHotKey(KeyEvent e) {
private boolean isHotKey(KeyEvent event) {
if (System.getProperty("user.language").equals("zh")) {
return e.character == ' '
&& ((e.stateMask & SWT.CONTROL) != 0)
&& ((e.stateMask & SWT.ALT) != 0);
return event.character == ' '
&& ((event.stateMask & SWT.CONTROL) != 0)
&& ((event.stateMask & SWT.ALT) != 0);
} else if (OsHelper.isMac()) {
// character is empty when pressing special key in macOs
return e.keyCode == 32
&& ((e.stateMask & SWT.CONTROL) != 0)
&& ((e.stateMask & SWT.ALT) == 0);
return event.keyCode == 32
&& ((event.stateMask & SWT.CONTROL) != 0)
&& ((event.stateMask & SWT.ALT) == 0);
} else {
return e.character == ' '
&& ((e.stateMask & SWT.CONTROL) != 0)
&& ((e.stateMask & SWT.ALT) == 0);
return event.character == ' '
&& ((event.stateMask & SWT.CONTROL) != 0)
&& ((event.stateMask & SWT.ALT) == 0);
}
}

@Override
public void keyPressed(KeyEvent e) {
public void keyPressed(KeyEvent event) {
// CTRL-<SPACE> --> Insert a variable
if (isHotKey(e)) {
e.doit = false;
if (isHotKey(event)) {
event.doit = false;

// textField.setData(TRUE) indicates we have transitioned from the textbox to list mode...
// This will be set to false when the list selection has been processed
Expand All @@ -136,11 +131,21 @@ public void keyPressed(KeyEvent e) {
// Drop down a list of variables...
//
Rectangle bounds = control.getBounds();
Point location = GuiResource.calculateControlPosition(control);
Point location;
if (control instanceof StyledText styledText) {
// Position the list under the caret
location = styledText.getLocationAtOffset(styledText.getCaretOffset());
location.y += styledText.getLineHeight();
location = styledText.toDisplay(location);
} else {
// Position the list under the control
location = GuiResource.calculateControlPosition(control);
location.y += bounds.height;
}

final Shell shell = new Shell(control.getShell(), SWT.NONE);
shell.setSize(bounds.width > 300 ? bounds.width : 300, 200);
shell.setLocation(location.x, location.y + bounds.height);
shell.setLocation(location.x, location.y);
shell.setLayout(new FormLayout());
final List list = new List(shell, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL);
PropsUi.setLook(list);
Expand All @@ -149,59 +154,50 @@ public void keyPressed(KeyEvent e) {
final ToolTip toolTip = new ToolTip(list.getShell(), SWT.BALLOON);
toolTip.setAutoHide(true);

list.addSelectionListener(
new SelectionAdapter() {
// Enter or double-click: picks the variable
//
@Override
public synchronized void widgetDefaultSelected(SelectionEvent e) {
applyChanges(shell, list, control, position, insertTextInterface);
// Double-click: picks the variable
list.addListener(
SWT.DefaultSelection,
e -> applyChanges(shell, list, control, position, insertTextInterface));

// Select a variable name: display the value in a tool tip
list.addListener(
SWT.Selection,
e -> {
if (list.getSelectionCount() <= 0) {
return;
}

// Select a variable name: display the value in a tool tip
//
@Override
public void widgetSelected(SelectionEvent event) {
if (list.getSelectionCount() <= 0) {
return;
}
String name = list.getSelection()[0];
String value = variables.getVariable(name);
Rectangle shellBounds = shell.getBounds();
String message =
BaseMessages.getString(PKG, "TextVar.VariableValue.Message", name, value);
if (name.startsWith(Const.INTERNAL_VARIABLE_PREFIX)) {
message += BaseMessages.getString(PKG, "TextVar.InternalVariable.Message");
}
toolTip.setText(message);
toolTip.setVisible(false);
toolTip.setLocation(
shell.getLocation().x, shell.getLocation().y + shellBounds.height);
toolTip.setVisible(true);
String name = list.getSelection()[0];
String value = variables.getVariable(name);
Rectangle shellBounds = shell.getBounds();
String message =
BaseMessages.getString(PKG, "TextVar.VariableValue.Message", name, value);
if (name.startsWith(Const.INTERNAL_VARIABLE_PREFIX)) {
message += BaseMessages.getString(PKG, "TextVar.InternalVariable.Message");
}
toolTip.setText(message);
toolTip.setVisible(false);
toolTip.setLocation(shell.getLocation().x, shell.getLocation().y + shellBounds.height);
toolTip.setVisible(true);
});

list.addKeyListener(
new KeyAdapter() {

@Override
public synchronized void keyPressed(KeyEvent e) {
if (e.keyCode == SWT.CR
&& ((e.keyCode & SWT.CONTROL) == 0)
&& ((e.keyCode & SWT.SHIFT) == 0)) {
applyChanges(shell, list, control, position, insertTextInterface);
}
// Enter key pressed: picks the variable
list.addListener(
SWT.KeyDown,
e -> {
if (e.keyCode == SWT.CR
&& ((e.stateMask & SWT.CONTROL) == 0)
&& ((e.stateMask & SWT.SHIFT) == 0)) {
applyChanges(shell, list, control, position, insertTextInterface);
}
});

list.addFocusListener(
new FocusAdapter() {
@Override
public void focusLost(FocusEvent event) {
shell.dispose();
if (!control.isDisposed()) {
control.setData(Boolean.FALSE);
}
// Focus lost: close the list
list.addListener(
SWT.FocusOut,
e -> {
shell.dispose();
if (!control.isDisposed()) {
control.setData(Boolean.FALSE);
}
});

Expand All @@ -226,13 +222,13 @@ private static void applyChanges(
if (list.getSelectionCount() <= 0) {
return;
}
if (control instanceof Text textControl) {
textControl.insert(extra);
if (control instanceof Text text) {
text.insert(extra);
} else if (control instanceof CCombo combo) {
combo.setText(
extra); // We can't know the location of the cursor yet. All we can do is overwrite.
} else if (control instanceof StyledTextComp styledTextCompControl) {
styledTextCompControl.insert(extra);
// We can't know the location of the cursor yet. All we can do is overwrite.
combo.setText(extra);
} else if (control instanceof StyledText styledText) {
styledText.insert(extra);
}
}
if (!shell.isDisposed()) {
Expand Down
Loading

0 comments on commit ee38ced

Please sign in to comment.