Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Looking for input masking and data validation widgets for SWT
|
Re: Looking for input masking and data validation widgets for SWT [message #451551 is a reply to message #451432] |
Wed, 02 March 2005 20:20 |
jmi Messages: 84 Registered: July 2009 |
Member |
|
|
Hi,
I've done my own for my application use because, i've got the same problem.
It's just what i need, it's not perfect but it is OK for me. There are some
limitations but ... it's perhaps a good starting point.
HTH
JMi
======================================================
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.jface.resource.ColorRegistry;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
/**
* Used to define a Mask Edit Text widget
*/
public class OMaskEdit extends OText {
/**
* Array representing the regex sequence string - ie: "[Z,z]", "[ ]",
* "[0-2]", "[-]", "[0-3]", "[/]", "[0-4]" and finally "[0-5]"
*/
protected String[] m_maskFlds;
/** Map of 4 separators - [/], [-], [:], [.], [,] and finally [ ] */
protected Map m_sepMap;
/** String for the template char, default is _ */
protected String m_templateChar = "_";
/** Caret position */
protected int m_caretPosition;
/** Flag to permit the verification or not */
protected boolean m_doVerifyText;
/**
* Flag to know if it is a keyEvent or not. It is used when doVerify
input
* text is processed.
*/
protected boolean m_isKeyEvent;
/** String Mask - ie: "[Z,z][ ][0-2][-][0-3][/][0-4][0-5]" */
private String m_mask;
/**
* Constructor
*
* Created 07-Nov-2004
*
* @param argParent
* Parent Composite
* @param argColorRegistry
* Application color registry
*/
public OMaskEdit(final Composite argParent,
final ColorRegistry argColorRegistry) {
this(argParent, SWT.SINGLE | SWT.WRAP | SWT.BORDER, true,
argColorRegistry);
}
/**
* Constructor
*
* Created 07-Nov-2004
*
* @param argParent
* Parent Composite
* @param argMandatory
* Mandotary or not
* @param argColorRegistry
* Application color registry
*/
public OMaskEdit(final Composite argParent, final boolean argMandatory,
final ColorRegistry argColorRegistry) {
this(argParent, SWT.SINGLE | SWT.WRAP | SWT.BORDER, argMandatory,
argColorRegistry);
}
/**
* Constructor
*
* Created 07-Nov-2004
*
* @param argParent
* Parent Composite
* @param argMandatory
* Mandotary or not
* @param argMask
* Mask to apply
* @param argColorRegistry
* Application color registry
*/
public OMaskEdit(final Composite argParent, final boolean argMandatory,
final String argMask, final ColorRegistry argColorRegistry) {
this(argParent, SWT.SINGLE | SWT.WRAP | SWT.BORDER, argMandatory,
argColorRegistry);
setMask(argMask);
}
/**
* Constructor
*
* Created 07-Nov-2004
*
* @param argParent
* Parent Composite
* @param argStyle
* Default Style
* @param argMandatory
* Mandotary or not
* @param argColorRegistry
* Application color registry
*/
protected OMaskEdit(final Composite argParent, final int argStyle,
final boolean argMandatory, final ColorRegistry
argColorRegistry) {
super(argParent, argStyle, argMandatory, argColorRegistry);
m_doVerifyText = true;
m_isKeyEvent = false;
// Fill the Map of separators
m_sepMap = new HashMap();
m_sepMap.put("[/]", "/");
m_sepMap.put("[-]", "-");
m_sepMap.put("[:]", ":");
//_sepMap.put("[.]", "."); // Pose un pbl lors du replaceAll
m_sepMap.put("[,]", ",");
m_sepMap.put("[ ]", " ");
// Colors
//_focusGainedBackGroundColor = new Color(null, 255, 204, 204);
//_focusLostBackGroundColor = new Color(null, 255, 255, 255);
// Define the control
//_textCtl = new Text(parent, style);
// Add the VerifyListener
addVerifyListener(new VerifyListener() {
public void verifyText(final VerifyEvent argEvent) {
if (m_doVerifyText) {
doVerifyText(argEvent);
} else {
m_doVerifyText = true;
argEvent.doit = true;
}
}
});
// Add the KeyListener to know if a key has been pressed or not
addKeyListener(new KeyListener() {
public void keyPressed(final KeyEvent argEvent) {
m_isKeyEvent = true;
}
public void keyReleased(final KeyEvent argEvent) {
m_isKeyEvent = false;
}
});
}
/**
* doVerifyText
*
* Created 07-Nov-2004
*
* Used to Verify the input text
*
* @param argEvent
* Verify Event
*/
protected void doVerifyText(final VerifyEvent argEvent) {
if ((!isEditable() && !m_isKeyEvent) || (isEditable() &&
m_isKeyEvent)
|| (isEditable() && !m_isKeyEvent)) {
argEvent.doit = false;
//final Text control = (Text) argEvent.widget;
//String currentText = control.getText();
String currentText = m_textCtl.getText();
final int currentTextLength = currentText.length();
// Delete from left to right
if (argEvent.keyCode == SWT.BS) {
m_caretPosition = argEvent.end;
if (m_caretPosition > 0) {
String part1 = currentText;
String part2 = currentText;
if (!m_sepMap.containsKey(m_maskFlds[m_caretPosition -
1])) {
part1 = part1.substring(0, m_caretPosition - 1);
part2 = part2.substring(m_caretPosition,
currentTextLength);
m_caretPosition = m_caretPosition - 1;
} else {
// It is a separator
if (m_caretPosition > 1) {
part1 = part1.substring(0, m_caretPosition - 2);
part2 = part2.substring(m_caretPosition - 1,
currentTextLength);
m_caretPosition = m_caretPosition - 2;
}
}
currentText = part1 + m_templateChar + part2;
m_doVerifyText = false;
// control.setText(currentText);
// control.setSelection(m_caretPosition);
m_textCtl.setText(currentText);
m_textCtl.setSelection(m_caretPosition);
}
argEvent.doit = false;
return;
}
//Delete from right to left
if (argEvent.keyCode == SWT.DEL) {
m_caretPosition = argEvent.end;
if (m_caretPosition >= 0) {
String part1 = currentText;
String part2 = currentText;
if ((m_caretPosition < m_maskFlds.length)
&& (!m_sepMap
.containsKey(m_maskFlds[m_caretPosition])))
{
part1 = part1.substring(0, m_caretPosition - 1);
part2 = part2.substring(m_caretPosition,
currentTextLength);
} else {
// It is a separator
part1 = part1.substring(0, m_caretPosition - 1);
part2 = part2.substring(m_caretPosition,
currentTextLength);
m_caretPosition = m_caretPosition + 1;
}
currentText = part1 + m_templateChar + part2;
m_doVerifyText = false;
// control.setText(currentText);
// control.setSelection(m_caretPosition);
m_textCtl.setText(currentText);
m_textCtl.setSelection(m_caretPosition);
}
argEvent.doit = false;
return;
}
try {
m_caretPosition = m_textCtl.getCaretPosition();
String newInputText = argEvent.text;
newInputText = newInputText.toUpperCase();
final int newInputTextLength = newInputText.length();
if ((newInputTextLength == 1) && m_isKeyEvent) {
if (m_caretPosition < m_maskFlds.length) {
// Only 1 char -> key typed
final Pattern pattern = Pattern
.compile(m_maskFlds[argEvent.end]);
final Matcher matcher =
pattern.matcher(newInputText);
// Fill the rest of the string
for (int i = argEvent.end; i < m_maskFlds.length;
i++) {
if (i > argEvent.end) {
if (m_sepMap.containsKey(m_maskFlds[i])) {
newInputText = newInputText
+ (String) m_sepMap
.get(m_maskFlds[i]);
} else {
newInputText = newInputText
+ m_templateChar;
}
}
}
if (matcher.find()) {
if ((m_caretPosition + 1 < m_maskFlds.length)
&& (m_sepMap
.containsKey(m_maskFlds[m_caretPosition
+ 1]))) {
m_caretPosition = m_caretPosition + 1;
}
m_caretPosition = m_caretPosition + 1;
currentText = currentText
.substring(0, argEvent.end)
+ newInputText;
m_doVerifyText = false;
// control.setText(currentText);
// control.setSelection(m_caretPosition);
m_textCtl.setText(currentText);
m_textCtl.setSelection(m_caretPosition);
argEvent.doit = false;
} else {
argEvent.doit = false;
}
}
} else {
// More than 1 char -> at least 2 char - ie: Z10
Pattern pattern = null;
Matcher matcher = null;
String textFormatted = "";
int count = -1;
for (int i = 0; i < m_maskFlds.length; i++) {
if (m_sepMap.containsKey(m_maskFlds[i])) {
// If it is a separator -> add separator
textFormatted = textFormatted
+ (String) m_sepMap.get(m_maskFlds[i]);
} else {
// Else if it is not a separator...
count++;
if (count < newInputTextLength) {
final String strToVerify = newInputText
.substring(count, count + 1);
pattern = Pattern.compile(m_maskFlds[i]);
matcher = pattern.matcher(strToVerify);
if (matcher.find()) {
textFormatted = textFormatted +
strToVerify;
m_doVerifyText = false;
// control.setText(textFormatted);
// control.setSelection(m_caretPosition);
m_textCtl.setText(textFormatted);
m_textCtl.setSelection(m_caretPosition);
argEvent.doit = false;
} else {
argEvent.doit = false;
return;
}
} else {
// The string to insert is smaller than the
mask
// so i insert the m_templateChar - ie: Z100
textFormatted = textFormatted +
m_templateChar;
m_doVerifyText = false;
// control.setText(textFormatted);
// control.setSelection(m_caretPosition);
m_textCtl.setText(textFormatted);
m_textCtl.setSelection(m_caretPosition);
argEvent.doit = false;
}
}
}
}
} catch (final Exception argex) {
System.out.println("OMaskEdit - doVerifyText(...) - ERROR :
"
+ argex.getMessage());
}
}
}
/**
*
* setMask
*
* Created 17-nov.-2004
*
* Used to set the mask string ie: [Z,z][ ][0-2][-][0-3][/][0-4][0-5]
The
* text is reseted/it is set to blank/empty field
*
* @param argMask
* Mask string
*/
public void setMask(final String argMask) {
setMask(argMask, true);
}
/**
*
* setMask
*
* Created 17-nov.-2004
*
* Used to set the mask string ie: [Z,z][ ][0-2][-][0-3][/][0-4][0-5]
*
* @param argMask
* Mask string
* @param argResetText
* Flag to reset text or not
*/
public void setMask(final String argMask, final boolean argResetText) {
m_mask = argMask;
convertMaskToArray(argMask, argResetText);
}
/**
*
* convertMaskToArray
*
* Created 17-nov.-2004
*
* Used to split the mask into an array of string. Each element of the
array
* represents a char
*
* @param argMask
* Mask string
* @param argResetText
* Flag to reset text or not
*/
private void convertMaskToArray(final String argMask,
final boolean argResetText) {
// Split the mask string into regex elements
final Pattern pattern = Pattern.compile("]",
Pattern.CASE_INSENSITIVE);
m_maskFlds = pattern.split(argMask);
for (int i = 0; i < m_maskFlds.length; i++) {
m_maskFlds[i] = m_maskFlds[i] + ']';
//System.out.println(" field[" + i + "] = " + flds[i]);
}
if (argResetText) {
setText(""); // Used to display the mask
}
}
// TODO voir si besoin d'activer. Attention, lorsque je tape je dois
// replacer au debut
// /**
// * setText
// *
// * Created 16-Nov-2004
// *
// * Used to set the input of the widget
// *
// * @param argString
// * the new text
// */
// public void setText(String argString) {
// super.setText(argString);
// m_textCtl.setSelection(0, m_maskFlds.length);
// }
/**
*
* getText
*
* Created 17-nov.-2004
*
* Used to get the text with the mask as it is displayed
*
* @return the text as it is displayed
*/
public String getText() {
return getText(true);
}
/**
*
* getText
*
* Created 17-nov.-2004
*
* Used to get the text with/without the mask
*
* @param argMask
* Flag to add the mask or not
* @return the text with/without the mask according to argMask
*/
public String getText(final boolean argMask) {
String retVal = "";
if (argMask) {
// displayed text with mask
retVal = m_textCtl.getText();
} else {
// there is no mask, so i have to remove the separator
retVal = m_textCtl.getText();
final Iterator it = m_sepMap.values().iterator();
while (it.hasNext()) {
retVal = retVal.replaceAll((String) it.next(), "");
}
retVal = retVal.replaceAll(m_templateChar, "");
}
return retVal;
}
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
final GridLayout gridLayout = new GridLayout();
shell.setLayout(gridLayout);
// Mask Label
final Label label1 = new Label(shell, SWT.NONE);
label1.setText("Mask = [Z,z][ ][0-2][-][0-3][/][0-4][0-5]");
GridData data = new GridData();
data.verticalAlignment = GridData.BEGINNING;
label1.setLayoutData(data);
final Label label2 = new Label(shell, SWT.NONE);
label2.setText("So you can enter Z1001 but not Z3001 or A1001");
data = new GridData();
data.verticalAlignment = GridData.BEGINNING;
label2.setLayoutData(data);
// MaskEdit control
final OMaskEdit maskEditText = new OMaskEdit(shell, null);
maskEditText.setMask("[Z,z][ ][0-2][-][0-3][/][0-4][0-5]");
data = new GridData();
data.verticalAlignment = GridData.BEGINNING;
maskEditText.setLayoutData(data);
//maskEditText.setEditable(false);
maskEditText.setText("Z1001");
//maskEditText.setText("Z21");
//maskEditText.setText("Z2115");
//maskEditText.setText("");
final Button btn = new Button(shell, SWT.PUSH);
btn.setText("Button");
btn.setSize(100, 20);
data = new GridData();
data.verticalAlignment = GridData.BEGINNING;
btn.setLayoutData(data);
final Label label3 = new Label(shell, SWT.SINGLE | SWT.WRAP
| SWT.BORDER);
label3.setText("................................................... ");
data = new GridData();
data.verticalAlignment = GridData.BEGINNING;
label3.setLayoutData(data);
btn.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent argE) {
System.out.println("Btn was clicked...");
System.out.println("Btn was clicked..."
+ maskEditText.getText());
System.out.println("Btn was clicked..."
+ maskEditText.getText(false));
//label3.setText(maskEditText.getText());
label3.setText(maskEditText.getText() + "\t"
+ maskEditText.getText(false));
}
});
// shell.addListener(SWT.Close, new Listener() {
// public void handleEvent(Event e) {
// System.out.println("Ok button tapped");
//
// label3.setText(maskEditText.getText() + "\t" +
// maskEditText.getText(false));
// }
// });
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
=============================================
import org.eclipse.jface.resource.ColorRegistry;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.HelpListener;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.MouseTrackListener;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.events.TraverseListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
/**
* Used to define a Text widget which can be extended
*
* It requires some colors in a org.eclipse.jface.resource.ColorRegistry
*
* (1) editMandatoryBackGroundColor (2) editDefaultBackGroundColor (3)
* readOnlyDefaultBackGroundColor (4) readOnlyDefaultForeGroundColor (5)
* readOnlyHighlightedForeGroundColor (6) editForeGroundColor
*
*/
public class OText extends Composite {
/** Text control */
protected Text m_textCtl;
/**
* Flag mandatory field. If true, the background is in a given color. If
* false, another color is used for the background
*/
protected boolean m_mandatory;
/** Flag to set foreground in a highlighted color */
protected boolean m_highlighted;
/** The color registry */
protected ColorRegistry m_colorRegistry;
/** Control edit Mandatory BackGround Color */
protected Color m_editMandatoryBackGroundColor;
/** Control edit Default BackGround Color */
protected Color m_editDefaultBackGroundColor;
/** Control readOnly Default BackGround Color */
protected Color m_readOnlyDefaultBackGroundColor;
/** Control readOnly Default ForeGround Color */
protected Color m_readOnlyDefaultForeGroundColor;
/** Control readOnly Highlighted ForeGround Color */
protected Color m_readOnlyHighlightedForeGroundColor;
/** Control edit ForeGround Color */
protected Color m_editForeGroundColor;
/**
* Constructor
*
* @param argParent
* Parent Composite
* @param argColorRegistry
* Application color registry
*/
public OText(final Composite argParent, final ColorRegistry
argColorRegistry) {
this(argParent, SWT.SINGLE | SWT.WRAP | SWT.BORDER, true,
argColorRegistry);
}
/**
* Constructor
*
* Created 17-nov.-2004
*
* @param argParent
* Parent Composite
* @param argMandatory
* Mandatory or not
* @param argColorRegistry
* Application color registry
*/
public OText(final Composite argParent, final boolean argMandatory,
final ColorRegistry argColorRegistry) {
this(argParent, SWT.SINGLE | SWT.WRAP | SWT.BORDER, argMandatory,
argColorRegistry);
}
/**
* Constructor - Always Mandatory
*
* Created 17-nov.-2004
*
* @param argParent
* Parent Composite
* @param argStyle
* Widget Style
* @param argColorRegistry
* Application color registry
*/
public OText(final Composite argParent, final int argStyle,
final ColorRegistry argColorRegistry) {
this(argParent, argStyle, true, argColorRegistry);
}
/**
* Constructor
*
* Created 17-nov.-2004
*
* @param argParent
* Parent Composite
* @param argStyle
* Widget Style
* @param argMandatory
* Mandatory or not
* @param argColorRegistry
* Application color registry
*/
public OText(final Composite argParent, final int argStyle,
final boolean argMandatory, final ColorRegistry
argColorRegistry) {
super(argParent, SWT.NONE);
m_mandatory = argMandatory;
m_highlighted = false;
m_colorRegistry = argColorRegistry;
// Define the control
m_textCtl = new Text(this, argStyle);
if ((argColorRegistry != null)
&& (argColorRegistry.get("editMandatoryBackGroundColor") !=
null)
&& (argColorRegistry.get("editDefaultBackGroundColor") !=
null)
&& (argColorRegistry.get("readOnlyDefaultBackGroundColor")
!= null)
&& (argColorRegistry.get("readOnlyDefaultForeGroundColor")
!= null)
&&
(argColorRegistry.get("readOnlyHighlightedForeGroundColor") != null)
&& (argColorRegistry.get("editForeGroundColor") != null)) {
// Set the color's variables
m_editMandatoryBackGroundColor = argColorRegistry
.get("editMandatoryBackGroundColor");
m_editDefaultBackGroundColor = argColorRegistry
.get("editDefaultBackGroundColor");
m_readOnlyDefaultBackGroundColor = argColorRegistry
.get("readOnlyDefaultBackGroundColor");
m_readOnlyDefaultForeGroundColor = argColorRegistry
.get("readOnlyDefaultForeGroundColor");
m_readOnlyHighlightedForeGroundColor = argColorRegistry
.get("readOnlyHighlightedForeGroundColor");
m_editForeGroundColor =
argColorRegistry.get("editForeGroundColor");
// Initialize the default colors
m_textCtl.setBackground(m_readOnlyDefaultBackGroundColor);
if (m_highlighted) {
m_textCtl.setForeground(m_readOnlyHighlightedForeGroundColor );
} else {
m_textCtl.setForeground(m_readOnlyDefaultForeGroundColor);
}
}
// Add the FocusListener for color
m_textCtl.addFocusListener(new FocusAdapter() {
public void focusGained(final FocusEvent argE) {
doFocusGained(argE);
}
public void focusLost(final FocusEvent argE) {
doFocusLost(argE);
}
});
// Perhaps a bug...
addListener(SWT.Dispose, new Listener() {
public void handleEvent(final Event argEvent) {
widgetDisposed();
}
});
addListener(SWT.Resize, new Listener() {
public void handleEvent(final Event argEvent) {
widgetResize();
}
});
addListener(SWT.FocusIn, new Listener() {
public void handleEvent(final Event argEvent) {
m_textCtl.setFocus();
}
});
}
/**
*
* doFocusGained
*
* Created 17-nov.-2004
*
* Used to set the background color according to the m_mandatory flag
*
* @param argEvent
* FocusEvent of the source
*/
protected void doFocusGained(final FocusEvent argEvent) {
if ((m_colorRegistry != null) && isEditable()) {
if (m_mandatory) {
m_textCtl.setBackground(m_editMandatoryBackGroundColor);
} else {
m_textCtl.setBackground(m_editDefaultBackGroundColor);
}
m_textCtl.setForeground(m_editForeGroundColor);
}
// if(isEditable()) {
// m_textCtl.traverse(SWT.TRAVERSE_TAB_NEXT);
// }
}
/**
*
* doFocusLost
*
* Created 17-nov.-2004
*
* Used to set the background color of the widget to white when the
focus is
* lost
*
* @param argEvent
* FocusEvent of the source
*/
protected void doFocusLost(final FocusEvent argEvent) {
if (m_colorRegistry != null) {
if (isEditable()) {
m_textCtl.setBackground(m_readOnlyDefaultBackGroundColor);
m_textCtl.setForeground(m_editForeGroundColor);
} else {// ReadOnly
m_textCtl.setBackground(m_readOnlyDefaultBackGroundColor);
if (m_highlighted) {
m_textCtl
.setForeground(m_readOnlyHighlightedForeGroundColor);
} else {
m_textCtl.setForeground(m_readOnlyDefaultForeGroundColor);
}
}
}
}
/**
*
* widgetDisposed
*
* Created 17-nov.-2004
*
* Used to dispose the created resources
*
*/
private void widgetDisposed() {
//m_focusGainedMandatoryBackGroundColor.dispose();
//m_focusGainedNotMandatoryBackGroundColor.dispose();
// The next color must not be dispose because it is a SystemColor
//m_focusLostBackGroundColor.dispose();
}
/**
*
* widgetResize
*
* Created 17-nov.-2004
*
* Used to resize and repaint the widget according to the client area.
*
* There is a workaround because of the input string not fully shown
when
* redraw. In fact it seems that the redraw doesn't work correctly.
*
*
============================================================ ============
* veronika_irvine@ca.ibm.com
*
* This is a bug in SWT. I have entered the following bug report:
* https://bugs.eclipse.org/bugs/show_bug.cgi?id=78943 As a workaround,
* instead of calling: _textCtl.setText(_textCtl.getText()); try:
* _textCtl.setSelection(_textCtl.getSelection());
*
*/
private void widgetResize() {
final Rectangle area = getClientArea();
m_textCtl.setBounds(0, 0, area.width, area.height);
// TODO bug in SWT - verifier sur
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=78943
//m_textCtl.setText(m_textCtl.getText());
m_textCtl.setSelection(m_textCtl.getSelection());
// Normaly the next condition is always false
// So it will never happen
// if (m_textCtl.isFocusControl()) {
// m_textCtl.setSelection(1,
m_textCtl.getText().length());
// }
}
/**
* Gets the editable state.
*
* @return whether or not the reciever is editable
*/
public boolean isEditable() {
return getEditable();
}
/*********************************************************** ***************
************************************************************ *************/
/* FROM Text */
/**
* Adds the listener to the collection of listeners who will be notified
* when the receiver's text is modified, by sending it one of the
messages
* defined in the <code>ModifyListener</code> interface.
*
* @param argListener
* the listener which should be notified
*
* @exception IllegalArgumentException
* <ul>
* <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
* </ul>
*
* @see ModifyListener
* @see #removeModifyListener
*/
public void addModifyListener(final ModifyListener argListener) {
m_textCtl.addModifyListener(argListener);
}
/**
* Adds the listener to the collection of listeners who will be notified
* when the control is selected, by sending it one of the messages
defined
* in the <code>SelectionListener</code> interface.
* <p>
* <code>widgetSelected</code> is not called for texts.
* <code>widgetDefaultSelected</code> is typically called when ENTER is
* pressed in a single-line text.
* </p>
*
* @param argListener
* the listener which should be notified
*
* @exception IllegalArgumentException
* <ul>
* <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
* </ul>
*
* @see SelectionListener
* @see #removeSelectionListener
* @see SelectionEvent
*/
public void addSelectionListener(final SelectionListener argListener) {
m_textCtl.addSelectionListener(argListener);
}
/**
* Adds the listener to the collection of listeners who will be notified
* when the receiver's text is verified, by sending it one of the
messages
* defined in the <code>VerifyListener</code> interface.
*
* @param argListener
* the listener which should be notified
*
* @exception IllegalArgumentException
* <ul>
* <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
* </ul>
*
* @see VerifyListener
* @see #removeVerifyListener
*/
public void addVerifyListener(final VerifyListener argListener) {
m_textCtl.addVerifyListener(argListener);
}
/**
* Appends a string.
* <p>
* The new text is appended to the text at the end of the widget.
* </p>
*
* @param argString
* the string to be appended
*
* @exception IllegalArgumentException
* <ul>
* <li>ERROR_NULL_ARGUMENT - if the string is null</li>
* </ul>
*/
public void append(final String argString) {
m_textCtl.append(argString);
}
/**
* Clears the selection.
*/
public void clearSelection() {
m_textCtl.clearSelection();
}
/**
* Copies the selected text.
* <p>
* The current selection is copied to the clipboard.
* </p>
*/
public void copy() {
m_textCtl.copy();
}
/**
* Cuts the selected text.
* <p>
* The current selection is first copied to the clipboard and then
deleted
* from the widget.
* </p>
*/
public void cut() {
m_textCtl.cut();
}
/**
* Gets the line number of the caret.
* <p>
* The line number of the caret is returned.
* </p>
*
* @return the line number
*/
public int getCaretLineNumber() {
return m_textCtl.getCaretLineNumber();
}
/**
* Gets the location the caret.
* <p>
* The location of the caret is returned.
* </p>
*
* @return a point, the location of the caret
*/
public Point getCaretLocation() {
return m_textCtl.getCaretLocation();
}
/**
* Gets the position of the caret.
* <p>
* The character position of the caret is returned.
* </p>
*
* @return the position of the caret
*/
public int getCaretPosition() {
return m_textCtl.getCaretPosition();
}
/**
* Gets the number of characters.
*
* @return number of characters in the widget
*/
public int getCharCount() {
return m_textCtl.getCharCount();
}
/**
* Gets the double click enabled flag.
* <p>
* The double click flag enables or disables the default action of the
text
* widget when the user double clicks.
* </p>
*
* @return whether or not double click is enabled
*/
public boolean getDoubleClickEnabled() {
return m_textCtl.getDoubleClickEnabled();
}
/**
* Gets the echo character.
* <p>
* The echo character is the character that is displayed when the user
* enters text or the text is changed by the programmer.
* </p>
*
* @return the echo character
*/
public char getEchoChar() {
return m_textCtl.getEchoChar();
}
/**
* Gets the editable state.
*
* @return whether or not the reciever is editable
*/
public boolean getEditable() {
return m_textCtl.getEditable();
}
/**
* Gets the number of lines.
*
* @return the number of lines in the widget
*/
public int getLineCount() {
return m_textCtl.getLineCount();
}
/**
* Gets the line delimiter.
*
* @return a string that is the line delimiter
*/
public String getLineDelimiter() {
return m_textCtl.getLineDelimiter();
}
/**
* Gets the height of a line.
*
* @return the height of a row of text
*/
public int getLineHeight() {
return m_textCtl.getLineHeight();
}
/**
* Returns the orientation of the receiver.
*
* @return the orientation style
*/
public int getOrientation() {
return m_textCtl.getOrientation();
}
/**
* Gets the position of the selected text.
* <p>
* Indexing is zero based. The range of a selection is from 0..N where N
is
* the number of characters in the widget.
* </p>
*
* @return the start and end of the selection
*/
public Point getSelection() {
return m_textCtl.getSelection();
}
/**
* Gets the number of selected characters.
*
* @return the number of selected characters.
*/
public int getSelectionCount() {
return m_textCtl.getSelectionCount();
}
/**
* Gets the selected text.
*
* @return the selected text
*/
public String getSelectionText() {
return m_textCtl.getSelectionText();
}
/**
* Gets the number of tabs.
* <p>
* Tab stop spacing is specified in terms of the space (' ') character.
The
* width of a single tab stop is the pixel width of the spaces.
* </p>
*
* @return the number of tab characters
*/
public int getTabs() {
return m_textCtl.getTabs();
}
/**
* Gets the widget text.
* <p>
* The text for a text widget is the characters in the widget.
* </p>
*
* @return the widget text
*/
public String getText() {
return m_textCtl.getText().trim();
}
/**
* Gets a range of text. Returns an empty string if the start of the
range
* is greater than the end.
* <p>
* Indexing is zero based. The range of a selection is from 0..N-1 where
N
* is the number of characters in the widget.
* </p>
*
* @param argStart
* the start of the range
* @param argEnd
* the end of the range
* @return the range of text
*/
public String getText(final int argStart, final int argEnd) {
return m_textCtl.getText(argStart, argEnd).trim();
}
/**
* Returns the maximum number of characters that the receiver is capable
of
* holding.
* <p>
* If this has not been changed by <code>setTextLimit()</code>, it will
* be the constant <code>Text.LIMIT</code>.
* </p>
*
* @return the text limit
*/
public int getTextLimit() {
return m_textCtl.getTextLimit();
}
/**
* Returns the zero-relative index of the line which is currently at the
top
* of the receiver.
* <p>
* This index can change when lines are scrolled or new lines are added
or
* removed.
* </p>
*
* @return the index of the top line
*/
public int getTopIndex() {
return m_textCtl.getTopIndex();
}
/**
* Gets the top pixel.
* <p>
* The top pixel is the pixel position of the line that is currently at
the
* top of the widget. On some platforms, a text widget can be scrolled
by
* pixels instead of lines so that a partial line is displayed at the
top of
* the widget.
* </p>
* <p>
* The top pixel changes when the widget is scrolled. The top pixel does
not
* include the widget trimming.
* </p>
*
* @return the pixel position of the top line
*/
public int getTopPixel() {
return m_textCtl.getTopPixel();
}
/**
* Inserts a string.
* <p>
* The old selection is replaced with the new text.
* </p>
*
* @param argString
* the string
*/
public void insert(final String argString) {
m_textCtl.insert(argString);
}
/**
* Pastes text from clipboard.
* <p>
* The selected text is deleted from the widget and new text inserted
from
* the clipboard.
* </p>
*/
public void paste() {
m_textCtl.paste();
}
/**
* Removes the listener from the collection of listeners who will be
* notified when the receiver's text is modified.
*
* @param argListener
* the listener which should no longer be notified
*
* @exception IllegalArgumentException
* <ul>
* <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
* </ul>
*
* @see ModifyListener
* @see #addModifyListener
*/
public void removeModifyListener(final ModifyListener argListener) {
m_textCtl.removeModifyListener(argListener);
}
/**
* Removes the listener from the collection of listeners who will be
* notified when the control is selected.
*
* @param argListener
* the listener which should be notified
*
* @exception IllegalArgumentException
* <ul>
* <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
* </ul>
*
* @see SelectionListener
* @see #addSelectionListener
*/
public void removeSelectionListener(final SelectionListener argListener)
{
m_textCtl.removeSelectionListener(argListener);
}
/**
* Removes the listener from the collection of listeners who will be
* notified when the control is verified.
*
* @param argListener
* the listener which should be notified
*
* @exception IllegalArgumentException
* <ul>
* <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
* </ul>
*
* @see VerifyListener
* @see #addVerifyListener
*/
public void removeVerifyListener(final VerifyListener argListener) {
m_textCtl.removeVerifyListener(argListener);
}
/**
* Selects all the text in the receiver.
*/
public void selectAll() {
m_textCtl.selectAll();
}
/**
* Sets the double click enabled flag.
* <p>
* The double click flag enables or disables the default action of the
text
* widget when the user double clicks.
* </p>
*
* @param argDoubleClick
* the new double click flag
*/
public void setDoubleClickEnabled(final boolean argDoubleClick) {
m_textCtl.setDoubleClickEnabled(argDoubleClick);
}
/**
* Sets the echo character.
* <p>
* The echo character is the character that is displayed when the user
* enters text or the text is changed by the programmer. Setting the
echo
* character to '\0' clears the echo character and redraws the original
* text. If for any reason the echo character is invalid, the default
echo
* character for the platform is used.
* </p>
*
* @param argEcho
* the new echo character
*/
public void setEchoChar(final char argEcho) {
m_textCtl.setEchoChar(argEcho);
}
/**
* Sets the editable state.
*
* @param argEditable
* the new editable state
*/
public void setEditable(final boolean argEditable) {
m_textCtl.setEditable(argEditable);
// Colors
if (m_colorRegistry != null) {
if (isEditable()) {
m_textCtl.setBackground(m_readOnlyDefaultBackGroundColor);
m_textCtl.setForeground(m_editForeGroundColor);
} else {// ReadOnly
m_textCtl.setBackground(m_readOnlyDefaultBackGroundColor);
if (m_highlighted) {
m_textCtl
.setForeground(m_readOnlyHighlightedForeGroundColor);
} else {
m_textCtl.setForeground(m_readOnlyDefaultForeGroundColor);
}
}
}
}
/**
* Sets the mandatory state.
*
* @param argMandatory
* Mandatory or not
*/
public void setMandatory(final boolean argMandatory) {
m_mandatory = argMandatory;
}
/**
* Sets the orientation of the receiver, which must be one of the
constants
* <code>SWT.LEFT_TO_RIGHT</code> or <code>SWT.RIGHT_TO_LEFT</code>.
* <p>
*
* @param argOrientation
* new orientation style
*/
public void setOrientation(final int argOrientation) {
m_textCtl.setOrientation(argOrientation);
}
/**
* Sets the selection.
* <p>
* Indexing is zero based. The range of a selection is from 0..N where N
is
* the number of characters in the widget.
* </p>
* <p>
* Text selections are specified in terms of caret positions. In a text
* widget that contains N characters, there are N+1 caret positions,
ranging
* from 0..N. This differs from other functions that address character
* position such as getText () that use the regular array indexing
rules.
* </p>
*
* @param argStart
* new caret position
*/
public void setSelection(final int argStart) {
m_textCtl.setSelection(argStart);
}
/**
* Sets the selection.
* <p>
* Indexing is zero based. The range of a selection is from 0..N where N
is
* the number of characters in the widget.
* </p>
* <p>
* Text selections are specified in terms of caret positions. In a text
* widget that contains N characters, there are N+1 caret positions,
ranging
* from 0..N. This differs from other functions that address character
* position such as getText () that use the usual array indexing rules.
* </p>
*
* @param argStart
* the start of the range
* @param argEnd
* the end of the range
*/
public void setSelection(final int argStart, final int argEnd) {
m_textCtl.setSelection(argStart, argEnd);
}
/**
* Sets the selection.
* <p>
* Indexing is zero based. The range of a selection is from 0..N where N
is
* the number of characters in the widget.
* </p>
* <p>
* Text selections are specified in terms of caret positions. In a text
* widget that contains N characters, there are N+1 caret positions,
ranging
* from 0..N. This differs from other functions that address character
* position such as getText () that use the usual array indexing rules.
* </p>
*
* @param argSelection
* the point
*
* @exception IllegalArgumentException
* <ul>
* <li>ERROR_NULL_ARGUMENT - if the point is null</li>
* </ul>
*/
public void setSelection(final Point argSelection) {
m_textCtl.setSelection(argSelection);
}
/**
* Sets the number of tabs.
* <p>
* Tab stop spacing is specified in terms of the space (' ') character.
The
* width of a single tab stop is the pixel width of the spaces.
* </p>
*
* @param argTabs
* the number of tabs
*
* </ul>
*/
public void setTabs(final int argTabs) {
m_textCtl.setTabs(argTabs);
}
/**
* Sets the contents of the receiver to the given string. If the
receiver
* has style SINGLE and the argument contains multiple lines of text,
the
* result of this operation is undefined and may vary from platform to
* platform.
*
* @param argString
* the new text
*
* @exception IllegalArgumentException
* <ul>
* <li>ERROR_NULL_ARGUMENT - if the string is null</li>
* </ul>
*/
public void setText(final String argString) {
m_textCtl.setText(argString);
// Put the caret at the beginning of the string
if ((argString != null) && (!argString.equals(""))) {
m_textCtl.setSelection(0);
}
}
/**
* Sets the maximum number of characters that the receiver is capable of
* holding to be the argument.
* <p>
* Instead of trying to set the text limit to zero, consider creating a
* read-only text widget.
* </p>
* <p>
* To reset this value to the default, use
* <code>setTextLimit(Text.LIMIT)</code>.
* </p>
*
* @param argLimit
* new text limit
*
* @exception IllegalArgumentException
* <ul>
* <li>ERROR_CANNOT_BE_ZERO - if the limit is zero</li>
* </ul>
*/
public void setTextLimit(final int argLimit) {
m_textCtl.setTextLimit(argLimit);
}
/**
* Sets the zero-relative index of the line which is currently at the
top of
* the receiver. This index can change when lines are scrolled or new
lines
* are added and removed.
*
* @param argIndex
* the index of the top item
*/
public void setTopIndex(final int argIndex) {
m_textCtl.setTopIndex(argIndex);
}
/**
* Shows the selection.
* <p>
* If the selection is already showing in the receiver, this method
simply
* returns. Otherwise, lines are scrolled until the selection is
visible.
* </p>
*/
public void showSelection() {
m_textCtl.showSelection();
}
/* FROM Scrollable */
/**
* Given a desired <em>client area</em> for the receiver (as described
by
* the arguments), returns the bounding rectangle which would be
required to
* produce that client area.
* <p>
* In other words, it returns a rectangle such that, if the receiver's
* bounds were set to that rectangle, the area of the receiver which is
* capable of displaying data (that is, not covered by the "trimmings")
* would be the rectangle described by the arguments (relative to the
* receiver's parent).
* </p>
*
* @param argX
* the desired x coordinate of the client area
* @param argY
* the desired y coordinate of the client area
* @param argWidth
* the desired width of the client area
* @param argHeight
* the desired height of the client area
* @return the required bounds to produce the given client area
*
* @see #getClientArea
*/
public Rectangle computeTrim(final int argX, final int argY,
final int argWidth, final int argHeight) {
return m_textCtl.computeTrim(argX, argY, argWidth, argHeight);
}
/* FROM Control */
/**
* Adds the listener to the collection of listeners who will be notified
* when the control
|
|
|
Re: Looking for input masking and data validation widgets for SWT [message #451552 is a reply to message #451551] |
Wed, 02 March 2005 20:22 |
jmi Messages: 84 Registered: July 2009 |
Member |
|
|
Of course, if somebody use it and improve it, please post enhancement and
bug correction.... ;-)))
> Hi,
>
> I've done my own for my application use because, i've got the same
> problem. It's just what i need, it's not perfect but it is OK for me.
> There are some limitations but ... it's perhaps a good starting point.
>
> HTH
>
> JMi
>
>
>
> ======================================================
> import java.util.Map;
> import java.util.HashMap;
> import java.util.Iterator;
> import java.util.regex.Matcher;
> import java.util.regex.Pattern;
>
> import org.eclipse.jface.resource.ColorRegistry;
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.events.VerifyEvent;
> import org.eclipse.swt.events.VerifyListener;
> import org.eclipse.swt.events.KeyListener;
> import org.eclipse.swt.events.KeyEvent;
> import org.eclipse.swt.layout.GridData;
> import org.eclipse.swt.layout.GridLayout;
> import org.eclipse.swt.widgets.Composite;
> import org.eclipse.swt.widgets.Display;
> import org.eclipse.swt.widgets.Text;
> import org.eclipse.swt.widgets.Shell;
> import org.eclipse.swt.widgets.Label;
> import org.eclipse.swt.widgets.Button;
> import org.eclipse.swt.events.SelectionAdapter;
> import org.eclipse.swt.events.SelectionEvent;
>
>
> /**
> * Used to define a Mask Edit Text widget
> */
> public class OMaskEdit extends OText {
> /**
> * Array representing the regex sequence string - ie: "[Z,z]", "[ ]",
> * "[0-2]", "[-]", "[0-3]", "[/]", "[0-4]" and finally "[0-5]"
> */
> protected String[] m_maskFlds;
>
> /** Map of 4 separators - [/], [-], [:], [.], [,] and finally [ ] */
> protected Map m_sepMap;
>
> /** String for the template char, default is _ */
> protected String m_templateChar = "_";
>
> /** Caret position */
> protected int m_caretPosition;
>
> /** Flag to permit the verification or not */
> protected boolean m_doVerifyText;
>
> /**
> * Flag to know if it is a keyEvent or not. It is used when doVerify
> input
> * text is processed.
> */
> protected boolean m_isKeyEvent;
>
> /** String Mask - ie: "[Z,z][ ][0-2][-][0-3][/][0-4][0-5]" */
> private String m_mask;
>
> /**
> * Constructor
> *
> * Created 07-Nov-2004
> *
> * @param argParent
> * Parent Composite
> * @param argColorRegistry
> * Application color registry
> */
> public OMaskEdit(final Composite argParent,
> final ColorRegistry argColorRegistry) {
> this(argParent, SWT.SINGLE | SWT.WRAP | SWT.BORDER, true,
> argColorRegistry);
> }
>
> /**
> * Constructor
> *
> * Created 07-Nov-2004
> *
> * @param argParent
> * Parent Composite
> * @param argMandatory
> * Mandotary or not
> * @param argColorRegistry
> * Application color registry
> */
> public OMaskEdit(final Composite argParent, final boolean argMandatory,
> final ColorRegistry argColorRegistry) {
> this(argParent, SWT.SINGLE | SWT.WRAP | SWT.BORDER, argMandatory,
> argColorRegistry);
> }
>
> /**
> * Constructor
> *
> * Created 07-Nov-2004
> *
> * @param argParent
> * Parent Composite
> * @param argMandatory
> * Mandotary or not
> * @param argMask
> * Mask to apply
> * @param argColorRegistry
> * Application color registry
> */
> public OMaskEdit(final Composite argParent, final boolean argMandatory,
> final String argMask, final ColorRegistry argColorRegistry) {
> this(argParent, SWT.SINGLE | SWT.WRAP | SWT.BORDER, argMandatory,
> argColorRegistry);
> setMask(argMask);
> }
>
> /**
> * Constructor
> *
> * Created 07-Nov-2004
> *
> * @param argParent
> * Parent Composite
> * @param argStyle
> * Default Style
> * @param argMandatory
> * Mandotary or not
> * @param argColorRegistry
> * Application color registry
> */
> protected OMaskEdit(final Composite argParent, final int argStyle,
> final boolean argMandatory, final ColorRegistry
> argColorRegistry) {
> super(argParent, argStyle, argMandatory, argColorRegistry);
>
> m_doVerifyText = true;
> m_isKeyEvent = false;
>
> // Fill the Map of separators
> m_sepMap = new HashMap();
> m_sepMap.put("[/]", "/");
> m_sepMap.put("[-]", "-");
> m_sepMap.put("[:]", ":");
> //_sepMap.put("[.]", "."); // Pose un pbl lors du replaceAll
> m_sepMap.put("[,]", ",");
> m_sepMap.put("[ ]", " ");
>
> // Colors
> //_focusGainedBackGroundColor = new Color(null, 255, 204, 204);
> //_focusLostBackGroundColor = new Color(null, 255, 255, 255);
>
> // Define the control
> //_textCtl = new Text(parent, style);
>
> // Add the VerifyListener
> addVerifyListener(new VerifyListener() {
> public void verifyText(final VerifyEvent argEvent) {
> if (m_doVerifyText) {
> doVerifyText(argEvent);
> } else {
> m_doVerifyText = true;
> argEvent.doit = true;
> }
> }
> });
>
> // Add the KeyListener to know if a key has been pressed or not
> addKeyListener(new KeyListener() {
> public void keyPressed(final KeyEvent argEvent) {
> m_isKeyEvent = true;
> }
>
> public void keyReleased(final KeyEvent argEvent) {
> m_isKeyEvent = false;
> }
> });
> }
>
> /**
> * doVerifyText
> *
> * Created 07-Nov-2004
> *
> * Used to Verify the input text
> *
> * @param argEvent
> * Verify Event
> */
> protected void doVerifyText(final VerifyEvent argEvent) {
> if ((!isEditable() && !m_isKeyEvent) || (isEditable() &&
> m_isKeyEvent)
> || (isEditable() && !m_isKeyEvent)) {
> argEvent.doit = false;
>
> //final Text control = (Text) argEvent.widget;
> //String currentText = control.getText();
> String currentText = m_textCtl.getText();
>
> final int currentTextLength = currentText.length();
>
> // Delete from left to right
> if (argEvent.keyCode == SWT.BS) {
> m_caretPosition = argEvent.end;
> if (m_caretPosition > 0) {
> String part1 = currentText;
> String part2 = currentText;
>
> if (!m_sepMap.containsKey(m_maskFlds[m_caretPosition -
> 1])) {
> part1 = part1.substring(0, m_caretPosition - 1);
> part2 = part2.substring(m_caretPosition,
> currentTextLength);
> m_caretPosition = m_caretPosition - 1;
> } else {
> // It is a separator
> if (m_caretPosition > 1) {
> part1 = part1.substring(0, m_caretPosition -
> 2);
> part2 = part2.substring(m_caretPosition - 1,
> currentTextLength);
> m_caretPosition = m_caretPosition - 2;
> }
> }
> currentText = part1 + m_templateChar + part2;
>
> m_doVerifyText = false;
>
> // control.setText(currentText);
> // control.setSelection(m_caretPosition);
> m_textCtl.setText(currentText);
> m_textCtl.setSelection(m_caretPosition);
> }
> argEvent.doit = false;
> return;
> }
>
> //Delete from right to left
> if (argEvent.keyCode == SWT.DEL) {
> m_caretPosition = argEvent.end;
> if (m_caretPosition >= 0) {
> String part1 = currentText;
> String part2 = currentText;
>
> if ((m_caretPosition < m_maskFlds.length)
> && (!m_sepMap
>
> .containsKey(m_maskFlds[m_caretPosition]))) {
> part1 = part1.substring(0, m_caretPosition - 1);
> part2 = part2.substring(m_caretPosition,
> currentTextLength);
> } else {
> // It is a separator
> part1 = part1.substring(0, m_caretPosition - 1);
> part2 = part2.substring(m_caretPosition,
> currentTextLength);
> m_caretPosition = m_caretPosition + 1;
> }
> currentText = part1 + m_templateChar + part2;
>
> m_doVerifyText = false;
> // control.setText(currentText);
> // control.setSelection(m_caretPosition);
> m_textCtl.setText(currentText);
> m_textCtl.setSelection(m_caretPosition);
> }
> argEvent.doit = false;
> return;
> }
>
> try {
> m_caretPosition = m_textCtl.getCaretPosition();
> String newInputText = argEvent.text;
> newInputText = newInputText.toUpperCase();
> final int newInputTextLength = newInputText.length();
>
> if ((newInputTextLength == 1) && m_isKeyEvent) {
> if (m_caretPosition < m_maskFlds.length) {
> // Only 1 char -> key typed
> final Pattern pattern = Pattern
> .compile(m_maskFlds[argEvent.end]);
> final Matcher matcher =
> pattern.matcher(newInputText);
>
> // Fill the rest of the string
> for (int i = argEvent.end; i < m_maskFlds.length;
> i++) {
> if (i > argEvent.end) {
> if (m_sepMap.containsKey(m_maskFlds[i])) {
> newInputText = newInputText
> + (String) m_sepMap
> .get(m_maskFlds[i]);
> } else {
> newInputText = newInputText
> + m_templateChar;
> }
> }
> }
>
> if (matcher.find()) {
> if ((m_caretPosition + 1 < m_maskFlds.length)
> && (m_sepMap
>
> .containsKey(m_maskFlds[m_caretPosition + 1]))) {
> m_caretPosition = m_caretPosition + 1;
> }
> m_caretPosition = m_caretPosition + 1;
>
> currentText = currentText
> .substring(0, argEvent.end)
> + newInputText;
>
> m_doVerifyText = false;
> // control.setText(currentText);
> // control.setSelection(m_caretPosition);
> m_textCtl.setText(currentText);
> m_textCtl.setSelection(m_caretPosition);
> argEvent.doit = false;
> } else {
> argEvent.doit = false;
> }
> }
> } else {
> // More than 1 char -> at least 2 char - ie: Z10
> Pattern pattern = null;
> Matcher matcher = null;
> String textFormatted = "";
> int count = -1;
> for (int i = 0; i < m_maskFlds.length; i++) {
> if (m_sepMap.containsKey(m_maskFlds[i])) {
> // If it is a separator -> add separator
> textFormatted = textFormatted
> + (String) m_sepMap.get(m_maskFlds[i]);
> } else {
> // Else if it is not a separator...
> count++;
> if (count < newInputTextLength) {
> final String strToVerify = newInputText
> .substring(count, count + 1);
> pattern = Pattern.compile(m_maskFlds[i]);
> matcher = pattern.matcher(strToVerify);
> if (matcher.find()) {
> textFormatted = textFormatted +
> strToVerify;
> m_doVerifyText = false;
> // control.setText(textFormatted);
> //
> control.setSelection(m_caretPosition);
> m_textCtl.setText(textFormatted);
>
> m_textCtl.setSelection(m_caretPosition);
> argEvent.doit = false;
> } else {
> argEvent.doit = false;
> return;
> }
> } else {
> // The string to insert is smaller than the
> mask
> // so i insert the m_templateChar - ie:
> Z100
> textFormatted = textFormatted +
> m_templateChar;
> m_doVerifyText = false;
> // control.setText(textFormatted);
> // control.setSelection(m_caretPosition);
> m_textCtl.setText(textFormatted);
> m_textCtl.setSelection(m_caretPosition);
> argEvent.doit = false;
> }
>
> }
> }
> }
> } catch (final Exception argex) {
> System.out.println("OMaskEdit - doVerifyText(...) - ERROR :
> "
> + argex.getMessage());
> }
> }
> }
>
> /**
> *
> * setMask
> *
> * Created 17-nov.-2004
> *
> * Used to set the mask string ie: [Z,z][ ][0-2][-][0-3][/][0-4][0-5]
> The
> * text is reseted/it is set to blank/empty field
> *
> * @param argMask
> * Mask string
> */
> public void setMask(final String argMask) {
> setMask(argMask, true);
> }
>
> /**
> *
> * setMask
> *
> * Created 17-nov.-2004
> *
> * Used to set the mask string ie: [Z,z][ ][0-2][-][0-3][/][0-4][0-5]
> *
> * @param argMask
> * Mask string
> * @param argResetText
> * Flag to reset text or not
> */
> public void setMask(final String argMask, final boolean argResetText) {
> m_mask = argMask;
> convertMaskToArray(argMask, argResetText);
> }
>
> /**
> *
> * convertMaskToArray
> *
> * Created 17-nov.-2004
> *
> * Used to split the mask into an array of string. Each element of the
> array
> * represents a char
> *
> * @param argMask
> * Mask string
> * @param argResetText
> * Flag to reset text or not
> */
> private void convertMaskToArray(final String argMask,
> final boolean argResetText) {
> // Split the mask string into regex elements
> final Pattern pattern = Pattern.compile("]",
> Pattern.CASE_INSENSITIVE);
> m_maskFlds = pattern.split(argMask);
>
> for (int i = 0; i < m_maskFlds.length; i++) {
> m_maskFlds[i] = m_maskFlds[i] + ']';
> //System.out.println(" field[" + i + "] = " + flds[i]);
> }
>
> if (argResetText) {
> setText(""); // Used to display the mask
> }
> }
>
> // TODO voir si besoin d'activer. Attention, lorsque je tape je dois
> // replacer au debut
> // /**
> // * setText
> // *
> // * Created 16-Nov-2004
> // *
> // * Used to set the input of the widget
> // *
> // * @param argString
> // * the new text
> // */
> // public void setText(String argString) {
> // super.setText(argString);
> // m_textCtl.setSelection(0, m_maskFlds.length);
> // }
>
> /**
> *
> * getText
> *
> * Created 17-nov.-2004
> *
> * Used to get the text with the mask as it is displayed
> *
> * @return the text as it is displayed
> */
> public String getText() {
> return getText(true);
> }
>
> /**
> *
> * getText
> *
> * Created 17-nov.-2004
> *
> * Used to get the text with/without the mask
> *
> * @param argMask
> * Flag to add the mask or not
> * @return the text with/without the mask according to argMask
> */
> public String getText(final boolean argMask) {
> String retVal = "";
> if (argMask) {
> // displayed text with mask
> retVal = m_textCtl.getText();
> } else {
> // there is no mask, so i have to remove the separator
> retVal = m_textCtl.getText();
> final Iterator it = m_sepMap.values().iterator();
> while (it.hasNext()) {
> retVal = retVal.replaceAll((String) it.next(), "");
> }
> retVal = retVal.replaceAll(m_templateChar, "");
> }
> return retVal;
> }
>
> public static void main(String[] args) {
> final Display display = new Display();
> final Shell shell = new Shell(display);
> final GridLayout gridLayout = new GridLayout();
> shell.setLayout(gridLayout);
>
> // Mask Label
> final Label label1 = new Label(shell, SWT.NONE);
> label1.setText("Mask = [Z,z][ ][0-2][-][0-3][/][0-4][0-5]");
> GridData data = new GridData();
> data.verticalAlignment = GridData.BEGINNING;
> label1.setLayoutData(data);
>
> final Label label2 = new Label(shell, SWT.NONE);
> label2.setText("So you can enter Z1001 but not Z3001 or A1001");
> data = new GridData();
> data.verticalAlignment = GridData.BEGINNING;
> label2.setLayoutData(data);
>
> // MaskEdit control
> final OMaskEdit maskEditText = new OMaskEdit(shell, null);
> maskEditText.setMask("[Z,z][ ][0-2][-][0-3][/][0-4][0-5]");
> data = new GridData();
> data.verticalAlignment = GridData.BEGINNING;
> maskEditText.setLayoutData(data);
> //maskEditText.setEditable(false);
> maskEditText.setText("Z1001");
>
> //maskEditText.setText("Z21");
> //maskEditText.setText("Z2115");
> //maskEditText.setText("");
>
> final Button btn = new Button(shell, SWT.PUSH);
> btn.setText("Button");
> btn.setSize(100, 20);
> data = new GridData();
> data.verticalAlignment = GridData.BEGINNING;
> btn.setLayoutData(data);
>
> final Label label3 = new Label(shell, SWT.SINGLE | SWT.WRAP
> | SWT.BORDER);
>
> label3.setText("................................................... ");
> data = new GridData();
> data.verticalAlignment = GridData.BEGINNING;
> label3.setLayoutData(data);
>
> btn.addSelectionListener(new SelectionAdapter() {
> public void widgetSelected(final SelectionEvent argE) {
> System.out.println("Btn was clicked...");
> System.out.println("Btn was clicked..."
> + maskEditText.getText());
> System.out.println("Btn was clicked..."
> + maskEditText.getText(false));
>
> //label3.setText(maskEditText.getText());
> label3.setText(maskEditText.getText() + "\t"
> + maskEditText.getText(false));
> }
> });
> // shell.addListener(SWT.Close, new Listener() {
> // public void handleEvent(Event e) {
> // System.out.println("Ok button tapped");
> //
> // label3.setText(maskEditText.getText() + "\t" +
> // maskEditText.getText(false));
> // }
> // });
>
> shell.pack();
> shell.open();
>
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch()) {
> display.sleep();
> }
> }
> display.dispose();
> }
>
> }
>
>
> =============================================
>
> import org.eclipse.jface.resource.ColorRegistry;
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.widgets.Composite;
> import org.eclipse.swt.widgets.Display;
> import org.eclipse.swt.widgets.Menu;
> import org.eclipse.swt.widgets.Shell;
> import org.eclipse.swt.widgets.Text;
> import org.eclipse.swt.widgets.Label;
> import org.eclipse.swt.events.VerifyListener;
> import org.eclipse.swt.widgets.Listener;
> import org.eclipse.swt.widgets.Event;
> import org.eclipse.swt.events.FocusListener;
> import org.eclipse.swt.events.HelpListener;
> import org.eclipse.swt.events.KeyListener;
> import org.eclipse.swt.events.ModifyListener;
> import org.eclipse.swt.events.MouseListener;
> import org.eclipse.swt.events.MouseMoveListener;
> import org.eclipse.swt.events.MouseTrackListener;
> import org.eclipse.swt.events.FocusAdapter;
> import org.eclipse.swt.events.FocusEvent;
> import org.eclipse.swt.events.SelectionListener;
> import org.eclipse.swt.events.TraverseListener;
> import org.eclipse.swt.graphics.Color;
> import org.eclipse.swt.graphics.Cursor;
> import org.eclipse.swt.graphics.Font;
> import org.eclipse.swt.graphics.Point;
> import org.eclipse.swt.graphics.Rectangle;
> import org.eclipse.swt.layout.GridData;
> import org.eclipse.swt.layout.GridLayout;
>
> /**
> * Used to define a Text widget which can be extended
> *
> * It requires some colors in a org.eclipse.jface.resource.ColorRegistry
> *
> * (1) editMandatoryBackGroundColor (2) editDefaultBackGroundColor (3)
> * readOnlyDefaultBackGroundColor (4) readOnlyDefaultForeGroundColor (5)
> * readOnlyHighlightedForeGroundColor (6) editForeGroundColor
> *
> */
> public class OText extends Composite {
> /** Text control */
> protected Text m_textCtl;
>
> /**
> * Flag mandatory field. If true, the background is in a given color.
> If
> * false, another color is used for the background
> */
> protected boolean m_mandatory;
>
> /** Flag to set foreground in a highlighted color */
> protected boolean m_highlighted;
>
> /** The color registry */
> protected ColorRegistry m_colorRegistry;
>
> /** Control edit Mandatory BackGround Color */
> protected Color m_editMandatoryBackGroundColor;
>
> /** Control edit Default BackGround Color */
> protected Color m_editDefaultBackGroundColor;
>
> /** Control readOnly Default BackGround Color */
> protected Color m_readOnlyDefaultBackGroundColor;
>
> /** Control readOnly Default ForeGround Color */
> protected Color m_readOnlyDefaultForeGroundColor;
>
> /** Control readOnly Highlighted ForeGround Color */
> protected Color m_readOnlyHighlightedForeGroundColor;
>
> /** Control edit ForeGround Color */
> protected Color m_editForeGroundColor;
>
> /**
> * Constructor
> *
> * @param argParent
> * Parent Composite
> * @param argColorRegistry
> * Application color registry
> */
> public OText(final Composite argParent, final ColorRegistry
> argColorRegistry) {
> this(argParent, SWT.SINGLE | SWT.WRAP | SWT.BORDER, true,
> argColorRegistry);
> }
>
> /**
> * Constructor
> *
> * Created 17-nov.-2004
> *
> * @param argParent
> * Parent Composite
> * @param argMandatory
> * Mandatory or not
> * @param argColorRegistry
> * Application color registry
> */
> public OText(final Composite argParent, final boolean argMandatory,
> final ColorRegistry argColorRegistry) {
> this(argParent, SWT.SINGLE | SWT.WRAP | SWT.BORDER, argMandatory,
> argColorRegistry);
> }
>
> /**
> * Constructor - Always Mandatory
> *
> * Created 17-nov.-2004
> *
> * @param argParent
> * Parent Composite
> * @param argStyle
> * Widget Style
> * @param argColorRegistry
> * Application color registry
> */
> public OText(final Composite argParent, final int argStyle,
> final ColorRegistry argColorRegistry) {
> this(argParent, argStyle, true, argColorRegistry);
> }
>
> /**
> * Constructor
> *
> * Created 17-nov.-2004
> *
> * @param argParent
> * Parent Composite
> * @param argStyle
> * Widget Style
> * @param argMandatory
> * Mandatory or not
> * @param argColorRegistry
> * Application color registry
> */
> public OText(final Composite argParent, final int argStyle,
> final boolean argMandatory, final ColorRegistry
> argColorRegistry) {
> super(argParent, SWT.NONE);
> m_mandatory = argMandatory;
> m_highlighted = false;
> m_colorRegistry = argColorRegistry;
>
> // Define the control
> m_textCtl = new Text(this, argStyle);
>
> if ((argColorRegistry != null)
> && (argColorRegistry.get("editMandatoryBackGroundColor") !=
> null)
> && (argColorRegistry.get("editDefaultBackGroundColor") !=
> null)
> && (argColorRegistry.get("readOnlyDefaultBackGroundColor")
> != null)
> && (argColorRegistry.get("readOnlyDefaultForeGroundColor")
> != null)
> &&
> (argColorRegistry.get("readOnlyHighlightedForeGroundColor") != null)
> && (argColorRegistry.get("editForeGroundColor") != null)) {
>
> // Set the color's variables
> m_editMandatoryBackGroundColor = argColorRegistry
> .get("editMandatoryBackGroundColor");
> m_editDefaultBackGroundColor = argColorRegistry
> .get("editDefaultBackGroundColor");
> m_readOnlyDefaultBackGroundColor = argColorRegistry
> .get("readOnlyDefaultBackGroundColor");
> m_readOnlyDefaultForeGroundColor = argColorRegistry
> .get("readOnlyDefaultForeGroundColor");
> m_readOnlyHighlightedForeGroundColor = argColorRegistry
> .get("readOnlyHighlightedForeGroundColor");
> m_editForeGroundColor =
> argColorRegistry.get("editForeGroundColor");
>
> // Initialize the default colors
> m_textCtl.setBackground(m_readOnlyDefaultBackGroundColor);
> if (m_highlighted) {
>
> m_textCtl.setForeground(m_readOnlyHighlightedForeGroundColor );
> } else {
> m_textCtl.setForeground(m_readOnlyDefaultForeGroundColor);
> }
> }
>
> // Add the FocusListener for color
> m_textCtl.addFocusListener(new FocusAdapter() {
> public void focusGained(final FocusEvent argE) {
> doFocusGained(argE);
> }
>
> public void focusLost(final FocusEvent argE) {
> doFocusLost(argE);
> }
> });
>
> // Perhaps a bug...
> addListener(SWT.Dispose, new Listener() {
> public void handleEvent(final Event argEvent) {
> widgetDisposed();
> }
> });
>
> addListener(SWT.Resize, new Listener() {
> public void handleEvent(final Event argEvent) {
> widgetResize();
> }
> });
>
> addListener(SWT.FocusIn, new Listener() {
> public void handleEvent(final Event argEvent) {
> m_textCtl.setFocus();
> }
> });
>
> }
>
> /**
> *
> * doFocusGained
> *
> * Created 17-nov.-2004
> *
> * Used to set the background color according to the m_mandatory flag
> *
> * @param argEvent
> * FocusEvent of the source
> */
> protected void doFocusGained(final FocusEvent argEvent) {
> if ((m_colorRegistry != null) && isEditable()) {
> if (m_mandatory) {
> m_textCtl.setBackground(m_editMandatoryBackGroundColor);
> } else {
> m_textCtl.setBackground(m_editDefaultBackGroundColor);
> }
> m_textCtl.setForeground(m_editForeGroundColor);
> }
>
> // if(isEditable()) {
> // m_textCtl.traverse(SWT.TRAVERSE_TAB_NEXT);
> // }
> }
>
> /**
> *
> * doFocusLost
> *
> * Created 17-nov.-2004
> *
> * Used to set the background color of the widget to white when the
> focus is
> * lost
> *
> * @param argEvent
> * FocusEvent of the source
> */
> protected void doFocusLost(final FocusEvent argEvent) {
> if (m_colorRegistry != null) {
> if (isEditable()) {
> m_textCtl.setBackground(m_readOnlyDefaultBackGroundColor);
> m_textCtl.setForeground(m_editForeGroundColor);
> } else {// ReadOnly
> m_textCtl.setBackground(m_readOnlyDefaultBackGroundColor);
> if (m_highlighted) {
> m_textCtl
>
> .setForeground(m_readOnlyHighlightedForeGroundColor);
> } else {
>
> m_textCtl.setForeground(m_readOnlyDefaultForeGroundColor);
> }
> }
> }
> }
>
> /**
> *
> * widgetDisposed
> *
> * Created 17-nov.-2004
> *
> * Used to dispose the created resources
> *
> */
> private void widgetDisposed() {
> //m_focusGainedMandatoryBackGroundColor.dispose();
> //m_focusGainedNotMandatoryBackGroundColor.dispose();
>
> // The next color must not be dispose because it is a SystemColor
> //m_focusLostBackGroundColor.dispose();
> }
>
> /**
> *
> * widgetResize
> *
> * Created 17-nov.-2004
> *
> * Used to resize and repaint the widget according to the client area.
> *
> * There is a workaround because of the input string not fully shown
> when
> * redraw. In fact it seems that the redraw doesn't work correctly.
> *
> *
> ============================================================ ============
> * veronika_irvine@ca.ibm.com
> *
> * This is a bug in SWT. I have entered the following bug report:
> * https://bugs.eclipse.org/bugs/show_bug.cgi?id=78943 As a workaround,
> * instead of calling: _textCtl.setText(_textCtl.getText()); try:
> * _textCtl.setSelection(_textCtl.getSelection());
> *
> */
> private void widgetResize() {
> final Rectangle area = getClientArea();
> m_textCtl.setBounds(0, 0, area.width, area.height);
>
> // TODO bug in SWT - verifier sur
> // https://bugs.eclipse.org/bugs/show_bug.cgi?id=78943
> //m_textCtl.setText(m_textCtl.getText());
> m_textCtl.setSelection(m_textCtl.getSelection());
>
> // Normaly the next condition is always false
> // So it will never happen
> // if (m_textCtl.isFocusControl()) {
> // m_textCtl.setSelection(1,
> m_textCtl.getText().length());
> // }
> }
>
> /**
> * Gets the editable state.
> *
> * @return whether or not the reciever is editable
> */
> public boolean isEditable() {
> return getEditable();
> }
>
>
> /*********************************************************** ***************
>
> ************************************************************ *************/
>
> /* FROM Text */
>
> /**
> * Adds the listener to the collection of listeners who will be
> notified
> * when the receiver's text is modified, by sending it one of the
> messages
> * defined in the <code>ModifyListener</code> interface.
> *
> * @param argListener
> * the listener which should be notified
> *
> * @exception IllegalArgumentException
> * <ul>
> * <li>ERROR_NULL_ARGUMENT - if the listener is
> null</li>
> * </ul>
> *
> * @see ModifyListener
> * @see #removeModifyListener
> */
> public void addModifyListener(final ModifyListener argListener) {
> m_textCtl.addModifyListener(argListener);
> }
>
> /**
> * Adds the listener to the collection of listeners who will be
> notified
> * when the control is selected, by sending it one of the messages
> defined
> * in the <code>SelectionListener</code> interface.
> * <p>
> * <code>widgetSelected</code> is not called for texts.
> * <code>widgetDefaultSelected</code> is typically called when ENTER is
> * pressed in a single-line text.
> * </p>
> *
> * @param argListener
> * the listener which should be notified
> *
> * @exception IllegalArgumentException
> * <ul>
> * <li>ERROR_NULL_ARGUMENT - if the listener is
> null</li>
> * </ul>
> *
> * @see SelectionListener
> * @see #removeSelectionListener
> * @see SelectionEvent
> */
> public void addSelectionListener(final SelectionListener argListener) {
> m_textCtl.addSelectionListener(argListener);
> }
>
> /**
> * Adds the listener to the collection of listeners who will be
> notified
> * when the receiver's text is verified, by sending it one of the
> messages
> * defined in the <code>VerifyListener</code> interface.
> *
> * @param argListener
> * the listener which should be notified
> *
> * @exception IllegalArgumentException
> * <ul>
> * <li>ERROR_NULL_ARGUMENT - if the listener is
> null</li>
> * </ul>
> *
> * @see VerifyListener
> * @see #removeVerifyListener
> */
> public void addVerifyListener(final VerifyListener argListener) {
> m_textCtl.addVerifyListener(argListener);
> }
>
> /**
> * Appends a string.
> * <p>
> * The new text is appended to the text at the end of the widget.
> * </p>
> *
> * @param argString
> * the string to be appended
> *
> * @exception IllegalArgumentException
> * <ul>
> * <li>ERROR_NULL_ARGUMENT - if the string is null</li>
> * </ul>
> */
> public void append(final String argString) {
> m_textCtl.append(argString);
> }
>
> /**
> * Clears the selection.
> */
> public void clearSelection() {
> m_textCtl.clearSelection();
> }
>
> /**
> * Copies the selected text.
> * <p>
> * The current selection is copied to the clipboard.
> * </p>
> */
> public void copy() {
> m_textCtl.copy();
> }
>
> /**
> * Cuts the selected text.
> * <p>
> * The current selection is first copied to the clipboard and then
> deleted
> * from the widget.
> * </p>
> */
> public void cut() {
> m_textCtl.cut();
> }
>
> /**
> * Gets the line number of the caret.
> * <p>
> * The line number of the caret is returned.
> * </p>
> *
> * @return the line number
> */
> public int getCaretLineNumber() {
> return m_textCtl.getCaretLineNumber();
> }
>
> /**
> * Gets the location the caret.
> * <p>
> * The location of the caret is returned.
> * </p>
> *
> * @return a point, the location of the caret
> */
> public Point getCaretLocation() {
> return m_textCtl.getCaretLocation();
> }
>
> /**
> * Gets the position of the caret.
> * <p>
> * The character position of the caret is returned.
> * </p>
> *
> * @return the position of the caret
> */
> public int getCaretPosition() {
> return m_textCtl.getCaretPosition();
> }
>
> /**
> * Gets the number of characters.
> *
> * @return number of characters in the widget
> */
> public int getCharCount() {
> return m_textCtl.getCharCount();
> }
>
> /**
> * Gets the double click enabled flag.
> * <p>
> * The double click flag enables or disables the default action of the
> text
> * widget when the user double clicks.
> * </p>
> *
> * @return whether or not double click is enabled
> */
> public boolean getDoubleClickEnabled() {
> return m_textCtl.getDoubleClickEnabled();
> }
>
> /**
> * Gets the echo character.
> * <p>
> * The echo character is the character that is displayed when the user
> * enters text or the text is changed by the programmer.
> * </p>
> *
> * @return the echo character
> */
> public char getEchoChar() {
> return m_textCtl.getEchoChar();
> }
>
> /**
> * Gets the editable state.
> *
> * @return whether or not the reciever is editable
> */
> public boolean getEditable() {
> return m_textCtl.getEditable();
> }
>
> /**
> * Gets the number of lines.
> *
> * @return the number of lines in the widget
> */
> public int getLineCount() {
> return m_textCtl.getLineCount();
> }
>
> /**
> * Gets the line delimiter.
> *
> * @return a string that is the line delimiter
> */
> public String getLineDelimiter() {
> return m_textCtl.getLineDelimiter();
> }
>
> /**
> * Gets the height of a line.
> *
> * @return the height of a row of text
> */
> public int getLineHeight() {
> return m_textCtl.getLineHeight();
> }
>
> /**
> * Returns the orientation of the receiver.
> *
> * @return the orientation style
> */
> public int getOrientation() {
> return m_textCtl.getOrientation();
> }
>
> /**
> * Gets the position of the selected text.
> * <p>
> * Indexing is zero based. The range of a selection is from 0..N where
> N is
> * the number of characters in the widget.
> * </p>
> *
> * @return the start and end of the selection
> */
> public Point getSelection() {
> return m_textCtl.getSelection();
> }
>
> /**
> * Gets the number of selected characters.
> *
> * @return the number of selected characters.
> */
> public int getSelectionCount() {
> return m_textCtl.getSelectionCount();
> }
>
> /**
> * Gets the selected text.
> *
> * @return the selected text
> */
> public String getSelectionText() {
> return m_textCtl.getSelectionText();
> }
>
> /**
> * Gets the number of tabs.
> * <p>
> * Tab stop spacing is specified in terms of the space (' ') character.
> The
> * width of a single tab stop is the pixel width of the spaces.
> * </p>
> *
> * @return the number of tab characters
> */
> public int getTabs() {
> return m_textCtl.getTabs();
> }
>
> /**
> * Gets the widget text.
> * <p>
> * The text for a text widget is the characters in the widget.
> * </p>
> *
> * @return the widget text
> */
> public String getText() {
> return m_textCtl.getText().trim();
> }
>
> /**
> * Gets a range of text. Returns an empty string if the start of the
> range
> * is greater than the end.
> * <p>
> * Indexing is zero based. The range of a selection is from 0..N-1
> where N
> * is the number of characters in the widget.
> * </p>
> *
> * @param argStart
> * the start of the range
> * @param argEnd
> * the end of the range
> * @return the range of text
> */
> public String getText(final int argStart, final int argEnd) {
> return m_textCtl.getText(argStart, argEnd).trim();
> }
>
> /**
> * Returns the maximum number of characters that the receiver is
> capable of
> * holding.
> * <p>
> * If this has not been changed by <code>setTextLimit()</code>, it will
> * be the constant <code>Text.LIMIT</code>.
> * </p>
> *
> * @return the text limit
> */
> public int getTextLimit() {
> return m_textCtl.getTextLimit();
> }
>
> /**
> * Returns the zero-relative index of the line which is currently at
> the top
> * of the receiver.
> * <p>
> * This index can change when lines are scrolled or new lines are added
> or
> * removed.
> * </p>
> *
> * @return the index of the top line
> */
> public int getTopIndex() {
> return m_textCtl.getTopIndex();
> }
>
> /**
> * Gets the top pixel.
> * <p>
> * The top pixel is the pixel position of the line that is currently at
> the
> * top of the widget. On some platforms, a text widget can be scrolled
> by
> * pixels instead of lines so that a partial line is displayed at the
> top of
> * the widget.
> * </p>
> * <p>
> * The top pixel changes when the widget is scrolled. The top pixel
> does not
> * include the widget trimming.
> * </p>
> *
> * @return the pixel position of the top line
> */
> public int getTopPixel() {
> return m_textCtl.getTopPixel();
> }
>
> /**
> * Inserts a string.
> * <p>
> * The old selection is replaced with the new text.
> * </p>
> *
> * @param argString
> * the string
> */
> public void insert(final String argString) {
> m_textCtl.insert(argString);
> }
>
> /**
> * Pastes text from clipboard.
> * <p>
> * The selected text is deleted from the widget and new text inserted
> from
> * the clipboard.
> * </p>
> */
> public void paste() {
> m_textCtl.paste();
> }
>
> /**
> * Removes the listener from the collection of listeners who will be
> * notified when the receiver's text is modified.
> *
> * @param argListener
> * the listener which should no longer be notified
> *
> * @exception IllegalArgumentException
> * <ul>
> * <li>ERROR_NULL_ARGUMENT - if the listener is
> null</li>
> * </ul>
> *
> * @see ModifyListener
> * @see #addModifyListener
> */
> public void removeModifyListener(final ModifyListener argListener) {
> m_textCtl.removeModifyListener(argListener);
> }
>
> /**
> * Removes the listener from the collection of listeners who will be
> * notified when the control is selected.
> *
> * @param argListener
> * the listener which should be notified
> *
> * @exception IllegalArgumentException
> * <ul>
> * <li>ERROR_NULL_ARGUMENT - if the listener is
> null</li>
> * </ul>
> *
> * @see SelectionListener
> * @see #addSelectionListener
> */
> public void removeSelectionListener(final SelectionListener
> argListener) {
> m_textCtl.removeSelectionListener(argListener);
> }
>
> /**
> * Removes the listener from the collection of listeners who will be
> * notified when the control is verified.
> *
> * @param argListener
> * the listener which should be notified
> *
> * @exception IllegalArgumentException
> * <ul>
> * <li>ERROR_NULL_ARGUMENT - if the listener is
> null</li>
> * </ul>
> *
> * @see VerifyListener
> * @see #addVerifyListener
> */
> public void removeVerifyListener(final VerifyListener argListener) {
> m_textCtl.removeVerifyListener(argListener);
> }
>
> /**
> * Selects all the text in the receiver.
> */
> public void selectAll() {
> m_textCtl.selectAll();
> }
>
> /**
> * Sets the double click enabled flag.
> * <p>
> * The double click flag enables or disables the default action of the
> text
> * widget when the user double clicks.
> * </p>
> *
> * @param argDoubleClick
> * the new double click flag
> */
> public void setDoubleClickEnabled(final boolean argDoubleClick) {
> m_textCtl.setDoubleClickEnabled(argDoubleClick);
> }
>
> /**
> * Sets the echo character.
> * <p>
> * The echo character is the character that is displayed when the user
> * enters text or the text is changed by the programmer. Setting the
> echo
> * character to '\0' clears the echo character and redraws the original
> * text. If for any reason the echo character is invalid, the default
> echo
> * character for the platform is used.
> * </p>
> *
> * @param argEcho
> * the new echo character
> */
> public void setEchoChar(final char argEcho) {
> m_textCtl.setEchoChar(argEcho);
> }
>
> /**
> * Sets the editable state.
> *
> * @param argEditable
> * the new editable state
> */
> public void setEditable(final boolean argEditable) {
> m_textCtl.setEditable(argEditable);
>
> // Colors
> if (m_colorRegistry != null) {
> if (isEditable()) {
> m_textCtl.setBackground(m_readOnlyDefaultBackGroundColor);
> m_textCtl.setForeground(m_editForeGroundColor);
> } else {// ReadOnly
> m_textCtl.setBackground(m_readOnlyDefaultBackGroundColor);
> if (m_highlighted) {
> m_textCtl
>
> .setForeground(m_readOnlyHighlightedForeGroundColor);
> } else {
>
> m_textCtl.setForeground(m_readOnlyDefaultForeGroundColor);
> }
> }
> }
> }
>
> /**
> * Sets the mandatory state.
> *
> * @param argMandatory
> * Mandatory or not
> */
> public void setMandatory(final boolean argMandatory) {
> m_mandatory = argMandatory;
> }
>
> /**
> * Sets the orientation of the receiver, which must be one of the
> constants
> * <code>SWT.LEFT_TO_RIGHT</code> or <code>SWT.RIGHT_TO_LEFT</code>.
> * <p>
> *
> * @param argOrientation
> * new orientation style
> */
> public void setOrientation(final int argOrientation) {
> m_textCtl.setOrientation(argOrientation);
> }
>
> /**
> * Sets the selection.
> * <p>
> * Indexing is zero based. The range of a selection is from 0..N where
> N is
> * the number of characters in the widget.
> * </p>
> * <p>
> * Text selections are specified in terms of caret positions. In a text
> * widget that contains N characters, there are N+1 caret positions,
> ranging
> * from 0..N. This differs from other functions that address character
> * position such as getText () that use the regular array indexing
> rules.
> * </p>
> *
> * @param argStart
> * new caret position
> */
> public void setSelection(final int argStart) {
> m_textCtl.setSelection(argStart);
> }
>
> /**
> * Sets the selection.
> * <p>
> * Indexing is zero based. The range of a selection is from 0..N where
> N is
> * the number of characters in the widget.
> * </p>
> * <p>
> * Text selections are specified in terms of caret positions. In a text
> * widget that contains N characters, there are N+1 caret positions,
> ranging
> * from 0..N. This differs from other functions that address character
> * position such as getText () that use the usual array indexing rules.
> * </p>
> *
> * @param argStart
> * the start of the range
|
|
|
Re: Looking for input masking and data validation widgets for SWT [message #451773 is a reply to message #451432] |
Tue, 08 March 2005 21:28 |
Boris Munivrana Messages: 23 Registered: July 2009 |
Junior Member |
|
|
Hi Rod,
if your project's limited to win32 only, you could encapsulate the
MaskEd-Control provided by Microsoft via swt.ole.win32. Should take you a
minute or two.
Rod Bailey wrote:
> Folks,
> I'm looking for a source of widgets or infrastructure for SWT that supports
> input validation - particularly edit masks.
> In my searching, I stumbled onto a product called "Essential Data", the home
> page for which is here:
> http://www.swtworkbench.com/solutions/ed.shtml.
> I've never heard of the product or the company before, so I was hoping
> someone here might've tried out the product and could give me their
> impressions.
> I tried raising an enhancement request to add input masking facilities to
> the Text field in SWT, but my suggestion was not met with great enthusiasm:
> https://bugs.eclipse.org/bugs/show_bug.cgi?id=85935
> I know you can write input validation stuff yourself by setting up the
> appropriate listeners on an input widget, but I would prefer to buy those
> facilities rather than build them, in order to save time on my project
> schedule.
> Any insight you can provide would be much appreciated.
> Rod Bailey.
> BTW: In reply to a previous respondent, I'm neither a spammer or a
> representative of this company.
|
|
|
Goto Forum:
Current Time: Thu Apr 25 22:01:31 GMT 2024
Powered by FUDForum. Page generated in 0.03083 seconds
|