-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTabs.java
More file actions
174 lines (141 loc) · 4.38 KB
/
Tabs.java
File metadata and controls
174 lines (141 loc) · 4.38 KB
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
//
// This is the tabs for my tabbed notebook
// (c) Jimmy Larsson 1998
//
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
public final class Tabs extends Canvas implements MouseListener
{
protected final int TOP_TABINSET = 5;
protected final int LEFT_TABINSET = 2;
protected final int RIGHT_TABINSET = 2;
protected final int BOTTOM_TABINSET = 0;
protected final int BETWEEN_TABINSET = 5;
protected final int TEXT_TABINSET = 3;
protected int noOfTabs;
protected int selectedTab;
protected Vector tabNames;
protected Vector tabRectangles;
protected FontMetrics fm;
protected int fontHeight;
protected int fontBaseLine;
protected TabbedNotebook noteBook;
protected Color background = null;
protected Image buffer;
public Tabs (TabbedNotebook nb)
{
noteBook = nb;
tabNames = new Vector ();
tabRectangles = new Vector ();
noOfTabs = 0;
selectedTab = 1;
addMouseListener (this);
setSize(400,30);
}
public synchronized void addTab (String name)
{
noOfTabs++;
tabNames.addElement(name);
}
public void update(Graphics g)
{
paint(g);
}
public void paint (Graphics realG)
{
if (buffer == null)
{
buffer = createImage(getSize().width, getSize().height);
MediaTracker tr = new MediaTracker(this);
tr.addImage(buffer,1);
try {
tr.waitForID(1);
}catch(InterruptedException exp){}
}
// Paint everything into an invisible buffer first
Graphics g = buffer.getGraphics();
if (fm == null)
{
fm = g.getFontMetrics();
fontHeight = fm.getHeight();
fontBaseLine = TOP_TABINSET + fontHeight + TEXT_TABINSET - fm.getDescent() - 1;
}
// Get background color
if (background == null)
background = getBackground();
if (tabNames.size() != tabRectangles.size())
{
int nextXPos = LEFT_TABINSET;
for (Enumeration eNames = tabNames.elements(); eNames.hasMoreElements();)
{
String curTab = (String) eNames.nextElement();
int strWidth = fm.stringWidth(curTab);
tabRectangles.addElement(new Rectangle (nextXPos, TOP_TABINSET, strWidth + 2*TEXT_TABINSET, 2*TEXT_TABINSET + fontHeight));
nextXPos = nextXPos + strWidth + 2*TEXT_TABINSET + BETWEEN_TABINSET;
}
setSize(getSize().width, 2*TEXT_TABINSET + TOP_TABINSET + fontHeight + 1);
}
int i = 1;
Rectangle rSelected = null;
Enumeration eRects = tabRectangles.elements();
g.setColor(background);
for (Enumeration eNames = tabNames.elements(); eNames.hasMoreElements();)
{
String curTab = (String) eNames.nextElement();
Rectangle r = (Rectangle) eRects.nextElement();
g.draw3DRect (r.x, r.y, r.width, r.height, true);
g.setColor(Color.black);
g.drawString(curTab, r.x + TEXT_TABINSET, fontBaseLine);
if (i == selectedTab)
rSelected = r;
i++;
g.setColor(background);
}
g.setColor(background.brighter());
g.drawLine(0, TOP_TABINSET+2*TEXT_TABINSET+fontHeight, getSize().width, TOP_TABINSET+2*TEXT_TABINSET+fontHeight);
g.setColor(background);
if (rSelected != null)
g.drawLine(rSelected.x + 1, rSelected.y + rSelected.height, rSelected.x + rSelected.width - 1, rSelected.y + rSelected.height);
realG.drawImage(buffer, 0, 0, null);
g.dispose();
buffer = null;
}
// The Mouse listener interface
public void mouseEntered (MouseEvent e) {}
public void mouseExited (MouseEvent e) {}
public void mousePressed (MouseEvent e) {}
public void mouseReleased (MouseEvent e) {}
public void mouseClicked (MouseEvent e)
{
Point p = e.getPoint();
int i = 1;
int newSelected = selectedTab;
for (Enumeration eRect = tabRectangles.elements(); eRect.hasMoreElements(); i++)
{
if (((Rectangle) eRect.nextElement()).contains(p))
newSelected = i;
}
if (newSelected != selectedTab)
{
selectedTab = newSelected;
repaint();
if (noteBook != null)
noteBook.showPage((String) tabNames.elementAt(selectedTab - 1));
}
}
// A main for testing the tabs
public static void main (String[] args)
{
Frame f = new Frame("Tabs-test");
f.setLayout(new BorderLayout());
Tabs tabs = new Tabs(null);
tabs.addTab("Testg");
tabs.addTab("The worlds slowest tabs");
f.add("Center",tabs);
f.setSize(400,400);
f.pack();
f.show();
}
}