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.ui;
28 
29 import java.beans.*;
30 
31 import com.sun.star.wizards.ui.event.CommonListener;
32 import com.sun.star.wizards.ui.event.EventNames;
33 import com.sun.star.wizards.ui.event.MethodInvocation;
34 import com.sun.star.uno.UnoRuntime;
35 import com.sun.star.awt.*;
36 import com.sun.star.uno.AnyConverter;
37 import com.sun.star.uno.XInterface;
38 import com.sun.star.lang.EventObject;
39 import com.sun.star.lang.XSingleServiceFactory;
40 import com.sun.star.lang.XMultiServiceFactory;
41 import com.sun.star.wizards.common.Desktop;
42 import com.sun.star.wizards.common.Helper;
43 import com.sun.star.wizards.common.Resource;
44 import com.sun.star.container.XIndexContainer;
45 import com.sun.star.frame.XTerminateListener;
46 import com.sun.star.frame.TerminationVetoException;
47 import com.sun.star.lang.IllegalArgumentException;
48 import com.sun.star.beans.*;
49 import com.sun.star.wizards.common.HelpIds;
50 import com.sun.star.wizards.common.PropertyNames;
51 
52 public abstract class WizardDialog extends UnoDialog2 implements VetoableChangeListener, XTerminateListener, XCompletion
53 {
54 
55     private static final String NEXT_ACTION_PERFORMED = "gotoNextAvailableStep";
56     private static final String BACK_ACTION_PERFORMED = "gotoPreviousAvailableStep";
57     private static final String FINISH_ACTION_PERFORMED = "finishWizard_1";
58     private static final String CANCEL_ACTION_PERFORMED = "cancelWizard_1";
59     private static final String HELP_ACTION_PERFORMED = "callHelp";
60     public VetoableChangeSupport vetos = new VetoableChangeSupport(this);
61     private String[] sRightPaneHeaders;
62     private int iButtonWidth = 50;
63     private int nNewStep = 1;
64     private int nOldStep = 1;
65     private int nMaxStep = 1;
66     protected XItemListener RoadmapItemListener;
67     protected XControl xRoadmapControl;
68     XItemEventBroadcaster xRoadmapBroadcaster;
69     String[] sRMItemLabels;
70     private Object oRoadmap;
71     private XSingleServiceFactory xSSFRoadmap;
72     public XIndexContainer xIndexContRoadmap;
73     private Resource oWizardResource;
74     public String sMsgEndAutopilot;
75     private int hid;
76     private boolean bTerminateListenermustberemoved = true;
77 
78     /** Creates a new instance of WizardDialog
79      * the hid is used as following :
80      * "HID:(hid)"   - the dialog
81      * "HID:(hid+1)  - the help button
82      * "HID:(hid+2)" - the back button
83      * "HID:(hid+3)" - the next button
84      * "HID:(hid+4)" - the create button
85      * "HID:(hid+5)" - the cancel button
86      * @param xMSF
87      * @param hid_
88      */
89     public WizardDialog(XMultiServiceFactory xMSF, int hid_)
90     {
91         super(xMSF);
92         hid = hid_;
93         oWizardResource = new Resource(xMSF, "Common", "dbw");
94         sMsgEndAutopilot = oWizardResource.getResText(UIConsts.RID_DB_COMMON + 33);
95 
96     //new Resource(xMSF,"Common","com");
97     }
98 
99     /**
100      *
101      * @return
102      */
103     public Resource getResource()
104     {
105         return oWizardResource;
106     }
107 
108     public void activate()
109     {
110         try
111         {
112             XTopWindow top = UnoRuntime.queryInterface(XTopWindow.class, xWindow);
113             if (top != null)
114             {
115                 top.toFront();
116             }
117         }
118         catch (Exception ex)
119         {
120             // do nothing;
121         }
122     }
123 
124     public void setMaxStep(int i)
125     {
126         nMaxStep = i;
127     }
128 
129     public int getMaxStep()
130     {
131         return nMaxStep;
132     }
133 
134     public void setOldStep(int i)
135     {
136         nOldStep = i;
137     }
138 
139     public int getOldStep()
140     {
141         return nOldStep;
142     }
143 
144     public void setNewStep(int i)
145     {
146         nNewStep = i;
147     }
148 
149     public int getNewStep()
150     {
151         return nNewStep;
152     }
153 
154     /**
155      * @see java.beans.VetoableChangeListener#vetoableChange(java.beans.PropertyChangeEvent)
156      */
157     public void vetoableChange(java.beans.PropertyChangeEvent arg0)
158     {
159         nNewStep = nOldStep;
160     }
161 
162     public void itemStateChanged(com.sun.star.awt.ItemEvent itemEvent)
163     {
164         try
165         {
166             nNewStep = itemEvent.ItemId;
167             nOldStep = AnyConverter.toInt(Helper.getUnoPropertyValue(xDialogModel, PropertyNames.PROPERTY_STEP));
168             if (nNewStep != nOldStep)
169             {
170                 switchToStep();
171             }
172         }
173         catch (com.sun.star.lang.IllegalArgumentException exception)
174         {
175             exception.printStackTrace(System.out);
176         }
177     }
178 
179     public void setRoadmapInteractive(boolean _bInteractive)
180     {
181         Helper.setUnoPropertyValue(oRoadmap, "Activated", Boolean.valueOf(_bInteractive));
182     }
183 
184     public void setRoadmapComplete(boolean bComplete)
185     {
186         Helper.setUnoPropertyValue(oRoadmap, "Complete", Boolean.valueOf(bComplete));
187     }
188 
189     public boolean isRoadmapComplete()
190     {
191         try
192         {
193             return AnyConverter.toBoolean(Helper.getUnoPropertyValue(oRoadmap, "Complete"));
194         }
195         catch (IllegalArgumentException exception)
196         {
197             exception.printStackTrace(System.out);
198             return false;
199         }
200     }
201 
202     public void setCurrentRoadmapItemID(short ID)
203     {
204         if (oRoadmap != null)
205         {
206             int nCurItemID = getCurrentRoadmapItemID();
207             if (nCurItemID != ID)
208             {
209                 Helper.setUnoPropertyValue(oRoadmap, "CurrentItemID", new Short(ID));
210             }
211         }
212     }
213 
214     public int getCurrentRoadmapItemID()
215     {
216         try
217         {
218             return AnyConverter.toInt(Helper.getUnoPropertyValue(oRoadmap, "CurrentItemID"));
219         }
220         catch (com.sun.star.uno.Exception exception)
221         {
222             exception.printStackTrace(System.out);
223             return -1;
224         }
225     }
226 
227     public void addRoadmap()
228     {
229         try
230         {
231             int iDialogHeight = ((Integer) Helper.getUnoPropertyValue(this.xDialogModel, PropertyNames.PROPERTY_HEIGHT)).intValue();
232 
233             // the roadmap control has got no real TabIndex ever
234             // that is not correct, but changing this would need time, so it is used
235             // without TabIndex as before
236             oRoadmap = insertControlModel("com.sun.star.awt.UnoControlRoadmapModel", "rdmNavi",
237                     new String[]
238                     {
239                         PropertyNames.PROPERTY_HEIGHT,
240                         PropertyNames.PROPERTY_POSITION_X,
241                         PropertyNames.PROPERTY_POSITION_Y,
242                         PropertyNames.PROPERTY_STEP,
243                         PropertyNames.PROPERTY_TABINDEX,
244                         "Tabstop",
245                         PropertyNames.PROPERTY_WIDTH
246                     },
247                     new Object[]
248                     {
249                         new Integer(iDialogHeight - 26),
250                         0,
251                         0,
252                         0,
253                         new Short((short)0),
254                         Boolean.TRUE,
255                         85
256                     });
257             XPropertySet xPSet = UnoRuntime.queryInterface(XPropertySet.class, oRoadmap);
258             xPSet.setPropertyValue(PropertyNames.PROPERTY_NAME, "rdmNavi");
259 
260             xSSFRoadmap = UnoRuntime.queryInterface(XSingleServiceFactory.class, oRoadmap);
261             xIndexContRoadmap = UnoRuntime.queryInterface(XIndexContainer.class, oRoadmap);
262             //    XPropertySet xPropRoadmapModel = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, oRoadmap);
263             //    xPropRoadmapModel.addPropertyChangeListener("CurrentItemID", new WizardDialog.RoadmapItemListener(this.xDialogModel));
264 
265             MethodInvocation mi = new MethodInvocation("itemStateChanged", this, com.sun.star.awt.ItemEvent.class);
266             getGuiEventListener().add("rdmNavi", EventNames.EVENT_ITEM_CHANGED, mi);
267             xRoadmapControl = this.xDlgContainer.getControl("rdmNavi");
268             xRoadmapBroadcaster = UnoRuntime.queryInterface(XItemEventBroadcaster.class, xRoadmapControl);
269             xRoadmapBroadcaster.addItemListener((XItemListener) getGuiEventListener());
270 
271             //     xRoadmapControl = this.xDlgContainer.getControl("rdmNavi");
272             //     xRoadmapBroadcaster.addItemListener(new RoadmapItemListener());
273             Helper.setUnoPropertyValue(oRoadmap, "Text", oWizardResource.getResText(UIConsts.RID_COMMON + 16));
274         }
275         catch (NoSuchMethodException ex)
276         {
277             Resource.showCommonResourceError(xMSF);
278         }
279         catch (java.lang.Exception jexception)
280         {
281             jexception.printStackTrace(System.out);
282         }
283     }
284 
285     public void setRMItemLabels(Resource _oResource, int StartResID)
286     {
287         sRMItemLabels = _oResource.getResArray(StartResID, nMaxStep);
288     }
289 
290     public String[] getRMItemLabels()
291     {
292         return sRMItemLabels;
293     }
294 
295     /*    public void insertRoadmapItems(int StartIndex, int RMCount)
296     {
297     Object oRoadmapItem;
298     boolean bEnabled;
299     for (int i = StartIndex; i < (StartIndex + RMCount); i++)
300     insertSingleRoadmapItem(i, true, sRMItemLabels[i], i);
301     }*/
302     public int insertRoadmapItem(int _Index, boolean _bEnabled, int _LabelID, int _CurItemID)
303     {
304         return insertRoadmapItem(_Index, _bEnabled, sRMItemLabels[_LabelID], _CurItemID);
305     }
306 
307     public int insertRoadmapItem(int Index, boolean _bEnabled, String _sLabel, int _CurItemID)
308     {
309         try
310         {
311             Object oRoadmapItem = xSSFRoadmap.createInstance();
312             Helper.setUnoPropertyValue(oRoadmapItem, PropertyNames.PROPERTY_LABEL, _sLabel);
313             Helper.setUnoPropertyValue(oRoadmapItem, PropertyNames.PROPERTY_ENABLED, Boolean.valueOf(_bEnabled));
314             Helper.setUnoPropertyValue(oRoadmapItem, "ID", new Integer(_CurItemID));
315             xIndexContRoadmap.insertByIndex(Index, oRoadmapItem);
316             return Index + 1;
317         }
318         catch (com.sun.star.uno.Exception exception)
319         {
320             exception.printStackTrace(System.out);
321             return -1;
322         }
323     }
324 
325     public int getRMItemCount()
326     {
327         return xIndexContRoadmap.getCount();
328     }
329 
330     public XInterface getRoadmapItemByID(int _ID)
331     {
332         try
333         {
334             int CurID;
335             XInterface CurRoadmapItem;
336             for (int i = 0; i < xIndexContRoadmap.getCount(); i++)
337             {
338                 CurRoadmapItem = (XInterface) xIndexContRoadmap.getByIndex(i);
339                 CurID = AnyConverter.toInt(Helper.getUnoPropertyValue(CurRoadmapItem, "ID"));
340                 if (CurID == _ID)
341                 {
342                     return CurRoadmapItem;
343                 }
344             }
345             return null;
346         }
347         catch (com.sun.star.uno.Exception exception)
348         {
349             exception.printStackTrace(System.out);
350             return null;
351         }
352     }
353 
354     public boolean switchToStep(int _nOldStep, int _nNewStep)
355     {
356         nOldStep = _nOldStep;
357         nNewStep = _nNewStep;
358         return switchToStep();
359     }
360 
361     private boolean switchToStep()
362     {
363         leaveStep(nOldStep, nNewStep);
364         if (nNewStep != nOldStep)
365         {
366             if (nNewStep == nMaxStep)
367             {
368                 setControlProperty("btnWizardNext", "DefaultButton", Boolean.FALSE);
369                 setControlProperty("btnWizardFinish", "DefaultButton", Boolean.TRUE);
370             }
371             else
372             {
373                 setControlProperty("btnWizardNext", "DefaultButton", Boolean.TRUE);
374                 setControlProperty("btnWizardFinish", "DefaultButton", Boolean.FALSE);
375             }
376             changeToStep(nNewStep);
377             enterStep(nOldStep, nNewStep);
378             return true;
379         }
380         return false;
381     }
382 
383     abstract protected void leaveStep(int nOldStep, int nNewStep);
384 
385     abstract protected void enterStep(int nOldStep, int nNewStep);
386 
387     protected void changeToStep(int nNewStep)
388     {
389         Helper.setUnoPropertyValue(xDialogModel, PropertyNames.PROPERTY_STEP, new Integer(nNewStep));
390         setCurrentRoadmapItemID((short) (nNewStep));
391         enableNextButton(getNextAvailableStep() > 0);
392         enableBackButton(nNewStep != 1);
393     }
394 
395 
396     /* (non-Javadoc)
397      * @see com.sun.star.wizards.ui.XCompletion#iscompleted(int)
398      */
399     /**
400      *
401      * @param _ndialogpage
402      * @return
403      */
404     public boolean iscompleted(int _ndialogpage)
405     {
406         return false;
407     }
408     /* (non-Javadoc)
409      * @see com.sun.star.wizards.ui.XCompletion#ismodified(int)
410      */
411 
412     /**
413      *
414      * @param _ndialogpage
415      * @return
416      */
417     public boolean ismodified(int _ndialogpage)
418     {
419         return false;
420     }
421     /* (non-Javadoc)
422      * @see com.sun.star.wizards.ui.XCompletion#setcompleted(int, boolean)
423      */
424 
425     /**
426      *
427      * @param _ndialogpage
428      * @param _biscompleted
429      */
430     public void setcompleted(int _ndialogpage, boolean _biscompleted)
431     {
432     }
433     /* (non-Javadoc)
434      * @see com.sun.star.wizards.ui.XCompletion#setmodified(int, java.lang.Object, java.lang.Object)
435      */
436 
437     /**
438      *
439      * @param _ndialogpage
440      * @param ooldValue
441      * @param onewValue
442      */
443     public void setmodified(int _ndialogpage, Object ooldValue, Object onewValue)
444     {
445     }
446 
447     public void drawNaviBar()
448     {
449 
450         try
451         {
452             short curtabindex = UIConsts.SOFIRSTWIZARDNAVITABINDEX;
453             Integer IButtonWidth = new Integer(iButtonWidth);
454             int iButtonHeight = 14;
455             Integer IButtonHeight = new Integer(iButtonHeight);
456             Integer ICurStep = 0;
457             int iDialogHeight = ((Integer) Helper.getUnoPropertyValue(this.xDialogModel, PropertyNames.PROPERTY_HEIGHT)).intValue();
458             int iDialogWidth = ((Integer) Helper.getUnoPropertyValue(this.xDialogModel, PropertyNames.PROPERTY_WIDTH)).intValue();
459             int iHelpPosX = 8;
460             int iBtnPosY = iDialogHeight - iButtonHeight - 6;
461             int iCancelPosX = iDialogWidth - iButtonWidth - 6;
462             int iFinishPosX = iCancelPosX - 6 - iButtonWidth;
463             int iNextPosX = iFinishPosX - 6 - iButtonWidth;
464             int iBackPosX = iNextPosX - 3 - iButtonWidth;
465 
466             insertControlModel("com.sun.star.awt.UnoControlFixedLineModel", "lnNaviSep",
467                     new String[]
468                     {
469                         PropertyNames.PROPERTY_HEIGHT, PropertyNames.ORIENTATION, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_WIDTH
470                     },
471                     new Object[]
472                     {
473                         1, 0, 0, new Integer(iDialogHeight - 26), ICurStep, new Integer(iDialogWidth)
474                     });
475 
476             insertControlModel("com.sun.star.awt.UnoControlFixedLineModel", "lnRoadSep",
477                     new String[]
478                     {
479                         PropertyNames.PROPERTY_HEIGHT, PropertyNames.ORIENTATION, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_WIDTH
480                     },
481                     new Object[]
482                     {
483                         new Integer(iBtnPosY - 6), 1, 85, 0, ICurStep, 1
484                     });
485 
486             String[] propNames = new String[]
487             {
488                 PropertyNames.PROPERTY_ENABLED, PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_HELPURL, PropertyNames.PROPERTY_LABEL, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, "PushButtonType", PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_TABINDEX, PropertyNames.PROPERTY_WIDTH
489             };
490 
491             Helper.setUnoPropertyValue(super.xDialogModel, PropertyNames.PROPERTY_HELPURL, HelpIds.getHelpIdString(hid));
492             insertButton("btnWizardHelp", HELP_ACTION_PERFORMED, new String[]
493                     {
494                         PropertyNames.PROPERTY_ENABLED, PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_LABEL, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, "PushButtonType", PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_TABINDEX, PropertyNames.PROPERTY_WIDTH
495                     },
496                     new Object[]
497                     {
498                         true, IButtonHeight, oWizardResource.getResText(UIConsts.RID_COMMON + 15), new Integer(iHelpPosX), new Integer(iBtnPosY), new Short((short) PushButtonType.HELP_value), ICurStep, new Short(curtabindex++), IButtonWidth
499                     });
500             insertButton("btnWizardBack", BACK_ACTION_PERFORMED, propNames,
501                     new Object[]
502                     {
503                         false, IButtonHeight, HelpIds.getHelpIdString(hid + 2), oWizardResource.getResText(UIConsts.RID_COMMON + 13), new Integer(iBackPosX), new Integer(iBtnPosY), new Short((short) PushButtonType.STANDARD_value), ICurStep, new Short(curtabindex++), IButtonWidth
504                     });
505 
506             insertButton("btnWizardNext", NEXT_ACTION_PERFORMED, propNames,
507                     new Object[]
508                     {
509                         true, IButtonHeight, HelpIds.getHelpIdString(hid + 3), oWizardResource.getResText(UIConsts.RID_COMMON + 14), new Integer(iNextPosX), new Integer(iBtnPosY), new Short((short) PushButtonType.STANDARD_value), ICurStep, new Short(curtabindex++), IButtonWidth
510                     });
511 
512             insertButton("btnWizardFinish", FINISH_ACTION_PERFORMED, propNames,
513                     new Object[]
514                     {
515                         true, IButtonHeight, HelpIds.getHelpIdString(hid + 4), oWizardResource.getResText(UIConsts.RID_COMMON + 12), new Integer(iFinishPosX), new Integer(iBtnPosY), new Short((short) PushButtonType.STANDARD_value), ICurStep, new Short(curtabindex++), IButtonWidth
516                     });
517 
518             insertButton("btnWizardCancel", CANCEL_ACTION_PERFORMED, propNames,
519                     new Object[]
520                     {
521                         true, IButtonHeight, HelpIds.getHelpIdString(hid + 5), oWizardResource.getResText(UIConsts.RID_COMMON + 11), new Integer(iCancelPosX), new Integer(iBtnPosY), new Short((short) PushButtonType.STANDARD_value), ICurStep, new Short(curtabindex++), IButtonWidth
522                     });
523 
524             setControlProperty("btnWizardNext", "DefaultButton", Boolean.TRUE);
525             // add a window listener, to know
526             // if the user used "escape" key to
527             // close the dialog.
528             MethodInvocation windowHidden = new MethodInvocation("windowHidden", this);
529             xWindow.addWindowListener((CommonListener) getGuiEventListener());
530             String dialogName = (String) Helper.getUnoPropertyValue(xDialogModel, PropertyNames.PROPERTY_NAME);
531             getGuiEventListener().add(dialogName, EVENT_ACTION_PERFORMED, windowHidden);
532 
533         }
534         catch (java.lang.Exception jexception)
535         {
536             jexception.printStackTrace(System.out);
537         }
538     }
539 
540     protected void insertRoadMapItems(String[] items, int[] steps, boolean[] enabled)
541     {
542         for (int i = 0; i < items.length; i++)
543         {
544             insertRoadmapItem(i, enabled[i], items[i], steps[i]);
545         }
546     }
547 
548     /** This method also enables and disables the "next" button,
549      * if the step currently dis/enabled is the one of the next steps.
550      * @param _nStep
551      * @param bEnabled
552      * @param enableNextButton
553      */
554     public void setStepEnabled(int _nStep, boolean bEnabled, boolean enableNextButton)
555     {
556         setStepEnabled(_nStep, bEnabled);
557         if (getNextAvailableStep() > 0)
558         {
559             enableNextButton(bEnabled);
560         }
561     }
562 
563     public void enableNavigationButtons(boolean _bEnableBack, boolean _bEnableNext, boolean _bEnableFinish)
564     {
565         enableBackButton(_bEnableBack);
566         enableNextButton(_bEnableNext);
567         enableFinishButton(_bEnableFinish);
568     }
569 
570     public void enableBackButton(boolean enabled)
571     {
572         setControlProperty("btnWizardBack", PropertyNames.PROPERTY_ENABLED, enabled ? Boolean.TRUE : Boolean.FALSE);
573     }
574 
575     public void enableNextButton(boolean enabled)
576     {
577         setControlProperty("btnWizardNext", PropertyNames.PROPERTY_ENABLED, enabled ? Boolean.TRUE : Boolean.FALSE);
578     }
579 
580     public void enableFinishButton(boolean enabled)
581     {
582         setControlProperty("btnWizardFinish", PropertyNames.PROPERTY_ENABLED, enabled ? Boolean.TRUE : Boolean.FALSE);
583     }
584 
585     public void setStepEnabled(int _nStep, boolean bEnabled)
586     {
587         XInterface xRoadmapItem = getRoadmapItemByID(_nStep);
588         if (xRoadmapItem != null)
589         {
590             Helper.setUnoPropertyValue(xRoadmapItem, PropertyNames.PROPERTY_ENABLED, Boolean.valueOf(bEnabled));
591         }
592     }
593 
594     public void enablefromStep(int _iStep, boolean _bDoEnable)
595     {
596         if (_iStep <= this.nMaxStep)
597         {
598             for (int i = _iStep; i <= nMaxStep; i++)
599             {
600                 setStepEnabled(i, _bDoEnable);
601             }
602             enableFinishButton(_bDoEnable);
603             if (!_bDoEnable)
604             {
605                 enableNextButton(_iStep > getCurrentStep() + 1);
606             }
607             else
608             {
609                 enableNextButton(!(getCurrentStep() == nMaxStep));
610             }
611         }
612     }
613 
614     public boolean isStepEnabled(int _nStep)
615     {
616         try
617         {
618             boolean bIsEnabled;
619             XInterface xRoadmapItem = getRoadmapItemByID(_nStep);
620             if (xRoadmapItem == null)
621             // Todo: In this case an exception should be thrown
622             {
623                 return false;
624             }
625             bIsEnabled = AnyConverter.toBoolean(Helper.getUnoPropertyValue(xRoadmapItem, PropertyNames.PROPERTY_ENABLED));
626             return bIsEnabled;
627         }
628         catch (com.sun.star.uno.Exception exception)
629         {
630             exception.printStackTrace(System.out);
631             return false;
632         }
633     }
634 
635     public synchronized void gotoPreviousAvailableStep()
636     {
637         boolean bIsEnabled;
638         if (nNewStep > 1)
639         {
640             nOldStep = nNewStep;
641             nNewStep--;
642             while (nNewStep > 0)
643             {
644                 bIsEnabled = isStepEnabled(nNewStep);
645                 if (bIsEnabled)
646                 {
647                     break;
648                 }
649                 nNewStep--;
650             }
651             if (nNewStep == 0) // Exception???
652             {
653                 nNewStep = nOldStep;
654             }
655             switchToStep();
656         }
657     }
658 
659     //TODO discuss with rp
660     protected int getNextAvailableStep()
661     {
662         if (isRoadmapComplete())
663         {
664             for (int i = nNewStep + 1; i <= nMaxStep; i++)
665             {
666                 if (isStepEnabled(i))
667                 {
668                     return i;
669                 }
670             }
671         }
672         return -1;
673     }
674 
675     public synchronized void gotoNextAvailableStep()
676     {
677         nOldStep = nNewStep;
678         nNewStep = getNextAvailableStep();
679         if (nNewStep > -1)
680         {
681             switchToStep();
682         }
683     }
684 
685     public abstract boolean finishWizard();
686 
687     /**
688      * This function will call if the finish button is pressed on the UI.
689      */
690     public void finishWizard_1()
691     {
692         enableFinishButton(false);
693         boolean success = false;
694         try
695         {
696             success = finishWizard();
697         }
698         finally
699         {
700             if ( !success )
701                 enableFinishButton( true );
702         }
703         if ( success )
704             removeTerminateListener();
705     }
706 
707     public int getMaximalStep()
708     {
709         return this.nMaxStep;
710     }
711 
712     public int getCurrentStep()
713     {
714         try
715         {
716             return AnyConverter.toInt(Helper.getUnoPropertyValue(this.MSFDialogModel, PropertyNames.PROPERTY_STEP));
717         }
718         catch (com.sun.star.uno.Exception exception)
719         {
720             exception.printStackTrace(System.out);
721             return -1;
722         }
723     }
724 
725     public void setCurrentStep(int _nNewstep)
726     {
727         nNewStep = _nNewstep;
728         changeToStep(nNewStep);
729     }
730 
731     public void setRightPaneHeaders(Resource _oResource, int StartResID, int _nMaxStep)
732     {
733         String[] sRightPaneHeaders = _oResource.getResArray(StartResID, _nMaxStep);
734         setRightPaneHeaders(sRightPaneHeaders);
735     }
736 
737     public void setRightPaneHeaders(String[] _sRightPaneHeaders)
738     {
739         this.nMaxStep = _sRightPaneHeaders.length;
740         this.sRightPaneHeaders = _sRightPaneHeaders;
741         FontDescriptor oFontDesc = new FontDescriptor();
742         oFontDesc.Weight = com.sun.star.awt.FontWeight.BOLD;
743 
744         for (int i = 0; i < sRightPaneHeaders.length; i++)
745         {
746             insertLabel("lblQueryTitle" + String.valueOf(i),
747                     new String[]
748                     {
749                         PropertyNames.FONT_DESCRIPTOR, PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_LABEL, PropertyNames.PROPERTY_MULTILINE, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_TABINDEX, PropertyNames.PROPERTY_WIDTH
750                     },
751                     new Object[]
752                     {
753                         oFontDesc, 16, sRightPaneHeaders[i], Boolean.TRUE, 91, 8, new Integer(i + 1), new Short((short) 12), 212
754                     });
755         }
756     }
757 
758     public void cancelWizard()
759     {
760         //can be overwritten by extending class
761         xDialog.endExecute();
762     }
763 
764     public void callHelp()
765     {
766         //should be overwritten by extending class
767     }
768 
769     public void removeTerminateListener()
770     {
771         if (bTerminateListenermustberemoved)
772         {
773             Desktop.getDesktop(xMSF).removeTerminateListener(this);
774             bTerminateListenermustberemoved = false;
775         }
776     }
777 
778     /**
779      * called by the cancel button and
780      * by the window hidden event.
781      * if this method was not called before,
782      * perform a cancel.
783      */
784     public void cancelWizard_1()
785     {
786         cancelWizard();
787         removeTerminateListener();
788     }
789 
790     public void windowHidden()
791     {
792         cancelWizard_1();
793     }
794 
795     public void notifyTermination(EventObject arg0)
796     {
797         cancelWizard_1();
798     }
799 
800     public void queryTermination(EventObject arg0)
801             throws TerminationVetoException
802     {
803         activate();
804         throw new TerminationVetoException();
805     }
806 
807     public void disposing(EventObject arg0)
808     {
809         cancelWizard_1();
810     }
811 }
812