1 /*************************************************************************
2  *
3  *  The Contents of this file are made available subject to the terms of
4  *  the BSD license.
5  *
6  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7  *  All rights reserved.
8  *
9  *  Redistribution and use in source and binary forms, with or without
10  *  modification, are permitted provided that the following conditions
11  *  are met:
12  *  1. Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *  2. Redistributions in binary form must reproduce the above copyright
15  *     notice, this list of conditions and the following disclaimer in the
16  *     documentation and/or other materials provided with the distribution.
17  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18  *     contributors may be used to endorse or promote products derived
19  *     from this software without specific prior written permission.
20  *
21  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  *************************************************************************/
34 
35 import com.sun.star.linguistic2.XLinguServiceEventBroadcaster;
36 import com.sun.star.linguistic2.XLinguServiceEventListener;
37 import com.sun.star.linguistic2.LinguServiceEvent;
38 import com.sun.star.beans.XPropertySet;
39 import com.sun.star.beans.XPropertyChangeListener;
40 import com.sun.star.beans.PropertyChangeEvent;
41 import com.sun.star.lang.EventObject;
42 import com.sun.star.uno.XInterface;
43 
44 import java.util.ArrayList;
45 
46 public class PropChgHelper implements
47         XPropertyChangeListener,
48         XLinguServiceEventBroadcaster
49 {
50     XInterface          xEvtSource;
51     String[]            aPropNames;
52     XPropertySet        xPropSet;
53     ArrayList           aLngSvcEvtListeners;
54 
55     public PropChgHelper(
56             XInterface      xEvtSource,
57             String[]        aPropNames )
58     {
59         this.xEvtSource = xEvtSource;
60         this.aPropNames = aPropNames;
61         xPropSet        = null;
62         aLngSvcEvtListeners = new ArrayList();
63     }
64 
65     public XInterface GetEvtSource()
66     {
67         return xEvtSource;
68     }
69 
70     public XPropertySet GetPropSet()
71     {
72         return xPropSet;
73     }
74 
75     public String[] GetPropNames()
76     {
77         return aPropNames;
78     }
79 
80     public void LaunchEvent( LinguServiceEvent aEvt )
81     {
82         int nCnt = aLngSvcEvtListeners.size();
83         for (int i = 0;  i < nCnt;  ++i)
84         {
85             XLinguServiceEventListener xLstnr =
86                     (XLinguServiceEventListener) aLngSvcEvtListeners.get(i);
87             if (xLstnr != null)
88                 xLstnr.processLinguServiceEvent( aEvt );
89         }
90     }
91 
92     public void AddAsListenerTo( XPropertySet xPropertySet )
93     {
94         // do not listen any longer to the old property set (if any)
95         RemoveAsListener();
96 
97         // set new property set to be used and register as listener to it
98         xPropSet = xPropertySet;
99         if (xPropSet != null)
100         {
101             int nLen = aPropNames.length;
102             for (int i = 0;  i < nLen;  ++i)
103             {
104                 if (aPropNames[i].length() != 0)
105                 {
106                     try {
107                         xPropSet.addPropertyChangeListener(
108                                 aPropNames[i], (XPropertyChangeListener) this );
109                     }
110                     catch( Exception e ) {
111                     }
112                 }
113             }
114         }
115     }
116 
117     public void RemoveAsListener()
118     {
119         if (xPropSet != null)
120         {
121             int nLen = aPropNames.length;
122             for (int i = 0;  i < nLen;  ++i)
123             {
124                 if (aPropNames[i].length() != 0)
125                 {
126                     try {
127                         xPropSet.removePropertyChangeListener(
128                                 aPropNames[i], (XPropertyChangeListener) this );
129                     }
130                     catch( Exception e ) {
131                     }
132                 }
133             }
134 
135             xPropSet = null;
136         }
137     }
138 
139     // __________ interface methods __________
140 
141     //***************
142 	// XEventListener
143     //***************
144     public void disposing( EventObject aSource )
145             throws com.sun.star.uno.RuntimeException
146     {
147         if (aSource.Source == xPropSet)
148         {
149             RemoveAsListener();
150         }
151     }
152 
153     //************************
154 	// XPropertyChangeListener
155     //************************
156     public void propertyChange( PropertyChangeEvent aEvt )
157             throws com.sun.star.uno.RuntimeException
158     {
159         // will be overloaded in derived classes
160     }
161 
162     //******************************
163 	// XLinguServiceEventBroadcaster
164     //******************************
165     public boolean addLinguServiceEventListener(
166             XLinguServiceEventListener xListener )
167         throws com.sun.star.uno.RuntimeException
168     {
169         boolean bRes = false;
170         if (xListener != null)
171         {
172             bRes = aLngSvcEvtListeners.add( xListener );
173         }
174         return bRes;
175     }
176 
177     public boolean removeLinguServiceEventListener(
178             XLinguServiceEventListener xListener )
179         throws com.sun.star.uno.RuntimeException
180     {
181         boolean bRes = false;
182         if (xListener != null)
183         {
184             int nIdx = aLngSvcEvtListeners.indexOf( xListener );
185             if (nIdx != -1)
186             {
187                 aLngSvcEvtListeners.remove( nIdx );
188                 bRes = true;
189             }
190         }
191         return bRes;
192     }
193 };
194 
195