Skip to main content
summaryrefslogtreecommitdiffstats
blob: 81114276dabf2b5ade23625b160e54b782e846e4 (plain) (blame)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*******************************************************************************
 * Copyright (c) 2004, 2006 Sybase, Inc. and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/

package org.eclipse.jst.jsf.facesconfig.ui.preference;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.runtime.ListenerList;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.FigureUtilities;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.MouseEvent;
import org.eclipse.draw2d.MouseListener;
import org.eclipse.draw2d.SchemeBorder;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.widgets.Composite;

public class TabbedTitleBarBorder extends SchemeBorder {
	// private int state = CompoundFigureListener.RESTORED;

	private IFigure parent;

	private ListenerList listenerList;

	private List tabList;

	private int currentTab;

	private Font font;

	private boolean visible = true;

	// from TitleBarBorder:
	private Color textColor = ColorConstants.black;

	private Color bgColor = ColorConstants.menuBackground;

	private Insets insets;

	private Insets padding = new Insets(2, 2, 2, 2);

	/**
	 * Constructs a TitleBarBorder with its label set to the name of this class.
	 * 
	 * @since 2.0
	 */
	public TabbedTitleBarBorder(final IFigure parent) {
		super(SCHEMES.LOWERED);

		this.parent = parent;

		parent.addMouseListener(new MouseListener() {
			public void mousePressed(MouseEvent me) {
				Insets padding_ = getPadding();
				Point mp = me.getLocation();
				mp.x -= padding_.left;
				mp.y -= padding_.top + 3; // 3==width of the outer border
				Point pp = parent.getBounds().getLocation();
				Rectangle tr = new Rectangle(pp.x, pp.y, 0, 0);
				//int activeIndex = -1;

				for (int i = 0; i < getTabList().size(); ++i) {
					Tab t = (Tab) tabList.get(i);
					Dimension d = t.getTextExtents();
					d.height += padding_.top + padding_.bottom;
					d.width += padding_.left;
					tr.setSize(d);
					if (tr.contains(mp)) {
						setCurrentTab(i);
						return;
					}
					tr.x += d.width;
				}
			}

			public void mouseReleased(MouseEvent me) {
                // do nothing: not handling release
			}

			public void mouseDoubleClicked(MouseEvent me) {
                // do nothing: not handling release
			}
		});
	}

	protected List getTabList() {
		if (tabList == null)
			tabList = new ArrayList();
		return tabList;
	}

	public int addTab(String text) {
		getTabList().add(new Tab(text));
		return getTabList().size() - 1;
	}

	public void removeTab(int index) {
		if (index >= 0 && index < getTabList().size()) {
			tabList.remove(index);
			if (index >= tabList.size())
				index = tabList.size() - 1;
			setCurrentTab(index);
		}
	}

	public void setCurrentTab(int newIndex) {
		if (newIndex >= 0 && newIndex < getTabList().size()) {
			Tab newTab = (Tab) tabList.get(newIndex);
			int oldIndex = -1;
			for (int i = 0; i < tabList.size(); ++i) {
				Tab t = (Tab) tabList.get(i);
				if (t.isActive()) {
					oldIndex = i;
					t.setActive(false);
					break;
				}
			}
			newTab.setActive(true);
			if (parent != null) {
				parent.invalidate();
				parent.repaint();
			}
			currentTab = newIndex;
			fireTabChanged(oldIndex, newIndex);
		}
	}

	public int getCurrentTab() {
		return currentTab;
	}

	public Object getContents(int index) {
		if (index >= 0 && index < getTabList().size())
			return ((Tab) tabList.get(index)).getContents();
		return null;
	}

	public void setContents(int index, Object contents) {
		if (index >= 0 && index < getTabList().size())
			((Tab) tabList.get(index)).setContents(contents);
	}

	public void addTabbedWindowListener(WindowFigureListener listener) {
		if (listenerList == null)
			listenerList = new ListenerList(ListenerList.IDENTITY);
		listenerList.add(listener);
	}

	public void removeTabbedWindowListener(WindowFigureListener listener) {
		if (listenerList != null)
			listenerList.remove(listener);
	}

	public Object[] getListeners() {
		return listenerList.getListeners();
	}

	protected void fireTabChanged(int oldIndex, int newIndex) {
		Object l[] = listenerList.getListeners();
		for (int i = 0; i < l.length; ++i) {
			if (l[i] instanceof WindowFigureListener)
				((WindowFigureListener) l[i]).tabChanged(oldIndex, newIndex);
		}
	}

	/**
	 * @return Returns the font.
	 */
	public Font getFont() {
		if (font == null) {
			font = parent.getFont();
			if (font == null)
				font = JFaceResources.getFontRegistry().get(
						JFaceResources.DEFAULT_FONT);
		}
		return font;
	}

	/**
	 * @param font
	 *            The font to set.
	 */
	public void setFont(Font font) {
		this.font = font;
		invalidate();
	}

	/**
	 * @return Returns the insets.
	 */
	public Insets getInsets() {
		return insets;
	}

	public void setTextColor(Color c) {
		textColor = c;
	}

	public Color getTextColor() {
		return textColor;
	}

	public void setBackgroundColor(Color c) {
		bgColor = c;
	}

	public Color getBackgroundColor() {
		return bgColor;
	}

	public void setPadding(Insets p) {
		padding = p;
		invalidate();
	}

	public Insets getPadding() {
		return padding;
	}

	public void setLabel(String text) {
		setLabel(currentTab, text);
	}

	public void setLabel(int index, String text) {
		if (index >= 0 && index < getTabList().size())
			((Tab) tabList.get(index)).setLabel(text);
	}

	public String getLabel() {
		return getLabel(currentTab);
	}

	public String getLabel(int index) {
		if (index >= 0 && index < getTabList().size())
			return ((Tab) tabList.get(index)).getLabel();
		return "";
	}

	public IFigure getParent() {
		return parent;
	}

	public void invalidate() {
		insets = null;
		for (int i = 0; i < getTabList().size(); ++i) {
			Tab t = (Tab) tabList.get(i);
			t.invalidate();
		}
	}

	public Dimension getTextExtents(IFigure f) {
		Dimension d = new Dimension(0, 0);
		for (int i = 0; i < getTabList().size(); ++i) {
			Tab t = (Tab) tabList.get(i);
			if (d.height == 0)
				d = t.getTextExtents();
			else
				d.width += t.getTextExtents().width;
		}
		return d;
	}

	/**
	 * Sets the min/max buttons visible
	 * 
	 * @param flag -
	 *            if true, buttons are made visible.
	 */
	public void setVisible(boolean flag) {
		visible = flag;
	}

	/**
	 * Calculates and returns the Insets for this border.
	 * 
	 * @param figure
	 *            the figure on which Insets calculations are based
	 * @return the calculated Insets
	 * @since 2.0
	 */
	protected Insets calculateInsets(IFigure figure) {
		insets = new Insets(super.getInsets(figure));
		insets.top = getTextExtents(figure).height;
		return insets;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.draw2d.Border#getInsets(org.eclipse.draw2d.IFigure)
	 */
	public Insets getInsets(IFigure figure) {
		if (insets == null)
			calculateInsets(figure);
		return insets;
	}

	/**
	 * @see org.eclipse.draw2d.Border#paint(IFigure, Graphics, Insets)
	 */
	public void paint(IFigure figure, Graphics g, Insets in) {
		if (!visible)
			return;

		Insets is = new Insets(in);
		getInsets(figure);
		is.top += insets.top;
		super.paint(figure, g, is);
		// Insets insets = getInsets(pane);
		// Insets padding = getPadding();
		// Insets in = new Insets(figureInsets);
		// in.top += insets.top;
		// super.paint(pane, g, in);

		tempRect.setBounds(getPaintRectangle(figure, in));
		Rectangle r = tempRect;
		r.height = Math.min(r.height, getTextExtents(figure).height);

		g.clipRect(r);
		g.setBackgroundColor(getBackgroundColor());
		g.fillRectangle(r);
		int x = r.x;
		int y = r.y;

		Iterator iter = getTabList().iterator();
		while (iter.hasNext()) {
			Tab t = (Tab) iter.next();
			t.paint(g, figure, x, y);
			x += t.getTextExtents().width;
		}

		g.setBackgroundColor(getBackgroundColor());
	}

	public Dimension getMinimumSize(int wHint, int hHint) {
		Dimension d = getTextExtents(parent);
		getInsets(parent);
		d.expand(insets.left + insets.right, insets.top + insets.bottom);
		// add enough width for the min/max buttons
		// d.width += minButton.getSize().width + maxButton.getSize().width;
		return d;
	}

	private class Tab {
		private String label = "";

		private Object contents = null;

		private Dimension textExtents;

		private boolean active;

		public Tab(String text) {
			this.label = text;
		}

		public void setContents(Object contents) {
			this.contents = contents;
		}

		public Object getContents() {
			return contents;
		}

		public void setActive(boolean active) {
			this.active = active;
		}

		public boolean isActive() {
			return active;
		}

		public String getLabel() {
			return label;
		}

		public void setLabel(String text) {
			if (text == null)
				this.label = "";
			else
				this.label = text;
			textExtents = null;
		}

		public Dimension calculateTextExtents() {
			textExtents = FigureUtilities.getTextExtents(label == null ? "W"
					: label, getFont());
			textExtents.width += getPadding().getWidth();
			textExtents.height += getPadding().getHeight();
			return textExtents;
		}

		public Dimension getTextExtents() {
			if (textExtents == null)
				calculateTextExtents();
			return textExtents.getCopy();
		}

		public void invalidate() {
			textExtents = null;
		}

		public void paint(Graphics g, IFigure f, int x, int y) {
			if (contents instanceof Composite) {
				return;
			}
			IFigure pane = (IFigure) contents;
			getTextExtents();

			Insets p = getPadding();
			int w = textExtents.width;
			int h = textExtents.height;
			int radius = Math.max(p.getWidth(), p.getHeight()) + 2;

			// CR408950: BP Save problems
			// fixed icon label refresh problems
			if (getTabList().size() > 1) {
				// only draw tabList if there are more than 1
				if (active)
					g.setBackgroundColor(pane == null ? ColorConstants.white
							: pane.getBackgroundColor());
				else
					g.setBackgroundColor(getBackgroundColor());

				--w;
				g.setForegroundColor(active ? ColorConstants.buttonDarkest
						: ColorConstants.buttonDarker);
				// g.setForegroundColor(ColorConstants.red);
				g.drawRectangle(x, y + h / 2, w, h);
				// g.setBackgroundColor(ColorConstants.green);
				g.fillRoundRectangle(new Rectangle(x, y, w, h), radius, radius);
				// g.setForegroundColor(ColorConstants.blue);
				g.drawRoundRectangle(new Rectangle(x, y, w, h), radius, radius);
				// g.setBackgroundColor(ColorConstants.yellow);
				g.fillRectangle(x + 1, y + h / 2, w - 1, h);
			} else
				g.setBackgroundColor(getBackgroundColor());

			g.setFont(getFont());
			g.setForegroundColor(getTextColor());
			if (label != null)
				g.drawString(label, x + padding.left + 1, y + padding.top);
		}
	}
}

Back to the top