Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extTableSummarize / src / org / gvsig / app / documents / table / summarize / utils / DocumentFilterExt.java @ 27691

History | View | Annotate | Download (4.12 KB)

1
/*
2
 * @(#) DocumentFilterExt.java        12/11/2007
3
 *
4
 * Proyecto: Seguridad Vial - Carreteras
5
 *
6
 * Copyright (c) 2007 IVER T.I.
7
 * L?rida 20, 46009 Valencia, Espa?a
8
 * Todos los derechos reservados.
9
 *
10
 */
11
package org.gvsig.app.documents.table.summarize.utils;
12

    
13
import javax.swing.text.AttributeSet;
14
import javax.swing.text.BadLocationException;
15
import javax.swing.text.DocumentFilter;
16

    
17
/**
18
 * DocumentFilterExt extiende a DocumentFilter para limitar el n? m?ximo de caracteres que
19
 * se pueden introducir en un documento o para bloquear la escritura en el componente. Es 
20
 * posible usar esta clase con todas aquellas que usen un objeto Document como modelo  
21
 * (JTextField, JTextArea, etc.)
22
 */
23
class DocumentFilterExt extends DocumentFilter {
24
    private int maxLength = 0;                  //Indica la longitud m?xima de caracteres admitida. Si cero, no hay longitud m?xima!
25
    private boolean locked = false;             //Bloquea la edici?n en el documento
26
        private boolean upperCase = false;                        //Devuelve el texto en mayusculas
27
    
28
        public void setUpperCase(boolean upperCase){
29
                this.upperCase = upperCase;
30
        }
31
        public boolean isUpperCase(){
32
                return this.upperCase;
33
        }
34
        
35
        public void setLocked(boolean locked){
36
                this.locked = locked;
37
        }
38
        
39
        public boolean isLocked(){
40
                return this.locked;
41
        }
42
    
43
    public void insertString(FilterBypass fb, int offs, String str, AttributeSet a) throws BadLocationException {
44

    
45
            if (locked) return;
46
            String sTextoAInsertar = getTextoAInsertar(fb, offs, 0, str);
47
            if (sTextoAInsertar != null) super.insertString(fb, offs, sTextoAInsertar, a);
48
            
49
    }//public void insertString(FilterBypass fb, int offs, String str, AttributeSet a) throws BadLocationException {
50
    
51
    public void replace(FilterBypass fb, int offs, int length, String str, AttributeSet a)throws BadLocationException {
52
            
53
            if (locked) return;
54
            String sTextoAInsertar = getTextoAInsertar(fb, offs, length, str);
55
            if (sTextoAInsertar != null) super.replace(fb, offs, length, sTextoAInsertar, a); 
56
        
57
    }//public void replace(FilterBypass fb, int offs, int length, String str, AttributeSet a)throws BadLocationException {
58
    
59
        public void remove(FilterBypass fb, int offset, int length) throws BadLocationException {
60
                if (locked) return;
61
                super.remove(fb, offset, length);
62
        }
63

    
64
        /**
65
     * Esta funci?n se encarga de analizar el texto que se va a insertar en el documento. En caso
66
     * de que sea m?s grande que el tama?o m?ximo permitido devuelve la parte "que cabe" hasta 
67
     * completar maxLength.<p>
68
     * Nota: Si maxLength = 0, entonces no hay un l?mite establecido.
69
     * @param fb: FilterBypass que puede ser usado para modificar el documento
70
     * @param offs: Lugar de inserci?n en el documento
71
     * @param length: Longitud del texto a eliminar del documento (se usa en reemplazos)
72
     * @param str: Texto a insertar. 
73
     * @return String
74
     */
75
    private String getTextoAInsertar(FilterBypass fb, int offs, int length, String str){
76
                
77
            //Si upperCase == true, se convierte todo a mayusculas
78
                if (upperCase) str = str.toUpperCase();
79
            
80
            //Si no hay l?mite en el n? m?ximo de caracteres admitidos...
81
            if (maxLength <= 0) return str;
82
                
83
                //Si la cadena a insertar cabe entera en el documento...
84
                if ((fb.getDocument().getLength() + str.length() - length) <= maxLength) {
85
                        return str;
86

    
87
                //Si la cadena a insertar no cabe entera en el documento...
88
                } else {
89
            
90
                        int liNumChars = 0;
91
            liNumChars = maxLength - (fb.getDocument().getLength() - length);
92
            if (liNumChars > 0) {
93
                    if (liNumChars > str.length()) liNumChars = str.length();
94
                    return str.substring(0, liNumChars);
95
            }                            
96
                } //if ((fb.getDocument().getLength() + str.length() - length) <= maxLength) {
97
            
98
                return null;
99
    }//private String getTextoAInsertar(...
100

    
101
        public int getMaxLength(){
102
                return maxLength;
103
        }
104
        
105
        public void setMaxLength(int maxLength){
106
                if (maxLength < 0) maxLength = 0;
107
                this.maxLength = maxLength;
108
        }
109
        
110
}//class DocumentFilterExt extends DocumentFilter {
111