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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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.URLSubsitutionsPanel;
  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 URLSubsitutionsPanel 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. super();
  91. this.globalConfig = globalConfig;
  92. this.userSettings = userSettings;
  93. uri = url;
  94. this.useInsets = useInsets;
  95. initComponents();
  96. layoutComponents();
  97. addListeners();
  98. }
  99. /** Initialises the components. */
  100. private void initComponents() {
  101. setOpaque(false);
  102. showFileChooser = new JButton("Browse");
  103. commandPath = new JTextField();
  104. optionType = new ButtonGroup();
  105. dmdirc = new JRadioButton("Use DMDirc");
  106. browser = new JRadioButton("Use browser (or system registered handler)");
  107. mail = new JRadioButton("Use mail client");
  108. custom = new JRadioButton("Custom command");
  109. subsLabel = new JLabel();
  110. exampleLabel = new JLabel();
  111. commandPath.setEnabled(false);
  112. showFileChooser.setEnabled(false);
  113. subsLabel.setEnabled(false);
  114. exampleLabel.setEnabled(false);
  115. optionType.add(dmdirc);
  116. optionType.add(browser);
  117. optionType.add(mail);
  118. optionType.add(custom);
  119. subsPanel = new URLSubsitutionsPanel(Arrays.asList(new String[]{
  120. "url",
  121. "fragment", "host", "path", "port", "query", "protocol",
  122. "username",
  123. "password"
  124. }));
  125. subsPanel.setVisible(custom.isSelected());
  126. updateSelection();
  127. }
  128. /** Lays out the components. */
  129. private void layoutComponents() {
  130. if (useInsets) {
  131. setLayout(new MigLayout("fillx, wrap 1, hidemode 3"));
  132. } else {
  133. setLayout(new MigLayout("ins 0, fillx, wrap 1, hidemode 3"));
  134. }
  135. add(dmdirc, "growx, pushx");
  136. add(browser, "growx, pushx");
  137. add(mail, "growx, pushx");
  138. add(custom, "growx, pushx");
  139. add(commandPath, "split 2, growx, pushx, sgy line");
  140. add(showFileChooser, "sgy line");
  141. add(subsLabel, "growx, pushx");
  142. add(exampleLabel, "width ::100%" + (useInsets ? "-2*u" : ""));
  143. add(subsPanel, "growx, pushx");
  144. }
  145. /** Adds listeners to the components. */
  146. private void addListeners() {
  147. showFileChooser.addActionListener(this);
  148. dmdirc.addActionListener(this);
  149. browser.addActionListener(this);
  150. mail.addActionListener(this);
  151. custom.addActionListener(this);
  152. commandPath.getDocument().addDocumentListener(this);
  153. }
  154. /** Saves the settings. */
  155. public void save() {
  156. userSettings.setOption("protocol", uri.getScheme().toLowerCase(), getSelection());
  157. }
  158. /**
  159. * Returns the selected value.
  160. *
  161. * @return Selected value
  162. */
  163. public String getSelection() {
  164. if (optionType.getSelection() == dmdirc.getModel()) {
  165. return "DMDIRC";
  166. } else if (optionType.getSelection() == browser.getModel()) {
  167. return "BROWSER";
  168. } else if (optionType.getSelection() == mail.getModel()) {
  169. return "MAIL";
  170. } else if (optionType.getSelection() == custom.getModel()) {
  171. return commandPath.getText();
  172. } else {
  173. return "";
  174. }
  175. }
  176. /**
  177. * Updates the example label.
  178. */
  179. private void updateExample() {
  180. if (uri == null) {
  181. setEnabled(false);
  182. exampleLabel.setText("Example: ");
  183. } else {
  184. exampleLabel.setText("Example: "
  185. + URLHandler.substituteParams(uri, commandPath.getText()));
  186. }
  187. }
  188. /**
  189. * Updates the selection.
  190. */
  191. public void updateSelection() {
  192. if (uri != null && globalConfig.hasOptionString("protocol", uri.getScheme())) {
  193. final String option = globalConfig.getOption("protocol", uri.getScheme());
  194. switch (option) {
  195. case "DMDIRC":
  196. optionType.setSelected(dmdirc.getModel(), true);
  197. break;
  198. case "BROWSER":
  199. optionType.setSelected(browser.getModel(), true);
  200. break;
  201. case "MAIL":
  202. optionType.setSelected(mail.getModel(), true);
  203. break;
  204. default:
  205. optionType.setSelected(custom.getModel(), true);
  206. commandPath.setText(option);
  207. actionPerformed(null);
  208. break;
  209. }
  210. } else {
  211. optionType.clearSelection();
  212. commandPath.setText("");
  213. }
  214. updateExample();
  215. }
  216. /** {@inheritDoc} */
  217. @Override
  218. public void setEnabled(final boolean enabled) {
  219. super.setEnabled(enabled);
  220. final Enumeration<AbstractButton> buttons = optionType.getElements();
  221. while (buttons.hasMoreElements()) {
  222. buttons.nextElement().setEnabled(enabled);
  223. }
  224. }
  225. /**
  226. * {@inheritDoc}
  227. *
  228. * @param e action event
  229. */
  230. @Override
  231. public void actionPerformed(final ActionEvent e) {
  232. if (e != null && e.getSource() == showFileChooser) {
  233. final JFileChooser fileChooser = new JFileChooser();
  234. fileChooser.addChoosableFileFilter(new ExecutableFileFilter());
  235. fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
  236. if (fileChooser.showDialog(this, "Select")
  237. == JFileChooser.APPROVE_OPTION) {
  238. commandPath.setText(fileChooser.getSelectedFile().toString());
  239. }
  240. } else {
  241. subsPanel.setVisible(custom.isSelected());
  242. if (optionType.getSelection() == custom.getModel()) {
  243. commandPath.setEnabled(true);
  244. showFileChooser.setEnabled(true);
  245. subsLabel.setEnabled(true);
  246. exampleLabel.setEnabled(true);
  247. } else {
  248. commandPath.setEnabled(false);
  249. showFileChooser.setEnabled(false);
  250. subsLabel.setEnabled(false);
  251. exampleLabel.setEnabled(false);
  252. }
  253. validate();
  254. }
  255. }
  256. /** {@inheritDoc} */
  257. @Override
  258. public void insertUpdate(final DocumentEvent e) {
  259. updateExample();
  260. }
  261. /** {@inheritDoc} */
  262. @Override
  263. public void removeUpdate(final DocumentEvent e) {
  264. updateExample();
  265. }
  266. /** {@inheritDoc} */
  267. @Override
  268. public void changedUpdate(final DocumentEvent e) {
  269. // Ignore
  270. }
  271. }