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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Copyright (c) 2006-2013 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.SwingController;
  24. import com.dmdirc.addons.ui_swing.dialogs.url.URLSubsitutionsPanel;
  25. import com.dmdirc.ui.core.util.URLHandler;
  26. import java.awt.event.ActionEvent;
  27. import java.awt.event.ActionListener;
  28. import java.net.URI;
  29. import java.util.Arrays;
  30. import java.util.Enumeration;
  31. import javax.swing.AbstractButton;
  32. import javax.swing.ButtonGroup;
  33. import javax.swing.JButton;
  34. import javax.swing.JFileChooser;
  35. import javax.swing.JLabel;
  36. import javax.swing.JPanel;
  37. import javax.swing.JRadioButton;
  38. import javax.swing.JTextField;
  39. import javax.swing.event.DocumentEvent;
  40. import javax.swing.event.DocumentListener;
  41. import net.miginfocom.swing.MigLayout;
  42. /**
  43. * URL Protocol configuration panel.
  44. */
  45. public class URLProtocolPanel extends JPanel implements ActionListener,
  46. DocumentListener {
  47. /** Serial version UID. */
  48. private static final long serialVersionUID = 1;
  49. /** Swing controller. */
  50. private final SwingController controller;
  51. /** URL. */
  52. private final URI uri;
  53. /** Show insets? */
  54. private final boolean useInsets;
  55. /** Show file chooser. */
  56. private JButton showFileChooser;
  57. /** Command text field. */
  58. private JTextField commandPath;
  59. /** Option selection. */
  60. private ButtonGroup optionType;
  61. /** DMDirc choice. */
  62. private JRadioButton dmdirc;
  63. /** Browser choice. */
  64. private JRadioButton browser;
  65. /** Mail choice. */
  66. private JRadioButton mail;
  67. /** Custom command choice. */
  68. private JRadioButton custom;
  69. /** Substitutions label. */
  70. private JLabel subsLabel;
  71. /** example label. */
  72. private JLabel exampleLabel;
  73. /** Substitutions panel. */
  74. private URLSubsitutionsPanel subsPanel;
  75. /**
  76. * Instantiates the URLDialog.
  77. *
  78. * @param controller Swing controller
  79. * @param url URL to open once added
  80. * @param useInsets Show insets?
  81. */
  82. public URLProtocolPanel(final SwingController controller, final URI url,
  83. final boolean useInsets) {
  84. super();
  85. this.controller = controller;
  86. uri = url;
  87. this.useInsets = useInsets;
  88. initComponents();
  89. layoutComponents();
  90. addListeners();
  91. }
  92. /** Initialises the components. */
  93. private void initComponents() {
  94. setOpaque(false);
  95. showFileChooser = new JButton("Browse");
  96. commandPath = new JTextField();
  97. optionType = new ButtonGroup();
  98. dmdirc = new JRadioButton("Use DMDirc");
  99. browser = new JRadioButton(
  100. "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 URLSubsitutionsPanel(Arrays.asList(new String[] {
  114. "url",
  115. "fragment", "host", "path", "port", "query", "protocol",
  116. "username",
  117. "password"
  118. }));
  119. subsPanel.setVisible(custom.isSelected());
  120. updateSelection();
  121. }
  122. /** Lays out the components. */
  123. private void layoutComponents() {
  124. if (useInsets) {
  125. setLayout(new MigLayout("fillx, wrap 1, hidemode 3"));
  126. } else {
  127. setLayout(new MigLayout("ins 0, fillx, wrap 1, hidemode 3"));
  128. }
  129. add(dmdirc, "growx, pushx");
  130. add(browser, "growx, pushx");
  131. add(mail, "growx, pushx");
  132. add(custom, "growx, pushx");
  133. add(commandPath, "split 2, growx, pushx, sgy line");
  134. add(showFileChooser, "sgy line");
  135. add(subsLabel, "growx, pushx");
  136. add(exampleLabel, "width ::100%" + (useInsets ? "-2*u" : ""));
  137. add(subsPanel, "growx, pushx");
  138. }
  139. /** Adds listeners to the components. */
  140. private void addListeners() {
  141. showFileChooser.addActionListener(this);
  142. dmdirc.addActionListener(this);
  143. browser.addActionListener(this);
  144. mail.addActionListener(this);
  145. custom.addActionListener(this);
  146. commandPath.getDocument().addDocumentListener(this);
  147. }
  148. /** Saves the settings. */
  149. public void save() {
  150. controller.getGlobalIdentity().setOption("protocol",
  151. 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 && controller.getGlobalConfig().hasOptionString(
  188. "protocol", uri.getScheme())) {
  189. final String option =
  190. controller.getGlobalConfig().getOption("protocol",
  191. uri.getScheme());
  192. if ("DMDIRC".equals(option)) {
  193. optionType.setSelected(dmdirc.getModel(), true);
  194. } else if ("BROWSER".equals(option)) {
  195. optionType.setSelected(browser.getModel(), true);
  196. } else if ("MAIL".equals(option)) {
  197. optionType.setSelected(mail.getModel(), true);
  198. } else {
  199. optionType.setSelected(custom.getModel(), true);
  200. commandPath.setText(option);
  201. actionPerformed(null);
  202. }
  203. } else {
  204. optionType.clearSelection();
  205. commandPath.setText("");
  206. }
  207. updateExample();
  208. }
  209. /** {@inheritDoc} */
  210. @Override
  211. public void setEnabled(final boolean enabled) {
  212. super.setEnabled(enabled);
  213. final Enumeration<AbstractButton> buttons = optionType.getElements();
  214. while (buttons.hasMoreElements()) {
  215. buttons.nextElement().setEnabled(enabled);
  216. }
  217. }
  218. /**
  219. * {@inheritDoc}
  220. *
  221. * @param e action event
  222. */
  223. @Override
  224. public void actionPerformed(final ActionEvent e) {
  225. if (e != null && e.getSource() == showFileChooser) {
  226. final JFileChooser fileChooser = new JFileChooser();
  227. fileChooser.addChoosableFileFilter(new ExecutableFileFilter());
  228. fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
  229. if (fileChooser.showDialog(this, "Select")
  230. == JFileChooser.APPROVE_OPTION) {
  231. commandPath.setText(fileChooser.getSelectedFile().toString());
  232. }
  233. } else {
  234. subsPanel.setVisible(custom.isSelected());
  235. if (optionType.getSelection() == custom.getModel()) {
  236. commandPath.setEnabled(true);
  237. showFileChooser.setEnabled(true);
  238. subsLabel.setEnabled(true);
  239. exampleLabel.setEnabled(true);
  240. } else {
  241. commandPath.setEnabled(false);
  242. showFileChooser.setEnabled(false);
  243. subsLabel.setEnabled(false);
  244. exampleLabel.setEnabled(false);
  245. }
  246. validate();
  247. }
  248. }
  249. /** {@inheritDoc} */
  250. @Override
  251. public void insertUpdate(final DocumentEvent e) {
  252. updateExample();
  253. }
  254. /** {@inheritDoc} */
  255. @Override
  256. public void removeUpdate(final DocumentEvent e) {
  257. updateExample();
  258. }
  259. /** {@inheritDoc} */
  260. @Override
  261. public void changedUpdate(final DocumentEvent e) {
  262. // Ignore
  263. }
  264. }