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.util; 029 030import org.opencms.ade.contenteditor.shared.CmsEditorConstants; 031 032import java.util.HashMap; 033import java.util.Map; 034 035/** 036 * Keeps track of the actions which should be executed for each collector context id.<p> 037 */ 038public class CmsNewLinkFunctionTable { 039 040 /** The global instance for this class. */ 041 public static final CmsNewLinkFunctionTable INSTANCE = new CmsNewLinkFunctionTable(); 042 043 /** 044 * The map from collector context ids to the corresponding actions.<p> 045 */ 046 private Map<String, Runnable> m_createFunctions = new HashMap<String, Runnable>(); 047 048 /** 049 * Creates a new instance.<p> 050 */ 051 public CmsNewLinkFunctionTable() { 052 053 installFunction(CmsEditorConstants.FUNCTION_CREATE_NEW); 054 } 055 056 /** 057 * Static method for used to create a new element based on a collector's context id and open the editor for id.<p> 058 * 059 * @param contextId the collector context id 060 */ 061 public static void createAndEditNewElementStatic(String contextId) { 062 063 INSTANCE.createAndEditNewElement(contextId); 064 } 065 066 /** 067 * Helper method for logging.<p> 068 * 069 * @param s the string to log 070 */ 071 private static native void log(String s) /*-{ 072 if ($wnd.console && $wnd.console.log) { 073 $wnd.console.log(s); 074 } 075 }-*/; 076 077 /** 078 * Triggers creation and editing of a new element for the given collector, identified by its context id.<p> 079 * 080 * @param contextId the context id of the collector 081 */ 082 public void createAndEditNewElement(String contextId) { 083 084 Runnable action = m_createFunctions.get(contextId); 085 if (action != null) { 086 action.run(); 087 } else { 088 log("Could not execute create action for context id '" + contextId + "'"); 089 } 090 } 091 092 /** 093 * Installs a Javascript function which can be used to create and edit a new element given the collector context id.<p> 094 * 095 * @param functionName the name that should be used for the function 096 */ 097 public native void installFunction(String functionName) /*-{ 098 $wnd[functionName] = function(s) { 099 @org.opencms.gwt.client.util.CmsNewLinkFunctionTable::createAndEditNewElementStatic(Ljava/lang/String;)(s); 100 } 101 }-*/; 102 103 /** 104 * Sets the action which should be executed if cmsCreateAndEditNewElement is called with the given collector context id.<p> 105 * 106 * @param contextId the collector context id 107 * @param action the action to execute 108 */ 109 public void setHandler(String contextId, Runnable action) { 110 111 if (contextId != null) { 112 m_createFunctions.put(contextId, action); 113 } 114 } 115 116}