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