xref: /trunk/main/wizards/com/sun/star/wizards/web/WWD_Startup.java (revision 565bea5d874efc40bc68a4981c2c2749fbc88f2d)
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 package com.sun.star.wizards.web;
23 
24 //import com.sun.star.awt.ItemEvent;
25 //import com.sun.star.awt.XItemListener;
26 import java.io.FileNotFoundException;
27 import java.util.Comparator;
28 import java.util.List;
29 import java.util.Vector;
30 
31 import com.sun.star.awt.VclWindowPeerAttribute;
32 import com.sun.star.awt.XControl;
33 import com.sun.star.awt.XItemListener;
34 import com.sun.star.awt.XWindow;
35 import com.sun.star.awt.XWindowPeer;
36 import com.sun.star.frame.XDesktop;
37 import com.sun.star.frame.XFrame;
38 import com.sun.star.frame.XModel;
39 import com.sun.star.lang.EventObject;
40 import com.sun.star.lang.XMultiServiceFactory;
41 import com.sun.star.uno.UnoRuntime;
42 import com.sun.star.wizards.common.ConfigSet;
43 import com.sun.star.wizards.common.Configuration;
44 import com.sun.star.wizards.common.Desktop;
45 import com.sun.star.wizards.common.FileAccess;
46 import com.sun.star.wizards.common.Helper;
47 import com.sun.star.wizards.common.JavaTools;
48 import com.sun.star.wizards.common.PropertyNames;
49 import com.sun.star.wizards.common.SystemDialog;
50 import com.sun.star.wizards.document.OfficeDocument;
51 import com.sun.star.wizards.ui.DocumentPreview;
52 import com.sun.star.wizards.ui.event.DataAware;
53 import com.sun.star.wizards.ui.event.ListModelBinder;
54 import com.sun.star.wizards.ui.event.RadioDataAware;
55 import com.sun.star.wizards.ui.event.SimpleDataAware;
56 import com.sun.star.wizards.ui.event.Task;
57 import com.sun.star.wizards.ui.event.UnoDataAware;
58 import com.sun.star.wizards.web.data.CGContent;
59 import com.sun.star.wizards.web.data.CGDocument;
60 import com.sun.star.wizards.web.data.CGIconSet;
61 import com.sun.star.wizards.web.data.CGPublish;
62 import com.sun.star.wizards.web.data.CGSession;
63 import com.sun.star.wizards.web.data.CGSessionName;
64 import com.sun.star.wizards.web.data.CGSettings;
65 import com.sun.star.wizards.web.data.CGStyle;
66 
67 /**
68  * Web Wizard Dialog implementation : Startup.
69  * This class contains the startup implementation and session mounting methods.<p/>
70  * <h2> Data Aware Concept </h2>
71  * <p> I use DataAware objects, which synchronize
72  * UI and Data. A DataAware object a kind of interface
73  * between a Value and a UI element.
74  * </P>
75  * <p>
76  * A Value is either a JavaBean property with
77  * a Getter and a Setter or a public class Member.
78  * </P>
79  * When the UI Control changes, the Value changes correspondingly.
80  * This depends on settings a Listener which calls the updateData()
81  * method od the DataAware object.
82  * When the Value changes, the UI Control changes respopndingly.
83  * This also depends on settings a Listener which calls the updateUI()
84  * method of the DataAware object.
85  * </P>
86  * <P>
87  * here I use only UI Listeners, and keep calling
88  * the updateUI() method whenever I change the Value.
89  * </P>
90  * To contain the Data, I use the Configuration Data Objects
91  * which read themselves out of the Configuration. they are all located under
92  * the data package.
93  * <p/>
94  * Different groups of DataAware objects are grouped into vectors.
95  * Each group (all elements of a vector) uses the same DataObject,
96  * so when I change the underlying dataObject, I change the whole vector's DataObject,
97  * thus actualizing UI.<br/>
98  * This is what happens when a session loads: I Load the session into
99  * a tree of DataObjects, and change the DataAware's DataObject. <br/>
100  * One could also look at this as a kind of "View-Model" relationship.
101  * The controls are the View, The DataObjects are the Model, containing the
102  * Data rad directly from the configuration, and the DataAware objects
103  * are the controller in between.
104  *
105  * @author rpiterman
106  *
107  */
108 public abstract class WWD_Startup extends WWD_General
109 {
110 
111     SimpleDataAware sda = null;
112     /**
113      * This is currently not used.
114      * It should have been a Listener which invokes
115      * an Update to the Preview-Document, only
116      * We did not implement the live-preview document,
117      * so this became obsolete.
118      */
119     protected DataAware.Listener refresh;
120     /**
121      * Is called whenever a field is changed which
122      * could affect the "create" button enable state,
123      * like publishing-checkboxes, save-session check- and textbox.
124      */
125     protected DataAware.Listener checkPublish = new CheckPublish();
126     /**
127      * The Vector containing DataAware objects
128      * which relay on the selected document
129      * as DataObject (or Model).
130      */
131     protected List docAware = new Vector();
132     /**
133      * The Vector containing DataAware objects
134      * which relay on the session's generalInfo
135      * Object (CGSession.cp_GeneralInfo) as
136      * DataObject (or model).
137      */
138     protected List genAware = new Vector();
139     /**
140      * The vector containing DataAware objectscm25npd ..
141      * which relay on the session's Design Object
142      * (CGSession.cp_Design) as DataObject
143      * (or model).
144      *
145      */
146     protected List designAware = new Vector();
147     /**
148      * A Vector containing the DataAware objects
149      * which relay on Publishing Objects.
150      * Differently from other Vectors, each
151      * DataAware object in this Vector relays
152      * on a <b>different</b> CGPublish object,
153      * So they are handled with more care.
154      */
155     protected List pubAware = new Vector(3);
156     /**
157      * The DataAware object which handles
158      * the documents-list events.
159      * This will call the set/getDocument() method
160      * implemented in WWD_General.
161      */
162     protected UnoDataAware docListDA;
163     /**
164      * DataAware object which synchronizes the session
165      * name in step 7 with the CGSession.cp_Name
166      */
167     protected UnoDataAware sessionNameDA;
168     /**
169      * Binds a ListModel to the UnoControlListBox.
170      */
171     protected ListModelBinder docsBinder;
172     /**
173      *  a DocumentPreview object is
174      * the UI OO Document preview
175      * in the Dialog
176      */
177     protected DocumentPreview dpStylePreview;
178     /**
179      * An Object that implements the
180      * File-copy and refresh of the
181      * style preview
182      */
183     protected StylePreview stylePreview;
184     /**
185      * the currently selected document.
186      */
187     protected short[] selectedDoc = new short[0];
188     /**
189      * If ftp proxies are on, ftp is disabled, and
190      * the true/false of the FTP publisher is set to false.
191      * In order to save it correctly when saving the session
192      * at the end, the original loaded value is saved to this variable;
193      */
194     boolean __ftp;
195     /**
196      * When the wizard starts, a new document opens.
197      * The backgroundDoc member contains the TextDocument
198      * instance used for that purpose.
199      */
200     protected XFrame myFrame;
201 
202 
203     /* ******************************************
204      *  ****************************************
205      *             General Methods
206      *  ****************************************
207      * ******************************************  */
208 
209     /*
210      * GENERAL Initialization  methods
211      */
212     /**
213      * He - my constructor !
214      * I call/do here in this order: <br/>
215      * Check if ftp http proxy is set, and warn user he can not use
216      * ftp if it is.<br/>
217      * draw NaviBar and steps 1 to 7, incl. Step X,
218      * which is the Layouts ImageList on step 3.<br/>
219      * load the settings from the configuration. <br/>
220      * set the default save session name to the first
221      * available one (sessionX or something like that). <br/>
222      * set the Layouts ImageList ListModel, and call
223      * its "create()" method. <br/>
224      * check the content (documents) specified in the default session.<br/>
225      * fill the list boxes (saved sessions, styles, combobox save session). <br/>
226      * make data aware. <br/>
227      * updateUI (refreshes the display to correspond to the data in
228      * the default session.)<br/>
229      * fill the documents listbox. <br/>
230      * if proxies are set, disable the ftp controls in step 7.
231      */
WWD_Startup(XMultiServiceFactory xmsf)232     public WWD_Startup(XMultiServiceFactory xmsf) throws Exception
233     {
234         super(xmsf);
235 
236         proxies = getOOProxies();
237 
238         String soTemplateDir = FileAccess.getOfficePath(xmsf, "Template", "share", "/wizard");
239 
240         String exclamationURL = FileAccess.connectURLs(soTemplateDir, "../wizard/bitmap/caution_16.png");
241         this.drawNaviBar();
242         this.buildStep1();
243         this.buildStep2();
244         this.buildStep3();
245 
246         this.buildStep4();
247         this.buildStep5();
248         this.buildStep6();
249         this.buildStep7(proxies, exclamationURL);
250         buildStepX();
251 
252         this.xMSF = xmsf;
253         XDesktop xDesktop = Desktop.getDesktop(xMSF);
254         myFrame = OfficeDocument.createNewFrame(xMSF, this);
255         Object doc = OfficeDocument.createNewDocument(myFrame, "swriter", false, true);
256 
257         loadSettings(doc);
258         setSaveSessionName(settings.cp_DefaultSession);
259 
260         ilLayouts.setListModel(settings.cp_Layouts);
261         ilLayouts.create(this);
262 
263         checkContent(settings.cp_DefaultSession.cp_Content, new Task(PropertyNames.EMPTY_STRING, PropertyNames.EMPTY_STRING, 99999), this.xControl);
264 
265         //saved sessions, styles, combobox save session.
266         // also set the chosen saved session...
267         fillLists();
268         makeDataAware();
269         // change the display to correspond to the current session.
270         updateUI();
271         // fill the documents listbox.
272         mount(settings.cp_DefaultSession.cp_Content);
273 
274 
275         if (proxies)
276         {
277             setEnabled(btnFTP, false);
278             setEnabled(chkFTP, false);
279         }
280     }
281 
282     /**
283      * return true if http proxies or other proxies
284      * which do not enable ftp to function correctly are set.
285      * @return true if (http) proxies are on.
286      * @throws Exception
287      */
getOOProxies()288     private boolean getOOProxies() throws Exception
289     {
290         Object node = Configuration.getConfigurationRoot(xMSF, "org.openoffice.Inet/Settings", false);
291         int i = Configuration.getInt("ooInetProxyType", node);
292         //System.out.println("WWD:Startup:getOOProxies:" + i);
293         switch (i)
294         {
295             case 0: //no proxies
296                 return false;
297             case 2: //http proxies
298                 return true;
299             default:
300                 return true;
301         }
302     }
303 
304     /**
305      * calculates the first available session name,
306      * and sets the current session name to it.
307      * The combobox text in step 7 will be updated
308      * automatically when updateUI() is called.
309      */
setSaveSessionName(CGSession session)310     protected void setSaveSessionName(CGSession session)
311     {
312         int max = 0;
313         int len = resources.resSessionName.length();
314         // traverse between the sessions and find the one that
315         // has the biggest number.
316         for (int i = 0; i < settings.cp_SavedSessions.getSize(); i++)
317         {
318             String sessionName = ((CGSessionName) settings.cp_SavedSessions.getElementAt(i)).cp_Name;
319             if (sessionName.startsWith(resources.resSessionName))
320             {
321                 max = max(max, Integer.valueOf(sessionName.substring(len)).intValue());
322             }
323         }
324 
325         session.cp_Name = resources.resSessionName + ++max;
326 
327     }
328 
max(int i1, int i2)329     private int max(int i1, int i2)
330     {
331         if (i1 > i2)
332         {
333             return i1;
334         }
335         else
336         {
337             return i2;
338         }
339     }
340 
341     /**
342      * fills the road map, and sets the necessary properties,
343      * like MaxStep, Complete, Interactive-
344      * Disables the finishButton.
345      */
addRoadMapItems()346     private void addRoadMapItems()
347     {
348         insertRoadMapItems(new String[]
349                 {
350                     resources.resStep1, resources.resStep2, resources.resStep3, resources.resStep4, resources.resStep5, resources.resStep6, resources.resStep7
351                 }, new int[]
352                 {
353                     1, 2, 3, 4, 5, 6, 7
354                 }, new boolean[]
355                 {
356                     true, true, false, false, false, false, false
357                 });
358 
359         setRoadmapInteractive(true);
360         setRoadmapComplete(true);
361         setCurrentRoadmapItemID((short) 1);
362         setMaxStep(7);
363         enableFinishButton(false);
364 
365     }
366 
367     /**
368      * This method goes through all
369      * the DataAware vectors and objects and
370      * calls their updateUI() method.
371      * In response, the UI is synchronized to
372      * the state of the corresponding data objects.
373      *
374      */
updateUI()375     private void updateUI()
376     {
377         DataAware.updateUI(designAware);
378         DataAware.updateUI(genAware);
379         DataAware.updateUI(pubAware);
380         sessionNameDA.updateUI();
381         checkPublish();
382     }
383 
getFrame(Object model)384     private XFrame getFrame(Object model)
385     {
386         XModel xmodel = UnoRuntime.queryInterface(XModel.class, model);
387         return xmodel.getCurrentController().getFrame();
388     }
389 
390     /**
391      * create the peer, add roadmap,
392      * add roadmap items, add style-preview,
393      * disable the steps 3 to 7 if no documents are
394      * on the list, and... show the dialog!
395      */
show()396     public void show()
397     {
398         try
399         {
400 
401 
402             /* myFrame.initialize(docWindow);
403              * */
404 
405             //desktopFrame = Desktop.findAFrame(xMSF, myFrame, desktopFrame);
406 
407             //XWindow xContainerWindow = myFrame.getContainerWindow();
408 
409             XWindow xContainerWindow = myFrame.getComponentWindow();
410             XWindowPeer xWindowPeer = UnoRuntime.queryInterface(XWindowPeer.class, xContainerWindow);
411 
412             createWindowPeer(xWindowPeer);
413 
414             addRoadmap();
415             addRoadMapItems();
416             addStylePreview();
417             checkSteps();
418 
419             executeDialog(myFrame);
420             removeTerminateListener();
421 
422         }
423         catch (java.lang.Exception jexception)
424         {
425             jexception.printStackTrace(System.out);
426         }
427     }
428 
429     /**
430      * initializes the style preview.
431      */
addStylePreview()432     private void addStylePreview()
433     {
434         try
435         {
436             dpStylePreview = new DocumentPreview(xMSF, imgPreview);
437             stylePreview = new StylePreview(xMSF, settings.workPath);
438             stylePreview.refresh(settings.cp_DefaultSession.getStyle(), settings.cp_DefaultSession.cp_Design.cp_BackgroundImage);
439             dpStylePreview.setDocument(stylePreview.htmlFilename, DocumentPreview.PREVIEW_MODE);
440 
441         }
442         catch (Exception ex)
443         {
444             ex.printStackTrace();
445         }
446     }
447 
448     /**
449      * Loads the web wizard settings from the registry.
450      */
loadSettings(Object document)451     private void loadSettings(Object document)
452     {
453         try
454         {
455             // instantiate
456             String[] settingsResources = new String[]
457             {
458                 resources.resPages,
459                 resources.resSlides,
460                 resources.resCreatedTemplate,
461                 resources.resUpdatedTemplate,
462                 resources.resSizeTemplate
463             };
464 
465             settings = new CGSettings(xMSF, settingsResources, document);
466 
467             // get configuration view
468             Object confRoot = Configuration.getConfigurationRoot(xMSF, CONFIG_PATH, false);
469             // read
470             settings.readConfiguration(confRoot, CONFIG_READ_PARAM);
471 
472             ConfigSet set = settings.cp_DefaultSession.cp_Publishing;
473 
474             // now if path variables are used in publisher paths, they
475             // are getting replaced here...
476             for (int i = 0; i < set.getSize(); i++)
477             {
478                 CGPublish p = (CGPublish) set.getElementAt(i);
479                 p.cp_URL = substitute(p.cp_URL);
480             }
481 
482             // initialize the settings.
483             settings.configure(xMSF);
484 
485             // set resource needed for web page.
486 
487             // sort the styles alphabetically
488             settings.cp_Styles.sort(new StylesComparator());
489 
490             prepareSessionLists();
491 
492             if (proxies)
493             {
494                 __ftp = getPublisher(FTP_PUBLISHER).cp_Publish;
495                 getPublisher(FTP_PUBLISHER).cp_Publish = false;
496 
497             }
498 
499         }
500         catch (Exception ex)
501         {
502             ex.printStackTrace();
503         }
504     }
505 
prepareSessionLists()506     protected void prepareSessionLists()
507     {
508         // now copy the sessions list...
509         Object[] sessions = settings.cp_SavedSessions.items();
510         settings.savedSessions.clear();
511         for (int i = 0; i < sessions.length; i++)
512         {
513             settings.savedSessions.add(i, sessions[i]); // add an empty session to the saved session list which appears in step 1
514         }
515         CGSessionName sn = new CGSessionName();
516         sn.cp_Name = resources.resSessionNameNone;
517         settings.cp_SavedSessions.add(0, sn);
518     }
519 
520     /**
521      * fills the saved session list, the styles list,
522      * and save session combo box.
523      * Also set the selected "load" session to the last session
524      * which was saved.
525      */
fillLists()526     private void fillLists()
527     {
528         // fill the saved session list.
529         ListModelBinder.fillList(lstLoadSettings, settings.cp_SavedSessions.items(), null);
530 
531         // set the selected session to load. (step 1)
532         selectSession();
533 
534         // fill the styles list.
535         ListModelBinder.fillList(lstStyles, settings.cp_Styles.items(), null);
536 
537         // fill the save session combobox (step 7)
538         ListModelBinder.fillComboBox(cbSaveSettings, settings.savedSessions.items(), null);
539 
540 
541     }
542 
selectSession()543     protected void selectSession()
544     {
545         int selectedSession = 0;
546         if (settings.cp_LastSavedSession != null && !settings.cp_LastSavedSession.equals(PropertyNames.EMPTY_STRING))
547         {
548 
549             Object ses = settings.cp_SavedSessions.getElement(settings.cp_LastSavedSession);
550             if (ses != null)
551             {
552                 selectedSession = settings.cp_SavedSessions.getIndexOf(ses);
553             }
554         }
555         Helper.setUnoPropertyValue(getModel(lstLoadSettings), PropertyNames.SELECTED_ITEMS, new short[]
556                 {
557                     (short) selectedSession
558                 });
559 
560     }
561 
562     public class SimpleDataawareUpdater implements XItemListener
563     {
564         /* (non-Javadoc)
565          * @see com.sun.star.lang.XEventListener#disposing(com.sun.star.lang.EventObject)
566          */
567 
disposing(EventObject arg0)568         public void disposing(EventObject arg0)
569         {
570             // TODO Auto-generated method stub
571         }
572 
itemStateChanged(com.sun.star.awt.ItemEvent itemEvent)573         public void itemStateChanged(com.sun.star.awt.ItemEvent itemEvent)
574         {
575             sda.updateData();
576         //TODO xf uncomment
577         //refresh.eventPerformed(ie);
578         }
579     }
580 
581     /**
582      * attaches to each ui-data-control (like checkbox, groupbox or
583      * textbox, no buttons though), a DataObject's JavaBean Property,
584      * or class member.
585      */
makeDataAware()586     private void makeDataAware()
587     {
588         //page 1
589         new ListModelBinder(lstLoadSettings, settings.cp_SavedSessions);
590 
591         //page 2 : document properties
592 
593         docListDA = UnoDataAware.attachListBox(this, "SelectedDoc", lstDocuments, null, false);
594         docListDA.disableControls(new Object[]
595                 {
596                     /*btnDocDown, btnDocUp, */
597                     lnDocsInfo, btnRemoveDoc, lblDocTitle, txtDocTitle, lblDocInfo, txtDocInfo, lblDocAuthor, txtDocAuthor, lblDocExportFormat, lstDocTargetType
598                 });
599         docListDA.updateUI();
600 
601         CGDocument doc = new CGDocument(); //dummy
602 
603         docsBinder = new ListModelBinder(lstDocuments, settings.cp_DefaultSession.cp_Content.cp_Documents);
604 
605         docAware.add(UnoDataAware.attachEditControl(doc, "cp_Title", txtDocTitle, refresh, true));
606         docAware.add(UnoDataAware.attachEditControl(doc, "cp_Description", txtDocInfo, refresh, true));
607         docAware.add(UnoDataAware.attachEditControl(doc, "cp_Author", txtDocAuthor, refresh, true));
608         docAware.add(UnoDataAware.attachListBox(doc, "Exporter", lstDocTargetType, refresh, false));
609 
610         //page 3 : Layout
611         Object design = settings.cp_DefaultSession.cp_Design;
612         sda = new SimpleDataAware(design, new DataAware.PropertyValue("Layout", design), ilLayouts, new DataAware.PropertyValue("Selected", ilLayouts));
613         ilLayouts.addItemListener(new SimpleDataawareUpdater());
614         designAware.add(sda);
615 
616         //page 4 : layout 2
617         designAware.add(UnoDataAware.attachCheckBox(design, "cp_DisplayDescription", chbDocDesc, refresh, true));
618         designAware.add(UnoDataAware.attachCheckBox(design, "cp_DisplayAuthor", chbDocAuthor, refresh, true));
619         designAware.add(UnoDataAware.attachCheckBox(design, "cp_DisplayCreateDate", chkDocCreated, refresh, true));
620         designAware.add(UnoDataAware.attachCheckBox(design, "cp_DisplayUpdateDate", chkDocChanged, refresh, true));
621         designAware.add(UnoDataAware.attachCheckBox(design, "cp_DisplayFilename", chkDocFilename, refresh, true));
622         designAware.add(UnoDataAware.attachCheckBox(design, "cp_DisplayFileFormat", chkDocFormat, refresh, true));
623         designAware.add(UnoDataAware.attachCheckBox(design, "cp_DisplayFormatIcon", chkDocFormatIcon, refresh, true));
624         designAware.add(UnoDataAware.attachCheckBox(design, "cp_DisplayPages", chkDocPages, refresh, true));
625         designAware.add(UnoDataAware.attachCheckBox(design, "cp_DisplaySize", chkDocSize, refresh, true));
626         designAware.add(RadioDataAware.attachRadioButtons(settings.cp_DefaultSession.cp_Design, "cp_OptimizeDisplaySize", new Object[]
627                 {
628                     optOptimize640x480, optOptimize800x600, optOptimize1024x768
629                 }, refresh, true));
630 
631 
632         //page 5 : Style
633         /*
634          * note : on style change, i do not call here refresh ,but rather on
635          * a special method which will perform some display, background and Iconsets changes.
636          */
637         designAware.add(UnoDataAware.attachListBox(settings.cp_DefaultSession.cp_Design, "Style", lstStyles, null, false));
638 
639         //page 6 : site general props
640         genAware.add(UnoDataAware.attachEditControl(settings.cp_DefaultSession.cp_GeneralInfo, "cp_Title", txtSiteTitle, refresh, true));
641         genAware.add(UnoDataAware.attachEditControl(settings.cp_DefaultSession.cp_GeneralInfo, "cp_Description", txtSiteDesc, refresh, true));
642 
643         genAware.add(UnoDataAware.attachDateControl(settings.cp_DefaultSession.cp_GeneralInfo, "CreationDate", dateSiteCreated, refresh, false));
644         genAware.add(UnoDataAware.attachDateControl(settings.cp_DefaultSession.cp_GeneralInfo, "UpdateDate", dateSiteUpdate, refresh, false));
645         genAware.add(UnoDataAware.attachEditControl(settings.cp_DefaultSession.cp_GeneralInfo, "cp_Email", txtEmail, refresh, true));
646         genAware.add(UnoDataAware.attachEditControl(settings.cp_DefaultSession.cp_GeneralInfo, "cp_Copyright", txtCopyright, refresh, true));
647 
648         //page 7 : publishing
649 
650         pubAware(LOCAL_PUBLISHER, chkLocalDir, txtLocalDir, false);
651         pubAware(FTP_PUBLISHER, chkFTP, lblFTP, true);
652         pubAware(ZIP_PUBLISHER, chkZip, txtZip, false);
653 
654         sessionNameDA = UnoDataAware.attachEditControl(settings.cp_DefaultSession, "cp_Name", cbSaveSettings, null, true);
655 
656     //cleanup when exiting wizard.
657     //guiEventListener.add("WebWizardDialog",EventNames.EVENT_WINDOW_HIDDEN, "cleanup", this);
658     //xWindow.addWindowListener((XWindowListener)guiEventListener);
659     }
660 
661     /**
662      * A help method to attach a Checkbox and a TextBox to
663      * a CGPublish object properties/class members,
664      * @param publish
665      * @param checkbox
666      * @param textbox
667      */
pubAware(String publish, Object checkbox, Object textbox, boolean isLabel)668     private void pubAware(String publish, Object checkbox, Object textbox, boolean isLabel)
669     {
670         Object p = settings.cp_DefaultSession.cp_Publishing.getElement(publish);
671         UnoDataAware uda = UnoDataAware.attachCheckBox(p, "cp_Publish", checkbox, checkPublish, true);
672         uda.setInverse(true);
673 
674         uda.disableControls(new Object[]
675                 {
676                     textbox
677                 });
678         pubAware.add(uda);
679         pubAware.add(
680                 isLabel ? UnoDataAware.attachLabel(p, PropertyNames.URL, textbox, checkPublish, false)
681                 : UnoDataAware.attachEditControl(p, PropertyNames.URL, textbox, checkPublish, false));
682 
683     }
684 
685     /*
686      * Session load methods
687      * (are used both on the start of the wizard and
688      * when the user loads a session)
689      */
690     /**
691      * Is called when a new session/settings is
692      * loaded. <br/>
693      * Checks the documents (validate), fills the
694      * documents listbox, and changes the
695      * DataAware data objects to the
696      * new session's objects.<br/>
697      * Task advances 4 times in the mount method,
698      * and ??? times for each document in the session.
699      *
700      */
mount(CGSession session, Task task, boolean refreshStyle, XControl xC)701     protected void mount(CGSession session, Task task, boolean refreshStyle, XControl xC)
702     {
703         /* This checks the documents. If the user
704          * chooses to cancel, the session is not loaded.
705          */
706         checkContent(session.cp_Content, task, xC);
707 
708         settings.cp_DefaultSession = session;
709 
710         mount(session.cp_Content);
711 
712         task.advance(true);
713 
714         mount(session.cp_Design, designAware);
715         mount(session.cp_GeneralInfo, genAware);
716 
717         task.advance(true);
718 
719         mount(session.cp_Publishing.getElement(LOCAL_PUBLISHER), 0);
720         mount(session.cp_Publishing.getElement(FTP_PUBLISHER), 1);
721         mount(session.cp_Publishing.getElement(ZIP_PUBLISHER), 2);
722 
723         task.advance(true);
724 
725         sessionNameDA.setDataObject(session, true);
726         Helper.setUnoPropertyValue(getModel(chkSaveSettings), PropertyNames.PROPERTY_STATE, new Short((short) 1));
727         docListDA.updateUI();
728 
729         task.advance(true);
730 
731         if (refreshStyle)
732         {
733             refreshStylePreview();
734             updateIconsetText();
735         }
736 
737     //updateUI();
738     }
739 
740     /**
741      * used to mount the publishing objects. This is somehow cryptic -
742      * there are 6 DataAware objects, 2 for each Publishing Object (CGPublish).
743      * All 6 reside on one Vector (pubAware).
744      * Since, otherwise than other DataAware objects, different
745      * Objects on the same Vector need different DataObjectBinding,
746      * I use this method...
747      * @param data the CGPublish object
748      * @param i the number of the object (0 = local, 1 = ftp, 2 = zip)
749      */
mount(Object data, int i)750     private void mount(Object data, int i)
751     {
752         ((DataAware) pubAware.get(i * 2)).setDataObject(data, true);
753         ((DataAware) pubAware.get(i * 2 + 1)).setDataObject(data, true);
754     }
755 
756     /**
757      * Fills the documents listbox.
758      * @param root the CGContent object
759      * that contains the documents (a ListModel)
760      */
mount(CGContent root)761     private void mount(CGContent root)
762     {
763         ListModelBinder.fillList(lstDocuments, root.cp_Documents.items(), null);
764         docsBinder.setListModel(root.cp_Documents);
765         disableDocUpDown();
766 
767     }
768 
769     /**
770      * changes the DataAwares Objects' (in
771      * the given list) DataObject to the
772      * @param data
773      * @param list
774      */
mount(Object data, List list)775     protected void mount(Object data, List list)
776     {
777         for (int i = 0; i < list.size(); i++)
778         {
779             ((DataAware) list.get(i)).setDataObject(data, true);
780         }
781     }
782 
783     /**
784      * Checks if the document specified by the given CGDocument
785      * object (cp_URL) exists. Calls also the validate(...) method
786      * of this document.
787      * If the document does not exist, it asks the user whether
788      * to specify a new URL, or forget about this document.
789      * @param doc the document to check.
790      * @param task while loading a session, a status is
791      * displayed, using the Task object to monitor progress.
792      * @return true if the document is ok (a file exists in the given url).
793      */
checkDocument(CGDocument doc, Task task, XControl xC)794     protected boolean checkDocument(CGDocument doc, Task task, XControl xC)
795     {
796         try
797         {
798             doc.validate(xMSF, task);
799             return true;
800         }
801         catch (FileNotFoundException ex)
802         {
803 
804             int relocate = SystemDialog.showMessageBox(xMSF, xC.getPeer(), "WarningBox", VclWindowPeerAttribute.YES_NO + VclWindowPeerAttribute.DEF_NO, getFileAccess().getPath(doc.cp_URL, PropertyNames.EMPTY_STRING) + "\n\n" + resources.resSpecifyNewFileLocation);
805 
806             if (relocate == 2)
807             {
808                 String[] file = getDocAddDialog().callOpenDialog(false, FileAccess.getParentDir(doc.cp_URL));
809                 if (file == null)
810                 {
811                     return false;
812                 }
813                 else
814                 {
815                     doc.cp_URL = file[0];
816                     return checkDocument(doc, task, xC);
817                 }
818             }
819             else
820             {
821                 return false;
822             }
823         }
824         catch (IllegalArgumentException iaex)
825         {
826             //file is a directory
827             AbstractErrorHandler.showMessage(xMSF, xControl.getPeer(),
828                     JavaTools.replaceSubString(resources.resErrIsDirectory,
829                     getFileAccess().getPath(doc.cp_URL, PropertyNames.EMPTY_STRING),
830                     "%FILENAME"),
831                     ErrorHandler.ERROR_PROCESS_FATAL);
832             return false;
833         }
834         catch (Exception exp)
835         {
836             //something went wrong.
837             exp.printStackTrace();
838             AbstractErrorHandler.showMessage(xMSF, xControl.getPeer(),
839                     JavaTools.replaceSubString(resources.resErrDocValidate,
840                     getFileAccess().getPath(doc.cp_URL, PropertyNames.EMPTY_STRING),
841                     "%FILENAME"), ErrorHandler.ERROR_PROCESS_FATAL);
842             return false;
843         }
844 
845     }
846 
847     /**
848      * Checks the documents contained in this content.
849      * @param content
850      * @param task
851      */
checkContent(CGContent content, Task task, XControl xC)852     private void checkContent(CGContent content, Task task, XControl xC)
853     {
854         for (int i = 0; i < content.cp_Documents.getSize(); i++)
855         {
856             if (!checkDocument((CGDocument) content.cp_Documents.getElementAt(i), task, xC))
857             // I use here 'i--' since, when the document is removed
858             // an index change occurs
859             {
860                 content.cp_Documents.remove(i--);
861             /*for (Iterator i = content.cp_Contents.childrenMap.values().iterator(); i.hasNext();)
862             checkContent((CGContent)i.next(),task);*/
863             }
864         }
865     }
866 
867     /**
868      * A Listener which is called whenever
869      * a Publish checkbox/textbox state changes, and
870      * changes the "create" button enable state accordingly.
871      * @author rpiterman
872      */
873     private class CheckPublish implements DataAware.Listener
874     {
875 
eventPerformed(Object event)876         public void eventPerformed(Object event)
877         {
878             checkPublish();
879         }
880     }
881 
882     /**
883      * Disables/enables the docUpDown buttons (step 2)
884      * according to the currently selected document
885      * (no doc selected - both disabled, last doc selected,
886      * down disabled and so on...)
887      *
888      */
disableDocUpDown()889     protected void disableDocUpDown()
890     {
891         try
892         {
893             setEnabled(btnDocUp, selectedDoc.length == 0 ? Boolean.FALSE : (selectedDoc[0] == 0 ? Boolean.FALSE : Boolean.TRUE));
894             setEnabled(btnDocDown, selectedDoc.length == 0 ? Boolean.FALSE : (selectedDoc[0] + 1 < settings.cp_DefaultSession.cp_Content.cp_Documents.getSize() ? Boolean.TRUE : Boolean.FALSE));
895         }
896         catch (Exception e)
897         {
898             e.printStackTrace();
899         }
900     }
901 
updateBackgroundText()902     void updateBackgroundText()
903     {
904         String bg = settings.cp_DefaultSession.cp_Design.cp_BackgroundImage;
905         if (bg == null || bg.equals(PropertyNames.EMPTY_STRING))
906         {
907             bg = resources.resBackgroundNone;
908         }
909         else
910         {
911             bg = FileAccess.getPathFilename(getFileAccess().getPath(bg, null));
912         }
913         Helper.setUnoPropertyValue(getModel(txtBackground), PropertyNames.PROPERTY_LABEL, bg);
914     }
915 
updateIconsetText()916     void updateIconsetText()
917     {
918         String iconset = settings.cp_DefaultSession.cp_Design.cp_IconSet;
919         String iconsetName;
920         if (iconset == null || iconset.equals(PropertyNames.EMPTY_STRING))
921         {
922             iconsetName = resources.resIconsetNone;
923         }
924         else
925         {
926             CGIconSet is = (CGIconSet) settings.cp_IconSets.getElement(iconset);
927             if (is == null)
928             {
929                 iconsetName = resources.resIconsetNone;
930             }
931             else
932             {
933                 iconsetName = is.cp_Name;
934             }
935         }
936 
937         Helper.setUnoPropertyValue(getModel(txtIconset), PropertyNames.PROPERTY_LABEL, iconsetName);
938     }
939 
940     /**
941      * refreshes the style preview.
942      * I also call here "updateBackgroundtext", because always
943      * when the background is changed, this method
944      * has to be called, so I am walking on the safe side here...
945      */
refreshStylePreview()946     public void refreshStylePreview()
947     {
948         try
949         {
950             updateBackgroundText();
951             stylePreview.refresh(settings.cp_DefaultSession.getStyle(), settings.cp_DefaultSession.cp_Design.cp_BackgroundImage);
952             dpStylePreview.reload(xMSF);
953         }
954         catch (Exception ex)
955         {
956             ex.printStackTrace();
957         }
958     }
959 
960     private class StylesComparator implements Comparator
961     {
962 
963         /* (non-Javadoc)
964          * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
965          */
compare(Object o1, Object o2)966         public int compare(Object o1, Object o2)
967         {
968             // TODO Auto-generated method stub
969             if (o1 instanceof CGStyle && o2 instanceof CGStyle)
970             {
971                 return ((CGStyle) o1).cp_Name.compareTo(
972                         ((CGStyle) o2).cp_Name);
973             }
974             else
975             {
976                 throw new IllegalArgumentException("Cannot compare objects which are not CGStyle.");
977             }
978         }
979     }
980 }
981