1*34dd1e25SAndrew Rist /**************************************************************
2*34dd1e25SAndrew Rist  *
3*34dd1e25SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*34dd1e25SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*34dd1e25SAndrew Rist  * distributed with this work for additional information
6*34dd1e25SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*34dd1e25SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*34dd1e25SAndrew Rist  * "License"); you may not use this file except in compliance
9*34dd1e25SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*34dd1e25SAndrew Rist  *
11*34dd1e25SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*34dd1e25SAndrew Rist  *
13*34dd1e25SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*34dd1e25SAndrew Rist  * software distributed under the License is distributed on an
15*34dd1e25SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*34dd1e25SAndrew Rist  * KIND, either express or implied.  See the License for the
17*34dd1e25SAndrew Rist  * specific language governing permissions and limitations
18*34dd1e25SAndrew Rist  * under the License.
19*34dd1e25SAndrew Rist  *
20*34dd1e25SAndrew Rist  *************************************************************/
21*34dd1e25SAndrew Rist 
22*34dd1e25SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir import com.sun.star.linguistic2.XLinguServiceEventBroadcaster;
25cdf0e10cSrcweir import com.sun.star.linguistic2.XLinguServiceEventListener;
26cdf0e10cSrcweir import com.sun.star.linguistic2.LinguServiceEvent;
27cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
28cdf0e10cSrcweir import com.sun.star.beans.XPropertyChangeListener;
29cdf0e10cSrcweir import com.sun.star.beans.PropertyChangeEvent;
30cdf0e10cSrcweir import com.sun.star.lang.EventObject;
31cdf0e10cSrcweir import com.sun.star.uno.XInterface;
32cdf0e10cSrcweir 
33cdf0e10cSrcweir import java.util.ArrayList;
34cdf0e10cSrcweir 
35cdf0e10cSrcweir public class PropChgHelper implements
36cdf0e10cSrcweir         XPropertyChangeListener,
37cdf0e10cSrcweir         XLinguServiceEventBroadcaster
38cdf0e10cSrcweir {
39cdf0e10cSrcweir     XInterface          xEvtSource;
40cdf0e10cSrcweir     String[]            aPropNames;
41cdf0e10cSrcweir     XPropertySet        xPropSet;
42cdf0e10cSrcweir     ArrayList           aLngSvcEvtListeners;
43cdf0e10cSrcweir 
PropChgHelper( XInterface xEvtSource, String[] aPropNames )44cdf0e10cSrcweir     public PropChgHelper(
45cdf0e10cSrcweir             XInterface      xEvtSource,
46cdf0e10cSrcweir             String[]        aPropNames )
47cdf0e10cSrcweir     {
48cdf0e10cSrcweir         this.xEvtSource = xEvtSource;
49cdf0e10cSrcweir         this.aPropNames = aPropNames;
50cdf0e10cSrcweir         xPropSet        = null;
51cdf0e10cSrcweir         aLngSvcEvtListeners = new ArrayList();
52cdf0e10cSrcweir     }
53cdf0e10cSrcweir 
GetEvtSource()54cdf0e10cSrcweir     public XInterface GetEvtSource()
55cdf0e10cSrcweir     {
56cdf0e10cSrcweir         return xEvtSource;
57cdf0e10cSrcweir     }
58cdf0e10cSrcweir 
GetPropSet()59cdf0e10cSrcweir     public XPropertySet GetPropSet()
60cdf0e10cSrcweir     {
61cdf0e10cSrcweir         return xPropSet;
62cdf0e10cSrcweir     }
63cdf0e10cSrcweir 
GetPropNames()64cdf0e10cSrcweir     public String[] GetPropNames()
65cdf0e10cSrcweir     {
66cdf0e10cSrcweir         return aPropNames;
67cdf0e10cSrcweir     }
68cdf0e10cSrcweir 
LaunchEvent( LinguServiceEvent aEvt )69cdf0e10cSrcweir     public void LaunchEvent( LinguServiceEvent aEvt )
70cdf0e10cSrcweir     {
71cdf0e10cSrcweir         int nCnt = aLngSvcEvtListeners.size();
72cdf0e10cSrcweir         for (int i = 0;  i < nCnt;  ++i)
73cdf0e10cSrcweir         {
74cdf0e10cSrcweir             XLinguServiceEventListener xLstnr =
75cdf0e10cSrcweir                     (XLinguServiceEventListener) aLngSvcEvtListeners.get(i);
76cdf0e10cSrcweir             if (xLstnr != null)
77cdf0e10cSrcweir                 xLstnr.processLinguServiceEvent( aEvt );
78cdf0e10cSrcweir         }
79cdf0e10cSrcweir     }
80cdf0e10cSrcweir 
AddAsListenerTo( XPropertySet xPropertySet )81cdf0e10cSrcweir     public void AddAsListenerTo( XPropertySet xPropertySet )
82cdf0e10cSrcweir     {
83cdf0e10cSrcweir         // do not listen any longer to the old property set (if any)
84cdf0e10cSrcweir         RemoveAsListener();
85cdf0e10cSrcweir 
86cdf0e10cSrcweir         // set new property set to be used and register as listener to it
87cdf0e10cSrcweir         xPropSet = xPropertySet;
88cdf0e10cSrcweir         if (xPropSet != null)
89cdf0e10cSrcweir         {
90cdf0e10cSrcweir             int nLen = aPropNames.length;
91cdf0e10cSrcweir             for (int i = 0;  i < nLen;  ++i)
92cdf0e10cSrcweir             {
93cdf0e10cSrcweir                 if (aPropNames[i].length() != 0)
94cdf0e10cSrcweir                 {
95cdf0e10cSrcweir                     try {
96cdf0e10cSrcweir                         xPropSet.addPropertyChangeListener(
97cdf0e10cSrcweir                                 aPropNames[i], (XPropertyChangeListener) this );
98cdf0e10cSrcweir                     }
99cdf0e10cSrcweir                     catch( Exception e ) {
100cdf0e10cSrcweir                     }
101cdf0e10cSrcweir                 }
102cdf0e10cSrcweir             }
103cdf0e10cSrcweir         }
104cdf0e10cSrcweir     }
105cdf0e10cSrcweir 
RemoveAsListener()106cdf0e10cSrcweir     public void RemoveAsListener()
107cdf0e10cSrcweir     {
108cdf0e10cSrcweir         if (xPropSet != null)
109cdf0e10cSrcweir         {
110cdf0e10cSrcweir             int nLen = aPropNames.length;
111cdf0e10cSrcweir             for (int i = 0;  i < nLen;  ++i)
112cdf0e10cSrcweir             {
113cdf0e10cSrcweir                 if (aPropNames[i].length() != 0)
114cdf0e10cSrcweir                 {
115cdf0e10cSrcweir                     try {
116cdf0e10cSrcweir                         xPropSet.removePropertyChangeListener(
117cdf0e10cSrcweir                                 aPropNames[i], (XPropertyChangeListener) this );
118cdf0e10cSrcweir                     }
119cdf0e10cSrcweir                     catch( Exception e ) {
120cdf0e10cSrcweir                     }
121cdf0e10cSrcweir                 }
122cdf0e10cSrcweir             }
123cdf0e10cSrcweir 
124cdf0e10cSrcweir             xPropSet = null;
125cdf0e10cSrcweir         }
126cdf0e10cSrcweir     }
127cdf0e10cSrcweir 
128cdf0e10cSrcweir     // __________ interface methods __________
129cdf0e10cSrcweir 
130cdf0e10cSrcweir     //***************
131cdf0e10cSrcweir 	// XEventListener
132cdf0e10cSrcweir     //***************
disposing( EventObject aSource )133cdf0e10cSrcweir     public void disposing( EventObject aSource )
134cdf0e10cSrcweir             throws com.sun.star.uno.RuntimeException
135cdf0e10cSrcweir     {
136cdf0e10cSrcweir         if (aSource.Source == xPropSet)
137cdf0e10cSrcweir         {
138cdf0e10cSrcweir             RemoveAsListener();
139cdf0e10cSrcweir         }
140cdf0e10cSrcweir     }
141cdf0e10cSrcweir 
142cdf0e10cSrcweir     //************************
143cdf0e10cSrcweir 	// XPropertyChangeListener
144cdf0e10cSrcweir     //************************
propertyChange( PropertyChangeEvent aEvt )145cdf0e10cSrcweir     public void propertyChange( PropertyChangeEvent aEvt )
146cdf0e10cSrcweir             throws com.sun.star.uno.RuntimeException
147cdf0e10cSrcweir     {
148cdf0e10cSrcweir         // will be overloaded in derived classes
149cdf0e10cSrcweir     }
150cdf0e10cSrcweir 
151cdf0e10cSrcweir     //******************************
152cdf0e10cSrcweir 	// XLinguServiceEventBroadcaster
153cdf0e10cSrcweir     //******************************
addLinguServiceEventListener( XLinguServiceEventListener xListener )154cdf0e10cSrcweir     public boolean addLinguServiceEventListener(
155cdf0e10cSrcweir             XLinguServiceEventListener xListener )
156cdf0e10cSrcweir         throws com.sun.star.uno.RuntimeException
157cdf0e10cSrcweir     {
158cdf0e10cSrcweir         boolean bRes = false;
159cdf0e10cSrcweir         if (xListener != null)
160cdf0e10cSrcweir         {
161cdf0e10cSrcweir             bRes = aLngSvcEvtListeners.add( xListener );
162cdf0e10cSrcweir         }
163cdf0e10cSrcweir         return bRes;
164cdf0e10cSrcweir     }
165cdf0e10cSrcweir 
removeLinguServiceEventListener( XLinguServiceEventListener xListener )166cdf0e10cSrcweir     public boolean removeLinguServiceEventListener(
167cdf0e10cSrcweir             XLinguServiceEventListener xListener )
168cdf0e10cSrcweir         throws com.sun.star.uno.RuntimeException
169cdf0e10cSrcweir     {
170cdf0e10cSrcweir         boolean bRes = false;
171cdf0e10cSrcweir         if (xListener != null)
172cdf0e10cSrcweir         {
173cdf0e10cSrcweir             int nIdx = aLngSvcEvtListeners.indexOf( xListener );
174cdf0e10cSrcweir             if (nIdx != -1)
175cdf0e10cSrcweir             {
176cdf0e10cSrcweir                 aLngSvcEvtListeners.remove( nIdx );
177cdf0e10cSrcweir                 bRes = true;
178cdf0e10cSrcweir             }
179cdf0e10cSrcweir         }
180cdf0e10cSrcweir         return bRes;
181cdf0e10cSrcweir     }
182cdf0e10cSrcweir };
183cdf0e10cSrcweir 
184