您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

URLProtocolPanel.java 9.4KB

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