Revision: 591
http://svn.sourceforge.net/magicmap/?rev=591&view=rev
Author: Jan_fride
Date: 2007-03-02 13:22:09 -0800 (Fri, 02 Mar 2007)
Log Message:
-----------
Helper class fpr Swing text documents
Added Paths:
-----------
trunk/magicmapclient/src/net/sf/magicmap/client/utils/DocumentAdapter.java
Added: trunk/magicmapclient/src/net/sf/magicmap/client/utils/DocumentAdapter.java
===================================================================
--- trunk/magicmapclient/src/net/sf/magicmap/client/utils/DocumentAdapter.java (rev 0)
+++ trunk/magicmapclient/src/net/sf/magicmap/client/utils/DocumentAdapter.java 2007-03-02 21:22:09 UTC (rev 591)
@@ -0,0 +1,65 @@
+package net.sf.magicmap.client.utils;
+
+import javax.swing.event.DocumentEvent;
+import javax.swing.event.DocumentListener;
+import javax.swing.text.BadLocationException;
+import javax.swing.text.Document;
+import javax.swing.text.JTextComponent;
+import javax.swing.text.SimpleAttributeSet;
+
+
+/**
+ *
+ * @author Jan Friderici
+ *
+ */
+public abstract class DocumentAdapter implements DocumentListener {
+
+ private final Document document;
+
+ public DocumentAdapter(Document document){
+ this.document = document;
+ document.addDocumentListener(this);
+ }
+
+ public DocumentAdapter(JTextComponent component){
+ this(component.getDocument());
+ }
+
+ public void changedUpdate(DocumentEvent e){
+ handleChange(getDocumentContents());
+ }
+
+ public void insertUpdate(DocumentEvent e){
+ handleChange(getDocumentContents());
+ }
+
+ public void removeUpdate(DocumentEvent e){
+ handleChange(getDocumentContents());
+ }
+ /**
+ * to implement
+ * @param text
+ */
+ public abstract void handleChange(String text);
+
+ public final String getDocumentContents(){
+ try {
+ return document.getText(0, document.getLength());
+ } catch (BadLocationException e) {
+ throw new IllegalStateException(e);
+ }
+ }
+
+ public final synchronized void setDocumentContents(String contents){
+ document.removeDocumentListener(this);
+ try {
+ document.remove(0, document.getLength());
+ document.insertString(0, contents, SimpleAttributeSet.EMPTY);
+ } catch (BadLocationException e) {
+ throw new IllegalStateException(e);
+ }finally{
+ document.addDocumentListener(this);
+ }
+ }
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|