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.rebind; 029 030import java.io.PrintWriter; 031import java.util.ArrayList; 032import java.util.List; 033 034import com.google.gwt.core.ext.Generator; 035import com.google.gwt.core.ext.GeneratorContext; 036import com.google.gwt.core.ext.TreeLogger; 037import com.google.gwt.core.ext.UnableToCompleteException; 038import com.google.gwt.core.ext.typeinfo.JClassType; 039import com.google.gwt.core.ext.typeinfo.JMethod; 040import com.google.gwt.core.ext.typeinfo.JType; 041import com.google.gwt.core.ext.typeinfo.NotFoundException; 042import com.google.gwt.core.ext.typeinfo.TypeOracle; 043import com.google.gwt.user.rebind.ClassSourceFileComposerFactory; 044import com.google.gwt.user.rebind.SourceWriter; 045 046/** 047 * Context menu command init generator.<p> 048 * 049 * @since version 8.0.1 050 */ 051public class CmsCommandInitGenerator extends Generator { 052 053 /** The name of the class to generate. */ 054 private static final String CLASS_NAME = "CmsContextMenuCommandInitializer"; 055 056 /** The name of the interface passed into GWT.create(). */ 057 private static final String INIT_INTERFACE_NAME = "org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuCommandInitializer"; 058 059 /** The name of the marker interface for the generator. */ 060 private static final String MARKER_INTERFACE_NAME = "org.opencms.gwt.client.ui.contextmenu.I_CmsHasContextMenuCommand"; 061 062 /** The name of the context menu command interface. */ 063 private static final String COMMAND_INTERFACE = "org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuCommand"; 064 065 /** The name of the get context menu command method. */ 066 private static final String GET_COMMAND_METHOD = "getContextMenuCommand"; 067 068 /** The package of the class to generate. */ 069 private static final String PACKAGE_NAME = "org.opencms.gwt.client"; 070 071 /** 072 * @see com.google.gwt.core.ext.Generator#generate(com.google.gwt.core.ext.TreeLogger, com.google.gwt.core.ext.GeneratorContext, java.lang.String) 073 */ 074 @Override 075 public String generate(TreeLogger logger, GeneratorContext context, String typeName) 076 throws UnableToCompleteException { 077 078 TypeOracle oracle = context.getTypeOracle(); 079 JClassType initClass = oracle.findType(MARKER_INTERFACE_NAME); 080 List<JClassType> initTypes = new ArrayList<JClassType>(); 081 for (JClassType subtype : initClass.getSubtypes()) { 082 try { 083 JMethod method = subtype.getMethod(GET_COMMAND_METHOD, new JType[] {}); 084 if (!method.isStatic()) { 085 throw new NotFoundException(); 086 } 087 initTypes.add(subtype); 088 } catch (NotFoundException e) { 089 logger.log( 090 TreeLogger.ERROR, 091 "Could not find " + GET_COMMAND_METHOD + "() method in class " + subtype.getQualifiedSourceName()); 092 throw new UnableToCompleteException(); 093 } 094 } 095 generateClass(logger, context, initTypes); 096 return PACKAGE_NAME + "." + CLASS_NAME; 097 } 098 099 /** 100 * This method generates the source code for the class initializer class.<p> 101 * 102 * @param logger the logger to be used 103 * @param context the generator context 104 * @param subclasses the classes for which the generated code should the initClass() method 105 */ 106 public void generateClass(TreeLogger logger, GeneratorContext context, List<JClassType> subclasses) { 107 108 PrintWriter printWriter = context.tryCreate(logger, PACKAGE_NAME, CLASS_NAME); 109 if (printWriter == null) { 110 return; 111 } 112 ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(PACKAGE_NAME, CLASS_NAME); 113 composer.addImplementedInterface(INIT_INTERFACE_NAME); 114 SourceWriter sourceWriter = composer.createSourceWriter(context, printWriter); 115 sourceWriter.println("public java.util.Map<String, " + COMMAND_INTERFACE + "> initCommands() {"); 116 sourceWriter.indent(); 117 sourceWriter.println( 118 "java.util.Map<String, " 119 + COMMAND_INTERFACE 120 + "> result=new java.util.HashMap<String, " 121 + COMMAND_INTERFACE 122 + ">();"); 123 for (JClassType type : subclasses) { 124 sourceWriter.println( 125 "result.put(\"" 126 + type.getQualifiedSourceName() 127 + "\"," 128 + type.getQualifiedSourceName() 129 + "." 130 + GET_COMMAND_METHOD 131 + "());"); 132 } 133 sourceWriter.println("return result;"); 134 sourceWriter.outdent(); 135 sourceWriter.println("}"); 136 sourceWriter.outdent(); 137 sourceWriter.println("}"); 138 context.commit(logger, printWriter); 139 } 140}