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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.config.IdentityManager;
  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. /**
  48. * A version number for this class. It should be changed whenever the class
  49. * structure is changed (or anything else that would prevent serialized
  50. * objects being unserialized with the new class).
  51. */
  52. private static final long serialVersionUID = 1;
  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. /** URL. */
  72. private final URI uri;
  73. /** Show insets? */
  74. private final boolean useInsets;
  75. /** Substitutions panel. */
  76. private URLSubsitutionsPanel subsPanel;
  77. /**
  78. * Instantiates the URLDialog.
  79. *
  80. * @param url URL to open once added
  81. * @param useInsets Show insets?
  82. * @param urlHandler The URL Handler to use to handle clicked links
  83. */
  84. public URLProtocolPanel(final URI url, final boolean useInsets,
  85. final URLHandler urlHandler) {
  86. super();
  87. this.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(
  101. "Use browser (or system registered handler)");
  102. mail = new JRadioButton("Use mail client");
  103. custom = new JRadioButton("Custom command");
  104. subsLabel = new JLabel();
  105. exampleLabel = new JLabel();
  106. commandPath.setEnabled(false);
  107. showFileChooser.setEnabled(false);
  108. subsLabel.setEnabled(false);
  109. exampleLabel.setEnabled(false);
  110. optionType.add(dmdirc);
  111. optionType.add(browser);
  112. optionType.add(mail);
  113. optionType.add(custom);
  114. subsPanel = new URLSubsitutionsPanel(Arrays.asList(new String[]{"url",
  115. "fragment", "host", "path", "port", "query", "protocol", "username",
  116. "password"
  117. }));
  118. subsPanel.setVisible(custom.isSelected());
  119. updateSelection();
  120. }
  121. /** Lays out the components. */
  122. private void layoutComponents() {
  123. if (useInsets) {
  124. setLayout(new MigLayout("fillx, wrap 1, hidemode 3"));
  125. } else {
  126. setLayout(new MigLayout("ins 0, fillx, wrap 1, hidemode 3"));
  127. }
  128. add(dmdirc, "growx, pushx");
  129. add(browser, "growx, pushx");
  130. add(mail, "growx, pushx");
  131. add(custom, "growx, pushx");
  132. add(commandPath, "split 2, growx, pushx, sgy line");
  133. add(showFileChooser, "sgy line");
  134. add(subsLabel, "growx, pushx");
  135. add(exampleLabel, "width ::100%" + (useInsets ? "-2*u" : ""));
  136. add(subsPanel, "growx, pushx");
  137. }
  138. /** Adds listeners to the components. */
  139. private void addListeners() {
  140. showFileChooser.addActionListener(this);
  141. dmdirc.addActionListener(this);
  142. browser.addActionListener(this);
  143. mail.addActionListener(this);
  144. custom.addActionListener(this);
  145. commandPath.getDocument().addDocumentListener(this);
  146. }
  147. /** Saves the settings. */
  148. public void save() {
  149. IdentityManager.getConfigIdentity().setOption("protocol",
  150. uri.getScheme().toLowerCase(), getSelection());
  151. }
  152. /**
  153. * Returns the selected value.
  154. *
  155. * @return Selected value
  156. */
  157. public String getSelection() {
  158. if (optionType.getSelection() == dmdirc.getModel()) {
  159. return "DMDIRC";
  160. } else if (optionType.getSelection() == browser.getModel()) {
  161. return "BROWSER";
  162. } else if (optionType.getSelection() == mail.getModel()) {
  163. return "MAIL";
  164. } else if (optionType.getSelection() == custom.getModel()) {
  165. return commandPath.getText();
  166. } else {
  167. return "";
  168. }
  169. }
  170. /**
  171. * Updates the example label.
  172. */
  173. private void updateExample() {
  174. if (uri == null) {
  175. setEnabled(false);
  176. exampleLabel.setText("Example: ");
  177. } else {
  178. exampleLabel.setText("Example: "
  179. + URLHandler.substituteParams(uri, commandPath.getText()));
  180. }
  181. }
  182. /**
  183. * Updates the selection.
  184. */
  185. public void updateSelection() {
  186. if (uri != null && IdentityManager.getGlobalConfig().hasOptionString(
  187. "protocol", uri.getScheme())) {
  188. final String option =
  189. IdentityManager.getGlobalConfig().getOption("protocol",
  190. uri.getScheme());
  191. if ("DMDIRC".equals(option)) {
  192. optionType.setSelected(dmdirc.getModel(), true);
  193. } else if ("BROWSER".equals(option)) {
  194. optionType.setSelected(browser.getModel(), true);
  195. } else if ("MAIL".equals(option)) {
  196. optionType.setSelected(mail.getModel(), true);
  197. } else {
  198. optionType.setSelected(custom.getModel(), true);
  199. commandPath.setText(option);
  200. actionPerformed(null);
  201. }
  202. } else {
  203. optionType.clearSelection();
  204. commandPath.setText("");
  205. }
  206. updateExample();
  207. }
  208. /** {@inheritDoc} */
  209. @Override
  210. public void setEnabled(final boolean enabled) {
  211. super.setEnabled(enabled);
  212. final Enumeration<AbstractButton> buttons = optionType.getElements();
  213. while (buttons.hasMoreElements()) {
  214. buttons.nextElement().setEnabled(enabled);
  215. }
  216. }
  217. /**
  218. * {@inheritDoc}
  219. *
  220. * @param e action event
  221. */
  222. @Override
  223. public void actionPerformed(final ActionEvent e) {
  224. if (e != null && e.getSource() == showFileChooser) {
  225. final JFileChooser fileChooser = new JFileChooser();
  226. fileChooser.addChoosableFileFilter(new ExecutableFileFilter());
  227. fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
  228. if (fileChooser.showDialog(this, "Select")
  229. == JFileChooser.APPROVE_OPTION) {
  230. commandPath.setText(fileChooser.getSelectedFile().toString());
  231. }
  232. } else {
  233. subsPanel.setVisible(custom.isSelected());
  234. if (optionType.getSelection() == custom.getModel()) {
  235. commandPath.setEnabled(true);
  236. showFileChooser.setEnabled(true);
  237. subsLabel.setEnabled(true);
  238. exampleLabel.setEnabled(true);
  239. } else {
  240. commandPath.setEnabled(false);
  241. showFileChooser.setEnabled(false);
  242. subsLabel.setEnabled(false);
  243. exampleLabel.setEnabled(false);
  244. }
  245. validate();
  246. }
  247. }
  248. /** {@inheritDoc} */
  249. @Override
  250. public void insertUpdate(final DocumentEvent e) {
  251. updateExample();
  252. }
  253. /** {@inheritDoc} */
  254. @Override
  255. public void removeUpdate(final DocumentEvent e) {
  256. updateExample();
  257. }
  258. /** {@inheritDoc} */
  259. @Override
  260. public void changedUpdate(final DocumentEvent e) {
  261. //Ignore
  262. }
  263. }