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.*; 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 /** 32 * @author rpiterman 33 * 34 * This class suppoprts imple cases where a UI control can 35 * be directly synchronized with a data property. 36 * Such controls are: the different text controls 37 * (synchronizing the "Text" , "Value", "Date", "Time" property), 38 * Checkbox controls, Dropdown listbox controls (synchronizing the 39 * SelectedItems[] property. 40 * For those controls, static convenience methods are offered, to simplify use. 41 */ 42 public class UnoDataAware extends DataAware 43 { 44 45 protected Object unoControl; 46 protected Object unoModel; 47 protected String unoPropName; 48 protected Object[] disableObjects = new Object[0]; 49 protected boolean inverse = false; 50 UnoDataAware(Object dataObject, Value value, Object unoObject_, String unoPropName_)51 protected UnoDataAware(Object dataObject, Value value, Object unoObject_, String unoPropName_) 52 { 53 super(dataObject, value); 54 unoControl = unoObject_; 55 unoModel = getModel(unoControl); 56 unoPropName = unoPropName_; 57 } 58 setInverse(boolean i)59 public void setInverse(boolean i) 60 { 61 inverse = i; 62 } 63 enableControls(Object value)64 protected void enableControls(Object value) 65 { 66 Boolean b = getBoolean(value); 67 if (inverse) 68 { 69 b = b.booleanValue() ? Boolean.FALSE : Boolean.TRUE; 70 } 71 for (int i = 0; i < disableObjects.length; i++) 72 { 73 setEnabled(disableObjects[i], b); 74 } 75 } 76 setToUI(Object value)77 protected void setToUI(Object value) 78 { 79 //System.out.println("Settings uno property : "+ Helper.getUnoPropertyValue(this.unoModel,PropertyNames.PROPERTY_NAME) + "<-" +stringof(value)); 80 Helper.setUnoPropertyValue(unoModel, unoPropName, value); 81 } 82 stringof(Object value)83 private String stringof(Object value) 84 { 85 if (value.getClass().isArray()) 86 { 87 StringBuffer sb = new StringBuffer("["); 88 for (int i = 0; i < ((short[]) value).length; i++) 89 { 90 sb.append(((short[]) value)[i]).append(" , "); 91 } 92 sb.append("]"); 93 return sb.toString(); 94 } 95 else 96 { 97 return value.toString(); 98 } 99 } 100 101 /** 102 * Try to get from an arbitrary object a boolean value. 103 * Null returns Boolean.FALSE; 104 * A Boolean object returns itself. 105 * An Array returns true if it not empty. 106 * An Empty String returns Boolean.FALSE. 107 * everything else returns a Boolean.TRUE. 108 * @param value 109 * @return 110 */ getBoolean(Object value)111 protected Boolean getBoolean(Object value) 112 { 113 if (value == null) 114 { 115 return Boolean.FALSE; 116 } 117 if (value instanceof Boolean) 118 { 119 return (Boolean) value; 120 } 121 else if (value.getClass().isArray()) 122 { 123 return ((short[]) value).length != 0 ? Boolean.TRUE : Boolean.FALSE; 124 } 125 else if (value.equals(PropertyNames.EMPTY_STRING)) 126 { 127 return Boolean.FALSE; 128 } 129 else if (value instanceof Number) 130 { 131 return ((Number) value).intValue() == 0 ? Boolean.TRUE : Boolean.FALSE; 132 } 133 else 134 { 135 return Boolean.TRUE; 136 } 137 } 138 disableControls(Object[] controls)139 public void disableControls(Object[] controls) 140 { 141 disableObjects = controls; 142 } 143 getFromUI()144 protected Object getFromUI() 145 { 146 return Helper.getUnoPropertyValue(unoModel, unoPropName); 147 } 148 attachTextControl(Object data, String prop, Object unoText, final Listener listener, String unoProperty, boolean field, Object value)149 private static UnoDataAware attachTextControl(Object data, String prop, Object unoText, final Listener listener, String unoProperty, boolean field, Object value) 150 { 151 XTextComponent text = UnoRuntime.queryInterface(XTextComponent.class, unoText); 152 final UnoDataAware uda = new UnoDataAware(data, 153 field 154 ? DataAwareFields.getFieldValueFor(data, prop, value) 155 : new DataAware.PropertyValue(prop, data), 156 text, unoProperty); 157 text.addTextListener(new XTextListener() 158 { 159 160 public void textChanged(TextEvent te) 161 { 162 uda.updateData(); 163 if (listener != null) 164 { 165 listener.eventPerformed(te); 166 } 167 } 168 169 public void disposing(EventObject eo) 170 { 171 } 172 }); 173 return uda; 174 } 175 attachEditControl(Object data, String prop, Object unoControl, Listener listener, boolean field)176 public static UnoDataAware attachEditControl(Object data, String prop, Object unoControl, Listener listener, boolean field) 177 { 178 return attachTextControl(data, prop, unoControl, listener, "Text", field, PropertyNames.EMPTY_STRING); 179 } 180 attachDateControl(Object data, String prop, Object unoControl, Listener listener, boolean field)181 public static UnoDataAware attachDateControl(Object data, String prop, Object unoControl, Listener listener, boolean field) 182 { 183 return attachTextControl(data, prop, unoControl, listener, "Date", field, 0); 184 } 185 attachTimeControl(Object data, String prop, Object unoControl, Listener listener, boolean field)186 public static UnoDataAware attachTimeControl(Object data, String prop, Object unoControl, Listener listener, boolean field) 187 { 188 return attachTextControl(data, prop, unoControl, listener, "Time", field, 0); 189 } 190 attachNumericControl(Object data, String prop, Object unoControl, Listener listener, boolean field)191 public static UnoDataAware attachNumericControl(Object data, String prop, Object unoControl, Listener listener, boolean field) 192 { 193 return attachTextControl(data, prop, unoControl, listener, "Value", field, new Double(0)); 194 } 195 attachCheckBox(Object data, String prop, Object checkBox, final Listener listener, boolean field)196 public static UnoDataAware attachCheckBox(Object data, String prop, Object checkBox, final Listener listener, boolean field) 197 { 198 XCheckBox xcheckBox = UnoRuntime.queryInterface(XCheckBox.class, checkBox); 199 final UnoDataAware uda = new UnoDataAware(data, 200 field 201 ? DataAwareFields.getFieldValueFor(data, prop, new Short((short) 0)) 202 : new DataAware.PropertyValue(prop, data), 203 checkBox, PropertyNames.PROPERTY_STATE); 204 xcheckBox.addItemListener(itemListener(uda, listener)); 205 return uda; 206 } 207 itemListener(final DataAware da, final Listener listener)208 static XItemListener itemListener(final DataAware da, final Listener listener) 209 { 210 return new XItemListener() 211 { 212 213 public void itemStateChanged(ItemEvent ie) 214 { 215 da.updateData(); 216 if (listener != null) 217 { 218 listener.eventPerformed(ie); 219 } 220 } 221 222 public void disposing(EventObject eo) 223 { 224 } 225 }; 226 } 227 228 public static UnoDataAware attachLabel(Object data, String prop, Object label, final Listener listener, boolean field) 229 { 230 return new UnoDataAware(data, 231 field ? DataAwareFields.getFieldValueFor(data, prop, PropertyNames.EMPTY_STRING) 232 : new DataAware.PropertyValue(prop, data), 233 label, PropertyNames.PROPERTY_LABEL); 234 } 235 236 public static UnoDataAware attachListBox(Object data, String prop, Object listBox, final Listener listener, boolean field) 237 { 238 XListBox xListBox = UnoRuntime.queryInterface(XListBox.class, listBox); 239 final UnoDataAware uda = new UnoDataAware(data, 240 field 241 ? DataAwareFields.getFieldValueFor(data, prop, new short[0]) 242 : new DataAware.PropertyValue(prop, data), 243 listBox, PropertyNames.SELECTED_ITEMS); 244 xListBox.addItemListener(itemListener(uda, listener)); 245 return uda; 246 } 247 248 public static Object getModel(Object control) 249 { 250 return UnoRuntime.queryInterface(XControl.class, control).getModel(); 251 } 252 253 public static void setEnabled(Object control, boolean enabled) 254 { 255 setEnabled(control, enabled ? Boolean.TRUE : Boolean.FALSE); 256 } 257 258 public static void setEnabled(Object control, Boolean enabled) 259 { 260 Helper.setUnoPropertyValue(getModel(control), PropertyNames.PROPERTY_ENABLED, enabled); 261 } 262 } 263