summaryrefslogtreecommitdiff
path: root/wizards/com/sun/star/wizards/web/ImageListDialog.java
blob: 4fbf832f32ffbe0c4ce55ae3ebb38f4c01478963 (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
/*************************************************************************
 *
 * 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: ImageListDialog.java,v $
 * $Revision: 1.6 $
 *
 * 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.FontDescriptor;
import com.sun.star.awt.Size;
import com.sun.star.awt.XButton;
import com.sun.star.awt.XFixedText;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.wizards.common.Helper;
import com.sun.star.wizards.common.JavaTools;
import com.sun.star.wizards.common.IRenderer;
import com.sun.star.wizards.ui.ImageList;
import com.sun.star.wizards.ui.UIConsts;
import com.sun.star.wizards.ui.UnoDialog2;
import com.sun.star.wizards.ui.ImageList.Counter;

/**
 * This class is a general implementation for a
 * dialog which displays a choice of images.
 * The model and the renderer are
 * still abstract in this class.
 * To use the class one should extend it,
 * in the constructor then set the imageList
 * properties (member name il) like image size, grid size,
 * model renderer aso, and then call "build".
 * This class uses a counter renderer which
 * gets an expression which contains %START %END %TOTAL and replaces
 * them with the actual numbers.
 * <BR/>
 * two buttons: "other" and "none" which are there
 * to enable choosing images that are not in the list and
 * "no image" respectivley, are optional, with default to true,
 * so dialogs which do not need those, should set the corresponding
 * members showDeselectButton and/or showOtherButton to false.
 * <br/>
 * the consturctor should recieve, among others, an Array of String resources - see
 * constructor documentation for details.
 *
 * @author rpiterman
 */
public abstract class ImageListDialog extends UnoDialog2 implements UIConsts
{

    private String[] resources;
    private final static int RES_TITLE = 0;
    private final static int RES_LABEL = 1;
    private final static int RES_OK = 2;
    private final static int RES_CANCEL = 3;
    private final static int RES_HELP = 4;
    private final static int RES_DESELECT = 5;
    private final static int RES_OTHER = 6;
    private final static int RES_COUNTER = 7;    //GUI Components as Class members.
    private XButton btnOK;
    private XButton btnCancel;
    private XButton btnHelp;
    private XButton btnOther;
    private XButton btnDeselect;
    private static final String START = "%START";
    private static final String END = "%END";
    private static final String TOTAL = "%TOTAL";
    protected int cutFilename = 0;
    protected boolean showDeselectButton = true;
    protected boolean showOtherButton = true;
    private XFixedText lblTitle;
    //Font Descriptors as Class members.
    private FontDescriptor fontDescriptor1 = new FontDescriptor();
    protected ImageList il;    //private FileAccess fileAccess;
    private Object result;
    private int hid;

    /**
     *
     * @param xmsf
     * @param resources_ a string array with the following strings :
     * dialog title, label text, ok, cancel, help, deselect, other.
     * <br/> if "deselect" and "other" are not displayed,
     * the array can also be shorter. but if "other" is displayed
     * and "deselect" not, both must be there :-(
     */
    public ImageListDialog(
            XMultiServiceFactory xmsf, int hid_, String[] resources_)
    {
        super(xmsf);
        hid = hid_;
        resources = resources_;
        il = new ImageList();
        il.counterRenderer = new ARenderer(resources[RES_COUNTER]);
    }

    /**
     * adds the controls to the dialog, depending on
     * the size of the image list.
     * This method should be called by subclasses after setting
     * the il ImageList member properties
     */
    protected void build()
    {
        //set dialog properties...

        int ilWidth = (il.getImageSize().Width + il.getGap().Width) * il.getCols() + il.getGap().Width;
        int ilHeight = (il.getImageSize().Height + il.getGap().Height) * il.getRows() + il.getGap().Height;

        int dialogWidth = 6 + ilWidth + 6 + 50 + 6;
        int dialogHeight = 3 + 16 + 3 + (ilHeight + 8 + 14) + 6;

        Helper.setUnoPropertyValues(xDialogModel,
                new String[]
                {
                    "Closeable", "Height", "HelpURL", "Moveable", "Name", "PositionX", "PositionY", "Step", "Title", "Width"
                },
                new Object[]
                {
                    Boolean.TRUE, new Integer(dialogHeight), "HID:" + hid, Boolean.TRUE, "imgDialog", new Integer(59), new Integer(24), INTEGERS[1], resources[RES_TITLE], new Integer(dialogWidth)
                });
        //Set member- FontDescriptors...
        fontDescriptor1.Weight = 150;

        final String[] PROPNAMES = new String[]
        {
            "DefaultButton", "Height", "HelpURL", "Label", "Name", "PositionX", "PositionY", "PushButtonType", "TabIndex", "Width"
        };

        Integer iButtonsX = new Integer(6 + ilWidth + 6);

        btnOK = insertButton("btnOK", null,
                PROPNAMES,
                new Object[]
                {
                    Boolean.TRUE, INTEGER_14, "HID:" + (hid + 3), resources[RES_OK], "btnOK", iButtonsX, new Integer(22), new Short((short) com.sun.star.awt.PushButtonType.OK_value), new Short((short) 7), INTEGER_50
                });
        btnCancel = insertButton("btnCancel", null,
                PROPNAMES,
                new Object[]
                {
                    Boolean.FALSE, INTEGER_14, "HID:" + (hid + 4), resources[RES_CANCEL], "btnCancel", iButtonsX, new Integer(41), new Short((short) com.sun.star.awt.PushButtonType.CANCEL_value), new Short((short) 8), INTEGER_50
                });
        btnHelp = insertButton("btnHelp", null,
                PROPNAMES,
                new Object[]
                {
                    Boolean.FALSE, INTEGER_14, "", resources[RES_HELP], "CommandButton3", iButtonsX, new Integer(71), new Short((short) com.sun.star.awt.PushButtonType.HELP_value), new Short((short) 9), INTEGER_50
                });

        if (showOtherButton)
        {

            int otherY = 22 + ilHeight - 14 - (showDeselectButton ? 19 : 0);

            btnOther = insertButton("btnOther", "other",
                    PROPNAMES,
                    new Object[]
                    {
                        Boolean.FALSE, INTEGER_14, "HID:" + (hid + 1), resources[RES_OTHER], "btnOther", iButtonsX, new Integer(otherY), new Short((short) com.sun.star.awt.PushButtonType.STANDARD_value), new Short((short) 5), INTEGER_50
                    });
        }

        if (showDeselectButton)
        {

            int deselectY = 22 + ilHeight - 14;

            btnDeselect = insertButton("btnNoImage", "deselect",
                    PROPNAMES,
                    new Object[]
                    {
                        Boolean.FALSE, INTEGER_14, "HID:" + (hid + 2), resources[RES_DESELECT], "btnNoImage", iButtonsX, new Integer(deselectY), new Short((short) com.sun.star.awt.PushButtonType.STANDARD_value), new Short((short) 4), INTEGER_50
                    });
        }

        il.setStep(new Short((short) 1));
        il.setPos(new Size(6, 22));
        il.helpURL = hid + 5;
        il.tabIndex = 1;
        il.create(this);

        /*lblContainer = insertLabel("lblContainer",
        new String[] {"Height", "Name", "PositionX", "PositionY", "TabIndex", "Width"},
        new Object[] { new Integer(176),"lblContainer",new Integer(6),new Integer(17),new Short((short)5),new Integer(214)}
        );*/

        lblTitle = insertLabel("lblTitle",
                new String[]
                {
                    "FontDescriptor", "Height", "Label", "Name", "PositionX", "PositionY", "Step", "TabIndex", "Width"
                },
                new Object[]
                {
                    fontDescriptor1, INTEGERS[8], resources[RES_LABEL], "lblTitle", INTEGERS[6], INTEGERS[6], INTEGERS[1], new Short((short) 4), new Integer(216)
                });

    }

    /**
     * is called when the user clicks "none"
     */
    public void deselect()
    {
        il.setSelected(-1);
    }

    /**
     * is called when the user clicks "other"
     *
     */
    public void other()
    {
    }

    /**
     * @return the currently elected object.
     */
    public Object getSelected()
    {
        return il.getSelectedObject();
    }

    /**
     * sets the currently selected object.
     * @param obj the object (out of the model) to be selected.
     */
    public void setSelected(Object obj)
    {
        il.setSelected(obj);
        il.showSelected();
    }

    /**
     * The counter renderer, which uses a template.
     * The template replaces the Strings "%START", "%END" and
     * "%TOTAL" with the respective values.
     * @author rpiterman
     *
     */
    public static class ARenderer implements IRenderer
    {

        String template;

        /**
         * @param aTempalte a template for this renderer.
         * The strings %START, %END ,%TOTAL will be replaced
         * with the actual values.
         */
        public ARenderer(String aTemplate)
        {
            template = aTemplate;
        }

        public String render(Object counter)
        {
            String s = JavaTools.replaceSubString(template, "" + ((Counter) counter).start, START);
            s = JavaTools.replaceSubString(s, "" + ((Counter) counter).end, END);
            s = JavaTools.replaceSubString(s, "" + ((Counter) counter).max, TOTAL);
            return s;
        }
    }
}