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