Subversion Repositories svn.mios

Rev

Rev 721 | 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
    }
741 adamjking 66
 
67
    public JTabbedPane getTabbedPane() {
68
        return tabbedPane;
69
    }
628 adamjking 70
 
71
    private JPanel createButtonPanel() {
72
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 4, 4));
73
 
74
        JLabel label = new JLabel("MIOS Hex Upload:", JLabel.LEFT);
75
        label.setOpaque(true);
76
        buttonPanel.add(label);
77
        buttonPanel.add(Box.createHorizontalStrut(10));
78
 
79
        JButton button = new JButton("New Tab");
80
        button.setActionCommand("newUpload");
81
        button.setToolTipText("Open new upload tab");
82
        button.addActionListener(this);
83
        buttonPanel.add(button);
84
 
85
        button = new JButton("Close Tab");
86
        button.setActionCommand("closeUpload");
87
        button.setToolTipText("Close current upload tab");
88
        button.addActionListener(this);
89
        buttonPanel.add(button);
90
 
91
        return buttonPanel;
92
    }
93
 
94
    private JPanel createTabPanel() {
95
        JPanel tabPanel = new JPanel(new BorderLayout());
96
        tabbedPane = new JTabbedPane();
97
        tabPanel.add(tabbedPane, BorderLayout.CENTER);
98
        tabPanel.add(new JSeparator(JSeparator.HORIZONTAL), BorderLayout.NORTH);
99
        return tabPanel;
100
    }
101
 
102
    private void createGUIs() {
103
 
104
        Iterator it = hexFileUploadDeviceManager.getHexFileUploadDevices()
105
                .iterator();
106
 
107
        while (it.hasNext()) {
721 adamjking 108
            HexFileUploadDevice hexFileUploadDevice = (HexFileUploadDevice) it
109
                    .next();
628 adamjking 110
 
111
            boolean hasGUI = false;
112
 
113
            Iterator it2 = hexFileUploadGUIs.iterator();
114
 
115
            while (it2.hasNext()) {
116
 
721 adamjking 117
                HexFileUploadGUI hexFileUploadGUI = (HexFileUploadGUI) it2
118
                        .next();
119
 
120
                if (hexFileUploadGUI.getHexFileUpload() == hexFileUploadDevice
121
                        .getHexFileUpload()) {
628 adamjking 122
                    hasGUI = true;
123
                }
124
            }
125
 
126
            if (!hasGUI) {
721 adamjking 127
 
128
                HexFileUploadGUI hexFileUploadGUI = new HexFileUploadGUI(
129
                        hexFileUploadDevice.getHexFileUpload());
130
 
131
                hexFileUploadGUIs.add(hexFileUploadGUI);
132
 
133
                tabbedPane.addTab(
134
                        hexFileUploadDevice.getDeviceInfo().getName(),
135
                        ImageLoader.getImageIcon("hex.png"), hexFileUploadGUI);
628 adamjking 136
            }
137
        }
138
 
139
        it = ((Vector) hexFileUploadGUIs.clone()).iterator();
140
 
141
        while (it.hasNext()) {
142
 
721 adamjking 143
            HexFileUploadGUI hexFileUploadGUI = (HexFileUploadGUI) it.next();
628 adamjking 144
 
145
            boolean hasDevice = false;
146
 
147
            Iterator it2 = hexFileUploadDeviceManager.getHexFileUploadDevices()
148
                    .iterator();
149
 
150
            while (it2.hasNext()) {
721 adamjking 151
 
152
                HexFileUploadDevice hexFileUploadDevice = (HexFileUploadDevice) it2
153
                        .next();
154
 
155
                if (hexFileUploadGUI.getHexFileUpload() == hexFileUploadDevice
156
                        .getHexFileUpload()) {
628 adamjking 157
                    hasDevice = true;
158
                }
159
            }
160
 
161
            if (!hasDevice) {
721 adamjking 162
 
163
                hexFileUploadGUIs.remove(hexFileUploadGUI);
164
 
165
                tabbedPane.remove(hexFileUploadGUI);
628 adamjking 166
            }
167
        }
721 adamjking 168
 
169
        for (int i = 0; i < hexFileUploadDeviceManager
170
                .getHexFileUploadDevices().size(); i++) {
171
 
172
            HexFileUploadDevice hexFileUploadDevice = (HexFileUploadDevice) hexFileUploadDeviceManager
173
                    .getHexFileUploadDevices().elementAt(i);
174
 
175
            tabbedPane.setTitleAt(i, hexFileUploadDevice.getDeviceInfo()
176
                    .getName());
177
        }
628 adamjking 178
    }
179
 
180
    public void addNewUploadTab() {
181
        hexFileUploadDeviceManager.newHexFileUploadDevice();
182
    }
183
 
184
    public void closeUploadTab() {
185
        int selectedTab = tabbedPane.getSelectedIndex();
186
 
187
        if (selectedTab > -1) {
188
            hexFileUploadDeviceManager
189
                    .removeHexFileUploadDevice((HexFileUploadDevice) hexFileUploadDeviceManager
190
                            .getHexFileUploadDevices().elementAt(selectedTab));
191
        }
192
    }
193
 
194
    public void update(Observable observable, Object object) {
721 adamjking 195
 
628 adamjking 196
        if (observable == hexFileUploadDeviceManager) {
721 adamjking 197
 
628 adamjking 198
            createGUIs();
199
        }
200
    }
201
 
202
    public void actionPerformed(ActionEvent ae) {
203
 
204
        String command = ae.getActionCommand();
205
 
206
        if (command.equals("newUpload")) {
207
            addNewUploadTab();
208
        }
209
 
210
        if (command.equals("closeUpload")) {
211
            closeUploadTab();
212
        }
213
    }
214
}