summaryrefslogtreecommitdiff
path: root/wizards/com/sun/star/wizards/web/WWD_General.java
blob: d4e273f4889be4333feb91faf27d0580c8afadf8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2000, 2010 Oracle and/or its affiliates.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/
package com.sun.star.wizards.web;

import com.sun.star.awt.XWindowPeer;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.util.XStringSubstitution;
import com.sun.star.wizards.common.FileAccess;
import com.sun.star.wizards.common.Helper;
import com.sun.star.wizards.common.JavaTools;
import com.sun.star.wizards.common.SystemDialog;
import com.sun.star.wizards.ui.event.ListModelBinder;
import com.sun.star.wizards.web.data.CGDocument;
import com.sun.star.wizards.web.data.CGFilter;
import com.sun.star.wizards.web.data.CGPublish;
import com.sun.star.wizards.web.data.CGSettings;

/**
 * @author rpiterman
 * This class implements general methods, used by different sub-classes (either WWD_Sturtup, or WWD_Events)
 * or both.
 */
public abstract class WWD_General extends WebWizardDialog
{

    private FileAccess fileAccess;
    private SystemDialog docAddDialog,  folderDialog,  favIconDialog,  zipDialog;
    protected FTPDialog ftpDialog;
    protected CGSettings settings;
    /**
     * true if proxies are on, which means, ftp is disabled.
     */
    protected boolean proxies;
    private XStringSubstitution xStringSubstitution;

    protected StatusDialog getStatusDialog()
    {

        StatusDialog statusDialog = new StatusDialog(xMSF, StatusDialog.STANDARD_WIDTH, resources.resLoadingSession, false, new String[]
                {
                    resources.prodName, "", "", "", "", ""
                }, "HID:" + HID0_STATUS_DIALOG);
        try
        {
            statusDialog.createWindowPeer(xControl.getPeer());
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return statusDialog;
    }

    /**
     * @param xmsf
     */
    public WWD_General(XMultiServiceFactory xmsf)
    {
        super(xmsf);
        xStringSubstitution = SystemDialog.createStringSubstitution(xmsf);
    }

    /*
     *  File Dialog methods
     */
    protected SystemDialog getDocAddDialog()
    {
        //if (docAddDialog == null) {
        docAddDialog = SystemDialog.createOpenDialog(xMSF);
        for (int i = 0; i < settings.cp_Filters.getSize(); i++)
        {
            CGFilter f = ((CGFilter) settings.cp_Filters.getElementAt(i));
            docAddDialog.addFilter(
                    JavaTools.replaceSubString(f.cp_Name, resources.prodName, "%PRODNAME"), f.cp_Filter, i == 0);
        }
        //docAddDialog.addFilter(resources.resSODocs, "*.oxt;*.sxw;*.sxc;*.sxd;*.sxi;*.sdw;*.sdc;*.sdd;*.sdi;*.sda;*.sdp"  ,true);
        //docAddDialog.addFilter(resources.resMSDocs, "*.doc;*.xls;*.ppt;*.pps",false);
        //docAddDialog.addFilter(resources.resImages, "*.jpg;*.gif;*.png;*.bmp;*.tiff;*.jpeg;*.jpe",false);
        //docAddDialog.addFilter(resources.resAllFiles,"*.*",false);
        //}
        return docAddDialog;
    }

    protected SystemDialog getZipDialog()
    {
        if (zipDialog == null)
        {
            zipDialog = SystemDialog.createStoreDialog(xMSF);
            zipDialog.addFilter(resources.resZipFiles, "*.zip", true);
        }
        return zipDialog;
    }

    protected FTPDialog getFTPDialog(CGPublish pub)
    {
        if (ftpDialog == null)
        {
            try
            {
                ftpDialog = new FTPDialog(xMSF, pub);
                ftpDialog.createWindowPeer(xControl.getPeer());
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
        }
        return ftpDialog;
    }

    protected String showFolderDialog(String title, String description, String dir)
    {
        if (folderDialog == null)
        {
            folderDialog = SystemDialog.createFolderDialog(xMSF);
        }
        return folderDialog.callFolderDialog(title, description, dir);
    }

    protected FileAccess getFileAccess()
    {
        if (fileAccess == null)
        {
            try
            {
                fileAccess = new FileAccess(xMSF);
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
        }
        return fileAccess;
    }

    /**
     * returns the document specified
     * by the given short array.
     * @param s
     * @return
     */
    protected CGDocument getDoc(short[] s)
    {
        if (s.length == 0)
        {
            return null;
        }
        else if (settings.cp_DefaultSession.cp_Content.cp_Documents.getSize() <= s[0])
        {
            return null;
        }
        else
        {
            return (CGDocument) settings.cp_DefaultSession.cp_Content.cp_Documents.getElementAt(s[0]);
        }
    }

    /**
     * how many documents are in the list?
     * @return the number of documents in the docs list.
     */
    protected int getDocsCount()
    {
        return settings.cp_DefaultSession.cp_Content.cp_Documents.getSize();
    }

    /**
     * fills the export listbox.
     * @param listContent
     */
    protected void fillExportList(Object[] listContent)
    {
        ListModelBinder.fillList(lstDocTargetType, listContent, null);
    }

    /**
     * returns a publisher object for the given name
     * @param name one of the WebWizardConst constants : FTP
     * @return
     */
    protected CGPublish getPublisher(String name)
    {
        return (CGPublish) settings.cp_DefaultSession.cp_Publishing.getElement(name);
    }

    /**
     * @return true if the checkbox "save session" is checked.
     */
    protected boolean isSaveSession()
    {
        return (((Number) Helper.getUnoPropertyValue(
                getModel(chkSaveSettings), "State")).intValue() == 1);
    }

    /**
     * @return the name to save the session (step 7)
     */
    protected String getSessionSaveName()
    {
        return (String) Helper.getUnoPropertyValue(
                getModel(cbSaveSettings), "Text");
    }

    /**
     * This method checks the status of the wizards and
     * enables or disables the 'next' and the 'create' button.
     *
     */
    protected void checkSteps()
    {
        /* first I check the document list.
         * If it is empty, then step3 and on are disabled.
         */
        if (checkDocList())
        {
            checkPublish();
        }
    }

    /**
     * enables/disables the steps 3 to 7)
     * @param enabled true = enabled, false = disabled.
     */
    private void enableSteps(boolean enabled)
    {

        if (!enabled && !isStepEnabled(3))
        {
            return;
        /*
         * disbale steps 3-7
         */
        }
        for (int i = 3; i < 8; i++)
        {
            setStepEnabled(i, enabled, true);
        /* in this place i just disable the finish button.
         * later, in the checkPublish, which is only performed if
         * this one is true, it will be enabled (if the check
         * is positive)
         */
        }
        if (!enabled)
        {
            enableFinishButton(false);
        }
    }

    /**
     * Checks if the documents list is
     * empty. If it is, disables the steps 3-7, and the
     * create button.
     * @return
     */
    protected boolean checkDocList()
    {
        if (settings.cp_DefaultSession.cp_Content.cp_Documents.getSize() == 0)
        {
            enableSteps(false);
            return false;
        }
        else
        {
            enableSteps(true);
            return true;
        }
    }

    /**
     * check if the save-settings input is ok.
     * (eather the checkbox is unmarked, or,
     * if it is marked, a session name exists.
     *
     */
    public boolean checkSaveSession()
    {
        return (!isSaveSession() ||
                !getSessionSaveName().equals(""));

    }

    /**
     * @return false if this publisher is not active, or, if it
     * active, returns true if the url is not empty...
     * if the url is empty, throws an exception
     */
    private boolean checkPublish(String s, Object text, String property)
    {
        CGPublish p = getPublisher(s);
        if (p.cp_Publish)
        {
            String url = (String) Helper.getUnoPropertyValue(getModel(text), property);
            if ((url == null) || (url.equals("")))
            {
                throw new IllegalArgumentException();
            }
            else
            {
                return true;
            }
        }
        else
        {
            return false;
        }
    }

    /**
     *
     * @return false either if publishing input is wrong or there
     * are no publishing targets chosen. returns true when at least
     * one target is chosen, *and* all
     * which are chosen are legal.
     * If proxies are on, ftp publisher is ignored.
     */
    private boolean checkPublish_()
    {
        try
        {
            return (checkPublish(LOCAL_PUBLISHER, txtLocalDir, "Text") | (!proxies && checkPublish(FTP_PUBLISHER, lblFTP, "Label")) | checkPublish(ZIP_PUBLISHER, txtZip, "Text")) && checkSaveSession();
        }
        catch (IllegalArgumentException ex)
        {
            return false;
        }
    }

    /**
     * This method checks if the publishing
     * input is ok, and enables and disables
     * the 'create' button.
     * public because it is called from
     * an event listener object.
     */
    public void checkPublish()
    {
        enableFinishButton(checkPublish_());
    }

    /**
     * shows a message box "Unexpected Error... " :-)
     * @param ex
     */
    protected void unexpectedError(Exception ex)
    {
        ex.printStackTrace();
        XWindowPeer peer = xControl.getPeer();
        AbstractErrorHandler.showMessage(xMSF, peer, resources.resErrUnexpected, ErrorHandler.ERROR_PROCESS_FATAL);
    }

    /**
     * substitutes path variables with the corresponding values.
     * @param path a path, which might contain OOo path variables.
     * @return the path, after substituing path variables.
     */
    protected String substitute(String path)
    {
        try
        {
            return xStringSubstitution.substituteVariables(path, false);
        }
        catch (Exception ex)
        {
            return path;
        }
    }
}