16 Noviembre 2004Por Ivan Pablo Pedrazs Arce210 líneas de código

Mascara para Text (SWT)

Adaptación de un código anterior para utilizarlo con widgets de SWT.
Versión 0.2.
Este código sirve para establecer una máscara que podrá utilizarse después en cualquier Text.
java210 líneas Descargar adjuntos
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Text;

public class Mascara
{

String mascara = "";
public String texto = "";
int posMascara = 0;
static char caracterEspacio = '_';

public Mascara()
{
}

public Mascara (String mascara)
{
setMascara (mascara);
}

public void setMascara (String mascara)
{
this.mascara = mascara;
}

public void setPosMascara (int posicion)
{
if (posicion < mascara.length())
posMascara = posicion;
}

public int textoAPosMascara (int posMascara)
{
byte[] m = mascara.getBytes();
for (int i = 0, k = 0; i < posMascara &&
i < m.length; i++) {
while (m[k] != '#' && k < m.length)
k++;
if (k < m.length)
k++;
}
return 0;
}

public int getPosTexto ()
{
int posTexto = 0;
byte[] m = mascara.getBytes();
for (int i = 0, k = 0; i < posMascara &&
i < m.length; i++) {
if (m[i] == '#')
posTexto++;
}
return posTexto;
}

public int getPosMascara ()
{
return posMascara;
}

public String limpiar (int pos, int lon)
{
byte[] m = mascara.getBytes();
byte[] t = texto.getBytes();
for (int i = 0, k = 0; i < m.length; i++)
{
if (m[i] == '#' && i >= pos && i <(pos + lon))
t[i] = (byte)(' ');
}
texto = new String(t);
return texto;
}

public boolean mascaraAplicada ()
{
byte[] m = mascara.getBytes();
String textoLocal = texto + " ";
for (int i = 0; i < m.length; i++)
{
if (m[i] != '#')
{
if (textoLocal.charAt(i) != (char)m[i])
return false;
}
else
{
if (textoLocal.charAt(i) != ' ')
if (!Character.isDigit(textoLocal.charAt(i)))
return false;
}
}
return true;
}

public String aplicarMascara ()
{
byte[] m = mascara.getBytes();
for (int i = 0, k = 0; i < m.length && k < texto.length(); i++)
{
System.err.println(" m[" + i + "] = " + m[i]);
System.err.println("text charAt(" + k + ") = " + texto.charAt(k));
if (m[i] == '#')
{
if (Character.isDigit(texto.charAt(k)))
m[i] = (byte)((k < texto.length())? texto.charAt(k++):' ');
else
return mascara.replaceAll("#", " ");
}
else if (m[i] == texto.charAt(k))
k++;
}
return new String(m).replaceAll("#", " ");
}

public String deapplyMask()
{
StringBuffer s1 = new StringBuffer("");
byte[] m = mascara.getBytes();
for (int i = 0; i < m.length && i < texto.length(); i++)
{
if (m[i] == '#') {
s1.append(texto.charAt(i));
}
}
return s1.toString();
}


public static void textoMascaraGenerico(Event e, Mascara mascaraA)
{
Text t = (Text) e.widget;
t.setEditable(false);
String mascara = mascaraA.mascara;
String s = t.getText();
mascaraA.texto = s;
int pos = t.getCaretPosition();
int range = t.getSelectionCount();
mascaraA.setPosMascara (pos);


if (e.character != SWT.BS
&& ((Character.getNumericValue(e.character) == -1)
|| (e.keyCode == 0)))
return;
boolean moveBack =
(e.character == SWT.BS || e.keyCode == SWT.ARROW_LEFT);

if (range != 0)
s = mascaraA.limpiar (pos, range);
range = 0;

if ((mascara.length() <= pos && !moveBack) || range != 0) {
t.setText(s);
t.setSelection(pos);
return;
}

if (!mascaraA.mascaraAplicada())
{
//System.err.println("Mask NOT APPLIED");
mascaraA.texto = s;
s = mascaraA.aplicarMascara();
}

byte[] b = s.getBytes();
char replace = e.character;
if (Character.isDigit(e.character))
{
pos++;
/* not over read-only character */

if (mascara.charAt(pos - 1) != '#')
pos = mascara.indexOf('#', pos) + 1;

if (pos - 1 <= mascara.length())
{
b[pos - 1] = (byte) replace;

if (pos < mascara.length() && mascara.charAt(pos) != '#')
pos = mascara.indexOf('#', pos);
}
}
else
{
if (moveBack)
{
pos--;
if (pos < 0)
pos = 0;
if (mascara.charAt(pos) != '#') {
pos = mascara.lastIndexOf('#', pos);
if (pos < 0)
pos = mascara.indexOf('#');
}
if (e.character == SWT.BS)
b[pos] = (byte) ' ';
}
else
{
if (pos < mascara.length() && mascara.charAt(pos) != '#')
pos = mascara.indexOf('#', pos);
}
}
String out = new String(b);
t.setText(out);
t.setSelection(pos);
}
}

Más código de Java