/
BranchPresenter.java
308 lines (278 loc) · 9.66 KB
/
BranchPresenter.java
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
/*
* Copyright (c) 2012-2017 Red Hat, Inc.
* 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:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.che.ide.ext.git.client.branch;
import static org.eclipse.che.ide.api.notification.StatusNotification.DisplayMode.FLOAT_MODE;
import static org.eclipse.che.ide.api.notification.StatusNotification.Status.FAIL;
import static org.eclipse.che.ide.util.ExceptionUtils.getErrorCode;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import javax.validation.constraints.NotNull;
import org.eclipse.che.api.core.ErrorCodes;
import org.eclipse.che.api.git.shared.Branch;
import org.eclipse.che.api.git.shared.BranchListMode;
import org.eclipse.che.api.git.shared.CheckoutRequest;
import org.eclipse.che.ide.api.notification.NotificationManager;
import org.eclipse.che.ide.api.resources.Project;
import org.eclipse.che.ide.dto.DtoFactory;
import org.eclipse.che.ide.ext.git.client.GitLocalizationConstant;
import org.eclipse.che.ide.ext.git.client.GitServiceClient;
import org.eclipse.che.ide.ext.git.client.outputconsole.GitOutputConsole;
import org.eclipse.che.ide.ext.git.client.outputconsole.GitOutputConsoleFactory;
import org.eclipse.che.ide.processes.panel.ProcessesPanelPresenter;
import org.eclipse.che.ide.ui.dialogs.DialogFactory;
/**
* Presenter for displaying and work with branches.
*
* @author Ann Zhuleva
* @author Vlad Zhukovskyi
*/
@Singleton
public class BranchPresenter implements BranchView.ActionDelegate {
private static final String BRANCH_RENAME_COMMAND_NAME = "Git rename branch";
private static final String BRANCH_DELETE_COMMAND_NAME = "Git delete branch";
private static final String BRANCH_CHECKOUT_COMMAND_NAME = "Git checkout branch";
private static final String BRANCH_CREATE_COMMAND_NAME = "Git create branch";
private static final String BRANCH_LIST_COMMAND_NAME = "Git list of branches";
private final DtoFactory dtoFactory;
private final BranchView view;
private final GitOutputConsoleFactory gitOutputConsoleFactory;
private final ProcessesPanelPresenter consolesPanelPresenter;
private final DialogFactory dialogFactory;
private final GitServiceClient service;
private final GitLocalizationConstant constant;
private final NotificationManager notificationManager;
private Branch selectedBranch;
private Project project;
/** Create presenter. */
@Inject
BranchPresenter(
BranchView view,
DtoFactory dtoFactory,
GitServiceClient service,
GitLocalizationConstant constant,
NotificationManager notificationManager,
GitOutputConsoleFactory gitOutputConsoleFactory,
ProcessesPanelPresenter processesPanelPresenter,
DialogFactory dialogFactory) {
this.view = view;
this.dtoFactory = dtoFactory;
this.gitOutputConsoleFactory = gitOutputConsoleFactory;
this.consolesPanelPresenter = processesPanelPresenter;
this.dialogFactory = dialogFactory;
this.view.setDelegate(this);
this.service = service;
this.constant = constant;
this.notificationManager = notificationManager;
}
/** Open dialog if closed and shows branches. */
public void showBranches(Project project) {
this.project = project;
getBranches();
}
@Override
public void onClose() {
view.close();
view.setTextToSearchFilterLabel("");
view.clearSearchFilter();
}
@Override
public void onRenameClicked() {
if (selectedBranch.isRemote()) {
dialogFactory
.createConfirmDialog(
constant.branchConfirmRenameTitle(),
constant.branchConfirmRenameMessage(),
this::renameBranch,
null)
.show();
} else {
renameBranch();
}
}
private void renameBranch() {
final String selectedBranchName = getSelectedBranchName();
dialogFactory
.createInputDialog(
constant.branchTitleRename(),
constant.branchTypeRename(),
selectedBranchName,
0,
selectedBranchName.length(),
this::renameBranch,
null)
.show();
}
private void renameBranch(String newName) {
service
.branchRename(project.getLocation(), selectedBranch.getDisplayName(), newName)
.then(
ignored -> {
getBranches();
})
.catchError(
error -> {
handleError(error.getCause(), BRANCH_RENAME_COMMAND_NAME);
getBranches(); // rename of remote branch occurs in three stages, so needs update list
// of branches on view
});
}
/** @return name of branch, e.g. 'origin/master' -> 'master' */
private String getSelectedBranchName() {
String selectedBranchName = selectedBranch.getDisplayName();
String[] tokens = selectedBranchName.split("/");
return tokens.length > 0 ? tokens[tokens.length - 1] : selectedBranchName;
}
@Override
public void onDeleteClicked() {
service
.branchDelete(project.getLocation(), selectedBranch.getName(), true)
.then(
ignored -> {
getBranches();
})
.catchError(
error -> {
handleError(error.getCause(), BRANCH_DELETE_COMMAND_NAME);
});
}
@Override
public void onCheckoutClicked() {
final CheckoutRequest checkoutRequest = dtoFactory.createDto(CheckoutRequest.class);
if (selectedBranch.isRemote()) {
checkoutRequest.setTrackBranch(selectedBranch.getDisplayName());
} else {
checkoutRequest.setName(selectedBranch.getDisplayName());
}
service
.checkout(project.getLocation(), checkoutRequest)
.then(
ignored -> {
view.close();
})
.catchError(
error -> {
handleError(error.getCause(), BRANCH_CHECKOUT_COMMAND_NAME);
});
}
/** Get the list of branches. */
private void getBranches() {
service
.branchList(project.getLocation(), BranchListMode.from(view.getFilterValue()))
.then(
branches -> {
if (branches.isEmpty()) {
dialogFactory
.createMessageDialog(
constant.branchTitle(), constant.initCommitWasNotPerformed(), null)
.show();
} else {
view.setBranches(branches);
view.showDialogIfClosed();
}
})
.catchError(
error -> {
handleError(error.getCause(), BRANCH_LIST_COMMAND_NAME);
});
}
@Override
public void onCreateClicked() {
dialogFactory
.createInputDialog(
constant.branchCreateNew(),
constant.branchTypeNew(),
value -> {
if (value.isEmpty()) {
return;
}
service
.branchCreate(project.getLocation(), value, null)
.then(
branch -> {
getBranches();
})
.catchError(
error -> {
handleError(error.getCause(), BRANCH_CREATE_COMMAND_NAME);
});
},
null)
.show();
}
@Override
public void onBranchUnselected() {
selectedBranch = null;
view.setEnableCheckoutButton(false);
view.setEnableRenameButton(false);
view.setEnableDeleteButton(false);
}
@Override
public void onLocalRemoteFilterChanged() {
getBranches();
}
@Override
public void onSearchFilterChanged(String filter) {
view.setTextToSearchFilterLabel(filter);
}
@Override
public void onBranchSelected(@NotNull Branch branch) {
selectedBranch = branch;
boolean isActive = selectedBranch.isActive();
view.setEnableCheckoutButton(!isActive);
view.setEnableDeleteButton(!isActive);
view.setEnableRenameButton(true);
}
/**
* Handler some action whether some exception happened.
*
* @param exception exception what happened
* @param commandName name of the executed command
*/
void handleError(@NotNull Throwable exception, String commandName) {
if (getErrorCode(exception) == ErrorCodes.UNABLE_GET_PRIVATE_SSH_KEY) {
dialogFactory
.createMessageDialog(commandName, constant.messagesUnableGetSshKey(), null)
.show();
return;
}
String errorMessage = exception.getMessage();
if (errorMessage == null) {
switch (commandName) {
case BRANCH_CREATE_COMMAND_NAME:
errorMessage = constant.branchCreateFailed();
break;
case BRANCH_DELETE_COMMAND_NAME:
errorMessage = constant.branchDeleteFailed();
break;
case BRANCH_LIST_COMMAND_NAME:
errorMessage = constant.branchesListFailed();
break;
case BRANCH_RENAME_COMMAND_NAME:
errorMessage = constant.branchRenameFailed();
break;
case BRANCH_CHECKOUT_COMMAND_NAME:
errorMessage = constant.branchCheckoutFailed();
break;
}
}
GitOutputConsole console = gitOutputConsoleFactory.create(commandName);
printGitMessage(errorMessage, console);
consolesPanelPresenter.addCommandOutput(console);
notificationManager.notify(errorMessage, FAIL, FLOAT_MODE);
}
private void printGitMessage(String messageText, GitOutputConsole console) {
console.print("");
String[] lines = messageText.split("\n");
for (String line : lines) {
console.printError(line);
}
}
}