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