You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IRCTextAttribute.java 3.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 java.io.InvalidObjectException;
  19. import java.text.AttributedCharacterIterator.Attribute;
  20. import java.util.HashMap;
  21. import java.util.Map;
  22. /**
  23. * Defines attribute keys that can be used to identify text attributes. These keys are used in
  24. * AttributedCharacterIterator and AttributedString.
  25. */
  26. public final class IRCTextAttribute extends Attribute {
  27. /** A version number for this class. */
  28. private static final long serialVersionUID = 1;
  29. /** table of all instances in this class, used by readResolve. */
  30. private static final Map<String, IRCTextAttribute> INSTANCE_MAP = new HashMap<>(1);
  31. /**
  32. * Constructs an Attribute with the given name.
  33. *
  34. * @param name name for the attribute
  35. */
  36. protected IRCTextAttribute(final String name) {
  37. super(name);
  38. if (getClass() == IRCTextAttribute.class) {
  39. INSTANCE_MAP.put(name, this);
  40. }
  41. }
  42. /**
  43. * Resolves instances being deserialized to the predefined constants.
  44. *
  45. * @return IRCTextAttribute instance
  46. *
  47. * @throws InvalidObjectException when the class being deserialized is not an instance of
  48. * IRCTextAttribute
  49. */
  50. @Override
  51. protected Object readResolve() throws InvalidObjectException {
  52. if (getClass() != IRCTextAttribute.class) {
  53. throw new InvalidObjectException("subclass didn't correctly implement readResolve");
  54. }
  55. final IRCTextAttribute instance = INSTANCE_MAP.get(getName());
  56. if (instance == null) {
  57. throw new InvalidObjectException("unknown attribute name");
  58. } else {
  59. return instance;
  60. }
  61. }
  62. /** Hyperlink attribute. */
  63. public static final IRCTextAttribute HYPERLINK = new IRCTextAttribute("hyperlink");
  64. /** Nickname attribute. */
  65. public static final IRCTextAttribute NICKNAME = new IRCTextAttribute("nickname");
  66. /** Channel attribute. */
  67. public static final IRCTextAttribute CHANNEL = new IRCTextAttribute("channel");
  68. /** Smiley attribute. */
  69. public static final IRCTextAttribute SMILEY = new IRCTextAttribute("smiley");
  70. /** Tooltip attribute. */
  71. public static final IRCTextAttribute TOOLTIP = new IRCTextAttribute("tooltip");
  72. }