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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Copyright (c) 2006-2015 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.dialogs.prefs;
  23. import com.dmdirc.ClientModule.UserConfig;
  24. import com.dmdirc.addons.ui_swing.components.GenericTableModel;
  25. import com.dmdirc.addons.ui_swing.components.IconManager;
  26. import com.dmdirc.addons.ui_swing.components.PackingTable;
  27. import com.dmdirc.addons.ui_swing.components.URLProtocolPanel;
  28. import com.dmdirc.addons.ui_swing.dialogs.StandardInputDialog;
  29. import com.dmdirc.addons.ui_swing.injection.MainWindow;
  30. import com.dmdirc.config.GlobalConfig;
  31. import com.dmdirc.config.prefs.PreferencesInterface;
  32. import com.dmdirc.config.validators.URLProtocolValidator;
  33. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  34. import com.dmdirc.interfaces.config.ConfigProvider;
  35. import java.awt.Dialog.ModalityType;
  36. import java.awt.Window;
  37. import java.awt.event.ActionEvent;
  38. import java.awt.event.ActionListener;
  39. import java.net.URI;
  40. import java.net.URISyntaxException;
  41. import java.util.HashMap;
  42. import java.util.Map;
  43. import java.util.Map.Entry;
  44. import java.util.Set;
  45. import java.util.stream.Collectors;
  46. import javax.inject.Inject;
  47. import javax.swing.JButton;
  48. import javax.swing.JPanel;
  49. import javax.swing.JScrollPane;
  50. import javax.swing.ListSelectionModel;
  51. import javax.swing.event.ListSelectionEvent;
  52. import javax.swing.event.ListSelectionListener;
  53. import net.miginfocom.swing.MigLayout;
  54. /**
  55. * URL Config panel. List all known url protocols and allows them to be configured.
  56. */
  57. public class URLConfigPanel extends JPanel implements
  58. ListSelectionListener, ActionListener, PreferencesInterface {
  59. /** Serial version UID. */
  60. private static final long serialVersionUID = 1;
  61. /** The global configuration to read settings from. */
  62. private final AggregateConfigProvider globalConfig;
  63. /** The user configuration to store settings in. */
  64. private final ConfigProvider userConfig;
  65. /** The icon manager to use for input dialogs. */
  66. private final IconManager iconManager;
  67. /** Protocol list. */
  68. private PackingTable table;
  69. /** Table mode. */
  70. private GenericTableModel<URLHandlerHolder> model;
  71. /** Table scrollpane. */
  72. private JScrollPane tableScrollPane;
  73. /** Protocol config panel. */
  74. private Map<URI, URLProtocolPanel> details;
  75. /** Empty info panel. */
  76. private URLProtocolPanel empty;
  77. /** Current component. */
  78. private URLProtocolPanel activeComponent;
  79. /** Add button. */
  80. private JButton add;
  81. /** Removed button. */
  82. private JButton remove;
  83. /** Selected row. */
  84. private int selectedRow;
  85. /** Parent window. */
  86. private final Window parentWindow;
  87. /**
  88. * Instantiates a new URL config panel.
  89. *
  90. * @param parentWindow Parent window
  91. * @param globalConfig The global configuration to read settings from.
  92. * @param userConfig The user configuration to write settings to.
  93. * @param iconManager The icon manager to use for input dialogs.
  94. */
  95. @Inject
  96. public URLConfigPanel(
  97. @MainWindow final Window parentWindow,
  98. @GlobalConfig final AggregateConfigProvider globalConfig,
  99. @UserConfig final ConfigProvider userConfig,
  100. final IconManager iconManager) {
  101. this.parentWindow = parentWindow;
  102. this.globalConfig = globalConfig;
  103. this.userConfig = userConfig;
  104. this.iconManager = iconManager;
  105. initComponents();
  106. addListeners();
  107. layoutComponents();
  108. selectedRow = -1;
  109. }
  110. /**
  111. * Initialises the components.
  112. */
  113. private void initComponents() {
  114. tableScrollPane = new JScrollPane();
  115. model = new GenericTableModel<>(URLHandlerHolder.class, "getUri", "getHandler");
  116. model.setHeaderNames("Protocol", "Handler");
  117. table = new PackingTable(model, tableScrollPane);
  118. table.setDefaultRenderer(URISchemeCellRenderer.class, new URISchemeCellRenderer());
  119. table.setDefaultRenderer(URIHandlerCellRenderer.class, new URIHandlerCellRenderer());
  120. table.setAutoCreateRowSorter(true);
  121. table.setAutoCreateColumnsFromModel(true);
  122. table.setColumnSelectionAllowed(false);
  123. table.setCellSelectionEnabled(false);
  124. table.setFillsViewportHeight(false);
  125. table.setRowSelectionAllowed(true);
  126. table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  127. table.getRowSorter().toggleSortOrder(0);
  128. details = new HashMap<>();
  129. empty = new URLProtocolPanel(globalConfig, userConfig, null, true);
  130. activeComponent = empty;
  131. add = new JButton("Add");
  132. remove = new JButton("Remove");
  133. remove.setEnabled(false);
  134. tableScrollPane.setViewportView(table);
  135. final Set<String> options = globalConfig.getOptions("protocol").keySet();
  136. for (final String option : options) {
  137. try {
  138. final URI uri = new URI(option + "://example.test.com");
  139. model.addValue(new URLHandlerHolder(uri, getURLHandler(uri)));
  140. details.put(uri, new URLProtocolPanel(globalConfig, userConfig, uri, true));
  141. } catch (final URISyntaxException ex) {
  142. //Ignore wont happen
  143. }
  144. }
  145. }
  146. private String getURLHandler(final URI uri) {
  147. if (globalConfig.hasOptionString("protocol", uri.getScheme())) {
  148. return globalConfig.getOption("protocol", uri.getScheme());
  149. } else {
  150. return "";
  151. }
  152. }
  153. /**
  154. * Adds listeners.
  155. */
  156. private void addListeners() {
  157. table.getSelectionModel().addListSelectionListener(this);
  158. add.addActionListener(this);
  159. remove.addActionListener(this);
  160. }
  161. /**
  162. * Lays out the components.
  163. */
  164. private void layoutComponents() {
  165. removeAll();
  166. setLayout(new MigLayout("ins 0, wrap 1, nocache"));
  167. add(tableScrollPane, "growx, pushx, h 150!");
  168. add(add, "split 2, growx, pushx");
  169. add(remove, "growx, pushx");
  170. add(activeComponent, "growx, pushx, wmax 100%");
  171. }
  172. @Override
  173. public void save() {
  174. valueChanged(null);
  175. final Map<URI, String> handlers = model.elements().stream()
  176. .collect(Collectors.toMap(URLHandlerHolder::getUri, URLHandlerHolder::getHandler));
  177. final Set<String> protocols = globalConfig.getOptions("protocol").keySet();
  178. for (final String protocol : protocols) {
  179. URI uri;
  180. try {
  181. uri = new URI(protocol + "://example.test.com");
  182. } catch (final URISyntaxException ex) {
  183. uri = null;
  184. }
  185. if (uri != null && handlers.containsKey(uri)) {
  186. saveHandler(protocol, handlers.get(uri));
  187. } else {
  188. saveHandler(protocol, "");
  189. }
  190. handlers.remove(uri);
  191. }
  192. for (final Entry<URI, String> entry : handlers.entrySet()) {
  193. saveHandler(entry.getKey().getScheme(), entry.getValue());
  194. }
  195. }
  196. /**
  197. * Saves or updates a handler to the config.
  198. *
  199. * @param protocol Protocol for the handler
  200. * @param handler Handler for the protocol
  201. */
  202. private void saveHandler(final String protocol, final String handler) {
  203. if (handler.isEmpty()) {
  204. userConfig.unsetOption("protocol", protocol);
  205. } else {
  206. userConfig.setOption("protocol", protocol, handler);
  207. }
  208. }
  209. @Override
  210. public void valueChanged(final ListSelectionEvent e) {
  211. if (e == null || !e.getValueIsAdjusting()) {
  212. setVisible(false);
  213. if (selectedRow != -1 && selectedRow < model.getRowCount()) {
  214. final URLProtocolPanel panel = details.get(model.getValue(selectedRow).getUri());
  215. model.getValue(selectedRow).setHandler(panel.getSelection());
  216. }
  217. if (table.getSelectedRow() == -1) {
  218. activeComponent = empty;
  219. layoutComponents();
  220. add.setEnabled(false);
  221. remove.setEnabled(false);
  222. selectedRow = -1;
  223. } else {
  224. activeComponent = details.get(model.getValue(table.getRowSorter().
  225. convertRowIndexToModel(table.getSelectedRow())).getUri());
  226. layoutComponents();
  227. add.setEnabled(true);
  228. remove.setEnabled(true);
  229. selectedRow = table.getRowSorter().convertRowIndexToModel(table.
  230. getSelectedRow());
  231. }
  232. setVisible(true);
  233. }
  234. }
  235. @Override
  236. public void actionPerformed(final ActionEvent e) {
  237. if (e.getSource() == add) {
  238. new StandardInputDialog(parentWindow,
  239. ModalityType.MODELESS, iconManager, "New URL handler",
  240. "Please enter the name of the new protocol.",
  241. new URLProtocolValidator(globalConfig), this::saveAddNewURLHandler).display();
  242. } else if (e.getSource() == remove) {
  243. model.removeValue(model.getValue(table.getRowSorter().convertRowIndexToModel(
  244. table.getSelectedRow())));
  245. }
  246. }
  247. private boolean saveAddNewURLHandler(final String text) {
  248. try {
  249. final URI uri = new URI(text + "://example.test.com");
  250. model.addValue(new URLHandlerHolder(uri, getURLHandler(uri)));
  251. details.put(uri, new URLProtocolPanel(globalConfig, userConfig, uri, true));
  252. return true;
  253. } catch (final URISyntaxException ex) {
  254. return false;
  255. }
  256. }
  257. }