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; 029 030import org.opencms.json.JSONArray; 031import org.opencms.json.JSONException; 032import org.opencms.json.JSONObject; 033import org.opencms.main.CmsLog; 034import org.opencms.ui.components.CmsResourceTableProperty; 035 036import java.io.Serializable; 037import java.util.ArrayList; 038import java.util.Collections; 039import java.util.List; 040import java.util.Map; 041 042import org.apache.commons.logging.Log; 043 044import com.google.common.collect.Lists; 045 046/** 047 * Stores the file explorer settings.<p> 048 */ 049public class CmsFileExplorerSettings implements Serializable, I_CmsAppSettings { 050 051 /** JSON key. */ 052 private static final String COLLAPSED_COLUMNS_KEY = "collapsed_collumns"; 053 054 /** Log instance for this class. */ 055 private static final Log LOG = CmsLog.getLog(CmsFileExplorerSettings.class); 056 057 /** The serial version id. */ 058 private static final long serialVersionUID = 1L; 059 060 /** JSON key. */ 061 private static final String SORT_COLUMN_KEY = "sort_column"; 062 063 /** JSON key. */ 064 private static final String SORT_ORDER_KEY = "sort_order"; 065 066 /** The collapsed column ids. */ 067 private List<CmsResourceTableProperty> m_collapsedColumns; 068 069 /** The sort order. */ 070 private boolean m_sortAscending; 071 072 /** The sort column id. */ 073 private CmsResourceTableProperty m_sortColumnId; 074 075 /** 076 * Constructor.<p> 077 * Will initialize the default settings.<p> 078 */ 079 public CmsFileExplorerSettings() { 080 // initialize with the default settings 081 m_sortColumnId = CmsResourceTableProperty.PROPERTY_RESOURCE_NAME; 082 m_sortAscending = true; 083 m_collapsedColumns = new ArrayList<CmsResourceTableProperty>(); 084 Collections.addAll( 085 m_collapsedColumns, 086 CmsResourceTableProperty.PROPERTY_NAVIGATION_TEXT, 087 CmsResourceTableProperty.PROPERTY_PERMISSIONS, 088 CmsResourceTableProperty.PROPERTY_USER_MODIFIED, 089 CmsResourceTableProperty.PROPERTY_DATE_CREATED, 090 CmsResourceTableProperty.PROPERTY_USER_CREATED, 091 CmsResourceTableProperty.PROPERTY_STATE_NAME, 092 CmsResourceTableProperty.PROPERTY_USER_LOCKED); 093 } 094 095 /** 096 * Returns the collapsed column ids.<p> 097 * 098 * @return the collapsed column ids 099 */ 100 public List<CmsResourceTableProperty> getCollapsedColumns() { 101 102 return m_collapsedColumns; 103 } 104 105 /** 106 * @see org.opencms.ui.apps.I_CmsAppSettings#getSettingsString() 107 */ 108 public String getSettingsString() { 109 110 JSONObject json = new JSONObject(); 111 try { 112 json.put(SORT_ORDER_KEY, m_sortAscending); 113 json.put(SORT_COLUMN_KEY, m_sortColumnId.getId()); 114 List<String> collapsed = Lists.newArrayList(); 115 for (CmsResourceTableProperty column : m_collapsedColumns) { 116 collapsed.add(column.getId()); 117 } 118 json.put(COLLAPSED_COLUMNS_KEY, new JSONArray(collapsed)); 119 } catch (JSONException e) { 120 LOG.error(e.getLocalizedMessage(), e); 121 } 122 123 return json.toString(); 124 } 125 126 /** 127 * Gets the sort column id.<p> 128 * 129 * @return the sort column id 130 */ 131 public CmsResourceTableProperty getSortColumnId() { 132 133 return m_sortColumnId; 134 } 135 136 /** 137 * Returns the sort order.<p> 138 * 139 * @return the sort order 140 */ 141 public boolean isSortAscending() { 142 143 return m_sortAscending; 144 } 145 146 /** 147 * @see org.opencms.ui.apps.I_CmsAppSettings#restoreSettings(java.lang.String) 148 */ 149 public void restoreSettings(String storedSettings) { 150 151 Map<String, CmsResourceTableProperty> columnMap = CmsResourceTableProperty.getDefaultColumnsByName(); 152 153 try { 154 JSONObject json = new JSONObject(storedSettings); 155 if (json.has(SORT_ORDER_KEY)) { 156 m_sortAscending = json.getBoolean(SORT_ORDER_KEY); 157 } 158 if (json.has(SORT_COLUMN_KEY)) { 159 m_sortColumnId = columnMap.get(json.getString(SORT_COLUMN_KEY)); 160 } 161 if (json.has(COLLAPSED_COLUMNS_KEY)) { 162 List<CmsResourceTableProperty> collapsed = new ArrayList<CmsResourceTableProperty>(); 163 JSONArray array = json.getJSONArray(COLLAPSED_COLUMNS_KEY); 164 165 for (int i = 0; i < array.length(); i++) { 166 collapsed.add(columnMap.get(array.getString(i))); 167 } 168 m_collapsedColumns = collapsed; 169 } 170 171 } catch (JSONException e) { 172 LOG.error("Failed to restore file explorer settings from '" + storedSettings + "'", e); 173 } 174 } 175 176 /** 177 * Sets the collapsed columns.<p> 178 * 179 * @param collapsedColumns the collapsed columns 180 */ 181 public void setCollapsedColumns(List<CmsResourceTableProperty> collapsedColumns) { 182 183 m_collapsedColumns = collapsedColumns; 184 } 185 186 /** 187 * Sets the sort order.<p> 188 * 189 * @param sortAscending the sort order 190 */ 191 public void setSortAscending(boolean sortAscending) { 192 193 m_sortAscending = sortAscending; 194 } 195 196 /** 197 * Sets the sort column.<p> 198 * 199 * @param sortColumnId the sort column 200 */ 201 public void setSortColumnId(CmsResourceTableProperty sortColumnId) { 202 203 m_sortColumnId = sortColumnId; 204 } 205}