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.ItemEvent;
26 import com.sun.star.awt.VclWindowPeerAttribute;
27 import com.sun.star.awt.XFixedText;
28 import com.sun.star.awt.XListBox;
29 import com.sun.star.uno.Exception;
30 import com.sun.star.wizards.common.Helper;
31 import com.sun.star.wizards.common.HelpIds;
32 import com.sun.star.wizards.common.JavaTools;
33 import com.sun.star.wizards.db.RelationController;
34 import com.sun.star.wizards.ui.UnoDialog;
35 import com.sun.star.wizards.ui.WizardDialog;
36 import com.sun.star.wizards.ui.UIConsts;
37 import com.sun.star.wizards.ui.DBLimitedFieldSelection;
38 import com.sun.star.wizards.common.PropertyNames;
39 
40 public class FieldLinker extends DBLimitedFieldSelection
41 {
42 
43     private XFixedText[] lblSlaveFields;
44     private XFixedText[] lblMasterFields;
45     private XListBox[] lstSlaveFields;
46     private XListBox[] lstMasterFields;
47     private int[] SOLINKLST = null;
48     private String[] sSlaveListHeader;
49     private String[] sMasterListHeader; //CurUnoDialog.m_oResource.getResText(UIConsts.RID_FORM + 40);
50 
FieldLinker(WizardDialog _CurUnoDialog, int iStep, int iCompPosX, int iCompPosY, int iCompWidth, int _firsthelpid)51     public FieldLinker(WizardDialog _CurUnoDialog, int iStep, int iCompPosX, int iCompPosY, int iCompWidth, int _firsthelpid)
52     {
53         super(_CurUnoDialog, iStep, iCompPosX, iCompPosY, iCompWidth, _firsthelpid);
54     }
55 
insertControlGroup(int i)56     protected void insertControlGroup(int i)
57     {
58         try
59         {
60             if (i == 0)
61             {
62                 lblSlaveFields = new XFixedText[rowcount];
63                 lblMasterFields = new XFixedText[rowcount];
64                 lstSlaveFields = new XListBox[rowcount];
65                 lstMasterFields = new XListBox[rowcount];
66                 int SOFIRSTLINKLST = 0;
67                 int SOSECLINKLST = 1;
68                 int SOTHIRDLINKLST = 2;
69                 int SOFOURTHLINKLST = 3;
70                 Integer IListBoxPosX = new Integer(iCompPosX + 6);
71                 sSlaveListHeader = CurUnoDialog.m_oResource.getResArray(UIConsts.RID_FORM + 20, 4); //new String[rowcount];PropertyNames.EMPTY_STRING; //CurUnoDialog.m_oResource.getResText(UIConsts.RID_FORM + 40);
72                 sMasterListHeader = CurUnoDialog.m_oResource.getResArray(UIConsts.RID_FORM + 24, 4);// new String[rowcount];PropertyNames.EMPTY_STRING; //CurUnoDialog.m_oResource.getResText(UIConsts.RID_FORM + 40);
73                 SOLINKLST = new int[]
74                         {
75                                 SOFIRSTLINKLST, SOSECLINKLST, SOTHIRDLINKLST, SOFOURTHLINKLST
76                         };
77             }
78             String sSlaveHidString = HelpIds.getHelpIdString(FirstHelpIndex + (i * 2));
79             String sMasterHidString = HelpIds.getHelpIdString(FirstHelpIndex + (i * 2) + 1);
80             boolean bDoEnable = (i < 2);
81             lblSlaveFields[i] = CurUnoDialog.insertLabel("lblSlaveFieldLink" + Integer.toString(i + 1),
82                     new String[]
83                     {
84                         PropertyNames.PROPERTY_ENABLED, PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_LABEL, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_TABINDEX, PropertyNames.PROPERTY_WIDTH
85                     },
86                     new Object[]
87                     {
88                             Boolean.valueOf(bDoEnable), 8, sSlaveListHeader[i], 97, new Integer(iCurPosY), IStep, new Short(curtabindex++), 97
89                     });
90             lstSlaveFields[i] = CurUnoDialog.insertListBox("lstSlaveFieldLink" + (i + 1), SOLINKLST[i], null, new ItemListenerImpl(),
91                     new String[]
92                     {
93                         "Dropdown",
94                         PropertyNames.PROPERTY_ENABLED,
95                         PropertyNames.PROPERTY_HEIGHT,
96                         PropertyNames.PROPERTY_HELPURL,
97                         "LineCount",
98                         PropertyNames.PROPERTY_POSITION_X,
99                         PropertyNames.PROPERTY_POSITION_Y,
100                         PropertyNames.PROPERTY_STEP,
101                         PropertyNames.PROPERTY_TABINDEX,
102                         PropertyNames.PROPERTY_WIDTH
103                     },
104                     new Object[]
105                     {
106                         Boolean.TRUE,
107                             Boolean.valueOf(bDoEnable),
108                         UIConsts.INTEGER_12,
109                             sSlaveHidString,
110                         Short.valueOf(UnoDialog.getListBoxLineCount()),
111                         97,
112                         new Integer(iCurPosY + 10),
113                         IStep,
114                         new Short(curtabindex++),
115                         97
116                     });
117 
118             lblMasterFields[i] = CurUnoDialog.insertLabel("lblMasterFieldLink" + Integer.toString(i + 1),
119                     new String[]
120                     {
121                         PropertyNames.PROPERTY_ENABLED, PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_LABEL, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_TABINDEX, PropertyNames.PROPERTY_WIDTH
122                     },
123                     new Object[]
124                     {
125                             Boolean.valueOf(bDoEnable), 8, sMasterListHeader[i], 206, new Integer(iCurPosY), IStep, new Short(curtabindex++), 97
126                     });
127 
128             lstMasterFields[i] = CurUnoDialog.insertListBox("lstMasterFieldLink" + Integer.toString(i + 1), SOLINKLST[i], null, new ItemListenerImpl(),
129                     new String[]
130                     {
131                         "Dropdown",
132                         PropertyNames.PROPERTY_ENABLED,
133                         PropertyNames.PROPERTY_HEIGHT,
134                         PropertyNames.PROPERTY_HELPURL,
135                         "LineCount",
136                         PropertyNames.PROPERTY_POSITION_X,
137                         PropertyNames.PROPERTY_POSITION_Y,
138                         PropertyNames.PROPERTY_STEP,
139                         PropertyNames.PROPERTY_TABINDEX,
140                         PropertyNames.PROPERTY_WIDTH
141                     },
142                     new Object[]
143                     {
144                         Boolean.TRUE,
145                             Boolean.valueOf(bDoEnable),
146                         UIConsts.INTEGER_12,
147                             sMasterHidString,
148                         Short.valueOf(UnoDialog.getListBoxLineCount()),
149                         206,
150                         new Integer(iCurPosY + 10),
151                         IStep,
152                         new Short(curtabindex++),
153                         97
154                     });
155             iCurPosY = iCurPosY + 38;
156         }
157         catch (Exception e)
158         {
159             e.printStackTrace(System.out);
160         }
161     }
162 
enableNextControlRow(int curindex)163     protected void enableNextControlRow(int curindex)
164     {
165         // setMaxSelIndex();
166         boolean bSlaveField = lstSlaveFields[curindex].getSelectedItemPos() > 0;
167         boolean bMasterField = lstMasterFields[curindex].getSelectedItemPos() > 0;
168         boolean bDoEnable = (bSlaveField && bMasterField);
169         if (!bDoEnable)
170         {
171             moveupSelectedItems(curindex, bDoEnable);
172         }
173         else
174         {
175             toggleControlRow(curindex + 1, true);
176         }
177     }
178 
getMaxSelIndex()179     protected int getMaxSelIndex()
180     {
181         int MaxSelIndex = -1;
182         for (int i = 0; i < rowcount; i++)
183         {
184             if ((lstSlaveFields[i].getSelectedItemPos() > 0) && (lstMasterFields[i].getSelectedItemPos() > 0))
185             {
186                 MaxSelIndex += 1;
187             }
188         }
189         return MaxSelIndex;
190     }
191 
toggleControlRow(int i, boolean bDoEnable)192     protected void toggleControlRow(int i, boolean bDoEnable)
193     {
194         if (i < rowcount)
195         {
196             Helper.setUnoPropertyValue(UnoDialog.getModel(lblSlaveFields[i]), PropertyNames.PROPERTY_ENABLED, Boolean.valueOf(bDoEnable));
197             Helper.setUnoPropertyValue(UnoDialog.getModel(lstSlaveFields[i]), PropertyNames.PROPERTY_ENABLED, Boolean.valueOf(bDoEnable));
198             Helper.setUnoPropertyValue(UnoDialog.getModel(lblMasterFields[i]), PropertyNames.PROPERTY_ENABLED, Boolean.valueOf(bDoEnable));
199             Helper.setUnoPropertyValue(UnoDialog.getModel(lstMasterFields[i]), PropertyNames.PROPERTY_ENABLED, Boolean.valueOf(bDoEnable));
200             if (!bDoEnable)
201             {
202                 Helper.setUnoPropertyValue(UnoDialog.getModel(lstSlaveFields[i]), PropertyNames.SELECTED_ITEMS, new short[] { 0 });
203                 Helper.setUnoPropertyValue(UnoDialog.getModel(lstMasterFields[i]), PropertyNames.SELECTED_ITEMS, new short[] { 0 });
204             }
205         }
206     }
207 
updateFromNextControlRow(int curindex)208     protected void updateFromNextControlRow(int curindex)
209     {
210         short iNextMasterItemPos = lstMasterFields[curindex + 1].getSelectedItemPos();
211         short iNextSlaveItemPos = lstSlaveFields[curindex + 1].getSelectedItemPos();
212 
213         if ((iNextMasterItemPos != 0) && (iNextSlaveItemPos != 0))
214         {
215             Helper.setUnoPropertyValue(UnoDialog.getModel(lstMasterFields[curindex]), PropertyNames.SELECTED_ITEMS, new short[] {iNextMasterItemPos });
216             Helper.setUnoPropertyValue(UnoDialog.getModel(lstSlaveFields[curindex]), PropertyNames.SELECTED_ITEMS, new short[]  {iNextSlaveItemPos});
217 
218             Helper.setUnoPropertyValue(UnoDialog.getModel(lstMasterFields[curindex + 1]), PropertyNames.SELECTED_ITEMS, new short[] { 0 });
219             Helper.setUnoPropertyValue(UnoDialog.getModel(lstSlaveFields[curindex + 1]), PropertyNames.SELECTED_ITEMS, new short[] { 0 });
220             toggleControlRow(curindex, true);
221         }
222     }
223 
initialize(String[] _AllMasterFieldNames, String[] _AllSlaveFieldNames, String[][] _LinkFieldNames)224     public void initialize(String[] _AllMasterFieldNames, String[] _AllSlaveFieldNames, String[][] _LinkFieldNames)
225     {
226         // short[] MasterSelList = null;
227         // short[] SlaveSelList = null;
228         int SOMASTERINDEX = 1;
229         String[] MasterLinkNames = JavaTools.ArrayOutOfMultiDimArray(_LinkFieldNames, SOMASTERINDEX);
230         int SOSLAVEINDEX = 0;
231         String[] SlaveLinkNames = JavaTools.ArrayOutOfMultiDimArray(_LinkFieldNames, SOSLAVEINDEX);
232         String[] ViewMasterFieldNames = addNoneFieldItemToList(_AllMasterFieldNames); // add '-undefiened-'
233         String[] ViewSlaveFieldNames = addNoneFieldItemToList(_AllSlaveFieldNames);
234         for (int i = 0; i < super.rowcount; i++)
235         {
236             super.initializeListBox(lstMasterFields[i], ViewMasterFieldNames, MasterLinkNames, i);
237             super.initializeListBox(lstSlaveFields[i], ViewSlaveFieldNames, SlaveLinkNames, i);
238             if (_LinkFieldNames != null)
239             {
240                 toggleControlRow(i, (i <= _LinkFieldNames.length));
241             }
242             else
243             {
244                 toggleControlRow(i, i == 0);
245             }
246         }
247     }
248 
getLinkFieldNames(RelationController _oRelationController, String _sReferencedTableName)249     public String[][] getLinkFieldNames(RelationController _oRelationController, String _sReferencedTableName)
250     {
251         return _oRelationController.getImportedKeyColumns(_sReferencedTableName);
252     }
253 
254     /**
255      * @return the LinkFieldnames of the joins. When no LinkFieldNames were selected the returned Array is empty.
256      * When Joins were assigned duplicate a null value is returned
257      *
258      */
getLinkFieldNames()259     public String[][] getLinkFieldNames()
260     {
261         // setMaxSelIndex();
262         int nSelectedIndex = getMaxSelIndex();
263         String[][] LinkFieldNames = new String[2][nSelectedIndex + 1];
264         for (int i = 0; i <= nSelectedIndex; i++)
265         {
266             LinkFieldNames[0][i] = lstSlaveFields[i].getSelectedItem();
267             LinkFieldNames[1][i] = lstMasterFields[i].getSelectedItem();
268         }
269         int iduplicate = JavaTools.getDuplicateFieldIndex(LinkFieldNames);
270         if (iduplicate != -1)
271         {
272             String sLinkFieldsAreDuplicate = CurUnoDialog.m_oResource.getResText(UIConsts.RID_FORM + 19);
273             String sLocLinkFieldsAreDuplicate = JavaTools.replaceSubString(sLinkFieldsAreDuplicate, LinkFieldNames[0][iduplicate], "<FIELDNAME1>");
274             sLocLinkFieldsAreDuplicate = JavaTools.replaceSubString(sLocLinkFieldsAreDuplicate, LinkFieldNames[1][iduplicate], "<FIELDNAME2>");
275             CurUnoDialog.setCurrentStep(FormWizard.SOFIELDLINKER_PAGE);
276             CurUnoDialog.enableNavigationButtons(true, true, true);
277             CurUnoDialog.showMessageBox("WarningBox", VclWindowPeerAttribute.OK, sLocLinkFieldsAreDuplicate);
278             CurUnoDialog.setFocus("lstSlaveFieldLink" + (iduplicate + 1));
279             return null;
280         }
281             return LinkFieldNames;
282 
283     }
284 
enable(boolean _bdoenable)285     public void enable(boolean _bdoenable)
286     {
287         CurUnoDialog.setStepEnabled(IStep.intValue(), _bdoenable);
288     }
289 
290     class ItemListenerImpl implements com.sun.star.awt.XItemListener
291     {
292 
itemStateChanged(ItemEvent EventObject)293         public void itemStateChanged(ItemEvent EventObject)
294         {
295             int ikey = CurUnoDialog.getControlKey(EventObject.Source, CurUnoDialog.ControlList);
296             enableNextControlRow(ikey);
297         }
298 
disposing(com.sun.star.lang.EventObject eventObject)299         public void disposing(com.sun.star.lang.EventObject eventObject)
300         {
301         }
302     }
303 }
304