Hi Brey, The Extension API has changed. UITextArea sets the document in the postInializeState() method which is called after createBasicObject(). Therefore, the document set in the old extension gets overwritten.
Please see the snippet at the end of this mail. It has the corrected extension which works as desired. Thanks and regards, Janak >-----Original Message----- >From: Kliman, Elizabeth [mailto:[EMAIL PROTECTED] >Sent: Wednesday, October 04, 2006 12:36 AM >To: Janak Mulani >Subject: Limitted length text area > > >Hi Janak, > >This is Brey using Elizabeth's workstation/email. We had worked >together to create a limitted length text field... see code at the >bottom of the email. I debugged through this and it appears that >during modification of the field, the ULC code no longer calls >insertString() on the Document within the JTextArea. This means >that the string is not intercepted and the field now allows >unlimitted text entry. This appears to have changed from 6.0 to >6.1. Is there any work around? > >public class UILimitedLengthTextArea extends UITextArea { > protected Object createBasicObject(Object[] anArgs) { > Object o = super.createBasicObject(anArgs); > int maxLength = >((Integer)anArgs[0]).intValue();//.get("maxLength").asInt(Integer.M >AX_VALUE); > ((JTextArea) o).setDocument(new LimitedDocument(maxLength)); > return o; > } > > public class LimitedDocument extends PlainDocument { > > private int maxLength; > > public LimitedDocument(int aMaxLength) { > maxLength = aMaxLength; > } > > public void insertString(int anOffset, String >aString, AttributeSet anAttributeSet) > throws BadLocationException { > int currentLength = getLength(); > if (currentLength >= maxLength) { > return; > } > if (currentLength + aString.length() > maxLength) { > aString = aString.substring(0, >maxLength - currentLength); > } > super.insertString(anOffset, aString, >anAttributeSet); > } > } >} ------------------------ import javax.swing.text.AttributeSet; import javax.swing.text.BadLocationException; import javax.swing.text.PlainDocument; import com.ulcjava.base.application.AbstractApplication; import com.ulcjava.base.application.ULCBoxPane; import com.ulcjava.base.application.ULCContainer; import com.ulcjava.base.application.ULCFrame; import com.ulcjava.base.application.ULCScrollPane; import com.ulcjava.base.application.ULCTextArea; import com.ulcjava.base.client.UITextArea; import com.ulcjava.base.development.DevelopmentRunner; public class LimitNumCharsOnTextArea extends AbstractApplication { public void start() { ULCFrame frame = new ULCFrame("LimitNumCharsOnTextAreaSample"); frame.setDefaultCloseOperation(ULCFrame.TERMINATE_ON_CLOSE); frame.setContentPane(createContentPane()); frame.setSize(300, 300); frame.setVisible(true); } private ULCContainer createContentPane() { ULCBoxPane containerPane = new ULCBoxPane(true); ULCTextArea textArea = new ULCLimitedTextArea(10); textArea.setText("ABBBBBBBBBBcccc"); containerPane.add(ULCBoxPane.BOX_EXPAND_EXPAND, textArea ); ULCTextArea setTextArea = new ULCLimitedTextArea(-1); setTextArea.setText(getSampleText()); containerPane.add(ULCBoxPane.BOX_EXPAND_EXPAND, new ULCScrollPane(setTextArea)); return containerPane; } private String getSampleText() { return "This is line 1\n" + "This is line 2 which is much longer than line 1\n" + "This is line 3 which is even longer than line 2 which is longer than line 1\n" + "This is line 4 which is even longer than line 3 which is longer than line 2 which is longer than line 1."; } public static void main(String[] args) { DevelopmentRunner.setApplicationClass(LimitNumCharsOnTextArea.class); DevelopmentRunner.main(args); } public static class ULCLimitedTextArea extends ULCTextArea { private int fMaxLength; public ULCLimitedTextArea(int maxLength) { super(); fMaxLength = maxLength; } protected void uploadStateUI() { super.uploadStateUI(); createStateUI(new Object[] {new Integer(fMaxLength < 0 ? Integer.MAX_VALUE : fMaxLength)}); } protected String typeString() { return UILimitedTextArea.class.getName(); } } public static class UILimitedTextArea extends UITextArea { private int maxLength; protected void postInitializeState() { super.postInitializeState(); getBasicTextArea().setDocument(new LimitedDocument(maxLength)); } protected Object createBasicObject(Object[] args) { maxLength = ((Integer)args[0]).intValue(); return super.createBasicObject(args); } } public static class LimitedDocument extends PlainDocument { private int maxLength; public LimitedDocument(int maxLength) { this.maxLength = maxLength; } // This method is overriden from the super class. It will be called when // you are trying to insert text in your text component (by typing // or pasting). public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { int currentLength = getLength(); if( currentLength >= maxLength ) { // There's not room for more characters. Return. return; } if( currentLength + str.length() > maxLength ) { // All of the characters we are trying to insert will not fit. // We must trim the string. str = str.substring(0, maxLength - currentLength); } // Insert the text: super.insertString(offs, str, a); } } } _______________________________________________ ULC-developer mailing list [email protected] http://lists.canoo.com/mailman/listinfo/ulc-developer
