1 /*
   2  * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.javafx.tools.ant;
  27 
  28 import java.util.LinkedList;
  29 import java.util.List;
  30 import java.util.Map;
  31 import java.util.Properties;
  32 import com.sun.javafx.tools.packager.HtmlParam;
  33 import com.sun.javafx.tools.packager.Param;
  34 import org.apache.tools.ant.BuildException;
  35 import org.apache.tools.ant.types.DataType;
  36 import org.apache.tools.ant.types.Reference;
  37 
  38 /**
  39  * Basic application descriptor.
  40  * <p>
  41  * Defines main components of application and default set of parameters.
  42  *
  43  *
  44  * Examples:
  45  * <pre>
  46  *    &lt;info vendor="Uncle Joe" description="Test program"/&gt;
  47  * </pre>
  48  *
  49  * @ant.type name="application" category="javafx"
  50  */
  51 public class Application extends DataType implements Cloneable {
  52     String mainClass = null;
  53     String module = null;
  54     String preloaderClass = null;
  55     String name = null;
  56     List<Param> parameters = new LinkedList<Param>();
  57     List<HtmlParam> htmlParameters = new LinkedList<HtmlParam>();
  58     public List<Argument> arguments = new LinkedList<Argument>();
  59     String fallbackApp = null;
  60     String id = null;
  61     boolean embeddedIntoSwing = false;
  62     String version = null;
  63     Boolean daemon = null;
  64 
  65     public void setVersion(String v) {
  66         version = v;
  67     }
  68 
  69     public void setToolkit(String v) {
  70         embeddedIntoSwing = "swing".equalsIgnoreCase(v);
  71     }
  72 
  73     public void setName(String v) {
  74         name = v;
  75     }
  76 
  77     public Param createParam() {
  78         Param p = new Param();
  79         parameters.add(p);
  80         return p;
  81     }
  82 
  83     public void setParams(Properties props) {
  84         if (props != null) {
  85             for (Map.Entry en : props.entrySet()) {
  86                 Param p = new Param();
  87                 p.setName((String)en.getKey());
  88                 p.setValue((String)en.getValue());
  89                 parameters.add(p);
  90             }
  91         }
  92     }
  93 
  94     public class Argument {
  95         private String value;
  96 
  97         public void addText(String value) {
  98             this.value = value;
  99         }
 100 
 101         public String getValue() {
 102             return this.value;
 103         }
 104     }
 105 
 106     public Argument createArgument() {
 107         Argument a = new Argument();
 108         arguments.add(a);
 109         return a;
 110     }
 111 
 112     List<String> getArguments() {
 113         List<String> lst = new LinkedList();
 114         for(Argument a: arguments) {
 115             lst.add(a.value);
 116         }
 117         return lst;
 118     }
 119 
 120     public Object clone() {
 121         try {
 122             Application result = (Application) super.clone();
 123             return result;
 124         } catch (CloneNotSupportedException e) {
 125             throw new BuildException(e);
 126         }
 127     }
 128 
 129     public HtmlParam createHtmlParam() {
 130         HtmlParam p = new HtmlParam();
 131         htmlParameters.add(p);
 132         return p;
 133     }
 134 
 135     /**
 136      * Application id that can be used to obtain Javascript reference to the application in HTML.
 137      * Same id can be also used to refer to application object in the ant task (using refid).
 138      *
 139      * @ant.not-required
 140      */
 141     public void setId(String id) {
 142         this.id = id;
 143     }
 144 
 145     @Override
 146     public void setRefid(Reference id) {
 147         this.id = id.getRefId();
 148         super.setRefid(id);
 149     }
 150 
 151     /**
 152      * Main application class.
 153      *
 154      * @ant.required
 155      */
 156     public void setMainClass(String v) {
 157         mainClass = v;
 158     }
 159 
 160     /**
 161      * Module containing the application class.
 162      *
 163      * @ant.not-required Default is to not bundle as a modular application.
 164      */
 165     public String getModule() {
 166         return this.module;
 167     }
 168 
 169     /**
 170      * Module containing the application class.
 171      *
 172      * @ant.not-required Default is to not bundle as a modular application.
 173      */
 174     public void setModule(String value) {
 175         this.module = value;
 176     }
 177 
 178     /**
 179      * Preloader class to be used.
 180      *
 181      * @ant.not-required Default is preloader shipped in JavaFX Runtime.
 182      */
 183     public void setPreloaderClass(String v) {
 184         preloaderClass = v;
 185     }
 186 
 187     /**
 188      * Is this class a daemon/service?
 189      *
 190      * @ant.not-required Default is false, i.e. an interactive app
 191      */
 192     public void setDaemon(boolean b) {
 193         daemon = b;
 194     }
 195 
 196     //return instance that actually has data. Could be referenced object ...
 197     public Application get() {
 198         return isReference() ?
 199                 (Application) getRefid().getReferencedObject() : this;
 200     }
 201 
 202     public void selfcheck() {
 203         if (get().mainClass == null) {
 204             throw new BuildException("Application main class is required.");
 205         }
 206     }
 207 
 208     @Override
 209     public String toString() {
 210         return "Application[id=" + id + ", mainClass=" + mainClass + "]";
 211     }
 212 }