001: /*******************************************************************************
002: * Copyright (c) 2004, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM - Initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.progress;
011:
012: import org.eclipse.jface.viewers.IContentProvider;
013: import org.eclipse.swt.SWT;
014: import org.eclipse.swt.events.MouseAdapter;
015: import org.eclipse.swt.events.MouseEvent;
016: import org.eclipse.swt.graphics.GC;
017: import org.eclipse.swt.graphics.Point;
018: import org.eclipse.swt.layout.GridData;
019: import org.eclipse.swt.layout.GridLayout;
020: import org.eclipse.swt.widgets.Composite;
021: import org.eclipse.swt.widgets.Control;
022: import org.eclipse.ui.internal.TrimUtil;
023: import org.eclipse.ui.internal.WorkbenchMessages;
024: import org.eclipse.ui.internal.WorkbenchWindow;
025: import org.eclipse.ui.internal.layout.IWindowTrim;
026:
027: /**
028: * The ProgressRegion is class for the region of the workbench where the
029: * progress line and the animation item are shown.
030: */
031: public class ProgressRegion implements IWindowTrim {
032: ProgressCanvasViewer viewer;
033:
034: ProgressAnimationItem animationItem;
035:
036: Composite region;
037:
038: WorkbenchWindow workbenchWindow;
039:
040: private int fWidthHint = SWT.DEFAULT;
041:
042: private int fHeightHint = SWT.DEFAULT;
043:
044: /**
045: * the side the receiver is placed on
046: */
047: private int side = SWT.BOTTOM;
048:
049: private boolean forceHorizontal;
050:
051: /**
052: * Create a new instance of the receiver.
053: */
054: public ProgressRegion() {
055: //No default behavior.
056: }
057:
058: /**
059: * Create the contents of the receiver in the parent. Use the window for the
060: * animation item.
061: *
062: * @param parent
063: * The parent widget of the composite.
064: * @param window
065: * The WorkbenchWindow this is in.
066: * @return Control
067: */
068: public Control createContents(Composite parent,
069: WorkbenchWindow window) {
070: workbenchWindow = window;
071:
072: // Test whether or not 'advanced' graphics are available
073: // If not then we'll 'force' the ProgressBar to always be
074: // HORIZONTAL...
075: //TODO: This should likely be at some 'global' level state
076: GC gc = new GC(parent);
077: gc.setAdvanced(true);
078: forceHorizontal = !gc.getAdvanced();
079: gc.dispose();
080:
081: region = new Composite(parent, SWT.NONE) {
082: /*
083: * (non-Javadoc)
084: *
085: * @see org.eclipse.swt.widgets.Composite#computeSize(int, int,
086: * boolean)
087: */
088: public Point computeSize(int wHint, int hHint,
089: boolean changed) {
090: Point size = super .computeSize(wHint, hHint, changed);
091: if (isHorizontal(side))
092: size.y = TrimUtil.TRIM_DEFAULT_HEIGHT;
093: else {
094: size.x = TrimUtil.TRIM_DEFAULT_HEIGHT;
095: }
096: return size;
097: }
098: };
099:
100: GridLayout gl = new GridLayout();
101: gl.marginHeight = 0;
102: gl.marginWidth = 0;
103: if (isHorizontal(side))
104: gl.numColumns = 3;
105: region.setLayout(gl);
106:
107: viewer = new ProgressCanvasViewer(region, SWT.NO_FOCUS, 1, 36,
108: isHorizontal(side) ? SWT.HORIZONTAL : SWT.VERTICAL);
109: viewer.setUseHashlookup(true);
110: Control viewerControl = viewer.getControl();
111: GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
112: Point viewerSizeHints = viewer.getSizeHints();
113: if (isHorizontal(side)) {
114: gd.widthHint = viewerSizeHints.x;
115: gd.heightHint = viewerSizeHints.y;
116: } else {
117: gd.widthHint = viewerSizeHints.y;
118: gd.heightHint = viewerSizeHints.x;
119: }
120: viewerControl.setLayoutData(gd);
121:
122: int widthPreference = AnimationManager.getInstance()
123: .getPreferredWidth() + 25;
124: animationItem = new ProgressAnimationItem(this ,
125: isHorizontal(side) ? SWT.HORIZONTAL : SWT.VERTICAL);
126: animationItem.createControl(region);
127:
128: animationItem
129: .setAnimationContainer(new AnimationItem.IAnimationContainer() {
130: /* (non-Javadoc)
131: * @see org.eclipse.ui.internal.progress.AnimationItem.IAnimationContainer#animationDone()
132: */
133: public void animationDone() {
134: //Add an extra refresh to the viewer in case
135: //of stale input if the controls are not disposed
136: if (viewer.getControl().isDisposed()) {
137: return;
138: }
139: viewer.refresh();
140: }
141:
142: /* (non-Javadoc)
143: * @see org.eclipse.ui.internal.progress.AnimationItem.IAnimationContainer#animationStart()
144: */
145: public void animationStart() {
146: // Nothing by default here.
147:
148: }
149: });
150: if (isHorizontal(side)) {
151: gd = new GridData(GridData.FILL_VERTICAL);
152: gd.widthHint = widthPreference;
153: } else {
154: gd = new GridData(GridData.FILL_HORIZONTAL);
155: gd.heightHint = widthPreference;
156: }
157:
158: animationItem.getControl().setLayoutData(gd);
159:
160: viewerControl.addMouseListener(new MouseAdapter() {
161: /*
162: * (non-Javadoc)
163: *
164: * @see org.eclipse.swt.events.MouseAdapter#mouseDoubleClick(org.eclipse.swt.events.MouseEvent)
165: */
166: public void mouseDoubleClick(MouseEvent e) {
167: processDoubleClick();
168: }
169: });
170:
171: //Never show debug info
172: IContentProvider provider = new ProgressViewerContentProvider(
173: viewer, false, false);
174: viewer.setContentProvider(provider);
175: viewer.setInput(provider);
176: viewer.setLabelProvider(new ProgressViewerLabelProvider(
177: viewerControl));
178: viewer.setComparator(ProgressManagerUtil
179: .getProgressViewerComparator());
180: return region;
181: }
182:
183: /**
184: * Return the animationItem for the receiver.
185: *
186: * @return AnimationItem
187: */
188: public AnimationItem getAnimationItem() {
189: return animationItem;
190: }
191:
192: /**
193: * Return the control for the receiver.
194: *
195: * @return Control
196: */
197: public Control getControl() {
198: return region;
199: }
200:
201: /**
202: * Process the double click event.
203: */
204: public void processDoubleClick() {
205: ProgressManagerUtil.openProgressView(workbenchWindow);
206: }
207:
208: /* (non-Javadoc)
209: * @see org.eclipse.ui.internal.IWindowTrim#dock(int)
210: */
211: public void dock(int dropSide) {
212: int oldSide = side;
213: side = dropSide;
214: if (oldSide == dropSide
215: || (isVertical(oldSide) && isVertical(dropSide))
216: || (isHorizontal(oldSide) && isHorizontal(dropSide)))
217: return;
218: recreate();
219:
220: }
221:
222: /**
223: * Answer true if the side is a horizonal one
224: *
225: * @param dropSide
226: * @return <code>true</code> if the side is horizontal
227: */
228: private boolean isHorizontal(int dropSide) {
229: if (forceHorizontal)
230: return true;
231: return dropSide == SWT.TOP || dropSide == SWT.BOTTOM;
232: }
233:
234: /**
235: * Answer true if the side is a horizonal one
236: *
237: * @param dropSide
238: * @return <code>true</code> if the side is horizontal
239: */
240: private boolean isVertical(int dropSide) {
241: if (forceHorizontal)
242: return false;
243: return dropSide == SWT.LEFT || dropSide == SWT.RIGHT;
244: }
245:
246: /**
247: * Recreate the receiver given the new side
248: */
249: private void recreate() {
250: if (region != null && !region.isDisposed()) {
251: Composite parent = region.getParent();
252: boolean animating = animationItem.animationRunning();
253: AnimationManager.getInstance().removeItem(animationItem);
254: region.dispose();
255: createContents(parent, workbenchWindow);
256: if (animating)
257: animationItem.animationStart();
258: }
259: }
260:
261: /* (non-Javadoc)
262: * @see org.eclipse.ui.internal.IWindowTrim#getId()
263: */
264: public String getId() {
265: return "org.eclipse.ui.internal.progress.ProgressRegion"; //$NON-NLS-1$
266: }
267:
268: public String getDisplayName() {
269: return WorkbenchMessages.TrimCommon_Progress_TrimName;
270: }
271:
272: /* (non-Javadoc)
273: * @see org.eclipse.ui.internal.IWindowTrim#getValidSides()
274: */
275: public int getValidSides() {
276: return SWT.BOTTOM | SWT.TOP | SWT.LEFT | SWT.RIGHT;
277: }
278:
279: /* (non-Javadoc)
280: * @see org.eclipse.ui.internal.IWindowTrim#isCloseable()
281: */
282: public boolean isCloseable() {
283: return false;
284: }
285:
286: /* (non-Javadoc)
287: * @see org.eclipse.ui.internal.IWindowTrim#handleClose()
288: */
289: public void handleClose() {
290: // nothing to do...
291: }
292:
293: /*
294: * (non-Javadoc)
295: *
296: * @see org.eclipse.ui.IWindowTrim#getWidthHint()
297: */
298: public int getWidthHint() {
299: return fWidthHint;
300: }
301:
302: /**
303: * Update the width hint for this control.
304: * @param w pixels, or SWT.DEFAULT
305: */
306: public void setWidthHint(int w) {
307: fWidthHint = w;
308: }
309:
310: /*
311: * (non-Javadoc)
312: *
313: * @see org.eclipse.ui.IWindowTrim#getHeightHint()
314: */
315: public int getHeightHint() {
316: return fHeightHint;
317: }
318:
319: /**
320: * Update the height hint for this control.
321: * @param h pixels, or SWT.DEFAULT
322: */
323: public void setHeightHint(int h) {
324: fHeightHint = h;
325: }
326:
327: /* (non-Javadoc)
328: * @see org.eclipse.ui.IWindowTrim#isResizeable()
329: */
330: public boolean isResizeable() {
331: return false;
332: }
333: }
|