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