Skip to main content
summaryrefslogtreecommitdiffstats
blob: 90c9a963d17ff9c487a1f61a7df993bc92c094dc (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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
/*******************************************************************************
 * Copyright (c) 2010 Tasktop Technologies 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:
 *     Tasktop Technologies - initial API and implementation
 *     Itema AS - bug 325079 added support for build service messages
 *     Itema AS - bug 331008 automatic resize of view columns
 *******************************************************************************/

package org.eclipse.mylyn.internal.builds.ui.view;

import java.io.File;
import java.text.DateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.Set;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.impl.AdapterImpl;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.GroupMarker;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.jface.viewers.DecoratingStyledCellLabelProvider;
import org.eclipse.jface.viewers.IOpenListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.OpenEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.TreeViewerColumn;
import org.eclipse.mylyn.builds.core.BuildStatus;
import org.eclipse.mylyn.builds.core.IBuildElement;
import org.eclipse.mylyn.builds.core.IBuildPlan;
import org.eclipse.mylyn.builds.core.IBuildServer;
import org.eclipse.mylyn.builds.internal.core.BuildModel;
import org.eclipse.mylyn.builds.internal.core.util.BuildsConstants;
import org.eclipse.mylyn.builds.ui.BuildsUiConstants;
import org.eclipse.mylyn.commons.core.CoreUtil;
import org.eclipse.mylyn.commons.ui.AbstractColumnViewerSupport;
import org.eclipse.mylyn.commons.ui.CommonImages;
import org.eclipse.mylyn.commons.ui.TreeSorter;
import org.eclipse.mylyn.commons.ui.TreeViewerSupport;
import org.eclipse.mylyn.commons.ui.actions.CollapseAllAction;
import org.eclipse.mylyn.commons.ui.actions.ExpandAllAction;
import org.eclipse.mylyn.internal.builds.ui.BuildImages;
import org.eclipse.mylyn.internal.builds.ui.BuildToolTip;
import org.eclipse.mylyn.internal.builds.ui.BuildsUiInternal;
import org.eclipse.mylyn.internal.builds.ui.BuildsUiPlugin;
import org.eclipse.mylyn.internal.builds.ui.actions.RunBuildAction;
import org.eclipse.mylyn.internal.builds.ui.actions.ShowBuildOutputAction;
import org.eclipse.mylyn.internal.builds.ui.actions.ShowTestResultsAction;
import org.eclipse.mylyn.internal.builds.ui.commands.OpenHandler;
import org.eclipse.mylyn.internal.builds.ui.notifications.BuildsServiceMessageControl;
import org.eclipse.mylyn.internal.builds.ui.view.BuildContentProvider.Presentation;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.IShowInSource;
import org.eclipse.ui.part.IShowInTarget;
import org.eclipse.ui.part.IShowInTargetList;
import org.eclipse.ui.part.ShowInContext;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.ui.progress.IWorkbenchSiteProgressService;

/**
 * @author Steffen Pingel
 * @author Torkild U. Resheim
 */
public class BuildsView extends ViewPart implements IShowInTarget {

	private class BuildTreeSorter extends TreeSorter {

		@Override
		public int compare(TreeViewer viewer, Object e1, Object e2, int columnIndex) {
			if (e1 instanceof IBuildServer && e2 instanceof IBuildServer) {
				// keep server sort order stable for now
				int res = getComparator().compare(((IBuildElement) e1).getLabel(), ((IBuildElement) e2).getLabel());
				if (getSortDirection(viewer) == SWT.UP) {
					return -res;
				}
				return res;
			}
			if (e1 instanceof IBuildPlan && e2 instanceof IBuildPlan) {
				IBuildPlan p1 = (IBuildPlan) e1;
				IBuildPlan p2 = (IBuildPlan) e2;
				switch (columnIndex) {
				case -1: // default
					return CoreUtil.compare(p1.getLabel(), p2.getLabel());
				case 0: // build
					return CoreUtil.compare(p1.getStatus(), p2.getStatus());
				case 1: // summary
					return p1.getHealth() - p2.getHealth();
				case 2: // last build
					Long t1 = (p1.getLastBuild() != null) ? p1.getLastBuild().getTimestamp() : null;
					Long t2 = (p2.getLastBuild() != null) ? p2.getLastBuild().getTimestamp() : null;
					return CoreUtil.compare(t1, t2);
				}
			}
			return super.compare(viewer, e1, e2, columnIndex);
		}

		@Override
		protected int compareDefault(TreeViewer viewer, Object e1, Object e2) {
			return compare(viewer, e1, e2, -1);
		}

	}

	public static BuildsView openInActivePerspective() {
		IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
		if (window != null) {
			IWorkbenchPage page = window.getActivePage();
			if (page != null) {
				try {
					return (BuildsView) page.showView(BuildsUiConstants.ID_VIEW_BUILDS);
				} catch (PartInitException e) {
					// ignore
				}
			}
		}
		return null;
	}

	private BuildStatusFilter buildStatusFilter;

	private Action collapseAllAction;

	private BuildContentProvider contentProvider;

	private Action expandAllAction;

	private FilterByStatusAction filterDisabledAction;

	private Date lastRefresh;

	private Composite messageComposite;

	private BuildModel model;

	private AdapterImpl modelListener;

	private BuildElementPropertiesAction propertiesAction;

	private final IPropertyChangeListener propertyChangeListener = new IPropertyChangeListener() {
		public void propertyChange(PropertyChangeEvent event) {
			if (org.eclipse.mylyn.internal.builds.ui.BuildsUiInternal.PREF_AUTO_REFRESH_ENABLED.equals(event.getProperty())) {
				Display.getDefault().asyncExec(new Runnable() {
					public void run() {
						if (refreshAutomaticallyAction != null) {
							refreshAutomaticallyAction.updateState();
						}
					}
				});
			}
		}
	};

	private RefreshAutomaticallyAction refreshAutomaticallyAction;

	private StackLayout stackLayout;

	private IMemento stateMemento;

	private TreeViewer viewer;

	private PresentationMenuAction presentationsMenuAction;

	private BuildToolTip toolTip;

	private BuildsServiceMessageControl serviceMessageControl;

	private TreeViewerSupport treeViewerSupport;

	public BuildsView() {
		BuildsUiPlugin.getDefault().initializeRefresh();
	}

	private void contributeToActionBars() {
		IActionBars bars = getViewSite().getActionBars();
		fillLocalPullDown(bars.getMenuManager());
		fillLocalToolBar(bars.getToolBarManager());
	}

	private void createMessage(Composite parent) {
		messageComposite = new Composite(parent, SWT.NONE);
		FillLayout layout = new FillLayout();
		layout.marginHeight = 5;
		layout.marginWidth = 5;
		messageComposite.setLayout(layout);
		//GridLayoutFactory.swtDefaults().applyTo(messageComposite);
		messageComposite.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND));

		Link link = new Link(messageComposite, SWT.WRAP);
		link.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
		link.setText("No build servers available. Create a <a href=\"create\">build server</a>...");
		link.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent event) {
				if ("create".equals(event.text)) {
					new NewBuildServerAction().run();
				}
			}
		});
	}

	@Override
	public void createPartControl(Composite parent) {
		Composite body = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout(1, false);
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		layout.horizontalSpacing = 0;
		layout.verticalSpacing = 0;
		layout.numColumns = 1;
		body.setLayout(layout);

		Composite composite = new Composite(body, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		stackLayout = new StackLayout();
		composite.setLayout(stackLayout);
		composite.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND));

		createMessage(composite);
		createViewer(composite);

		initActions();
		createPopupMenu(body);
		contributeToActionBars();

		toolTip = new BuildToolTip(getViewer().getControl());
		toolTip.setViewer(viewer);
		// bug#160897: set to empty string to disable native tooltips (windows only?)
		viewer.getTree().setToolTipText(""); //$NON-NLS-1$

		IWorkbenchSiteProgressService progress = (IWorkbenchSiteProgressService) getSite().getAdapter(
				IWorkbenchSiteProgressService.class);
		if (progress != null) {
			// show indicator for all running refreshes
			progress.showBusyForFamily(BuildsConstants.JOB_FAMILY);
		}

		model = BuildsUiInternal.getModel();
		modelListener = new BuildModelContentAdapter() {
			@Override
			public void doNotifyChanged(Notification msg) {
				if (!viewer.getControl().isDisposed()) {
					lastRefresh = new Date();
					updateContents(Status.OK_STATUS);
				}
			}
		};
		model.eAdapters().add(modelListener);

		if (stateMemento != null) {
			restoreState(stateMemento);
			stateMemento = null;
		}
		serviceMessageControl = new BuildsServiceMessageControl(body);

		viewer.setInput(model);
		viewer.setSorter(new BuildTreeSorter());
		viewer.expandAll();

		installAutomaticResize(viewer.getTree());

		getSite().setSelectionProvider(viewer);
		getSite().getSelectionProvider().addSelectionChangedListener(propertiesAction);
		propertiesAction.selectionChanged((IStructuredSelection) getSite().getSelectionProvider().getSelection());

		updateContents(Status.OK_STATUS);

		treeViewerSupport = new TreeViewerSupport(viewer, getStateFile());
		// Make sure we get notifications
		NotificationSinkProxy.setControl(serviceMessageControl);
	}

	/**
	 * Initializes automatic resize of the tree control columns. The size of these will be adjusted when a node is
	 * expanded or collapsed and when the tree changes size.
	 * 
	 * @param tree
	 *            the tree to resize
	 */
	private void installAutomaticResize(final Tree tree) {
		Listener listener = new Listener() {
			public void handleEvent(Event e) {
				packColumnsAsync(tree);
			}

		};
		// Automatically resize columns when we expand tree nodes.
		tree.addListener(SWT.Collapse, listener);
		tree.addListener(SWT.Expand, listener);
		tree.getParent().addListener(SWT.Resize, listener);
	}

	void packColumnsAsync(final Tree tree) {
		tree.getDisplay().asyncExec(new Runnable() {
			public void run() {
				if (!tree.isDisposed()) {
					try {
						tree.setRedraw(false);
						packColumns(tree);
					} finally {
						tree.setRedraw(true);
					}
				}
			}
		});
	}

	private File getStateFile() {
		IPath stateLocation = Platform.getStateLocation(BuildsUiPlugin.getDefault().getBundle());
		return stateLocation.append("BuildView.xml").toFile(); //$NON-NLS-1$
	}

	protected void createPopupMenu(Composite parent) {
		MenuManager menuManager = new MenuManager();

		menuManager.add(new GroupMarker("group.open"));
		menuManager.add(new Separator("group.edit"));
		menuManager.add(new Separator("group.file"));
		menuManager.add(new Separator("group.run"));
		menuManager.add(new Separator("group.refresh"));
		menuManager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
		menuManager.add(new Separator(BuildsUiConstants.GROUP_PROPERTIES));

		Menu contextMenu = menuManager.createContextMenu(parent);
		viewer.getTree().setMenu(contextMenu);
		getSite().registerContextMenu(menuManager, viewer);
	}

	protected void createViewer(Composite parent) {
//		Composite composite = new Composite(parent, SWT.NONE);
//		TreeColumnLayout treeColumnLayout = new TreeColumnLayout();
//		composite.setLayout(treeColumnLayout);

		viewer = new TreeViewer(parent, SWT.FULL_SELECTION);
		Tree tree = viewer.getTree();
		tree.setHeaderVisible(true);

		TreeViewerColumn buildViewerColumn = new TreeViewerColumn(viewer, SWT.LEFT);
		buildViewerColumn.setLabelProvider(new DecoratingStyledCellLabelProvider(new BuildLabelProvider(true),
				PlatformUI.getWorkbench().getDecoratorManager().getLabelDecorator(), null));
		TreeColumn buildColumn = buildViewerColumn.getColumn();
		buildColumn.setText("Build");
		buildColumn.setWidth(220);
		buildColumn.setData(AbstractColumnViewerSupport.KEY_COLUMN_CAN_HIDE, false);
		//treeColumnLayout.setColumnData(buildColumn, new ColumnWeightData(20, 50));

		TreeViewerColumn summaryViewerColumn = new TreeViewerColumn(viewer, SWT.LEFT);
		summaryViewerColumn.setLabelProvider(new BuildSummaryLabelProvider());
		TreeColumn summaryColumn = summaryViewerColumn.getColumn();
		summaryColumn.setText("Summary");
		summaryColumn.setWidth(220);
		//treeColumnLayout.setColumnData(summaryColumn, new ColumnWeightData(60, 200));

		TreeViewerColumn lastBuiltViewerColumn = new TreeViewerColumn(viewer, SWT.RIGHT);
		lastBuiltViewerColumn.setLabelProvider(new RelativeBuildTimeLabelProvider());
		TreeColumn lastBuiltColumn = lastBuiltViewerColumn.getColumn();
		lastBuiltColumn.setText("Last Built");
		lastBuiltColumn.setWidth(50);
		//treeColumnLayout.setColumnData(lastBuiltColumn, new ColumnWeightData(20, 50));

		contentProvider = new BuildContentProvider();
		contentProvider.setSelectedOnly(true);
		viewer.setContentProvider(contentProvider);

		viewer.addOpenListener(new IOpenListener() {
			public void open(OpenEvent event) {
				Object element = ((IStructuredSelection) event.getSelection()).getFirstElement();
				if (element instanceof IBuildElement) {
					OpenHandler.open(getSite().getPage(), Collections.singletonList((IBuildElement) element));
				}
			}
		});
	}

	@Override
	public void dispose() {
		super.dispose();
		model.eAdapters().remove(modelListener);
		BuildsUiPlugin.getDefault().getPreferenceStore().removePropertyChangeListener(propertyChangeListener);
	}

	private void fillLocalPullDown(IMenuManager manager) {
		manager.add(presentationsMenuAction);
		manager.add(new Separator());
		manager.add(collapseAllAction);
		manager.add(expandAllAction);
		manager.add(new Separator(BuildsUiConstants.GROUP_FILTER));
		manager.add(filterDisabledAction);
		manager.add(new Separator(BuildsUiConstants.GROUP_NAVIGATE));
		manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
		manager.add(new Separator(BuildsUiConstants.GROUP_PROPERTIES));
		manager.add(refreshAutomaticallyAction);
		manager.add(new Separator(BuildsUiConstants.GROUP_PREFERENCES));
		manager.add(new OpenBuildsPreferencesAction());
	}

	private void fillLocalToolBar(IToolBarManager manager) {
		manager.add(new NewBuildServerMenuAction());

		manager.add(new Separator(BuildsUiConstants.GROUP_REFRESH));

		RefreshAction refresh = new RefreshAction();
		manager.add(refresh);

		manager.add(new Separator(BuildsUiConstants.GROUP_OPEN));

		OpenWithBrowserAction openInBrowserAction = new OpenWithBrowserAction();
		openInBrowserAction.selectionChanged(StructuredSelection.EMPTY);
		viewer.addSelectionChangedListener(openInBrowserAction);
		manager.add(openInBrowserAction);

		RunBuildAction runBuildAction = new RunBuildAction();
		runBuildAction.selectionChanged(StructuredSelection.EMPTY);
		viewer.addSelectionChangedListener(runBuildAction);
		manager.add(runBuildAction);

		manager.add(new Separator(BuildsUiConstants.GROUP_FILE));

		ShowBuildOutputAction openConsoleAction = new ShowBuildOutputAction();
		openConsoleAction.selectionChanged(StructuredSelection.EMPTY);
		viewer.addSelectionChangedListener(openConsoleAction);
		manager.add(openConsoleAction);

		ShowTestResultsAction showTestResultsAction = new ShowTestResultsAction();
		showTestResultsAction.selectionChanged(StructuredSelection.EMPTY);
		viewer.addSelectionChangedListener(showTestResultsAction);
		manager.add(showTestResultsAction);
	}

	public BuildStatusFilter getBuildStatusFilter() {
		if (buildStatusFilter == null) {
			buildStatusFilter = new BuildStatusFilter();
			getViewer().addFilter(buildStatusFilter);
		}
		return buildStatusFilter;
	}

	public BuildContentProvider getContentProvider() {
		return contentProvider;
	}

	protected BuildStatus getPlanStatus() {
		BuildStatus planStatus = null;
		boolean plansSelected = false;
		if (contentProvider != null) {
			for (IBuildPlan plan : model.getPlans()) {
				if (plan.getStatus() != null) {
					switch (plan.getStatus()) {
					case SUCCESS:
						if (planStatus == null) {
							planStatus = BuildStatus.SUCCESS;
						}
						break;
					case UNSTABLE:
						if (planStatus == null || planStatus == BuildStatus.SUCCESS) {
							planStatus = BuildStatus.UNSTABLE;
						}
						break;
					case FAILED:
						planStatus = BuildStatus.FAILED;
						break;
					}
				}
				plansSelected = true;
			}
		}
		if (plansSelected) {
			return (planStatus != null) ? planStatus : BuildStatus.DISABLED;
		}
		return null;
	}

	TreeViewer getViewer() {
		return viewer;
	}

	@Override
	public void init(IViewSite site, IMemento memento) throws PartInitException {
		super.init(site, memento);

		BuildsUiPlugin.getDefault().getPreferenceStore().addPropertyChangeListener(propertyChangeListener);

		// defer restore until view is created
		this.stateMemento = memento;
	}

	private void initActions() {
		collapseAllAction = new CollapseAllAction(viewer);
		expandAllAction = new ExpandAllAction(viewer);
		propertiesAction = new BuildElementPropertiesAction();
		refreshAutomaticallyAction = new RefreshAutomaticallyAction();
		filterDisabledAction = new FilterByStatusAction(this, BuildStatus.DISABLED);
		presentationsMenuAction = new PresentationMenuAction(this);
	}

	private void restoreState(IMemento memento) {
		IMemento child = memento.getChild("statusFilter");
		if (child != null) {
			boolean changed = false;
			for (BuildStatus status : BuildStatus.values()) {
				Boolean value = child.getBoolean(status.name());
				if (value != null && value.booleanValue()) {
					getBuildStatusFilter().addFiltered(status);
					changed = true;
				}
			}
			if (changed) {
				filterDisabledAction.update();
			}
		}
		child = memento.getChild("presentation");
		if (child != null) {
			String id = child.getString("id");
			if (id != null) {
				try {
					getContentProvider().setPresentation(Presentation.valueOf(id));
				} catch (IllegalArgumentException e) {
					// use default
				}
			}
		}
	}

	@Override
	public void saveState(IMemento memento) {
		super.saveState(memento);

		if (buildStatusFilter != null) {
			Set<BuildStatus> statuses = buildStatusFilter.getFiltered();
			if (statuses.size() > 0) {
				IMemento child = memento.createChild("statusFilter");
				for (BuildStatus status : statuses) {
					child.putBoolean(status.name(), true);
				}
			}
		}
		IMemento child = memento.createChild("presentation");
		child.putString("id", getContentProvider().getPresentation().name());
	}

	@Override
	public void setFocus() {
		getViewer().getControl().setFocus();
	}

	private void setTopControl(Control control) {
		if (stackLayout.topControl != control) {
			stackLayout.topControl = control;
			control.getParent().layout();
		}
	}

	public boolean show(ShowInContext context) {
		if (context.getSelection() != null) {
			getViewer().setSelection(context.getSelection());
			return true;
		}
		return false;
	}

	private void updateContents(IStatus status) {
		boolean isShowingViewer = (stackLayout.topControl == viewer.getControl());
		boolean hasContents = false;
		if (contentProvider != null) {
			if (model.getPlans().size() > 0 || model.getServers().size() > 0) {
				hasContents = true;
			}
		}
		if (hasContents) {
			setTopControl(viewer.getControl());
			if (!isShowingViewer) {
				// initial flip
				packColumnsAsync(viewer.getTree());
			}
		} else {
			setTopControl(messageComposite);
		}
		updateDecoration(status);
	}

	public void updateDecoration(IStatus status) {
		String statusMessage = "";
		if (status.isOK()) {
			BuildStatus planStatus = getPlanStatus();
			if (planStatus == BuildStatus.SUCCESS) {
				setTitleImage(CommonImages.getImageWithOverlay(BuildImages.VIEW_BUILDS, CommonImages.OVERLAY_SUCCESS,
						false, false));
			} else if (planStatus == BuildStatus.UNSTABLE) {
				setTitleImage(CommonImages.getImageWithOverlay(BuildImages.VIEW_BUILDS, CommonImages.OVERLAY_FAILED,
						false, false));

			} else if (planStatus == BuildStatus.FAILED) {
				setTitleImage(CommonImages.getImageWithOverlay(BuildImages.VIEW_BUILDS, CommonImages.OVERLAY_ERROR,
						false, false));
			} else {
				setTitleImage(CommonImages.getImage(BuildImages.VIEW_BUILDS));
			}
			if (lastRefresh != null) {
				statusMessage = NLS.bind("Last update: {0}", DateFormat.getDateTimeInstance().format(lastRefresh));
			}
		} else {
			setTitleImage(CommonImages.getImageWithOverlay(BuildImages.VIEW_BUILDS, CommonImages.OVERLAY_WARNING,
					false, false));
			statusMessage = "Last update failed";
		}

		if (statusMessage != null) {
			final IViewSite viewSite = getViewSite();
			if (viewSite != null) {
				final IStatusLineManager statusLine = viewSite.getActionBars().getStatusLineManager();
				if (statusLine != null) {
					statusLine.setMessage(statusMessage);
				}
			}
		}
	}

	@Override
	public Object getAdapter(@SuppressWarnings("rawtypes")
	Class adapter) {
		if (adapter == IShowInTargetList.class) {
			return new IShowInTargetList() {
				public String[] getShowInTargetIds() {
					return new String[] { "org.eclipse.team.ui.GenericHistoryView" }; //$NON-NLS-1$
				}

			};
		} else if (adapter == IShowInSource.class) {
			return new IShowInSource() {
				public ShowInContext getShowInContext() {
					return new ShowInContext(getViewer().getInput(), getViewer().getSelection());
				}
			};
		}
		return super.getAdapter(adapter);
	}

	/**
	 * Packs all columns so that they are able to display all content. If there is any space left, the second column
	 * (summary) will be resized so that the entire width of the control is used.
	 * 
	 * @param tree
	 *            the tree to resize
	 */
	private void packColumns(final Tree tree) {
		int totalColumnWidth = 0;
		for (TreeColumn tc : tree.getColumns()) {
			if (tc.getResizable()) {
				tc.pack();
				totalColumnWidth += tc.getWidth() + 1;
			} else {
				totalColumnWidth += 1; // TODO: Determine the exact width of the column separator
			}
		}

		// Adjust the width of the "Summary" column unless it's not resizeable 
		// which case we adjust the width of the "Build" column instead.
		TreeColumn column = (tree.getColumn(1).getResizable()) ? tree.getColumn(1) : tree.getColumn(0);
		int nw = column.getWidth() + tree.getClientArea().width - totalColumnWidth;
		column.setWidth(nw);
	}

}

Back to the top