001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/commons/CmsProgressThread.java,v $
003: * Date : $Date: 2008-02-27 12:05:25 $
004: * Version: $Revision: 1.6 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.workplace.commons;
033:
034: import org.opencms.main.CmsLog;
035: import org.opencms.workplace.list.A_CmsListDialog;
036: import org.opencms.workplace.list.CmsHtmlList;
037: import org.opencms.workplace.list.CmsListExplorerColumn;
038:
039: import java.util.Locale;
040:
041: import org.apache.commons.logging.Log;
042:
043: /**
044: * This class is used to build a list ({@link A_CmsListDialog}) in the background
045: * and to show the progress of building to the user.<p>
046: *
047: * To work correctly the operations while building the list have to update the
048: * actual progress.<p>
049: *
050: * @author Peter Bonrad
051: *
052: * @version $Revision: 1.6 $
053: *
054: * @since 7.0.0
055: */
056: public class CmsProgressThread extends Thread {
057:
058: /** The log object for this class. */
059: private static final Log LOG = CmsLog
060: .getLog(CmsProgressThread.class);
061:
062: /** The description to show for the progress. */
063: private String m_description;
064:
065: /** Stores the error occurred while the thread was running. */
066: private Throwable m_error;
067:
068: /** The time the thread has finished. */
069: private long m_finishtime;
070:
071: /** The key of this thread. */
072: private String m_key;
073:
074: /** The list the thread displays the progress of building it. */
075: private A_CmsListDialog m_list;
076:
077: /** The locale to use for this thread. */
078: private Locale m_locale;
079:
080: /** The actual progress of the thread. */
081: private int m_progress;
082:
083: /** The finished result as HTML of the list. */
084: private String m_result;
085:
086: /** The time the thread has started. */
087: private long m_starttime;
088:
089: /**
090: * Constructs a new progress thread with the given name.<p>
091: *
092: * @param list the list to use for the progress to display
093: * @param key the key of the thread
094: * @param locale the locale to use for this thread
095: */
096: public CmsProgressThread(A_CmsListDialog list, String key,
097: Locale locale) {
098:
099: m_progress = 0;
100: m_list = list;
101: m_key = key;
102: m_starttime = 0;
103: m_finishtime = 0;
104: m_locale = locale;
105:
106: setName(Messages.get().getBundle().key(
107: Messages.GUI_PROGRESS_THREAD_NAME_1, key));
108: }
109:
110: /**
111: * Returns the description to show for the progress.<p>
112: *
113: * @return the description to show for the progress
114: */
115: public String getDescription() {
116:
117: return m_description;
118: }
119:
120: /**
121: * Returns the error exception in case there was an error during the execution of
122: * this thread, null otherwise.<p>
123: *
124: * @return the error exception in case there was an error, null otherwise
125: */
126: public Throwable getError() {
127:
128: return m_error;
129: }
130:
131: /**
132: * Returns the time the thread was finished.<p>
133: *
134: * @return the time the thread was finished
135: */
136: public long getFinishTime() {
137:
138: return m_finishtime;
139: }
140:
141: /**
142: * Returns the key of this thread.<p>
143: *
144: * @return the key of this thread
145: */
146: public String getKey() {
147:
148: return m_key;
149: }
150:
151: /**
152: * Returns the list of the progress bar.<p>
153: *
154: * @return the list of the progress bar
155: */
156: public A_CmsListDialog getList() {
157:
158: return m_list;
159: }
160:
161: /**
162: * Returns the locale to use for this thread.<p>
163: *
164: * @return the locale to use for this thread
165: */
166: public Locale getLocale() {
167:
168: return m_locale;
169: }
170:
171: /**
172: * Returns the progress.<p>
173: *
174: * @return the progress
175: */
176: public int getProgress() {
177:
178: return m_progress;
179: }
180:
181: /**
182: * Returns the finished result as HTML of the list.<p>
183: *
184: * @return the finished result as HTML of the list
185: */
186: public String getResult() {
187:
188: return m_result;
189: }
190:
191: /**
192: * Returns the time this report has been running.<p>
193: *
194: * @return the time this report has been running
195: */
196: public synchronized long getRuntime() {
197:
198: if (!isAlive()) {
199: return m_finishtime - m_starttime;
200: } else if (m_starttime > 0) {
201: return System.currentTimeMillis() - m_starttime;
202: } else {
203: return 0;
204: }
205: }
206:
207: /**
208: * Returns the time the thread has started.<p>
209: *
210: * @return the time the thread has started
211: */
212: public long getStartTime() {
213:
214: return m_starttime;
215: }
216:
217: /**
218: * @see java.lang.Thread#run()
219: */
220: public void run() {
221:
222: try {
223: if (LOG.isDebugEnabled()) {
224: LOG
225: .debug(Messages.get().getBundle().key(
226: Messages.LOG_PROGRESS_START_THREAD_1,
227: getKey()));
228: }
229: m_starttime = System.currentTimeMillis();
230:
231: // calculate size of the list
232: m_list.refreshList();
233:
234: // create the list
235: CmsHtmlList list = m_list.getList();
236: list.setBoxed(false);
237:
238: StringBuffer result = new StringBuffer();
239:
240: result.append("<input type='hidden' name='result' value='");
241: result.append(list.getTotalSize()).append("'>\n");
242: result.append(CmsListExplorerColumn.getExplorerStyleDef());
243: result
244: .append("<div style='height:200px; overflow: auto;'>\n");
245: result.append(list.listHtml());
246: result.append("</div>\n");
247:
248: m_result = result.toString();
249: } catch (Throwable t) {
250: m_error = t;
251:
252: if (LOG.isErrorEnabled()) {
253: LOG.error(Messages.get().getBundle().key(
254: Messages.LOG_ERROR_EXECUTING_THREAD_0), t);
255: }
256: } finally {
257: m_finishtime = System.currentTimeMillis();
258: if (LOG.isDebugEnabled()) {
259: LOG.debug(Messages.get().getBundle().key(
260: Messages.LOG_PROGRESS_FINISHED_THREAD_1,
261: getKey()));
262: }
263: }
264:
265: }
266:
267: /**
268: * Sets the description to show for the progress.<p>
269: *
270: * @param description the description to show for the progress to set
271: */
272: public void setDescription(String description) {
273:
274: m_description = description;
275: }
276:
277: /**
278: * Sets the locale to use for this thread.<p>
279: *
280: * @param locale the locale to use for this thread to set
281: */
282: public void setLocale(Locale locale) {
283:
284: m_locale = locale;
285: }
286:
287: /**
288: * Sets the progress.<p>
289: *
290: * @param progress the progress to set
291: */
292: public void setProgress(int progress) {
293:
294: m_progress = progress;
295: }
296:
297: }
|