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