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.

URLProtocolPanel.java 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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;
  18. import com.dmdirc.addons.ui_swing.dialogs.url.URLSubstitutionsPanel;
  19. import com.dmdirc.config.provider.AggregateConfigProvider;
  20. import com.dmdirc.config.provider.ConfigProvider;
  21. import com.dmdirc.ui.core.util.URLHandler;
  22. import java.awt.event.ActionEvent;
  23. import java.awt.event.ActionListener;
  24. import java.net.URI;
  25. import java.util.Arrays;
  26. import java.util.Enumeration;
  27. import javax.swing.AbstractButton;
  28. import javax.swing.ButtonGroup;
  29. import javax.swing.JButton;
  30. import javax.swing.JFileChooser;
  31. import javax.swing.JLabel;
  32. import javax.swing.JPanel;
  33. import javax.swing.JRadioButton;
  34. import javax.swing.JTextField;
  35. import javax.swing.event.DocumentEvent;
  36. import javax.swing.event.DocumentListener;
  37. import net.miginfocom.swing.MigLayout;
  38. /**
  39. * URL Protocol configuration panel.
  40. */
  41. public class URLProtocolPanel extends JPanel implements ActionListener,
  42. DocumentListener {
  43. /** Serial version UID. */
  44. private static final long serialVersionUID = 1;
  45. /** Global config. */
  46. private final AggregateConfigProvider globalConfig;
  47. /** User settings. */
  48. private final ConfigProvider userSettings;
  49. /** URL. */
  50. private final URI uri;
  51. /** Show insets? */
  52. private final boolean useInsets;
  53. /** Show file chooser. */
  54. private JButton showFileChooser;
  55. /** Command text field. */
  56. private JTextField commandPath;
  57. /** Option selection. */
  58. private ButtonGroup optionType;
  59. /** DMDirc choice. */
  60. private JRadioButton dmdirc;
  61. /** Browser choice. */
  62. private JRadioButton browser;
  63. /** Mail choice. */
  64. private JRadioButton mail;
  65. /** Custom command choice. */
  66. private JRadioButton custom;
  67. /** Substitutions label. */
  68. private JLabel subsLabel;
  69. /** example label. */
  70. private JLabel exampleLabel;
  71. /** Substitutions panel. */
  72. private URLSubstitutionsPanel subsPanel;
  73. /**
  74. * Instantiates the URLDialog.
  75. *
  76. * @param globalConfig Global configuration
  77. * @param userSettings User settings
  78. * @param url URL to open once added
  79. * @param useInsets Show insets?
  80. */
  81. public URLProtocolPanel(final AggregateConfigProvider globalConfig,
  82. final ConfigProvider userSettings,
  83. final URI url,
  84. final boolean useInsets) {
  85. this.globalConfig = globalConfig;
  86. this.userSettings = userSettings;
  87. uri = url;
  88. this.useInsets = useInsets;
  89. initComponents();
  90. layoutComponents();
  91. addListeners();
  92. }
  93. /** Initialises the components. */
  94. private void initComponents() {
  95. setOpaque(false);
  96. showFileChooser = new JButton("Browse");
  97. commandPath = new JTextField();
  98. optionType = new ButtonGroup();
  99. dmdirc = new JRadioButton("Use DMDirc");
  100. browser = new JRadioButton("Use browser (or system registered handler)");
  101. mail = new JRadioButton("Use mail client");
  102. custom = new JRadioButton("Custom command");
  103. subsLabel = new JLabel();
  104. exampleLabel = new JLabel();
  105. commandPath.setEnabled(false);
  106. showFileChooser.setEnabled(false);
  107. subsLabel.setEnabled(false);
  108. exampleLabel.setEnabled(false);
  109. optionType.add(dmdirc);
  110. optionType.add(browser);
  111. optionType.add(mail);
  112. optionType.add(custom);
  113. subsPanel = new URLSubstitutionsPanel(Arrays.asList("url", "fragment", "host", "path",
  114. "port", "query", "protocol", "username", "password"));
  115. subsPanel.setVisible(custom.isSelected());
  116. updateSelection();
  117. }
  118. /** Lays out the components. */
  119. private void layoutComponents() {
  120. if (useInsets) {
  121. setLayout(new MigLayout("fillx, wrap 1, hidemode 3"));
  122. } else {
  123. setLayout(new MigLayout("ins 0, fillx, wrap 1, hidemode 3"));
  124. }
  125. add(dmdirc, "growx, pushx");
  126. add(browser, "growx, pushx");
  127. add(mail, "growx, pushx");
  128. add(custom, "growx, pushx");
  129. add(commandPath, "split 2, growx, pushx, sgy line");
  130. add(showFileChooser, "sgy line");
  131. add(subsLabel, "growx, pushx");
  132. add(exampleLabel, "width ::100%" + (useInsets ? "-2*u" : ""));
  133. add(subsPanel, "growx, pushx");
  134. }
  135. /** Adds listeners to the components. */
  136. private void addListeners() {
  137. showFileChooser.addActionListener(this);
  138. dmdirc.addActionListener(this);
  139. browser.addActionListener(this);
  140. mail.addActionListener(this);
  141. custom.addActionListener(this);
  142. commandPath.getDocument().addDocumentListener(this);
  143. }
  144. /** Saves the settings. */
  145. public void save() {
  146. userSettings.setOption("protocol", uri.getScheme().toLowerCase(), getSelection());
  147. }
  148. /**
  149. * Returns the selected value.
  150. *
  151. * @return Selected value
  152. */
  153. public String getSelection() {
  154. if (optionType.getSelection() == dmdirc.getModel()) {
  155. return "DMDIRC";
  156. } else if (optionType.getSelection() == browser.getModel()) {
  157. return "BROWSER";
  158. } else if (optionType.getSelection() == mail.getModel()) {
  159. return "MAIL";
  160. } else if (optionType.getSelection() == custom.getModel()) {
  161. return commandPath.getText();
  162. } else {
  163. return "";
  164. }
  165. }
  166. /**
  167. * Updates the example label.
  168. */
  169. private void updateExample() {
  170. if (uri == null) {
  171. setEnabled(false);
  172. exampleLabel.setText("Example: ");
  173. } else {
  174. exampleLabel.setText("Example: "
  175. + URLHandler.substituteParams(uri, commandPath.getText()));
  176. }
  177. }
  178. /**
  179. * Updates the selection.
  180. */
  181. public void updateSelection() {
  182. if (uri != null && globalConfig.hasOptionString("protocol", uri.getScheme())) {
  183. final String option = globalConfig.getOption("protocol", uri.getScheme());
  184. switch (option) {
  185. case "DMDIRC":
  186. optionType.setSelected(dmdirc.getModel(), true);
  187. break;
  188. case "BROWSER":
  189. optionType.setSelected(browser.getModel(), true);
  190. break;
  191. case "MAIL":
  192. optionType.setSelected(mail.getModel(), true);
  193. break;
  194. default:
  195. optionType.setSelected(custom.getModel(), true);
  196. commandPath.setText(option);
  197. actionPerformed(null);
  198. break;
  199. }
  200. } else {
  201. optionType.clearSelection();
  202. commandPath.setText("");
  203. }
  204. updateExample();
  205. }
  206. @Override
  207. public void setEnabled(final boolean enabled) {
  208. super.setEnabled(enabled);
  209. final Enumeration<AbstractButton> buttons = optionType.getElements();
  210. while (buttons.hasMoreElements()) {
  211. buttons.nextElement().setEnabled(enabled);
  212. }
  213. }
  214. @Override
  215. public void actionPerformed(final ActionEvent e) {
  216. if (e != null && e.getSource() == showFileChooser) {
  217. final JFileChooser fileChooser = new JFileChooser();
  218. fileChooser.addChoosableFileFilter(new ExecutableFileFilter());
  219. fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
  220. if (fileChooser.showDialog(this, "Select")
  221. == JFileChooser.APPROVE_OPTION) {
  222. commandPath.setText(fileChooser.getSelectedFile().toString());
  223. }
  224. } else {
  225. subsPanel.setVisible(custom.isSelected());
  226. if (optionType.getSelection() == custom.getModel()) {
  227. commandPath.setEnabled(true);
  228. showFileChooser.setEnabled(true);
  229. subsLabel.setEnabled(true);
  230. exampleLabel.setEnabled(true);
  231. } else {
  232. commandPath.setEnabled(false);
  233. showFileChooser.setEnabled(false);
  234. subsLabel.setEnabled(false);
  235. exampleLabel.setEnabled(false);
  236. }
  237. validate();
  238. }
  239. }
  240. @Override
  241. public void insertUpdate(final DocumentEvent e) {
  242. updateExample();
  243. }
  244. @Override
  245. public void removeUpdate(final DocumentEvent e) {
  246. updateExample();
  247. }
  248. @Override
  249. public void changedUpdate(final DocumentEvent e) {
  250. // Ignore
  251. }
  252. }