How is it possible to display non-printable characters in an SWT StyledText widget in a way it's done by Swing with an empty square?
To replace all occurrences with a symbol via regex in the source string can't be the solution because copy & paste with the original codepoint won't be possible anymore.
The best way might be to intercept the text rendering and replace them there, but that seems to open Pandora's box...
Edit 1:
The control characters, it's all about, are characters that are normally just skipped and not shown by the editor like HOP (U+0081)
For an E4 RCP application I have attached some code to start with.
Basically is uses the IEventBroker to add/remove the painter of the TextViewer by means of a button.
The StyledText can be obtained from
styledText = tv.getTextWidget();
as commented above
import org.eclipse.jface.text.WhitespaceCharacterPainter;
public class TheEditor{
#Inject MPart thePart;
private WhitespaceCharacterPainter whitespaceCharacterPainter;
#PostConstruct
public void postConstruct(Composite parent) {
TextViewer tv = new TextViewer(parent, SWT.MULTI | SWT.V_SCROLL |SWT.H_SCROLL);
whitespaceCharacterPainter = new WhitespaceCharacterPainter(tv);
tv.addPainter(whitespaceCharacterPainter);
whitespaceCharacterPainter.deactivate(true);
}
#Inject
#Optional
public void updatePartByWSButton(#UIEventTopic(EventConstants.WHITE_CHARACTERS_STATUS) boolean newButtonStatus) {
final MElementContainer<MUIElement>container = thePart.getParent();
if (thePart.equals((MPart)container.getSelectedElement())){
wsToolBarButtonStatus = newButtonStatus;
if(wsToolBarButtonStatus){
this.whitespaceCharacterPainter.paint(IPainter.CONFIGURATION);
}
else{
whitespaceCharacterPainter.deactivate(true);
tv.removePainter(whitespaceCharacterPainter);
}
}
}
}
Handler Class
public class WhitespaceCharactersHandler {
boolean status;
#Execute
public void execute(final MToolItem item, IEventBroker broker) {
if (item.isSelected()){
status = true;
}
else{
status = false;
}
broker.post(EventConstants.WHITE_CHARACTERS_STATUS, status);
}
}
Contants interface:
public interface EventConstants {
String WHITE_CHARACTERS_STATUS = "WHITE-CHARACTERS-STATUS";
}
Related
I am working on eclipse JFace GUI stuff.
I am reading some input from the user and able to validate using the validator as below.
// creating input prompt
InputDialog inputDialog = new InputDialog(parentShell, dialogTitle, dialogMessage, initialValue, validator);
// Validator
IInputValidator validator = new IInputValidator()
{
#Override
public String isValid(String newName)
{
if (newName.isBlank())
{
return "Warning: Input is empty";
}
return null;
}
};
What I want to implement is to add a note to the user which is not related to validation.
Basically the note is about describing what happens if the button OK is pressed (As shown in image).
Any help/idea will be much appreciated.
You would have to create a new dialog class extending InputDialog to do this overriding createDialog to add extra controls. Something like:
public class InputDialogWithNote extends InputDialog
{
private Label noteLabel;
public InputDialogWithNote(final Shell parentShell, final String dialogTitle, final String dialogMessage, final String initialValue, final IInputValidator validator)
{
super(parentShell, dialogTitle, dialogMessage, initialValue, validator);
}
#Override
protected Control createDialogArea(final Composite parent)
{
Composite body = (Composite)super.createDialogArea(parent);
noteLabel = new Label(body, SWT.LEAD);
noteLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
return body;
}
}
You will have to arrange some way to set the noteLabel field.
My Setup
I have some code in an Eclipse RCP application that looks like this (it is in the #PostConstruct method of a Part):
scroll = new ScrolledComposite(parent, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
taskingInputsGroup = new Composite(scroll, SWT.NONE);
textSendTime = new Text(taskingInputsGroup, SWT.BORDER);
textSubject = new Text(taskingInputsGroup, SWT.BORDER);
textTaskStartTime = new Text(taskingInputsGroup, SWT.BORDER);
I'm trying to set a private Control field for an Enum's constants to each of these Text objects:
textSendTime = new Text(taskingInputsGroup, SWT.BORDER);
MsgField.SEND_TIME.setControl(textSendTime);
In the Enum, I just have a simple getter/setter for the Control field.
I have a method that is called when a user presses a Button. This method loops through the Enum constants and sets the text of some TreeItems to whatever is in the Control objects:
MsgField[] msgFields= MsgField.values();
for (int i = 0; i < msgFields.length; i++) {
Control control = msgFields[i].getControl();
if (control != null) {
if (control instanceof Text) {
root.getItem(i).getItem(0).setText(((Text)control).getText());
}
}
}
My Question
I am getting empty text from ((Text)control).getText() even if there is text in the Text field. Why could this be? I know I'm overlooking something simple (it's been a long day). I've read a bunch of SO posts on Java passing by value, but I can't seem to apply the answers to this issue. This works fine if I call getText() directly on the object in my view class:
root.getItem(1).getItem(0).setText(textSendTime.getText());
EDIT - Some MsgField enum code:
public enum MsgField {
private boolean isRequiredField;
private String treeName;
public abstract void setValue(Msg msg, String value);
public abstract Object getValue(Msg msg);
private MsgField(boolean req, String name) {
isRequiredField = req;
treeName = name;
}
SEND_TIME("Send Time", true) {
#Override
public void setValue(Msg msg, String value) {
msg.setSendTime(value);
}
#Override
public Object getValue(Msg msg) {
return msg.getSendTime();
}
},
// ....
// other fields
// ....
START_TIME("Start Time", false) {
#Override
public void setValue(Msg msg, String value) {
msg.setStartTime(value);
}
#Override
public Object getValue(Msg msg) {
return msg.getStartTime();
}
};
}
So the idea here is to represent the fields for the Msg class and have these get/set methods for each one so that I can easily iterate over them and get/set the fields for my Msg object. This worked fine (although maybe you can suggest a better alternative), but the trouble came with the addition of that Control field as I mentioned.
I have a TextArea that I am trying to restrict user inputs to allow only IP addresses format in that Area. So I thought of only allowing digits and decimal points. For multiple IPs, One IP per line, so the TextArea needs to accept new lines. For the most part what I have below is working except for delete. I can't delete any entry even if I am using the associate Unicode. I am running MAC OS 10, not sure if it makes any difference or not but the info is out there just in case.
public class RestrictIpInputTextArea extends TextArea {
#Override
public void replaceText(int i, int il, String string){
if(string.matches("[0-9_\\u000A_\\u232B_\\u0008_\\u2421_._\\u007F]") || string.isEmpty()){
super.replaceText(il, il, string);
}
}
#Override
public void replaceSelection(String string) {
super.replaceSelection(string);
}
I was able to find out the solution for it. Visit the link below for more reference.
http://docs.oracle.com/javafx/2/ui_controls/custom.htm
However, I still want to explore TextFormatter function deployed in JavaFX 8U40
public class numericValue extends TextArea {
#Override
public void replaceText(int start, int end, String text) {
String old = getText();
if (text.matches("[0-9_._\\u000A]*")) {
super.replaceText(start, end, text);
}
}
#Override
public void replaceSelection(String text) {
String old = getText();
if (text.matches("[0-9_._\\u000A]*")) {
super.replaceSelection(text);
}
}
}
I have JTextArea component and I need to disable modify\delete current content in component by users. Users may only add\insert some text at the end, but setText method must work as usual.
tnx
I need to disable modify\delete current content in component by users.
textArea.setEditable( false );
Users may only add\insert some text at the end, but setText method must work as usual.
You should have an "Add Text" button that will take text from a separate text field and then append the text to the Document using the append(...) method of the JTextArea.
Could you post an example of what you already have?
To clarify, if you want users to be unable to certain things, you may need to re-insert the original text manually. I'm unsure of the editor used by a JTextArea, but you could try overriding that.
Horrific code I'm coming up with on the spot incoming, you can probably do this much easier:
private static String mand = "mandatory.";
private static JTextArea test = new JTextArea(mand);
public static String getMand() {
return mand;
}
public static JTextArea getTest() {
return test;
}
public static void setMand(String mand2) {
mand = mand2;
}
public static void setTest(JTextArea test2) {
test = test2;
}
getTest().addKeyListener(new KeyListener() {
#Override
public void keyPressed(KeyEvent arg0) {
// do nothing
}
#Override
public void keyReleased(KeyEvent arg0) {
// do nothing
}
#Override
public void keyTyped(KeyEvent arg0) {
if(getTest().getText().startsWith(getMand())) {
System.out.println("good, text still present");
setMand(test.getText());
} else {
getTest().setText(getMand());
}
}
});
WARNING :: if the user makes any mistakes in adding information to the JTextArea, the code will not allow the user to fix these mistakes.
Tested successfully under JDK (/JRE) 7.
How is possible to identify if a selected code is a method, a function, a variable...???
public class Modifiers implements IObjectActionDelegate{
private Shell shell;
public void run(IAction action) {
SelectedText selectedText;
IEditorPart editor = getActiveEditor();
if (editor instanceof AbstractTextEditor) {
selectedText = getSelectedText(editor);
//HOW TO IDENTIFY THE SELECTED CODE
}
}
public void selectionChanged(IAction action, ISelection selection) {
}
public void setActivePart(IAction action, IWorkbenchPart targetPart) {
shell = targetPart.getSite().getShell();
}
private IEditorPart getActiveEditor() {
return Activator.getDefault().getWorkbench().getActiveWorkbenchWindow()
.getActivePage().getActiveEditor();
}
private SelectedText getSelectedText(IEditorPart editor) {
SelectedText selectedText;
try {
final ISelection selection = editor.getEditorSite().getSelectionProvider().getSelection();
final ITextSelection textSelection = (ITextSelection) selection;
selectedText = new SelectedText(textSelection.getText(), textSelection.getOffset(), textSelection.getLength());
} catch (Exception e) {
selectedText = new SelectedText("", 0, 0);
}
return selectedText;
}
}
As you can see I have the selected code in selectedText. Now I want to know is how can I identify if the code in that variable is a method, a variable or whatever it contains.
When doing Refactors with eclipse it shows the code information you have selected. The idea is to do something like that.
Thanks for your help.
If i am correct,you need to simply move your mouse pointer on the code about which you want to get details, it will display a pop up window containing the details with specific symbol such as for static variable it will display 's' in that symbol or icon.
See carefully each symbol containing the different 2 letter and color has its own meaning.
i.e. green for public, red for private and gray for local etc.