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.uno.Any;
27 import com.sun.star.awt.XDialog;
28 import com.sun.star.awt.XCallback;
29 import com.sun.star.awt.XMessageBox;
30 import com.sun.star.awt.XRequestCallback;
31 import com.sun.star.lang.XMultiComponentFactory;
32 import com.sun.star.uno.UnoRuntime;
33 import com.sun.star.uno.XComponentContext;
34 
35 public class MainThreadDialogExecutor implements XCallback
36 {
37     private WikiDialog m_aWikiDialog;
38     private XDialog    m_xDialog;
39     private XMessageBox m_xMessageBox;
40     private boolean    m_bResult = false;
41     private boolean    m_bCalled = false;
42     private boolean    m_bClose = false;
43 
Show( XComponentContext xContext, WikiDialog aWikiDialog )44     static public boolean Show( XComponentContext xContext, WikiDialog aWikiDialog )
45     {
46         MainThreadDialogExecutor aExecutor = new MainThreadDialogExecutor( aWikiDialog );
47         return GetCallback( xContext, aExecutor );
48     }
49 
Execute( XComponentContext xContext, XDialog xDialog )50     static public boolean Execute( XComponentContext xContext, XDialog xDialog )
51     {
52         MainThreadDialogExecutor aExecutor = new MainThreadDialogExecutor( xDialog );
53         return GetCallback( xContext, aExecutor );
54     }
55 
Execute( XComponentContext xContext, XMessageBox xMessageBox )56     static public boolean Execute( XComponentContext xContext, XMessageBox xMessageBox )
57     {
58         MainThreadDialogExecutor aExecutor = new MainThreadDialogExecutor( xMessageBox );
59         return GetCallback( xContext, aExecutor );
60     }
61 
Close( XComponentContext xContext, XDialog xDialog )62     static public boolean Close( XComponentContext xContext, XDialog xDialog )
63     {
64         MainThreadDialogExecutor aExecutor = new MainThreadDialogExecutor( xDialog, true );
65         return GetCallback( xContext, aExecutor );
66     }
67 
GetCallback( XComponentContext xContext, MainThreadDialogExecutor aExecutor )68     static private boolean GetCallback( XComponentContext xContext, MainThreadDialogExecutor aExecutor )
69     {
70         try
71         {
72             if ( aExecutor != null )
73             {
74                 String aThreadName = null;
75                 Thread aCurThread = Thread.currentThread();
76                 if ( aCurThread != null )
77                     aThreadName = aCurThread.getName();
78 
79                 if ( aThreadName != null && aThreadName.equals( "com.sun.star.thread.WikiEditorSendingThread" ) )
80                 {
81                     // the main thread should be accessed asynchronously
82                     XMultiComponentFactory xFactory = xContext.getServiceManager();
83                     if ( xFactory == null )
84                         throw new com.sun.star.uno.RuntimeException();
85 
86                     XRequestCallback xRequest = (XRequestCallback)UnoRuntime.queryInterface(
87                         XRequestCallback.class,
88                         xFactory.createInstanceWithContext( "com.sun.star.awt.AsyncCallback", xContext ) );
89                     if ( xRequest != null )
90                     {
91                         xRequest.addCallback( aExecutor, Any.VOID );
92                         do
93                         {
94                             Thread.yield();
95                         }
96                         while( !aExecutor.m_bCalled );
97                     }
98                 }
99                 else
100                 {
101                     // handle it as a main thread
102                     aExecutor.notify( Any.VOID );
103                 }
104             }
105         }
106         catch( Exception e )
107         {
108             e.printStackTrace();
109         }
110 
111         return aExecutor.GetResult();
112     }
113 
MainThreadDialogExecutor( WikiDialog aWikiDialog )114     private MainThreadDialogExecutor( WikiDialog aWikiDialog )
115     {
116         m_aWikiDialog = aWikiDialog;
117     }
118 
MainThreadDialogExecutor( XDialog xDialog )119     private MainThreadDialogExecutor( XDialog xDialog )
120     {
121         m_xDialog = xDialog;
122     }
123 
MainThreadDialogExecutor( XDialog xDialog, boolean bClose )124     private MainThreadDialogExecutor( XDialog xDialog, boolean bClose )
125     {
126         m_xDialog = xDialog;
127         m_bClose = true;
128         m_bCalled = true; // no yielding, asynchronous closing
129     }
130 
MainThreadDialogExecutor( XMessageBox xMessageBox )131     private MainThreadDialogExecutor( XMessageBox xMessageBox )
132     {
133         m_xMessageBox = xMessageBox;
134     }
135 
GetResult()136     private boolean GetResult()
137     {
138         return m_bResult;
139     }
140 
notify( Object aData )141     public void notify( Object aData )
142     {
143         if ( m_aWikiDialog != null )
144             m_bResult = m_aWikiDialog.show();
145         else if ( m_xDialog != null )
146         {
147             if ( !m_bClose )
148                 m_bResult = ( m_xDialog.execute() == 1 );
149             else
150             {
151                 try
152                 {
153                     m_xDialog.endExecute();
154                 }
155                 catch( Exception e )
156                 {
157                     e.printStackTrace();
158                 }
159                 m_bResult = true;
160             }
161         }
162         else if ( m_xMessageBox != null )
163         {
164             int nRes = m_xMessageBox.execute();
165             m_bResult = ( nRes == com.sun.star.awt.MessageBoxResults.OK
166                           || nRes == com.sun.star.awt.MessageBoxResults.YES );
167         }
168 
169         m_bCalled = true;
170     }
171 }
172 
173