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