cid#1607171 Data race condition
[LibreOffice.git] / swext / mediawiki / src / com / sun / star / wiki / WikiPropDialog.java
blob9ac41d4d316cf3fd01e129c4bb919d6a8984bc43
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 java.util.Map;
23 import com.sun.star.awt.XDialog;
24 import com.sun.star.beans.XPropertySet;
25 import com.sun.star.lang.EventObject;
26 import com.sun.star.uno.XComponentContext;
28 public class WikiPropDialog extends WikiDialog{
30 private WikiEditorImpl m_aWikiEditor;
32 private static final String sSendMethod = "Send";
33 private static final String sWikiListMethod = "WikiListChange";
34 private static final String sArticleTextMethod = "ArticleTextChange";
35 private static final String sAddWikiMethod = "AddWiki";
37 private final String[] m_pMethods = {sSendMethod, sWikiListMethod, sArticleTextMethod, sAddWikiMethod};
39 private String m_sWikiTitle = "";
40 private String m_sWikiEngineURL = "";
41 protected String m_sWikiComment = "";
42 protected boolean m_bWikiMinorEdit = false;
44 /** Creates a new instance of WikiPropDialog */
45 public WikiPropDialog(XComponentContext xContext, String DialogURL, WikiEditorImpl aWikiEditorForThrobber )
47 super(xContext, DialogURL);
48 super.setMethods(m_pMethods);
50 if ( aWikiEditorForThrobber != null )
52 InsertThrobber( 224, 122, 10, 10 );
53 m_aWikiEditor = aWikiEditorForThrobber;
56 InitStrings( xContext );
57 InitShowBrowser();
58 InitControls();
61 private void InitControls()
63 try
65 GetPropSet( "CommentText" ).setPropertyValue( "AutoVScroll", Boolean.TRUE );
67 catch( Exception e )
69 e.printStackTrace();
73 private void InitStrings( XComponentContext xContext )
75 try
77 SetTitle( Helper.GetLocalizedString( xContext, Helper.DLG_SENDTITLE ) );
78 GetPropSet( "Label1" ).setPropertyValue( "Label", Helper.GetLocalizedString( xContext, Helper.DLG_SENDTOMEDIAWIKI_LABEL1 ) );
79 GetPropSet( "FixedLine2" ).setPropertyValue( "Label", Helper.GetLocalizedString( xContext, Helper.DLG_WIKIARTICLE ) );
80 GetPropSet( "Label2" ).setPropertyValue( "Label", Helper.GetLocalizedString( xContext, Helper.DLG_SENDTOMEDIAWIKI_LABEL2 ) );
81 GetPropSet( "Label3" ).setPropertyValue( "Label", Helper.GetLocalizedString( xContext, Helper.DLG_SENDTOMEDIAWIKI_LABEL3 ) );
82 GetPropSet( "MinorCheck" ).setPropertyValue( "Label", Helper.GetLocalizedString( xContext, Helper.DLG_SENDTOMEDIAWIKI_MINORCHECK ) );
83 GetPropSet( "BrowserCheck" ).setPropertyValue( "Label", Helper.GetLocalizedString( xContext, Helper.DLG_SENDTOMEDIAWIKI_BROWSERCHECK ) );
84 GetPropSet( "AddButton" ).setPropertyValue( "Label", Helper.GetLocalizedString( xContext, Helper.DLG_ADDBUTTON ) );
85 GetPropSet( "SendButton" ).setPropertyValue( "Label", Helper.GetLocalizedString( xContext, Helper.DLG_SENDBUTTON ) );
87 catch( Exception e )
89 e.printStackTrace();
93 private void InitShowBrowser()
95 try
97 GetPropSet( "BrowserCheck" ).setPropertyValue( "State", Short.valueOf( Helper.GetShowInBrowserByDefault( m_xContext ) ? (short)1 : (short)0 ) );
99 catch( Exception e )
101 e.printStackTrace();
105 @Override
106 public boolean show()
108 EnableControls( true );
109 boolean bResult = super.show();
111 if ( bResult && Helper.GetShowInBrowserByDefault( m_xContext ) )
112 Helper.ShowURLInBrowser( m_xContext, m_sWikiEngineURL + "index.php?title=" + m_sWikiTitle );
114 return bResult;
117 @Override
118 public synchronized void ThreadStop( boolean bSelf )
120 boolean bShowError = ( !bSelf && m_aThread != null && !m_bThreadFinished );
122 super.ThreadStop( bSelf );
124 if ( bShowError )
125 Helper.ShowError( m_xContext,
126 m_xDialog,
127 Helper.DLG_SENDTITLE,
128 Helper.CANCELSENDING_ERROR,
129 null,
130 false );
133 public void fillWikiList()
135 String [] WikiList = m_aSettings.getWikiURLs();
139 XPropertySet xPS = GetPropSet("WikiList");
140 xPS.setPropertyValue("StringItemList", WikiList);
142 catch (Exception ex)
144 ex.printStackTrace();
148 private void fillDocList()
150 XPropertySet xPS = GetPropSet("ArticleText");
153 short [] sel = (short[]) GetPropSet("WikiList").getPropertyValue("SelectedItems");
154 xPS.setPropertyValue("StringItemList", m_aSettings.getWikiDocList(sel[0]));
156 catch (Exception ex)
158 ex.printStackTrace();
163 public String GetWikiTitle()
165 return m_sWikiTitle;
168 public void SetWikiTitle(String sArticle)
170 m_sWikiTitle = sArticle;
173 XPropertySet xPS = GetPropSet("ArticleText");
174 xPS.setPropertyValue("Text", sArticle);
176 catch (Exception ex)
178 ex.printStackTrace();
183 private void switchSendButtonIfNecessary()
185 XPropertySet xSendButton = GetPropSet( "SendButton" );
186 if ( xSendButton != null )
188 XPropertySet xWikiListProps = GetPropSet( "WikiList" );
189 XPropertySet xArticleProps = GetPropSet( "ArticleText" );
190 if ( xWikiListProps != null && xArticleProps != null )
194 short [] pSel = (short[]) GetPropSet("WikiList").getPropertyValue("SelectedItems");
195 String sArticle = (String)xArticleProps.getPropertyValue( "Text" );
196 if ( pSel != null && pSel.length > 0 && sArticle != null && sArticle.length() != 0 )
197 xSendButton.setPropertyValue( "Enabled", Boolean.TRUE );
198 else
199 xSendButton.setPropertyValue( "Enabled", Boolean.FALSE );
201 catch (Exception ex)
203 ex.printStackTrace();
209 private void EnableControls( boolean bEnable )
213 String[] pControls = { "WikiList",
214 "ArticleText",
215 "CommentText",
216 "MinorCheck",
217 "BrowserCheck",
218 "HelpButton",
219 "AddButton" };
221 for ( int nInd = 0; nInd < pControls.length; nInd++ )
222 GetPropSet( pControls[nInd] ).setPropertyValue( "Enabled", Boolean.valueOf( bEnable ) );
224 if ( bEnable )
226 SetFocusTo( "WikiList" );
227 switchSendButtonIfNecessary();
229 else
231 GetPropSet( "SendButton" ).setPropertyValue( "Enabled", Boolean.valueOf( bEnable ) );
232 SetFocusTo( "CancelButton" );
235 catch (Exception ex)
237 ex.printStackTrace();
241 @Override
242 public boolean callHandlerMethod( XDialog xDialog, Object EventObject, String MethodName )
244 if ( MethodName.equals( sSendMethod ) )
248 XPropertySet aWikiListProps = GetPropSet( "WikiList" );
249 XPropertySet aArticleTextProps = GetPropSet( "ArticleText" );
250 XPropertySet aCommentTextProps = GetPropSet( "CommentText" );
251 XPropertySet aMinorCheckProps = GetPropSet( "MinorCheck" );
252 XPropertySet aBrowserCheckProps = GetPropSet( "BrowserCheck" );
254 short [] sel = (short[]) aWikiListProps.getPropertyValue("SelectedItems");
255 String [] items = (String []) aWikiListProps.getPropertyValue("StringItemList");
256 m_sWikiEngineURL = items[sel[0]];
257 m_sWikiTitle = (String) aArticleTextProps.getPropertyValue("Text");
258 m_sWikiComment = (String) aCommentTextProps.getPropertyValue("Text");
260 short minorState = ((Short) aMinorCheckProps.getPropertyValue("State")).shortValue();
261 if (minorState != 0)
262 m_bWikiMinorEdit = true;
263 else
264 m_bWikiMinorEdit = false;
266 short nBrowserState = ((Short) aBrowserCheckProps.getPropertyValue("State")).shortValue();
267 Helper.SetShowInBrowserByDefault( m_xContext, nBrowserState != 0 );
269 // allow to disable other buttons
270 EnableControls( false );
272 catch (Exception ex)
274 ex.printStackTrace();
277 final XDialog xDialogToClose = xDialog;
278 final XComponentContext xContext = m_xContext;
280 // start spinning
281 SetThrobberVisible( true );
282 SetThrobberActive( true );
284 // the following method might show a dialog, should be used in main thread
285 final Map<String,String> aWikiSettings = m_aSettings.getSettingByUrl( m_sWikiEngineURL );
286 if ( Helper.AllowThreadUsage( m_xContext ) )
288 m_aThread = new Thread( "com.sun.star.thread.WikiEditorSendingThread" )
290 @Override
291 public void run()
295 if ( m_aWikiEditor != null )
297 Thread.yield();
298 m_bAction = m_aWikiEditor.SendArticleImpl(
299 WikiPropDialog.this, aWikiSettings);
302 finally
304 EnableControls( true );
305 SetThrobberActive( false );
306 SetThrobberVisible( false );
308 ThreadStop( true );
309 if ( m_bAction )
310 MainThreadDialogExecutor.Close( xContext, xDialogToClose );
315 m_aThread.start();
317 else
321 if (m_aWikiEditor != null && aWikiSettings != null)
323 m_bAction = m_aWikiEditor.SendArticleImpl(
324 WikiPropDialog.this, aWikiSettings);
326 } catch( java.lang.Exception e )
328 finally
330 EnableControls( true );
331 SetThrobberActive( false );
332 SetThrobberVisible( false );
334 if ( m_bAction )
335 xDialogToClose.endExecute();
339 return true;
341 else if ( MethodName.equals( sWikiListMethod ) )
343 fillDocList();
344 switchSendButtonIfNecessary();
345 return true;
347 else if ( MethodName.equals( sArticleTextMethod ) )
349 switchSendButtonIfNecessary();
350 return true;
352 else if ( MethodName.equals( sAddWikiMethod ) )
354 WikiEditSettingDialog xAddDialog = new WikiEditSettingDialog(m_xContext, "vnd.sun.star.script:WikiEditor.EditSetting?location=application");
355 if ( xAddDialog.show() )
356 fillWikiList();
358 xAddDialog.DisposeDialog();
360 return true;
363 return false;
366 @Override
367 public void windowClosed( EventObject e )
369 ThreadStop( false );