Parcourir la source

DI the swing debug plugin.

Change-Id: I4d4867681150dcc6dbb7063fe0bdd2bdd55c029d
Reviewed-on: http://gerrit.dmdirc.com/3222
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Chris Smith <chris@dmdirc.com>
tags/0.8
Greg Holmes il y a 10 ans
Parent
révision
0cdcd466af

+ 156
- 0
src/com/dmdirc/addons/swingdebug/SwingDebugManager.java Voir le fichier

@@ -0,0 +1,156 @@
1
+/*
2
+ * Copyright (c) 2006-2014 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.addons.swingdebug;
24
+
25
+import com.dmdirc.ClientModule.GlobalConfig;
26
+import com.dmdirc.FrameContainer;
27
+import com.dmdirc.addons.ui_swing.DMDircEventQueue;
28
+import com.dmdirc.addons.ui_swing.MainFrame;
29
+import com.dmdirc.addons.ui_swing.SwingController;
30
+import com.dmdirc.addons.ui_swing.components.CheckBoxMenuItem;
31
+import com.dmdirc.interfaces.FrameCloseListener;
32
+import com.dmdirc.interfaces.config.AggregateConfigProvider;
33
+import com.dmdirc.plugins.PluginDomain;
34
+import com.dmdirc.ui.WindowManager;
35
+import com.dmdirc.util.URLBuilder;
36
+
37
+import java.awt.Toolkit;
38
+import java.awt.event.ActionEvent;
39
+import java.awt.event.ActionListener;
40
+
41
+import javax.inject.Inject;
42
+import javax.swing.JCheckBoxMenuItem;
43
+import javax.swing.JMenu;
44
+
45
+public class SwingDebugManager implements ActionListener, FrameCloseListener {
46
+
47
+    /** This plugin's settings domain. */
48
+    private final String domain;
49
+    /** The config to read settings with. */
50
+    private final AggregateConfigProvider globalConfig;
51
+    /** Swing controller. */
52
+    private final SwingController controller;
53
+    /** Window Management. */
54
+    private final WindowManager windowManager;
55
+    /** Swing main frame. */
56
+    private final MainFrame mainFrame;
57
+    /** Debug menu. */
58
+    private JMenu debugMenu;
59
+    /** Debug EDT menu item. */
60
+    private JCheckBoxMenuItem debugEDT;
61
+    /** Debug EDT menu item. */
62
+    private JCheckBoxMenuItem showSysOut;
63
+    /** Debug EDT menu item. */
64
+    private JCheckBoxMenuItem showSysErr;
65
+    /** System out window. */
66
+    private SystemStreamContainer sysoutFrame;
67
+    /** System error window. */
68
+    private SystemStreamContainer syserrFrame;
69
+    /** URL Builder to use for frame containers. */
70
+    private final URLBuilder urlBuilder;
71
+
72
+    @Inject
73
+    public SwingDebugManager(
74
+            @PluginDomain(SwingDebugPlugin.class) final String domain,
75
+            @GlobalConfig final AggregateConfigProvider globalConfig,
76
+            final MainFrame mainFrame,
77
+            final SwingController controller,
78
+            final WindowManager windowManager,
79
+            final URLBuilder urlBuilder) {
80
+        this.domain = domain;
81
+        this.globalConfig = globalConfig;
82
+        this.controller = controller;
83
+        this.windowManager = windowManager;
84
+        this.urlBuilder = urlBuilder;
85
+        this.mainFrame = mainFrame;
86
+    }
87
+
88
+    public void load() {
89
+        debugMenu = new JMenu("Debug");
90
+        debugEDT = new CheckBoxMenuItem("Check EDT task length");
91
+        showSysOut = new CheckBoxMenuItem("Show System.out Console");
92
+        showSysErr = new CheckBoxMenuItem("Show System.err Console");
93
+        debugEDT.addActionListener(this);
94
+        showSysErr.addActionListener(this);
95
+        showSysOut.addActionListener(this);
96
+        mainFrame.getJMenuBar().add(debugMenu);
97
+        debugMenu.add(debugEDT);
98
+        debugMenu.add(showSysOut);
99
+        debugMenu.add(showSysErr);
100
+    }
101
+
102
+    public void unload() {
103
+        mainFrame.getJMenuBar().remove(debugMenu);
104
+        debugMenu = null;
105
+        debugEDT = null;
106
+        showSysOut = null;
107
+        showSysErr = null;
108
+    }
109
+
110
+    @Override
111
+    public void actionPerformed(final ActionEvent e) {
112
+        if (e.getSource() == debugEDT) {
113
+            if (debugEDT.getState()) {
114
+                Toolkit.getDefaultToolkit().getSystemEventQueue().
115
+                        push(new TracingEventQueue(domain, globalConfig, controller));
116
+            } else {
117
+                Toolkit.getDefaultToolkit().getSystemEventQueue().
118
+                        push(new DMDircEventQueue(controller));
119
+            }
120
+        }
121
+        if (e.getSource() == showSysOut) {
122
+            if (showSysOut.isSelected()) {
123
+                sysoutFrame = new SystemStreamContainer(SystemStreamType.Out, globalConfig,
124
+                        urlBuilder);
125
+                sysoutFrame.addCloseListener(this);
126
+                windowManager.addWindow(sysoutFrame);
127
+            } else {
128
+                sysoutFrame.close();
129
+            }
130
+        }
131
+
132
+        if (e.getSource() == showSysErr) {
133
+            if (showSysErr.isSelected()) {
134
+                syserrFrame = new SystemStreamContainer(SystemStreamType.Error, globalConfig,
135
+                        urlBuilder);
136
+                syserrFrame.addCloseListener(this);
137
+                windowManager.addWindow(syserrFrame);
138
+            } else {
139
+                syserrFrame.close();
140
+            }
141
+        }
142
+    }
143
+
144
+    @Override
145
+    public void windowClosing(final FrameContainer window) {
146
+        if (window == syserrFrame) {
147
+            showSysErr.setSelected(false);
148
+            syserrFrame.removeCloseListener(this);
149
+        }
150
+        if (window == sysoutFrame) {
151
+            showSysOut.setSelected(false);
152
+            sysoutFrame.removeCloseListener(this);
153
+        }
154
+    }
155
+
156
+}

+ 47
- 0
src/com/dmdirc/addons/swingdebug/SwingDebugModule.java Voir le fichier

@@ -0,0 +1,47 @@
1
+/*
2
+ * Copyright (c) 2006-2014 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.addons.swingdebug;
24
+
25
+import com.dmdirc.addons.ui_swing.injection.SwingModule;
26
+import com.dmdirc.plugins.PluginDomain;
27
+import com.dmdirc.plugins.PluginInfo;
28
+
29
+import dagger.Module;
30
+import dagger.Provides;
31
+
32
+@Module(injects = SwingDebugManager.class, addsTo = SwingModule.class)
33
+public class SwingDebugModule {
34
+
35
+    private final PluginInfo pluginInfo;
36
+
37
+    public SwingDebugModule(final PluginInfo pluginInfo) {
38
+        this.pluginInfo = pluginInfo;
39
+    }
40
+
41
+    @Provides
42
+    @PluginDomain(SwingDebugPlugin.class)
43
+    public String getSettignsDomain() {
44
+        return pluginInfo.getDomain();
45
+    }
46
+
47
+}

+ 11
- 107
src/com/dmdirc/addons/swingdebug/SwingDebugPlugin.java Voir le fichier

@@ -22,130 +22,34 @@
22 22
 
23 23
 package com.dmdirc.addons.swingdebug;
24 24
 
25
-import com.dmdirc.addons.ui_swing.DMDircEventQueue;
26
-import com.dmdirc.addons.ui_swing.SwingController;
27
-import com.dmdirc.addons.ui_swing.components.CheckBoxMenuItem;
28
-import com.dmdirc.interfaces.config.IdentityController;
25
+import com.dmdirc.plugins.PluginInfo;
29 26
 import com.dmdirc.plugins.implementations.BasePlugin;
30
-import com.dmdirc.ui.WindowManager;
31
-import com.dmdirc.util.URLBuilder;
32 27
 
33
-import java.awt.Toolkit;
34
-import java.awt.event.ActionEvent;
35
-import java.awt.event.ActionListener;
36
-
37
-import javax.swing.JCheckBoxMenuItem;
38
-import javax.swing.JMenu;
28
+import dagger.ObjectGraph;
39 29
 
40 30
 /**
41 31
  * Swing debug plugin. Provides long running EDT task violation detection and a console for
42 32
  * System.out and System.err.
43 33
  */
44
-public class SwingDebugPlugin extends BasePlugin implements ActionListener {
34
+public class SwingDebugPlugin extends BasePlugin {
45 35
 
46
-    /** The controller to read/write settings with. */
47
-    private final IdentityController identityController;
48
-    /** Swing controller. */
49
-    private final SwingController controller;
50
-    /** Window Management. */
51
-    private final WindowManager windowManager;
52
-    /** Debug menu. */
53
-    private JMenu debugMenu;
54
-    /** Debug EDT menu item. */
55
-    private JCheckBoxMenuItem debugEDT;
56
-    /** Debug EDT menu item. */
57
-    private JCheckBoxMenuItem showSysOut;
58
-    /** Debug EDT menu item. */
59
-    private JCheckBoxMenuItem showSysErr;
60
-    /** System out window. */
61
-    private SystemStreamContainer sysoutFrame;
62
-    /** System error window. */
63
-    private SystemStreamContainer syserrFrame;
64
-    /** URL Builder to use for frame containers. */
65
-    private final URLBuilder urlBuilder;
36
+    private SwingDebugManager manager;
66 37
 
67
-    public SwingDebugPlugin(
68
-            final IdentityController identityController,
69
-            final SwingController controller,
70
-            final WindowManager windowManager,
71
-            final URLBuilder urlBuilder) {
72
-        this.identityController = identityController;
73
-        this.controller = controller;
74
-        this.windowManager = windowManager;
75
-        this.urlBuilder = urlBuilder;
38
+    @Override
39
+    public void load(final PluginInfo pluginInfo, final ObjectGraph graph) {
40
+        super.load(pluginInfo, graph);
41
+        setObjectGraph(graph.plus(new SwingDebugModule(pluginInfo)));
42
+        manager = getObjectGraph().get(SwingDebugManager.class);
76 43
     }
77 44
 
78
-    /** {@inheritDoc} */
79 45
     @Override
80 46
     public void onLoad() {
81
-        debugMenu = new JMenu("Debug");
82
-        debugEDT = new CheckBoxMenuItem("Check EDT task length");
83
-        showSysOut = new CheckBoxMenuItem("Show System.out Console");
84
-        showSysErr = new CheckBoxMenuItem("Show System.err Console");
85
-        debugEDT.addActionListener(this);
86
-        showSysErr.addActionListener(this);
87
-        showSysOut.addActionListener(this);
88
-        controller.getMainFrame().getJMenuBar().add(debugMenu);
89
-        debugMenu.add(debugEDT);
90
-        debugMenu.add(showSysOut);
91
-        debugMenu.add(showSysErr);
47
+        manager.load();
92 48
     }
93 49
 
94
-    /** {@inheritDoc} */
95 50
     @Override
96 51
     public void onUnload() {
97
-        controller.getMainFrame().getJMenuBar().remove(debugMenu);
98
-    }
99
-
100
-    /**
101
-     * {@inheritDoc}
102
-     *
103
-     * @param e Action event
104
-     */
105
-    @Override
106
-    public void actionPerformed(final ActionEvent e) {
107
-        if (e.getSource() == debugEDT) {
108
-            if (debugEDT.getState()) {
109
-                Toolkit.getDefaultToolkit().getSystemEventQueue().
110
-                        push(new TracingEventQueue(this, identityController, controller));
111
-            } else {
112
-                Toolkit.getDefaultToolkit().getSystemEventQueue().
113
-                        push(new DMDircEventQueue(controller));
114
-            }
115
-        }
116
-        if (e.getSource() == showSysOut) {
117
-            if (showSysOut.isSelected()) {
118
-                sysoutFrame = new SystemStreamContainer(SystemStreamType.Out,
119
-                        controller.getGlobalConfig(), this, urlBuilder);
120
-                windowManager.addWindow(sysoutFrame);
121
-            } else {
122
-                sysoutFrame.close();
123
-            }
124
-        }
125
-
126
-        if (e.getSource() == showSysErr) {
127
-            if (showSysErr.isSelected()) {
128
-                syserrFrame = new SystemStreamContainer(SystemStreamType.Error,
129
-                        controller.getGlobalConfig(), this, urlBuilder);
130
-                windowManager.addWindow(syserrFrame);
131
-            } else {
132
-                syserrFrame.close();
133
-            }
134
-        }
135
-    }
136
-
137
-    /**
138
-     * Notifies this plugin the specified container is closing.
139
-     *
140
-     * @param container Container that is closing
141
-     */
142
-    void windowClosing(final SystemStreamContainer container) {
143
-        if (container == sysoutFrame) {
144
-            showSysOut.setSelected(false);
145
-        }
146
-        if (container == syserrFrame) {
147
-            showSysErr.setSelected(false);
148
-        }
52
+        manager.unload();
149 53
     }
150 54
 
151 55
 }

+ 0
- 8
src/com/dmdirc/addons/swingdebug/SystemStreamContainer.java Voir le fichier

@@ -38,25 +38,20 @@ public class SystemStreamContainer extends FrameContainer {
38 38
 
39 39
     /** Stream reader thread. */
40 40
     private SystemStreamRedirectThread thread;
41
-    /** Parent plugin. */
42
-    private SwingDebugPlugin plugin;
43 41
 
44 42
     /**
45 43
      * Creates a new system stream container wrapping the specified stream.
46 44
      *
47 45
      * @param stream     Stream to wrap
48 46
      * @param config     Config to wrap
49
-     * @param plugin     Parent plugin
50 47
      * @param urlBuilder The URL builder to use when finding icons.
51 48
      */
52 49
     public SystemStreamContainer(
53 50
             final SystemStreamType stream,
54 51
             final AggregateConfigProvider config,
55
-            final SwingDebugPlugin plugin,
56 52
             final URLBuilder urlBuilder) {
57 53
         super("dmdirc", stream.toString(), stream.toString(), config, urlBuilder,
58 54
                 Arrays.asList(WindowComponent.TEXTAREA.getIdentifier()));
59
-        this.plugin = plugin;
60 55
         try {
61 56
             thread = new SystemStreamRedirectThread(stream, getDocument());
62 57
             thread.start();
@@ -64,19 +59,16 @@ public class SystemStreamContainer extends FrameContainer {
64 59
         }
65 60
     }
66 61
 
67
-    /** {@inheritDoc} */
68 62
     @Override
69 63
     public Connection getConnection() {
70 64
         return getParent() == null ? null : getParent().getConnection();
71 65
     }
72 66
 
73
-    /** {@inheritDoc} */
74 67
     @Override
75 68
     public void close() {
76 69
         super.close();
77 70
 
78 71
         thread.cancel();
79
-        plugin.windowClosing(this);
80 72
     }
81 73
 
82 74
 }

+ 17
- 25
src/com/dmdirc/addons/swingdebug/TracingEventQueue.java Voir le fichier

@@ -28,9 +28,8 @@ package com.dmdirc.addons.swingdebug;
28 28
 
29 29
 import com.dmdirc.addons.ui_swing.DMDircEventQueue;
30 30
 import com.dmdirc.addons.ui_swing.SwingController;
31
+import com.dmdirc.interfaces.config.AggregateConfigProvider;
31 32
 import com.dmdirc.interfaces.config.ConfigChangeListener;
32
-import com.dmdirc.interfaces.config.IdentityController;
33
-import com.dmdirc.plugins.Plugin;
34 33
 
35 34
 import java.awt.AWTEvent;
36 35
 import java.io.IOException;
@@ -50,8 +49,7 @@ import javax.management.ObjectName;
50 49
  * Event queue extention to monitor long running tasks on the EDT. Original code found at
51 50
  * http://today.java.net/lpt/a/433 modified to work as a DMDirc plugin.
52 51
  */
53
-public class TracingEventQueue extends DMDircEventQueue implements
54
-        Runnable, ConfigChangeListener {
52
+public class TracingEventQueue extends DMDircEventQueue implements Runnable, ConfigChangeListener {
55 53
 
56 54
     /** Threshold before event is considered long running. */
57 55
     private long thresholdDelay;
@@ -61,33 +59,29 @@ public class TracingEventQueue extends DMDircEventQueue implements
61 59
     private ThreadMXBean threadBean;
62 60
     /** boolean to end thread. */
63 61
     private boolean running = false;
64
-    /** Parent plugin. */
65
-    private final Plugin parentPlugin;
66
-    /** The controller to read/write settings with. */
67
-    private final IdentityController identityController;
62
+    /** The controller to read settings from. */
63
+    private final AggregateConfigProvider config;
64
+    /** This plugin's settings domain. */
65
+    private final String domain;
68 66
     /** Tracing thread. */
69 67
     private Thread tracingThread;
70 68
 
71 69
     /**
72 70
      * Instantiates a new tracing thread.
73 71
      *
74
-     * @param parentPlugin       Parent plugin
75
-     * @param identityController The controller to read/write settings with.
76
-     * @param controller         Swing controller
72
+     * @param domain     This plugin's settings domain
73
+     * @param config     Config to read settings from
74
+     * @param controller Swing controller
77 75
      */
78
-    public TracingEventQueue(
79
-            final Plugin parentPlugin,
80
-            final IdentityController identityController,
76
+    public TracingEventQueue(final String domain, final AggregateConfigProvider config,
81 77
             final SwingController controller) {
82 78
         super(controller);
83
-        this.parentPlugin = parentPlugin;
84
-        this.identityController = identityController;
79
+        this.domain = domain;
80
+        this.config = config;
85 81
 
86 82
         eventTimeMap = Collections.synchronizedMap(new HashMap<AWTEvent, Long>());
87
-        identityController.getGlobalConfiguration().addChangeListener(
88
-                parentPlugin.getDomain(), "debugEDT", this);
89
-        identityController.getGlobalConfiguration().addChangeListener(
90
-                parentPlugin.getDomain(), "slowedttaskthreshold", this);
83
+        config.addChangeListener(domain, "debugEDT", this);
84
+        config.addChangeListener(domain, "slowedttaskthreshold", this);
91 85
         checkTracing();
92 86
 
93 87
         try {
@@ -161,7 +155,7 @@ public class TracingEventQueue extends DMDircEventQueue implements
161 155
                             threadId, Integer.MAX_VALUE);
162 156
                     if (threadInfo != null
163 157
                             && threadInfo.getThreadName().startsWith(
164
-                            "AWT-EventQueue")) {
158
+                                    "AWT-EventQueue")) {
165 159
                         System.out.println(threadInfo.getThreadName() + " / "
166 160
                                 + threadInfo.getThreadState());
167 161
                         final StackTraceElement[] stack = threadInfo.getStackTrace();
@@ -226,10 +220,8 @@ public class TracingEventQueue extends DMDircEventQueue implements
226 220
     }
227 221
 
228 222
     private void checkTracing() {
229
-        final boolean tracing = identityController.getGlobalConfiguration()
230
-                .getOptionBool(parentPlugin.getDomain(), "debugEDT");
231
-        thresholdDelay = identityController.getGlobalConfiguration()
232
-                .getOptionInt(parentPlugin.getDomain(), "slowedttaskthreshold");
223
+        final boolean tracing = config.getOptionBool(domain, "debugEDT");
224
+        thresholdDelay = config.getOptionInt(domain, "slowedttaskthreshold");
233 225
         if (tracing) {
234 226
             running = true;
235 227
             tracingThread = new Thread(this);

Chargement…
Annuler
Enregistrer