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.XContainerWindowEventHandler;
31 import com.sun.star.awt.XControl;
32 import com.sun.star.awt.XControlContainer;
33 import com.sun.star.awt.XDialog;
34 import com.sun.star.awt.XDialogEventHandler;
35 import com.sun.star.awt.XWindow;
36 import com.sun.star.beans.XPropertySet;
37 import com.sun.star.lang.WrappedTargetException;
38 import com.sun.star.lang.XServiceInfo;
39 import com.sun.star.lib.uno.helper.WeakBase;
40 import com.sun.star.uno.AnyConverter;
41 import com.sun.star.uno.UnoRuntime;
42 import com.sun.star.uno.XComponentContext;
43 import java.util.Hashtable;
44 
45 public final class WikiOptionsEventHandlerImpl extends WeakBase
46     implements XServiceInfo, XContainerWindowEventHandler, XDialogEventHandler
47 {
48     static final String[] m_pServiceNames = { "com.sun.star.wiki.WikiOptionsEventHandler" };
49     static final String m_sImplementationName = WikiOptionsEventHandlerImpl.class.getName();
50 
51     static final String sExternalEvent = "external_event";
52     static final String sAdd = "Add";
53     static final String sEdit = "Edit";
54     static final String sRemove = "Remove";
55     static final String sListStatus = "ListStatus";
56     static final String sListEdit = "ListEdit";
57     static final String sInitialize = "initialize";
58     static final String sOk = "ok";
59     static final String sBack = "back";
60 
61     private XComponentContext m_xContext;
62     private XDialog m_xDialog;
63     private XControlContainer m_xControlContainer;
64 
65     Settings m_aSettings;
66 
67     public WikiOptionsEventHandlerImpl( XComponentContext xContext )
68     {
69         m_xContext = xContext;
70     }
71 
72     protected XPropertySet GetPropSet( String sControl )
73     {
74         if ( m_xControlContainer != null )
75         {
76             XControl xControl = m_xControlContainer.getControl(sControl);
77             XPropertySet xListProps = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xControl.getModel() );
78             return xListProps;
79         }
80 
81         return null;
82     }
83 
84     private void RefreshView()
85     {
86         if ( m_aSettings != null )
87         {
88             String[] pWikiList = m_aSettings.getWikiURLs();
89             XPropertySet xListProps = GetPropSet( "WikiList" );
90             if ( xListProps != null )
91             {
92                 try
93                 {
94                     xListProps.setPropertyValue( "StringItemList", pWikiList );
95                 }
96                 catch ( Exception ex )
97                 {
98                     ex.printStackTrace();
99                 }
100             }
101         }
102     }
103 
104     private void CheckButtonState()
105     {
106         XPropertySet xListProps = GetPropSet( "WikiList" );
107         if ( xListProps != null )
108         {
109             try
110             {
111                 short [] pSel = (short []) xListProps.getPropertyValue( "SelectedItems" );
112                 XPropertySet xEditProps = GetPropSet( "EditButton" );
113                 XPropertySet xRemoveProps = GetPropSet( "RemoveButton" );
114                 Boolean bState = new Boolean( pSel.length != 0 );
115 
116                 xEditProps.setPropertyValue( "Enabled", bState );
117                 xRemoveProps.setPropertyValue( "Enabled", bState );
118             }
119             catch ( Exception ex )
120             {
121                 ex.printStackTrace();
122             }
123         }
124     }
125 
126     private void AddSetting()
127     {
128         WikiEditSettingDialog aSettingDialog = new WikiEditSettingDialog( m_xContext, "vnd.sun.star.script:WikiEditor.EditSetting?location=application" );
129         if ( aSettingDialog.show() )
130             RefreshView();
131 
132         aSettingDialog.DisposeDialog();
133     }
134 
135     private void EditSetting()
136     {
137         XPropertySet xListProps = GetPropSet( "WikiList" );
138         if ( xListProps != null )
139         {
140             Hashtable ht = null;
141             try
142             {
143                 short[] pSel = (short []) xListProps.getPropertyValue( "SelectedItems" );
144                 String[] pItems = (String []) xListProps.getPropertyValue("StringItemList");
145                 if ( pSel.length > 0 && pItems.length > pSel[0] )
146                 {
147                     String selName = pItems[pSel[0]];
148                     ht = m_aSettings.getSettingByUrl( pItems[pSel[0]] );
149                 }
150             }
151             catch ( Exception ex )
152             {
153                 ex.printStackTrace();
154             }
155 
156             WikiEditSettingDialog aSettingDialog = new WikiEditSettingDialog(m_xContext, "vnd.sun.star.script:WikiEditor.EditSetting?location=application", ht, true );
157             if ( aSettingDialog.show() )
158                 RefreshView();
159 
160             aSettingDialog.DisposeDialog();
161         }
162     }
163 
164     private void RemoveSetting()
165     {
166         XPropertySet xListProps = GetPropSet("WikiList");
167         if ( xListProps != null )
168         {
169             try
170             {
171                 short[] pSel = (short []) xListProps.getPropertyValue("SelectedItems");
172                 String[] pItems = (String []) GetPropSet("WikiList").getPropertyValue("StringItemList");
173                 if ( pSel.length > 0 && pItems.length > pSel[0] )
174                 {
175                     m_aSettings.removeSettingByUrl( pItems[pSel[0]] );
176                     RefreshView();
177                 }
178             }
179             catch (Exception ex)
180             {
181                 ex.printStackTrace();
182             }
183         }
184     }
185 
186     private void InitStrings()
187     {
188         try
189         {
190 
191             GetPropSet( "FixedLine1" ).setPropertyValue( "Label", Helper.GetLocalizedString( m_xContext, Helper.DLG_MEDIAWIKIEXTENSION_STRING ) );
192             GetPropSet( "AddButton" ).setPropertyValue( "Label", Helper.GetLocalizedString( m_xContext, Helper.DLG_ADDBUTTON ) );
193             GetPropSet( "EditButton" ).setPropertyValue( "Label", Helper.GetLocalizedString( m_xContext, Helper.DLG_EDITBUTTON ) );
194             GetPropSet( "RemoveButton" ).setPropertyValue( "Label", Helper.GetLocalizedString( m_xContext, Helper.DLG_REMOVEBUTTON ) );
195         }
196         catch( Exception e )
197         {
198             e.printStackTrace();
199         }
200     }
201 
202     // com.sun.star.lang.XServiceInfo:
203     public String getImplementationName()
204     {
205          return m_sImplementationName;
206     }
207 
208     public boolean supportsService( String sService )
209     {
210         int len = m_pServiceNames.length;
211 
212         for( int i=0; i < len; i++ )
213         {
214             if ( sService.equals( m_pServiceNames[i] ))
215                 return true;
216         }
217         return false;
218     }
219 
220     public String[] getSupportedServiceNames()
221     {
222         return m_pServiceNames;
223     }
224 
225     // XContainerWindowEventHandler
226     public boolean callHandlerMethod( XWindow xWindow, Object aEventObject, String sMethod )
227         throws WrappedTargetException, com.sun.star.uno.RuntimeException
228     {
229         if ( sMethod.equals( sExternalEvent ) )
230         {
231             try
232             {
233                 String sEvent = (String)AnyConverter.toString( aEventObject );
234                 if ( sEvent != null )
235                 {
236                     if ( sEvent.equals( sOk ) )
237                     {
238                         if ( m_aSettings != null )
239                             m_aSettings.storeConfiguration();
240                     }
241                     else if ( sEvent.equals( sInitialize ) || sEvent.equals( sBack ) )
242                     {
243                         if ( sEvent.equals( sInitialize ) )
244                         {
245                             m_xDialog = (XDialog)UnoRuntime.queryInterface( XDialog.class, xWindow );
246                             m_xControlContainer = (XControlContainer)UnoRuntime.queryInterface(
247                                                             XControlContainer.class, m_xDialog );
248                             m_aSettings = Settings.getSettings( m_xContext );
249                             m_aSettings.loadConfiguration(); // throw away all the noncommited changes
250                             InitStrings();
251                         }
252                         else if ( m_aSettings != null )
253                             m_aSettings.loadConfiguration(); // throw away all the noncommited changes
254 
255                         RefreshView();
256                         CheckButtonState();
257                     }
258                 }
259             }
260             catch ( com.sun.star.uno.RuntimeException r )
261             {
262                 throw r;
263             }
264             catch ( com.sun.star.uno.Exception e )
265             {
266                 throw new WrappedTargetException( sMethod, this, e );
267             }
268         }
269         else if ( sMethod.equals( sAdd ) )
270         {
271             AddSetting();
272         }
273         else if ( sMethod.equals( sEdit ) || sMethod.equals( sListEdit ) )
274         {
275             EditSetting();
276         }
277         else if ( sMethod.equals( sRemove ) )
278         {
279             RemoveSetting();
280             CheckButtonState();
281         }
282         else if ( sMethod.equals( sListStatus ) )
283         {
284             CheckButtonState();
285         }
286 
287         return true;
288     }
289 
290     public boolean callHandlerMethod( XDialog xDialog, Object aEventObject, String sMethod )
291         throws WrappedTargetException, com.sun.star.uno.RuntimeException
292     {
293 
294 
295         return true;
296     }
297 
298     public String[] getSupportedMethodNames()
299     {
300         return new String[] { sExternalEvent, sAdd, sEdit, sRemove };
301     }
302 };
303 
304