summaryrefslogtreecommitdiff
path: root/cmpcj/src/de/schwarzrot/control/config/ConfigDialog.java
blob: 4bc71c72370af8a4c4d27d4f9ea9ab0c4dc32598 (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
/**
 * ======================== legal notice ======================
 * 
 * File: ConfigDialog.java Created: 13. June 2012, 04:57 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.control.config;


import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.KeyStroke;
import ca.odell.glazedlists.BasicEventList;
import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.GlazedLists;
import ca.odell.glazedlists.swing.DefaultEventTableModel;
import de.schwarzrot.base.util.AbstractDialog;
import de.schwarzrot.base.util.ApplicationServiceProvider;
import de.schwarzrot.control.table.PlayerDefinitionTableFormat;
import de.schwarzrot.control.table.ServerDefinitionTableFormat;
import de.schwarzrot.media.domain.AbstractMediaNode;
import de.schwarzrot.media.domain.Config;
import de.schwarzrot.media.domain.MediaServer;
import de.schwarzrot.media.domain.PlayerDefinition;


public class ConfigDialog extends AbstractDialog {
    private static final long serialVersionUID = 713L;


    public ConfigDialog(Window parent) {
        super(parent, true, DialogMode.CANCEL_APPROVE, Orientation.Right);
        config = ApplicationServiceProvider.getService(Config.class);
    }


    @Override
    public JComponent createContentPane() {
        JTabbedPane rv = new JTabbedPane();

        rv.addTab("server", createServerTable());
        rv.addTab("player", createPlayerTable());

        return rv;
    }


    protected JComponent createPlayerTable() {
        playerDefinitions = new BasicEventList<PlayerDefinition>();
        for (AbstractMediaNode.SupportedMediaType mt : AbstractMediaNode.SupportedMediaType.values()) {
            if (mt == AbstractMediaNode.SupportedMediaType.Unknown)
                break;
            PlayerDefinition pd = config.getPlayerMap().get(mt);

            pd.setMediaType(mt);
            playerDefinitions.add(pd);
        }
        PlayerDefinitionTableFormat ptf = new PlayerDefinitionTableFormat();
        DefaultEventTableModel<PlayerDefinition> tm = new DefaultEventTableModel<PlayerDefinition>(playerDefinitions,
                ptf);
        JTable playerTable = new JTable(tm);
        JScrollPane playerTableScrollPane = new JScrollPane(playerTable);
        int mx = playerTable.getColumnModel().getColumnCount();

        for (int i = 0; i < mx; ++i) {
            int cw = ptf.getColumnWidth(i);

            if (cw > 0) {
                playerTable.getColumnModel().getColumn(i).setPreferredWidth(cw);
                playerTable.getColumnModel().getColumn(i).setMaxWidth(cw);
            }
        }
        return playerTableScrollPane;
    }


    protected void createServerDefinition() {
        serverDefinitions.add(new MediaServer());
    }


    protected JComponent createServerTable() {
        serverDefinitions = GlazedLists.eventList(config.getKnownServers());
        ServerDefinitionTableFormat stf = new ServerDefinitionTableFormat();
        DefaultEventTableModel<MediaServer> tm = new DefaultEventTableModel<MediaServer>(serverDefinitions, stf);
        JTable serverTable = new JTable(tm);
        JScrollPane serverTableScrollPane = new JScrollPane(serverTable);
        int mx = serverTable.getColumnModel().getColumnCount();

        for (int i = 0; i < mx; ++i) {
            int cw = stf.getColumnWidth(i);

            if (cw > 0) {
                serverTable.getColumnModel().getColumn(i).setPreferredWidth(cw);
                serverTable.getColumnModel().getColumn(i).setMaxWidth(cw);
            }
        }
        serverTable.getActionMap().put("createServerDefinition", new AbstractAction() {
            private static final long serialVersionUID = 1L;


            @Override
            public void actionPerformed(ActionEvent e) {
                createServerDefinition();
            }
        });
        serverTable.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_INSERT, 0), "createServerDefinition");

        return serverTableScrollPane;
    }


    @Override
    protected void performApprove() {
        System.out.println("should save config now!?!");
        List<MediaServer> cfgServers = config.getKnownServers();

        cfgServers.clear();
        cfgServers.addAll(serverDefinitions);
    }

    private EventList<MediaServer> serverDefinitions;
    private EventList<PlayerDefinition> playerDefinitions;
    private Config config;
}