Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 829d35f51669a3e31c0f901d59e5624123faeb23 (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
/******************************************************************************
 *  Copyright (c) 2012, 2013 GitHub Inc and others.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License 2.0
 *  which accompanies this distribution, and is available at
 *  https://www.eclipse.org/legal/epl-2.0/
 *
 *  SPDX-License-Identifier: EPL-2.0
 *
 *  Contributors:
 *    Kevin Sawicki (GitHub Inc.) - initial API and implementation
 *    Stefan Lay (SAP AG)
 *****************************************************************************/
package org.eclipse.egit.ui.internal.stash;

import org.eclipse.core.resources.WorkspaceJob;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.egit.core.op.StashCreateOperation;
import org.eclipse.egit.ui.Activator;
import org.eclipse.egit.ui.JobFamilies;
import org.eclipse.egit.ui.UIPreferences;
import org.eclipse.egit.ui.UIUtils;
import org.eclipse.egit.ui.internal.UIText;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.window.Window;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;

/**
* The UI wrapper for {@link StashCreateOperation} */
public class StashCreateUI {

	private Repository repo;

	/**
	 * @param repo
	 */
	public StashCreateUI(Repository repo) {
		this.repo = repo;
	}

	/**
	 * @param shell
	 *            the shell to use for showing the message input dialog
	 * @return true if a stash create operation was triggered
	 */
	public boolean createStash(Shell shell) {
		if (!UIUtils.saveAllEditors(repo))
			return false;
		StashCreateDialog commitMessageDialog = new StashCreateDialog(shell);
		if (commitMessageDialog.open() != Window.OK)
			return false;
		String message = commitMessageDialog.getValue();
		if (message.length() == 0)
			message = null;

		boolean includeUntracked = commitMessageDialog.getIncludeUntracked();

		final StashCreateOperation op = new StashCreateOperation(repo, message,
				includeUntracked);
		Job job = new WorkspaceJob(UIText.StashCreateCommand_jobTitle) {

			@Override
			public IStatus runInWorkspace(IProgressMonitor monitor) {
				try {
					op.execute(monitor);
					RevCommit commit = op.getCommit();
					if (commit == null) {
						showNoChangesToStash();
					}
				} catch (CoreException e) {
					Activator
							.logError(UIText.StashCreateCommand_stashFailed, e);
				}
				return Status.OK_STATUS;
			}

			@Override
			public boolean belongsTo(Object family) {
				if (JobFamilies.STASH.equals(family))
					return true;
				return super.belongsTo(family);
			}
		};
		job.setUser(true);
		job.setRule(op.getSchedulingRule());
		job.schedule();
		return true;

	}

	private static void showNoChangesToStash() {
		PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {

			@Override
			public void run() {
				Shell shell = PlatformUI.getWorkbench()
						.getActiveWorkbenchWindow().getShell();
				MessageDialog.openInformation(shell,
						UIText.StashCreateCommand_titleNoChanges,
						UIText.StashCreateCommand_messageNoChanges);
			}
		});
	}

	private static class StashCreateDialog extends Dialog {

		/**
		 * Commit message widget.
		 */
		private Text text;

		/**
		 * Include untracked checkbox.
		 */
		private Button untrackedButton;

		/**
		 * The input value; the empty string by default.
		 */
		private String commitMessage = ""; //$NON-NLS-1$

		private boolean includeUntracked;

		private final IPreferenceStore store = Activator.getDefault()
				.getPreferenceStore();

		public StashCreateDialog(Shell shell) {
			super(shell);
		}

		@Override
		protected Control createDialogArea(Composite parent) {
			Composite composite = (Composite) super.createDialogArea(parent);

			getShell().setText(
					UIText.StashCreateCommand_titleEnterCommitMessage);

			Label label = new Label(composite, SWT.WRAP);
			label.setText(UIText.StashCreateCommand_messageEnterCommitMessage);
			GridData data = new GridData(GridData.GRAB_HORIZONTAL
					| GridData.GRAB_VERTICAL | GridData.HORIZONTAL_ALIGN_FILL
					| GridData.VERTICAL_ALIGN_CENTER);
			data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);
			label.setLayoutData(data);
			label.setFont(parent.getFont());

			text = new Text(composite, SWT.SINGLE | SWT.BORDER);
			text.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL
					| GridData.HORIZONTAL_ALIGN_FILL));

			untrackedButton = new Button(composite, SWT.CHECK);
			untrackedButton
					.setText(UIText.StashCreateCommand_includeUntrackedLabel);
			includeUntracked = store
					.getBoolean(UIPreferences.STASH_INCLUDE_UNTRACKED);
			untrackedButton.setSelection(includeUntracked);

			text.setFocus();
			return composite;
		}

		@Override
		protected void buttonPressed(int buttonId) {
			if (buttonId == IDialogConstants.OK_ID) {
				commitMessage = text.getText();
				includeUntracked = untrackedButton.getSelection();
				store.setValue(UIPreferences.STASH_INCLUDE_UNTRACKED,
						includeUntracked);
			} else {
				commitMessage = null;
				includeUntracked = false;
			}
			super.buttonPressed(buttonId);
		}

		public String getValue() {
			return commitMessage;
		}

		public boolean getIncludeUntracked() {
			return includeUntracked;
		}

		@Override
		protected void createButtonsForButtonBar(Composite parent) {
			createButton(parent, IDialogConstants.OK_ID,
					UIText.StashCreateCommand_ButtonOK, true);
			createButton(parent, IDialogConstants.CANCEL_ID,
					IDialogConstants.CANCEL_LABEL, false);
		}

	}
}

Back to the top