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.

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