| 1 |
/* |
| 2 |
* This file is part of the LibreOffice project. |
| 3 |
* |
| 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/. |
| 7 |
* |
| 8 |
* This file incorporates work covered by the following license notice: |
| 9 |
* |
| 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 . |
| 17 |
*/ |
| 18 |
|
| 19 |
package com.sun.star.wiki; |
| 20 |
|
| 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; |
| 29 |
|
| 30 |
public class MainThreadDialogExecutor implements XCallback |
| 31 |
{ |
| 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; |
| 38 |
|
| 39 |
public static boolean Show( XComponentContext xContext, WikiDialog aWikiDialog ) |
| 40 |
{ |
| 41 |
MainThreadDialogExecutor aExecutor = new MainThreadDialogExecutor( aWikiDialog ); |
| 42 |
return GetCallback( xContext, aExecutor ); |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public static boolean Execute( XComponentContext xContext, XMessageBox xMessageBox ) |
| 48 |
{ |
| 49 |
MainThreadDialogExecutor aExecutor = new MainThreadDialogExecutor( xMessageBox ); |
| 50 |
return GetCallback( xContext, aExecutor ); |
| 51 |
} |
| 52 |
|
| 53 |
public static boolean Close( XComponentContext xContext, XDialog xDialog ) |
| 54 |
{ |
| 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 ); |
| 59 |
} |
| 60 |
|
| 61 |
private static boolean GetCallback( XComponentContext xContext, MainThreadDialogExecutor aExecutor ) |
| 62 |
{ |
| 63 |
if (aExecutor == null) |
| 64 |
return false; |
| 65 |
|
| 66 |
try |
| 67 |
{ |
| 68 |
String aThreadName = null; |
| 69 |
Thread aCurThread = Thread.currentThread(); |
| 70 |
if ( aCurThread != null ) |
| 71 |
aThreadName = aCurThread.getName(); |
| 72 |
|
| 73 |
if ( aThreadName != null && aThreadName.equals( "com.sun.star.thread.WikiEditorSendingThread" ) ) |
| 74 |
{ |
| 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(); |
| 79 |
|
| 80 |
XRequestCallback xRequest = UnoRuntime.queryInterface( |
| 81 |
XRequestCallback.class, |
| 82 |
xFactory.createInstanceWithContext( "com.sun.star.awt.AsyncCallback", xContext ) ); |
| 83 |
if ( xRequest != null ) |
| 84 |
{ |
| 85 |
xRequest.addCallback( aExecutor, Any.VOID ); |
| 86 |
do |
| 87 |
{ |
| 88 |
Thread.yield(); |
| 89 |
} |
| 90 |
while( !aExecutor.m_bCalled ); |
| 91 |
} |
| 92 |
} |
| 93 |
else |
| 94 |
{ |
| 95 |
// handle it as a main thread |
| 96 |
aExecutor.notify( Any.VOID ); |
| 97 |
} |
| 98 |
} |
| 99 |
catch( Exception e ) |
| 100 |
{ |
| 101 |
e.printStackTrace(); |
| 102 |
} |
| 103 |
|
| 104 |
return aExecutor.GetResult(); |
| 105 |
} |
| 106 |
|
| 107 |
private MainThreadDialogExecutor( WikiDialog aWikiDialog ) |
| 108 |
{ |
| 109 |
m_aWikiDialog = aWikiDialog; |
| 110 |
} |
| 111 |
|
| 112 |
private MainThreadDialogExecutor( XDialog xDialog ) |
| 113 |
{ |
| 114 |
m_xDialog = xDialog; |
| 115 |
} |
| 116 |
|
| 117 |
private MainThreadDialogExecutor( XMessageBox xMessageBox ) |
| 118 |
{ |
| 119 |
m_xMessageBox = xMessageBox; |
| 120 |
} |
| 121 |
|
| 122 |
private boolean GetResult() |
| 123 |
{ |
| 124 |
return m_bResult; |
| 125 |
} |
| 126 |
|
| 127 |
public void notify( Object aData ) |
| 128 |
{ |
| 129 |
if ( m_aWikiDialog != null ) |
| 130 |
m_bResult = m_aWikiDialog.show(); |
| 131 |
else if ( m_xDialog != null ) |
| 132 |
{ |
| 133 |
if ( !m_bClose ) |
| 134 |
m_bResult = ( m_xDialog.execute() == 1 ); |
| 135 |
else |
| 136 |
{ |
| 137 |
try |
| 138 |
{ |
| 139 |
m_xDialog.endExecute(); |
| 140 |
} |
| 141 |
catch( Exception e ) |
| 142 |
{ |
| 143 |
e.printStackTrace(); |
| 144 |
} |
| 145 |
m_bResult = true; |
| 146 |
} |
| 147 |
} |
| 148 |
else if ( m_xMessageBox != null ) |
| 149 |
{ |
| 150 |
int nRes = m_xMessageBox.execute(); |
| 151 |
m_bResult = ( nRes == com.sun.star.awt.MessageBoxResults.OK |
| 152 |
|| nRes == com.sun.star.awt.MessageBoxResults.YES ); |
| 153 |
} |
| 154 |
|
| 155 |
m_bCalled = true; |
| 156 |
} |
| 157 |
} |
| 158 |
|