Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ConfigPanel.java 9.2KB

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