123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- /*
- * Copyright (c) 2006-2014 DMDirc Developers
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- package com.dmdirc.addons.ui_swing.components.frames;
-
- import com.dmdirc.ServerState;
- import com.dmdirc.addons.ui_swing.injection.EdtHandlerInvocation;
- import com.dmdirc.addons.ui_swing.components.inputfields.SwingInputField;
- import com.dmdirc.addons.ui_swing.dialogs.serversetting.ServerSettingsDialog;
- import com.dmdirc.addons.ui_swing.dialogs.sslcertificate.SSLCertificateDialog;
- import com.dmdirc.addons.ui_swing.injection.KeyedDialogProvider;
- import com.dmdirc.commandparser.PopupType;
- import com.dmdirc.events.FrameClosingEvent;
- import com.dmdirc.interfaces.Connection;
- import com.dmdirc.tls.CertificateManager;
- import com.dmdirc.tls.CertificateProblemListener;
- import com.dmdirc.ui.IconManager;
- import com.dmdirc.ui.core.dialogs.sslcertificate.SSLCertificateDialogModel;
-
- import java.awt.Window;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.security.cert.CertificateException;
- import java.security.cert.X509Certificate;
- import java.util.Collection;
-
- import javax.inject.Provider;
- import javax.swing.JMenuItem;
- import javax.swing.JPopupMenu;
- import javax.swing.SwingUtilities;
-
- import net.miginfocom.swing.MigLayout;
-
- import net.engio.mbassy.listener.Handler;
-
- /**
- * The ServerFrame is the MDI window that shows server messages to the user.
- */
- public final class ServerFrame extends InputTextFrame implements
- ActionListener, CertificateProblemListener {
-
- /** Serial version UID. */
- private static final long serialVersionUID = 9;
- /** Main window provider. */
- private final Provider<Window> mainWindow;
- /** Icon manager. */
- private final IconManager iconManager;
- /** Dialog provider to close SSD. */
- private final KeyedDialogProvider<Connection, ServerSettingsDialog> dialogProvider;
- /** popup menu item. */
- private JMenuItem settingsMI;
- /** The SSL certificate dialog we're displaying for this server, if any. */
- private SSLCertificateDialog sslDialog = null;
- /** Server instance. */
- private final Connection connection;
-
- /**
- * Creates a new ServerFrame.
- *
- * @param deps The dependencies required by text frames.
- * @param inputFieldProvider The provider to use to create a new input field.
- * @param dialogProvider Dialog provider to close SSD with
- * @param owner Parent Frame container
- */
- public ServerFrame(
- final TextFrameDependencies deps,
- final Provider<SwingInputField> inputFieldProvider,
- final KeyedDialogProvider<Connection, ServerSettingsDialog> dialogProvider,
- final Connection owner) {
- super(deps, inputFieldProvider, owner.getWindowModel());
- this.mainWindow = deps.mainWindow;
- this.iconManager = deps.iconManager;
- this.dialogProvider = dialogProvider;
- this.connection = owner;
- initComponents();
-
- owner.addCertificateProblemListener(this);
- }
-
- /**
- * Initialises components in this frame.
- */
- private void initComponents() {
- settingsMI = new JMenuItem("Settings");
- settingsMI.addActionListener(this);
-
- setLayout(new MigLayout("ins 0, fill, hidemode 3, wrap 1"));
- add(getTextPane(), "grow, push");
- add(getSearchBar(), "growx, pushx");
- add(inputPanel, "growx, pushx");
- }
-
- /**
- * {@inheritDoc}.
- *
- * @param actionEvent Action event
- */
- @Override
- public void actionPerformed(final ActionEvent actionEvent) {
- if (actionEvent.getSource() == settingsMI) {
- dialogProvider.displayOrRequestFocus(connection);
- }
- }
-
- @Override
- public PopupType getNicknamePopupType() {
- return PopupType.CHAN_NICK;
- }
-
- @Override
- public PopupType getChannelPopupType() {
- return PopupType.CHAN_NORMAL;
- }
-
- @Override
- public PopupType getHyperlinkPopupType() {
- return PopupType.CHAN_HYPERLINK;
- }
-
- @Override
- public PopupType getNormalPopupType() {
- return PopupType.CHAN_NORMAL;
- }
-
- @Override
- public void addCustomPopupItems(final JPopupMenu popupMenu) {
- if (getContainer().getConnection().getState() == ServerState.CONNECTED) {
- settingsMI.setEnabled(true);
- } else {
- settingsMI.setEnabled(false);
- }
-
- if (popupMenu.getComponentCount() > 0) {
- popupMenu.addSeparator();
- }
-
- popupMenu.add(settingsMI);
- }
-
- @Override
- public void certificateProblemEncountered(final X509Certificate[] chain,
- final Collection<CertificateException> problems,
- final CertificateManager certificateManager) {
- sslDialog = new SSLCertificateDialog(iconManager, mainWindow.get(),
- new SSLCertificateDialogModel(chain, problems, certificateManager));
- sslDialog.display();
- }
-
- @Override
- public void certificateProblemResolved(final CertificateManager manager) {
- SwingUtilities.invokeLater(() -> {
- if (sslDialog != null) {
- sslDialog.dispose();
- }
- });
- }
-
- @Override
- @Handler(invocation = EdtHandlerInvocation.class)
- public void windowClosing(final FrameClosingEvent event) {
- dialogProvider.dispose(connection);
- super.windowClosing(event);
- }
-
- @Override
- public void dispose() {
- ((Connection) frameParent).removeCertificateProblemListener(this);
- super.dispose();
- }
-
- }
|