123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- /*
- * Copyright (c) 2006-2017 DMDirc Developers
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
- * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
- * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
- * permit persons to whom the Software is furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
- * Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
- * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
- * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
- * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- */
-
- package com.dmdirc.addons.nowplaying;
-
- import com.dmdirc.addons.ui_swing.UIUtilities;
- import com.dmdirc.addons.ui_swing.components.reorderablelist.ListReorderButtonPanel;
- import com.dmdirc.addons.ui_swing.components.reorderablelist.ReorderableJList;
- import com.dmdirc.addons.ui_swing.components.text.TextLabel;
- import com.dmdirc.config.prefs.PreferencesInterface;
- import com.dmdirc.config.provider.AggregateConfigProvider;
- import com.dmdirc.config.provider.ConfigProvider;
-
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.util.Arrays;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Timer;
- import java.util.TimerTask;
- import java.util.concurrent.Callable;
-
- import javax.swing.BorderFactory;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JScrollPane;
- import javax.swing.JTextField;
- import javax.swing.SwingUtilities;
- import javax.swing.UIManager;
-
- import net.miginfocom.swing.MigLayout;
-
- import static com.google.common.base.Preconditions.checkNotNull;
-
- /**
- * Now playing plugin config panel.
- */
- public class ConfigPanel extends JPanel implements PreferencesInterface, KeyListener {
-
- /** A version number for this class. */
- private static final long serialVersionUID = 1;
- /** Now playing manager to get and handle sources. */
- private final NowPlayingManager manager;
- /** Global configuration to read settings from. */
- private final AggregateConfigProvider globalConfig;
- /** User settings to write settings to. */
- private final ConfigProvider userSettings;
- /** This plugin's settings domain. */
- private final String domain;
- /** Media sources. */
- private final List<String> sources;
- /** Media source order list. */
- private ReorderableJList<String> list;
- /** Text field for our setting. */
- private JTextField textfield;
- /** Panel that the preview is in. */
- private JPanel previewPanel;
- /** Label for previews. */
- private TextLabel preview;
- /** Update timer. */
- private Timer updateTimer;
-
- /**
- * Creates a new instance of ConfigPanel.
- *
- * @param manager Now playing manager to get and handle sources
- * @param globalConfig Global config to read from
- * @param userSettings Config to write settings to
- * @param domain This plugin's settings domain
- * @param sources A list of sources to be used in the panel
- */
- public ConfigPanel(
- final NowPlayingManager manager,
- final AggregateConfigProvider globalConfig,
- final ConfigProvider userSettings,
- final String domain,
- final List<String> sources) {
- this.manager = manager;
- this.globalConfig = globalConfig;
- this.userSettings = userSettings;
- this.domain = domain;
- this.sources = new LinkedList<>();
- this.sources.addAll(checkNotNull(sources));
-
- initComponents();
- }
-
- /**
- * Initialises the components.
- */
- private void initComponents() {
- list = new ReorderableJList<>();
-
- for (String source : sources) {
- list.getModel().add(source);
- }
-
- textfield = new JTextField(globalConfig.getOption(domain, "format"));
- textfield.addKeyListener(this);
- preview = new TextLabel("Preview:\n");
-
- setLayout(new MigLayout("fillx, ins 0"));
-
- JPanel panel = new JPanel();
-
- panel.setBorder(BorderFactory.createTitledBorder(UIManager.getBorder(
- "TitledBorder.border"), "Source order"));
- panel.setLayout(new MigLayout("fillx, ins 5"));
-
- panel.add(new JLabel("Drag and drop items to reorder"), "wrap");
- panel.add(new JScrollPane(list), "growx, pushx");
- panel.add(new ListReorderButtonPanel<>(list), "");
-
- add(panel, "growx, wrap");
-
- panel = new JPanel();
-
- panel.setBorder(BorderFactory.createTitledBorder(UIManager.getBorder(
- "TitledBorder.border"), "Output format"));
- panel.setLayout(new MigLayout("fillx, ins 5"));
-
- panel.add(textfield, "span, growx, wrap");
- panel.add(preview, "span, grow, wrap, gaptop 10");
- add(panel, "growx, wrap");
-
- previewPanel = panel;
-
- add(new NowPlayingSubsitutionPanel(Arrays.asList("app", "title", "artist", "album",
- "bitrate", "format", "length", "time", "state")), "growx");
- schedulePreviewUpdate();
- }
-
- /**
- * Updates the preview text.
- */
- private void updatePreview() {
- updateTimer.cancel();
-
- MediaSource source = manager.getBestSource();
-
- if (source == null) {
- source = new DummyMediaSource();
- }
-
- final String text = manager.doSubstitution(
- UIUtilities.invokeAndWait((Callable<String>) textfield::getText), source);
- SwingUtilities.invokeLater(() -> {
- preview.setText("Preview:\n" + text);
- preview.repaint();
- previewPanel.revalidate();
- revalidate();
- });
- }
-
- /**
- * Retrieves the (new) source order from this config panel.
- *
- * @return An ordered list of sources
- */
- public List<String> getSources() {
- return new LinkedList<>(list.getModel().elements());
- }
-
- @Override
- public void save() {
- userSettings.setOption(domain, "sourceOrder", getSources());
- userSettings.setOption(domain, "format", textfield.getText());
- }
-
- @Override
- public void keyTyped(final KeyEvent e) {
- // Do nothing
- }
-
- @Override
- public void keyPressed(final KeyEvent e) {
- // Do nothing
- }
-
- @Override
- public void keyReleased(final KeyEvent e) {
- schedulePreviewUpdate();
- }
-
- /**
- * Schedules an update to the preview text.
- */
- private void schedulePreviewUpdate() {
- if (updateTimer != null) {
- updateTimer.cancel();
- }
-
- updateTimer = new Timer("Nowplaying config timer");
- updateTimer.schedule(new TimerTask() {
-
- @Override
- public void run() {
- try {
- updatePreview();
- } catch (LinkageError | Exception e) {
- preview.setText("Error updating preview: " + e.getMessage());
- }
- }
- }, 500);
- }
-
- /**
- * A dummy media source for use in previews.
- */
- private static class DummyMediaSource implements MediaSource {
-
- @Override
- public MediaSourceState getState() {
- return MediaSourceState.PLAYING;
- }
-
- @Override
- public String getAppName() {
- return "MyProgram";
- }
-
- @Override
- public String getArtist() {
- return "The Artist";
- }
-
- @Override
- public String getTitle() {
- return "Song about nothing";
- }
-
- @Override
- public String getAlbum() {
- return "Album 45";
- }
-
- @Override
- public String getLength() {
- return "3:45";
- }
-
- @Override
- public String getTime() {
- return "1:20";
- }
-
- @Override
- public String getFormat() {
- return "flac";
- }
-
- @Override
- public String getBitrate() {
- return "128";
- }
-
- }
-
- }
|