1 /*
   2  * $Id$
   3  *
   4  * Copyright (c) 2004, 2016, Oracle and/or its affiliates. All rights reserved.
   5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   6  *
   7  * This code is free software; you can redistribute it and/or modify it
   8  * under the terms of the GNU General Public License version 2 only, as
   9  * published by the Free Software Foundation.  Oracle designates this
  10  * particular file as subject to the "Classpath" exception as provided
  11  * by Oracle in the LICENSE file that accompanied this code.
  12  *
  13  * This code is distributed in the hope that it will be useful, but WITHOUT
  14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16  * version 2 for more details (a copy is included in the LICENSE file that
  17  * accompanied this code).
  18  *
  19  * You should have received a copy of the GNU General Public License version
  20  * 2 along with this work; if not, write to the Free Software Foundation,
  21  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  22  *
  23  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  24  * or visit www.oracle.com if you need additional information or have any
  25  * questions.
  26  */
  27 package com.sun.javatest.tool;
  28 
  29 import java.util.ListIterator;
  30 
  31 import com.sun.javatest.util.HelpTree;
  32 import com.sun.javatest.util.I18NResourceBundle;
  33 
  34 /**
  35  * A tool manager to handle the command line options for the JT Harness desktop.
  36  * <ul>
  37  * <li><code>-newDesktop</code>: behave as though this is the first time startup
  38  * <li><code>-cleanDesktop</code>: synonym for newDesktop (backward compatibility)
  39  * </ul>
  40  */
  41 public class DesktopManager extends CommandManager
  42 {
  43     public HelpTree.Node getHelp() {
  44         String[] cmds = {"cleanDesktop", "newDesktop", "resume", "laf"};
  45         return new HelpTree.Node(i18n, "dt.opts", cmds);
  46     }
  47 
  48     Desktop createDesktop() {
  49         Desktop d = new Desktop();
  50         if (firstTimeFlag)
  51             d.setFirstTime(firstTimeFlag);
  52         if (resumeFlag) {
  53             d.setRestoreOnStart(true);
  54         }
  55         return d;
  56     }
  57 
  58     Desktop createDesktop(CommandContext ctx) {
  59         Desktop d = new Desktop(ctx);
  60         if (firstTimeFlag)
  61             d.setFirstTime(firstTimeFlag);
  62         if (resumeFlag) {
  63             d.setRestoreOnStart(true);
  64         }
  65         return d;
  66     }
  67 
  68 
  69     //----------------------------------------------------------------------------
  70 
  71     public boolean parseCommand(String cmd, ListIterator<String> argIter, CommandContext ctx)
  72         throws Command.Fault
  73     {
  74         if (cmd.equalsIgnoreCase("newDesktop") || cmd.equalsIgnoreCase("cleanDesktop")) {
  75             firstTimeFlag = true;
  76             ctx.addCommand(new NewDesktopCommand());
  77             return true;
  78         } else if (cmd.equalsIgnoreCase("resume")) {
  79             resumeFlag = true;
  80             ctx.addCommand(new ResumeCommand());
  81             return true;
  82         } else if (cmd.equalsIgnoreCase("laf")) {
  83             ctx.addCommand(new SetLafCommand(argIter, ctx));
  84             return true;
  85         }
  86         return false;
  87     }
  88 
  89     private boolean firstTimeFlag;
  90     private boolean resumeFlag;
  91     private static I18NResourceBundle i18n = I18NResourceBundle.getBundleForClass(ConfigManager.class);
  92 
  93     private static class NewDesktopCommand extends Command {
  94         NewDesktopCommand() {
  95             super("newDesktop");
  96         }
  97 
  98         public void run(CommandContext ctx) {
  99         }
 100     }
 101 
 102     private static class ResumeCommand extends Command {
 103 
 104         public ResumeCommand() {
 105             super("resume");
 106         }
 107 
 108         public void run(CommandContext ctx) throws Fault {
 109         }
 110     }
 111 
 112     private static class SetLafCommand extends Command {
 113 
 114         private SetLafCommand(ListIterator argIter, CommandContext ctx) throws Fault {
 115             super("laf");
 116 
 117             if (!argIter.hasNext()) {
 118                 throw new Fault(i18n, "dt.opts.laf.missingArg");
 119             }
 120 
 121             int laf;
 122 
 123             String lafName = (String) argIter.next();
 124             if ("nimbus".equalsIgnoreCase(lafName)) {
 125                 laf = CommandContext.NIMBUS_LAF;
 126             } else if ("metal".equalsIgnoreCase(lafName)) {
 127                 laf = CommandContext.METAL_LAF;
 128             } else if ("system".equalsIgnoreCase(lafName) || "sys".equalsIgnoreCase(lafName)) {
 129                 laf = CommandContext.SYSTEM_LAF;
 130             } else if ("default".equalsIgnoreCase(lafName)) {
 131                 laf = CommandContext.DEFAULT_LAF;
 132             } else {
 133                 throw new Fault(i18n, "dt.opts.laf.badArg", new Object[] {lafName});
 134             }
 135 
 136             addArg(lafName);
 137 
 138             ctx.setPreferredLookAndFeel(laf);
 139         }
 140 
 141         @Override
 142         public void run(CommandContext ctx) throws Fault {
 143         }
 144     }
 145 }