Subversion Repositories svn.mios

Rev

Rev 628 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
628 adamjking 1
/*
2
 * @(#)HexFileUploadDeviceManagerGUI.java   beta8   2006/04/23
3
 *
4
 * Copyright (C) 2008   Adam King (adamjking@optusnet.com.au)
5
 *
6
 * This application is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 2 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This application is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this application; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 */
20
 
21
package org.midibox.mios.gui;
22
 
23
import java.awt.BorderLayout;
24
import java.awt.FlowLayout;
25
import java.awt.event.ActionEvent;
26
import java.awt.event.ActionListener;
27
import java.util.Iterator;
28
import java.util.Observable;
29
import java.util.Observer;
30
import java.util.Vector;
31
 
32
import javax.swing.Box;
33
import javax.swing.JButton;
34
import javax.swing.JLabel;
35
import javax.swing.JPanel;
36
import javax.swing.JSeparator;
37
import javax.swing.JTabbedPane;
38
 
39
import org.midibox.mios.HexFileUploadDevice;
40
import org.midibox.mios.HexFileUploadDeviceManager;
41
import org.midibox.utils.gui.ImageLoader;
42
 
43
public class HexFileUploadDeviceManagerGUI extends JPanel implements
44
        ActionListener, Observer {
45
 
46
    private HexFileUploadDeviceManager hexFileUploadDeviceManager;
47
 
48
    private JTabbedPane tabbedPane;
49
 
50
    private Vector hexFileUploadGUIs;
51
 
52
    public HexFileUploadDeviceManagerGUI(
53
            HexFileUploadDeviceManager hexFileUploadDeviceManager) {
54
        super(new BorderLayout());
55
 
56
        this.hexFileUploadDeviceManager = hexFileUploadDeviceManager;
57
        hexFileUploadDeviceManager.addObserver(this);
58
 
59
        hexFileUploadGUIs = new Vector();
60
 
61
        add(createButtonPanel(), BorderLayout.NORTH);
62
        add(createTabPanel(), BorderLayout.CENTER);
63
 
64
        createGUIs();
65
    }
66
 
67
    private JPanel createButtonPanel() {
68
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 4, 4));
69
 
70
        JLabel label = new JLabel("MIOS Hex Upload:", JLabel.LEFT);
71
        label.setOpaque(true);
72
        buttonPanel.add(label);
73
        buttonPanel.add(Box.createHorizontalStrut(10));
74
 
75
        JButton button = new JButton("New Tab");
76
        button.setActionCommand("newUpload");
77
        button.setToolTipText("Open new upload tab");
78
        button.addActionListener(this);
79
        buttonPanel.add(button);
80
 
81
        button = new JButton("Close Tab");
82
        button.setActionCommand("closeUpload");
83
        button.setToolTipText("Close current upload tab");
84
        button.addActionListener(this);
85
        buttonPanel.add(button);
86
 
87
        return buttonPanel;
88
    }
89
 
90
    private JPanel createTabPanel() {
91
        JPanel tabPanel = new JPanel(new BorderLayout());
92
        tabbedPane = new JTabbedPane();
93
        tabPanel.add(tabbedPane, BorderLayout.CENTER);
94
        tabPanel.add(new JSeparator(JSeparator.HORIZONTAL), BorderLayout.NORTH);
95
        return tabPanel;
96
    }
97
 
98
    private void createGUIs() {
99
 
100
        Iterator it = hexFileUploadDeviceManager.getHexFileUploadDevices()
101
                .iterator();
102
 
103
        while (it.hasNext()) {
721 adamjking 104
            HexFileUploadDevice hexFileUploadDevice = (HexFileUploadDevice) it
105
                    .next();
628 adamjking 106
 
107
            boolean hasGUI = false;
108
 
109
            Iterator it2 = hexFileUploadGUIs.iterator();
110
 
111
            while (it2.hasNext()) {
112
 
721 adamjking 113
                HexFileUploadGUI hexFileUploadGUI = (HexFileUploadGUI) it2
114
                        .next();
115
 
116
                if (hexFileUploadGUI.getHexFileUpload() == hexFileUploadDevice
117
                        .getHexFileUpload()) {
628 adamjking 118
                    hasGUI = true;
119
                }
120
            }
121
 
122
            if (!hasGUI) {
721 adamjking 123
 
124
                HexFileUploadGUI hexFileUploadGUI = new HexFileUploadGUI(
125
                        hexFileUploadDevice.getHexFileUpload());
126
 
127
                hexFileUploadGUIs.add(hexFileUploadGUI);
128
 
129
                tabbedPane.addTab(
130
                        hexFileUploadDevice.getDeviceInfo().getName(),
131
                        ImageLoader.getImageIcon("hex.png"), hexFileUploadGUI);
628 adamjking 132
            }
133
        }
134
 
135
        it = ((Vector) hexFileUploadGUIs.clone()).iterator();
136
 
137
        while (it.hasNext()) {
138
 
721 adamjking 139
            HexFileUploadGUI hexFileUploadGUI = (HexFileUploadGUI) it.next();
628 adamjking 140
 
141
            boolean hasDevice = false;
142
 
143
            Iterator it2 = hexFileUploadDeviceManager.getHexFileUploadDevices()
144
                    .iterator();
145
 
146
            while (it2.hasNext()) {
721 adamjking 147
 
148
                HexFileUploadDevice hexFileUploadDevice = (HexFileUploadDevice) it2
149
                        .next();
150
 
151
                if (hexFileUploadGUI.getHexFileUpload() == hexFileUploadDevice
152
                        .getHexFileUpload()) {
628 adamjking 153
                    hasDevice = true;
154
                }
155
            }
156
 
157
            if (!hasDevice) {
721 adamjking 158
 
159
                hexFileUploadGUIs.remove(hexFileUploadGUI);
160
 
161
                tabbedPane.remove(hexFileUploadGUI);
628 adamjking 162
            }
163
        }
721 adamjking 164
 
165
        for (int i = 0; i < hexFileUploadDeviceManager
166
                .getHexFileUploadDevices().size(); i++) {
167
 
168
            HexFileUploadDevice hexFileUploadDevice = (HexFileUploadDevice) hexFileUploadDeviceManager
169
                    .getHexFileUploadDevices().elementAt(i);
170
 
171
            tabbedPane.setTitleAt(i, hexFileUploadDevice.getDeviceInfo()
172
                    .getName());
173
        }
628 adamjking 174
    }
175
 
176
    public void addNewUploadTab() {
177
        hexFileUploadDeviceManager.newHexFileUploadDevice();
178
    }
179
 
180
    public void closeUploadTab() {
181
        int selectedTab = tabbedPane.getSelectedIndex();
182
 
183
        if (selectedTab > -1) {
184
            hexFileUploadDeviceManager
185
                    .removeHexFileUploadDevice((HexFileUploadDevice) hexFileUploadDeviceManager
186
                            .getHexFileUploadDevices().elementAt(selectedTab));
187
        }
188
    }
189
 
190
    public void update(Observable observable, Object object) {
721 adamjking 191
 
628 adamjking 192
        if (observable == hexFileUploadDeviceManager) {
721 adamjking 193
 
628 adamjking 194
            createGUIs();
195
        }
196
    }
197
 
198
    public void actionPerformed(ActionEvent ae) {
199
 
200
        String command = ae.getActionCommand();
201
 
202
        if (command.equals("newUpload")) {
203
            addNewUploadTab();
204
        }
205
 
206
        if (command.equals("closeUpload")) {
207
            closeUploadTab();
208
        }
209
    }
210
}