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.table;
24 
25 import java.util.Hashtable;
26 
27 import com.sun.star.awt.TextEvent;
28 import com.sun.star.awt.VclWindowPeerAttribute;
29 import com.sun.star.awt.XTextListener;
30 import com.sun.star.beans.PropertyValue;
31 import com.sun.star.lang.XInitialization;
32 import com.sun.star.lang.XMultiServiceFactory;
33 import com.sun.star.sdb.CommandType;
34 import com.sun.star.sdb.application.DatabaseObject;
35 import com.sun.star.sdbc.SQLException;
36 import com.sun.star.task.XJobExecutor;
37 import com.sun.star.uno.UnoRuntime;
38 import com.sun.star.wizards.common.*;
39 import com.sun.star.wizards.db.DatabaseObjectWizard;
40 import com.sun.star.wizards.db.TableDescriptor;
41 import com.sun.star.wizards.ui.*;
42 
43 public class TableWizard extends DatabaseObjectWizard implements XTextListener, XCompletion
44 {
45 
46     static String slblFields;
47     static String slblSelFields;
48     Finalizer curFinalizer;
49     ScenarioSelector curScenarioSelector;
50     FieldFormatter curFieldFormatter;
51     PrimaryKeyHandler curPrimaryKeyHandler;
52     String sMsgWizardName = PropertyNames.EMPTY_STRING;
53     public Hashtable fielditems;
54     int wizardmode;
55     String tablename;
56     String serrToManyFields;
57     String serrTableNameexists;
58     String scomposedtablename;
59     TableDescriptor curTableDescriptor;
60     public static final int SONULLPAGE = 0;
61     public static final int SOMAINPAGE = 1;
62     public static final int SOFIELDSFORMATPAGE = 2;
63     public static final int SOPRIMARYKEYPAGE = 3;
64     public static final int SOFINALPAGE = 4;
65     private String sMsgColumnAlreadyExists = PropertyNames.EMPTY_STRING;
66     String WizardHeaderText[] = new String[8];
67 
68     private String m_tableName;
69 
TableWizard( XMultiServiceFactory xMSF, PropertyValue[] i_wizardContext )70     public TableWizard( XMultiServiceFactory xMSF, PropertyValue[] i_wizardContext )
71     {
72         super( xMSF, 41200, i_wizardContext );
73         super.addResourceHandler("TableWizard", "dbw");
74         String sTitle = m_oResource.getResText(UIConsts.RID_TABLE + 1);
75         Helper.setUnoPropertyValues(xDialogModel,
76                 new String[]
77                 {
78                     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
79                 },
80                 new Object[]
81                 {
82                     218, Boolean.TRUE, "DialogTable", 102, 41, 1, new Short((short) 0), sTitle, 330
83                 });
84         drawNaviBar();
85         fielditems = new Hashtable();
86         //TODO if reportResouces cannot be gotten dispose officedocument
87         if (getTableResources())
88         {
89             setRightPaneHeaders(m_oResource, UIConsts.RID_TABLE + 8, 4);
90         }
91     }
92 
leaveStep(int nOldStep, int nNewStep)93     protected void leaveStep(int nOldStep, int nNewStep)
94     {
95         switch (nOldStep)
96         {
97             case SOMAINPAGE:
98                 curScenarioSelector.addColumnsToDescriptor();
99                 break;
100             case SOFIELDSFORMATPAGE:
101                 curFieldFormatter.updateColumnofColumnDescriptor();
102                 String[] sfieldnames = curFieldFormatter.getFieldNames();
103                 super.setStepEnabled(SOFIELDSFORMATPAGE, sfieldnames.length > 0);
104                 curScenarioSelector.setSelectedFieldNames(sfieldnames);
105                 break;
106             case SOPRIMARYKEYPAGE:
107                 break;
108             case SOFINALPAGE:
109                 break;
110             default:
111                 break;
112         }
113     }
114 
enterStep(int nOldStep, int nNewStep)115     protected void enterStep(int nOldStep, int nNewStep)
116     {
117         switch (nNewStep)
118         {
119             case SOMAINPAGE:
120                 break;
121             case SOFIELDSFORMATPAGE:
122                 curFieldFormatter.initialize(curTableDescriptor, this.curScenarioSelector.getSelectedFieldNames());
123                 break;
124             case SOPRIMARYKEYPAGE:
125                 curPrimaryKeyHandler.initialize();
126                 break;
127             case SOFINALPAGE:
128                 curFinalizer.initialize(curScenarioSelector.getFirstTableName());
129                 break;
130             default:
131                 break;
132         }
133     }
134 
135 
136     /* (non-Javadoc)
137      * @see com.sun.star.wizards.ui.XCompletion#iscompleted(int)
138      */
iscompleted(int _ndialogpage)139     public boolean iscompleted(int _ndialogpage)
140     {
141         switch (_ndialogpage)
142         {
143             case SOMAINPAGE:
144                 return curScenarioSelector.iscompleted();
145             case SOFIELDSFORMATPAGE:
146                 return this.curFieldFormatter.iscompleted();
147             case SOPRIMARYKEYPAGE:
148                 if (curPrimaryKeyHandler != null)
149                 {
150                     return this.curPrimaryKeyHandler.iscompleted();
151                 }
152             case SOFINALPAGE:
153                 return this.curFinalizer.iscompleted();
154             default:
155                 return false;
156         }
157     }
158 
159 
160     /* (non-Javadoc)
161      * @see com.sun.star.wizards.ui.XCompletion#setcompleted(int, boolean)
162      */
setcompleted(int _ndialogpage, boolean _biscompleted)163     public void setcompleted(int _ndialogpage, boolean _biscompleted)
164     {
165         boolean bScenarioiscompleted = _biscompleted;
166         boolean bFieldFormatsiscompleted = _biscompleted;
167         boolean bPrimaryKeysiscompleted = _biscompleted;
168         boolean bFinalPageiscompleted = _biscompleted;
169         if (_ndialogpage == SOMAINPAGE)
170         {
171             curFinalizer.initialize(curScenarioSelector.getFirstTableName());
172         }
173         else
174         {
175             bScenarioiscompleted = iscompleted(SOMAINPAGE);
176         }
177         if (_ndialogpage != TableWizard.SOFIELDSFORMATPAGE)
178         {
179             bFieldFormatsiscompleted = iscompleted(SOFIELDSFORMATPAGE);
180             if (!bFieldFormatsiscompleted)                              // it might be that the Fieldformatter has not yet been initialized
181             {
182                 bFieldFormatsiscompleted = bScenarioiscompleted;        // in this case query the scenarioselector
183             }
184         }
185         if (_ndialogpage != TableWizard.SOPRIMARYKEYPAGE && (this.curPrimaryKeyHandler != null))
186         {
187             bPrimaryKeysiscompleted = iscompleted(SOPRIMARYKEYPAGE);
188         }
189         if (_ndialogpage != TableWizard.SOFINALPAGE)
190         {
191             bFinalPageiscompleted = iscompleted(SOFINALPAGE);           // Basically the finalpage is always enabled
192         }
193         if (bScenarioiscompleted)
194         {
195             super.setStepEnabled(SOFIELDSFORMATPAGE, true);
196             super.setStepEnabled(SOPRIMARYKEYPAGE, true);
197             if (bPrimaryKeysiscompleted)
198             {
199                 super.enablefromStep(SOFINALPAGE, true);
200                 super.enableFinishButton(bFinalPageiscompleted);
201             }
202             else
203             {
204                 super.enablefromStep(SOFINALPAGE, false);
205                 enableNextButton(false);
206             }
207         }
208         else if (_ndialogpage == SOFIELDSFORMATPAGE)
209         {
210             super.enablefromStep(super.getCurrentStep() + 1, iscompleted(SOFIELDSFORMATPAGE));
211         }
212         else
213         {
214             super.enablefromStep(super.getCurrentStep() + 1, false);
215         }
216     }
217 
218 /*
219     public static void main(String args[])
220     {
221         String ConnectStr = "uno:socket,host=localhost,port=8100;urp,negotiate=0,forcesynchronous=1;StarOffice.NamingService";
222         PropertyValue[] curproperties = null;
223         try
224         {
225             XMultiServiceFactory xLocMSF = com.sun.star.wizards.common.Desktop.connect(ConnectStr);
226             TableWizard CurTableWizard = new TableWizard(xLocMSF);
227             if (xLocMSF != null)
228             {
229                 System.out.println("Connected to " + ConnectStr);
230                 curproperties = new PropertyValue[1];
231                 curproperties[0] = Properties.createProperty("DataSourceName", "Bibliography");
232                 //curproperties[0] = Properties.createProperty("DatabaseLocation", "file:///path/to/database.odb");
233                 CurTableWizard.startTableWizard(xLocMSF, curproperties);
234             }
235         }
236         catch (Exception exception)
237         {
238             exception.printStackTrace(System.out);
239         }
240     }
241 */
buildSteps()242     public void buildSteps()
243     {
244         curScenarioSelector = new ScenarioSelector(this, this.curTableDescriptor, slblFields, slblSelFields);
245         curFieldFormatter = new FieldFormatter(this, curTableDescriptor);
246         if ( this.curTableDescriptor.supportsPrimaryKeys() )
247         {
248             curPrimaryKeyHandler = new PrimaryKeyHandler(this, curTableDescriptor);
249         }
250         curFinalizer = new Finalizer(this, curTableDescriptor);
251         enableNavigationButtons(false, false, false);
252     }
253 
createTable()254     public boolean createTable()
255     {
256         boolean bIsSuccessfull = true;
257         boolean bTableCreated = false;
258         String schemaname = curFinalizer.getSchemaName();
259         String catalogname = curFinalizer.getCatalogName();
260         if (curTableDescriptor.supportsPrimaryKeys())
261         {
262             String[] keyfieldnames = curPrimaryKeyHandler.getPrimaryKeyFields(curTableDescriptor);
263             if (keyfieldnames != null)
264             {
265                 if (keyfieldnames.length > 0)
266                 {
267                     boolean bIsAutoIncrement = curPrimaryKeyHandler.isAutoIncremented();
268                     bIsSuccessfull = curTableDescriptor.createTable(catalogname, schemaname, tablename, keyfieldnames, bIsAutoIncrement, curScenarioSelector.getSelectedFieldNames());
269                     bTableCreated = true;
270                 }
271             }
272         }
273         if (!bTableCreated)
274         {
275             bIsSuccessfull = curTableDescriptor.createTable(catalogname, schemaname, tablename, curScenarioSelector.getSelectedFieldNames());
276         }
277         if ((!bIsSuccessfull) && (curPrimaryKeyHandler.isAutomaticMode()))
278         {
279             curTableDescriptor.dropColumnbyName(curPrimaryKeyHandler.getAutomaticFieldName());
280         }
281         return bIsSuccessfull;
282     }
283 
finishWizard()284     public boolean finishWizard()
285     {
286         super.switchToStep(super.getCurrentStep(), SOFINALPAGE);
287         tablename = curFinalizer.getTableName(curScenarioSelector.getFirstTableName());
288         scomposedtablename = curFinalizer.getComposedTableName(tablename);
289         if (this.curTableDescriptor.isSQL92CheckEnabled())
290         {
291             Desktop.removeSpecialCharacters(curTableDescriptor.xMSF, Configuration.getOfficeLocale(this.curTableDescriptor.xMSF), tablename);
292         }
293         if ( tablename.length() > 0 )
294         {
295             if (!curTableDescriptor.hasTableByName(scomposedtablename))
296             {
297                 wizardmode = curFinalizer.finish();
298                 if (createTable())
299                 {
300                     final boolean editTableDesign = (wizardmode == Finalizer.MODIFYTABLEMODE );
301                     loadSubComponent( DatabaseObject.TABLE, curTableDescriptor.getComposedTableName(), editTableDesign );
302                     m_tableName = curTableDescriptor.getComposedTableName();
303                     super.xDialog.endExecute();
304                     return true;
305                 }
306             }
307             else
308             {
309                 String smessage = JavaTools.replaceSubString(serrTableNameexists, tablename, "%TABLENAME");
310                 super.showMessageBox("WarningBox", com.sun.star.awt.VclWindowPeerAttribute.OK, smessage);
311                 curFinalizer.setFocusToTableNameControl();
312             }
313         }
314         return false;
315     }
316 
callFormWizard()317     private void callFormWizard()
318     {
319         try
320         {
321             Object oFormWizard = this.xMSF.createInstance("com.sun.star.wizards.form.CallFormWizard");
322 
323             NamedValueCollection wizardContext = new NamedValueCollection();
324             wizardContext.put( PropertyNames.ACTIVE_CONNECTION, curTableDescriptor.DBConnection );
325             wizardContext.put( "DataSource", curTableDescriptor.getDataSource() );
326             wizardContext.put( PropertyNames.COMMAND_TYPE, CommandType.TABLE );
327             wizardContext.put( PropertyNames.COMMAND, scomposedtablename );
328             wizardContext.put( "DocumentUI", m_docUI );
329             XInitialization xInitialization = UnoRuntime.queryInterface( XInitialization.class, oFormWizard );
330             xInitialization.initialize( wizardContext.getPropertyValues() );
331             XJobExecutor xJobExecutor = UnoRuntime.queryInterface( XJobExecutor.class, oFormWizard );
332             xJobExecutor.trigger(PropertyNames.START);
333         }
334         catch (Exception e)
335         {
336             e.printStackTrace(System.out);
337         }
338     }
339 
cancelWizard()340     public void cancelWizard()
341     {
342         xDialog.endExecute();
343     }
344 
insertFormRelatedSteps()345     public void insertFormRelatedSteps()
346     {
347         addRoadmap();
348         int i = 0;
349         i = insertRoadmapItem(0, true, m_oResource.getResText(UIConsts.RID_TABLE + 2), SOMAINPAGE);
350         i = insertRoadmapItem(i, false, m_oResource.getResText(UIConsts.RID_TABLE + 3), SOFIELDSFORMATPAGE);
351         if (this.curTableDescriptor.supportsPrimaryKeys())
352         {
353             i = insertRoadmapItem(i, false, m_oResource.getResText(UIConsts.RID_TABLE + 4), SOPRIMARYKEYPAGE);
354         }
355         i = insertRoadmapItem(i, false, m_oResource.getResText(UIConsts.RID_TABLE + 5), SOFINALPAGE);        // Orderby is always supported
356         setRoadmapInteractive(true);
357         setRoadmapComplete(true);
358         setCurrentRoadmapItemID((short) 1);
359     }
360 
startTableWizard( )361     public String startTableWizard(  )
362     {
363         try
364         {
365             curTableDescriptor = new TableDescriptor(xMSF, super.xWindow, this.sMsgColumnAlreadyExists);
366             if ( curTableDescriptor.getConnection( m_wizardContext ) )
367             {
368                 buildSteps();
369                 createWindowPeer();
370                 curTableDescriptor.setWindowPeer(this.xControl.getPeer());
371                 insertFormRelatedSteps();
372                 short RetValue = executeDialog();
373                 xComponent.dispose();
374                 if ( RetValue == 0 )
375                 {
376                     if (  wizardmode == Finalizer.STARTFORMWIZARDMODE )
377                         callFormWizard();
378                     return m_tableName;
379                 }
380             }
381         }
382         catch (java.lang.Exception jexception)
383         {
384             jexception.printStackTrace(System.out);
385         }
386         return PropertyNames.EMPTY_STRING;
387     }
388 
getTableResources()389     public boolean getTableResources()
390     {
391         sMsgWizardName = super.m_oResource.getResText(UIConsts.RID_TABLE + 1);
392         slblFields = m_oResource.getResText(UIConsts.RID_TABLE + 19);
393         slblSelFields = m_oResource.getResText(UIConsts.RID_TABLE + 25);
394         serrToManyFields = m_oResource.getResText(UIConsts.RID_TABLE + 47);
395         serrTableNameexists = m_oResource.getResText(UIConsts.RID_TABLE + 48);
396         sMsgColumnAlreadyExists = m_oResource.getResText(UIConsts.RID_TABLE + 51);
397         return true;
398     }
399 
verifyfieldcount(int _icount)400     public boolean verifyfieldcount(int _icount)
401     {
402         try
403         {
404             int maxfieldcount = curTableDescriptor.getMaxColumnsInTable();
405             if (_icount >= (maxfieldcount - 1))
406             {   // keep one column as reserve for the automaticcally created key
407                 String smessage = serrToManyFields;
408                 smessage = JavaTools.replaceSubString(smessage, String.valueOf(maxfieldcount), "%COUNT");
409                 showMessageBox("ErrorBox", VclWindowPeerAttribute.OK, smessage);
410                 return false;
411             }
412         }
413         catch (SQLException e)
414         {
415             e.printStackTrace(System.out);
416         }
417         return true;
418     }
419 
420 
421     /* (non-Javadoc)
422      * @see com.sun.star.awt.XTextListener#textChanged(com.sun.star.awt.TextEvent)
423      */
textChanged(TextEvent aTextEvent)424     public void textChanged(TextEvent aTextEvent)
425     {
426         if (this.curTableDescriptor.isSQL92CheckEnabled())
427         {
428             Object otextcomponent = UnoDialog.getModel(aTextEvent.Source);
429             String sName = (String) Helper.getUnoPropertyValue(otextcomponent, "Text");
430             sName = Desktop.removeSpecialCharacters(curTableDescriptor.xMSF, Configuration.getOfficeLocale(curTableDescriptor.xMSF), sName);
431             Helper.setUnoPropertyValue(otextcomponent, "Text", sName);
432         }
433     }
434 }
435