PageRenderTime 42ms CodeModel.GetById 21ms RepoModel.GetById 0ms 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. */
  16. package com.google.jstestdriver.idea.ui;
  17. import static com.google.inject.multibindings.Multibinder.newSetBinder;
  18. import static java.awt.BorderLayout.CENTER;
  19. import static java.awt.BorderLayout.NORTH;
  20. import static java.awt.BorderLayout.SOUTH;
  21. import com.google.common.collect.Lists;
  22. import com.google.inject.Binder;
  23. import com.google.inject.Guice;
  24. import com.google.inject.Injector;
  25. import com.google.inject.Module;
  26. import com.google.inject.multibindings.Multibinder;
  27. import com.google.jstestdriver.ActionFactory;
  28. import com.google.jstestdriver.ActionRunner;
  29. import com.google.jstestdriver.Args4jFlagsParser;
  30. import com.google.jstestdriver.CapturedBrowsers;
  31. import com.google.jstestdriver.Flags;
  32. import com.google.jstestdriver.JsTestDriver;
  33. import com.google.jstestdriver.PluginLoader;
  34. import com.google.jstestdriver.ServerStartupAction;
  35. import com.google.jstestdriver.config.CmdFlags;
  36. import com.google.jstestdriver.config.CmdLineFlagsFactory;
  37. import com.google.jstestdriver.config.Configuration;
  38. import com.google.jstestdriver.config.InitializeModule;
  39. import com.google.jstestdriver.config.Initializer;
  40. import com.google.jstestdriver.config.YamlParser;
  41. import com.google.jstestdriver.guice.TestResultPrintingModule.TestResultPrintingInitializer;
  42. import com.google.jstestdriver.hooks.PluginInitializer;
  43. import org.apache.commons.logging.LogFactory;
  44. import org.mortbay.log.Slf4jLog;
  45. import java.awt.BorderLayout;
  46. import java.util.Collections;
  47. import java.util.List;
  48. import java.util.ResourceBundle;
  49. import java.util.logging.Level;
  50. import java.util.logging.Logger;
  51. import javax.swing.JFrame;
  52. import javax.swing.JPanel;
  53. import javax.swing.JSplitPane;
  54. /**
  55. * This class provides an entry point for the Swing GUI of JSTestDriver, independent of running
  56. * as an IDEA plugin. Right now, this is not released in a form that anyone uses.
  57. * TODO(alexeagle): should we delete this, or document that it can be used?
  58. *
  59. * @author alexeagle@google.com (Alex Eagle)
  60. */
  61. public class MainUI {
  62. private final class UiInitializer implements PluginInitializer {
  63. @Override
  64. public Module initializeModule(Flags flags, Configuration config) {
  65. return new Module() {
  66. @Override
  67. public void configure(Binder binder) {
  68. binder.bind(ResourceBundle.class).toInstance(messageBundle);
  69. binder.bind(StatusBar.Status.class).toInstance(StatusBar.Status.NOT_RUNNING);
  70. }
  71. };
  72. }
  73. }
  74. private static final String JCL_LOG_CONFIG_ATTR = "org.apache.commons.logging.Log";
  75. private static final String JETTY_LOG_CLASS_PROP = "org.mortbay.log.class";
  76. private static final String JCL_SIMPLELOG_SHOWLOGNAME = "org.apache.commons.logging.simplelog.showlogname";
  77. private static final String JCL_SIMPLELOG_SHOW_SHORT_LOGNAME = "org.apache.commons.logging.simplelog.showShortLogname";
  78. private final ResourceBundle messageBundle;
  79. private LogPanel logPanel;
  80. private StatusBar statusBar;
  81. private CapturedBrowsersPanel capturedBrowsersPanel;
  82. private JPanel infoPanel;
  83. private final Logger logger = Logger.getLogger(MainUI.class.getCanonicalName());
  84. private final CmdFlags preparsedFlags;
  85. public MainUI(CmdFlags preparsedFlags, ResourceBundle bundle) {
  86. this.preparsedFlags = preparsedFlags;
  87. this.messageBundle = bundle;
  88. }
  89. public static void main(String[] args) {
  90. CmdFlags preparsedFlags = new CmdLineFlagsFactory().create(args);
  91. configureLogging();
  92. ResourceBundle bundle = ResourceBundle.getBundle("com.google.jstestdriver.ui.messages");
  93. new MainUI(preparsedFlags, bundle).startUI();
  94. }
  95. private static void configureLogging() {
  96. // Configure commons logging to log to the Swing log panel logger
  97. LogFactory.getFactory().setAttribute(JCL_LOG_CONFIG_ATTR, LogPanelLog.class.getName());
  98. // Configure Jetty to log to SLF4J. Since slf4j-jcl.jar is in the classpath, SLF4J will be
  99. // configured to delegate logging to commons logging.
  100. System.setProperty(JETTY_LOG_CLASS_PROP, Slf4jLog.class.getName());
  101. System.setProperty(JCL_SIMPLELOG_SHOW_SHORT_LOGNAME, "false");
  102. System.setProperty(JCL_SIMPLELOG_SHOWLOGNAME, "false");
  103. }
  104. private void startUI() {
  105. try {
  106. // TODO(corysmith): Fix the set up to have an injection class and proper arrangements for listening.
  107. final Configuration configuration = preparsedFlags.getConfigurationSource().parse(
  108. preparsedFlags.getBasePath(), new YamlParser());
  109. List<Module> initializeModules = Lists.newLinkedList();
  110. initializeModules.add(
  111. new InitializeModule(new PluginLoader(),
  112. preparsedFlags.getBasePath(),
  113. new Args4jFlagsParser(),
  114. preparsedFlags.getRunnerMode()));
  115. initializeModules.add(new Module() {
  116. public void configure(Binder binder) {
  117. final Multibinder<PluginInitializer> initBinder = newSetBinder(binder, PluginInitializer.class);
  118. initBinder.addBinding().to(TestResultPrintingInitializer.class);
  119. initBinder.addBinding().toInstance(new UiInitializer());
  120. }
  121. });
  122. final Injector initializeInjector = Guice.createInjector(initializeModules);
  123. final List<Module> actionRunnerModules =
  124. initializeInjector.getInstance(Initializer.class)
  125. .initialize(Collections.<Module>emptyList(), configuration, preparsedFlags.getRunnerMode(),
  126. preparsedFlags.getUnusedFlagsAsArgs());
  127. final Injector injector = Guice.createInjector(actionRunnerModules);
  128. statusBar = injector.getInstance(StatusBar.class);
  129. logPanel = injector.getInstance(LogPanel.class);
  130. capturedBrowsersPanel = injector.getInstance(CapturedBrowsersPanel.class);
  131. LogPanelLog.LogPanelHolder.setLogPanel(logPanel);
  132. infoPanel =injector.getInstance(InfoPanel.class);
  133. ActionFactory actionFactory = injector.getInstance(ActionFactory.class);
  134. actionFactory.registerListener(ServerStartupAction.class, statusBar);
  135. actionFactory.registerListener(CapturedBrowsers.class, statusBar);
  136. actionFactory.registerListener(CapturedBrowsers.class, capturedBrowsersPanel);
  137. injector.getInstance(ActionRunner.class).runActions();
  138. JFrame appFrame = buildMainAppFrame();
  139. appFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  140. appFrame.pack();
  141. appFrame.setVisible(true);
  142. } catch (Exception e) {
  143. logger.log(Level.SEVERE, "Failed to start the server: ", e);
  144. }
  145. }
  146. @SuppressWarnings("serial")
  147. private JFrame buildMainAppFrame() {
  148. return new JFrame("JSTestDriver Browser Manager") {{
  149. add(new JSplitPane(JSplitPane.VERTICAL_SPLIT) {{
  150. setTopComponent(new JPanel(new BorderLayout()) {{
  151. add(new JPanel(new BorderLayout()) {{
  152. add(statusBar, NORTH);
  153. add(infoPanel, CENTER);
  154. add(capturedBrowsersPanel, SOUTH);
  155. }}, SOUTH);
  156. }});
  157. setBottomComponent(logPanel);
  158. }});
  159. }};
  160. }
  161. }