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 package com.sun.star.wizards.ui.event; 24 25 import com.sun.star.awt.XControl; 26 import com.sun.star.lang.EventObject; 27 import com.sun.star.uno.UnoRuntime; 28 import com.sun.star.wizards.common.Helper; 29 import com.sun.star.wizards.common.PropertyNames; 30 31 import java.lang.reflect.InvocationTargetException; 32 import java.util.Hashtable; 33 34 /** 35 * This class is a base class for listener classes. 36 * It uses a hashtable to map between a ComponentName, EventName and a MethodInvokation Object. 37 * To use this class do the following:<br/> 38 * <list> 39 * <li>Write a subclass which implements the needed Listener(s).</li> 40 * in the even methods, use invoke(...). 41 * <li>When instanciating the component, register the subclass as the event listener.</li> 42 * <li>Write the methods which should be performed when the event occures.</li> 43 * <li>call the "add" method, to define a component-event-action mapping.</li> 44 * </list> 45 * @author rpiterman 46 */ 47 public class AbstractListener 48 { 49 50 private Hashtable mHashtable = new Hashtable(); 51 52 /** Creates a new instance of AbstractListener */ AbstractListener()53 public AbstractListener() 54 { 55 } 56 add(String componentName, String eventName, String methodName, Object target)57 public void add(String componentName, String eventName, String methodName, Object target) 58 { 59 try 60 { 61 add(componentName, eventName, new MethodInvocation(methodName, target)); 62 } 63 catch (Exception ex) 64 { 65 ex.printStackTrace(); 66 } 67 } 68 add(String componentName, String eventName, MethodInvocation mi)69 public void add(String componentName, String eventName, MethodInvocation mi) 70 { 71 mHashtable.put(componentName + eventName, mi); 72 } 73 get(String componentName, String eventName)74 public MethodInvocation get(String componentName, String eventName) 75 { 76 return (MethodInvocation) mHashtable.get(componentName + eventName); 77 } 78 invoke(String componentName, String eventName, Object param)79 public Object invoke(String componentName, String eventName, Object param) 80 { 81 try 82 { 83 MethodInvocation mi = get(componentName, eventName); 84 if (mi != null) 85 { 86 return mi.invoke(param); 87 } 88 else 89 { 90 return null; 91 } 92 } 93 catch (InvocationTargetException ite) 94 { 95 96 System.out.println("======================================================="); 97 System.out.println("=== Note: An Exception was thrown which should have ==="); 98 System.out.println("=== caused a crash. I caught it. Please report this ==="); 99 System.out.println("=== to openoffice.org ==="); 100 System.out.println("======================================================="); 101 102 ite.printStackTrace(); 103 104 } 105 catch (IllegalAccessException iae) 106 { 107 iae.printStackTrace(); 108 } 109 catch (Exception ex) 110 { 111 System.out.println("======================================================="); 112 System.out.println("=== Note: An Exception was thrown which should have ==="); 113 System.out.println("=== caused a crash. I Catched it. Please report this =="); 114 System.out.println("=== to openoffice.org =="); 115 System.out.println("======================================================="); 116 ex.printStackTrace(); 117 } 118 119 return null; 120 } 121 122 /** 123 * Rerurns the property "name" of the Object which is the source of the event. 124 */ getEventSourceName(EventObject eventObject)125 public static String getEventSourceName(EventObject eventObject) 126 { 127 XControl xControl = UnoRuntime.queryInterface(XControl.class, eventObject.Source); 128 return (String) Helper.getUnoPropertyValue(xControl.getModel(), PropertyNames.PROPERTY_NAME, String.class); 129 } 130 } 131