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.document; 24 25 import com.sun.star.awt.Point; 26 import com.sun.star.awt.Size; 27 import com.sun.star.awt.XControl; 28 import com.sun.star.awt.XControlModel; 29 import com.sun.star.awt.XLayoutConstrains; 30 import com.sun.star.awt.XWindowPeer; 31 import com.sun.star.beans.XPropertySet; 32 import com.sun.star.beans.XPropertySetInfo; 33 import com.sun.star.container.XNameAccess; 34 import com.sun.star.container.XNameContainer; 35 import com.sun.star.container.XNamed; 36 import com.sun.star.wizards.common.*; 37 import com.sun.star.uno.Exception; 38 import com.sun.star.uno.UnoRuntime; 39 import com.sun.star.uno.AnyConverter; 40 import com.sun.star.drawing.XShape; 41 import com.sun.star.drawing.XShapes; 42 import com.sun.star.lang.IllegalArgumentException; 43 44 public class Control extends Shape 45 { 46 47 XControlModel xControlModel; 48 XControl xControl; 49 public XPropertySet xPropertySet; 50 XPropertySet xControlPropertySet; 51 XWindowPeer xWindowPeer; 52 Object oDefaultValue; 53 GridControl oGridControl; 54 String sServiceName; 55 XNamed xNamed; 56 final int SOMAXTEXTSIZE = 50; 57 private int icontroltype; 58 protected XNameContainer xFormName; 59 protected final int IIMGFIELDWIDTH = 3000; 60 Control()61 public Control() 62 { 63 } 64 Control(FormHandler _oFormHandler, String _sServiceName, Point _aPoint)65 public Control(FormHandler _oFormHandler, String _sServiceName, Point _aPoint) 66 { 67 super(_oFormHandler, _sServiceName, _aPoint, null); 68 } 69 Control(FormHandler _oFormHandler, XNameContainer _xFormName, int _icontroltype, String _FieldName, Point _aPoint, Size _aSize)70 public Control(FormHandler _oFormHandler, XNameContainer _xFormName, int _icontroltype, String _FieldName, Point _aPoint, Size _aSize) 71 { 72 super(_oFormHandler, _aPoint, _aSize); 73 xFormName = _xFormName; 74 createControl(_icontroltype, _aPoint, _aSize, null, _FieldName); 75 } 76 Control(FormHandler _oFormHandler, XShapes _xGroupShapes, XNameContainer _xFormName, int _icontroltype, Point _aPoint, Size _aSize)77 public Control(FormHandler _oFormHandler, XShapes _xGroupShapes, XNameContainer _xFormName, int _icontroltype, Point _aPoint, Size _aSize) 78 { 79 super(_oFormHandler, _aPoint, _aSize); 80 xFormName = _xFormName; 81 createControl(_icontroltype, _aPoint, _aSize, _xGroupShapes, null); 82 } 83 Control(FormHandler _oFormHandler, int _icontroltype, Point _aPoint, Size _aSize)84 public Control(FormHandler _oFormHandler, int _icontroltype, Point _aPoint, Size _aSize) 85 { 86 super(_oFormHandler, _aPoint, _aSize); 87 createControl(_icontroltype, _aPoint, _aSize, null, null); 88 } 89 createControl(int _icontroltype, Point _aPoint, Size _aSize, XShapes _xGroupShapes, String _FieldName)90 public void createControl(int _icontroltype, Point _aPoint, Size _aSize, XShapes _xGroupShapes, String _FieldName) 91 { 92 try 93 { 94 icontroltype = _icontroltype; 95 sServiceName = oFormHandler.sModelServices[getControlType()]; 96 Object oControlModel = oFormHandler.xMSFDoc.createInstance(sServiceName); 97 xControlModel = UnoRuntime.queryInterface( XControlModel.class, oControlModel ); 98 xPropertySet = UnoRuntime.queryInterface( XPropertySet.class, oControlModel ); 99 100 XPropertySetInfo xPSI = xPropertySet.getPropertySetInfo(); 101 if ( xPSI.hasPropertyByName( "MouseWheelBehavior" ) ) 102 xPropertySet.setPropertyValue( "MouseWheelBehavior", new Short( com.sun.star.awt.MouseWheelBehavior.SCROLL_DISABLED ) ); 103 104 insertControlInContainer(_FieldName); 105 xControlShape.setControl(xControlModel); 106 if (_xGroupShapes == null) 107 { 108 oFormHandler.xDrawPage.add(xShape); 109 } 110 else 111 { 112 _xGroupShapes.add(xShape); 113 } 114 xControl = oFormHandler.xControlAccess.getControl(xControlModel); 115 xControlPropertySet = UnoRuntime.queryInterface( XPropertySet.class, xControl ); 116 xWindowPeer = xControl.getPeer(); 117 } 118 catch (Exception e) 119 { 120 e.printStackTrace(System.out); 121 } 122 } 123 insertControlInContainer(String _fieldname)124 public void insertControlInContainer(String _fieldname) 125 { 126 try 127 { 128 if (xFormName != null) 129 { 130 XNameAccess xNameAccess = UnoRuntime.queryInterface(XNameAccess.class, xFormName); 131 String sControlName = Desktop.getUniqueName(xNameAccess, getControlName(_fieldname)); 132 xPropertySet.setPropertyValue(PropertyNames.PROPERTY_NAME, sControlName); 133 xFormName.insertByName(sControlName, xControlModel); 134 } 135 } 136 catch (Exception e) 137 { 138 e.printStackTrace(System.out); 139 } 140 } 141 getControlName(String _fieldname)142 public String getControlName(String _fieldname) 143 { 144 String controlname = PropertyNames.EMPTY_STRING; 145 switch (getControlType()) 146 { 147 case FormHandler.SOLABEL: 148 controlname = "lbl" + _fieldname; 149 break; 150 case FormHandler.SOTEXTBOX: 151 controlname = "txt" + _fieldname; 152 break; 153 case FormHandler.SOCHECKBOX: 154 controlname = "chk" + _fieldname; 155 break; 156 case FormHandler.SODATECONTROL: 157 controlname = "dat" + _fieldname; 158 break; 159 case FormHandler.SOTIMECONTROL: 160 controlname = "tim" + _fieldname; 161 break; 162 case FormHandler.SONUMERICCONTROL: 163 controlname = "fmt" + _fieldname; 164 break; 165 case FormHandler.SOGRIDCONTROL: 166 controlname = "grd" + _fieldname; 167 break; 168 case FormHandler.SOIMAGECONTROL: 169 controlname = "img" + _fieldname; 170 break; 171 default: 172 controlname = "ctrl" + _fieldname; 173 } 174 return controlname; 175 } 176 setDefaultValue(Object DatabaseField)177 private void setDefaultValue(Object DatabaseField) 178 { 179 oDefaultValue = Helper.getUnoPropertyValue(DatabaseField, "DefaultValue"); 180 } 181 getPreferredWidth(String sText)182 public int getPreferredWidth(String sText) 183 { 184 Size aPeerSize = getPreferredSize(sText); 185 return ((aPeerSize.Width + 10) * oFormHandler.getXPixelFactor()); 186 } 187 getPreferredHeight(String sText)188 public int getPreferredHeight(String sText) 189 { 190 Size aPeerSize = getPreferredSize(sText); 191 if (getControlType() == FormHandler.SOCHECKBOX) 192 { 193 return (aPeerSize.Height * oFormHandler.getXPixelFactor()); 194 } 195 else 196 { 197 return ((aPeerSize.Height + 2) * oFormHandler.getXPixelFactor()); 198 } 199 } 200 getPreferredWidth()201 public int getPreferredWidth() 202 { 203 if (getControlType() == FormHandler.SOIMAGECONTROL) 204 { 205 return IIMGFIELDWIDTH; 206 } 207 else 208 { 209 Size aPeerSize = getPeerSize(); 210 // We increase the preferred Width a bit so that the control does not become too small 211 // when we change the border from "3D" to "Flat" 212 if (getControlType() == FormHandler.SOCHECKBOX) 213 { 214 return ((aPeerSize.Width * oFormHandler.getXPixelFactor())); 215 } 216 else 217 { 218 return ((aPeerSize.Width * oFormHandler.getXPixelFactor()) + 200); 219 } 220 } 221 } 222 getPreferredHeight()223 public int getPreferredHeight() 224 { 225 if (getControlType() == FormHandler.SOIMAGECONTROL) 226 { 227 return 2000; 228 } 229 else 230 { 231 Size aPeerSize = getPeerSize(); 232 int nHeight = aPeerSize.Height; 233 // We increase the preferred Height a bit so that the control does not become too small 234 // when we change the border from "3D" to "Flat" 235 return ((nHeight + 1) * oFormHandler.getYPixelFactor()); 236 } 237 } 238 getPreferredSize(String sText)239 public Size getPreferredSize(String sText) 240 { 241 try 242 { 243 if (xPropertySet.getPropertySetInfo().hasPropertyByName("Text")) 244 { 245 xPropertySet.setPropertyValue("Text", sText); 246 } 247 else if (xPropertySet.getPropertySetInfo().hasPropertyByName(PropertyNames.PROPERTY_LABEL)) 248 { 249 xPropertySet.setPropertyValue(PropertyNames.PROPERTY_LABEL, sText); 250 } 251 else 252 { 253 throw new IllegalArgumentException(); 254 } 255 return getPeer().getPreferredSize(); 256 } 257 catch (Exception e) 258 { 259 e.printStackTrace(System.out); 260 return null; 261 } 262 } 263 setPropertyValue(String _sPropertyName, Object _aPropertyValue)264 public void setPropertyValue(String _sPropertyName, Object _aPropertyValue) throws Exception 265 { 266 if (xPropertySet.getPropertySetInfo().hasPropertyByName(_sPropertyName)) 267 { 268 xPropertySet.setPropertyValue(_sPropertyName, _aPropertyValue); 269 } 270 } 271 272 /** the peer should be retrieved every time before it is used because it 273 * might be disposed otherwise 274 * 275 * @return 276 */ getPeer()277 public XLayoutConstrains getPeer() 278 { 279 return UnoRuntime.queryInterface(XLayoutConstrains.class, xControl.getPeer()); 280 } 281 getPeerSize()282 public Size getPeerSize() 283 { 284 try 285 { 286 Size aPreferredSize = null; 287 double dblEffMax = 0; 288 if (xPropertySet.getPropertySetInfo().hasPropertyByName("EffectiveMax")) 289 { 290 Object oValue = xPropertySet.getPropertyValue("EffectiveMax"); 291 if (xPropertySet.getPropertyValue("EffectiveMax") != com.sun.star.uno.Any.VOID) 292 { 293 dblEffMax = AnyConverter.toDouble(xPropertySet.getPropertyValue("EffectiveMax")); 294 } 295 if (dblEffMax == 0) 296 { 297 // This is relevant for decimal fields 298 xPropertySet.setPropertyValue("EffectiveValue", new Double(99999)); 299 } 300 else 301 { 302 xPropertySet.setPropertyValue("EffectiveValue", new Double(dblEffMax)); //new Double(100000.2)); // 303 } 304 aPreferredSize = getPeer().getPreferredSize(); 305 xPropertySet.setPropertyValue("EffectiveValue", com.sun.star.uno.Any.VOID); 306 } 307 else if (getControlType() == FormHandler.SOCHECKBOX) 308 { 309 aPreferredSize = getPeer().getPreferredSize(); 310 } 311 else if (getControlType() == FormHandler.SODATECONTROL) 312 { 313 xPropertySet.setPropertyValue("Date", 4711); //TODO find a better date 314 aPreferredSize = getPeer().getPreferredSize(); 315 xPropertySet.setPropertyValue("Date", com.sun.star.uno.Any.VOID); 316 } 317 else if (getControlType() == FormHandler.SOTIMECONTROL) 318 { 319 xPropertySet.setPropertyValue("Time", 47114); //TODO find a better time 320 aPreferredSize = getPeer().getPreferredSize(); 321 xPropertySet.setPropertyValue("Time", com.sun.star.uno.Any.VOID); 322 } 323 else 324 { 325 String stext; 326 short iTextLength = AnyConverter.toShort(xPropertySet.getPropertyValue("MaxTextLen")); 327 if (iTextLength < this.SOMAXTEXTSIZE) 328 { 329 stext = FormHandler.SOSIZETEXT.substring(0, this.SOMAXTEXTSIZE); 330 } 331 else 332 { 333 stext = FormHandler.SOSIZETEXT.substring(0, iTextLength); 334 } 335 xPropertySet.setPropertyValue("Text", stext); 336 aPreferredSize = getPeer().getPreferredSize(); 337 xPropertySet.setPropertyValue("Text", PropertyNames.EMPTY_STRING); 338 } 339 return aPreferredSize; 340 } 341 catch (Exception e) 342 { 343 e.printStackTrace(System.out); 344 return null; 345 } 346 } 347 348 /** 349 * @return 350 */ getControlType()351 public int getControlType() 352 { 353 return icontroltype; 354 } 355 } 356