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.

SetNickColour.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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.commandparser.commands.channel;
  18. import com.dmdirc.commandparser.BaseCommandInfo;
  19. import com.dmdirc.commandparser.CommandArguments;
  20. import com.dmdirc.commandparser.CommandInfo;
  21. import com.dmdirc.commandparser.CommandType;
  22. import com.dmdirc.commandparser.commands.BaseCommand;
  23. import com.dmdirc.commandparser.commands.IntelligentCommand;
  24. import com.dmdirc.commandparser.commands.context.ChannelCommandContext;
  25. import com.dmdirc.commandparser.commands.context.CommandContext;
  26. import com.dmdirc.events.DisplayProperty;
  27. import com.dmdirc.interfaces.CommandController;
  28. import com.dmdirc.interfaces.GroupChat;
  29. import com.dmdirc.interfaces.GroupChatUser;
  30. import com.dmdirc.interfaces.WindowModel;
  31. import com.dmdirc.ui.input.AdditionalTabTargets;
  32. import com.dmdirc.ui.input.TabCompletionType;
  33. import com.dmdirc.ui.messages.ColourManagerFactory;
  34. import com.dmdirc.util.colours.Colour;
  35. import javax.annotation.Nonnull;
  36. import javax.inject.Inject;
  37. import java.util.Optional;
  38. /**
  39. * Allows the user to set a nickname on the channel to use a custom colour.
  40. */
  41. public class SetNickColour extends BaseCommand implements IntelligentCommand {
  42. /** A command info object for this command. */
  43. public static final CommandInfo INFO = new BaseCommandInfo("setnickcolour",
  44. "setnickcolour <nick> [colour] - set the specified person's display colour",
  45. CommandType.TYPE_CHANNEL);
  46. /** Manager to use to convert colours. */
  47. private final ColourManagerFactory colourManagerFactory;
  48. /**
  49. * Creates a new instance of the {@link SetNickColour} command.
  50. *
  51. * @param controller The command controller that owns this command.
  52. * @param colourManagerFactory The colour manager factory to use to convert colours.
  53. */
  54. @Inject
  55. public SetNickColour(final CommandController controller,
  56. final ColourManagerFactory colourManagerFactory) {
  57. super(controller);
  58. this.colourManagerFactory = colourManagerFactory;
  59. }
  60. @Override
  61. public void execute(@Nonnull final WindowModel origin,
  62. final CommandArguments args, final CommandContext context) {
  63. final GroupChat channel = ((ChannelCommandContext) context).getGroupChat();
  64. if (args.getArguments().length == 0) {
  65. showUsage(origin, args.isSilent(), INFO.getName(), INFO.getHelp());
  66. return;
  67. }
  68. final Optional<GroupChatUser> target = channel.getUser(channel.getConnection().get()
  69. .getUser(args.getArguments()[0]));
  70. if (!target.isPresent()) {
  71. showError(origin, args.isSilent(),
  72. "No such nickname (" + args.getArguments()[0] + ")!");
  73. } else if (args.getArguments().length == 1) {
  74. // We're removing the colour
  75. target.get().removeDisplayProperty(DisplayProperty.FOREGROUND_COLOUR);
  76. channel.refreshClients();
  77. } else {
  78. // We're setting the colour
  79. final Colour newColour = colourManagerFactory.getColourManager(origin.getConfigManager())
  80. .getColourFromString(args.getArguments()[1], null);
  81. if (newColour == null) {
  82. showError(origin, args.isSilent(),
  83. "Invalid colour specified (" + args.getArguments()[1] + ").");
  84. return;
  85. }
  86. target.get().setDisplayProperty(DisplayProperty.FOREGROUND_COLOUR, newColour);
  87. channel.refreshClients();
  88. }
  89. }
  90. @Override
  91. public AdditionalTabTargets getSuggestions(final int arg,
  92. final IntelligentCommandContext context) {
  93. final AdditionalTabTargets targets = new AdditionalTabTargets();
  94. targets.excludeAll();
  95. if (arg == 0) {
  96. targets.include(TabCompletionType.CHANNEL_NICK);
  97. }
  98. return targets;
  99. }
  100. }