Focus default button in PullResultDialog
[egit/eclipse.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / pull / PullResultDialog.java
blob226d50604441a068f25f339a3ba119a58de9afce
1 /*******************************************************************************
2 * Copyright (c) 2010, 2012 SAP AG and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License 2.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-2.0/
8 * SPDX-License-Identifier: EPL-2.0
10 * Contributors:
11 * Mathias Kinzler (SAP AG) - initial implementation
12 * Markus Keller <markus_keller@ch.ibm.com> - Show the repository name in the title of the Pull Result dialog
13 *******************************************************************************/
14 package org.eclipse.egit.ui.internal.pull;
16 import org.eclipse.egit.ui.Activator;
17 import org.eclipse.egit.ui.UIUtils;
18 import org.eclipse.egit.ui.internal.UIText;
19 import org.eclipse.egit.ui.internal.fetch.FetchResultDialog;
20 import org.eclipse.egit.ui.internal.merge.MergeResultDialog;
21 import org.eclipse.egit.ui.internal.rebase.RebaseResultDialog;
22 import org.eclipse.jface.dialogs.Dialog;
23 import org.eclipse.jface.dialogs.IDialogConstants;
24 import org.eclipse.jface.dialogs.IDialogSettings;
25 import org.eclipse.jface.layout.GridDataFactory;
26 import org.eclipse.jface.layout.GridLayoutFactory;
27 import org.eclipse.jgit.api.MergeResult;
28 import org.eclipse.jgit.api.MergeResult.MergeStatus;
29 import org.eclipse.jgit.api.PullResult;
30 import org.eclipse.jgit.api.RebaseResult;
31 import org.eclipse.jgit.api.RebaseResult.Status;
32 import org.eclipse.jgit.lib.Repository;
33 import org.eclipse.jgit.transport.FetchResult;
34 import org.eclipse.osgi.util.NLS;
35 import org.eclipse.swt.SWT;
36 import org.eclipse.swt.graphics.Point;
37 import org.eclipse.swt.layout.GridData;
38 import org.eclipse.swt.widgets.Button;
39 import org.eclipse.swt.widgets.Composite;
40 import org.eclipse.swt.widgets.Control;
41 import org.eclipse.swt.widgets.Group;
42 import org.eclipse.swt.widgets.Label;
43 import org.eclipse.swt.widgets.Shell;
45 /**
46 * Display the result of a pull.
47 * <p>
48 * Simply combines fetch and merge result dialogs into one dialog.
50 public class PullResultDialog extends Dialog {
51 private final Repository repo;
53 private final PullResult result;
55 private boolean persistSize;
57 /**
58 * @param shell
59 * @param repo
60 * @param result
62 public PullResultDialog(Shell shell, Repository repo, PullResult result) {
63 super(shell);
64 setShellStyle(getShellStyle() & ~SWT.APPLICATION_MODAL | SWT.SHELL_TRIM);
65 setBlockOnOpen(false);
66 this.repo = repo;
67 this.result = result;
68 persistSize = hasFetchResults() || hasMergeResults();
71 private boolean hasFetchResults() {
72 final FetchResult fetchResult = result.getFetchResult();
73 return fetchResult != null
74 && !fetchResult.getTrackingRefUpdates().isEmpty();
77 private boolean hasMergeResults() {
78 final MergeResult mergeResult = result.getMergeResult();
79 return mergeResult != null
80 && mergeResult.getMergeStatus() != MergeStatus.ALREADY_UP_TO_DATE;
83 private boolean hasRebaseResults() {
84 final RebaseResult rebaseResult = result.getRebaseResult();
85 return rebaseResult != null
86 && rebaseResult.getStatus() != Status.UP_TO_DATE;
89 @Override
90 protected Control createDialogArea(Composite parent) {
91 Composite main = new Composite(parent, SWT.NONE);
92 GridLayoutFactory.swtDefaults().applyTo(main);
93 GridDataFactory.fillDefaults().indent(0, 0).grab(true, true).applyTo(
94 main);
95 Group fetchResultGroup = new Group(main, SWT.SHADOW_ETCHED_IN);
96 fetchResultGroup
97 .setText(UIText.PullResultDialog_FetchResultGroupHeader);
98 GridLayoutFactory.fillDefaults().applyTo(fetchResultGroup);
99 GridDataFactory.fillDefaults().grab(true, true).applyTo(
100 fetchResultGroup);
101 FetchResult fRes = result.getFetchResult();
102 if (hasFetchResults()) {
103 GridLayoutFactory.fillDefaults().applyTo(fetchResultGroup);
104 FetchResultDialog dlg = new FetchResultDialog(getParentShell(),
105 repo, fRes, result.getFetchedFrom());
106 Control fresult = dlg.createFetchResultTable(fetchResultGroup);
107 Object layoutData = fresult.getLayoutData();
108 if (layoutData instanceof GridData)
109 GridDataFactory.createFrom((GridData) layoutData)
110 .hint(SWT.DEFAULT, 130).applyTo(fresult);
112 } else {
113 GridLayoutFactory.swtDefaults().applyTo(fetchResultGroup);
114 Label noResult = new Label(fetchResultGroup, SWT.NONE);
115 if (result.getFetchedFrom().equals(".")) //$NON-NLS-1$
116 noResult
117 .setText(UIText.PullResultDialog_NothingToFetchFromLocal);
118 else
119 noResult.setText(NLS.bind(
120 UIText.FetchResultDialog_labelEmptyResult, result
121 .getFetchedFrom()));
124 Group mergeResultGroup = new Group(main, SWT.SHADOW_ETCHED_IN);
125 mergeResultGroup
126 .setText(UIText.PullResultDialog_MergeResultGroupHeader);
127 if (hasMergeResults()) {
128 GridDataFactory.fillDefaults().grab(true, true).applyTo(
129 mergeResultGroup);
130 GridLayoutFactory.fillDefaults().applyTo(mergeResultGroup);
131 MergeResultDialog dlg = new MergeResultDialog(getParentShell(),
132 repo, result.getMergeResult());
133 dlg.createDialogArea(mergeResultGroup);
134 } else if (hasRebaseResults()) {
135 RebaseResultDialog.createFailedOrConflictsParts(mergeResultGroup,
136 result.getRebaseResult());
137 GridDataFactory.fillDefaults().grab(true, false).applyTo(
138 mergeResultGroup);
139 } else {
140 GridDataFactory.fillDefaults().grab(true, false).applyTo(
141 mergeResultGroup);
142 GridLayoutFactory.swtDefaults().applyTo(mergeResultGroup);
143 Label noResult = new Label(mergeResultGroup, SWT.NONE);
144 noResult
145 .setText(UIText.PullResultDialog_MergeAlreadyUpToDateMessage);
147 return main;
150 @Override
151 protected void createButtonsForButtonBar(Composite parent) {
152 Button okayButton = createButton(parent, IDialogConstants.OK_ID,
153 IDialogConstants.CLOSE_LABEL,
154 true);
155 okayButton.setFocus();
158 @Override
159 protected void configureShell(Shell newShell) {
160 super.configureShell(newShell);
161 newShell.setText(NLS.bind(
162 UIText.PullResultDialog_DialogTitle,
163 Activator.getDefault().getRepositoryUtil()
164 .getRepositoryName(repo)));
167 @Override
168 protected IDialogSettings getDialogBoundsSettings() {
169 return UIUtils.getDialogBoundSettings(getClass());
172 @Override
173 protected int getDialogBoundsStrategy() {
174 int strategy = DIALOG_PERSISTLOCATION;
175 if (persistSize)
176 strategy |= DIALOG_PERSISTSIZE;
177 return strategy;
180 @Override
181 protected Point getInitialSize() {
182 if (!persistSize) {
183 // For "small" dialogs with label-only results, use the default
184 // height and the persisted width
185 Point size = super.getInitialSize();
186 size.x = getPersistedSize().x;
187 return size;
189 return super.getInitialSize();
192 private Point getPersistedSize() {
193 boolean oldPersistSize = persistSize;
194 // This affects getDialogBoundsStrategy
195 persistSize = true;
196 try {
197 Point persistedSize = super.getInitialSize();
198 return persistedSize;
199 } finally {
200 persistSize = oldPersistSize;