Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ConfigPanel.java 8.8KB

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