Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ParserDebugManager.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (c) 2006-2015 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. package com.dmdirc.addons.parserdebug;
  23. import com.dmdirc.events.CommandOutputEvent;
  24. import com.dmdirc.events.ServerDisconnectedEvent;
  25. import com.dmdirc.interfaces.Connection;
  26. import com.dmdirc.interfaces.EventBus;
  27. import com.dmdirc.parser.events.DebugInfoEvent;
  28. import com.dmdirc.parser.interfaces.Parser;
  29. import com.dmdirc.ui.WindowManager;
  30. import com.dmdirc.ui.messages.BackBufferFactory;
  31. import java.util.HashMap;
  32. import java.util.Map;
  33. import javax.inject.Inject;
  34. import net.engio.mbassy.listener.Handler;
  35. public class ParserDebugManager {
  36. /** Event bus to subscribe to events on. */
  37. private final EventBus eventBus;
  38. /** Map of parsers registered. */
  39. protected final Map<Parser, DebugWindow> registeredParsers;
  40. /** Window manager. */
  41. private final WindowManager windowManager;
  42. private final BackBufferFactory backBufferFactory;
  43. @Inject
  44. public ParserDebugManager(
  45. final WindowManager windowManager,
  46. final EventBus eventBus,
  47. final BackBufferFactory backBufferFactory) {
  48. this.windowManager = windowManager;
  49. this.eventBus = eventBus;
  50. this.backBufferFactory = backBufferFactory;
  51. registeredParsers = new HashMap<>();
  52. }
  53. /**
  54. * Adds action listener.
  55. */
  56. public void addActionListener() {
  57. eventBus.subscribe(this);
  58. }
  59. /**
  60. * Remove action listener.
  61. */
  62. public void removeActionListener() {
  63. eventBus.unsubscribe(this);
  64. }
  65. /**
  66. * Are we listener on the specified parser?
  67. *
  68. * @param parser Parser to check
  69. *
  70. * @return true if we're listening
  71. */
  72. public boolean containsParser(final Parser parser) {
  73. return registeredParsers.containsKey(parser);
  74. }
  75. /**
  76. * Adds a parser to this manager, adding any required call backs.
  77. *
  78. * @param parser Parser to add
  79. * @param connection The connection associated with the parser
  80. */
  81. public void addParser(final Parser parser, final Connection connection) {
  82. parser.getCallbackManager().subscribe(this);
  83. final DebugWindow window = new DebugWindow(this, "Parser Debug", parser,
  84. connection, eventBus, backBufferFactory);
  85. windowManager.addWindow(connection.getWindowModel(), window);
  86. registeredParsers.put(parser, window);
  87. window.getEventBus().publishAsync(new CommandOutputEvent(window,
  88. "======================\n" +
  89. "Started Monitoring: " + parser + '\n' +
  90. "======================"));
  91. }
  92. /**
  93. * Removes the parser from this manager, removing any call backs as required.
  94. *
  95. * @param parser Parser to add
  96. * @param close Close debug window?
  97. */
  98. public void removeParser(final Parser parser, final boolean close) {
  99. parser.getCallbackManager().unsubscribe(this);
  100. final DebugWindow window = registeredParsers.get(parser);
  101. window.getEventBus().publishAsync(new CommandOutputEvent(window,
  102. "======================\n" +
  103. "No Longer Monitoring: " + parser + " (User Requested)\n" +
  104. "======================"));
  105. if (close) {
  106. window.close();
  107. }
  108. registeredParsers.remove(parser);
  109. }
  110. /**
  111. * Removes all parser listeners and closes.
  112. *
  113. * @param close Close debug windows?
  114. */
  115. public void removeAllParserListeners(final boolean close) {
  116. for (Parser parser : registeredParsers.keySet()) {
  117. removeParser(parser, close);
  118. }
  119. }
  120. @Handler
  121. public void handleServerDisconnected(final ServerDisconnectedEvent event) {
  122. final Parser parser = event.getConnection().getParser().get();
  123. if (registeredParsers.containsKey(parser)) {
  124. removeParser(parser, false);
  125. }
  126. }
  127. @Handler
  128. public void onDebugInfo(final DebugInfoEvent event) {
  129. final DebugWindow window = registeredParsers.get(event.getParser());
  130. if (window != null) {
  131. window.getEventBus().publishAsync(new CommandOutputEvent(window,
  132. String.format("[%d] %s%n", event.getLevel(), event.getData())));
  133. }
  134. }
  135. }