Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

BackBufferImpl.java 4.1KB

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