View Javadoc
1   /*
2    * Copyright (C) 2013 4th Line GmbH, Switzerland
3    *
4    * The contents of this file are subject to the terms of either the GNU
5    * Lesser General Public License Version 2 or later ("LGPL") or the
6    * Common Development and Distribution License Version 1 or later
7    * ("CDDL") (collectively, the "License"). You may not use this file
8    * except in compliance with the License. See LICENSE.txt for more
9    * information.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14   */
15  
16  package org.fourthline.cling.support.shared;
17  
18  import org.fourthline.cling.UpnpService;
19  import org.seamless.util.logging.LoggingUtil;
20  import org.seamless.swing.AbstractController;
21  import org.seamless.swing.Application;
22  import org.seamless.swing.logging.LogCategory;
23  import org.seamless.swing.logging.LogController;
24  import org.seamless.swing.logging.LogMessage;
25  import org.seamless.swing.logging.LoggingHandler;
26  
27  import javax.swing.BorderFactory;
28  import javax.swing.JFrame;
29  import javax.swing.JLabel;
30  import javax.swing.JPanel;
31  import javax.swing.JWindow;
32  import javax.swing.UIManager;
33  import java.awt.Dimension;
34  import java.awt.Frame;
35  import java.util.List;
36  import java.util.logging.Handler;
37  import java.util.logging.Level;
38  import java.util.logging.LogManager;
39  
40  /**
41   * @author Christian Bauer
42   */
43  public abstract class MainController extends AbstractController<JFrame> {
44  
45      // Dependencies
46      final private LogController logController;
47  
48      // View
49      final private JPanel logPanel;
50  
51      public MainController(JFrame view, List<LogCategory> logCategories) {
52          super(view);
53  
54          // Some UI stuff (of course, why would the OS L&F be the default -- too easy?!)
55          try {
56              UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
57          } catch (Exception ex) {
58              System.out.println("Unable to load native look and feel: " + ex.toString());
59          }
60  
61          // Exception handler
62          System.setProperty("sun.awt.exception.handler", AWTExceptionHandler.class.getName());
63  
64          // Shutdown behavior
65          Runtime.getRuntime().addShutdownHook(new Thread() {
66              @Override
67              public void run() {
68                  if (getUpnpService() != null)
69                      getUpnpService().shutdown();
70              }
71          });
72  
73          // Logging UI
74          logController = new LogController(this, logCategories) {
75              @Override
76              protected void expand(LogMessage logMessage) {
77                  fireEventGlobal(
78                          new TextExpandEvent(logMessage.getMessage())
79                  );
80              }
81  
82              @Override
83              protected Frame getParentWindow() {
84                  return MainController.this.getView();
85              }
86          };
87          logPanel = logController.getView();
88          logPanel.setBorder(BorderFactory.createEmptyBorder(5, 0, 0, 0));
89  
90          // Wire UI into JUL
91          // Don't reset JUL root logger but add if there is a JUL config file)
92          Handler handler = new LoggingHandler() {
93              protected void log(LogMessage msg) {
94                  logController.pushMessage(msg);
95              }
96          };
97          if (System.getProperty("java.util.logging.config.file") == null) {
98              LoggingUtil.resetRootHandler(handler);
99          } else {
100             LogManager.getLogManager().getLogger("").addHandler(handler);
101         }
102     }
103 
104     public LogController getLogController() {
105         return logController;
106     }
107 
108     public JPanel getLogPanel() {
109         return logPanel;
110     }
111 
112     public void log(Level level, String msg) {
113         log(new LogMessage(level, msg));
114     }
115 
116     public void log(LogMessage message) {
117         getLogController().pushMessage(message);
118     }
119 
120     @Override
121     public void dispose() {
122         super.dispose();
123         ShutdownWindow.INSTANCE.setVisible(true);
124         new Thread() {
125             @Override
126             public void run() {
127                 System.exit(0);
128             }
129         }.start();
130     }
131 
132     public static class ShutdownWindow extends JWindow {
133         final public static JWindow INSTANCE = new ShutdownWindow();
134 
135         protected ShutdownWindow() {
136             JLabel shutdownLabel = new JLabel("Shutting down, please wait...");
137             shutdownLabel.setHorizontalAlignment(JLabel.CENTER);
138             getContentPane().add(shutdownLabel);
139             setPreferredSize(new Dimension(300, 30));
140             pack();
141             Application.center(this);
142         }
143     }
144 
145     public abstract UpnpService getUpnpService();
146 
147 }