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

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