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.

ConfigPanel.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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.nowplaying;
  18. import com.dmdirc.addons.ui_swing.UIUtilities;
  19. import com.dmdirc.addons.ui_swing.components.reorderablelist.ListReorderButtonPanel;
  20. import com.dmdirc.addons.ui_swing.components.reorderablelist.ReorderableJList;
  21. import com.dmdirc.addons.ui_swing.components.text.TextLabel;
  22. import com.dmdirc.config.prefs.PreferencesInterface;
  23. import com.dmdirc.config.provider.AggregateConfigProvider;
  24. import com.dmdirc.config.provider.ConfigProvider;
  25. import java.awt.event.KeyEvent;
  26. import java.awt.event.KeyListener;
  27. import java.util.Arrays;
  28. import java.util.LinkedList;
  29. import java.util.List;
  30. import java.util.Timer;
  31. import java.util.TimerTask;
  32. import java.util.concurrent.Callable;
  33. import javax.swing.BorderFactory;
  34. import javax.swing.JLabel;
  35. import javax.swing.JPanel;
  36. import javax.swing.JScrollPane;
  37. import javax.swing.JTextField;
  38. import javax.swing.SwingUtilities;
  39. import javax.swing.UIManager;
  40. import net.miginfocom.swing.MigLayout;
  41. import static com.google.common.base.Preconditions.checkNotNull;
  42. /**
  43. * Now playing plugin config panel.
  44. */
  45. public class ConfigPanel extends JPanel implements PreferencesInterface, KeyListener {
  46. /** A version number for this class. */
  47. private static final long serialVersionUID = 1;
  48. /** Now playing manager to get and handle sources. */
  49. private final NowPlayingManager manager;
  50. /** Global configuration to read settings from. */
  51. private final AggregateConfigProvider globalConfig;
  52. /** User settings to write settings to. */
  53. private final ConfigProvider userSettings;
  54. /** This plugin's settings domain. */
  55. private final String domain;
  56. /** Media sources. */
  57. private final List<String> sources;
  58. /** Media source order list. */
  59. private ReorderableJList<String> list;
  60. /** Text field for our setting. */
  61. private JTextField textfield;
  62. /** Panel that the preview is in. */
  63. private JPanel previewPanel;
  64. /** Label for previews. */
  65. private TextLabel preview;
  66. /** Update timer. */
  67. private Timer updateTimer;
  68. /**
  69. * Creates a new instance of ConfigPanel.
  70. *
  71. * @param manager Now playing manager to get and handle sources
  72. * @param globalConfig Global config to read from
  73. * @param userSettings Config to write settings to
  74. * @param domain This plugin's settings domain
  75. * @param sources A list of sources to be used in the panel
  76. */
  77. public ConfigPanel(
  78. final NowPlayingManager manager,
  79. final AggregateConfigProvider globalConfig,
  80. final ConfigProvider userSettings,
  81. final String domain,
  82. final List<String> sources) {
  83. this.manager = manager;
  84. this.globalConfig = globalConfig;
  85. this.userSettings = userSettings;
  86. this.domain = domain;
  87. this.sources = new LinkedList<>();
  88. this.sources.addAll(checkNotNull(sources));
  89. initComponents();
  90. }
  91. /**
  92. * Initialises the components.
  93. */
  94. private void initComponents() {
  95. list = new ReorderableJList<>();
  96. for (String source : sources) {
  97. list.getModel().add(source);
  98. }
  99. textfield = new JTextField(globalConfig.getOption(domain, "format"));
  100. textfield.addKeyListener(this);
  101. preview = new TextLabel("Preview:\n");
  102. setLayout(new MigLayout("fillx, ins 0"));
  103. JPanel panel = new JPanel();
  104. panel.setBorder(BorderFactory.createTitledBorder(UIManager.getBorder(
  105. "TitledBorder.border"), "Source order"));
  106. panel.setLayout(new MigLayout("fillx, ins 5"));
  107. panel.add(new JLabel("Drag and drop items to reorder"), "wrap");
  108. panel.add(new JScrollPane(list), "growx, pushx");
  109. panel.add(new ListReorderButtonPanel<>(list), "");
  110. add(panel, "growx, wrap");
  111. panel = new JPanel();
  112. panel.setBorder(BorderFactory.createTitledBorder(UIManager.getBorder(
  113. "TitledBorder.border"), "Output format"));
  114. panel.setLayout(new MigLayout("fillx, ins 5"));
  115. panel.add(textfield, "span, growx, wrap");
  116. panel.add(preview, "span, grow, wrap, gaptop 10");
  117. add(panel, "growx, wrap");
  118. previewPanel = panel;
  119. add(new NowPlayingSubsitutionPanel(Arrays.asList("app", "title", "artist", "album",
  120. "bitrate", "format", "length", "time", "state")), "growx");
  121. schedulePreviewUpdate();
  122. }
  123. /**
  124. * Updates the preview text.
  125. */
  126. private void updatePreview() {
  127. updateTimer.cancel();
  128. MediaSource source = manager.getBestSource();
  129. if (source == null) {
  130. source = new DummyMediaSource();
  131. }
  132. final String text = manager.doSubstitution(
  133. UIUtilities.invokeAndWait((Callable<String>) textfield::getText), source);
  134. SwingUtilities.invokeLater(() -> {
  135. preview.setText("Preview:\n" + text);
  136. preview.repaint();
  137. previewPanel.revalidate();
  138. revalidate();
  139. });
  140. }
  141. /**
  142. * Retrieves the (new) source order from this config panel.
  143. *
  144. * @return An ordered list of sources
  145. */
  146. public List<String> getSources() {
  147. return new LinkedList<>(list.getModel().elements());
  148. }
  149. @Override
  150. public void save() {
  151. userSettings.setOption(domain, "sourceOrder", getSources());
  152. userSettings.setOption(domain, "format", textfield.getText());
  153. }
  154. @Override
  155. public void keyTyped(final KeyEvent e) {
  156. // Do nothing
  157. }
  158. @Override
  159. public void keyPressed(final KeyEvent e) {
  160. // Do nothing
  161. }
  162. @Override
  163. public void keyReleased(final KeyEvent e) {
  164. schedulePreviewUpdate();
  165. }
  166. /**
  167. * Schedules an update to the preview text.
  168. */
  169. private void schedulePreviewUpdate() {
  170. if (updateTimer != null) {
  171. updateTimer.cancel();
  172. }
  173. updateTimer = new Timer("Nowplaying config timer");
  174. updateTimer.schedule(new TimerTask() {
  175. @Override
  176. public void run() {
  177. try {
  178. updatePreview();
  179. } catch (LinkageError | Exception e) {
  180. preview.setText("Error updating preview: " + e.getMessage());
  181. }
  182. }
  183. }, 500);
  184. }
  185. /**
  186. * A dummy media source for use in previews.
  187. */
  188. private static class DummyMediaSource implements MediaSource {
  189. @Override
  190. public MediaSourceState getState() {
  191. return MediaSourceState.PLAYING;
  192. }
  193. @Override
  194. public String getAppName() {
  195. return "MyProgram";
  196. }
  197. @Override
  198. public String getArtist() {
  199. return "The Artist";
  200. }
  201. @Override
  202. public String getTitle() {
  203. return "Song about nothing";
  204. }
  205. @Override
  206. public String getAlbum() {
  207. return "Album 45";
  208. }
  209. @Override
  210. public String getLength() {
  211. return "3:45";
  212. }
  213. @Override
  214. public String getTime() {
  215. return "1:20";
  216. }
  217. @Override
  218. public String getFormat() {
  219. return "flac";
  220. }
  221. @Override
  222. public String getBitrate() {
  223. return "128";
  224. }
  225. }
  226. }