Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

EventBusViewer.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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.debug.commands;
  23. import com.dmdirc.ClientModule.GlobalConfig;
  24. import com.dmdirc.CustomWindow;
  25. import com.dmdirc.DMDircMBassador;
  26. import com.dmdirc.addons.debug.Debug;
  27. import com.dmdirc.addons.debug.DebugCommand;
  28. import com.dmdirc.commandparser.CommandArguments;
  29. import com.dmdirc.commandparser.commands.context.CommandContext;
  30. import com.dmdirc.events.ClientLineAddedEvent;
  31. import com.dmdirc.events.DMDircEvent;
  32. import com.dmdirc.events.FrameClosingEvent;
  33. import com.dmdirc.interfaces.WindowModel;
  34. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  35. import com.dmdirc.ui.WindowManager;
  36. import com.dmdirc.ui.messages.BackBufferFactory;
  37. import com.dmdirc.ui.messages.Styliser;
  38. import java.lang.reflect.Method;
  39. import javax.annotation.Nonnull;
  40. import javax.inject.Inject;
  41. import javax.inject.Provider;
  42. import net.engio.mbassy.listener.Handler;
  43. import net.engio.mbassy.listener.Listener;
  44. import net.engio.mbassy.listener.References;
  45. /**
  46. * Displays events passed on an event bus.
  47. */
  48. public class EventBusViewer extends DebugCommand {
  49. private final AggregateConfigProvider globalConfig;
  50. private final WindowManager windowManager;
  51. private final DMDircMBassador globalEventBus;
  52. private final BackBufferFactory backBufferFactory;
  53. /**
  54. * Creates a new instance of the command.
  55. */
  56. @Inject
  57. public EventBusViewer(
  58. final Provider<Debug> commandProvider,
  59. @GlobalConfig final AggregateConfigProvider globalConfig,
  60. final WindowManager windowManager,
  61. final DMDircMBassador globalEventBus,
  62. final BackBufferFactory backBufferFactory) {
  63. super(commandProvider);
  64. this.globalConfig = globalConfig;
  65. this.windowManager = windowManager;
  66. this.globalEventBus = globalEventBus;
  67. this.backBufferFactory = backBufferFactory;
  68. }
  69. @Override
  70. public String getName() {
  71. return "eventbus";
  72. }
  73. @Override
  74. public String getUsage() {
  75. return "[--global] - Shows events being sent on an event bus";
  76. }
  77. @Override
  78. public void execute(@Nonnull final WindowModel origin,
  79. final CommandArguments args, final CommandContext context) {
  80. final boolean isGlobal = args.getArguments().length > 0
  81. && "--global".equals(args.getArguments()[0]);
  82. final CustomWindow window;
  83. if (isGlobal) {
  84. window = new CustomWindow("Event bus", "Event bus", globalConfig,
  85. globalEventBus, backBufferFactory);
  86. windowManager.addWindow(window);
  87. } else {
  88. window = new CustomWindow("Event bus", "Event bus", origin, backBufferFactory);
  89. windowManager.addWindow(origin, window);
  90. }
  91. final DMDircMBassador eventBus = isGlobal ? globalEventBus : origin.getEventBus();
  92. final WindowUpdater updater = new WindowUpdater(eventBus, window);
  93. eventBus.subscribe(updater);
  94. }
  95. /**
  96. * Updates a custom window with details of each event received on an event bus.
  97. */
  98. @Listener(references = References.Strong)
  99. private static class WindowUpdater {
  100. private final DMDircMBassador eventBus;
  101. private final WindowModel target;
  102. WindowUpdater(final DMDircMBassador eventBus, final WindowModel target) {
  103. this.eventBus = eventBus;
  104. this.target = target;
  105. }
  106. @Handler
  107. public void handleFrameClosing(final FrameClosingEvent event) {
  108. eventBus.unsubscribe(this);
  109. }
  110. @Handler
  111. public void handleEvent(final DMDircEvent event) {
  112. if (event instanceof ClientLineAddedEvent
  113. && ((ClientLineAddedEvent) event).getFrameContainer() == target) {
  114. // Don't add a line every time we add a line to our output window.
  115. // Things will explode otherwise.
  116. return;
  117. }
  118. final StringBuilder output = new StringBuilder();
  119. output.append(Styliser.CODE_BOLD)
  120. .append(event.getClass().getSimpleName())
  121. .append(Styliser.CODE_BOLD);
  122. for (Method method : event.getClass().getMethods()) {
  123. if (method.getName().startsWith("get") && method.getParameterTypes().length == 0) {
  124. try {
  125. output.append(' ')
  126. .append(Styliser.CODE_UNDERLINE)
  127. .append(method.getName().substring(3))
  128. .append(Styliser.CODE_UNDERLINE)
  129. .append('=')
  130. .append(method.invoke(event));
  131. } catch (ReflectiveOperationException ex) {
  132. // Ignore.
  133. }
  134. }
  135. }
  136. target.addLine(FORMAT_OUTPUT, output.toString());
  137. }
  138. }
  139. }