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 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (c) 2006-2014 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.commandparser.commands.channel;
  23. import com.dmdirc.Channel;
  24. import com.dmdirc.ChannelClientProperty;
  25. import com.dmdirc.FrameContainer;
  26. import com.dmdirc.commandparser.BaseCommandInfo;
  27. import com.dmdirc.commandparser.CommandArguments;
  28. import com.dmdirc.commandparser.CommandInfo;
  29. import com.dmdirc.commandparser.CommandType;
  30. import com.dmdirc.commandparser.commands.Command;
  31. import com.dmdirc.commandparser.commands.IntelligentCommand;
  32. import com.dmdirc.commandparser.commands.context.ChannelCommandContext;
  33. import com.dmdirc.commandparser.commands.context.CommandContext;
  34. import com.dmdirc.interfaces.CommandController;
  35. import com.dmdirc.parser.interfaces.ChannelClientInfo;
  36. import com.dmdirc.ui.Colour;
  37. import com.dmdirc.ui.input.AdditionalTabTargets;
  38. import com.dmdirc.ui.input.TabCompletionType;
  39. import com.dmdirc.ui.messages.ColourManager;
  40. import javax.inject.Inject;
  41. /**
  42. * Allows the user to set a nickname on the channel to use a custom colour.
  43. */
  44. public class SetNickColour extends Command implements IntelligentCommand {
  45. /** A command info object for this command. */
  46. public static final CommandInfo INFO = new BaseCommandInfo("setnickcolour",
  47. "setnickcolour [--nicklist|--text] <nick> [colour] - "
  48. + "set the specified person's display colour",
  49. CommandType.TYPE_CHANNEL);
  50. /** Manager to use to convert colours. */
  51. private final ColourManager colourManager;
  52. /**
  53. * Creates a new instance of the {@link SetNickColour} command.
  54. *
  55. * @param controller The command controller that owns this command.
  56. * @param colourManager The colour manager to use to convert colours.
  57. */
  58. @Inject
  59. public SetNickColour(final CommandController controller, final ColourManager colourManager) {
  60. super(controller);
  61. this.colourManager = colourManager;
  62. }
  63. /** {@inheritDoc} */
  64. @Override
  65. public void execute(final FrameContainer origin,
  66. final CommandArguments args, final CommandContext context) {
  67. final Channel channel = ((ChannelCommandContext) context).getChannel();
  68. int offset = 0;
  69. boolean nicklist = true;
  70. boolean text = true;
  71. if (args.getArguments().length > offset && args.getArguments()[offset]
  72. .equalsIgnoreCase("--nicklist")) {
  73. text = false;
  74. offset++;
  75. } else if (args.getArguments().length > offset && args.getArguments()[offset]
  76. .equalsIgnoreCase("--text")) {
  77. nicklist = false;
  78. offset++;
  79. }
  80. if (args.getArguments().length <= offset) {
  81. showUsage(origin, args.isSilent(), "setnickcolour",
  82. "[--nicklist|--text] <nick> [colour]");
  83. return;
  84. }
  85. final ChannelClientInfo target = channel.getChannelInfo()
  86. .getChannelClient(args.getArguments()[offset]);
  87. offset++;
  88. if (target == null) {
  89. sendLine(origin, args.isSilent(), FORMAT_ERROR, "No such nickname ("
  90. + args.getArguments()[offset - 1] + ")!");
  91. } else if (args.getArguments().length <= offset) {
  92. // We're removing the colour
  93. if (nicklist) {
  94. target.getMap().remove(ChannelClientProperty.NICKLIST_FOREGROUND);
  95. }
  96. if (text) {
  97. target.getMap().remove(ChannelClientProperty.TEXT_FOREGROUND);
  98. }
  99. channel.refreshClients();
  100. } else {
  101. // We're setting the colour
  102. final Colour newColour = colourManager.getColourFromString(args.getArguments()[offset],
  103. null);
  104. if (newColour == null) {
  105. sendLine(origin, args.isSilent(), FORMAT_ERROR, "Invalid colour specified.");
  106. return;
  107. }
  108. if (nicklist) {
  109. target.getMap().put(ChannelClientProperty.NICKLIST_FOREGROUND, newColour);
  110. }
  111. if (text) {
  112. target.getMap().put(ChannelClientProperty.TEXT_FOREGROUND, newColour);
  113. }
  114. channel.refreshClients();
  115. }
  116. }
  117. /** {@inheritDoc} */
  118. @Override
  119. public AdditionalTabTargets getSuggestions(final int arg,
  120. final IntelligentCommandContext context) {
  121. final AdditionalTabTargets targets = new AdditionalTabTargets();
  122. targets.excludeAll();
  123. if (arg == 0) {
  124. targets.include(TabCompletionType.CHANNEL_NICK);
  125. targets.add("--nicklist");
  126. targets.add("--text");
  127. } else if (arg == 1 && (context.getPreviousArgs().get(0).equals("--text")
  128. || context.getPreviousArgs().get(0).equals("--nicklist"))) {
  129. targets.include(TabCompletionType.CHANNEL_NICK);
  130. }
  131. return targets;
  132. }
  133. }