Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

BackBufferImpl.java 4.1KB

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