Commit 78712b7a by Facundo Javier Gelatti

Fix bugs in implementation of placeholder text. Extract methods to define a placeholder

1 parent 729048b2
package org.stegosuite.ui.gui;
import java.lang.reflect.Method;
import java.util.Objects;
import java.util.ResourceBundle;
import org.eclipse.swt.SWT;
......@@ -15,19 +17,7 @@ import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.*;
/**
* Contains methods for creating the GUI-elements and setting their default parameters.
......@@ -37,6 +27,9 @@ public class GuiComponents {
final static byte LOAD_MENU_ITEM = 0;
EmbedUi embedUi;
private final ResourceBundle L = ResourceBundle.getBundle("Messages");
public static final int EMPTY = 1;
public static final int NOT_EMPTY = 0;
public static final int PROCESSING = 2;
Shell createShell(Display display) {
Shell shell = new Shell(display);
......@@ -123,25 +116,9 @@ public class GuiComponents {
StyledText text = new StyledText(compositeControls, SWT.MULTI | SWT.V_SCROLL | SWT.WRAP | SWT.BORDER);
text.setAlwaysShowScrollBars(false);
text.setText(L.getString("message_text"));
text.setToolTipText(L.getString("message_text_tooltip"));
text.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_DARK_GRAY));
text.addListener(SWT.MouseDown, event -> {
if (text.getData() == null) {
text.setData(1);
text.setForeground(null);
text.setText("");
}
});
text.addListener(SWT.FocusOut, event -> {
if (text.getText().isEmpty()) {
text.setText(L.getString("message_text"));
text.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_DARK_GRAY));
text.setData(null);
}
});
setPlaceholder(text, L.getString("message_text"));
/*
* text.addListener(SWT.MouseDown, event -> { text.setText(""); });
......@@ -154,6 +131,59 @@ public class GuiComponents {
return compositeControls;
}
private void setPlaceholder(Scrollable text, String placeholder) {
text.setData(EMPTY);
Listener entryListener = event -> {
if (Objects.equals(text.getData(), EMPTY)) {
text.setData(PROCESSING);
setText(text, "");
text.setForeground(null);
text.setData(NOT_EMPTY);
}
};
Listener exitListener = event -> {
if (Objects.equals(text.getData(), NOT_EMPTY) && getText(text).isEmpty()) {
text.setData(PROCESSING);
setText(text, placeholder);
text.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_DARK_GRAY));
text.setData(EMPTY);
}
};
text.addListener(SWT.FocusIn, entryListener);
text.addListener(SWT.Verify, entryListener);
text.addListener(SWT.Modify, exitListener);
text.addListener(SWT.FocusOut, exitListener);
entryListener.handleEvent(null);
exitListener.handleEvent(null);
}
private void setText(Scrollable textField, String value) {
// Reflection was needed to call the method on instances of Text and StyledText
// (whose common ancestor class is Scrollable)
try {
Method setText = textField.getClass().getMethod("setText", String.class);
setText.invoke(textField, value);
} catch (Exception e) {
e.printStackTrace();
}
}
private String getText(Scrollable textField) {
// Reflection was needed to call the method on instances of Text and StyledText
// (whose common ancestor class is Scrollable)
try {
Method getText = textField.getClass().getMethod("getText");
return getText.invoke(textField).toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
Composite createFileEmbedding(Composite parent) {
Composite composite = new Composite(parent, SWT.NONE);
final GridData gridData2Columns = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
......@@ -236,26 +266,11 @@ public class GuiComponents {
// labelKey.setText("Secret key:");
// labelKey.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, true, false));
Text t2 = new Text(parent, SWT.SINGLE | SWT.BORDER);
t2.setText(L.getString("key_text"));
t2.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_DARK_GRAY));
t2.setToolTipText(L.getString("key_text_tooltip"));
t2.setLayoutData(data);
t2.addListener(SWT.MouseDown, event -> {
if (t2.getData() == null) {
t2.setData(1);
t2.setForeground(null);
t2.setText("");
}
});
Text txtPassword = new Text(parent, SWT.SINGLE | SWT.BORDER);
txtPassword.setToolTipText(L.getString("key_text_tooltip"));
txtPassword.setLayoutData(data);
t2.addListener(SWT.FocusOut, event -> {
if (t2.getText().isEmpty()) {
t2.setText(L.getString("key_text"));
t2.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_DARK_GRAY));
t2.setData(null);
}
});
setPlaceholder(txtPassword, L.getString("key_text"));
// GridLayout mGridLayoutEncryption = new GridLayout(1, true);
// mGridLayoutEncryption.verticalSpacing = 6;
......@@ -285,7 +300,7 @@ public class GuiComponents {
// }
// });
return t2;
return txtPassword;
}
Button createMainButton(Composite parent, String text) {
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!