Hey,

I do a lot of prototyping in GWT before I build the backend and I've
recently had the need to generate a "build version" for my GWT
interface.  While there are about fiftybazillionmillion different ways
to do this, I've chosen a GWT generator to create a buildstamp.  This
lets me tar up the www results of clicking on "Compile/Browse" and
fire them off for others to play.  Code follows, wiring and example
proceed it.  Imports omitted, use your favourite IDE to fill them in.
This makes a pretty simple Hello World example. Comments are more than
welcome.

-- 8< -- begin -- 8< --
package org.example.rebind;

public class BuildStampGenerator extends Generator {

        private String className;
        private String packageName;

        @Override
        public String generate(TreeLogger logger, GeneratorContext context,
String typeName)
                        throws UnableToCompleteException {

                try {

                        TypeOracle typeOracle = context.getTypeOracle();
                        JClassType classType = typeOracle.getType(typeName);
                        packageName = classType.getPackage().getName();
                        className = classType.getSimpleSourceName() + "Impl";

                        generateClass(logger, context);

                } catch (Exception e) {
                        logger.log(TreeLogger.ERROR, "Exception during 
BuildStamp
creation.", e);
                }

                return packageName + "." + className;

        }

        private void generateClass(TreeLogger logger, GeneratorContext
context) {

                PrintWriter printWriter = context.tryCreate(logger, packageName,
className);

                /* Returns null if the package has already been generated. */
                if (printWriter == null) {
                        return;
                }

                ClassSourceFileComposerFactory composer = new
ClassSourceFileComposerFactory(packageName, className);

                // shouldn't magic just happen here?
        
composer.addImplementedInterface(BuildStamp.class.getCanonicalName());

                SourceWriter sourceWriter = composer.createSourceWriter(context,
printWriter);

                writeGetBuildStamp(sourceWriter);

                sourceWriter.outdent();
                sourceWriter.println("}");

                context.commit(logger, printWriter);

        }

        /**
         * Write the source code for [EMAIL PROTECTED] 
BuildStamp#getBuildStamp()}.
         */
        private void writeGetBuildStamp(SourceWriter sourceWriter) {
                sourceWriter.println("public String getBuildStamp() {");
                sourceWriter.println(" return \"" +
Generator.escape(getBuildStamp()) + "\";");
                sourceWriter.println("}");
        }

        /**
         * Executed at *compile* time, this generates a string that is
embedded in the
         * generated class. In hosted mode, this code is rebuilt each
Refresh.
         *
         * @return A user-friendly build string, good for hiding in tooltips.
         */
        String getBuildStamp() {
                StringBuilder msg = new StringBuilder();
                msg.append("Built on ");
                try {
                        String hostname =
InetAddress.getLocalHost().getCanonicalHostName();
                        msg.append(hostname);
                } catch (UnknownHostException e) {
                        msg.append("(hostname not available)");
                }
                msg.append(" at ");
                msg.append(new Date());
                return msg.toString();
        }
}

-- 8< -- end -- 8< --

You will need a BuildStamp interface:

package org.example.client;
public interface BuildStamp { public String getBuildStamp(); }

And wire it up in your .gwt.xml:

        <generate-with class="org.example.rebind.BuildStampGenerator">
                <when-type-assignable class="org.example.client.BuildStamp" />
        </generate-with>

Then serve:
                ...
                BuildStamp buildStamp = GWT.create(BuildStamp.class);
                yourLogo.setTitle(buildStamp.getBuildStamp());
                ...

Much of this is borrowed from this forum post:
http://groups.google.com/group/Google-Web-Toolkit/msg/ae249ea67c2c3435

Cheers,

   JAmes


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To post to this group, send email to Google-Web-Toolkit@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to