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.

URLDialog.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.dialogs.url;
  18. import com.dmdirc.addons.ui_swing.components.URLProtocolPanel;
  19. import com.dmdirc.addons.ui_swing.components.text.TextLabel;
  20. import com.dmdirc.addons.ui_swing.dialogs.StandardDialog;
  21. import com.dmdirc.config.provider.AggregateConfigProvider;
  22. import com.dmdirc.config.provider.ConfigProvider;
  23. import com.dmdirc.ui.core.util.URLHandler;
  24. import java.awt.Window;
  25. import java.awt.event.ActionEvent;
  26. import java.awt.event.ActionListener;
  27. import java.net.URI;
  28. import javax.swing.JButton;
  29. import net.miginfocom.swing.MigLayout;
  30. /** URL Protocol dialog. */
  31. public class URLDialog extends StandardDialog implements ActionListener {
  32. /** Serial version UID. */
  33. private static final long serialVersionUID = 1;
  34. /** URL protocol config panel. */
  35. private URLProtocolPanel panel;
  36. /** URL. */
  37. private final URI url;
  38. /** Blurb label. */
  39. private TextLabel blurb;
  40. /** Swing controller. */
  41. private final Window parentWindow;
  42. /** The URL Handler to use to handle clicked links. */
  43. private final URLHandler urlHandler;
  44. /**
  45. * Instantiates the URLDialog.
  46. *
  47. * @param url URL to open once added
  48. * @param global Global Configuration
  49. * @param config User settings
  50. * @param parentWindow Parent window
  51. * @param urlHandler The URL Handler to use to handle clicked links
  52. */
  53. public URLDialog(
  54. final URI url,
  55. final AggregateConfigProvider global,
  56. final ConfigProvider config,
  57. final Window parentWindow,
  58. final URLHandler urlHandler) {
  59. super(parentWindow, ModalityType.MODELESS);
  60. this.url = url;
  61. this.parentWindow = parentWindow;
  62. this.urlHandler = urlHandler;
  63. initComponents(global, config);
  64. layoutComponents();
  65. addListeners();
  66. setTitle("Unknown URL Protocol");
  67. }
  68. /** Initialises the components. */
  69. private void initComponents(final AggregateConfigProvider global, final ConfigProvider config) {
  70. orderButtons(new JButton(), new JButton());
  71. blurb = new TextLabel("Please select the appropriate action to " + "handle " + url.
  72. getScheme() + ":// URLs from the list " + "below.");
  73. panel = new URLProtocolPanel(global, config, url, false);
  74. }
  75. /** Lays out the components. */
  76. private void layoutComponents() {
  77. setLayout(new MigLayout("fill, wrap 1, pack"));
  78. add(blurb, "");
  79. add(panel, "grow, push");
  80. add(getLeftButton(), "split 2, right");
  81. add(getRightButton(), "right");
  82. }
  83. /** Adds listeners to the components. */
  84. private void addListeners() {
  85. getOkButton().addActionListener(this);
  86. getCancelButton().addActionListener(this);
  87. }
  88. @Override
  89. public void actionPerformed(final ActionEvent e) {
  90. if (e.getSource() == getOkButton()) {
  91. panel.save();
  92. dispose();
  93. urlHandler.launchApp(url);
  94. } else if (e.getSource() == getCancelButton()) {
  95. dispose();
  96. }
  97. }
  98. @Override
  99. public boolean enterPressed() {
  100. if (panel.getSelection().isEmpty()) {
  101. return false;
  102. } else {
  103. executeAction(getOkButton());
  104. return true;
  105. }
  106. }
  107. @Override
  108. public void validate() {
  109. super.validate();
  110. setLocationRelativeTo(parentWindow);
  111. }
  112. }