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.XButton;
26 import com.sun.star.awt.XFixedText;
27 import com.sun.star.awt.XProgressBar;
28 import com.sun.star.lang.XMultiServiceFactory;
29 import com.sun.star.wizards.common.Helper;
30 import com.sun.star.wizards.common.IRenderer;
31 import com.sun.star.wizards.common.PropertyNames;
32 import com.sun.star.wizards.ui.UnoDialog;
33 import com.sun.star.wizards.ui.UnoDialog2;
34 import com.sun.star.wizards.ui.event.MethodInvocation;
35 import com.sun.star.wizards.ui.event.Task;
36 import com.sun.star.wizards.ui.event.TaskEvent;
37 import com.sun.star.wizards.ui.event.TaskListener;
38 
39 /**
40  * @author rpiterman
41  * A Class which displays a Status Dialog with status bars.
42  * This can display an X number of bars, to enable the
43  * status display of more complex tasks.
44  *
45  */
46 public class StatusDialog extends UnoDialog2 implements TaskListener
47 {
48 
49     public static final int STANDARD_WIDTH = 240;
50     private XProgressBar progressBar;
51     private XFixedText lblTaskName;
52     private XFixedText lblCounter;
53     private XButton btnCancel;
54     private String[] res;
55     private IRenderer renderer;
56     private boolean enableBreak = false;
57     private boolean closeOnFinish = true;
58     private MethodInvocation finishedMethod;
59     private UnoDialog parent;
60     private boolean finished;
61 
62     /**
63      * Note on the argument resource:
64      * This should be a String array containing the followin strings, in the
65      * following order:
66      * dialog title, cancel, close, counter prefix, counter midfix, counter postfix
67      */
StatusDialog(XMultiServiceFactory xmsf, int width, String taskName, boolean displayCount, String[] resources, String hid)68     public StatusDialog(XMultiServiceFactory xmsf, int width, String taskName, boolean displayCount, String[] resources, String hid)
69     {
70         super(xmsf);
71 
72         res = resources;
73         if (res.length != 6)
74         {
75             throw new IllegalArgumentException("The resources argument should contain 6 Strings, see Javadoc on constructor.");        //display a close button?
76         // if enableBreak == false and closeOnFinsih == false;
77         }
78         boolean b = !enableBreak && !closeOnFinish;
79 
80         Helper.setUnoPropertyValues(xDialogModel,
81                 new String[]
82                 {
83                     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
84                 },
85                 new Object[]
86                 {
87                     Boolean.FALSE, new Integer(6 + 25 + (b ? 27 : 7)), hid, Boolean.TRUE, "StatusDialog", 102, 52, 0, res[0], new Integer(width)
88                 });
89 
90         short tabstop = 1;
91 
92         lblTaskName = insertLabel("lblTask",
93                 new String[]
94                 {
95                     PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_LABEL, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_TABINDEX, PropertyNames.PROPERTY_WIDTH
96                 },
97                 new Object[]
98                 {
99                     8, taskName, 6, 6, new Short(tabstop++), new Integer(width * 2 / 3)
100                 });
101         lblCounter = insertLabel("lblCounter",
102                 new String[]
103                 {
104                     PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_LABEL, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_TABINDEX, PropertyNames.PROPERTY_WIDTH
105                 },
106                 new Object[]
107                 {
108                     8, PropertyNames.EMPTY_STRING, new Integer(width * 2 / 3), 6, new Short(tabstop++), new Integer(width / 3 - 4)
109                 });
110         progressBar = insertProgressBar("progress",
111                 new String[]
112                 {
113                     PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_TABINDEX, PropertyNames.PROPERTY_WIDTH
114                 },
115                 new Object[]
116                 {
117                     10, 6, 16, new Short(tabstop++), new Integer(width - 12)
118                 });
119 
120 
121         if (b)
122         {
123             btnCancel = insertButton("btnCancel", "performCancel", this,
124                     new String[]
125                     {
126                         PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_LABEL, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_TABINDEX, PropertyNames.PROPERTY_WIDTH
127                     },
128                     new Object[]
129                     {
130                         14, res[1], new Integer(width / 2 - 20), new Integer(6 + 25 + 7), new Short(tabstop++), 40
131                     });
132         }
133 
134 //      xWindow.addWindowListener((XWindowListener)guiEventListener);
135 //      guiEventListener.add("StatusDialog" ,EVENT_WINDOW_SHOWN, "performRunnable",this);
136 
137     }
138 
initProgressBar(Task t)139     private void initProgressBar(Task t)
140     {
141         progressBar.setRange(0, t.getMax());
142         progressBar.setValue(0);
143     }
144 
setStatus(int status)145     private void setStatus(int status)
146     {
147         if (finished)
148         {
149             return;
150         }
151         progressBar.setValue(status);
152         xReschedule.reschedule();
153     }
154 
setLabel(String s)155     public void setLabel(String s)
156     {
157 //      lblTaskName.setText(s);
158         Helper.setUnoPropertyValue(UnoDialog.getModel(lblTaskName), PropertyNames.PROPERTY_LABEL, s);
159         xReschedule.reschedule();
160     }
161 
162     /**
163      * change the max property of the status bar
164      * @param max
165      */
setMax(int max)166     private void setMax(int max)
167     {
168         if (finished)
169         {
170             return;
171         }
172         Helper.setUnoPropertyValue(getModel(progressBar), "ProgressValueMax", new Integer(max));
173     }
174 
175     /**
176      * initialize the status bar according
177      * to the given event.
178      */
taskStarted(TaskEvent te)179     public void taskStarted(TaskEvent te)
180     {
181         finished = false;
182         initProgressBar(te.getTask());
183     }
184 
185     /**
186      * closes the dialog.
187      */
taskFinished(TaskEvent te)188     public void taskFinished(TaskEvent te)
189     {
190         finished = true;
191         if (closeOnFinish)
192         {
193 //          xDialog.endExecute();
194             parent.xWindow.setEnable(true);
195             try
196             {
197                 xWindow.setVisible(false);
198                 xComponent.dispose();
199             //System.out.println("disposed");
200             }
201             catch (Exception ex)
202             {
203                 ex.printStackTrace();
204             }
205         }
206         else
207         {
208             Helper.setUnoPropertyValue(getModel(btnCancel), PropertyNames.PROPERTY_LABEL, res[2]);
209         }
210     }
211 
212     /**
213      * changes the status display
214      */
taskStatusChanged(TaskEvent te)215     public void taskStatusChanged(TaskEvent te)
216     {
217         setMax(te.getTask().getMax());
218         setStatus(te.getTask().getStatus());
219     }
220 
221     /**
222      * changes the displayed text.
223      * A renderer is used to render
224      * the task's subtask name to a resource string.
225      */
subtaskNameChanged(TaskEvent te)226     public void subtaskNameChanged(TaskEvent te)
227     {
228         if (renderer != null)
229         {
230             setLabel(renderer.render(te.getTask().getSubtaskName()));
231         }
232     }
233 
234     /**
235      * displays the status dialog
236      * @param parent the parent dialog
237      * @param r what to do
238      */
execute(final UnoDialog parent_, final Task task, String title)239     public void execute(final UnoDialog parent_, final Task task, String title)
240     {
241         try
242         {
243             this.parent = parent_;
244             Helper.setUnoPropertyValue(this.xDialogModel, PropertyNames.PROPERTY_TITLE, title);
245             try
246             {
247                 //TODO change this to another execute dialog method.
248                 task.addTaskListener(StatusDialog.this);
249                 setMax(10);
250                 setStatus(0);
251                 setLabel(task.getSubtaskName());
252                 parent.xWindow.setEnable(false);
253                 setVisible(parent);
254                 if (finishedMethod != null)
255                 {
256                     finishedMethod.invoke();
257                 }
258             }
259             catch (Exception e)
260             {
261                 e.printStackTrace();
262             }
263         }
264         catch (Exception ex)
265         {
266             ex.printStackTrace();
267         }
268     }
269 
270     /**
271      * not supported !
272      */
performCancel()273     public void performCancel()
274     {//TODO - implement a thread thing here...
275         xWindow.setVisible(false);
276     }
277 
278     /**
279      * @return the subTask renderer object
280      */
getRenderer()281     public IRenderer getRenderer()
282     {
283         return renderer;
284     }
285 
286     /**
287      * @param renderer
288      */
setRenderer(IRenderer renderer)289     public void setRenderer(IRenderer renderer)
290     {
291         this.renderer = renderer;
292     }
293 
294     /**
295      * sets a method to be invoced when the
296      *
297      */
setFinishedMethod(MethodInvocation mi)298     public void setFinishedMethod(MethodInvocation mi)
299     {
300         finishedMethod = mi;
301     }
302 }
303