1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2005-2008 Grigor Iliev <grigor@grigoriliev.com> |
5 |
* |
6 |
* This file is part of JSampler. |
7 |
* |
8 |
* JSampler is free software; you can redistribute it and/or modify |
9 |
* it under the terms of the GNU General Public License version 2 |
10 |
* as published by the Free Software Foundation. |
11 |
* |
12 |
* JSampler is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with JSampler; if not, write to the Free Software |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
20 |
* MA 02111-1307 USA |
21 |
*/ |
22 |
|
23 |
package org.jsampler.view.fantasia.basic; |
24 |
|
25 |
import java.awt.Component; |
26 |
import java.awt.Composite; |
27 |
import java.awt.Graphics; |
28 |
import java.awt.Graphics2D; |
29 |
import java.awt.GridBagConstraints; |
30 |
import java.awt.GridBagLayout; |
31 |
import java.awt.Paint; |
32 |
import java.awt.RenderingHints; |
33 |
|
34 |
import java.awt.event.ItemEvent; |
35 |
import java.awt.event.ItemListener; |
36 |
|
37 |
import java.util.Vector; |
38 |
|
39 |
import javax.swing.ButtonGroup; |
40 |
import javax.swing.JPanel; |
41 |
|
42 |
import javax.swing.event.ChangeEvent; |
43 |
import javax.swing.event.ChangeListener; |
44 |
|
45 |
/** |
46 |
* |
47 |
* @author Grigor Iliev |
48 |
*/ |
49 |
public class FantasiaTabbedPane extends JPanel implements ItemListener { |
50 |
private final FantasiaTabPanel mainPane = new FantasiaTabPanel(); |
51 |
private final Vector<FantasiaTabButton> buttons = new Vector<FantasiaTabButton>(); |
52 |
private final Vector<Component> panes = new Vector<Component>(); |
53 |
|
54 |
private final ButtonGroup buttonGroup = new ButtonGroup(); |
55 |
|
56 |
private final Vector<ChangeListener> listeners = new Vector<ChangeListener>(); |
57 |
|
58 |
public |
59 |
FantasiaTabbedPane() { |
60 |
setOpaque(false); |
61 |
|
62 |
mainPane.setLayout(new java.awt.BorderLayout()); |
63 |
|
64 |
GridBagLayout gridbag = new GridBagLayout(); |
65 |
setLayout(gridbag); |
66 |
} |
67 |
|
68 |
public void |
69 |
addChangeListener(ChangeListener l) { listeners.add(l); } |
70 |
|
71 |
public void |
72 |
removeChangeListener(ChangeListener l) { listeners.remove(l); } |
73 |
|
74 |
private void |
75 |
fireChangeEvent() { |
76 |
ChangeEvent e = new ChangeEvent(this); |
77 |
for(ChangeListener l : listeners) l.stateChanged(e); |
78 |
} |
79 |
|
80 |
public JPanel |
81 |
getMainPane() { return mainPane; } |
82 |
|
83 |
public void |
84 |
addTab(String title, Component component) { |
85 |
FantasiaTabButton btn = new FantasiaTabButton(this, title); |
86 |
int idx = buttons.size(); |
87 |
|
88 |
GridBagConstraints c = new GridBagConstraints(); |
89 |
GridBagLayout gridbag = (GridBagLayout)getLayout(); |
90 |
|
91 |
c.gridx = idx; |
92 |
c.gridy = 0; |
93 |
c.fill = GridBagConstraints.HORIZONTAL; |
94 |
c.weightx = 0.5; |
95 |
gridbag.setConstraints(btn, c); |
96 |
add(btn); |
97 |
|
98 |
remove(mainPane); |
99 |
|
100 |
c.fill = GridBagConstraints.BOTH; |
101 |
c.weightx = 1.0; |
102 |
c.weighty = 1.0; |
103 |
c.gridx = 0; |
104 |
c.gridy = 1; |
105 |
c.gridwidth = idx + 1; |
106 |
gridbag.setConstraints(mainPane, c); |
107 |
add(mainPane); |
108 |
|
109 |
btn.setIndex(idx); |
110 |
buttonGroup.add(btn); |
111 |
buttons.add(btn); |
112 |
btn.addItemListener(this); |
113 |
panes.add(component); |
114 |
|
115 |
if(idx == 0) btn.doClick(0); |
116 |
} |
117 |
|
118 |
public int |
119 |
getTabCount() { return buttons.size(); } |
120 |
|
121 |
public FantasiaTabButton |
122 |
getTabButton(int index) { return buttons.get(index); } |
123 |
|
124 |
public int |
125 |
getSelectedIndex() { |
126 |
for(int i = 0; i < buttons.size(); i++) { |
127 |
if(buttons.get(i).isSelected()) return i; |
128 |
} |
129 |
|
130 |
return -1; |
131 |
} |
132 |
|
133 |
public void |
134 |
setSelectedIndex(int index) { |
135 |
if(buttons.get(index).isSelected()) return; |
136 |
buttons.get(index).doClick(0); |
137 |
} |
138 |
|
139 |
@Override |
140 |
public void |
141 |
itemStateChanged(ItemEvent e) { |
142 |
int idx = buttons.indexOf(e.getItem()); |
143 |
if(idx == -1) return; |
144 |
|
145 |
if (e.getStateChange() == ItemEvent.SELECTED) { |
146 |
mainPane.add(panes.get(idx)); |
147 |
fireChangeEvent(); |
148 |
} else { |
149 |
mainPane.remove(panes.get(idx)); |
150 |
} |
151 |
|
152 |
mainPane.revalidate(); |
153 |
mainPane.repaint(); |
154 |
} |
155 |
|
156 |
@Override |
157 |
public void |
158 |
removeAll() { |
159 |
for(FantasiaTabButton btn : buttons) { |
160 |
buttonGroup.remove(btn); |
161 |
btn.removeItemListener(this); |
162 |
} |
163 |
|
164 |
buttons.removeAllElements(); |
165 |
panes.removeAllElements(); |
166 |
|
167 |
super.removeAll(); |
168 |
} |
169 |
} |
170 |
|
171 |
class FantasiaTabPanel extends JPanel { |
172 |
public |
173 |
FantasiaTabPanel() { } |
174 |
|
175 |
@Override |
176 |
protected void |
177 |
paintComponent(Graphics g) { |
178 |
double h = getSize().getHeight(); |
179 |
double w = getSize().getWidth(); |
180 |
|
181 |
paintComponent((Graphics2D)g, 0, 0, w, h); |
182 |
} |
183 |
|
184 |
protected void |
185 |
paintComponent(Graphics2D g2, double x1, double y1, double width, double height) { |
186 |
Paint oldPaint = g2.getPaint(); |
187 |
Composite oldComposite = g2.getComposite(); |
188 |
|
189 |
g2.setRenderingHint ( |
190 |
RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF |
191 |
); |
192 |
|
193 |
FantasiaPainter.paintGradient(g2, x1, y1, x1 + width - 1, y1 + height - 1); |
194 |
|
195 |
FantasiaPainter.Border b = new FantasiaPainter.Border(false, true, true, true); |
196 |
FantasiaPainter.paintBoldOuterBorder(g2, x1, y1, x1 + width - 1, y1 + height - 1, b); |
197 |
|
198 |
g2.setPaint(oldPaint); |
199 |
g2.setComposite(oldComposite); |
200 |
} |
201 |
} |
202 |
|