1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package com.sun.star.wiki;
29 
30 import com.sun.star.awt.XControl;
31 import com.sun.star.awt.XControlContainer;
32 import com.sun.star.awt.XControlModel;
33 import com.sun.star.awt.XDialog;
34 import com.sun.star.awt.XDialogEventHandler;
35 import com.sun.star.awt.XDialogProvider2;
36 import com.sun.star.awt.XThrobber;
37 import com.sun.star.beans.XPropertySet;
38 import com.sun.star.lang.XMultiComponentFactory;
39 import com.sun.star.uno.UnoRuntime;
40 import com.sun.star.uno.XComponentContext;
41 import com.sun.star.awt.XTopWindow;
42 import com.sun.star.awt.XTopWindowListener;
43 import com.sun.star.awt.XWindow;
44 import com.sun.star.container.XNameContainer;
45 import com.sun.star.lang.EventObject;
46 import com.sun.star.lang.XMultiServiceFactory;
47 
48 public class WikiDialog implements XDialogEventHandler, XTopWindowListener
49 {
50     XComponentContext m_xContext;
51     XControlContainer m_xControlContainer;
52     XDialog m_xDialog;
53     String[] m_aMethods;
54     boolean m_bAction = false;
55     Settings m_aSettings;
56 
57     protected Thread m_aThread;
58     protected boolean m_bThreadFinished = false;
59 
60 
61     /** Creates a new instance of WikiDialog */
62     public WikiDialog(XComponentContext c, String DialogURL)
63     {
64         this.m_xContext = c;
65         XMultiComponentFactory xMCF = m_xContext.getServiceManager();
66         m_aSettings = Settings.getSettings(m_xContext);
67         try
68         {
69             Object obj;
70             obj = xMCF.createInstanceWithContext("com.sun.star.awt.DialogProvider2", m_xContext );
71             XDialogProvider2 xDialogProvider = (XDialogProvider2) UnoRuntime.queryInterface( XDialogProvider2.class, obj );
72 
73             m_xDialog = xDialogProvider.createDialogWithHandler( DialogURL, this );
74             m_xControlContainer = (XControlContainer)UnoRuntime.queryInterface( XControlContainer.class, m_xDialog );
75             XTopWindow xTopWindow = (XTopWindow)UnoRuntime.queryInterface( XTopWindow.class, m_xDialog );
76             if ( xTopWindow != null )
77                 xTopWindow.addTopWindowListener( this );
78         }
79         catch (com.sun.star.uno.Exception ex)
80         {
81             ex.printStackTrace();
82         }
83     }
84 
85     public synchronized void ThreadStop( boolean bSelf )
86     {
87         if ( bSelf || m_aThread != null && !m_bThreadFinished )
88         {
89             try
90             {
91                 Helper.AllowConnection( bSelf );
92             }
93             catch( Exception ex )
94             {
95                 ex.printStackTrace();
96             }
97         }
98 
99         m_aThread = null;
100         m_bThreadFinished = true;
101     }
102 
103     protected void setMethods (String [] Methods)
104     {
105         this.m_aMethods = Methods;
106     }
107 
108 
109     public boolean show( )
110     {
111         m_bThreadFinished = false;
112 
113         if( m_xDialog != null ) m_xDialog.execute();
114         return m_bAction;
115     }
116 
117 
118     public String[] getSupportedMethodNames()
119     {
120         return m_aMethods;
121     }
122 
123 
124     public boolean callHandlerMethod( XDialog xDialog, Object EventObject, String MethodName )
125     {
126         return true;
127     }
128 
129     public void SetTitle( String sTitle )
130         throws Exception
131     {
132         SetTitle( m_xDialog, sTitle );
133     }
134 
135     public static void SetTitle( XDialog xDialog, String sTitle )
136         throws Exception
137     {
138         if ( xDialog != null && sTitle != null )
139         {
140             XControl xDialogControl = (XControl)UnoRuntime.queryInterface( XControl.class, xDialog );
141             if ( xDialogControl != null )
142             {
143                 XPropertySet xPropSet = (XPropertySet)UnoRuntime.queryInterface( XPropertySet.class, xDialogControl.getModel() );
144                 if ( xPropSet != null )
145                     xPropSet.setPropertyValue( "Title", sTitle );
146             }
147         }
148     }
149 
150     protected XPropertySet GetPropSet(String sControl)
151     {
152         return GetPropSet( m_xControlContainer, sControl );
153     }
154 
155     protected static XPropertySet GetPropSet( XControlContainer xControlContainer, String sControl )
156     {
157         XPropertySet xPS = null;
158 
159         if ( xControlContainer != null && sControl != null )
160         {
161             XControl xControl = xControlContainer.getControl(sControl);
162             xPS = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xControl.getModel() );
163         }
164 
165         if ( xPS == null )
166             throw new com.sun.star.uno.RuntimeException();
167 
168         return xPS;
169     }
170 
171     public static XDialog CreateSimpleDialog( XComponentContext xContext, String sURL, int nTitleID, String[] pControls, int[] pStringIDs )
172     {
173         XDialog xResult = null;
174 
175         if ( xContext != null && sURL != null && sURL.length() > 0 )
176         {
177             try
178             {
179                 Object oDialogProvider = xContext.getServiceManager().createInstanceWithContext("com.sun.star.awt.DialogProvider2", xContext );
180                 XDialogProvider2 xDialogProvider = (XDialogProvider2) UnoRuntime.queryInterface( XDialogProvider2.class, oDialogProvider );
181 
182                 if ( xDialogProvider != null )
183                     xResult = xDialogProvider.createDialog( sURL );
184 
185                 if ( xResult != null )
186                 {
187                     SetTitle( xResult, Helper.GetLocalizedString( xContext, nTitleID ) );
188                     if ( pControls != null && pStringIDs != null && pControls.length == pStringIDs.length )
189                     {
190                         XControlContainer xControlContainer = (XControlContainer)UnoRuntime.queryInterface( XControlContainer.class, xResult );
191                         for ( int nInd = 0; nInd < pControls.length; nInd++ )
192                             GetPropSet( xControlContainer, pControls[nInd] ).setPropertyValue( "Label", new Integer( pStringIDs[nInd] ) );
193                     }
194                 }
195             }
196             catch (Exception ex)
197             {
198                 ex.printStackTrace();
199             }
200         }
201 
202         return xResult;
203     }
204 
205     protected void InsertThrobber( int X, int Y, int Width, int Height )
206     {
207         try
208         {
209             XControl xDialogControl = ( XControl ) UnoRuntime.queryInterface( XControl.class, m_xDialog );
210             XControlModel xDialogModel = null;
211             if ( xDialogControl != null )
212                 xDialogModel = xDialogControl.getModel();
213 
214             XMultiServiceFactory xDialogFactory = ( XMultiServiceFactory ) UnoRuntime.queryInterface( XMultiServiceFactory.class, xDialogModel );
215             if ( xDialogFactory != null )
216             {
217                 XControlModel xThrobberModel = (XControlModel)UnoRuntime.queryInterface( XControlModel.class, xDialogFactory.createInstance( "com.sun.star.awt.UnoThrobberControlModel" ) );
218                 XPropertySet xThrobberProps = (XPropertySet)UnoRuntime.queryInterface( XPropertySet.class, xThrobberModel );
219                 if ( xThrobberProps != null )
220                 {
221                     xThrobberProps.setPropertyValue( "Name", "WikiThrobber" );
222                     xThrobberProps.setPropertyValue( "PositionX", new Integer( X ) );
223                     xThrobberProps.setPropertyValue( "PositionY", new Integer( Y ) );
224                     xThrobberProps.setPropertyValue( "Height", new Integer( Width ) );
225                     xThrobberProps.setPropertyValue( "Width", new Integer( Height ) );
226 
227                     XNameContainer xDialogContainer = (XNameContainer)UnoRuntime.queryInterface( XNameContainer.class, xDialogModel );
228                     xDialogContainer.insertByName( "WikiThrobber", xThrobberModel );
229                 }
230             }
231         }
232         catch( Exception e )
233         {
234             e.printStackTrace();
235         }
236 
237         SetThrobberVisible( false );
238     }
239 
240     public void SetThrobberActive( boolean bActive )
241     {
242         if ( m_xControlContainer != null )
243         {
244             try
245             {
246                 XThrobber xThrobber = (XThrobber)UnoRuntime.queryInterface( XThrobber.class, m_xControlContainer.getControl( "WikiThrobber" ) );
247                 if ( xThrobber != null )
248                 {
249                     if ( bActive )
250                         xThrobber.start();
251                     else
252                         xThrobber.stop();
253                 }
254             }
255             catch( Exception e )
256             {
257                 e.printStackTrace();
258             }
259         }
260     }
261 
262     public void SetThrobberVisible( boolean bVisible )
263     {
264         if ( m_xControlContainer != null )
265         {
266             try
267             {
268                 XWindow xWindow = (XWindow)UnoRuntime.queryInterface( XWindow.class, m_xControlContainer.getControl( "WikiThrobber" ) );
269                 if ( xWindow != null )
270                     xWindow.setVisible( bVisible );
271             }
272             catch ( Exception e )
273             {
274                 e.printStackTrace();
275             }
276         }
277     }
278 
279     public void SetFocusTo( String aControl )
280     {
281         if ( m_xControlContainer != null )
282         {
283             try
284             {
285                 XWindow xWindow = (XWindow)UnoRuntime.queryInterface( XWindow.class, m_xControlContainer.getControl( aControl ) );
286                 if ( xWindow != null )
287                     xWindow.setFocus();
288             }
289             catch ( Exception e )
290             {
291                 e.printStackTrace();
292             }
293         }
294     }
295 
296     public void DisposeDialog()
297     {
298         Helper.Dispose( m_xDialog );
299     }
300 
301     public void windowOpened( EventObject e )
302     {}
303 
304     public void windowClosing( EventObject e )
305     {}
306 
307     public void windowClosed( EventObject e )
308     {}
309 
310     public void windowMinimized( EventObject e )
311     {}
312 
313     public void windowNormalized( EventObject e )
314     {}
315 
316     public void windowActivated( EventObject e )
317     {}
318 
319     public void windowDeactivated( EventObject e )
320     {}
321 
322     public void disposing( EventObject e )
323     {}
324 }
325 
326