PageRenderTime 51ms CodeModel.GetById 38ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/idea-plugin/src/com/google/jstestdriver/idea/ui/MainUI.java

https://github.com/mistaecko/jstestdriver
Java | 178 lines | 135 code | 17 blank | 26 comment | 0 complexity | 82c6ffd60c4e0d5ec200e1c9e0bbef02 MD5 | raw file
  1/*
  2 * Copyright 2009 Google Inc.
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5 * use this file except in compliance with the License. You may obtain a copy of
  6 * the License at
  7 *
  8 * http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 13 * License for the specific language governing permissions and limitations under
 14 * the License.
 15 */
 16package com.google.jstestdriver.idea.ui;
 17
 18import static com.google.inject.multibindings.Multibinder.newSetBinder;
 19import static java.awt.BorderLayout.CENTER;
 20import static java.awt.BorderLayout.NORTH;
 21import static java.awt.BorderLayout.SOUTH;
 22
 23import com.google.common.collect.Lists;
 24import com.google.inject.Binder;
 25import com.google.inject.Guice;
 26import com.google.inject.Injector;
 27import com.google.inject.Module;
 28import com.google.inject.multibindings.Multibinder;
 29import com.google.jstestdriver.ActionFactory;
 30import com.google.jstestdriver.ActionRunner;
 31import com.google.jstestdriver.Args4jFlagsParser;
 32import com.google.jstestdriver.CapturedBrowsers;
 33import com.google.jstestdriver.Flags;
 34import com.google.jstestdriver.JsTestDriver;
 35import com.google.jstestdriver.PluginLoader;
 36import com.google.jstestdriver.ServerStartupAction;
 37import com.google.jstestdriver.config.CmdFlags;
 38import com.google.jstestdriver.config.CmdLineFlagsFactory;
 39import com.google.jstestdriver.config.Configuration;
 40import com.google.jstestdriver.config.InitializeModule;
 41import com.google.jstestdriver.config.Initializer;
 42import com.google.jstestdriver.config.YamlParser;
 43import com.google.jstestdriver.guice.TestResultPrintingModule.TestResultPrintingInitializer;
 44import com.google.jstestdriver.hooks.PluginInitializer;
 45
 46import org.apache.commons.logging.LogFactory;
 47import org.mortbay.log.Slf4jLog;
 48
 49import java.awt.BorderLayout;
 50import java.util.Collections;
 51import java.util.List;
 52import java.util.ResourceBundle;
 53import java.util.logging.Level;
 54import java.util.logging.Logger;
 55
 56import javax.swing.JFrame;
 57import javax.swing.JPanel;
 58import javax.swing.JSplitPane;
 59
 60/**
 61 * This class provides an entry point for the Swing GUI of JSTestDriver, independent of running
 62 * as an IDEA plugin. Right now, this is not released in a form that anyone uses.
 63 * TODO(alexeagle): should we delete this, or document that it can be used?
 64 *
 65 * @author alexeagle@google.com (Alex Eagle)
 66 */
 67public class MainUI {
 68
 69  private final class UiInitializer implements PluginInitializer {
 70    @Override
 71    public Module initializeModule(Flags flags, Configuration config) {
 72      return new Module() {
 73        @Override
 74        public void configure(Binder binder) {
 75          binder.bind(ResourceBundle.class).toInstance(messageBundle);
 76          binder.bind(StatusBar.Status.class).toInstance(StatusBar.Status.NOT_RUNNING);
 77        }
 78      };
 79    }
 80  }
 81
 82  private static final String JCL_LOG_CONFIG_ATTR = "org.apache.commons.logging.Log";
 83  private static final String JETTY_LOG_CLASS_PROP = "org.mortbay.log.class";
 84  private static final String JCL_SIMPLELOG_SHOWLOGNAME = "org.apache.commons.logging.simplelog.showlogname";
 85  private static final String JCL_SIMPLELOG_SHOW_SHORT_LOGNAME = "org.apache.commons.logging.simplelog.showShortLogname";
 86  private final ResourceBundle messageBundle;
 87  private LogPanel logPanel;
 88  private StatusBar statusBar;
 89  private CapturedBrowsersPanel capturedBrowsersPanel;
 90  private JPanel infoPanel;
 91  private final Logger logger = Logger.getLogger(MainUI.class.getCanonicalName());
 92  private final CmdFlags preparsedFlags;
 93
 94  public MainUI(CmdFlags preparsedFlags, ResourceBundle bundle) {
 95    this.preparsedFlags = preparsedFlags;
 96    this.messageBundle = bundle;
 97  }
 98
 99  public static void main(String[] args) {
100    CmdFlags preparsedFlags = new CmdLineFlagsFactory().create(args);
101    configureLogging();
102    ResourceBundle bundle = ResourceBundle.getBundle("com.google.jstestdriver.ui.messages");
103    new MainUI(preparsedFlags, bundle).startUI();
104  }
105
106  private static void configureLogging() {
107    // Configure commons logging to log to the Swing log panel logger
108    LogFactory.getFactory().setAttribute(JCL_LOG_CONFIG_ATTR, LogPanelLog.class.getName());
109    // Configure Jetty to log to SLF4J. Since slf4j-jcl.jar is in the classpath, SLF4J will be
110    // configured to delegate logging to commons logging.
111    System.setProperty(JETTY_LOG_CLASS_PROP, Slf4jLog.class.getName());
112    System.setProperty(JCL_SIMPLELOG_SHOW_SHORT_LOGNAME, "false");
113    System.setProperty(JCL_SIMPLELOG_SHOWLOGNAME, "false");
114  }
115
116  private void startUI() {
117    try {
118      // TODO(corysmith): Fix the set up to have an injection class and proper arrangements for listening.
119      final Configuration configuration = preparsedFlags.getConfigurationSource().parse(
120          preparsedFlags.getBasePath(), new YamlParser());
121      List<Module> initializeModules = Lists.newLinkedList();
122      initializeModules.add(
123          new InitializeModule(new PluginLoader(),
124              preparsedFlags.getBasePath(),
125              new Args4jFlagsParser(),
126              preparsedFlags.getRunnerMode()));
127      initializeModules.add(new Module() {
128        public void configure(Binder binder) {
129          final Multibinder<PluginInitializer> initBinder = newSetBinder(binder, PluginInitializer.class);
130          initBinder.addBinding().to(TestResultPrintingInitializer.class);
131          initBinder.addBinding().toInstance(new UiInitializer());
132        }
133      });
134      final Injector initializeInjector = Guice.createInjector(initializeModules);
135
136      final List<Module> actionRunnerModules =
137          initializeInjector.getInstance(Initializer.class)
138              .initialize(Collections.<Module>emptyList(), configuration, preparsedFlags.getRunnerMode(),
139                  preparsedFlags.getUnusedFlagsAsArgs());
140
141      final Injector injector = Guice.createInjector(actionRunnerModules);
142
143      statusBar = injector.getInstance(StatusBar.class);
144      logPanel = injector.getInstance(LogPanel.class);
145      capturedBrowsersPanel = injector.getInstance(CapturedBrowsersPanel.class);
146      LogPanelLog.LogPanelHolder.setLogPanel(logPanel);
147      infoPanel =injector.getInstance(InfoPanel.class);
148
149      ActionFactory actionFactory = injector.getInstance(ActionFactory.class);
150      actionFactory.registerListener(ServerStartupAction.class, statusBar);
151      actionFactory.registerListener(CapturedBrowsers.class, statusBar);
152      actionFactory.registerListener(CapturedBrowsers.class, capturedBrowsersPanel);
153      injector.getInstance(ActionRunner.class).runActions();
154      JFrame appFrame = buildMainAppFrame();
155      appFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
156      appFrame.pack();
157      appFrame.setVisible(true);
158    } catch (Exception e) {
159      logger.log(Level.SEVERE, "Failed to start the server: ", e);
160    }
161  }
162
163  @SuppressWarnings("serial")
164  private JFrame buildMainAppFrame() {
165    return new JFrame("JSTestDriver Browser Manager") {{
166      add(new JSplitPane(JSplitPane.VERTICAL_SPLIT) {{
167        setTopComponent(new JPanel(new BorderLayout()) {{
168          add(new JPanel(new BorderLayout()) {{
169            add(statusBar, NORTH);
170            add(infoPanel, CENTER);
171            add(capturedBrowsersPanel, SOUTH);
172          }}, SOUTH);
173        }});
174        setBottomComponent(logPanel);
175      }});
176    }};
177  }
178}