jdk/make/src/classes/build/tools/generatenimbus/Generator.java
author mseledtsov
Wed, 17 May 2017 11:28:56 -0700
changeset 46220 c9a98ebe678b
parent 23010 6dadb192ad81
permissions -rw-r--r--
8180393: [TESTBUG] CDSTestUtils property test.cds.copy.child.stdout should be true by default Summary: Changed default value of test.cds.copy.child.stdout to true Reviewed-by: hseigel, iklam, sspitsyn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3742
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21805
diff changeset
     2
 * Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
3742
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
     4
 *
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3742
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
3742
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3742
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
3742
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    10
 *
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    15
 * accompanied this code).
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    16
 *
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3742
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3742
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3742
diff changeset
    23
 * questions.
3742
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    24
 */
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    25
package build.tools.generatenimbus;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    26
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    27
import java.io.*;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    28
import java.util.HashMap;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    29
import java.util.Map;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    30
import javax.xml.bind.JAXBContext;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    31
import javax.xml.bind.Unmarshaller;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    32
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    33
/**
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    34
 * Generates the various Java artifacts based on a SynthModel.
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    35
 * <p/>
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    36
 * Generated source files are split up among two different locations. There are those source files that are meant to be
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    37
 * edited (generally, only the LookAndFeel class itself) and those that are autogenerated (everything else).
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    38
 * <p/>
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    39
 * All autogenerated files are placed in "buildPackageRoot" and are package private. A LAF author (one who has access to
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    40
 * the generated sources) will be able to access any of the generated classes. Those referencing the library, however,
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    41
 * will only be able to access the main LookAndFeel class itself (since everything else is package private).
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    42
 *
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    43
 * @author  Richard Bair
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    44
 * @author  Jasper Potts
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    45
 */
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    46
public class Generator {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    47
    private static Generator instance;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    48
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    49
    /** A map of variables that are used for variable substitution in the template files. */
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    50
    private Map<String, String> variables;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    51
    private boolean full = false;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    52
    private File buildPackageRoot;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    53
    private String packageNamePrefix;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    54
    private String lafName;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    55
    private SynthModel model;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    56
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    57
    /**
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    58
     * MAIN APPLICATION
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    59
     * <p/>
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    60
     * This is for using the generator as part of the java build process
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    61
     *
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    62
     * @param args The commandline arguments
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    63
     */
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    64
    public static void main(String[] args) throws Exception {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    65
        if (args.length == 0 || (args.length % 2) != 0) {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    66
            System.out.println("Usage: generator [-options]\n" +
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    67
                    "    -full <true|false>     True if we should build the whole LAF or false for building just states and painters.\n" +
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    68
                    "    -skinFile <value>      Path to the skin.laf file for the LAF to be generated from.\n" +
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    69
                    "    -buildDir <value>      The directory beneath which the build-controlled artifacts (such as the Painters) should\n" +
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    70
                    "                           be placed. This is the root directory beneath which the necessary packages and source\n" +
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    71
                    "                           files will be created.\n" +
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    72
                    "    -resourcesDir <value>  The resources directory containing templates and images.\n" +
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    73
                    "    -packagePrefix <value> The package name associated with this synth look and feel. For example,\n" +
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    74
                    "                           \"org.mypackage.mylaf\"\n" +
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    75
                    "    -lafName <value>       The name of the laf, such as \"MyLAF\".\n");
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    76
        } else {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    77
            boolean full = false;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    78
            File skinFile = new File(System.getProperty("user.dir"));
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    79
            File buildDir = new File(System.getProperty("user.dir"));
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    80
            File resourcesDir = new File(System.getProperty("user.dir"));
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    81
            String packagePrefix = "org.mypackage.mylaf";
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    82
            String lafName = "MyLAF";
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    83
            for (int i = 0; i < args.length; i += 2) {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    84
                String key = args[i].trim().toLowerCase();
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    85
                String value = args[i + 1].trim();
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    86
                if ("-full".equals(key)) {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    87
                    full = Boolean.parseBoolean(value);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    88
                } else if ("-skinfile".equals(key)) {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    89
                    skinFile = new File(value);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    90
                } else if ("-builddir".equals(key)) {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    91
                    buildDir = new File(value);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    92
                } else if ("-resourcesdir".equals(key)) {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    93
                    resourcesDir = new File(value);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    94
                } else if ("-packageprefix".equals(key)) {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    95
                    packagePrefix = value;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    96
                } else if ("-lafname".equals(key)) {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    97
                    lafName = value;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    98
                }
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    99
            }
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   100
            System.out.println("### GENERATING LAF CODE ################################");
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   101
            System.out.println("   full          :" + full);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   102
            System.out.println("   skinFile      :" + skinFile.getAbsolutePath());
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   103
            System.out.println("   buildDir      :" + buildDir.getAbsolutePath());
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   104
            System.out.println("   resourcesDir  :" + resourcesDir.getAbsolutePath());
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   105
            System.out.println("   packagePrefix :" +packagePrefix);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   106
            System.out.println("   lafName       :" +lafName);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   107
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   108
            JAXBContext ctx = JAXBContext.newInstance("build.tools.generatenimbus");
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   109
            Unmarshaller u = ctx.createUnmarshaller();
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   110
            SynthModel model = (SynthModel) u.unmarshal(skinFile);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   111
            Generator.init(full, buildDir, packagePrefix, lafName, model);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   112
            Generator.getInstance().generate();
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   113
        }
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   114
    }
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   115
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   116
    /**
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   117
     * Creates a new Generator, capable of outputting the source code artifacts related to a given SynthModel. It is
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   118
     * capable of generating the one-time artifacts in addition to the regeneration of build-controlled artifacts.
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   119
     *
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   120
     * @param full              True if we should build the whole LAF or false for building just states and painters.
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   121
     * @param buildDir          The directory beneath which the build-controlled artifacts (such as the Painters) should
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   122
     *                          be placed. This is the root directory beneath which the necessary packages and source
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   123
     *                          files will be created.
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   124
     * @param srcDir            The directory beneath which the normal user-controlled artifacts (such as the core
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   125
     *                          LookAndFeel file) should be placed. These are one-time generated files. This is the root
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   126
     *                          directory beneath which the necessary packages and source files will be created.
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   127
     * @param packageNamePrefix The package name associated with this synth look and feel. For example,
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   128
     *                          org.mypackage.mylaf
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   129
     * @param lafName           The name of the laf, such as MyLAF.
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   130
     * @param model             The actual SynthModel to base these generated files on.
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   131
     */
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   132
    private Generator(boolean full, File buildDir,
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   133
            String packageNamePrefix, String lafName, SynthModel model) {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   134
        this.full = full;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   135
        //validate the input variables
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   136
        if (packageNamePrefix == null) {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   137
            throw new IllegalArgumentException("You must specify a package name prefix");
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   138
        }
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   139
        if (buildDir == null) {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   140
            throw new IllegalArgumentException("You must specify the build directory");
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   141
        }
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   142
        if (model == null) {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   143
            throw new IllegalArgumentException("You must specify the SynthModel");
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   144
        }
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   145
        if (lafName == null) {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   146
            throw new IllegalArgumentException("You must specify the name of the look and feel");
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   147
        }
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   148
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   149
        //construct the map which is used to do variable substitution of the template
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   150
        //files
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   151
        variables = new HashMap<String, String>();
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   152
        variables.put("PACKAGE", packageNamePrefix);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   153
        variables.put("LAF_NAME", lafName);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   154
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   155
        //generate and save references to the package-root directories.
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   156
        //(That is, given the buildDir and srcDir, generate references to the
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   157
        //org.mypackage.mylaf subdirectories)
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   158
        buildPackageRoot = new File(buildDir, packageNamePrefix.replaceAll("\\.", "\\/"));
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   159
        buildPackageRoot.mkdirs();
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   160
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   161
        //save the variables
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   162
        this.packageNamePrefix = packageNamePrefix;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   163
        this.lafName = lafName;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   164
        this.model = model;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   165
    }
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   166
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   167
    public static void init(boolean full, File buildDir,
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   168
            String packageNamePrefix, String lafName, SynthModel model) {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   169
        instance = new Generator(full, buildDir, packageNamePrefix, lafName, model);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   170
        model.initStyles();
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   171
    }
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   172
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   173
    public static Generator getInstance() {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   174
        return instance;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   175
    }
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   176
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   177
    public static Map<String, String> getVariables() {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   178
        return new HashMap<String, String>(instance.variables);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   179
    }
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   180
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   181
    public void generate() {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   182
        if (full) {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   183
            //create the LookAndFeel file
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   184
            writeSrcFileImpl("LookAndFeel", variables, lafName + "LookAndFeel");
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   185
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   186
            writeSrcFileImpl("AbstractRegionPainter", variables);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   187
            writeSrcFileImpl("BlendingMode", variables);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   188
            writeSrcFileImpl("SynthPainterImpl", variables);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   189
            writeSrcFileImpl("IconImpl", variables, lafName + "Icon.java");
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   190
            writeSrcFileImpl("StyleImpl", variables, lafName + "Style.java");
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   191
            writeSrcFileImpl("Effect", variables);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   192
            writeSrcFileImpl("EffectUtils", variables);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   193
            writeSrcFileImpl("ShadowEffect", variables);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   194
            writeSrcFileImpl("DropShadowEffect", variables);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   195
            writeSrcFileImpl("InnerShadowEffect", variables);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   196
            writeSrcFileImpl("InnerGlowEffect", variables);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   197
            writeSrcFileImpl("OuterGlowEffect", variables);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   198
            writeSrcFileImpl("State", variables);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   199
            writeSrcFileImpl("ImageCache", variables);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   200
            writeSrcFileImpl("ImageScalingHelper", variables);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   201
        }
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   202
        //next, populate the first set of ui defaults based on what is in the
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   203
        //various palettes of the synth model
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   204
        StringBuilder defBuffer = new StringBuilder();
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   205
        StringBuilder styleBuffer = new StringBuilder();
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   206
        model.write(defBuffer, styleBuffer, packageNamePrefix);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   207
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   208
        Map<String, String> vars = getVariables();
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   209
        vars.put("UI_DEFAULT_INIT", defBuffer.toString());
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   210
        vars.put("STYLE_INIT", styleBuffer.toString());
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   211
        writeSrcFile("Defaults", vars, lafName + "Defaults");
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   212
    }
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   213
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   214
    private void writeSrcFileImpl(String name, Map<String, String> variables) {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   215
        writeSrcFileImpl(name, variables, name);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   216
    }
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   217
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   218
    private void writeSrcFileImpl(String templateName,
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   219
            Map<String, String> variables, String outputName) {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   220
        PrintWriter out = null;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   221
        try {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   222
            InputStream stream = getClass().getResourceAsStream(
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   223
                    "resources/" + templateName + ".template");
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   224
            TemplateReader in = new TemplateReader(variables, stream);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   225
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   226
            out = new PrintWriter(new File(buildPackageRoot, outputName + ".java"));
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   227
            String line = in.readLine();
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   228
            while (line != null) {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   229
                out.println(line);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   230
                line = in.readLine();
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   231
            }
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   232
        } catch (IOException e) {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   233
            throw new RuntimeException("IOException in writer", e);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   234
        } finally {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   235
            if (out != null) out.close();
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   236
        }
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   237
    }
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   238
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   239
    public static void writeSrcFile(String templateName,
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   240
            Map<String, String> variables, String outputName) {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   241
        instance.writeSrcFileImpl(templateName, variables, outputName);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   242
    }
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   243
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   244
    /** A BufferedReader implementation that automatically performs
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   245
     * string replacements as needed.
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   246
     */
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   247
    private static final class TemplateReader extends BufferedReader {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   248
        private Map<String, String> variables;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   249
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   250
        TemplateReader(Map<String, String> variables, InputStream template) {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   251
            super(new InputStreamReader(template));
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   252
            this.variables = variables;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   253
        }
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   254
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   255
        @Override public String readLine() throws IOException {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   256
            return substituteVariables(super.readLine());
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   257
        }
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   258
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   259
        private String substituteVariables(String input) {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   260
            if (input == null) return null;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   261
            for (Map.Entry<String, String> variable : variables.entrySet()) {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   262
                input = input.replace("${" + variable.getKey() + "}", variable.getValue());
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   263
            }
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   264
            return input;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   265
        }
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   266
    }
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   267
}