Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ServerMenu.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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.addons.ui_swing.components.menubar;
  18. import com.dmdirc.ServerState;
  19. import com.dmdirc.addons.ui_swing.Apple;
  20. import com.dmdirc.addons.ui_swing.UIUtilities;
  21. import com.dmdirc.addons.ui_swing.components.frames.TextFrame;
  22. import com.dmdirc.addons.ui_swing.dialogs.newserver.NewServerDialog;
  23. import com.dmdirc.addons.ui_swing.dialogs.serversetting.ServerSettingsDialog;
  24. import com.dmdirc.addons.ui_swing.injection.DialogProvider;
  25. import com.dmdirc.addons.ui_swing.injection.KeyedDialogProvider;
  26. import com.dmdirc.addons.ui_swing.interfaces.ActiveFrameManager;
  27. import com.dmdirc.interfaces.Connection;
  28. import com.dmdirc.util.system.LifecycleController;
  29. import com.dmdirc.interfaces.WindowModel;
  30. import java.util.Optional;
  31. import javax.inject.Inject;
  32. import javax.inject.Singleton;
  33. import javax.swing.JMenu;
  34. import javax.swing.JMenuItem;
  35. import javax.swing.event.MenuEvent;
  36. import javax.swing.event.MenuListener;
  37. /**
  38. * A menu providing server related commands to the menu bar.
  39. */
  40. @Singleton
  41. public class ServerMenu extends JMenu implements MenuListener {
  42. /** A version number for this class. */
  43. private static final long serialVersionUID = 1;
  44. /** Active frame manager. */
  45. private final ActiveFrameManager activeFrameManager;
  46. /** Lifecycle controller. */
  47. private final LifecycleController lifecycleController;
  48. /** Provider to use to retrieve NSD instances. */
  49. private final DialogProvider<NewServerDialog> newServerProvider;
  50. /** Provider for server settings dialogs. */
  51. private final KeyedDialogProvider<Connection, ServerSettingsDialog> ssdProvider;
  52. /** Menu items which can be enabled/disabled. */
  53. private JMenuItem ssd;
  54. private JMenuItem disconnect;
  55. /**
  56. * Creates a new Server menu.
  57. *
  58. * @param activeFrameManager Active frame manager.
  59. * @param lifecycleController Lifecycle controller
  60. * @param newServerProvider Provider to use to retrieve NSD instances.
  61. * @param ssdProvider Provider to get SSD instances
  62. */
  63. @Inject
  64. public ServerMenu(
  65. final ActiveFrameManager activeFrameManager,
  66. final LifecycleController lifecycleController,
  67. final DialogProvider<NewServerDialog> newServerProvider,
  68. final KeyedDialogProvider<Connection, ServerSettingsDialog> ssdProvider) {
  69. super("Server");
  70. this.activeFrameManager = activeFrameManager;
  71. this.lifecycleController = lifecycleController;
  72. this.newServerProvider = newServerProvider;
  73. this.ssdProvider = ssdProvider;
  74. setMnemonic('s');
  75. addMenuListener(this);
  76. initServerMenu();
  77. menuSelected(null);
  78. }
  79. /**
  80. * Initialises the server menu.
  81. */
  82. private void initServerMenu() {
  83. add(JMenuItemBuilder.create()
  84. .setText("New Server...")
  85. .setMnemonic('n')
  86. .addActionMethod(newServerProvider::displayOrRequestFocus)
  87. .build());
  88. disconnect = JMenuItemBuilder.create()
  89. .setText("Disconnect")
  90. .setMnemonic('d')
  91. .addActionMethod(() ->
  92. activeFrameManager.getActiveFrame().map(TextFrame::getContainer)
  93. .flatMap(WindowModel::getConnection)
  94. .ifPresent(Connection::disconnect))
  95. .build();
  96. add(disconnect);
  97. ssd = JMenuItemBuilder.create()
  98. .setMnemonic('s')
  99. .setText("Server settings")
  100. .addActionMethod(() -> activeFrameManager.getActiveFrame().ifPresent(
  101. f -> f.getContainer().getConnection()
  102. .ifPresent(ssdProvider::displayOrRequestFocus)))
  103. .build();
  104. add(ssd);
  105. if (!Apple.isAppleUI()) {
  106. add(JMenuItemBuilder.create()
  107. .setText("Exit")
  108. .setMnemonic('x')
  109. .addActionMethod(
  110. () -> UIUtilities.invokeOffEDTNoLogging(lifecycleController::quit))
  111. .build());
  112. }
  113. }
  114. @Override
  115. public final void menuSelected(final MenuEvent e) {
  116. final Optional<ServerState> activeConnectionState = activeFrameManager.getActiveFrame()
  117. .map(TextFrame::getContainer)
  118. .flatMap(WindowModel::getConnection)
  119. .map(Connection::getState);
  120. final boolean connected = activeConnectionState.equals(Optional.of(ServerState.CONNECTED));
  121. ssd.setEnabled(connected);
  122. disconnect.setEnabled(connected);
  123. }
  124. @Override
  125. public void menuDeselected(final MenuEvent e) {
  126. // Do nothing
  127. }
  128. @Override
  129. public void menuCanceled(final MenuEvent e) {
  130. // Do nothing
  131. }
  132. }