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