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 com.sun.star.awt.XWindowPeer;
26 import com.sun.star.beans.PropertyValue;
27 import com.sun.star.lang.XMultiServiceFactory;
28 import com.sun.star.sdb.application.DatabaseObject;
29 import com.sun.star.wizards.common.Helper;
30 import com.sun.star.wizards.common.PropertyNames;
31 import com.sun.star.wizards.common.JavaTools;
32 import com.sun.star.wizards.common.NoValidPathException;
33 import com.sun.star.wizards.common.Properties;
34 import com.sun.star.wizards.db.DatabaseObjectWizard;
35 import com.sun.star.wizards.db.RelationController;
36 import com.sun.star.wizards.document.OfficeDocument;
37 import com.sun.star.wizards.ui.CommandFieldSelection;
38 import com.sun.star.wizards.ui.UIConsts;
39 
40 public class FormWizard extends DatabaseObjectWizard
41 {
42 
43     private CommandFieldSelection curDBCommandFieldSelection;
44     private FormConfiguration curFormConfiguration;
45     private CommandFieldSelection curSubFormFieldSelection;
46     private FormDocument curFormDocument;
47     private FieldLinker curFieldLinker;
48     private UIControlArranger curControlArranger;
49     private DataEntrySetter CurDataEntrySetter;
50     private StyleApplier curStyleApplier;
51     private Finalizer curFinalizer;
52     private static String slblFields;
53     private static String slblSelFields;
54     private String sShowBinaryFields = PropertyNames.EMPTY_STRING;
55     private String serrFormNameexists = PropertyNames.EMPTY_STRING;
56     public static final int SOMAIN_PAGE = 1;
57     public static final int SOSUBFORM_PAGE = 2;
58     public static final int SOSUBFORMFIELDS_PAGE = 3;
59     public static final int SOFIELDLINKER_PAGE = 4;
60     public static final int SOCONTROL_PAGE = 5;
61     public static final int SODATA_PAGE = 6;
62     public static final int SOSTYLE_PAGE = 7;
63     public static final int SOSTORE_PAGE = 8;
64     public static final int COLUMNAR_LEFT = 1;
65     public static final int COLUMNAR_TOP = 2;
66     public static final int AS_GRID = 3;
67     public static final int IN_BLOCK_TOP = 4;
68     private String slblTables;
69     private boolean m_openForEditing;
70     private boolean m_success = false;
71     private String FormName;
72 
FormWizard(XMultiServiceFactory i_servicFactory, final PropertyValue[] i_wizardContext)73     public FormWizard(XMultiServiceFactory i_servicFactory, final PropertyValue[] i_wizardContext)
74     {
75         super(i_servicFactory, 34400, i_wizardContext);
76         super.addResourceHandler("FormWizard", "dbw");
77         Helper.setUnoPropertyValues(xDialogModel,
78                 new String[]
79                 {
80                     PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_MOVEABLE, PropertyNames.PROPERTY_NAME, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_TABINDEX, PropertyNames.PROPERTY_TITLE, PropertyNames.PROPERTY_WIDTH
81                 },
82                 new Object[]
83                 {
84                     210, Boolean.TRUE, "DialogForm", 102, 41, 1, new Short((short) 0), m_oResource.getResText(UIConsts.RID_FORM), 310
85                 });
86         drawNaviBar();
87         if (getFormResources())
88         {
89             setRightPaneHeaders(m_oResource, UIConsts.RID_FORM + 90, 8);
90         }
91     }
92 
main(String i_args[])93     public static void main(String i_args[])
94     {
95         executeWizardFromCommandLine( i_args, FormWizard.class.getName() );
96     }
97 
98     // @Override
enterStep(int nOldStep, int nNewStep)99     protected void enterStep(int nOldStep, int nNewStep)
100     {
101         try
102         {
103             if ((nOldStep < SOCONTROL_PAGE) && (nNewStep >= SOCONTROL_PAGE))
104             {
105                 curFormDocument.initialize(curDBCommandFieldSelection.isModified(), curFormConfiguration.hasSubForm(), curSubFormFieldSelection.isModified(), getBorderType());
106                 curDBCommandFieldSelection.setModified(false);
107                 curSubFormFieldSelection.setModified(false);
108             }
109             switch (nNewStep)
110             {
111                 case SOMAIN_PAGE:
112                     curDBCommandFieldSelection.setModified(false);
113                     break;
114                 case SOSUBFORM_PAGE:
115                 {
116                     final String sCommandName = curDBCommandFieldSelection.getSelectedCommandName();
117                     RelationController oRelationController = new RelationController(curFormDocument.oMainFormDBMetaData, sCommandName);
118                     curFormConfiguration.initialize(curSubFormFieldSelection, oRelationController);
119                 }
120                 break;
121                 case SOSUBFORMFIELDS_PAGE:
122                 {
123                     String sPreSelectedTableName = curFormConfiguration.getreferencedTableName();
124                     boolean bReadOnly = (sPreSelectedTableName.length() > 0);
125                     if (sPreSelectedTableName.length() == 0)
126                     {
127                         final String sTableName = curSubFormFieldSelection.getSelectedCommandName();
128                         String[] aFieldNames = curSubFormFieldSelection.getSelectedFieldNames();
129                         curFormDocument.oSubFormDBMetaData.initializeFieldColumns(true, sTableName, aFieldNames);
130                     }
131                     else
132                     {
133                         curSubFormFieldSelection.preselectCommand(sPreSelectedTableName, bReadOnly);
134                     }
135                 }
136                 break;
137                 case SOFIELDLINKER_PAGE:
138                 {
139                     final String[] aMainFieldNames = curFormDocument.oMainFormDBMetaData.getFieldNames();
140                     final String[] aSubFieldNames = curFormDocument.oSubFormDBMetaData.getFieldNames();
141                     curFieldLinker.initialize(aMainFieldNames, aSubFieldNames, curFormDocument.LinkFieldNames);
142                 }
143                 break;
144                 case SOCONTROL_PAGE:
145                     curControlArranger.enableSubFormImageList(curFormConfiguration.hasSubForm());
146                     break;
147                 case SODATA_PAGE:
148                     break;
149                 case SOSTYLE_PAGE:
150                     break;
151                 case SOSTORE_PAGE:
152                 {
153                     String sTableName = this.curDBCommandFieldSelection.getSelectedCommandName();
154                     this.curFinalizer.initialize(sTableName, curFormDocument);
155                 }
156                 break;
157                 default:
158                     break;
159             }
160         }
161         catch (Exception e)
162         {
163             e.printStackTrace(System.out);
164         }
165     }
166 
getBorderType()167     protected Short getBorderType()
168     {
169         return curStyleApplier.getBorderType();
170     }
171 
172     // @Override
leaveStep(int nOldStep, int nNewStep)173     protected void leaveStep(int nOldStep, int nNewStep)
174     {
175         switch (nOldStep)
176         {
177             case SOMAIN_PAGE:
178             {
179                 final String sTableName = curDBCommandFieldSelection.getSelectedCommandName();
180                 final String[] aFieldNames = curDBCommandFieldSelection.getSelectedFieldNames();
181                 curFormDocument.oMainFormDBMetaData.initializeFieldColumns(true, sTableName, aFieldNames);
182 
183                 final String[] aMainFieldNames = curFormDocument.oMainFormDBMetaData.getFieldNames();
184                 try
185                 {
186                     curFormDocument.LinkFieldNames = JavaTools.removeOutdatedFields(curFormDocument.LinkFieldNames, aMainFieldNames, 1);
187                 }
188                 catch (java.lang.Exception e)
189                 {
190                 }
191             }
192             break;
193             case SOSUBFORM_PAGE:
194                 break;
195             case SOSUBFORMFIELDS_PAGE:
196             {
197                 final String sTableName = curSubFormFieldSelection.getSelectedCommandName();
198                 final String[] aFieldNames = curSubFormFieldSelection.getSelectedFieldNames();
199                 curFormDocument.oSubFormDBMetaData.initializeFieldColumns(true, sTableName, aFieldNames);
200 
201                 final String[] aSubFieldNames = curFormDocument.oSubFormDBMetaData.getFieldNames();
202                 try
203                 {
204                     curFormDocument.LinkFieldNames = JavaTools.removeOutdatedFields(curFormDocument.LinkFieldNames, aSubFieldNames, 0);
205                 }
206                 catch (java.lang.Exception e)
207                 {
208                 }
209             }
210             break;
211             case SOFIELDLINKER_PAGE:
212                 curFormDocument.LinkFieldNames = curFieldLinker.getLinkFieldNames();
213                 break;
214             case SOCONTROL_PAGE:
215                 break;
216             case SODATA_PAGE:
217                 break;
218             case SOSTYLE_PAGE:
219                 break;
220             case SOSTORE_PAGE:
221                 break;
222             default:
223                 break;
224         }
225     }
226 
buildSteps()227     public void buildSteps() throws NoValidPathException
228     {
229         curDBCommandFieldSelection = new CommandFieldSelection(this, curFormDocument.oMainFormDBMetaData, 92, slblFields, slblSelFields, slblTables, true, 34411);
230         curDBCommandFieldSelection.addFieldSelectionListener(new FieldSelectionListener());
231         curFormDocument.xProgressBar.setValue(20);
232         // Label Help Text
233         insertLabel("lblBinaryHelpText",
234                 new String[]
235                 {
236                     PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_LABEL, PropertyNames.PROPERTY_MULTILINE, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_WIDTH
237                 },
238                 new Object[]
239                 {
240                     28, sShowBinaryFields, Boolean.TRUE, 95, 154, new Integer(SOMAIN_PAGE), 210
241                 });
242 
243         curFormConfiguration = new FormConfiguration(this);
244         curFormDocument.xProgressBar.setValue(30);
245 
246         curSubFormFieldSelection = new CommandFieldSelection(this, curFormDocument.oSubFormDBMetaData, SOSUBFORMFIELDS_PAGE, 92, slblFields, slblSelFields, slblTables, true, 34431);
247         curSubFormFieldSelection.addFieldSelectionListener(new FieldSelectionListener());
248         insertLabel("lblSubFormBinaryHelpText",
249                 new String[]
250                 {
251                     PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_LABEL, PropertyNames.PROPERTY_MULTILINE, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_WIDTH
252                 },
253                 new Object[]
254                 {
255                     28, sShowBinaryFields, Boolean.TRUE, 95, 154, new Integer(SOSUBFORMFIELDS_PAGE), 210
256                 });
257 
258         curFormDocument.xProgressBar.setValue(40);
259 
260         curFieldLinker = new FieldLinker(this, SOFIELDLINKER_PAGE, 95, 30, 210, 34441);
261         curFormDocument.xProgressBar.setValue(50);
262 
263         curControlArranger = new UIControlArranger(this, curFormDocument);
264         curFormDocument.addUIFormController(curControlArranger);
265         curFormDocument.xProgressBar.setValue(60);
266 
267         CurDataEntrySetter = new DataEntrySetter(this);
268         curFormDocument.xProgressBar.setValue(70);
269 
270         curStyleApplier = new StyleApplier(this, curFormDocument);
271         curFormDocument.addStyleApplier(curStyleApplier);
272         curFormDocument.xProgressBar.setValue(80);
273 
274         curFinalizer = new Finalizer(this);
275         curFormDocument.xProgressBar.setValue(100);
276 
277         enableNavigationButtons(false, false, false);
278         curFormDocument.xProgressBar.end();
279     }
280 
281     // @Override
finishWizard()282     public boolean finishWizard()
283     {
284         int ncurStep = getCurrentStep();
285         if ((switchToStep(ncurStep, SOSTORE_PAGE)) || (ncurStep == SOSTORE_PAGE))
286         {
287             this.curFinalizer.initialize(curDBCommandFieldSelection.getSelectedCommandName(), this.curFormDocument);
288             String sNewFormName = curFinalizer.getName();
289             if (!curFormDocument.oMainFormDBMetaData.hasFormDocumentByName(sNewFormName))
290             {
291                 m_openForEditing = curFinalizer.getOpenForEditing();
292                 FormName = curFinalizer.getName();
293                 if (curFormDocument.finalizeForms(CurDataEntrySetter, curFieldLinker, curFormConfiguration))
294                 {
295 
296                     if (curFinalizer.finish())
297                     {
298                         m_success = true;
299                         xDialog.endExecute();
300                         return true;
301                     }
302                 }
303             }
304             else
305             {
306                 String smessage = JavaTools.replaceSubString(serrFormNameexists, sNewFormName, "%FORMNAME");
307                 showMessageBox("WarningBox", com.sun.star.awt.VclWindowPeerAttribute.OK, smessage);
308             }
309         }
310         return false;
311     }
312 
313     // @Override
cancelWizard()314     public void cancelWizard()
315     {
316         m_success = false;
317         xDialog.endExecute();
318     }
319 
insertFormRelatedSteps()320     public void insertFormRelatedSteps()
321     {
322         addRoadmap();
323         int i = 0;
324         i = insertRoadmapItem(0, true, m_oResource.getResText(UIConsts.RID_FORM + 80), SOMAIN_PAGE);
325         i = insertRoadmapItem(i, false, m_oResource.getResText(UIConsts.RID_FORM + 81), SOSUBFORM_PAGE);
326         i = insertRoadmapItem(i, false, m_oResource.getResText(UIConsts.RID_FORM + 82), SOSUBFORMFIELDS_PAGE);
327         i = insertRoadmapItem(i, false, m_oResource.getResText(UIConsts.RID_FORM + 83), SOFIELDLINKER_PAGE);
328         i = insertRoadmapItem(i, false, m_oResource.getResText(UIConsts.RID_FORM + 84), SOCONTROL_PAGE);
329         i = insertRoadmapItem(i, false, m_oResource.getResText(UIConsts.RID_FORM + 85), SODATA_PAGE);
330         i = insertRoadmapItem(i, false, m_oResource.getResText(UIConsts.RID_FORM + 86), SOSTYLE_PAGE);
331         i = insertRoadmapItem(i, false, m_oResource.getResText(UIConsts.RID_FORM + 87), SOSTORE_PAGE);
332         setRoadmapInteractive(true);
333         setRoadmapComplete(true);
334         setCurrentRoadmapItemID((short) 1);
335     }
336 
start()337     public void start()
338     {
339         try
340         {
341             curFormDocument = new FormDocument(xMSF);
342             if (curFormDocument.oMainFormDBMetaData.getConnection(m_wizardContext))
343             {
344                 curFormDocument.oSubFormDBMetaData.getConnection(new PropertyValue[]
345                         {
346                             Properties.createProperty(PropertyNames.ACTIVE_CONNECTION, curFormDocument.oMainFormDBMetaData.DBConnection)
347                         });
348                 curFormDocument.xProgressBar.setValue(20);
349                 buildSteps();
350                 this.curDBCommandFieldSelection.preselectCommand(m_wizardContext, false);
351                 XWindowPeer xWindowPeer2 = createWindowPeer(curFormDocument.xWindowPeer);
352                 curFormDocument.oMainFormDBMetaData.setWindowPeer(xWindowPeer2);
353                 insertFormRelatedSteps();
354                 short dialogReturn = executeDialog(curFormDocument.xFrame);
355                 xComponent.dispose();
356                 if ((dialogReturn == 0) && m_success)
357                 {
358                     curFormDocument.oMainFormDBMetaData.addFormDocument(curFormDocument.xComponent);
359                     loadSubComponent(DatabaseObject.FORM, FormName, m_openForEditing);
360                 }
361             }
362         }
363         catch (java.lang.Exception jexception)
364         {
365             jexception.printStackTrace(System.out);
366         }
367         if ((!m_success) && (curFormDocument != null))
368         {
369             OfficeDocument.close(curFormDocument.xComponent);
370         }
371     }
372 
getFormResources()373     private boolean getFormResources()
374     {
375         sShowBinaryFields = m_oResource.getResText(UIConsts.RID_FORM + 2);
376         slblTables = m_oResource.getResText(UIConsts.RID_FORM + 6);
377         slblFields = m_oResource.getResText(UIConsts.RID_FORM + 12);
378         slblSelFields = m_oResource.getResText(UIConsts.RID_FORM + 1);
379         serrFormNameexists = m_oResource.getResText(UIConsts.RID_FORM + 98);
380 
381         return true;
382     }
383 
384     public class FieldSelectionListener implements com.sun.star.wizards.ui.XFieldSelectionListener
385     {
386 
387         protected int ID;
388 
389         // @Override
getID()390         public int getID()
391         {
392             return ID;
393         }
394 
395         // @Override
setID(String sIncSuffix)396         public void setID(String sIncSuffix)
397         {
398             ID = 1;
399             if (sIncSuffix != null)
400             {
401                 if ((!sIncSuffix.equals(PropertyNames.EMPTY_STRING)) && (!sIncSuffix.equals("_")))
402                 {
403                     String sID = JavaTools.ArrayoutofString(sIncSuffix, "_")[1];
404                     ID = Integer.parseInt(sID);
405                     int a = 0;
406                 }
407             }
408         }
409 
410         // @Override
shiftFromLeftToRight(String[] SelItems, String[] NewItems)411         public void shiftFromLeftToRight(String[] SelItems, String[] NewItems)
412         {
413             if (ID == 1)
414             {
415                 toggleMainFormSteps();
416             }
417             else
418             {
419                 toggleSubFormSteps();
420             }
421         }
422 
423         // @Override
shiftFromRightToLeft(String[] SelItems, String[] NewItems)424         public void shiftFromRightToLeft(String[] SelItems, String[] NewItems)
425         {
426             // TODO When the ListFieldbox is refilled only fields of the current Command may be merged into the Listbox
427             if (ID == 1)
428             {
429                 toggleMainFormSteps();
430             }
431             else
432             {
433                 toggleSubFormSteps();
434             }
435         }
436         // @Override
437 
moveItemDown(String item)438         public void moveItemDown(String item)
439         {
440         }
441 
442         // @Override
moveItemUp(String item)443         public void moveItemUp(String item)
444         {
445         }
446 
toggleSubFormSteps()447         private boolean toggleSubFormSteps()
448         {
449             curSubFormFieldSelection.setModified(true);
450             boolean benable = curSubFormFieldSelection.getSelectedFieldNames().length > 0;
451             enablefromStep(SOFIELDLINKER_PAGE, benable);
452             if (benable)
453             {
454                 curFieldLinker.enable(!curFormConfiguration.areexistingRelationsdefined());
455             }
456             return benable;
457         }
458 
toggleMainFormSteps()459         private void toggleMainFormSteps()
460         {
461             curDBCommandFieldSelection.setModified(true);
462             boolean enabled = curDBCommandFieldSelection.getSelectedFieldNames().length > 0;
463             enablefromStep(SOSUBFORM_PAGE, enabled);
464             setControlProperty("btnWizardNext", PropertyNames.PROPERTY_ENABLED, enabled);
465             if (enabled)
466             {
467                 if (curFormConfiguration.hasSubForm())
468                 {
469                     enabled = toggleSubFormSteps();
470                 }
471                 else
472                 {
473                     setStepEnabled(SOSUBFORMFIELDS_PAGE, false);
474                     setStepEnabled(SOFIELDLINKER_PAGE, false);
475                 }
476             }
477             setControlProperty("btnWizardFinish", PropertyNames.PROPERTY_ENABLED, enabled);
478         }
479     }
480 }
481