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

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