選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ServerFrame.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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.addons.ui_swing.components.frames;
  23. import com.dmdirc.ServerState;
  24. import com.dmdirc.addons.ui_swing.EdtHandlerInvocation;
  25. import com.dmdirc.addons.ui_swing.components.inputfields.SwingInputField;
  26. import com.dmdirc.addons.ui_swing.dialogs.serversetting.ServerSettingsDialog;
  27. import com.dmdirc.addons.ui_swing.dialogs.sslcertificate.SSLCertificateDialog;
  28. import com.dmdirc.addons.ui_swing.injection.KeyedDialogProvider;
  29. import com.dmdirc.commandparser.PopupType;
  30. import com.dmdirc.events.FrameClosingEvent;
  31. import com.dmdirc.interfaces.Connection;
  32. import com.dmdirc.tls.CertificateManager;
  33. import com.dmdirc.tls.CertificateProblemListener;
  34. import com.dmdirc.ui.IconManager;
  35. import com.dmdirc.ui.core.dialogs.sslcertificate.SSLCertificateDialogModel;
  36. import java.awt.Window;
  37. import java.awt.event.ActionEvent;
  38. import java.awt.event.ActionListener;
  39. import java.security.cert.CertificateException;
  40. import java.security.cert.X509Certificate;
  41. import java.util.Collection;
  42. import javax.inject.Provider;
  43. import javax.swing.JMenuItem;
  44. import javax.swing.JPopupMenu;
  45. import javax.swing.SwingUtilities;
  46. import net.miginfocom.swing.MigLayout;
  47. import net.engio.mbassy.listener.Handler;
  48. /**
  49. * The ServerFrame is the MDI window that shows server messages to the user.
  50. */
  51. public final class ServerFrame extends InputTextFrame implements
  52. ActionListener, CertificateProblemListener {
  53. /** Serial version UID. */
  54. private static final long serialVersionUID = 9;
  55. /** Main window provider. */
  56. private final Provider<Window> mainWindow;
  57. /** Icon manager. */
  58. private final IconManager iconManager;
  59. /** Dialog provider to close SSD. */
  60. private final KeyedDialogProvider<Connection, ServerSettingsDialog> dialogProvider;
  61. /** popup menu item. */
  62. private JMenuItem settingsMI;
  63. /** The SSL certificate dialog we're displaying for this server, if any. */
  64. private SSLCertificateDialog sslDialog = null;
  65. /** Server instance. */
  66. private final Connection connection;
  67. /**
  68. * Creates a new ServerFrame.
  69. *
  70. * @param deps The dependencies required by text frames.
  71. * @param inputFieldProvider The provider to use to create a new input field.
  72. * @param dialogProvider Dialog provider to close SSD with
  73. * @param owner Parent Frame container
  74. */
  75. public ServerFrame(
  76. final TextFrameDependencies deps,
  77. final Provider<SwingInputField> inputFieldProvider,
  78. final KeyedDialogProvider<Connection, ServerSettingsDialog> dialogProvider,
  79. final Connection owner) {
  80. super(deps, inputFieldProvider, owner.getWindowModel());
  81. this.mainWindow = deps.mainWindow;
  82. this.iconManager = deps.iconManager;
  83. this.dialogProvider = dialogProvider;
  84. this.connection = owner;
  85. initComponents();
  86. owner.addCertificateProblemListener(this);
  87. }
  88. /**
  89. * Initialises components in this frame.
  90. */
  91. private void initComponents() {
  92. settingsMI = new JMenuItem("Settings");
  93. settingsMI.addActionListener(this);
  94. setLayout(new MigLayout("ins 0, fill, hidemode 3, wrap 1"));
  95. add(getTextPane(), "grow, push");
  96. add(getSearchBar(), "growx, pushx");
  97. add(inputPanel, "growx, pushx");
  98. }
  99. /**
  100. * {@inheritDoc}.
  101. *
  102. * @param actionEvent Action event
  103. */
  104. @Override
  105. public void actionPerformed(final ActionEvent actionEvent) {
  106. if (actionEvent.getSource() == settingsMI) {
  107. dialogProvider.displayOrRequestFocus(connection);
  108. }
  109. }
  110. @Override
  111. public PopupType getNicknamePopupType() {
  112. return PopupType.CHAN_NICK;
  113. }
  114. @Override
  115. public PopupType getChannelPopupType() {
  116. return PopupType.CHAN_NORMAL;
  117. }
  118. @Override
  119. public PopupType getHyperlinkPopupType() {
  120. return PopupType.CHAN_HYPERLINK;
  121. }
  122. @Override
  123. public PopupType getNormalPopupType() {
  124. return PopupType.CHAN_NORMAL;
  125. }
  126. @Override
  127. public void addCustomPopupItems(final JPopupMenu popupMenu) {
  128. if (getContainer().getConnection().getState() == ServerState.CONNECTED) {
  129. settingsMI.setEnabled(true);
  130. } else {
  131. settingsMI.setEnabled(false);
  132. }
  133. if (popupMenu.getComponentCount() > 0) {
  134. popupMenu.addSeparator();
  135. }
  136. popupMenu.add(settingsMI);
  137. }
  138. @Override
  139. public void certificateProblemEncountered(final X509Certificate[] chain,
  140. final Collection<CertificateException> problems,
  141. final CertificateManager certificateManager) {
  142. sslDialog = new SSLCertificateDialog(iconManager, mainWindow.get(),
  143. new SSLCertificateDialogModel(chain, problems, certificateManager));
  144. sslDialog.display();
  145. }
  146. @Override
  147. public void certificateProblemResolved(final CertificateManager manager) {
  148. SwingUtilities.invokeLater(() -> {
  149. if (sslDialog != null) {
  150. sslDialog.dispose();
  151. }
  152. });
  153. }
  154. @Override
  155. @Handler(invocation = EdtHandlerInvocation.class)
  156. public void windowClosing(final FrameClosingEvent event) {
  157. dialogProvider.dispose(connection);
  158. super.windowClosing(event);
  159. }
  160. @Override
  161. public void dispose() {
  162. ((Connection) frameParent).removeCertificateProblemListener(this);
  163. super.dispose();
  164. }
  165. }