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 
23 package com.sun.star.wizards.web;
24 
25 import com.sun.star.awt.FontDescriptor;
26 import com.sun.star.awt.Size;
27 import com.sun.star.awt.XButton;
28 import com.sun.star.awt.XFixedText;
29 import com.sun.star.lang.XMultiServiceFactory;
30 import com.sun.star.wizards.common.Helper;
31 import com.sun.star.wizards.common.HelpIds;
32 import com.sun.star.wizards.common.JavaTools;
33 import com.sun.star.wizards.common.IRenderer;
34 import com.sun.star.wizards.common.PropertyNames;
35 import com.sun.star.wizards.ui.ImageList;
36 import com.sun.star.wizards.ui.UIConsts;
37 import com.sun.star.wizards.ui.UnoDialog2;
38 import com.sun.star.wizards.ui.ImageList.Counter;
39 
40 /**
41  * This class is a general implementation for a
42  * dialog which displays a choice of images.
43  * The model and the renderer are
44  * still abstract in this class.
45  * To use the class one should extend it,
46  * in the constructor then set the imageList
47  * properties (member name il) like image size, grid size,
48  * model renderer aso, and then call "build".
49  * This class uses a counter renderer which
50  * gets an expression which contains %START %END %TOTAL and replaces
51  * them with the actual numbers.
52  * <BR/>
53  * two buttons: "other" and "none" which are there
54  * to enable choosing images that are not in the list and
55  * "no image" respectivley, are optional, with default to true,
56  * so dialogs which do not need those, should set the corresponding
57  * members showDeselectButton and/or showOtherButton to false.
58  * <br/>
59  * the consturctor should recieve, among others, an Array of String resources - see
60  * constructor documentation for details.
61  *
62  * @author rpiterman
63  */
64 public abstract class ImageListDialog extends UnoDialog2 implements UIConsts
65 {
66 
67     private String[] resources;
68     private final static int RES_TITLE = 0;
69     private final static int RES_LABEL = 1;
70     private final static int RES_OK = 2;
71     private final static int RES_CANCEL = 3;
72     private final static int RES_HELP = 4;
73     private final static int RES_DESELECT = 5;
74     private final static int RES_OTHER = 6;
75     private final static int RES_COUNTER = 7;    //GUI Components as Class members.
76     private XButton btnOK;
77     private XButton btnCancel;
78     private XButton btnHelp;
79     private XButton btnOther;
80     private XButton btnDeselect;
81     private static final String START = "%START";
82     private static final String END = "%END";
83     private static final String TOTAL = "%TOTAL";
84     protected int cutFilename = 0;
85     protected boolean showDeselectButton = true;
86     protected boolean showOtherButton = true;
87     private XFixedText lblTitle;
88     //Font Descriptors as Class members.
89     private FontDescriptor fontDescriptor1 = new FontDescriptor();
90     protected ImageList il;    //private FileAccess fileAccess;
91     private Object result;
92     private int hid;
93 
94     /**
95      *
96      * @param xmsf
97      * @param resources_ a string array with the following strings :
98      * dialog title, label text, ok, cancel, help, deselect, other.
99      * <br/> if "deselect" and "other" are not displayed,
100      * the array can also be shorter. but if "other" is displayed
101      * and "deselect" not, both must be there :-(
102      */
ImageListDialog( XMultiServiceFactory xmsf, int hid_, String[] resources_)103     public ImageListDialog(
104             XMultiServiceFactory xmsf, int hid_, String[] resources_)
105     {
106         super(xmsf);
107         hid = hid_;
108         resources = resources_;
109         il = new ImageList();
110         il.counterRenderer = new ARenderer(resources[RES_COUNTER]);
111     }
112 
113     /**
114      * adds the controls to the dialog, depending on
115      * the size of the image list.
116      * This method should be called by subclasses after setting
117      * the il ImageList member properties
118      */
build()119     protected void build()
120     {
121         //set dialog properties...
122 
123         int ilWidth = (il.getImageSize().Width + il.getGap().Width) * il.getCols() + il.getGap().Width;
124         int ilHeight = (il.getImageSize().Height + il.getGap().Height) * il.getRows() + il.getGap().Height;
125 
126         int dialogWidth = 6 + ilWidth + 6 + 50 + 6;
127         int dialogHeight = 3 + 16 + 3 + (ilHeight + 8 + 14) + 6;
128 
129         Helper.setUnoPropertyValues(xDialogModel,
130                 new String[]
131                 {
132                     PropertyNames.PROPERTY_CLOSEABLE, PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_HELPURL, PropertyNames.PROPERTY_MOVEABLE, PropertyNames.PROPERTY_NAME, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_TITLE, PropertyNames.PROPERTY_WIDTH
133                 },
134                 new Object[]
135                 {
136                     Boolean.TRUE, new Integer(dialogHeight), HelpIds.getHelpIdString(hid), Boolean.TRUE, "imgDialog", 59, 24, INTEGERS[1], resources[RES_TITLE], new Integer(dialogWidth)
137                 });
138         //Set member- FontDescriptors...
139         fontDescriptor1.Weight = 150;
140 
141         final String[] PROPNAMES = new String[]
142         {
143             "DefaultButton", PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_HELPURL, PropertyNames.PROPERTY_LABEL, PropertyNames.PROPERTY_NAME, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, "PushButtonType", PropertyNames.PROPERTY_TABINDEX, PropertyNames.PROPERTY_WIDTH
144         };
145 
146         Integer iButtonsX = new Integer(6 + ilWidth + 6);
147 
148         btnOK = insertButton("btnOK", null,
149                 PROPNAMES,
150                 new Object[]
151                 {
152                     Boolean.TRUE, INTEGER_14, HelpIds.getHelpIdString(hid + 3), resources[RES_OK], "btnOK", iButtonsX, 22, new Short((short) com.sun.star.awt.PushButtonType.OK_value), new Short((short) 7), INTEGER_50
153                 });
154         btnCancel = insertButton("btnCancel", null,
155                 PROPNAMES,
156                 new Object[]
157                 {
158                     Boolean.FALSE, INTEGER_14, HelpIds.getHelpIdString(hid + 4), resources[RES_CANCEL], "btnCancel", iButtonsX, 41, new Short((short) com.sun.star.awt.PushButtonType.CANCEL_value), new Short((short) 8), INTEGER_50
159                 });
160         btnHelp = insertButton("btnHelp", null,
161                 PROPNAMES,
162                 new Object[]
163                 {
164                     Boolean.FALSE, INTEGER_14, PropertyNames.EMPTY_STRING, resources[RES_HELP], "CommandButton3", iButtonsX, 71, new Short((short) com.sun.star.awt.PushButtonType.HELP_value), new Short((short) 9), INTEGER_50
165                 });
166 
167         if (showOtherButton)
168         {
169 
170             int otherY = 22 + ilHeight - 14 - (showDeselectButton ? 19 : 0);
171 
172             btnOther = insertButton("btnOther", "other",
173                     PROPNAMES,
174                     new Object[]
175                     {
176                         Boolean.FALSE, INTEGER_14, HelpIds.getHelpIdString(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
177                     });
178         }
179 
180         if (showDeselectButton)
181         {
182 
183             int deselectY = 22 + ilHeight - 14;
184 
185             btnDeselect = insertButton("btnNoImage", "deselect",
186                     PROPNAMES,
187                     new Object[]
188                     {
189                         Boolean.FALSE, INTEGER_14, HelpIds.getHelpIdString(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
190                     });
191         }
192 
193         il.setStep(new Short((short) 1));
194         il.setPos(new Size(6, 22));
195         il.helpURL = hid + 5;
196         il.tabIndex = 1;
197         il.create(this);
198 
199         /*lblContainer = insertLabel("lblContainer",
200         new String[] {PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_NAME, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_TABINDEX, PropertyNames.PROPERTY_WIDTH},
201         new Object[] { 176,"lblContainer",6,17,new Short((short)5),214}
202         );*/
203 
204         lblTitle = insertLabel("lblTitle",
205                 new String[]
206                 {
207                     PropertyNames.FONT_DESCRIPTOR, PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_LABEL, PropertyNames.PROPERTY_NAME, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_TABINDEX, PropertyNames.PROPERTY_WIDTH
208                 },
209                 new Object[]
210                 {
211                     fontDescriptor1, INTEGERS[8], resources[RES_LABEL], "lblTitle", INTEGERS[6], INTEGERS[6], INTEGERS[1], new Short((short) 4), 216
212                 });
213 
214     }
215 
216     /**
217      * is called when the user clicks "none"
218      */
deselect()219     public void deselect()
220     {
221         il.setSelected(-1);
222     }
223 
224     /**
225      * is called when the user clicks "other"
226      *
227      */
other()228     public void other()
229     {
230     }
231 
232     /**
233      * @return the currently elected object.
234      */
getSelected()235     public Object getSelected()
236     {
237         return il.getSelectedObject();
238     }
239 
240     /**
241      * sets the currently selected object.
242      * @param obj the object (out of the model) to be selected.
243      */
setSelected(Object obj)244     public void setSelected(Object obj)
245     {
246         il.setSelected(obj);
247         il.showSelected();
248     }
249 
250     /**
251      * The counter renderer, which uses a template.
252      * The template replaces the Strings "%START", "%END" and
253      * "%TOTAL" with the respective values.
254      * @author rpiterman
255      *
256      */
257     public static class ARenderer implements IRenderer
258     {
259 
260         String template;
261 
262         /**
263          * @param aTempalte a template for this renderer.
264          * The strings %START, %END ,%TOTAL will be replaced
265          * with the actual values.
266          */
ARenderer(String aTemplate)267         public ARenderer(String aTemplate)
268         {
269             template = aTemplate;
270         }
271 
render(Object counter)272         public String render(Object counter)
273         {
274             String s = JavaTools.replaceSubString(template, PropertyNames.EMPTY_STRING + ((Counter) counter).start, START);
275             s = JavaTools.replaceSubString(s, PropertyNames.EMPTY_STRING + ((Counter) counter).end, END);
276             s = JavaTools.replaceSubString(s, PropertyNames.EMPTY_STRING + ((Counter) counter).max, TOTAL);
277             return s;
278         }
279     }
280 }
281