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