Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

BackBufferImpl.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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.ui.messages;
  18. import com.dmdirc.events.DisplayProperty;
  19. import com.dmdirc.events.DisplayableEvent;
  20. import com.dmdirc.events.eventbus.EventBus;
  21. import com.dmdirc.interfaces.WindowModel;
  22. import com.dmdirc.util.EventUtils;
  23. import java.util.Arrays;
  24. import net.engio.mbassy.listener.Handler;
  25. /**
  26. * Models the history of a window in the client.
  27. */
  28. public class BackBufferImpl implements BackBuffer {
  29. private final Document document;
  30. private final Styliser styliser;
  31. private final EventBus eventBus;
  32. private final EventFormatter formatter;
  33. private final WindowModel owner;
  34. public BackBufferImpl(
  35. final WindowModel owner,
  36. final ColourManagerFactory colourManagerFactory,
  37. final EventFormatter formatter) {
  38. this.owner = owner;
  39. this.styliser = new Styliser(
  40. owner.getConnection().orElse(null),
  41. owner.getConfigManager(),
  42. colourManagerFactory.getColourManager(owner.getConfigManager()));
  43. this.document = new IRCDocument(owner.getConfigManager(), styliser);
  44. this.eventBus = owner.getEventBus();
  45. this.formatter = formatter;
  46. }
  47. /**
  48. * Starts adding events received on the event bus to this buffer's document.
  49. */
  50. public void startAddingEvents() {
  51. eventBus.subscribe(this);
  52. }
  53. /**
  54. * Stops adding events received on the event bus to this buffer's document.
  55. */
  56. public void stopAddingEvents() {
  57. eventBus.unsubscribe(this);
  58. }
  59. /**
  60. * Handles a displayable event received on the event bus.
  61. *
  62. * @param event The event to be displayed.
  63. */
  64. @Handler(priority = EventUtils.PRIORITY_DISPLAYABLE_EVENT_HANDLER)
  65. private void handleDisplayableEvent(final DisplayableEvent event) {
  66. if (shouldDisplay(event)) {
  67. formatter.format(event).map(s -> s.split("\n")).map(Arrays::stream).ifPresent(
  68. t -> t.forEach(line -> document.addText(
  69. event.getTimestamp(), event.getDisplayProperties(), line)));
  70. }
  71. }
  72. /**
  73. * Determines if the specified event should be displayed in this backbuffer.
  74. *
  75. * @param event The event to check
  76. * @return True if the event should be displayed, false otherwise.
  77. */
  78. private boolean shouldDisplay(final DisplayableEvent event) {
  79. return event.getSource().equals(owner)
  80. && !event.hasDisplayProperty(DisplayProperty.DO_NOT_DISPLAY);
  81. }
  82. @Override
  83. public Document getDocument() {
  84. return document;
  85. }
  86. @Override
  87. public Styliser getStyliser() {
  88. return styliser;
  89. }
  90. }