001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.ui.apps.cacheadmin; 029 030import org.opencms.file.CmsObject; 031import org.opencms.file.CmsResource; 032import org.opencms.file.types.CmsResourceTypeImage; 033import org.opencms.main.CmsException; 034import org.opencms.main.CmsLog; 035import org.opencms.main.OpenCms; 036import org.opencms.ui.A_CmsUI; 037import org.opencms.ui.CmsVaadinUtils; 038import org.opencms.ui.apps.CmsAppWorkplaceUi; 039import org.opencms.ui.apps.CmsFileExplorerConfiguration; 040import org.opencms.ui.apps.Messages; 041import org.opencms.ui.apps.cacheadmin.CmsCacheViewApp.Mode; 042import org.opencms.ui.components.CmsBasicDialog; 043import org.opencms.ui.components.CmsBasicDialog.DialogWidth; 044import org.opencms.ui.components.OpenCmsTheme; 045import org.opencms.ui.contextmenu.CmsContextMenu; 046import org.opencms.ui.contextmenu.CmsMenuItemVisibilityMode; 047import org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry; 048import org.opencms.util.CmsStringUtil; 049import org.opencms.workplace.explorer.CmsResourceUtil; 050 051import java.util.ArrayList; 052import java.util.Collection; 053import java.util.Collections; 054import java.util.List; 055import java.util.Locale; 056import java.util.Set; 057 058import org.apache.commons.logging.Log; 059 060import com.vaadin.server.Resource; 061import com.vaadin.shared.MouseEventDetails.MouseButton; 062import com.vaadin.ui.Window; 063import com.vaadin.ui.themes.ValoTheme; 064import com.vaadin.v7.data.Item; 065import com.vaadin.v7.data.util.IndexedContainer; 066import com.vaadin.v7.data.util.filter.Or; 067import com.vaadin.v7.data.util.filter.SimpleStringFilter; 068import com.vaadin.v7.event.ItemClickEvent; 069import com.vaadin.v7.event.ItemClickEvent.ItemClickListener; 070import com.vaadin.v7.ui.Table; 071import com.vaadin.v7.ui.TextField; 072import com.vaadin.v7.ui.VerticalLayout; 073 074/** 075 * Table to show entries of image cache.<p> 076 */ 077public class CmsImageCacheTable extends Table { 078 079 /** 080 * Menu entry for show variations option.<p> 081 */ 082 class EntryVariations 083 implements I_CmsSimpleContextMenuEntry<Set<String>>, I_CmsSimpleContextMenuEntry.I_HasCssStyles { 084 085 /** 086 * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#executeAction(java.lang.Object) 087 */ 088 public void executeAction(Set<String> data) { 089 090 String resource = data.iterator().next(); 091 showVariationsWindow(resource); 092 } 093 094 /** 095 * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry.I_HasCssStyles#getStyles() 096 */ 097 public String getStyles() { 098 099 return ValoTheme.LABEL_BOLD; 100 } 101 102 /** 103 * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getTitle(java.util.Locale) 104 */ 105 public String getTitle(Locale locale) { 106 107 return CmsVaadinUtils.getMessageText(Messages.GUI_CACHE_FLEXCACHE_LABEL_STATS_VARIATIONS_0); 108 } 109 110 /** 111 * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getVisibility(java.lang.Object) 112 */ 113 public CmsMenuItemVisibilityMode getVisibility(Set<String> data) { 114 115 return (data != null) && (data.size() == 1) 116 ? CmsMenuItemVisibilityMode.VISIBILITY_ACTIVE 117 : CmsMenuItemVisibilityMode.VISIBILITY_INVISIBLE; 118 } 119 } 120 121 /** 122 * The menu entry to switch to the explorer of concerning site.<p> 123 */ 124 class ExplorerEntry implements I_CmsSimpleContextMenuEntry<Set<String>> { 125 126 /** 127 * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#executeAction(java.lang.Object) 128 */ 129 public void executeAction(Set<String> data) { 130 131 CmsResource res; 132 try { 133 res = getRootCms().readResource(data.iterator().next()); 134 openExplorerForParent(res.getRootPath(), res.getStructureId().getStringValue()); 135 } catch (CmsException e) { 136 e.printStackTrace(); 137 } 138 139 } 140 141 /** 142 * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getTitle(java.util.Locale) 143 */ 144 public String getTitle(Locale locale) { 145 146 return Messages.get().getBundle(locale).key(Messages.GUI_EXPLORER_TITLE_0); 147 } 148 149 /** 150 * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getVisibility(java.lang.Object) 151 */ 152 public CmsMenuItemVisibilityMode getVisibility(Set<String> data) { 153 154 if (data == null) { 155 return CmsMenuItemVisibilityMode.VISIBILITY_INVISIBLE; 156 } 157 158 String res = data.iterator().next(); 159 160 if (!A_CmsUI.getCmsObject().getRequestContext().getSiteRoot().equals("")) { 161 if (!res.startsWith(A_CmsUI.getCmsObject().getRequestContext().getSiteRoot())) { 162 return CmsMenuItemVisibilityMode.VISIBILITY_INACTIVE; 163 } 164 } 165 166 return data.size() == 1 167 ? CmsMenuItemVisibilityMode.VISIBILITY_ACTIVE 168 : CmsMenuItemVisibilityMode.VISIBILITY_INVISIBLE; 169 } 170 171 } 172 173 /** 174 * Column for dimensions of image.<p> 175 */ 176 class VariationsColumn implements Table.ColumnGenerator { 177 178 /**vaadin serial id.*/ 179 private static final long serialVersionUID = -4569513960107614645L; 180 181 /** 182 * @see com.vaadin.ui.Table.ColumnGenerator#generateCell(com.vaadin.ui.Table, java.lang.Object, java.lang.Object) 183 */ 184 public Object generateCell(Table source, Object itemId, Object columnId) { 185 186 return Integer.valueOf(HELPER.getVariationsCount((String)itemId)); 187 } 188 189 } 190 191 /**Image cache helper instance. */ 192 protected static CmsImageCacheHolder HELPER; 193 194 /** The logger for this class. */ 195 static Log LOG = CmsLog.getLog(CmsImageCacheTable.class.getName()); 196 197 /**Column for icon.*/ 198 private static final String PROP_ICON = "icon"; 199 200 /**column for name of image.*/ 201 private static final String PROP_NAME = "name"; 202 203 /**column for image dimension.*/ 204 private static final String PROP_VARIATIONS = "variations"; 205 206 /**vaadin serial id.*/ 207 private static final long serialVersionUID = -5559186186646954045L; 208 209 /** The context menu. */ 210 CmsContextMenu m_menu; 211 212 /**Indexed container.*/ 213 private IndexedContainer m_container; 214 215 /**intro result view.*/ 216 private VerticalLayout m_intro; 217 218 /** The available menu entries. */ 219 private List<I_CmsSimpleContextMenuEntry<Set<String>>> m_menuEntries; 220 221 /**null result view. */ 222 private VerticalLayout m_nullResult; 223 224 /**CmsObject at root.*/ 225 private CmsObject m_rootCms; 226 227 /**Filter text field for table. */ 228 private TextField m_siteTableFilter; 229 230 /** 231 * public constructor.<p> 232 * @param nullResult vaadin component 233 * @param intro vaadin component 234 * @param siteTableFilter vaadin component 235 */ 236 public CmsImageCacheTable(VerticalLayout intro, VerticalLayout nullResult, TextField siteTableFilter) { 237 238 m_intro = intro; 239 m_nullResult = nullResult; 240 m_siteTableFilter = siteTableFilter; 241 242 //Set menu 243 m_menu = new CmsContextMenu(); 244 m_menu.setAsTableContextMenu(this); 245 246 //Setup container, sortable only for Name because other properties are loaded in background 247 m_container = new IndexedContainer() { 248 249 private static final long serialVersionUID = -8679153149897733835L; 250 251 @Override 252 public Collection<?> getSortableContainerPropertyIds() { 253 254 return Collections.singleton(PROP_NAME); 255 } 256 }; 257 258 m_container.addContainerProperty( 259 PROP_ICON, 260 Resource.class, 261 CmsResourceUtil.getBigIconResource( 262 OpenCms.getWorkplaceManager().getExplorerTypeSetting(CmsResourceTypeImage.getStaticTypeName()), 263 null)); 264 m_container.addContainerProperty(PROP_NAME, String.class, ""); 265 m_container.addContainerProperty(PROP_VARIATIONS, Integer.class, ""); 266 //ini Table 267 setContainerDataSource(m_container); 268 setColumnHeader(PROP_NAME, CmsVaadinUtils.getMessageText(Messages.GUI_CACHE_IMAGECACHE_LIST_COLS_RESOURCE_0)); 269 setColumnHeader( 270 PROP_VARIATIONS, 271 CmsVaadinUtils.getMessageText(Messages.GUI_CACHE_IMAGECACHE_LIST_COLS_VARIATIONS_0)); 272 273 setItemIconPropertyId(PROP_ICON); 274 setRowHeaderMode(RowHeaderMode.ICON_ONLY); 275 276 setColumnWidth(null, 40); 277 278 setSelectable(true); 279 280 addGeneratedColumn(PROP_VARIATIONS, new VariationsColumn()); 281 282 addItemClickListener(new ItemClickListener() { 283 284 private static final long serialVersionUID = -4738296706762013443L; 285 286 public void itemClick(ItemClickEvent event) { 287 288 setValue(null); 289 select(event.getItemId()); 290 291 //Right click or click on icon column (=null) -> show menu 292 if (event.getButton().equals(MouseButton.RIGHT) || (event.getPropertyId() == null)) { 293 m_menu.setEntries(getMenuEntries(), Collections.singleton((String)getValue())); 294 m_menu.openForTable(event, event.getItemId(), event.getPropertyId(), CmsImageCacheTable.this); 295 } 296 297 if (event.getButton().equals(MouseButton.LEFT) & PROP_NAME.equals(event.getPropertyId())) { 298 showVariationsWindow(((String)getValue())); 299 } 300 } 301 }); 302 303 setCellStyleGenerator(new CellStyleGenerator() { 304 305 private static final long serialVersionUID = 1L; 306 307 public String getStyle(Table source, Object itemId, Object propertyId) { 308 309 if (PROP_NAME.equals(propertyId)) { 310 return " " + OpenCmsTheme.HOVER_COLUMN; 311 } 312 313 return null; 314 } 315 }); 316 317 setColumnWidth(PROP_VARIATIONS, 100); 318 319 } 320 321 /** 322 * Filters the table according to given search string.<p> 323 * 324 * @param search string to be looked for. 325 */ 326 public void filterTable(String search) { 327 328 m_container.removeAllContainerFilters(); 329 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(search)) { 330 m_container.addContainerFilter(new Or(new SimpleStringFilter(PROP_NAME, search, true, false))); 331 } 332 if ((getValue() != null)) { 333 setCurrentPageFirstItemId(getValue()); 334 } 335 } 336 337 /** 338 * Loads the table.<p> 339 * 340 * @param search searchstring to be considered 341 */ 342 public void load(String search) { 343 344 HELPER = new CmsImageCacheHolder(search); 345 loadTable(); 346 } 347 348 /** 349 * Returns the available menu entries.<p> 350 * 351 * @return the menu entries 352 */ 353 List<I_CmsSimpleContextMenuEntry<Set<String>>> getMenuEntries() { 354 355 if (m_menuEntries == null) { 356 m_menuEntries = new ArrayList<I_CmsSimpleContextMenuEntry<Set<String>>>(); 357 m_menuEntries.add(new EntryVariations()); //Option for Variations 358 m_menuEntries.add(new ExplorerEntry()); 359 } 360 return m_menuEntries; 361 } 362 363 /** 364 * Returns a cms object at root-site.<p> 365 * 366 * @return cmsobject 367 */ 368 CmsObject getRootCms() { 369 370 try { 371 if (m_rootCms == null) { 372 373 m_rootCms = OpenCms.initCmsObject(A_CmsUI.getCmsObject()); 374 m_rootCms.getRequestContext().setSiteRoot(""); 375 } 376 } catch (CmsException e) { 377 // 378 } 379 return m_rootCms; 380 381 } 382 383 /** 384 * Opens the explorer for given path and selected resource.<p> 385 * 386 * @param rootPath to be opened 387 * @param uuid to be selected 388 */ 389 void openExplorerForParent(String rootPath, String uuid) { 390 391 String parentPath = CmsResource.getParentFolder(rootPath); 392 393 CmsAppWorkplaceUi.get().getNavigator().navigateTo( 394 CmsFileExplorerConfiguration.APP_ID 395 + "/" 396 + A_CmsUI.getCmsObject().getRequestContext().getCurrentProject().getUuid() 397 + "!!" 398 + A_CmsUI.getCmsObject().getRequestContext().getSiteRoot() 399 + "!!" 400 + parentPath.substring(A_CmsUI.getCmsObject().getRequestContext().getSiteRoot().length()) 401 + "!!" 402 + uuid 403 + "!!"); 404 } 405 406 /** 407 * Shows dialog for variations of given resource.<p> 408 * 409 * @param resource to show variations for 410 */ 411 void showVariationsWindow(String resource) { 412 413 final Window window = CmsBasicDialog.prepareWindow(DialogWidth.max); 414 CmsVariationsDialog variationsDialog = new CmsVariationsDialog(resource, new Runnable() { 415 416 public void run() { 417 418 window.close(); 419 420 } 421 422 }, Mode.ImageCache); 423 try { 424 CmsResource resourceObject = getRootCms().readResource(resource); 425 variationsDialog.displayResourceInfo(Collections.singletonList(resourceObject)); 426 } catch (CmsException e) { 427 // 428 } 429 window.setCaption(CmsVaadinUtils.getMessageText(Messages.GUI_CACHE_VIEW_FLEX_VARIATIONS_1, resource)); 430 window.setContent(variationsDialog); 431 A_CmsUI.get().addWindow(window); 432 window.center(); 433 } 434 435 /** 436 * Fills table with entries from image cache helper.<p> 437 */ 438 private void loadTable() { 439 440 m_nullResult.setVisible(false); 441 m_intro.setVisible(false); 442 setVisible(true); 443 m_siteTableFilter.setVisible(true); 444 445 m_container.removeAllItems(); 446 447 setVisibleColumns(PROP_NAME, PROP_VARIATIONS); 448 449 List<String> resources = HELPER.getAllCachedImages(); 450 451 for (String res : resources) { 452 Item item = m_container.addItem(res); 453 item.getItemProperty(PROP_NAME).setValue(res); 454 } 455 456 if (resources.size() == 0) { 457 m_nullResult.setVisible(true); 458 setVisible(false); 459 m_siteTableFilter.setVisible(false); 460 } 461 462 } 463 464}