001: /*******************************************************************************
002: * Copyright (c) 2004 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Common Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/cpl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.examples.presentation.baretittle;
011:
012: import org.eclipse.jface.resource.JFaceResources;
013: import org.eclipse.swt.SWT;
014: import org.eclipse.swt.events.*;
015: import org.eclipse.swt.graphics.*;
016: import org.eclipse.swt.widgets.*;
017: import org.eclipse.ui.presentations.*;
018:
019: public class BareTitleStackPresentation extends StackPresentation {
020:
021: private static final String PART_DATA = "part";
022:
023: private static final int TITLE_HEIGHT = 22;
024:
025: private boolean activeFocus = false;
026:
027: /**
028: * Main widget for the presentation
029: */
030: private Composite presentationControl;
031: private Composite titleArea;
032: private Composite contentArea;
033:
034: /**
035: * Currently selected part
036: */
037: private IPresentablePart current;
038:
039: /**
040: * close button
041: */
042: private ToolItem close;
043:
044: /**
045: * View menu button
046: */
047: private ToolItem viewMenu;
048:
049: /**
050: * Minimize button
051: */
052: private ToolItem minView;
053:
054: private MouseListener mouseListener = new MouseAdapter() {
055: // If we single-click on an empty space on the toolbar, move focus to the
056: // active control
057: public void mouseDown(MouseEvent e) {
058: if (current != null) {
059: current.setFocus();
060: }
061: }
062: };
063:
064: /**
065: * Drag listener for regions outside the toolbar
066: */
067: Listener dragListener = new Listener() {
068: public void handleEvent(Event event) {
069: Point loc = new Point(event.x, event.y);
070: Control ctrl = (Control) event.widget;
071:
072: getSite().dragStart(ctrl.toDisplay(loc), false);
073: }
074: };
075:
076: public BareTitleStackPresentation(Composite parent,
077: IStackPresentationSite stackSite) {
078: super (stackSite);
079:
080: // Create a top-level control for the presentation.
081: presentationControl = new Composite(parent, SWT.NONE);
082: titleArea = new Composite(presentationControl, SWT.NONE);
083: contentArea = new Composite(presentationControl, SWT.NONE);
084: titleArea.addMouseListener(mouseListener);
085: PresentationUtil.addDragListener(titleArea, dragListener);
086:
087: // Add a dispose listener. This will call the presentationDisposed()
088: // method when the widget is destroyed.
089: presentationControl.addDisposeListener(new DisposeListener() {
090: public void widgetDisposed(DisposeEvent e) {
091: presentationDisposed();
092: }
093: });
094:
095: presentationControl.addPaintListener(new PaintListener() {
096: public void paintControl(PaintEvent e) {
097: Rectangle clientArea = presentationControl
098: .getClientArea();
099: e.gc.setLineWidth(1);
100: e.gc.setForeground(e.display
101: .getSystemColor(SWT.COLOR_DARK_GRAY));
102: e.gc.drawRectangle(clientArea.x, clientArea.y,
103: clientArea.width - 1, clientArea.height - 1);
104: Rectangle contentAreaBounds = contentArea.getBounds();
105: int ypos = contentAreaBounds.y - 1;
106: e.gc.drawLine(clientArea.x, ypos, clientArea.x
107: + clientArea.width - 1, ypos);
108: }
109: });
110:
111: titleArea.addPaintListener(new PaintListener() {
112: public void paintControl(PaintEvent e) {
113: Rectangle titleRect = titleArea.getClientArea();
114: GC gc = e.gc;
115: e.gc.setBackground(e.display
116: .getSystemColor(SWT.COLOR_GRAY));
117: if (activeFocus)
118: e.gc.setForeground(e.display
119: .getSystemColor(SWT.COLOR_BLUE));
120: else
121: e.gc.setForeground(e.display
122: .getSystemColor(SWT.COLOR_WHITE));
123: e.gc.fillGradientRectangle(titleRect.x, titleRect.y,
124: titleRect.x + titleRect.width, titleRect.y
125: + titleRect.height, true);
126:
127: if (current != null) {
128: int textWidth = titleRect.width - 1;
129: if (textWidth > 0) {
130: Font gcFont = gc.getFont();
131: gc.setFont(presentationControl.getFont());
132: String text = current.getTitle();
133:
134: Point extent = gc.textExtent(text,
135: SWT.DRAW_TRANSPARENT
136: | SWT.DRAW_MNEMONIC);
137: int textY = titleRect.y
138: + (titleRect.height - extent.y) / 2;
139:
140: if (activeFocus)
141: gc.setForeground(e.display
142: .getSystemColor(SWT.COLOR_WHITE));
143: else
144: gc.setForeground(e.display
145: .getSystemColor(SWT.COLOR_BLACK));
146: gc.setFont(JFaceResources.getBannerFont());
147: gc.drawText(text, titleRect.x + 3, textY,
148: SWT.DRAW_TRANSPARENT
149: | SWT.DRAW_MNEMONIC);
150: }
151: }
152: }
153: });
154: layout();
155: }
156:
157: protected void presentationDisposed() {
158: // Remove any listeners that were attached to any
159: // global Eclipse resources. This is necessary in order to prevent
160: // memory leaks.
161: }
162:
163: protected int getBorderWidth() {
164: return 1;
165: }
166:
167: public void layout() {
168: // Determine the inner bounds of the presentation
169: Rectangle presentationClientArea = presentationControl
170: .getClientArea();
171: presentationClientArea.x += getBorderWidth();
172: presentationClientArea.width -= getBorderWidth() * 2;
173: presentationClientArea.y += getBorderWidth();
174: presentationClientArea.height -= getBorderWidth() * 2;
175: titleArea.setBounds(presentationClientArea.x,
176: presentationClientArea.y, presentationClientArea.width,
177: presentationClientArea.y + TITLE_HEIGHT);
178: int yy = TITLE_HEIGHT + 2;
179: contentArea.setBounds(presentationClientArea.x,
180: presentationClientArea.y + yy,
181: presentationClientArea.width,
182: presentationClientArea.height - yy);
183:
184: titleArea.setBackground(titleArea.getDisplay().getSystemColor(
185: SWT.COLOR_DARK_GREEN));
186: contentArea.setBackground(titleArea.getDisplay()
187: .getSystemColor(SWT.COLOR_DARK_RED));
188:
189: // Position the view's widgets
190: if (current != null) {
191: Rectangle clientRectangle = contentArea.getBounds();
192: Point clientAreaStart = presentationControl.getParent()
193: .toControl(
194: presentationControl.toDisplay(
195: clientRectangle.x,
196: clientRectangle.y));
197: // current isn't parented by this widget hierarchy, the coordinates must be
198: // relative to the workbench window. The workbench window parents every
199: // part.
200: current.setBounds(new Rectangle(clientAreaStart.x,
201: clientAreaStart.y, clientRectangle.width,
202: clientRectangle.height));
203: }
204:
205: }
206:
207: public void setBounds(Rectangle bounds) {
208: presentationControl.setBounds(bounds);
209: layout();
210: }
211:
212: public void dispose() {
213: }
214:
215: public void setActive(int newState) {
216: activeFocus = (newState == AS_ACTIVE_FOCUS);
217: titleArea.redraw();
218: }
219:
220: public void setVisible(boolean isVisible) {
221: }
222:
223: public void setState(int state) {
224: }
225:
226: public Control getControl() {
227: return presentationControl;
228: }
229:
230: public void addPart(IPresentablePart newPart, Object cookie) {
231: newPart.setBounds(contentArea.getBounds());
232: this .current = newPart;
233: layout();
234: }
235:
236: public void removePart(IPresentablePart oldPart) {
237: }
238:
239: public void selectPart(IPresentablePart toSelect) {
240: }
241:
242: public StackDropResult dragOver(Control currentControl,
243: Point location) {
244: return null;
245: }
246:
247: public void showSystemMenu() {
248: }
249:
250: public void showPaneMenu() {
251: }
252:
253: public Control[] getTabList(IPresentablePart part) {
254: if (current != null) {
255: return new Control[] { current.getControl() };
256: } else {
257: return new Control[0];
258: }
259: }
260: }
|