/* * Copyright 2008-2010 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and */ import org.codehaus.groovy.grails.plugins.support.GrailsPluginUtils import org.codehaus.groovy.grails.commons.ControllerArtefactHandler import org.apache.commons.logging.LogFactory import org.codehaus.groovy.grails.plugins.j2d.Gfx /** * Enables graphics capabilities on controllers.
* Contains code from JSecurityGrailsPlugin by Peter Ledbrook. * * @author Andres Almiray */ class J2dGrailsPlugin { static LOG = LogFactory.getLog("org.codehaus.groovy.grails.plugins.j2d.J2dGrailsPlugin") def version = "0.4.2" def grailsVersion = "1.2.0 > *" def author = "Andres Almiray" def authorEmail = "aalmiray@users.sourceforge.net" def title = "Enables Java2D rendering using GfxBuilder" def description = """ J2D plugin allows you to draw images on the fly with Java2D, using GfxBuilder under the covers (http://griffon.codehaus.org/GfxBuilder) """ def documentation = "http://grails.org/J2D+Plugin" def dependsOn = [core:grailsVersion] def loadAfter = ['controllers'] def observe = ['controllers'] def doWithSpring = { // TODO Implement runtime spring config (optional) } def doWithApplicationContext = { applicationContext -> // TODO Implement post initialization spring config (optional) } def doWithWebDescriptor = { xml -> // TODO Implement additions to web.xml (optional) } /** * Adds renderImage() to controllers. */ def doWithDynamicMethods = { ctx -> // following snippet taken from JSecurityGrailsPlugin if( manager?.hasGrailsPlugin("controllers") ){ application.controllerClasses.each { controllerClass -> LOG.info "Adding gfx capabilities to ${controllerClass.clazz.name}" processController(controllerClass) } } } def onChange = { event -> // following snippet taken from JSecurityGrailsPlugin if( application.isControllerClass(event.source) ){ // Get the GrailsClass instance for the controller. def controllerClass = application.getControllerClass(event.source?.name) // If no GrailsClass can be found, i.e. 'controllerClass' // is null, then this is a new controller. if( controllerClass == null ){ controllerClass = application.addArtefact(ControllerArtefactHandler.TYPE, event.source) } // Now update dynamic methods for this controller. LOG.info "Reconfiguring gfx capabilities on ${controllerClass.shortName}" processController(controllerClass) } } def onConfigChange = { event -> // TODO Implement code that is executed when the project configuration changes. // The event is the same as for 'onChange'. } def processController(controllerClass) { controllerClass.metaClass.renderImage = { Map props, Closure cls -> Gfx.renderImage(delegate, props, cls) } } }