KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > galleries > CmsImageGallery


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/galleries/CmsImageGallery.java,v $
3  * Date : $Date: 2006/03/27 14:52:54 $
4  * Version: $Revision: 1.15 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.workplace.galleries;
33
34 import org.opencms.file.CmsProperty;
35 import org.opencms.file.CmsPropertyDefinition;
36 import org.opencms.file.CmsResource;
37 import org.opencms.file.types.CmsResourceTypeImage;
38 import org.opencms.jsp.CmsJspActionElement;
39 import org.opencms.loader.CmsImageLoader;
40 import org.opencms.loader.CmsImageScaler;
41 import org.opencms.main.CmsException;
42 import org.opencms.main.CmsLog;
43 import org.opencms.util.CmsStringUtil;
44
45 import java.awt.Color JavaDoc;
46
47 import javax.servlet.http.HttpServletRequest JavaDoc;
48 import javax.servlet.http.HttpServletResponse JavaDoc;
49 import javax.servlet.jsp.PageContext JavaDoc;
50
51 /**
52  * Generates the image gallery popup window which can be used in editors or as a dialog widget.<p>
53  *
54  * @author Andreas Zahner
55  * @author Armen Markarian
56  *
57  * @version $Revision: 1.15 $
58  *
59  * @since 6.0.0
60  */

61 public class CmsImageGallery extends A_CmsGallery {
62
63     /** URI of the image gallery popup dialog. */
64     public static final String JavaDoc URI_GALLERY = PATH_GALLERIES + "img_fs.jsp";
65
66     /** The order value of the gallery for sorting the galleries. */
67     private static final Integer JavaDoc ORDER_GALLERY = new Integer JavaDoc(10);
68
69     /** The default image scaling parameters for the gallery preview. */
70     private CmsImageScaler m_defaultScaleParams;
71
72     /**
73      * Public empty constructor, required for {@link A_CmsGallery#createInstance(String, CmsJspActionElement)}.<p>
74      */

75     public CmsImageGallery() {
76
77         // noop
78
}
79
80     /**
81      * Public constructor with JSP action element.<p>
82      *
83      * @param jsp an initialized JSP action element
84      */

85     public CmsImageGallery(CmsJspActionElement jsp) {
86
87         super(jsp);
88     }
89
90     /**
91      * Public constructor with JSP variables.<p>
92      *
93      * @param context the JSP page context
94      * @param req the JSP request
95      * @param res the JSP response
96      */

97     public CmsImageGallery(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
98
99         this(new CmsJspActionElement(context, req, res));
100     }
101
102     /**
103      * @see org.opencms.workplace.galleries.A_CmsGallery#applyButton()
104      */

105     public String JavaDoc applyButton() {
106
107         String JavaDoc width = null;
108         String JavaDoc height = null;
109         if (MODE_VIEW.equals(getParamDialogMode())) {
110             // in view mode, generate disabled button
111
return button(null, null, "apply_in.png", Messages.GUI_BUTTON_PASTE_0, 0);
112         } else {
113             // in editor or widget mode, generate enabled button
114
String JavaDoc uri = getParamResourcePath();
115             if (CmsStringUtil.isEmpty(getParamDialogMode())) {
116                 // in editor mode, create a valid link from resource path
117
uri = getJsp().link(uri);
118                 if (CmsImageLoader.isEnabled()) {
119                     // try to read the image size infromation from the "image.size" property
120
// the property will contain the information as following "h:x,w:y" with x and y integer values
121
try {
122                         CmsProperty imageSize = getJsp().getCmsObject().readPropertyObject(
123                             getParamResourcePath(),
124                             CmsPropertyDefinition.PROPERTY_IMAGE_SIZE,
125                             false);
126                         if (!imageSize.isNullProperty()) {
127                             // parse property value using standard procedures
128
CmsImageScaler scaler = new CmsImageScaler(imageSize.getValue());
129                             // javascript requires "null" String
130
if (scaler.getWidth() > 0) {
131                                 width = String.valueOf(scaler.getWidth());
132                             }
133                             if (scaler.getHeight() > 0) {
134                                 height = String.valueOf(scaler.getHeight());
135                             }
136                         }
137                     } catch (CmsException e) {
138                         // the size information could not be read (maybe the property was deleted)
139
// contine without any size information
140
}
141                 }
142             }
143             return button("javascript:pasteImage('"
144                 + uri
145                 + "',document.form.title.value, document.form.title.value,"
146                 + width
147                 + ","
148                 + height
149                 + ");", null, "apply.png", Messages.GUI_BUTTON_PASTE_0, 0);
150         }
151     }
152
153     /**
154      * Builds the html String for the preview frame.<p>
155      *
156      * @return the html String for the preview frame
157      */

158     public String JavaDoc buildGalleryItemPreview() {
159
160         StringBuffer JavaDoc html = new StringBuffer JavaDoc(16);
161         try {
162             if (CmsStringUtil.isNotEmpty(getParamResourcePath())) {
163                 CmsResource res = getCms().readResource(getParamResourcePath());
164                 if (res != null) {
165                     html.append("<img alt=\"\" SRC=\"");
166                     html.append(getJsp().link(getParamResourcePath()));
167                     html.append("\" border=\"0\">");
168                 }
169             }
170         } catch (CmsException e) {
171             // reading the resource failed
172
CmsLog.getLog(CmsImageGallery.class).error(e);
173         }
174
175         return html.toString();
176     }
177
178     /**
179      * @see org.opencms.workplace.galleries.A_CmsGallery#getGalleryItemsTypeId()
180      */

181     public int getGalleryItemsTypeId() {
182
183         return CmsResourceTypeImage.getStaticTypeId();
184     }
185
186     /**
187      * Returns the order of the implemented gallery, used to sort the gallery buttons in the editors.<p>
188      *
189      * @return the order of the implemented gallery
190      */

191     public Integer JavaDoc getOrder() {
192
193         return ORDER_GALLERY;
194     }
195
196     /**
197      * @see org.opencms.workplace.galleries.A_CmsGallery#getPreviewBodyStyle()
198      */

199     public String JavaDoc getPreviewBodyStyle() {
200
201         return "";
202     }
203
204     /**
205      * @see org.opencms.workplace.galleries.A_CmsGallery#init()
206      */

207     public void init() {
208
209         if (CmsImageLoader.isEnabled()) {
210             m_defaultScaleParams = new CmsImageScaler(getGalleryTypeParams());
211             if (!m_defaultScaleParams.isValid()) {
212                 // no valid parameters have been provided, use defaults
213
m_defaultScaleParams.setType(0);
214                 m_defaultScaleParams.setPosition(0);
215                 m_defaultScaleParams.setWidth(120);
216                 m_defaultScaleParams.setHeight(90);
217                 m_defaultScaleParams.setColor(new Color JavaDoc(221, 221, 221));
218             }
219         } else {
220             m_defaultScaleParams = null;
221         }
222     }
223
224     /**
225      * @see org.opencms.workplace.galleries.A_CmsGallery#previewButton()
226      */

227     public String JavaDoc previewButton() {
228
229         return "";
230     }
231
232     /**
233      * @see org.opencms.workplace.galleries.A_CmsGallery#targetSelectBox()
234      */

235     public String JavaDoc targetSelectBox() {
236
237         return "";
238     }
239
240     /**
241      * @see org.opencms.workplace.galleries.A_CmsGallery#buildGalleryItemListCustomEndCols(org.opencms.file.CmsResource, java.lang.String)
242      */

243     protected String JavaDoc buildGalleryItemListCustomEndCols(CmsResource res, String JavaDoc tdClass) {
244
245         if (!CmsImageLoader.isEnabled()) {
246
247             // scaling disabled, use default columns
248
return super.buildGalleryItemListCustomEndCols(res, tdClass);
249         }
250
251         StringBuffer JavaDoc result = new StringBuffer JavaDoc(128);
252         CmsImageScaler scaler = new CmsImageScaler(getCms(), res);
253
254         result.append("\t<td class=\"");
255         result.append(tdClass);
256         result.append("\" style=\"text-align: right;\">");
257         if (scaler.isValid()) {
258             // image dimensions are known
259
result.append(scaler.getWidth());
260             result.append("*");
261             result.append(scaler.getHeight());
262             result.append(" ");
263             result.append(key(Messages.GUI_LABEL_PIXELS_0));
264             result.append(" / ");
265         }
266         result.append(res.getLength() / 1024);
267         result.append(" ");
268         result.append(key(Messages.GUI_LABEL_KILOBYTES_0));
269         result.append("</td>\n");
270
271         return result.toString();
272     }
273
274     /**
275      * @see org.opencms.workplace.galleries.A_CmsGallery#buildGalleryItemListCustomStartCols(org.opencms.file.CmsResource, java.lang.String)
276      */

277     protected String JavaDoc buildGalleryItemListCustomStartCols(CmsResource res, String JavaDoc tdClass) {
278
279         if (!CmsImageLoader.isEnabled()) {
280
281             // scaling disabled, use default columns
282
return super.buildGalleryItemListCustomStartCols(res, tdClass);
283         }
284
285         CmsProperty sizeProp = CmsProperty.getNullProperty();
286         try {
287             sizeProp = getCms().readPropertyObject(res, CmsPropertyDefinition.PROPERTY_IMAGE_SIZE, false);
288         } catch (Exception JavaDoc e) {
289             // ignore
290
}
291         if (sizeProp.isNullProperty()) {
292             // image can probably not be scaled with scaler, use default columns
293
return super.buildGalleryItemListCustomStartCols(res, tdClass);
294         }
295
296         StringBuffer JavaDoc result = new StringBuffer JavaDoc(128);
297
298         if ((m_defaultScaleParams != null) && m_defaultScaleParams.isValid()) {
299             String JavaDoc resPath = getCms().getSitePath(res);
300
301             result.append("\t<td class=\"");
302             result.append(tdClass);
303             result.append("\">");
304             result.append("<a class=\"");
305             result.append(tdClass);
306             result.append("\" HREF=\"javascript: preview(\'");
307             result.append(resPath);
308             result.append("\');\" title=\"");
309             result.append(key(Messages.GUI_BUTTON_PREVIEW_0));
310             result.append("\">");
311             result.append("<img SRC=\"");
312             result.append(getJsp().link(resPath));
313             result.append(m_defaultScaleParams.toRequestParam());
314             result.append("\" border=\"0\" width=\"");
315             result.append(m_defaultScaleParams.getWidth());
316             result.append("\" height=\"");
317             result.append(m_defaultScaleParams.getHeight());
318             result.append("\"></a></td>\n");
319             result.append("</td>\n");
320         } else {
321             result.append(super.buildGalleryItemListCustomStartCols(res, tdClass));
322         }
323
324         return result.toString();
325     }
326 }
Popular Tags