You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ParserDebugManager.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.parserdebug;
  18. import com.dmdirc.events.CommandOutputEvent;
  19. import com.dmdirc.events.ServerDisconnectedEvent;
  20. import com.dmdirc.interfaces.Connection;
  21. import com.dmdirc.events.eventbus.EventBus;
  22. import com.dmdirc.parser.events.DebugInfoEvent;
  23. import com.dmdirc.parser.interfaces.Parser;
  24. import com.dmdirc.ui.WindowManager;
  25. import com.dmdirc.ui.messages.BackBufferFactory;
  26. import java.util.HashMap;
  27. import java.util.Map;
  28. import javax.inject.Inject;
  29. import net.engio.mbassy.listener.Handler;
  30. public class ParserDebugManager {
  31. /** Event bus to subscribe to events on. */
  32. private final EventBus eventBus;
  33. /** Map of parsers registered. */
  34. protected final Map<Parser, DebugWindow> registeredParsers;
  35. /** Window manager. */
  36. private final WindowManager windowManager;
  37. private final BackBufferFactory backBufferFactory;
  38. @Inject
  39. public ParserDebugManager(
  40. final WindowManager windowManager,
  41. final EventBus eventBus,
  42. final BackBufferFactory backBufferFactory) {
  43. this.windowManager = windowManager;
  44. this.eventBus = eventBus;
  45. this.backBufferFactory = backBufferFactory;
  46. registeredParsers = new HashMap<>();
  47. }
  48. /**
  49. * Adds action listener.
  50. */
  51. public void addActionListener() {
  52. eventBus.subscribe(this);
  53. }
  54. /**
  55. * Remove action listener.
  56. */
  57. public void removeActionListener() {
  58. eventBus.unsubscribe(this);
  59. }
  60. /**
  61. * Are we listener on the specified parser?
  62. *
  63. * @param parser Parser to check
  64. *
  65. * @return true if we're listening
  66. */
  67. public boolean containsParser(final Parser parser) {
  68. return registeredParsers.containsKey(parser);
  69. }
  70. /**
  71. * Adds a parser to this manager, adding any required call backs.
  72. *
  73. * @param parser Parser to add
  74. * @param connection The connection associated with the parser
  75. */
  76. public void addParser(final Parser parser, final Connection connection) {
  77. parser.getCallbackManager().subscribe(this);
  78. final DebugWindow window = new DebugWindow(this, "Parser Debug", parser,
  79. connection, eventBus, backBufferFactory);
  80. windowManager.addWindow(connection.getWindowModel(), window);
  81. registeredParsers.put(parser, window);
  82. window.getEventBus().publishAsync(new CommandOutputEvent(window,
  83. "======================\n" +
  84. "Started Monitoring: " + parser + '\n' +
  85. "======================"));
  86. }
  87. /**
  88. * Removes the parser from this manager, removing any call backs as required.
  89. *
  90. * @param parser Parser to add
  91. * @param close Close debug window?
  92. */
  93. public void removeParser(final Parser parser, final boolean close) {
  94. parser.getCallbackManager().unsubscribe(this);
  95. final DebugWindow window = registeredParsers.get(parser);
  96. window.getEventBus().publishAsync(new CommandOutputEvent(window,
  97. "======================\n" +
  98. "No Longer Monitoring: " + parser + " (User Requested)\n" +
  99. "======================"));
  100. if (close) {
  101. window.close();
  102. }
  103. registeredParsers.remove(parser);
  104. }
  105. /**
  106. * Removes all parser listeners and closes.
  107. *
  108. * @param close Close debug windows?
  109. */
  110. public void removeAllParserListeners(final boolean close) {
  111. for (Parser parser : registeredParsers.keySet()) {
  112. removeParser(parser, close);
  113. }
  114. }
  115. @Handler
  116. public void handleServerDisconnected(final ServerDisconnectedEvent event) {
  117. final Parser parser = event.getConnection().getParser().get();
  118. if (registeredParsers.containsKey(parser)) {
  119. removeParser(parser, false);
  120. }
  121. }
  122. @Handler
  123. public void onDebugInfo(final DebugInfoEvent event) {
  124. final DebugWindow window = registeredParsers.get(event.getParser());
  125. if (window != null) {
  126. window.getEventBus().publishAsync(new CommandOutputEvent(window,
  127. String.format("[%d] %s%n", event.getLevel(), event.getData())));
  128. }
  129. }
  130. }