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 package com.sun.star.wiki;
25 
26 import com.sun.star.awt.XControl;
27 import com.sun.star.awt.XControlContainer;
28 import com.sun.star.awt.XControlModel;
29 import com.sun.star.awt.XDialog;
30 import com.sun.star.awt.XDialogEventHandler;
31 import com.sun.star.awt.XDialogProvider2;
32 import com.sun.star.awt.XThrobber;
33 import com.sun.star.beans.XPropertySet;
34 import com.sun.star.lang.XMultiComponentFactory;
35 import com.sun.star.uno.UnoRuntime;
36 import com.sun.star.uno.XComponentContext;
37 import com.sun.star.awt.XTopWindow;
38 import com.sun.star.awt.XTopWindowListener;
39 import com.sun.star.awt.XWindow;
40 import com.sun.star.container.XNameContainer;
41 import com.sun.star.lang.EventObject;
42 import com.sun.star.lang.XMultiServiceFactory;
43 
44 public class WikiDialog implements XDialogEventHandler, XTopWindowListener
45 {
46     XComponentContext m_xContext;
47     XControlContainer m_xControlContainer;
48     XDialog m_xDialog;
49     String[] m_aMethods;
50     boolean m_bAction = false;
51     Settings m_aSettings;
52 
53     protected Thread m_aThread;
54     protected boolean m_bThreadFinished = false;
55 
56 
57     /** Creates a new instance of WikiDialog */
58     public WikiDialog(XComponentContext c, String DialogURL)
59     {
60         this.m_xContext = c;
61         XMultiComponentFactory xMCF = m_xContext.getServiceManager();
62         m_aSettings = Settings.getSettings(m_xContext);
63         try
64         {
65             Object obj;
66             obj = xMCF.createInstanceWithContext("com.sun.star.awt.DialogProvider2", m_xContext );
67             XDialogProvider2 xDialogProvider = (XDialogProvider2) UnoRuntime.queryInterface( XDialogProvider2.class, obj );
68 
69             m_xDialog = xDialogProvider.createDialogWithHandler( DialogURL, this );
70             m_xControlContainer = (XControlContainer)UnoRuntime.queryInterface( XControlContainer.class, m_xDialog );
71             XTopWindow xTopWindow = (XTopWindow)UnoRuntime.queryInterface( XTopWindow.class, m_xDialog );
72             if ( xTopWindow != null )
73                 xTopWindow.addTopWindowListener( this );
74         }
75         catch (com.sun.star.uno.Exception ex)
76         {
77             ex.printStackTrace();
78         }
79     }
80 
81     public synchronized void ThreadStop( boolean bSelf )
82     {
83         if ( bSelf || m_aThread != null && !m_bThreadFinished )
84         {
85             try
86             {
87                 Helper.AllowConnection( bSelf );
88             }
89             catch( Exception ex )
90             {
91                 ex.printStackTrace();
92             }
93         }
94 
95         m_aThread = null;
96         m_bThreadFinished = true;
97     }
98 
99     protected void setMethods (String [] Methods)
100     {
101         this.m_aMethods = Methods;
102     }
103 
104 
105     public boolean show( )
106     {
107         m_bThreadFinished = false;
108 
109         if( m_xDialog != null ) m_xDialog.execute();
110         return m_bAction;
111     }
112 
113 
114     public String[] getSupportedMethodNames()
115     {
116         return m_aMethods;
117     }
118 
119 
120     public boolean callHandlerMethod( XDialog xDialog, Object EventObject, String MethodName )
121     {
122         return true;
123     }
124 
125     public void SetTitle( String sTitle )
126         throws Exception
127     {
128         SetTitle( m_xDialog, sTitle );
129     }
130 
131     public static void SetTitle( XDialog xDialog, String sTitle )
132         throws Exception
133     {
134         if ( xDialog != null && sTitle != null )
135         {
136             XControl xDialogControl = (XControl)UnoRuntime.queryInterface( XControl.class, xDialog );
137             if ( xDialogControl != null )
138             {
139                 XPropertySet xPropSet = (XPropertySet)UnoRuntime.queryInterface( XPropertySet.class, xDialogControl.getModel() );
140                 if ( xPropSet != null )
141                     xPropSet.setPropertyValue( "Title", sTitle );
142             }
143         }
144     }
145 
146     protected XPropertySet GetPropSet(String sControl)
147     {
148         return GetPropSet( m_xControlContainer, sControl );
149     }
150 
151     protected static XPropertySet GetPropSet( XControlContainer xControlContainer, String sControl )
152     {
153         XPropertySet xPS = null;
154 
155         if ( xControlContainer != null && sControl != null )
156         {
157             XControl xControl = xControlContainer.getControl(sControl);
158             xPS = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xControl.getModel() );
159         }
160 
161         if ( xPS == null )
162             throw new com.sun.star.uno.RuntimeException();
163 
164         return xPS;
165     }
166 
167     public static XDialog CreateSimpleDialog( XComponentContext xContext, String sURL, int nTitleID, String[] pControls, int[] pStringIDs )
168     {
169         XDialog xResult = null;
170 
171         if ( xContext != null && sURL != null && sURL.length() > 0 )
172         {
173             try
174             {
175                 Object oDialogProvider = xContext.getServiceManager().createInstanceWithContext("com.sun.star.awt.DialogProvider2", xContext );
176                 XDialogProvider2 xDialogProvider = (XDialogProvider2) UnoRuntime.queryInterface( XDialogProvider2.class, oDialogProvider );
177 
178                 if ( xDialogProvider != null )
179                     xResult = xDialogProvider.createDialog( sURL );
180 
181                 if ( xResult != null )
182                 {
183                     SetTitle( xResult, Helper.GetLocalizedString( xContext, nTitleID ) );
184                     if ( pControls != null && pStringIDs != null && pControls.length == pStringIDs.length )
185                     {
186                         XControlContainer xControlContainer = (XControlContainer)UnoRuntime.queryInterface( XControlContainer.class, xResult );
187                         for ( int nInd = 0; nInd < pControls.length; nInd++ )
188                             GetPropSet( xControlContainer, pControls[nInd] ).setPropertyValue( "Label", new Integer( pStringIDs[nInd] ) );
189                     }
190                 }
191             }
192             catch (Exception ex)
193             {
194                 ex.printStackTrace();
195             }
196         }
197 
198         return xResult;
199     }
200 
201     protected void InsertThrobber( int X, int Y, int Width, int Height )
202     {
203         try
204         {
205             XControl xDialogControl = ( XControl ) UnoRuntime.queryInterface( XControl.class, m_xDialog );
206             XControlModel xDialogModel = null;
207             if ( xDialogControl != null )
208                 xDialogModel = xDialogControl.getModel();
209 
210             XMultiServiceFactory xDialogFactory = ( XMultiServiceFactory ) UnoRuntime.queryInterface( XMultiServiceFactory.class, xDialogModel );
211             if ( xDialogFactory != null )
212             {
213                 XControlModel xThrobberModel = (XControlModel)UnoRuntime.queryInterface( XControlModel.class, xDialogFactory.createInstance( "com.sun.star.awt.UnoThrobberControlModel" ) );
214                 XPropertySet xThrobberProps = (XPropertySet)UnoRuntime.queryInterface( XPropertySet.class, xThrobberModel );
215                 if ( xThrobberProps != null )
216                 {
217                     xThrobberProps.setPropertyValue( "Name", "WikiThrobber" );
218                     xThrobberProps.setPropertyValue( "PositionX", new Integer( X ) );
219                     xThrobberProps.setPropertyValue( "PositionY", new Integer( Y ) );
220                     xThrobberProps.setPropertyValue( "Height", new Integer( Width ) );
221                     xThrobberProps.setPropertyValue( "Width", new Integer( Height ) );
222 
223                     XNameContainer xDialogContainer = (XNameContainer)UnoRuntime.queryInterface( XNameContainer.class, xDialogModel );
224                     xDialogContainer.insertByName( "WikiThrobber", xThrobberModel );
225                 }
226             }
227         }
228         catch( Exception e )
229         {
230             e.printStackTrace();
231         }
232 
233         SetThrobberVisible( false );
234     }
235 
236     public void SetThrobberActive( boolean bActive )
237     {
238         if ( m_xControlContainer != null )
239         {
240             try
241             {
242                 XThrobber xThrobber = (XThrobber)UnoRuntime.queryInterface( XThrobber.class, m_xControlContainer.getControl( "WikiThrobber" ) );
243                 if ( xThrobber != null )
244                 {
245                     if ( bActive )
246                         xThrobber.start();
247                     else
248                         xThrobber.stop();
249                 }
250             }
251             catch( Exception e )
252             {
253                 e.printStackTrace();
254             }
255         }
256     }
257 
258     public void SetThrobberVisible( boolean bVisible )
259     {
260         if ( m_xControlContainer != null )
261         {
262             try
263             {
264                 XWindow xWindow = (XWindow)UnoRuntime.queryInterface( XWindow.class, m_xControlContainer.getControl( "WikiThrobber" ) );
265                 if ( xWindow != null )
266                     xWindow.setVisible( bVisible );
267             }
268             catch ( Exception e )
269             {
270                 e.printStackTrace();
271             }
272         }
273     }
274 
275     public void SetFocusTo( String aControl )
276     {
277         if ( m_xControlContainer != null )
278         {
279             try
280             {
281                 XWindow xWindow = (XWindow)UnoRuntime.queryInterface( XWindow.class, m_xControlContainer.getControl( aControl ) );
282                 if ( xWindow != null )
283                     xWindow.setFocus();
284             }
285             catch ( Exception e )
286             {
287                 e.printStackTrace();
288             }
289         }
290     }
291 
292     public void DisposeDialog()
293     {
294         Helper.Dispose( m_xDialog );
295     }
296 
297     public void windowOpened( EventObject e )
298     {}
299 
300     public void windowClosing( EventObject e )
301     {}
302 
303     public void windowClosed( EventObject e )
304     {}
305 
306     public void windowMinimized( EventObject e )
307     {}
308 
309     public void windowNormalized( EventObject e )
310     {}
311 
312     public void windowActivated( EventObject e )
313     {}
314 
315     public void windowDeactivated( EventObject e )
316     {}
317 
318     public void disposing( EventObject e )
319     {}
320 }
321 
322