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.5KB

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