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.form;
24 
25 import java.util.Vector;
26 
27 import com.sun.star.wizards.text.TextDocument;
28 import com.sun.star.awt.Point;
29 import com.sun.star.awt.Size;
30 import com.sun.star.beans.PropertyValue;
31 import com.sun.star.beans.XPropertySet;
32 import com.sun.star.container.XNameContainer;
33 import com.sun.star.lang.XMultiServiceFactory;
34 import com.sun.star.uno.Exception;
35 import com.sun.star.uno.UnoRuntime;
36 import com.sun.star.wizards.db.*;
37 import com.sun.star.wizards.common.*;
38 import com.sun.star.wizards.ui.*;
39 import com.sun.star.wizards.text.TextStyleHandler;
40 import com.sun.star.wizards.text.ViewHandler;
41 import com.sun.star.wizards.document.Control;
42 import com.sun.star.wizards.document.DatabaseControl;
43 import com.sun.star.wizards.document.FormHandler;
44 import com.sun.star.wizards.document.GridControl;
45 
46 public class FormDocument extends TextDocument
47 {
48     protected Vector oControlForms = new Vector();
49     protected CommandMetaData oMainFormDBMetaData;
50     protected CommandMetaData oSubFormDBMetaData;
51     protected String[][] LinkFieldNames;
52 
53     private FormHandler oFormHandler;
54     private ViewHandler oViewHandler;
55     private TextStyleHandler oTextStyleHandler;
56     private XPropertySet xPropPageStyle;
57     private final int SOFORMGAP = 2000;
58     private boolean bhasSubForm;
59     private UIControlArranger curUIControlArranger;
60     private StyleApplier curStyleApplier;
61     private int nPageWidth;
62     private int nPageHeight;
63     private int nFormWidth;
64     private int nFormHeight;
65     private Point aMainFormPoint;
66     private final static String SOMAINFORM = "MainForm";
67     private final static String SOSUBFORM = "SubForm";
68 
FormDocument(XMultiServiceFactory xMSF)69     public FormDocument(XMultiServiceFactory xMSF)
70     {
71         super(xMSF, new TextDocument.ModuleIdentifier("com.sun.star.sdb.FormDesign"), true);
72         try
73         {
74             oFormHandler = new FormHandler(xMSF, xTextDocument);
75             oFormHandler.setDrawObjectsCaptureMode(false);
76             oTextStyleHandler = new TextStyleHandler(xMSFDoc, xTextDocument);
77             oViewHandler = new ViewHandler(xMSFDoc, xTextDocument);
78             oMainFormDBMetaData = new CommandMetaData(xMSF);// , CharLocale);
79             oSubFormDBMetaData = new CommandMetaData(xMSF);// , CharLocale);
80             ViewHandler oViewHandler = new ViewHandler(xMSF, xTextDocument);
81             TextStyleHandler oTextStyleSupplier = new TextStyleHandler(xMSFDoc, xTextDocument);
82             Helper.setUnoPropertyValue(xTextDocument, "ApplyFormDesignMode", Boolean.FALSE);
83             oViewHandler.setViewSetting("ShowTableBoundaries", Boolean.FALSE);
84             oViewHandler.setViewSetting("ShowOnlineLayout", Boolean.TRUE);
85             xPropPageStyle = oTextStyleSupplier.getStyleByName("PageStyles", "Standard");
86             Size aSize = oTextStyleHandler.changePageAlignment(xPropPageStyle, true);
87             nPageWidth = aSize.Width;
88             nPageHeight = aSize.Height;
89         }
90         catch (Exception e)
91         {
92             e.printStackTrace(System.out);
93         }
94     }
95 
addUIFormController(UIControlArranger _curUIControlArranger)96     public void addUIFormController(UIControlArranger _curUIControlArranger)
97     {
98         this.curUIControlArranger = _curUIControlArranger;
99     }
100 
addStyleApplier(StyleApplier _curStyleApplier)101     public void addStyleApplier(StyleApplier _curStyleApplier)
102     {
103         this.curStyleApplier = _curStyleApplier;
104     }
105 
getDataSourceName()106     private String getDataSourceName()
107     {
108         return this.oMainFormDBMetaData.DataSourceName;
109     }
110 
adjustPageStyle()111     private void adjustPageStyle()
112     {
113         try
114         {
115             int nMargin;
116             int totfieldcount = getMainFieldCount() + getSubFieldCount();
117             if (totfieldcount > 30)
118             {
119                 nMargin = 500;
120             }
121             else if (totfieldcount > 20)
122             {
123                 nMargin = 750;
124             }
125             else
126             {
127                 nMargin = 1000;
128             }
129             xPropPageStyle.setPropertyValue("RightMargin", new Integer(nMargin));
130             xPropPageStyle.setPropertyValue("LeftMargin", new Integer(nMargin));
131             xPropPageStyle.setPropertyValue("TopMargin", new Integer(nMargin));
132             xPropPageStyle.setPropertyValue("BottomMargin", new Integer(nMargin));
133             aMainFormPoint = new Point(nMargin, nMargin);
134             nFormWidth = (int) (0.8 * (double) nPageWidth) - 2 * nMargin;
135             nFormHeight = (int) (0.65 * (double) nPageHeight) - 2 * nMargin;
136         }
137         catch (Exception e)
138         {
139             e.printStackTrace(System.out);
140         }
141     }
142 
initialize(boolean _baddParentForm, boolean _bShouldHaveSubForm, boolean _bModifySubForm, Short _NBorderType)143     public void initialize(boolean _baddParentForm, boolean _bShouldHaveSubForm, boolean _bModifySubForm, Short _NBorderType)
144     {
145         bhasSubForm = _bShouldHaveSubForm;
146         adjustPageStyle();
147         if (_baddParentForm)
148         {
149             if (oControlForms.size() == 0)
150             {
151                 final ControlForm aMainControlForm = new ControlForm(this, SOMAINFORM, aMainFormPoint, getMainFormSize(FormWizard.AS_GRID));
152                 oControlForms.addElement(aMainControlForm);
153             }
154             else
155             {
156                 oFormHandler.removeControlsofForm(SOMAINFORM);
157                 ((ControlForm) oControlForms.get(0)).oFormController = null;
158             }
159             ((ControlForm) oControlForms.get(0)).initialize(curUIControlArranger.getSelectedArrangement(0), _NBorderType);
160         }
161         if (_bShouldHaveSubForm)
162         {
163             if (oControlForms.size() == 1)
164             {
165                 adjustMainFormSize(_NBorderType);
166                 final ControlForm aSubControlForm = new ControlForm(this, SOSUBFORM, getSubFormPoint(), getSubFormSize());
167                 oControlForms.addElement(aSubControlForm);
168                 /* ((ControlForm) oControlForms.get(1))*/
169                 aSubControlForm.initialize(curUIControlArranger.getSelectedArrangement(1), _NBorderType);
170             }
171             else if (_bModifySubForm)
172             {
173                 if (oControlForms.size() > 1)
174                 {
175                     oFormHandler.removeControlsofForm(SOSUBFORM);
176                     ((ControlForm) oControlForms.get(1)).oFormController = null;
177                     ((ControlForm) oControlForms.get(1)).initialize(curUIControlArranger.getSelectedArrangement(1), _NBorderType);
178                 }
179             }
180         }
181         else
182         {
183             ControlForm aMainForm = (ControlForm) oControlForms.get(0);
184             // boolean bHasSubForm = aMainForm.xFormContainer.hasByName(SOSUBFORM);
185             // WRONG if (oFormHandler.hasFormByName(SOSUBFORM))
186             if (aMainForm.xFormContainer != null && aMainForm.xFormContainer.hasByName(SOSUBFORM))
187             {
188                 oFormHandler.removeControlsofForm(SOSUBFORM);
189                 oFormHandler.removeElement( aMainForm.xFormContainer, SOSUBFORM );
190                 ((ControlForm) oControlForms.get(1)).oFormController = null;
191                 // aMainForm.xFormContainer = null; // .removeFormByName(SOSUBFORM);
192                 oControlForms.remove(1);
193                 adjustMainFormSize(_NBorderType);
194             }
195         }
196     }
197 
getMainFieldCount()198     private int getMainFieldCount()
199     {
200         return oMainFormDBMetaData.getFieldNames().length;
201     }
getSubFieldCount()202     private int getSubFieldCount()
203     {
204         return oSubFormDBMetaData.getFieldNames().length;
205     }
206 
getMainFormSize(int _curArrangement)207     private Size getMainFormSize(int _curArrangement)
208     {
209         int nMainFormHeight = nFormHeight;
210         if (bhasSubForm)
211         {
212             if (_curArrangement == FormWizard.AS_GRID)
213             {
214                 nMainFormHeight = (int) ((double) (nFormHeight - SOFORMGAP) / 2);
215             }
216             else
217             {
218                 int nTotalFieldCount = getMainFieldCount() + getSubFieldCount();
219                 nMainFormHeight = (int) (((double) getMainFieldCount() / (double) nTotalFieldCount) * ((double) (nFormHeight - SOFORMGAP) / 2));
220             }
221         }
222         return new Size(nFormWidth, nMainFormHeight);
223     }
224 
getSubFormSize()225     private Size getSubFormSize()
226     {
227 //      int nSubFormHeight = (int) ((double)nFormHeight/2) - SOFORMGAP;
228 //      int nSubFormFieldCount = this.oSubFormDBMetaData.FieldNames.length;
229 //      int totfieldcount = oMainFormDBMetaData.FieldNames.length + nSubFormFieldCount;
230         int nMainFormHeight = ((ControlForm) oControlForms.get(0)).getActualFormHeight();
231         return new Size(nFormWidth, nFormHeight - nMainFormHeight - SOFORMGAP);
232     }
233 
getSubFormPoint()234     private Point getSubFormPoint()
235     {
236         ControlForm curMainControlForm = ((ControlForm) oControlForms.get(0));
237         return new Point(curMainControlForm.aStartPoint.X,
238                 (curMainControlForm.aStartPoint.Y + curMainControlForm.getFormSize().Height + SOFORMGAP));
239     }
240 
adjustMainFormSize(Short _NBorderType)241     private void adjustMainFormSize(Short _NBorderType)
242     {
243         ControlForm oMainControlForm = (ControlForm) oControlForms.get(0);
244         oMainControlForm.setFormSize(getMainFormSize(oMainControlForm.curArrangement));
245         if (oMainControlForm.curArrangement == FormWizard.AS_GRID)
246         {
247             oMainControlForm.oGridControl.setSize(oMainControlForm.getFormSize());
248         }
249         else
250         {
251             oMainControlForm.oFormController.positionControls(oMainControlForm.curArrangement,
252                     oMainControlForm.aStartPoint,
253                     oMainControlForm.getFormSize(),
254                     curUIControlArranger.getAlignValue(), _NBorderType);
255         }
256     }
257 
adjustSubFormPosSize(Short _NBorderType)258     private void adjustSubFormPosSize(Short _NBorderType)
259     {
260         ControlForm oMainControlForm = (ControlForm) oControlForms.get(0);
261         ControlForm oSubControlForm = (ControlForm) oControlForms.get(1);
262         oSubControlForm.setFormSize(new Size(nFormWidth, nFormHeight - oMainControlForm.getFormSize().Height));
263         if (oSubControlForm.curArrangement == FormWizard.AS_GRID)
264         {
265             Point aPoint = oSubControlForm.oGridControl.getPosition();
266             int idiffheight = oSubControlForm.getEntryPointY() - oMainControlForm.getActualFormHeight() - oMainControlForm.aStartPoint.Y - SOFORMGAP;
267             oSubControlForm.setStartPoint(new Point(aPoint.X, (aPoint.Y - idiffheight)));
268             oSubControlForm.oGridControl.setPosition(oSubControlForm.aStartPoint);
269             oSubControlForm.oGridControl.setSize(getSubFormSize());
270         }
271         else
272         {
273 //          oSubControlForm.oFormController.adjustYPositions(_idiffheight);
274             oSubControlForm.setStartPoint(new Point(oSubControlForm.aStartPoint.X, oMainControlForm.getActualFormHeight() + oMainControlForm.aStartPoint.Y + SOFORMGAP));
275             oSubControlForm.oFormController.positionControls(oSubControlForm.curArrangement, oSubControlForm.aStartPoint, oSubControlForm.getAvailableFormSize(), curUIControlArranger.getAlignValue(), _NBorderType);
276         }
277     }
278 
getControlFormByName(String _sname)279     public ControlForm getControlFormByName(String _sname)
280     {
281         for (int i = 0; i < oControlForms.size(); i++)
282         {
283             ControlForm curControlForm = ((ControlForm) oControlForms.get(i));
284             if (curControlForm.Name.equals(_sname))
285             {
286                 return curControlForm;
287             }
288         }
289         return null;
290     }
291 
getControlForms()292     public ControlForm[] getControlForms()
293     {
294         ControlForm[] aResult = new ControlForm[oControlForms.size()];
295         oControlForms.toArray( aResult );
296         return aResult;
297     }
298 
finalizeForms(DataEntrySetter _curDataEntrySetter, FieldLinker _curFieldLinker, FormConfiguration _curFormConfiguration)299     public boolean finalizeForms(DataEntrySetter _curDataEntrySetter, FieldLinker _curFieldLinker, FormConfiguration _curFormConfiguration)
300     {
301         try
302         {
303             this.xTextDocument.lockControllers();
304             PropertyValue[] aFormProperties = _curDataEntrySetter.getFormProperties();
305             ControlForm oMasterControlForm = getControlFormByName(SOMAINFORM);
306             oMasterControlForm.setFormProperties(aFormProperties, oMainFormDBMetaData);
307             oMasterControlForm.finalizeControls();
308             if (oMasterControlForm.xFormContainer.hasByName(SOSUBFORM))
309             {
310                 ControlForm oSubControlForm = getControlFormByName(SOSUBFORM);
311                 oSubControlForm.setFormProperties(aFormProperties, oSubFormDBMetaData);
312                 String sRefTableName = _curFormConfiguration.getreferencedTableName();
313                 if (sRefTableName.equals(PropertyNames.EMPTY_STRING))
314                 {
315                     LinkFieldNames = _curFieldLinker.getLinkFieldNames();
316                 }
317                 else
318                 {
319                     LinkFieldNames = _curFieldLinker.getLinkFieldNames(_curFormConfiguration.getRelationController(), sRefTableName);
320                 }
321                 if (LinkFieldNames != null)
322                 {
323                     if (LinkFieldNames.length > 0)
324                     {
325                         oSubControlForm.xPropertySet.setPropertyValue("DetailFields", LinkFieldNames[0]);
326                         oSubControlForm.xPropertySet.setPropertyValue("MasterFields", LinkFieldNames[1]);
327                         oSubControlForm.finalizeControls();
328                         return true;
329                     }
330                 }
331                 return false;
332             }
333             return true;
334         }
335         catch (Exception e)
336         {
337             e.printStackTrace(System.out);
338             return false;
339         }
340         finally
341         {
342             unlockallControllers();
343         }
344     }
345 
346     public class ControlForm
347     {
348 
349         XNameContainer xFormContainer;
350         GridControl oGridControl;
351         FormControlArranger oFormController;
352         int curArrangement;
353         FormDocument oFormDocument;
354         String Name;
355         Point aStartPoint;
356         private Size aFormSize;
357         CommandMetaData oDBMetaData;
358         XPropertySet xPropertySet;
359 
ControlForm(FormDocument _oFormDocument, String _sname, Point _astartPoint, Size _aFormSize)360         public ControlForm(FormDocument _oFormDocument, String _sname, Point _astartPoint, Size _aFormSize)
361         {
362             aStartPoint = _astartPoint;
363             aFormSize = _aFormSize;
364             oFormDocument = _oFormDocument;
365             Name = _sname;
366             if (_sname.equals(SOSUBFORM))
367             {
368                 ControlForm oMainControlForm = ((ControlForm) oControlForms.get(0));
369                 xFormContainer = oFormHandler.insertFormbyName(_sname, oMainControlForm.xFormContainer);
370             }
371             else
372             {
373                 xFormContainer = oFormHandler.insertFormbyName(_sname);
374             }
375             xPropertySet = UnoRuntime.queryInterface(XPropertySet.class, xFormContainer);
376             if (_sname.equals(SOMAINFORM))
377             {
378                 oDBMetaData = oFormDocument.oMainFormDBMetaData;
379             }
380             else
381             {
382                 oDBMetaData = oFormDocument.oSubFormDBMetaData;
383             }
384         }
385 
initialize(int _curArrangement, Short _NBorderType)386         public void initialize(int _curArrangement, Short _NBorderType)
387         {
388             boolean adaptControlStyles = false;
389             xTextDocument.lockControllers();
390             curArrangement = _curArrangement;
391             if (oGridControl != null)
392             {
393                 oFormHandler.xDrawPage.remove(oGridControl.xShape);
394                 oGridControl.xComponent.dispose();
395                 oGridControl = null;
396             }
397             if (oFormController == null)
398             {
399                 oFormController = new FormControlArranger(oFormHandler, xFormContainer, oDBMetaData, xProgressBar, aStartPoint, aFormSize);
400             }
401             else
402             {
403                 if (curArrangement == FormWizard.AS_GRID)
404                 {
405                     oFormHandler.moveShapesToNirwana(getLabelControls());
406                     oFormHandler.moveShapesToNirwana(getDatabaseControls());
407                 }
408             }
409             if (curArrangement == FormWizard.AS_GRID)
410             {
411                 insertGridControl(_NBorderType);
412                 adaptControlStyles = true;
413             }
414             else
415             {
416                 adaptControlStyles = !oFormController.areControlsexisting();
417                 oFormController.positionControls(_curArrangement, aStartPoint, getAvailableFormSize(), curUIControlArranger.getAlignValue(), _NBorderType);
418             }
419             if (adaptControlStyles)
420             {
421                 curStyleApplier.applyStyle(false, true);
422             }
423             if ((Name.equals(SOMAINFORM)) && (oControlForms.size() > 1))
424             {
425                 ControlForm curSubControlForm = ((ControlForm) oControlForms.get(1));
426                 if (curSubControlForm != null)
427                 {
428                     adjustSubFormPosSize(_NBorderType);
429                 }
430             }
431             setFormSize(new Size(aFormSize.Width, getActualFormHeight()));
432             unlockallControllers();
433         }
434 
getLabelControls()435         public Control[] getLabelControls()
436         {
437             if (oFormController != null)
438             {
439                 return oFormController.getLabelControlList();
440             }
441             else
442             {
443                 return null;
444             }
445         }
446 
getFormSize()447         public Size getFormSize()
448         {
449             return aFormSize;
450         }
451 
getAvailableFormSize()452         private Size getAvailableFormSize()
453         {
454             if (this.Name.equals(SOMAINFORM))
455             {
456                 setFormSize(getMainFormSize(curArrangement));
457             }
458             else
459             {
460                 setFormSize(getSubFormSize());
461             }
462             return aFormSize;
463         }
464 
setFormSize(Size _aSize)465         public void setFormSize(Size _aSize)
466         {
467             aFormSize = _aSize;
468             oFormController.setFormSize(aFormSize);
469         }
470 
setStartPoint(Point _aPoint)471         private void setStartPoint(Point _aPoint)
472         {
473             aStartPoint = _aPoint;
474             if (oFormController != null)
475             {
476                 oFormController.setStartPoint(_aPoint);
477             }
478         }
479 
getActualFormHeight()480         private int getActualFormHeight()
481         {
482             if (curArrangement == FormWizard.AS_GRID)
483             {
484                 return oGridControl.xShape.getSize().Height;
485             }
486             else
487             {
488                 return oFormController.getFormHeight();
489             }
490         }
491 
getEntryPointY()492         private int getEntryPointY()
493         {
494             if (curArrangement == FormWizard.AS_GRID)
495             {
496                 return oGridControl.xShape.getPosition().Y;
497             }
498             else
499             {
500                 return oFormController.getEntryPointY();
501             }
502         }
503 
setFormProperties(PropertyValue[] _aPropertySetList, CommandMetaData _oDBMetaData)504         private void setFormProperties(PropertyValue[] _aPropertySetList, CommandMetaData _oDBMetaData)
505         {
506             try
507             {
508                 xPropertySet.setPropertyValue("DataSourceName", getDataSourceName());
509                 xPropertySet.setPropertyValue(PropertyNames.COMMAND, _oDBMetaData.getCommandName());
510                 xPropertySet.setPropertyValue(PropertyNames.COMMAND_TYPE, new Integer(_oDBMetaData.getCommandType()));
511                 for (int i = 0; i < _aPropertySetList.length; i++)
512                 {
513                     xPropertySet.setPropertyValue(_aPropertySetList[i].Name, _aPropertySetList[i].Value);
514                 }
515             }
516             catch (Exception e)
517             {
518                 e.printStackTrace(System.out);
519             }
520         }
521 
getDatabaseControls()522         public DatabaseControl[] getDatabaseControls()
523         {
524             if (oFormController != null)
525             {
526                 return oFormController.DBControlList;
527             }
528             else
529             {
530                 return null;
531             }
532         }
533 
getGridControl()534         public GridControl getGridControl()
535         {
536             return oGridControl;
537         }
538 
getArrangemode()539         public int getArrangemode()
540         {
541             return curArrangement;
542         }
543 
insertGridControl(Short _NBorderType)544         private void insertGridControl(Short _NBorderType)
545         {
546             try
547             {
548                 curArrangement = FormWizard.AS_GRID;
549                 if (Name.equals(SOMAINFORM))
550                 {
551                     oGridControl = new GridControl(xMSF, Name + "_Grid", oFormHandler, xFormContainer, oDBMetaData.FieldColumns, aStartPoint, getMainFormSize(FormWizard.AS_GRID));
552                 }
553                 else
554                 {
555                     oGridControl = new GridControl(xMSF, Name + "_Grid", oFormHandler, xFormContainer, oDBMetaData.FieldColumns, aStartPoint, getSubFormSize());
556                 }
557                 oGridControl.xPropertySet.setPropertyValue(PropertyNames.PROPERTY_BORDER, _NBorderType);
558             }
559             catch (Exception e)
560             {
561                 e.printStackTrace(System.out);
562             }
563         }
564 
finalizeControls()565         public void finalizeControls()
566         {
567             Control[] oLabelControls = getLabelControls();
568             Control[] oDBControls = getDatabaseControls();
569             if (oLabelControls != null)
570             {
571                 for (int i = 0; i < getLabelControls().length; i++)
572                 {
573                     if (curArrangement == FormWizard.AS_GRID)
574                     {
575                         if ((oLabelControls[i] != null) && (oDBControls[i] != null))
576                         {
577                             oFormHandler.removeShape(oLabelControls[i].xShape);
578                             oFormHandler.removeShape(oDBControls[i].xShape);
579                         }
580                     }
581                     else
582                     {
583                         oFormHandler.groupShapesTogether(xMSF, oLabelControls[i].xShape, oDBControls[i].xShape);
584                     }
585 
586                 }
587             }
588         }
589     }
590 }
591