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

Quick Search    Search Deep

Source code: plugins/Messenger/AbstractMessengerPlugin.java


1   /*
2   This file is part of DeXter - Java Internet Communication Solution
3   Copyright (c) 2002 Tobias Riemer
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  GNU General Public License for more details.
14  
15  You should have received a copy of the GNU Lesser General Public
16  License along with this program; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  */
19  
20  /*
21   * AbstractMessengerPlugin.java
22   *
23   * Created on October 22, 2002, 7:32 PM
24   */
25  
26  package plugins.Messenger;
27  
28  import plugins.Messenger.event.*;
29  
30  /**
31   *
32   * @author  Christoph Walcher
33   */
34  public abstract class AbstractMessengerPlugin implements MessengerPlugin {
35      
36      private boolean enabled = false;
37      private java.util.List stateListeners;
38      
39      protected MessengerPluginProtocol protocol;
40      
41      protected Conversation conversation;
42      
43      /** Creates a new instance of AbstractMessengerPlugin */
44      public AbstractMessengerPlugin() {
45          stateListeners = new java.util.Vector();
46      }
47      
48      /**
49       * State switches from enabled to disabled. This methode is just called if the prior
50       * state was enabled!
51       */
52      public abstract void disable(boolean remote);
53      
54      public abstract void enable(boolean remote);
55      
56      public abstract MessengerPluginProtocol createProtocol();
57      
58      /** Returns an instance of the used Protocol. Be sure to return the same instance on subsequent
59       * calls to this method!
60       */
61      public MessengerPluginProtocol getProtocol() {
62          if (protocol == null) {
63              protocol = createProtocol();
64          }
65          
66          return protocol;
67      }
68      
69      /** Is called if the user decides to enable/disable a Plugin for a Conversation
70       */
71      public void setEnabled(boolean enabled, boolean remote) {
72           if (!(isEnabled() == enabled)) {
73              if (enabled) {    
74                  enable(remote);
75              }
76              else {
77                  disable(remote);
78              }
79             
80              this.enabled = enabled;
81              
82              fireStateChanged(this);
83          }
84      }
85      
86      /** Getter for property enabled.
87       * @return Value of property enabled.
88       */
89      public boolean isEnabled() {
90          return enabled;
91      }
92      
93      public void addStateListener(StateListener listener) {
94          if (listener == null) {
95              throw new IllegalArgumentException("StateListener must not be null");
96          }
97          
98          stateListeners.add(listener);
99      }
100     
101     public StateListener removeStateListener(StateListener listener) {
102         if (listener == null) {
103             throw new IllegalArgumentException("StateListener must not be null");
104         }
105         
106         if (stateListeners.remove(listener)) {
107             return listener;
108         }
109         else {
110             return null;
111         }
112     }
113     
114     protected void fireStateChanged(MessengerPlugin source) {
115         StateEvent e = null;
116         java.util.Iterator listenerIter = stateListeners.iterator();
117         
118         while (listenerIter.hasNext()) {
119             StateListener l = (StateListener)listenerIter.next();
120             if (e == null) {
121                e = new StateEvent(source);
122             }
123             
124             l.stateChanged(e);
125         }
126     }
127     
128     public abstract Object clone();    
129     
130     /** The init method is called after a new Plugin Instance is created. All code
131      * that is needed to create the initial state of the object should go here!
132      */
133     public void init(Conversation conversation) {
134         this.conversation = conversation;
135     }
136     
137     /** Getter for property conversation.
138      * @return Value of property conversation.
139      */
140     public plugins.Messenger.Conversation getConversation() {
141         return conversation;
142     }
143 }