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.awt.XControl;
31 import com.sun.star.awt.XControlModel;
32 import com.sun.star.awt.XDialog;
33 import com.sun.star.awt.XThrobber;
34 import com.sun.star.awt.XWindowPeer;
35 import com.sun.star.beans.UnknownPropertyException;
36 import com.sun.star.beans.XPropertySet;
37 import com.sun.star.container.XNameContainer;
38 import com.sun.star.lang.WrappedTargetException;
39 import com.sun.star.lang.XMultiComponentFactory;
40 import com.sun.star.lang.XMultiServiceFactory;
41 import com.sun.star.lang.EventObject;
42 import com.sun.star.uno.UnoRuntime;
43 import com.sun.star.uno.XComponentContext;
44 import java.util.Hashtable;
45 
46 public class WikiPropDialog extends WikiDialog{
47 
48     WikiEditorImpl m_aWikiEditor;
49 
50     private final String sSendMethod = "Send";
51     private final String sWikiListMethod = "WikiListChange";
52     private final String sArticleTextMethod = "ArticleTextChange";
53     private final String sAddWikiMethod = "AddWiki";
54 
55     String[] m_pMethods = {sSendMethod, sWikiListMethod, sArticleTextMethod, sAddWikiMethod};
56 
57     private String m_sWikiTitle = "";
58     protected String m_sWikiEngineURL = "";
59     protected String m_sWikiComment = "";
60     protected boolean m_bWikiMinorEdit = false;
61 
62     /** Creates a new instance of WikiPropDialog */
63     public WikiPropDialog(XComponentContext xContext, String DialogURL, WikiEditorImpl aWikiEditorForThrobber )
64     {
65         super(xContext, DialogURL);
66         super.setMethods(m_pMethods);
67 
68         if ( aWikiEditorForThrobber != null )
69         {
70             InsertThrobber( 224, 122, 10, 10 );
71             m_aWikiEditor = aWikiEditorForThrobber;
72         }
73 
74         InitStrings( xContext );
75         InitShowBrowser( xContext );
76         InitControls( xContext );
77     }
78 
79     private void InitControls( XComponentContext xContext )
80     {
81         try
82         {
83             GetPropSet( "CommentText" ).setPropertyValue( "AutoVScroll", Boolean.TRUE );
84         }
85         catch( Exception e )
86         {
87             e.printStackTrace();
88         }
89     }
90 
91     private void InitStrings( XComponentContext xContext )
92     {
93         try
94         {
95             SetTitle( Helper.GetLocalizedString( xContext, Helper.DLG_SENDTITLE ) );
96             GetPropSet( "Label1" ).setPropertyValue( "Label", Helper.GetLocalizedString( xContext, Helper.DLG_SENDTOMEDIAWIKI_LABEL1 ) );
97             GetPropSet( "FixedLine2" ).setPropertyValue( "Label", Helper.GetLocalizedString( xContext, Helper.DLG_WIKIARTICLE ) );
98             GetPropSet( "Label2" ).setPropertyValue( "Label", Helper.GetLocalizedString( xContext, Helper.DLG_SENDTOMEDIAWIKI_LABEL2 ) );
99             GetPropSet( "Label3" ).setPropertyValue( "Label", Helper.GetLocalizedString( xContext, Helper.DLG_SENDTOMEDIAWIKI_LABEL3 ) );
100             GetPropSet( "MinorCheck" ).setPropertyValue( "Label", Helper.GetLocalizedString( xContext, Helper.DLG_SENDTOMEDIAWIKI_MINORCHECK ) );
101             GetPropSet( "BrowserCheck" ).setPropertyValue( "Label", Helper.GetLocalizedString( xContext, Helper.DLG_SENDTOMEDIAWIKI_BROWSERCHECK ) );
102             GetPropSet( "AddButton" ).setPropertyValue( "Label", Helper.GetLocalizedString( xContext, Helper.DLG_ADDBUTTON ) );
103             GetPropSet( "SendButton" ).setPropertyValue( "Label", Helper.GetLocalizedString( xContext, Helper.DLG_SENDBUTTON ) );
104         }
105         catch( Exception e )
106         {
107             e.printStackTrace();
108         }
109     }
110 
111     private void InitShowBrowser( XComponentContext xContext )
112     {
113         try
114         {
115             GetPropSet( "BrowserCheck" ).setPropertyValue( "State", new Short( Helper.GetShowInBrowserByDefault( m_xContext ) ? (short)1 : (short)0 ) );
116         }
117         catch( Exception e )
118         {
119             e.printStackTrace();
120         }
121     }
122 
123     public boolean show()
124     {
125         EnableControls( true );
126         boolean bResult = super.show();
127 
128         if ( bResult && Helper.GetShowInBrowserByDefault( m_xContext ) )
129            Helper.ShowURLInBrowser( m_xContext, m_sWikiEngineURL + "index.php?title=" + m_sWikiTitle );
130 
131         return bResult;
132     }
133 
134     public synchronized void ThreadStop( boolean bSelf )
135     {
136         boolean bShowError = ( !bSelf && m_aThread != null && !m_bThreadFinished );
137 
138         super.ThreadStop( bSelf );
139 
140         if ( bShowError )
141             Helper.ShowError( m_xContext,
142                               m_xDialog,
143                               Helper.DLG_SENDTITLE,
144                               Helper.CANCELSENDING_ERROR,
145                               null,
146                               false );
147     }
148 
149     public void fillWikiList()
150     {
151         String [] WikiList = m_aSettings.getWikiURLs();
152 
153         try
154         {
155             XPropertySet xPS = GetPropSet("WikiList");
156             xPS.setPropertyValue("StringItemList", WikiList);
157             // short [] nSel = new short[1];
158             // nSel[0] = (short) m_aSettings.getLastUsedWikiServer();
159             // xPS.setPropertyValue("SelectedItems", sel);
160         }
161         catch (Exception ex)
162         {
163             ex.printStackTrace();
164         }
165     }
166 
167     public void fillDocList()
168     {
169         XPropertySet xPS = GetPropSet("ArticleText");
170         try
171         {
172             short [] sel = (short[]) GetPropSet("WikiList").getPropertyValue("SelectedItems");
173             xPS.setPropertyValue("StringItemList", m_aSettings.getWikiDocList(sel[0], 5));
174         }
175         catch (Exception ex)
176         {
177             ex.printStackTrace();
178         }
179     }
180 
181 
182     public String GetWikiTitle()
183     {
184         return m_sWikiTitle;
185     }
186 
187     public void SetWikiTitle(String sArticle)
188     {
189         m_sWikiTitle = sArticle;
190         try
191         {
192             XPropertySet xPS = GetPropSet("ArticleText");
193             xPS.setPropertyValue("Text", sArticle);
194         }
195         catch (Exception ex)
196         {
197             ex.printStackTrace();
198         }
199     }
200 
201 
202     public void switchSendButtonIfNecessary()
203     {
204         XPropertySet xSendButton = GetPropSet( "SendButton" );
205         if ( xSendButton != null )
206         {
207             XPropertySet xWikiListProps = GetPropSet( "WikiList" );
208             XPropertySet xArticleProps = GetPropSet( "ArticleText" );
209             if ( xWikiListProps != null && xArticleProps != null )
210             {
211                 try
212                 {
213                     short [] pSel = (short[]) GetPropSet("WikiList").getPropertyValue("SelectedItems");
214                     String sArticle = (String)xArticleProps.getPropertyValue( "Text" );
215                     if ( pSel != null && pSel.length > 0 && sArticle != null && sArticle.length() != 0 )
216                         xSendButton.setPropertyValue( "Enabled", Boolean.TRUE );
217                     else
218                         xSendButton.setPropertyValue( "Enabled", Boolean.FALSE );
219                 }
220                 catch (Exception ex)
221                 {
222                     ex.printStackTrace();
223                 }
224             }
225         }
226     }
227 
228     public void EnableControls( boolean bEnable )
229     {
230         try
231         {
232             String[] pControls = { "WikiList",
233                                    "ArticleText",
234                                    "CommentText",
235                                    "MinorCheck",
236                                    "BrowserCheck",
237                                    "HelpButton",
238                                    "AddButton" };
239 
240             for ( int nInd = 0; nInd < pControls.length; nInd++ )
241                 GetPropSet( pControls[nInd] ).setPropertyValue( "Enabled", new Boolean( bEnable ) );
242 
243             if ( bEnable )
244             {
245                 SetFocusTo( "WikiList" );
246                 switchSendButtonIfNecessary();
247             }
248             else
249             {
250                 GetPropSet( "SendButton" ).setPropertyValue( "Enabled", new Boolean( bEnable ) );
251                 SetFocusTo( "CancelButton" );
252             }
253         }
254         catch (Exception ex)
255         {
256             ex.printStackTrace();
257         }
258     }
259 
260     public boolean callHandlerMethod( XDialog xDialog, Object EventObject, String MethodName )
261     {
262         if ( MethodName.equals( sSendMethod ) )
263         {
264             try
265             {
266                 XPropertySet aWikiListProps = GetPropSet( "WikiList" );
267                 XPropertySet aArticleTextProps = GetPropSet( "ArticleText" );
268                 XPropertySet aCommentTextProps = GetPropSet( "CommentText" );
269                 XPropertySet aMinorCheckProps = GetPropSet( "MinorCheck" );
270                 XPropertySet aBrowserCheckProps = GetPropSet( "BrowserCheck" );
271 
272                 short [] sel = (short[]) aWikiListProps.getPropertyValue("SelectedItems");
273                 String [] items = (String []) aWikiListProps.getPropertyValue("StringItemList");
274                 m_sWikiEngineURL = items[sel[0]];
275                 m_aSettings.setLastUsedWikiServer(sel[0]);
276                 m_sWikiTitle = (String) aArticleTextProps.getPropertyValue("Text");
277                 m_sWikiComment = (String) aCommentTextProps.getPropertyValue("Text");
278 
279                 short minorState = ((Short) aMinorCheckProps.getPropertyValue("State")).shortValue();
280                 if (minorState != 0)
281                     m_bWikiMinorEdit = true;
282                 else
283                     m_bWikiMinorEdit = false;
284 
285                 short nBrowserState = ((Short) aBrowserCheckProps.getPropertyValue("State")).shortValue();
286                 Helper.SetShowInBrowserByDefault( m_xContext, nBrowserState != 0 );
287 
288                 // allow to disable other buttons
289                 EnableControls( false );
290             }
291             catch (Exception ex)
292             {
293                 ex.printStackTrace();
294             }
295 
296             final WikiPropDialog aThisDialog = this;
297             final XDialog xDialogToClose = xDialog;
298             final XComponentContext xContext = m_xContext;
299 
300             // start spinning
301             SetThrobberVisible( true );
302             SetThrobberActive( true );
303 
304             // the following method might show a dialog, should be used in main thread
305             final Hashtable aWikiSettings = m_aSettings.getSettingByUrl( m_sWikiEngineURL );
306             if ( Helper.AllowThreadUsage( m_xContext ) )
307             {
308                 m_aThread = new Thread( "com.sun.star.thread.WikiEditorSendingThread" )
309                 {
310                     public void run()
311                     {
312                         try
313                         {
314                             if ( m_aWikiEditor != null )
315                             {
316                                 Thread.yield();
317                                 m_bAction = m_aWikiEditor.SendArticleImpl( aThisDialog, aWikiSettings );
318                             }
319                         }
320                         finally
321                         {
322                             EnableControls( true );
323                             SetThrobberActive( false );
324                             SetThrobberVisible( false );
325 
326                             ThreadStop( true );
327                             if ( m_bAction )
328                                 MainThreadDialogExecutor.Close( xContext, xDialogToClose );
329                         }
330                     }
331                 };
332 
333                 m_aThread.start();
334             }
335             else
336             {
337                 try
338                 {
339                     if ( m_aWikiEditor != null )
340                     {
341                         m_bAction = m_aWikiEditor.SendArticleImpl( aThisDialog, aWikiSettings );
342                     }
343                 } catch( java.lang.Exception e )
344                 {}
345                 finally
346                 {
347                     EnableControls( true );
348                     SetThrobberActive( false );
349                     SetThrobberVisible( false );
350 
351                     if ( m_bAction )
352                         xDialogToClose.endExecute();
353                 }
354             }
355 
356             return true;
357         }
358         else if ( MethodName.equals( sWikiListMethod ) )
359         {
360             fillDocList();
361             switchSendButtonIfNecessary();
362             return true;
363         }
364         else if ( MethodName.equals( sArticleTextMethod ) )
365         {
366             switchSendButtonIfNecessary();
367             return true;
368         }
369         else if ( MethodName.equals( sAddWikiMethod ) )
370         {
371             WikiEditSettingDialog xAddDialog = new WikiEditSettingDialog(m_xContext, "vnd.sun.star.script:WikiEditor.EditSetting?location=application");
372             if ( xAddDialog.show() )
373                 fillWikiList();
374 
375             xAddDialog.DisposeDialog();
376 
377             return true;
378         }
379 
380         return false;
381     }
382 
383     public void windowClosed( EventObject e )
384     {
385         ThreadStop( false );
386     }
387 }
388 
389