Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/jempeg/util/DocumentActionAdapter.java


1   /**
2   * Copyright (c) 2001, Mike Schrag & Daniel Zimmerman
3   * All rights reserved.
4   *
5   * Redistribution and use in source and binary forms, with or without
6   * modification, are permitted provided that the following conditions are met:
7   *
8   * Redistributions of source code must retain the above copyright notice,
9   * this list of conditions and the following disclaimer.
10  *
11  * Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * Neither the name of Mike Schrag, Daniel Zimmerman, nor the names of any
16  * other contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31  package org.jempeg.util;
32  
33  import javax.swing.event.DocumentEvent;
34  import javax.swing.event.DocumentListener;
35  import javax.swing.text.Document;
36  import javax.swing.text.JTextComponent;
37  
38  /**
39  * Receives documentListener events and fires action performs
40  * from this.  This is used to signal some listener that some
41  * element on a panel changed (without the listener having to
42  * know that it was a document)
43  *
44  * @author Mike Schrag
45  * @version $Revision: 1.5 $
46  */
47  public class DocumentActionAdapter implements DocumentListener {
48    private ActionSource mySource;
49    private boolean myEnabled;
50  
51    public DocumentActionAdapter(ActionSource _source) {
52      mySource = _source;
53      myEnabled = true;
54    }
55  
56    public void listenTo(JTextComponent _textComponent) {
57      listenTo(_textComponent.getDocument());
58    }
59  
60    public void unlistenTo(JTextComponent _textComponent) {
61      unlistenTo(_textComponent.getDocument());
62    }
63    
64    public void listenTo(Document _document) {
65      _document.addDocumentListener(this);
66    }
67    
68    public void unlistenTo(Document _document) {
69      _document.removeDocumentListener(this);
70    }
71    
72    public void setEnabled(boolean _enabled) {
73      myEnabled = _enabled;
74    }
75    
76    public void changedUpdate(DocumentEvent _event) {
77      if (myEnabled) {
78        mySource.fireActionPerformed();
79      }
80    }
81  
82    public void insertUpdate(DocumentEvent _event) {
83      if (myEnabled) {
84        mySource.fireActionPerformed();
85      }
86    }
87  
88    public void removeUpdate(DocumentEvent _event) {
89      if (myEnabled) {
90        mySource.fireActionPerformed();
91      }
92    }
93  }