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.gwt.client;
029
030import org.opencms.ade.contenteditor.shared.CmsEditorConstants;
031import org.opencms.gwt.client.util.CmsEditableDataUtil;
032import org.opencms.gwt.shared.CmsGwtConstants;
033import org.opencms.gwt.shared.I_CmsEditableDataExtensions;
034import org.opencms.util.CmsUUID;
035
036import com.google.gwt.core.client.JavaScriptObject;
037
038/**
039 * Javascript overlay object holding data needed to edit a content collector list element.<p>
040 *
041 * @since 8.0.0
042 */
043public final class CmsEditableDataJSO extends JavaScriptObject implements I_CmsEditableData {
044
045    /**
046     * Constructor.<p>
047     */
048    protected CmsEditableDataJSO() {
049
050        // nothing to do
051    }
052
053    /**
054     * Parses the given JSON text and returns the editable data object.<p>
055     *
056     * @param jsonText the JSON text to parse
057     *
058     * @return the data object
059     */
060    public static native CmsEditableDataJSO parseEditableData(String jsonText) /*-{
061        return JSON.parse(jsonText);
062    }-*/;
063
064    /**
065     * Returns true if the element can be favorited.
066     *
067     * @return true if the element can be favorited
068     */
069    public boolean canFavorite() {
070
071        return getBoolean(CmsGwtConstants.ATTR_FAVORITE);
072    }
073
074    /**
075     * @see org.opencms.gwt.client.I_CmsEditableData#getContextId()
076     */
077    public String getContextId() {
078
079        return getString(CmsEditorConstants.ATTR_CONTEXT_ID);
080    }
081
082    /**
083     * @see org.opencms.gwt.client.I_CmsEditableData#getEditId()
084     */
085    public native String getEditId() /*-{
086        return this.editId ? this.editId : "";
087    }-*/;
088
089    /**
090     * @see org.opencms.gwt.client.I_CmsEditableData#getElementId()
091     */
092    public String getElementId() {
093
094        return getString(CmsGwtConstants.ATTR_ELEMENT_ID);
095    }
096
097    /**
098     * @see org.opencms.gwt.client.I_CmsEditableData#getElementLanguage()
099     */
100    public native String getElementLanguage() /*-{
101        return this.elementlanguage ? this.elementlanguage : "";
102    }-*/;
103
104    /**
105     * @see org.opencms.gwt.client.I_CmsEditableData#getElementName()
106     */
107    public native String getElementName() /*-{
108        return this.elementname ? this.elementname : "";
109    }-*/;
110
111    /**
112     * Gets the element view.<p>
113     *
114     * @return the element view
115     */
116    public CmsUUID getElementView() {
117
118        String elementViewString = getString(CmsEditorConstants.ATTR_ELEMENT_VIEW);
119        if (elementViewString == null) {
120            return null;
121        }
122        return new CmsUUID(elementViewString);
123    }
124
125    /**
126     * @see org.opencms.gwt.client.I_CmsEditableData#getExtensions()
127     */
128    public I_CmsEditableDataExtensions getExtensions() {
129
130        if (getStoredExtensions() == null) {
131            String extensionsJson = getString(CmsGwtConstants.ATTR_EXTENSIONS);
132            if (extensionsJson == null) {
133                extensionsJson = "{}";
134            }
135            I_CmsEditableDataExtensions extensions = CmsEditableDataUtil.parseExtensions(extensionsJson);
136            setStoredExtensions(extensions);
137        }
138        return getStoredExtensions();
139    }
140
141    /**
142     * @see org.opencms.gwt.client.I_CmsEditableData#getNewLink()
143     */
144    public native String getNewLink() /*-{
145        return this.newlink ? this.newlink : "";
146    }-*/;
147
148    /**
149     * @see org.opencms.gwt.client.I_CmsEditableData#getNewTitle()
150     */
151    public native String getNewTitle() /*-{
152        return this.newtitle ? this.newtitle : "";
153    }-*/;
154
155    /**
156     * @see org.opencms.gwt.client.I_CmsEditableData#getNoEditReason()
157     */
158    public native String getNoEditReason() /*-{
159
160        if (this.noEditReason)
161            return this.noEditReason;
162        else
163            return null;
164    }-*/;
165
166    /**
167     * @see org.opencms.gwt.client.I_CmsEditableData#getPostCreateHandler()
168     */
169    public String getPostCreateHandler() {
170
171        return getString(CmsEditorConstants.PARAM_POST_CREATE_HANDLER);
172    }
173
174    /**
175     * @see org.opencms.gwt.client.I_CmsEditableData#getSitePath()
176     */
177    public native String getSitePath() /*-{
178        return this.sitePath ? this.sitePath : "";
179    }-*/;
180
181    /**
182     * @see org.opencms.gwt.client.I_CmsEditableData#getStructureId()
183     */
184    public CmsUUID getStructureId() {
185
186        return new CmsUUID(nativeGetStructureId());
187    }
188
189    /**
190     * Returns if the delete button should be present.<p>
191     *
192     * @return <code>true</code> if the delete button should be present
193     */
194    public native boolean hasDelete() /*-{
195        return this.hasDelete;
196    }-*/;
197
198    /**
199     * Returns if the edit button should be present.<p>
200     *
201     * @return <code>true</code> if the edit button should be present
202     */
203    public native boolean hasEdit() /*-{
204        return this.hasEdit;
205    }-*/;
206
207    /**
208     * @see org.opencms.gwt.client.I_CmsEditableData#hasEditHandler()
209     */
210    public native boolean hasEditHandler() /*-{
211
212        return this.hasEditHandler;
213    }-*/;
214
215    /**
216     * Returns if the new button should be present.<p>
217     *
218     * @return <code>true</code> if the new button should be present
219     */
220    public native boolean hasNew() /*-{
221        return this.hasNew;
222    }-*/;
223
224    /**
225     * @see org.opencms.gwt.client.I_CmsEditableData#hasResource()
226     */
227    public native boolean hasResource() /*-{
228        return this.hasResource;
229    }-*/;
230
231    /**
232     * @see org.opencms.gwt.client.I_CmsEditableData#isUnreleasedOrExpired()
233     */
234    public native boolean isUnreleasedOrExpired() /*-{
235        return this.unreleaseOrExpired;
236    }-*/;
237
238    /**
239     * @see org.opencms.gwt.client.I_CmsEditableData#setSitePath(java.lang.String)
240     */
241    public native void setSitePath(String sitePath) /*-{
242
243        this.sitePath = sitePath;
244    }-*/;
245
246    /**
247     * Reads an attribute of the underlying Javascript object as a string.<p>
248     *
249     * @param attribute the name of the attribute
250     *
251     * @return the string contained in the given attribute
252     */
253    private native boolean getBoolean(String attribute) /*-{
254        return this[attribute];
255    }-*/;
256
257    /**
258     * Gets the cached extensions.
259     *
260     * @return the cached extensions
261     */
262    private native I_CmsEditableDataExtensions getStoredExtensions() /*-{
263        return this._ext;
264    }-*/;
265
266    /**
267     * Reads an attribute of the underlying Javascript object as a string.<p>
268     *
269     * @param attribute the name of the attribute
270     *
271     * @return the string contained in the given attribute
272     */
273    private native String getString(String attribute) /*-{
274        return this[attribute];
275    }-*/;
276
277    /**
278     * Returns the structure id as string.<p>
279     *
280     * @return the structure id as string
281     */
282    private native String nativeGetStructureId() /*-{
283        return this.structureId ? this.structureId : "";
284    }-*/;
285
286    /**
287     * Sets the cached extensions.
288     *
289     * @param extensions the cached extensions
290     */
291    private native void setStoredExtensions(I_CmsEditableDataExtensions extensions) /*-{
292        this._ext = extensions;
293    }-*/;
294
295}