cid#1607171 Data race condition
[LibreOffice.git] / swext / mediawiki / src / com / sun / star / wiki / MainThreadDialogExecutor.java
bloba6ee07fc85043b49d325df615c5215131a14113e
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package com.sun.star.wiki;
21 import com.sun.star.uno.Any;
22 import com.sun.star.awt.XDialog;
23 import com.sun.star.awt.XCallback;
24 import com.sun.star.awt.XMessageBox;
25 import com.sun.star.awt.XRequestCallback;
26 import com.sun.star.lang.XMultiComponentFactory;
27 import com.sun.star.uno.UnoRuntime;
28 import com.sun.star.uno.XComponentContext;
30 public class MainThreadDialogExecutor implements XCallback
32 private WikiDialog m_aWikiDialog;
33 private XDialog m_xDialog;
34 private XMessageBox m_xMessageBox;
35 private boolean m_bResult = false;
36 private boolean m_bCalled = false;
37 private boolean m_bClose = false;
39 public static boolean Show( XComponentContext xContext, WikiDialog aWikiDialog )
41 MainThreadDialogExecutor aExecutor = new MainThreadDialogExecutor( aWikiDialog );
42 return GetCallback( xContext, aExecutor );
47 public static boolean Execute( XComponentContext xContext, XMessageBox xMessageBox )
49 MainThreadDialogExecutor aExecutor = new MainThreadDialogExecutor( xMessageBox );
50 return GetCallback( xContext, aExecutor );
53 public static boolean Close( XComponentContext xContext, XDialog xDialog )
55 MainThreadDialogExecutor aExecutor = new MainThreadDialogExecutor( xDialog );
56 aExecutor.m_bClose = true;
57 aExecutor.m_bCalled = true; // no yielding, asynchronous closing
58 return GetCallback( xContext, aExecutor );
61 private static boolean GetCallback( XComponentContext xContext, MainThreadDialogExecutor aExecutor )
63 if (aExecutor == null)
64 return false;
66 try
68 String aThreadName = null;
69 Thread aCurThread = Thread.currentThread();
70 if ( aCurThread != null )
71 aThreadName = aCurThread.getName();
73 if ( aThreadName != null && aThreadName.equals( "com.sun.star.thread.WikiEditorSendingThread" ) )
75 // the main thread should be accessed asynchronously
76 XMultiComponentFactory xFactory = xContext.getServiceManager();
77 if ( xFactory == null )
78 throw new com.sun.star.uno.RuntimeException();
80 XRequestCallback xRequest = UnoRuntime.queryInterface(
81 XRequestCallback.class,
82 xFactory.createInstanceWithContext( "com.sun.star.awt.AsyncCallback", xContext ) );
83 if ( xRequest != null )
85 xRequest.addCallback( aExecutor, Any.VOID );
88 Thread.yield();
90 while( !aExecutor.m_bCalled );
93 else
95 // handle it as a main thread
96 aExecutor.notify( Any.VOID );
99 catch( Exception e )
101 e.printStackTrace();
104 return aExecutor.GetResult();
107 private MainThreadDialogExecutor( WikiDialog aWikiDialog )
109 m_aWikiDialog = aWikiDialog;
112 private MainThreadDialogExecutor( XDialog xDialog )
114 m_xDialog = xDialog;
117 private MainThreadDialogExecutor( XMessageBox xMessageBox )
119 m_xMessageBox = xMessageBox;
122 private boolean GetResult()
124 return m_bResult;
127 public void notify( Object aData )
129 if ( m_aWikiDialog != null )
130 m_bResult = m_aWikiDialog.show();
131 else if ( m_xDialog != null )
133 if ( !m_bClose )
134 m_bResult = ( m_xDialog.execute() == 1 );
135 else
139 m_xDialog.endExecute();
141 catch( Exception e )
143 e.printStackTrace();
145 m_bResult = true;
148 else if ( m_xMessageBox != null )
150 int nRes = m_xMessageBox.execute();
151 m_bResult = ( nRes == com.sun.star.awt.MessageBoxResults.OK
152 || nRes == com.sun.star.awt.MessageBoxResults.YES );
155 m_bCalled = true;