xref: /AOO41X/main/swext/mediawiki/src/com/sun/star/wiki/MainThreadDialogExecutor.java (revision 47148b3bc50811ceb41802e4cc50a5db21535900)
113efc523SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
313efc523SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
413efc523SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
513efc523SAndrew Rist  * distributed with this work for additional information
613efc523SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
713efc523SAndrew Rist  * to you under the Apache License, Version 2.0 (the
813efc523SAndrew Rist  * "License"); you may not use this file except in compliance
913efc523SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
1113efc523SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
1313efc523SAndrew Rist  * Unless required by applicable law or agreed to in writing,
1413efc523SAndrew Rist  * software distributed under the License is distributed on an
1513efc523SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1613efc523SAndrew Rist  * KIND, either express or implied.  See the License for the
1713efc523SAndrew Rist  * specific language governing permissions and limitations
1813efc523SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
2013efc523SAndrew Rist  *************************************************************/
2113efc523SAndrew Rist 
2213efc523SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package com.sun.star.wiki;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import com.sun.star.uno.Any;
27cdf0e10cSrcweir import com.sun.star.awt.XDialog;
28cdf0e10cSrcweir import com.sun.star.awt.XCallback;
29cdf0e10cSrcweir import com.sun.star.awt.XMessageBox;
30cdf0e10cSrcweir import com.sun.star.awt.XRequestCallback;
31cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory;
32cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
33cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
34cdf0e10cSrcweir 
35cdf0e10cSrcweir public class MainThreadDialogExecutor implements XCallback
36cdf0e10cSrcweir {
37cdf0e10cSrcweir     private WikiDialog m_aWikiDialog;
38cdf0e10cSrcweir     private XDialog    m_xDialog;
39cdf0e10cSrcweir     private XMessageBox m_xMessageBox;
40cdf0e10cSrcweir     private boolean    m_bResult = false;
41cdf0e10cSrcweir     private boolean    m_bCalled = false;
42cdf0e10cSrcweir     private boolean    m_bClose = false;
43cdf0e10cSrcweir 
Show( XComponentContext xContext, WikiDialog aWikiDialog )44cdf0e10cSrcweir     static public boolean Show( XComponentContext xContext, WikiDialog aWikiDialog )
45cdf0e10cSrcweir     {
46cdf0e10cSrcweir         MainThreadDialogExecutor aExecutor = new MainThreadDialogExecutor( aWikiDialog );
47cdf0e10cSrcweir         return GetCallback( xContext, aExecutor );
48cdf0e10cSrcweir     }
49cdf0e10cSrcweir 
Execute( XComponentContext xContext, XDialog xDialog )50cdf0e10cSrcweir     static public boolean Execute( XComponentContext xContext, XDialog xDialog )
51cdf0e10cSrcweir     {
52cdf0e10cSrcweir         MainThreadDialogExecutor aExecutor = new MainThreadDialogExecutor( xDialog );
53cdf0e10cSrcweir         return GetCallback( xContext, aExecutor );
54cdf0e10cSrcweir     }
55cdf0e10cSrcweir 
Execute( XComponentContext xContext, XMessageBox xMessageBox )56cdf0e10cSrcweir     static public boolean Execute( XComponentContext xContext, XMessageBox xMessageBox )
57cdf0e10cSrcweir     {
58cdf0e10cSrcweir         MainThreadDialogExecutor aExecutor = new MainThreadDialogExecutor( xMessageBox );
59cdf0e10cSrcweir         return GetCallback( xContext, aExecutor );
60cdf0e10cSrcweir     }
61cdf0e10cSrcweir 
Close( XComponentContext xContext, XDialog xDialog )62cdf0e10cSrcweir     static public boolean Close( XComponentContext xContext, XDialog xDialog )
63cdf0e10cSrcweir     {
64cdf0e10cSrcweir         MainThreadDialogExecutor aExecutor = new MainThreadDialogExecutor( xDialog, true );
65cdf0e10cSrcweir         return GetCallback( xContext, aExecutor );
66cdf0e10cSrcweir     }
67cdf0e10cSrcweir 
GetCallback( XComponentContext xContext, MainThreadDialogExecutor aExecutor )68cdf0e10cSrcweir     static private boolean GetCallback( XComponentContext xContext, MainThreadDialogExecutor aExecutor )
69cdf0e10cSrcweir     {
70cdf0e10cSrcweir         try
71cdf0e10cSrcweir         {
72cdf0e10cSrcweir             if ( aExecutor != null )
73cdf0e10cSrcweir             {
74cdf0e10cSrcweir                 String aThreadName = null;
75cdf0e10cSrcweir                 Thread aCurThread = Thread.currentThread();
76cdf0e10cSrcweir                 if ( aCurThread != null )
77cdf0e10cSrcweir                     aThreadName = aCurThread.getName();
78cdf0e10cSrcweir 
79cdf0e10cSrcweir                 if ( aThreadName != null && aThreadName.equals( "com.sun.star.thread.WikiEditorSendingThread" ) )
80cdf0e10cSrcweir                 {
81cdf0e10cSrcweir                     // the main thread should be accessed asynchronously
82cdf0e10cSrcweir                     XMultiComponentFactory xFactory = xContext.getServiceManager();
83cdf0e10cSrcweir                     if ( xFactory == null )
84cdf0e10cSrcweir                         throw new com.sun.star.uno.RuntimeException();
85cdf0e10cSrcweir 
86cdf0e10cSrcweir                     XRequestCallback xRequest = (XRequestCallback)UnoRuntime.queryInterface(
87cdf0e10cSrcweir                         XRequestCallback.class,
88cdf0e10cSrcweir                         xFactory.createInstanceWithContext( "com.sun.star.awt.AsyncCallback", xContext ) );
89cdf0e10cSrcweir                     if ( xRequest != null )
90cdf0e10cSrcweir                     {
91cdf0e10cSrcweir                         xRequest.addCallback( aExecutor, Any.VOID );
92cdf0e10cSrcweir                         do
93cdf0e10cSrcweir                         {
94cdf0e10cSrcweir                             Thread.yield();
95cdf0e10cSrcweir                         }
96cdf0e10cSrcweir                         while( !aExecutor.m_bCalled );
97cdf0e10cSrcweir                     }
98cdf0e10cSrcweir                 }
99cdf0e10cSrcweir                 else
100cdf0e10cSrcweir                 {
101cdf0e10cSrcweir                     // handle it as a main thread
102cdf0e10cSrcweir                     aExecutor.notify( Any.VOID );
103cdf0e10cSrcweir                 }
104cdf0e10cSrcweir             }
105cdf0e10cSrcweir         }
106cdf0e10cSrcweir         catch( Exception e )
107cdf0e10cSrcweir         {
108cdf0e10cSrcweir             e.printStackTrace();
109cdf0e10cSrcweir         }
110cdf0e10cSrcweir 
111cdf0e10cSrcweir         return aExecutor.GetResult();
112cdf0e10cSrcweir     }
113cdf0e10cSrcweir 
MainThreadDialogExecutor( WikiDialog aWikiDialog )114cdf0e10cSrcweir     private MainThreadDialogExecutor( WikiDialog aWikiDialog )
115cdf0e10cSrcweir     {
116cdf0e10cSrcweir         m_aWikiDialog = aWikiDialog;
117cdf0e10cSrcweir     }
118cdf0e10cSrcweir 
MainThreadDialogExecutor( XDialog xDialog )119cdf0e10cSrcweir     private MainThreadDialogExecutor( XDialog xDialog )
120cdf0e10cSrcweir     {
121cdf0e10cSrcweir         m_xDialog = xDialog;
122cdf0e10cSrcweir     }
123cdf0e10cSrcweir 
MainThreadDialogExecutor( XDialog xDialog, boolean bClose )124cdf0e10cSrcweir     private MainThreadDialogExecutor( XDialog xDialog, boolean bClose )
125cdf0e10cSrcweir     {
126cdf0e10cSrcweir         m_xDialog = xDialog;
127cdf0e10cSrcweir         m_bClose = true;
128cdf0e10cSrcweir         m_bCalled = true; // no yielding, asynchronous closing
129cdf0e10cSrcweir     }
130cdf0e10cSrcweir 
MainThreadDialogExecutor( XMessageBox xMessageBox )131cdf0e10cSrcweir     private MainThreadDialogExecutor( XMessageBox xMessageBox )
132cdf0e10cSrcweir     {
133cdf0e10cSrcweir         m_xMessageBox = xMessageBox;
134cdf0e10cSrcweir     }
135cdf0e10cSrcweir 
GetResult()136cdf0e10cSrcweir     private boolean GetResult()
137cdf0e10cSrcweir     {
138cdf0e10cSrcweir         return m_bResult;
139cdf0e10cSrcweir     }
140cdf0e10cSrcweir 
notify( Object aData )141cdf0e10cSrcweir     public void notify( Object aData )
142cdf0e10cSrcweir     {
143cdf0e10cSrcweir         if ( m_aWikiDialog != null )
144cdf0e10cSrcweir             m_bResult = m_aWikiDialog.show();
145cdf0e10cSrcweir         else if ( m_xDialog != null )
146cdf0e10cSrcweir         {
147cdf0e10cSrcweir             if ( !m_bClose )
148cdf0e10cSrcweir                 m_bResult = ( m_xDialog.execute() == 1 );
149cdf0e10cSrcweir             else
150cdf0e10cSrcweir             {
151cdf0e10cSrcweir                 try
152cdf0e10cSrcweir                 {
153cdf0e10cSrcweir                     m_xDialog.endExecute();
154cdf0e10cSrcweir                 }
155cdf0e10cSrcweir                 catch( Exception e )
156cdf0e10cSrcweir                 {
157cdf0e10cSrcweir                     e.printStackTrace();
158cdf0e10cSrcweir                 }
159cdf0e10cSrcweir                 m_bResult = true;
160cdf0e10cSrcweir             }
161cdf0e10cSrcweir         }
162cdf0e10cSrcweir         else if ( m_xMessageBox != null )
163cdf0e10cSrcweir         {
164cdf0e10cSrcweir             int nRes = m_xMessageBox.execute();
165*2329db7fSAriel Constenla-Haile             m_bResult = ( nRes == com.sun.star.awt.MessageBoxResults.OK
166*2329db7fSAriel Constenla-Haile                           || nRes == com.sun.star.awt.MessageBoxResults.YES );
167cdf0e10cSrcweir         }
168cdf0e10cSrcweir 
169cdf0e10cSrcweir         m_bCalled = true;
170cdf0e10cSrcweir     }
171cdf0e10cSrcweir };
172cdf0e10cSrcweir 
173