summaryrefslogtreecommitdiff
path: root/wizards/com/sun/star/wizards/web/BackgroundsDialog.java
blob: 0d50664b9efe64a95ce280de575c81cefd75594c (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
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2008 by Sun Microsystems, Inc.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * $RCSfile: BackgroundsDialog.java,v $
 * $Revision: 1.7 $
 *
 * 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 javax.swing.DefaultListModel;

import com.sun.star.awt.Size;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.wizards.common.ConfigSet;
import com.sun.star.wizards.common.Configuration;
import com.sun.star.wizards.common.FileAccess;
import com.sun.star.wizards.common.SystemDialog;
import com.sun.star.wizards.ui.ImageList;
import com.sun.star.wizards.web.data.CGImage;
import com.sun.star.wizards.web.data.CGSettings;

/**
 * @author rpiterman
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class BackgroundsDialog extends ImageListDialog
{

    private FileAccess fileAccess;
    private SystemDialog sd;
    private CGSettings settings;

    /**
     * @param xmsf
     */
    public BackgroundsDialog(
            XMultiServiceFactory xmsf,
            ConfigSet set_, WebWizardDialogResources resources) throws Exception
    {

        super(xmsf, WWHID.HID_BG, new String[]
                {
                    resources.resBackgroundsDialog,
                    resources.resBackgroundsDialogCaption,
                    resources.resOK,
                    resources.resCancel,
                    resources.resHelp,
                    resources.resDeselect,
                    resources.resOther,
                    resources.resCounter
                });

        sd = SystemDialog.createOpenDialog(xmsf);
        sd.addFilter(resources.resImages, "*.jpg;*.jpeg;*.jpe;*.gif", true);
        sd.addFilter(resources.resAllFiles, "*.*", false);

        settings = (CGSettings) set_.root;

        fileAccess = new FileAccess(xmsf);
        il.setListModel(new Model(set_));
        il.setImageSize(new Size(40, 40));
        il.setRenderer(new BGRenderer(0));
        build();


    }

    /**
     * trigered when the user clicks the "other" button.
     * opens a "file open" dialog, adds the selected
     * image to the list and to the web wizard configuration,
     * and then jumps to the new image, selecting it in the list.
     * @see add(String)
     */
    public void other()
    {
        String filename[] = sd.callOpenDialog(false, settings.cp_DefaultSession.cp_InDirectory);
        if (filename != null && filename.length > 0 && filename[0] != null)
        {
            settings.cp_DefaultSession.cp_InDirectory = FileAccess.getParentDir(filename[0]);
            int i = add(filename[0]);
            il.setSelected(i);
            il.display(i);
        }
    }

    /**
     * adds the given image to the image list (to the model)
     * and to the web wizard configuration.
     * @param s
     * @return
     */
    private int add(String s)
    {

        //first i check the item does not already exists in the list...
        for (int i = 0; i < il.getListModel().getSize(); i++)
        {
            if (il.getListModel().getElementAt(i).equals(s))
            {
                return i;
            }
        }
        ((DefaultListModel) il.getListModel()).addElement(s);
        try
        {
            Object configView = Configuration.getConfigurationRoot(xMSF, FileAccess.connectURLs(WebWizardConst.CONFIG_PATH, "BackgroundImages"), true);
            int i = Configuration.getChildrenNames(configView).length + 1;
            Object o = Configuration.addConfigNode(configView, "" + i);
            Configuration.set(s, "Href", o);
            Configuration.commit(configView);
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }

        return il.getListModel().getSize() - 1;

    }

    /**
     * an ImageList Imagerenderer implemtation.
     * The image URL is the object given from the list model.
     * the image name, got from the "render" method is
     * the filename portion of the url.
     * @author rpiterman
     *
     */
    private class BGRenderer implements ImageList.IImageRenderer
    {

        private int cut;

        public BGRenderer(int cut_)
        {
            cut = cut_;
        }

        public Object[] getImageUrls(Object listItem)
        {
            Object[] sRetUrls;
            if (listItem != null)
            {
                sRetUrls = new Object[1];
                sRetUrls[0] = listItem;
                return sRetUrls;
            }
            return null;
        }

        public String render(Object object)
        {
            return object == null ? "" : FileAccess.getPathFilename(fileAccess.getPath((String) object, null));
        }
    }

    /**
     * This is a list model for the image list of the
     * backgrounds dialog.
     * It takes the Backgrounds config set as an argument,
     * and "parses" it to a list of files:
     * It goes through each image in the set, and checks it:
     * if it is a directory it lists all image files in this directory.
     * if it is a file, it adds the file to the list.
     * @author rpiterman
     */
    private class Model extends DefaultListModel
    {

        /**
         * constructor. </br>
         * see class description for a description of
         * the handling of the given model
         * @param model the configuration set of the background images.
         */
        public Model(ConfigSet model)
        {
            try
            {
                for (int i = 0; i < model.getSize(); i++)
                {
                    CGImage image = (CGImage) model.getElementAt(i);
                    String path = sd.xStringSubstitution.substituteVariables(image.cp_Href, false);
                    if (fileAccess.exists(path, false))
                    {
                        addDir(path);
                    }
                    else
                    {
                        remove((String) model.getKey(image));
                    }
                }
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
        }

        /**
         * when instanciating the model, it checks if each image
         * exists. If it doesnot, it will be removed from
         * the configuration.
         * This is what this method does...
         * @param imageName
         */
        private void remove(String imageName)
        {
            try
            {
                Object conf = Configuration.getConfigurationRoot(xMSF, WebWizardConst.CONFIG_PATH + "/BackgroundImages", true);
                Configuration.removeNode(conf, imageName);
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
        }

        /**
         * if the given url is a directory
         * adds the images in the given directory,
         * otherwise (if it is a file) adds the file to the list.
         * @param url
         */
        private void addDir(String url)
        {
            if (fileAccess.isDirectory(url))
            {
                add(fileAccess.listFiles(url, false));
            }
            else
            {
                add(url);
            }
        }

        /**
         * adds the given filenames (urls) to
         * the list
         * @param filenames
         */
        private void add(String[] filenames)
        {
            for (int i = 0; i < filenames.length; i++)
            {
                add(filenames[i]);
            }
        }

        /**
         * adds the given image url to the list.
         * if and only if it ends with jpg, jpeg or gif
         * (case insensitive)
         * @param filename image url.
         */
        private void add(String filename)
        {
            String lcase = filename.toLowerCase();
            if (lcase.endsWith("jpg") ||
                    lcase.endsWith("jpeg") ||
                    lcase.endsWith("gif"))
            {
                Model.this.addElement(filename);
            }
        }
    }
}