選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

NickColourYamlStore.java 3.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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.nickcolours;
  23. import com.dmdirc.config.GlobalConfig;
  24. import com.dmdirc.ui.messages.ColourManager;
  25. import com.dmdirc.util.BaseYamlStore;
  26. import java.awt.Color;
  27. import java.nio.file.Path;
  28. import java.util.ArrayList;
  29. import java.util.Collection;
  30. import java.util.HashMap;
  31. import java.util.Map;
  32. import java.util.Optional;
  33. import javax.inject.Inject;
  34. import javax.inject.Singleton;
  35. import org.slf4j.Logger;
  36. import org.slf4j.LoggerFactory;
  37. import static com.dmdirc.util.YamlReaderUtils.asMap;
  38. import static com.dmdirc.util.YamlReaderUtils.requiredString;
  39. @Singleton
  40. public class NickColourYamlStore extends BaseYamlStore<NickColourEntry> {
  41. private static final Logger LOG = LoggerFactory.getLogger(NickColourYamlStore.class);
  42. private final ColourManager colourManager;
  43. @Inject
  44. public NickColourYamlStore(@GlobalConfig final ColourManager colourManager) {
  45. this.colourManager = colourManager;
  46. }
  47. public Map<String, Color> readNickColourEntries(final Path path) {
  48. final Map<String, Color> nickColours = new HashMap<>();
  49. final Collection<NickColourEntry> nickColourEntries = read(path);
  50. nickColourEntries.forEach(e -> nickColours.put(e.getNetwork() + ':' + e.getUser(), e.getColor()));
  51. return nickColours;
  52. }
  53. public void writeNickColourEntries(final Path path, final Map<String, Color> nickColours) {
  54. final Collection<NickColourEntry> nickColourEntries = new ArrayList<>();
  55. nickColours.forEach((description, colour) -> nickColourEntries.add(NickColourEntry
  56. .create(description.split(":")[0], description.split(":")[1], colour)));
  57. write(path, nickColourEntries);
  58. }
  59. @Override
  60. protected Optional<NickColourEntry> convertFromYaml(final Object object) {
  61. try {
  62. final Map<Object, Object> map = asMap(object);
  63. final String network = requiredString(map, "network");
  64. final String user = requiredString(map, "user");
  65. final String colour = requiredString(map, "colour");
  66. return Optional.of(NickColourEntry.create(network, user,
  67. NickColourUtils.getColorFromString(colourManager, colour)));
  68. } catch (IllegalArgumentException ex) {
  69. LOG.info("Unable to read profile", ex);
  70. return Optional.empty();
  71. }
  72. }
  73. @Override
  74. protected Object convertToYaml(final NickColourEntry object) {
  75. final Map<Object, Object> map = new HashMap<>();
  76. map.put("network", object.getNetwork());
  77. map.put("user", object.getUser());
  78. map.put("colour", NickColourUtils.getStringFromColor(object.getColor()));
  79. return map;
  80. }
  81. }