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.

URLConfigPanel.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.ui_swing.dialogs.prefs;
  18. import com.dmdirc.addons.ui_swing.components.GenericTableModel;
  19. import com.dmdirc.addons.ui_swing.components.IconManager;
  20. import com.dmdirc.addons.ui_swing.components.PackingTable;
  21. import com.dmdirc.addons.ui_swing.components.URLProtocolPanel;
  22. import com.dmdirc.addons.ui_swing.dialogs.StandardInputDialog;
  23. import com.dmdirc.addons.ui_swing.injection.MainWindow;
  24. import com.dmdirc.config.GlobalConfig;
  25. import com.dmdirc.config.UserConfig;
  26. import com.dmdirc.config.prefs.PreferencesInterface;
  27. import com.dmdirc.config.validators.URLProtocolValidator;
  28. import com.dmdirc.config.provider.AggregateConfigProvider;
  29. import com.dmdirc.config.provider.ConfigProvider;
  30. import java.awt.Dialog.ModalityType;
  31. import java.awt.Window;
  32. import java.awt.event.ActionEvent;
  33. import java.awt.event.ActionListener;
  34. import java.net.URI;
  35. import java.net.URISyntaxException;
  36. import java.util.HashMap;
  37. import java.util.Map;
  38. import java.util.Map.Entry;
  39. import java.util.Set;
  40. import java.util.stream.Collectors;
  41. import javax.inject.Inject;
  42. import javax.swing.JButton;
  43. import javax.swing.JPanel;
  44. import javax.swing.JScrollPane;
  45. import javax.swing.ListSelectionModel;
  46. import javax.swing.event.ListSelectionEvent;
  47. import javax.swing.event.ListSelectionListener;
  48. import net.miginfocom.swing.MigLayout;
  49. /**
  50. * URL Config panel. List all known url protocols and allows them to be configured.
  51. */
  52. public class URLConfigPanel extends JPanel implements
  53. ListSelectionListener, ActionListener, PreferencesInterface {
  54. /** Serial version UID. */
  55. private static final long serialVersionUID = 1;
  56. /** The global configuration to read settings from. */
  57. private final AggregateConfigProvider globalConfig;
  58. /** The user configuration to store settings in. */
  59. private final ConfigProvider userConfig;
  60. /** The icon manager to use for input dialogs. */
  61. private final IconManager iconManager;
  62. /** Protocol list. */
  63. private PackingTable table;
  64. /** Table mode. */
  65. private GenericTableModel<URLHandlerHolder> model;
  66. /** Table scrollpane. */
  67. private JScrollPane tableScrollPane;
  68. /** Protocol config panel. */
  69. private Map<URI, URLProtocolPanel> details;
  70. /** Empty info panel. */
  71. private URLProtocolPanel empty;
  72. /** Current component. */
  73. private URLProtocolPanel activeComponent;
  74. /** Add button. */
  75. private JButton add;
  76. /** Removed button. */
  77. private JButton remove;
  78. /** Selected row. */
  79. private int selectedRow;
  80. /** Parent window. */
  81. private final Window parentWindow;
  82. /**
  83. * Instantiates a new URL config panel.
  84. *
  85. * @param parentWindow Parent window
  86. * @param globalConfig The global configuration to read settings from.
  87. * @param userConfig The user configuration to write settings to.
  88. * @param iconManager The icon manager to use for input dialogs.
  89. */
  90. @Inject
  91. public URLConfigPanel(
  92. @MainWindow final Window parentWindow,
  93. @GlobalConfig final AggregateConfigProvider globalConfig,
  94. @UserConfig final ConfigProvider userConfig,
  95. final IconManager iconManager) {
  96. this.parentWindow = parentWindow;
  97. this.globalConfig = globalConfig;
  98. this.userConfig = userConfig;
  99. this.iconManager = iconManager;
  100. initComponents();
  101. addListeners();
  102. layoutComponents();
  103. selectedRow = -1;
  104. }
  105. /**
  106. * Initialises the components.
  107. */
  108. private void initComponents() {
  109. tableScrollPane = new JScrollPane();
  110. model = new GenericTableModel<>(URLHandlerHolder.class, "getUri", "getHandler");
  111. model.setHeaderNames("Protocol", "Handler");
  112. table = new PackingTable(model, tableScrollPane);
  113. table.setDefaultRenderer(URISchemeCellRenderer.class, new URISchemeCellRenderer());
  114. table.setDefaultRenderer(URIHandlerCellRenderer.class, new URIHandlerCellRenderer());
  115. table.setAutoCreateRowSorter(true);
  116. table.setAutoCreateColumnsFromModel(true);
  117. table.setColumnSelectionAllowed(false);
  118. table.setCellSelectionEnabled(false);
  119. table.setFillsViewportHeight(false);
  120. table.setRowSelectionAllowed(true);
  121. table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  122. table.getRowSorter().toggleSortOrder(0);
  123. details = new HashMap<>();
  124. empty = new URLProtocolPanel(globalConfig, userConfig, null, true);
  125. activeComponent = empty;
  126. add = new JButton("Add");
  127. remove = new JButton("Remove");
  128. remove.setEnabled(false);
  129. tableScrollPane.setViewportView(table);
  130. final Set<String> options = globalConfig.getOptions("protocol").keySet();
  131. for (final String option : options) {
  132. try {
  133. final URI uri = new URI(option + "://example.test.com");
  134. model.addValue(new URLHandlerHolder(uri, getURLHandler(uri)));
  135. details.put(uri, new URLProtocolPanel(globalConfig, userConfig, uri, true));
  136. } catch (final URISyntaxException ex) {
  137. //Ignore wont happen
  138. }
  139. }
  140. }
  141. private String getURLHandler(final URI uri) {
  142. if (globalConfig.hasOptionString("protocol", uri.getScheme())) {
  143. return globalConfig.getOption("protocol", uri.getScheme());
  144. } else {
  145. return "";
  146. }
  147. }
  148. /**
  149. * Adds listeners.
  150. */
  151. private void addListeners() {
  152. table.getSelectionModel().addListSelectionListener(this);
  153. add.addActionListener(this);
  154. remove.addActionListener(this);
  155. }
  156. /**
  157. * Lays out the components.
  158. */
  159. private void layoutComponents() {
  160. removeAll();
  161. setLayout(new MigLayout("ins 0, wrap 1, nocache"));
  162. add(tableScrollPane, "growx, pushx, h 150!");
  163. add(add, "split 2, growx, pushx");
  164. add(remove, "growx, pushx");
  165. add(activeComponent, "growx, pushx, wmax 100%");
  166. }
  167. @Override
  168. public void save() {
  169. valueChanged(null);
  170. final Map<URI, String> handlers = model.elements().stream()
  171. .collect(Collectors.toMap(URLHandlerHolder::getUri, URLHandlerHolder::getHandler));
  172. final Set<String> protocols = globalConfig.getOptions("protocol").keySet();
  173. for (final String protocol : protocols) {
  174. URI uri;
  175. try {
  176. uri = new URI(protocol + "://example.test.com");
  177. } catch (final URISyntaxException ex) {
  178. uri = null;
  179. }
  180. if (uri != null && handlers.containsKey(uri)) {
  181. saveHandler(protocol, handlers.get(uri));
  182. } else {
  183. saveHandler(protocol, "");
  184. }
  185. handlers.remove(uri);
  186. }
  187. for (final Entry<URI, String> entry : handlers.entrySet()) {
  188. saveHandler(entry.getKey().getScheme(), entry.getValue());
  189. }
  190. }
  191. /**
  192. * Saves or updates a handler to the config.
  193. *
  194. * @param protocol Protocol for the handler
  195. * @param handler Handler for the protocol
  196. */
  197. private void saveHandler(final String protocol, final String handler) {
  198. if (handler.isEmpty()) {
  199. userConfig.unsetOption("protocol", protocol);
  200. } else {
  201. userConfig.setOption("protocol", protocol, handler);
  202. }
  203. }
  204. @Override
  205. public void valueChanged(final ListSelectionEvent e) {
  206. if (e == null || !e.getValueIsAdjusting()) {
  207. setVisible(false);
  208. if (selectedRow != -1 && selectedRow < model.getRowCount()) {
  209. final URLProtocolPanel panel = details.get(model.getValue(selectedRow).getUri());
  210. model.getValue(selectedRow).setHandler(panel.getSelection());
  211. }
  212. if (table.getSelectedRow() == -1) {
  213. activeComponent = empty;
  214. layoutComponents();
  215. add.setEnabled(false);
  216. remove.setEnabled(false);
  217. selectedRow = -1;
  218. } else {
  219. activeComponent = details.get(model.getValue(table.getRowSorter().
  220. convertRowIndexToModel(table.getSelectedRow())).getUri());
  221. layoutComponents();
  222. add.setEnabled(true);
  223. remove.setEnabled(true);
  224. selectedRow = table.getRowSorter().convertRowIndexToModel(table.
  225. getSelectedRow());
  226. }
  227. setVisible(true);
  228. }
  229. }
  230. @Override
  231. public void actionPerformed(final ActionEvent e) {
  232. if (e.getSource() == add) {
  233. new StandardInputDialog(parentWindow,
  234. ModalityType.MODELESS, iconManager, "New URL handler",
  235. "Please enter the name of the new protocol.",
  236. new URLProtocolValidator(globalConfig), this::saveAddNewURLHandler).display();
  237. } else if (e.getSource() == remove) {
  238. model.removeValue(model.getValue(table.getRowSorter().convertRowIndexToModel(
  239. table.getSelectedRow())));
  240. }
  241. }
  242. private boolean saveAddNewURLHandler(final String text) {
  243. try {
  244. final URI uri = new URI(text + "://example.test.com");
  245. model.addValue(new URLHandlerHolder(uri, getURLHandler(uri)));
  246. details.put(uri, new URLProtocolPanel(globalConfig, userConfig, uri, true));
  247. return true;
  248. } catch (final URISyntaxException ex) {
  249. return false;
  250. }
  251. }
  252. }