Skip to content

Commit

Permalink
Merge pull request #907 from hansva/master
Browse files Browse the repository at this point in the history
HOP-2992: change sorting and fix ctrl+space on macOs
  • Loading branch information
hansva committed Jun 29, 2021
2 parents 3092db0 + 839ed23 commit 753004e
Showing 1 changed file with 24 additions and 50 deletions.
Expand Up @@ -28,52 +28,39 @@
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.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolTip;
import org.eclipse.swt.widgets.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.*;

public class ControlSpaceKeyAdapter extends KeyAdapter {

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

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

private IGetCaretPosition getCaretPositionInterface;
private final IGetCaretPosition getCaretPositionInterface;

private IInsertText insertTextInterface;
private final IInsertText insertTextInterface;

private IVariables variables;

private Control control;
private final Control control;

/**
* @param variables
* @param variables IVariables object
* @param control a Text or CCombo box object
*/
public ControlSpaceKeyAdapter(final IVariables variables, final Control control) {
this(variables, control, null, null);
}

/**
* @param variables
* @param variables IVariables object
* @param control a Text or CCombo box object
* @param getCaretPositionInterface
* @param insertTextInterface
Expand All @@ -94,17 +81,18 @@ public ControlSpaceKeyAdapter(
* in chinese window, Ctrl-SPACE is reversed by system for input chinese character. use
* Ctrl-ALT-SPACE instead.
*
* @param e
* @return
* @param e the keyevent
* @return true when ctrl-SPACE is pressed
*/
private boolean isHotKey(KeyEvent e) {
if (System.getProperty("user.language").equals("zh")) {
return e.character == ' '
&& ((e.stateMask & SWT.CONTROL) != 0)
&& ((e.stateMask & SWT.ALT) != 0);
} else if (System.getProperty("os.name").startsWith("Mac OS X")) {
return e.character == ' '
&& ((e.stateMask & SWT.MOD1) != 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);
} else {
return e.character == ' '
Expand All @@ -113,6 +101,7 @@ private boolean isHotKey(KeyEvent e) {
}
}

@Override
public void keyPressed(KeyEvent e) {
// CTRL-<SPACE> --> Insert a variable
if (isHotKey(e)) {
Expand Down Expand Up @@ -149,12 +138,14 @@ public void keyPressed(KeyEvent e) {
new SelectionAdapter() {
// Enter or double-click: picks the variable
//
@Override
public synchronized void widgetDefaultSelected(SelectionEvent e) {
applyChanges(shell, list, control, position, insertTextInterface);
}

// Select a variable name: display the value in a tool tip
//
@Override
public void widgetSelected(SelectionEvent event) {
if (list.getSelectionCount() <= 0) {
return;
Expand All @@ -178,6 +169,7 @@ public void widgetSelected(SelectionEvent event) {
list.addKeyListener(
new KeyAdapter() {

@Override
public synchronized void keyPressed(KeyEvent e) {
if (e.keyCode == SWT.CR
&& ((e.keyCode & SWT.CONTROL) == 0)
Expand All @@ -189,6 +181,7 @@ public synchronized void keyPressed(KeyEvent e) {

list.addFocusListener(
new FocusAdapter() {
@Override
public void focusLost(FocusEvent event) {
shell.dispose();
if (!control.isDisposed()) {
Expand All @@ -201,7 +194,7 @@ public void focusLost(FocusEvent event) {
}
}

private static final void applyChanges(
private static void applyChanges(
Shell shell, List list, Control control, int position, IInsertText insertTextInterface) {
String selection =
list.getSelection()[0].contains(Const.getDeprecatedPrefix())
Expand Down Expand Up @@ -236,7 +229,7 @@ private static final void applyChanges(
}
}

public static final String[] getVariableNames(IVariables variables) {
public static String[] getVariableNames(IVariables variables) {
String[] variableNames = variables.getVariableNames();
for (int i = 0; i < variableNames.length; i++) {
for (int j = 0; j < Const.DEPRECATED_VARIABLES.length; j++) {
Expand All @@ -260,7 +253,8 @@ public static final String[] getVariableNames(IVariables variables) {

// The Hop system settings variables
//
Set<String> hopSystemSettings = new HashSet(Arrays.asList(Const.HOP_SYSTEM_SETTING_VARIABLES));
Set<String> hopSystemSettings =
new HashSet<>(Arrays.asList(Const.HOP_SYSTEM_SETTING_VARIABLES));

Map<String, String> pluginsPrefixesMap = new HashMap<>();

Expand All @@ -275,27 +269,7 @@ public static final String[] getVariableNames(IVariables variables) {
"Error calling extension point 'HopGuiGetControlSpaceSortOrderPrefix'", e);
}

Arrays.sort(
variableNames,
(var1, var2) -> {
String str1 =
addPrefix(
var1,
systemProperties,
hopVariablesSet,
deprecatedSet,
hopSystemSettings,
pluginsPrefixesMap);
String str2 =
addPrefix(
var2,
systemProperties,
hopVariablesSet,
deprecatedSet,
hopSystemSettings,
pluginsPrefixesMap);
return str1.compareTo(str2);
});
Arrays.sort(variableNames);
return variableNames;
}

Expand Down

0 comments on commit 753004e

Please sign in to comment.