summaryrefslogtreecommitdiff
path: root/cmpcj/src/de/schwarzrot/cmpc/app/SRMediaPlayer.java
blob: 73f62048dfe927b9375a20ec137f512b643895c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
 * ======================== legal notice ======================
 * 
 * File:      SRMediaPlayer.java
 * Created:   
 * Author:    <a href="mailto:geronimo013@gmx.de">Geronimo</a>
 * Project:   cmpc - a java frontend (client) part of compound media player
 *                   uses external players to play the media
 * 
 * CMP - compound media player
 * 
 * is a client/server mediaplayer intended to play any media from any workstation
 * without the need to export or mount shares. cmps is an easy to use backend
 * with a (ready to use) HTML-interface. Additionally the backend supports
 * authentication via HTTP-digest authorization.
 * cmpc is a client with vdr-like osd-menues.
 * 
 * Copyright (c) 2012 Reinhard Mantey, some rights reserved!
 * published under Creative Commons by-sa
 * For details see http://creativecommons.org/licenses/by-sa/3.0/
 * 
 * The cmp project's homepage is at http://projects.vdr-developer.org/projects/cmp
 * 
 * --------------------------------------------------------------
 */
package de.schwarzrot.cmpc.app;


import java.awt.CardLayout;
import java.awt.Container;
import java.io.File;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import de.schwarzrot.cmpc.domain.Config;
import de.schwarzrot.cmpc.ui.AppSelector;
import de.schwarzrot.cmpc.ui.MediaClient;


public class SRMediaPlayer implements IMediaPlayer {
    private static final String DEFAULT_TITLE = "client";
    private static final String USER_HOME = "user.home";
    private static final String TITLE_MASK = "Compound Media Player (%s)";
    private static final String MAIN = "MediaServerSelector";
    private static final String CLIENT = "MediaClient";


    public SRMediaPlayer(String[] args, ConfigFactory cf) {
        for (String a : args) {
            if (a.compareTo("-d") == 0 || a.compareTo("--debug") == 0)
                debug = true;
        }
        configFactory = cf;
    }


    public void createAndShowGUI() {
        JFrame.setDefaultLookAndFeelDecorated(false);

        config = configFactory.getConfig();
        try {
            for (LookAndFeelInfo lfi : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(lfi.getName())) {
                    UIManager.setLookAndFeel(lfi.getClassName());
                    JFrame.setDefaultLookAndFeelDecorated(true);
                }
            }
        } catch (Throwable t) {
            t.printStackTrace();
            System.exit(-1);
        }
        mainFrame = new JFrame("JMediaPlayer");

        //Add components to it.
        Container contentPane = mainFrame.getContentPane();
        setupMainView(contentPane);

        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle(DEFAULT_TITLE);
        mainFrame.setSize(1000, 700);
        mainFrame.setLocationRelativeTo(null); //center it
        mainFrame.setVisible(true);
    }


    @Override
    public Config getConfig() {
        return config;
    }


    public ConfigFactory getConfigFactory() {
        return configFactory;
    }


    @Override
    public String getCurrentMediaCenter() {
        return currentMediaCenter;
    }


    @Override
    public boolean isDebug() {
        return debug;
    }


    @Override
    public void selectApp() {
        setTitle(DEFAULT_TITLE);
        switchView(MAIN);
    }


    public void setConfigFactory(ConfigFactory configFactory) {
        this.configFactory = configFactory;
    }


    @Override
    public void startApp(String name, int port) {
        currentMediaCenter = name;
        setTitle(name);
        switchView(CLIENT);
        try {
            mediaClientPane.startConnection(name, port);
        } catch (Throwable t) {
            mediaClientPane.setActive(false);
            switchView(MAIN);
        }
    }


    protected void setTitle(String title) {
        mainFrame.setTitle(String.format(TITLE_MASK, title));
    }


    protected void setupMainView(Container clientArea) {
        stakker = new CardLayout();
        switchingPane = new JPanel(stakker);
        mediaClientPane = new MediaClient(CLIENT, this);
        appSelectorPane = new AppSelector(MAIN, config.getKnownServers());

        mediaClientPane.setup(this);
        appSelectorPane.setup(this);

        switchingPane.add(appSelectorPane, MAIN);
        switchingPane.add(mediaClientPane, CLIENT);
        clientArea.add(switchingPane);
    }


    protected void switchView(String name) {
        stakker.show(switchingPane, name);
    }


    public static void main(final String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                File configDir = new File(System.getProperty(USER_HOME), ".cmp");
                SRMediaPlayer app = new SRMediaPlayer(args, new ConfigFactory(configDir));
                app.createAndShowGUI();
            }
        });
    }

    private boolean debug = false;
    private JFrame mainFrame;
    private Config config;
    private CardLayout stakker;
    private JPanel switchingPane;
    private MediaClient mediaClientPane;
    private AppSelector appSelectorPane;
    private ConfigFactory configFactory;
    private String currentMediaCenter;
}