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.

OpenWindow.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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.global;
  18. import com.dmdirc.CustomWindow;
  19. import com.dmdirc.commandparser.BaseCommandInfo;
  20. import com.dmdirc.commandparser.CommandArguments;
  21. import com.dmdirc.commandparser.CommandInfo;
  22. import com.dmdirc.commandparser.CommandType;
  23. import com.dmdirc.commandparser.commands.BaseCommand;
  24. import com.dmdirc.commandparser.commands.IntelligentCommand;
  25. import com.dmdirc.commandparser.commands.context.CommandContext;
  26. import com.dmdirc.config.GlobalConfig;
  27. import com.dmdirc.interfaces.CommandController;
  28. import com.dmdirc.interfaces.Connection;
  29. import com.dmdirc.events.eventbus.EventBus;
  30. import com.dmdirc.interfaces.WindowModel;
  31. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  32. import com.dmdirc.ui.WindowManager;
  33. import com.dmdirc.ui.input.AdditionalTabTargets;
  34. import com.dmdirc.ui.messages.BackBufferFactory;
  35. import javax.annotation.Nonnull;
  36. import javax.inject.Inject;
  37. import java.util.Optional;
  38. /**
  39. * Opens a new window.
  40. */
  41. public class OpenWindow extends BaseCommand implements IntelligentCommand {
  42. /** A command info object for this command. */
  43. public static final CommandInfo INFO = new BaseCommandInfo("openwindow",
  44. "openwindow [--server|--child] <name> [title] "
  45. + "- opens a window with the specified name and title",
  46. CommandType.TYPE_GLOBAL);
  47. /** Window management. */
  48. private final WindowManager windowManager;
  49. /** The bus to dispatch events on. */
  50. private final EventBus eventBus;
  51. /** The config provider to retrieve settings from. */
  52. private final AggregateConfigProvider configProvider;
  53. /** Back buffer factory. */
  54. private final BackBufferFactory backBufferFactory;
  55. /**
  56. * Creates a new instance of this command.
  57. */
  58. @Inject
  59. public OpenWindow(
  60. final CommandController controller,
  61. final WindowManager windowManager,
  62. final EventBus eventBus,
  63. @GlobalConfig final AggregateConfigProvider configProvider,
  64. final BackBufferFactory backBufferFactory) {
  65. super(controller);
  66. this.windowManager = windowManager;
  67. this.eventBus = eventBus;
  68. this.configProvider = configProvider;
  69. this.backBufferFactory = backBufferFactory;
  70. }
  71. @Override
  72. public void execute(@Nonnull final WindowModel origin,
  73. final CommandArguments args, final CommandContext context) {
  74. int start = 0;
  75. WindowModel parent = null;
  76. if (args.getArguments().length > 0 && "--server".equals(args.getArguments()[0])) {
  77. final Optional<Connection> connection = origin.getConnection();
  78. if (!connection.isPresent()) {
  79. showError(origin, args.isSilent(),
  80. "This window doesn't have an associated server.");
  81. return;
  82. }
  83. parent = connection.get().getWindowModel();
  84. start = 1;
  85. } else if (args.getArguments().length > 0 && "--child".equals(args.getArguments()[0])) {
  86. parent = origin;
  87. start = 1;
  88. }
  89. if (args.getArguments().length == start || args.getArguments()[start].isEmpty()) {
  90. showUsage(origin, args.isSilent(), "openwindow",
  91. "[--server|--child] <name> [title]");
  92. } else {
  93. final WindowModel window;
  94. if (parent == null) {
  95. window = windowManager.findCustomWindow(args.getArguments()[start]);
  96. } else {
  97. window = windowManager.findCustomWindow(parent, args.getArguments()[start]);
  98. }
  99. final String title = args.getArguments().length > start + 1
  100. ? args.getArgumentsAsString(start + 1) : args.getArguments()[start];
  101. if (window == null) {
  102. final CustomWindow newWindow;
  103. if (parent == null) {
  104. newWindow = new CustomWindow(args.getArguments()[start], title,
  105. configProvider, eventBus, backBufferFactory);
  106. windowManager.addWindow(newWindow);
  107. } else {
  108. newWindow = new CustomWindow(args.getArguments()[start], title, parent,
  109. backBufferFactory);
  110. windowManager.addWindow(parent, newWindow);
  111. }
  112. } else {
  113. showError(origin, args.isSilent(), "A custom window by that name already exists.");
  114. }
  115. }
  116. }
  117. @Override
  118. public AdditionalTabTargets getSuggestions(final int arg,
  119. final IntelligentCommandContext context) {
  120. final AdditionalTabTargets res = new AdditionalTabTargets();
  121. if (arg == 0) {
  122. res.add("--server");
  123. res.add("--child");
  124. }
  125. return res;
  126. }
  127. }