My extended TextArea widget finally works (thanks JAD...)...

Hereunder the code for fellow developers (for ULC 6.1)...

Regards,

Alberto A. Smulders
HostDat Lda. - Portugal

** SERVER **

package pt.hostdat.hcc.common.util.component.xtextarea.server;

import com.ulcjava.base.application.ULCTextArea;

/**
 * Server-side code of extended TextArea component
 * 
 * @author Alberto Smulders
 */
public class ULCXTextArea extends ULCTextArea {
        private int fMaxCharacters = 0;

        protected void uploadStateUI() {
                super.uploadStateUI();
                setStateUI("maxCharacters", 0, fMaxCharacters);
        }

        public int getMaxCharacters() {
                return fMaxCharacters;
        }
        
        public void setMaxCharacters(int maxCharacters) {
                fMaxCharacters = setStateUI("maxCharacters", fMaxCharacters, 
maxCharacters);
        }
        
        protected String typeString() {
                return 
"pt.hostdat.hcc.common.util.component.xtextarea.client.UIXTextArea";
        }
}


** CLIENT **

package pt.hostdat.hcc.common.util.component.xtextarea.client;

import com.ulcjava.base.client.UITextArea;
import com.ulcjava.base.client.UiPlainDocument;

/**
 * Client-side code of extended TextArea component
 * 
 * @author Alberto Smulders
 */
public class UIXTextArea extends UITextArea {
        private BasicTextArea fTextArea = null;
        private DocumentSizeFilter fDocumentSizeFilter = new 
DocumentSizeFilter();
        
        protected Object createBasicObject(Object[] a) {
                System.out.println("createBasicObject!");
                if (fTextArea == null) {
                        fTextArea = new BasicTextArea();
                }
                return fTextArea;
        }
        
        protected void postInitializeState() {
                super.postInitializeState();
                
((UiPlainDocument)fTextArea.getDocument()).setDocumentFilter(fDocumentSizeFilter);
        }
    
        public int getMaxCharacter() {
                return fDocumentSizeFilter.getMaxCharacters();
        }
                
        public void setMaxCharacters(int maxCharacters) {               
                System.out.println("UIXTextArea.setMaxCharacters : " + 
maxCharacters);                  
                if (maxCharacters >= 0) {
                        fDocumentSizeFilter.setMaxCharacters(maxCharacters);
                }
        }       
}




package pt.hostdat.hcc.common.util.component.xtextarea.client;

import java.awt.Toolkit;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;

/**
 * Custom document filter to limit size of entry in text fields
 * 
 * @author Alberto Smulders
 */
public class DocumentSizeFilter extends DocumentFilter {
        private int fMaxCharacters = 0;

        public int getMaxCharacters() {
                return fMaxCharacters;
        }
        
        public void setMaxCharacters(int maxCharacters) {
                fMaxCharacters = maxCharacters;
        }

        public void insertString(FilterBypass fb,
                             int offset, String string,
                             AttributeSet attr)
                        throws BadLocationException {
                replace(fb, offset, 0, string, attr);
        }

        public void replace(FilterBypass fb,
                                                int offset, int length, String 
text,
                                                AttributeSet attrs) 
                        throws BadLocationException {
                int newLength = fb.getDocument().getLength() - length + 
text.length();
                if (fMaxCharacters == 0 || newLength <= fMaxCharacters) {
                        fb.replace(offset, length, text, attrs);
                }
                else {
                        Toolkit.getDefaultToolkit().beep();
                }
        }
}
-- 


Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen!
Ideal für Modem und ISDN: http://www.gmx.net/de/go/smartsurfer
_______________________________________________
ULC-developer mailing list
[email protected]
http://lists.canoo.com/mailman/listinfo/ulc-developer

Reply via email to