make/jdk/src/classes/build/tools/generatenimbus/Generator.java
author vlivanov
Thu, 18 Jan 2018 02:25:18 +0300
changeset 48596 860326263d1f
parent 47389 18c850407be9
permissions -rw-r--r--
8194963: SystemDictionary::link_method_handle_constant() can't link MethodHandle.invoke()/invokeExact() Reviewed-by: kvn, psandoz
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
47389
18c850407be9 8187599: Remove dependency of Building Nimbus L&F on JAXB
ssadetsky
parents: 47216
diff changeset
    27
import javax.xml.stream.XMLInputFactory;
18c850407be9 8187599: Remove dependency of Building Nimbus L&F on JAXB
ssadetsky
parents: 47216
diff changeset
    28
import javax.xml.stream.XMLStreamException;
18c850407be9 8187599: Remove dependency of Building Nimbus L&F on JAXB
ssadetsky
parents: 47216
diff changeset
    29
import javax.xml.stream.XMLStreamReader;
3742
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    30
import java.io.*;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    31
import java.util.HashMap;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    32
import java.util.Map;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    33
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    34
/**
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    35
 * Generates the various Java artifacts based on a SynthModel.
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    36
 * <p/>
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    37
 * 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
    38
 * 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
    39
 * <p/>
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    40
 * 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
    41
 * 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
    42
 * 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
    43
 *
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    44
 * @author  Richard Bair
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    45
 * @author  Jasper Potts
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    46
 */
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    47
public class Generator {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    48
    private static Generator instance;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    49
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    50
    /** 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
    51
    private Map<String, String> variables;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    52
    private boolean full = false;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    53
    private File buildPackageRoot;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    54
    private String packageNamePrefix;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    55
    private String lafName;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    56
    private SynthModel model;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    57
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    58
    /**
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    59
     * MAIN APPLICATION
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    60
     * <p/>
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    61
     * This is for using the generator as part of the java build process
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    62
     *
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    63
     * @param args The commandline arguments
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    64
     */
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    65
    public static void main(String[] args) throws Exception {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    66
        if (args.length == 0 || (args.length % 2) != 0) {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    67
            System.out.println("Usage: generator [-options]\n" +
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    68
                    "    -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
    69
                    "    -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
    70
                    "    -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
    71
                    "                           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
    72
                    "                           files will be created.\n" +
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    73
                    "    -resourcesDir <value>  The resources directory containing templates and images.\n" +
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    74
                    "    -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
    75
                    "                           \"org.mypackage.mylaf\"\n" +
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    76
                    "    -lafName <value>       The name of the laf, such as \"MyLAF\".\n");
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    77
        } else {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    78
            boolean full = false;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    79
            File skinFile = new File(System.getProperty("user.dir"));
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    80
            File buildDir = new File(System.getProperty("user.dir"));
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    81
            File resourcesDir = new File(System.getProperty("user.dir"));
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    82
            String packagePrefix = "org.mypackage.mylaf";
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    83
            String lafName = "MyLAF";
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    84
            for (int i = 0; i < args.length; i += 2) {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    85
                String key = args[i].trim().toLowerCase();
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    86
                String value = args[i + 1].trim();
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    87
                if ("-full".equals(key)) {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    88
                    full = Boolean.parseBoolean(value);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    89
                } else if ("-skinfile".equals(key)) {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    90
                    skinFile = new File(value);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    91
                } else if ("-builddir".equals(key)) {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    92
                    buildDir = new File(value);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    93
                } else if ("-resourcesdir".equals(key)) {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    94
                    resourcesDir = new File(value);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    95
                } else if ("-packageprefix".equals(key)) {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    96
                    packagePrefix = value;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    97
                } else if ("-lafname".equals(key)) {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    98
                    lafName = value;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
    99
                }
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   100
            }
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   101
            System.out.println("### GENERATING LAF CODE ################################");
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   102
            System.out.println("   full          :" + full);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   103
            System.out.println("   skinFile      :" + skinFile.getAbsolutePath());
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   104
            System.out.println("   buildDir      :" + buildDir.getAbsolutePath());
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   105
            System.out.println("   resourcesDir  :" + resourcesDir.getAbsolutePath());
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   106
            System.out.println("   packagePrefix :" +packagePrefix);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   107
            System.out.println("   lafName       :" +lafName);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   108
47389
18c850407be9 8187599: Remove dependency of Building Nimbus L&F on JAXB
ssadetsky
parents: 47216
diff changeset
   109
            SynthModel model;
18c850407be9 8187599: Remove dependency of Building Nimbus L&F on JAXB
ssadetsky
parents: 47216
diff changeset
   110
            XMLInputFactory inputFactory = XMLInputFactory.newInstance();
18c850407be9 8187599: Remove dependency of Building Nimbus L&F on JAXB
ssadetsky
parents: 47216
diff changeset
   111
            XMLStreamReader reader;
18c850407be9 8187599: Remove dependency of Building Nimbus L&F on JAXB
ssadetsky
parents: 47216
diff changeset
   112
            try( InputStream fis = new FileInputStream(skinFile);
18c850407be9 8187599: Remove dependency of Building Nimbus L&F on JAXB
ssadetsky
parents: 47216
diff changeset
   113
                 InputStream is = new BufferedInputStream(fis)) {
18c850407be9 8187599: Remove dependency of Building Nimbus L&F on JAXB
ssadetsky
parents: 47216
diff changeset
   114
                reader = inputFactory.createXMLStreamReader(is);
18c850407be9 8187599: Remove dependency of Building Nimbus L&F on JAXB
ssadetsky
parents: 47216
diff changeset
   115
                model = new SynthModel(reader);
18c850407be9 8187599: Remove dependency of Building Nimbus L&F on JAXB
ssadetsky
parents: 47216
diff changeset
   116
            }
3742
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   117
            Generator.init(full, buildDir, packagePrefix, lafName, model);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   118
            Generator.getInstance().generate();
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   119
        }
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   120
    }
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   121
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   122
    /**
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   123
     * 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
   124
     * 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
   125
     *
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   126
     * @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
   127
     * @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
   128
     *                          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
   129
     *                          files will be created.
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   130
     * @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
   131
     *                          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
   132
     *                          directory beneath which the necessary packages and source files will be created.
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   133
     * @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
   134
     *                          org.mypackage.mylaf
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   135
     * @param lafName           The name of the laf, such as MyLAF.
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   136
     * @param model             The actual SynthModel to base these generated files on.
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   137
     */
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   138
    private Generator(boolean full, File buildDir,
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   139
            String packageNamePrefix, String lafName, SynthModel model) {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   140
        this.full = full;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   141
        //validate the input variables
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   142
        if (packageNamePrefix == null) {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   143
            throw new IllegalArgumentException("You must specify a package name prefix");
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 (buildDir == null) {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   146
            throw new IllegalArgumentException("You must specify the build directory");
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   147
        }
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   148
        if (model == null) {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   149
            throw new IllegalArgumentException("You must specify the SynthModel");
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   150
        }
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   151
        if (lafName == null) {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   152
            throw new IllegalArgumentException("You must specify the name of the look and feel");
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   153
        }
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   154
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   155
        //construct the map which is used to do variable substitution of the template
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   156
        //files
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   157
        variables = new HashMap<String, String>();
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   158
        variables.put("PACKAGE", packageNamePrefix);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   159
        variables.put("LAF_NAME", lafName);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   160
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   161
        //generate and save references to the package-root directories.
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   162
        //(That is, given the buildDir and srcDir, generate references to the
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   163
        //org.mypackage.mylaf subdirectories)
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   164
        buildPackageRoot = new File(buildDir, packageNamePrefix.replaceAll("\\.", "\\/"));
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   165
        buildPackageRoot.mkdirs();
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   166
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   167
        //save the variables
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   168
        this.packageNamePrefix = packageNamePrefix;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   169
        this.lafName = lafName;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   170
        this.model = model;
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 void init(boolean full, File buildDir,
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   174
            String packageNamePrefix, String lafName, SynthModel model) {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   175
        instance = new Generator(full, buildDir, packageNamePrefix, lafName, model);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   176
        model.initStyles();
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   177
    }
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   178
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   179
    public static Generator getInstance() {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   180
        return instance;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   181
    }
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   182
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   183
    public static Map<String, String> getVariables() {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   184
        return new HashMap<String, String>(instance.variables);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   185
    }
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   186
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   187
    public void generate() {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   188
        if (full) {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   189
            //create the LookAndFeel file
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   190
            writeSrcFileImpl("LookAndFeel", variables, lafName + "LookAndFeel");
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   191
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   192
            writeSrcFileImpl("AbstractRegionPainter", variables);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   193
            writeSrcFileImpl("BlendingMode", variables);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   194
            writeSrcFileImpl("SynthPainterImpl", variables);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   195
            writeSrcFileImpl("IconImpl", variables, lafName + "Icon.java");
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   196
            writeSrcFileImpl("StyleImpl", variables, lafName + "Style.java");
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   197
            writeSrcFileImpl("Effect", variables);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   198
            writeSrcFileImpl("EffectUtils", variables);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   199
            writeSrcFileImpl("ShadowEffect", variables);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   200
            writeSrcFileImpl("DropShadowEffect", variables);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   201
            writeSrcFileImpl("InnerShadowEffect", variables);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   202
            writeSrcFileImpl("InnerGlowEffect", variables);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   203
            writeSrcFileImpl("OuterGlowEffect", variables);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   204
            writeSrcFileImpl("State", variables);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   205
            writeSrcFileImpl("ImageCache", variables);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   206
            writeSrcFileImpl("ImageScalingHelper", variables);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   207
        }
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   208
        //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
   209
        //various palettes of the synth model
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   210
        StringBuilder defBuffer = new StringBuilder();
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   211
        StringBuilder styleBuffer = new StringBuilder();
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   212
        model.write(defBuffer, styleBuffer, packageNamePrefix);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   213
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   214
        Map<String, String> vars = getVariables();
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   215
        vars.put("UI_DEFAULT_INIT", defBuffer.toString());
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   216
        vars.put("STYLE_INIT", styleBuffer.toString());
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   217
        writeSrcFile("Defaults", vars, lafName + "Defaults");
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   218
    }
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   219
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   220
    private void writeSrcFileImpl(String name, Map<String, String> variables) {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   221
        writeSrcFileImpl(name, variables, name);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   222
    }
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   223
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   224
    private void writeSrcFileImpl(String templateName,
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   225
            Map<String, String> variables, String outputName) {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   226
        PrintWriter out = null;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   227
        try {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   228
            InputStream stream = getClass().getResourceAsStream(
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   229
                    "resources/" + templateName + ".template");
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   230
            TemplateReader in = new TemplateReader(variables, stream);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   231
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   232
            out = new PrintWriter(new File(buildPackageRoot, outputName + ".java"));
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   233
            String line = in.readLine();
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   234
            while (line != null) {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   235
                out.println(line);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   236
                line = in.readLine();
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   237
            }
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   238
        } catch (IOException e) {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   239
            throw new RuntimeException("IOException in writer", e);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   240
        } finally {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   241
            if (out != null) out.close();
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
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   245
    public static void writeSrcFile(String templateName,
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   246
            Map<String, String> variables, String outputName) {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   247
        instance.writeSrcFileImpl(templateName, variables, outputName);
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   248
    }
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   249
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   250
    /** A BufferedReader implementation that automatically performs
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   251
     * string replacements as needed.
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   252
     */
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   253
    private static final class TemplateReader extends BufferedReader {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   254
        private Map<String, String> variables;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   255
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   256
        TemplateReader(Map<String, String> variables, InputStream template) {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   257
            super(new InputStreamReader(template));
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   258
            this.variables = variables;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   259
        }
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   260
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   261
        @Override public String readLine() throws IOException {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   262
            return substituteVariables(super.readLine());
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   263
        }
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   264
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   265
        private String substituteVariables(String input) {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   266
            if (input == null) return null;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   267
            for (Map.Entry<String, String> variable : variables.entrySet()) {
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   268
                input = input.replace("${" + variable.getKey() + "}", variable.getValue());
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   269
            }
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   270
            return input;
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   271
        }
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   272
    }
5e1b8bc3cb1a 6844267: Nimbus generator depends on JIBX
peterz
parents:
diff changeset
   273
}