Eclipseplugins
CherryPickResultDialog.java
1 package com.proalpha.pds.gitutils.cherrypick;
2 
3 import java.io.IOException;
4 import java.text.DateFormat;
5 import java.util.ArrayList;
6 import java.util.Arrays;
7 import java.util.List;
8 
9 import org.eclipse.core.resources.IProject;
10 import org.eclipse.core.runtime.IProgressMonitor;
11 import org.eclipse.core.runtime.SubMonitor;
12 import org.eclipse.egit.core.internal.util.ProjectUtil;
13 import org.eclipse.egit.ui.internal.push.PushBranchWizard;
14 import org.eclipse.egit.ui.internal.push.PushWizardDialog;
15 import org.eclipse.jface.dialogs.Dialog;
16 import org.eclipse.jface.dialogs.IDialogConstants;
17 import org.eclipse.jface.dialogs.MessageDialog;
18 import org.eclipse.jface.layout.GridDataFactory;
19 import org.eclipse.jface.layout.GridLayoutFactory;
20 import org.eclipse.jface.resource.JFaceResources;
21 import org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider;
22 import org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider.IStyledLabelProvider;
23 import org.eclipse.jface.viewers.ITreeContentProvider;
24 import org.eclipse.jface.viewers.LabelProvider;
25 import org.eclipse.jface.viewers.StyledString;
26 import org.eclipse.jface.viewers.TreeViewer;
27 import org.eclipse.jface.viewers.TreeViewerColumn;
28 import org.eclipse.jface.viewers.Viewer;
29 import org.eclipse.jgit.api.CherryPickResult;
30 import org.eclipse.jgit.api.CherryPickResult.CherryPickStatus;
31 import org.eclipse.jgit.lib.Constants;
32 import org.eclipse.jgit.lib.ObjectId;
33 import org.eclipse.jgit.lib.Ref;
34 import org.eclipse.jgit.lib.Repository;
35 import org.eclipse.jgit.revwalk.RevCommit;
36 import org.eclipse.swt.SWT;
37 import org.eclipse.swt.events.SelectionEvent;
38 import org.eclipse.swt.events.SelectionListener;
39 import org.eclipse.swt.layout.GridData;
40 import org.eclipse.swt.layout.GridLayout;
41 import org.eclipse.swt.widgets.Button;
42 import org.eclipse.swt.widgets.Combo;
43 import org.eclipse.swt.widgets.Composite;
44 import org.eclipse.swt.widgets.Control;
45 import org.eclipse.swt.widgets.Group;
46 import org.eclipse.swt.widgets.Label;
47 import org.eclipse.swt.widgets.Shell;
48 import org.eclipse.swt.widgets.Text;
49 import org.eclipse.swt.widgets.Tree;
50 import org.eclipse.swt.widgets.TreeColumn;
51 import org.eclipse.ui.PlatformUI;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54 
55 import com.proalpha.git.PaGit;
56 import com.proalpha.git.model.PaCherryPickResult;
57 import com.proalpha.pds.gitutils.external.DbAndBinaryOperation;
58 import com.proalpha.pds.gitutils.mylyn.CheckoutAction;
59 import com.proalpha.pds.gitutils.mylyn.ReconcileActionSettings;
60 import com.proalpha.pds.paconnector.Activator;
61 
62 public class CherryPickResultDialog extends Dialog {
63 
64  private final Logger logger = LoggerFactory.getLogger(CherryPickResultDialog.class);
65 
66  private final PaCherryPickResult result;
67  private final List<RevCommit> successfulPicks;
68  private final List<RevCommit> openPicks;
69  private Button btnPush;
70  private Label lblHint;
71  private CheckoutAction currentAction = CheckoutAction.NONE;
72  private IProgressMonitor monitor;
73 
74  @Override
75  protected Control createDialogArea(Composite parent) {
76 
77  Composite main = new Composite(parent, SWT.NONE);
78  GridLayoutFactory.swtDefaults().applyTo(main);
79  GridDataFactory.fillDefaults().indent(0, 0).grab(true, true).applyTo(main);
80  main.setLayout(new GridLayout(1, false));
81 
82  Composite composite = new Composite(main, SWT.NONE);
83  composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
84  composite.setLayout(new GridLayout(2, false));
85  Label lblNewLabel = new Label(composite, SWT.NONE);
86  lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
87  lblNewLabel.setText("Result Status:");
88 
89  Text text = new Text(composite, SWT.READ_ONLY);
90  text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
91  text.setText(result.getStatus().toString());
92  text.setFont(JFaceResources.getFont("Courier New"));
93 
94  Group grpPerformedcommits = new Group(main, SWT.NONE);
95  grpPerformedcommits.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
96  grpPerformedcommits.setText("Performed Commits");
97 
98  TreeViewer succViewer = new TreeViewer(grpPerformedcommits);
99  Tree succTree = succViewer.getTree();
100  succTree.setBounds(0, 22, 460, 130);
101  succViewer.setAutoExpandLevel(2);
102  fillTreeView(succViewer, this.successfulPicks);
103 
104  Group grpOpenCommits = new Group(main, SWT.NONE);
105  grpOpenCommits.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
106  grpOpenCommits.setText("Open Commits");
107 
108  TreeViewer openViewer = new TreeViewer(grpOpenCommits);
109  Tree openTree = openViewer.getTree();
110  openTree.setBounds(10, 22, 460, 90);
111  openViewer.setAutoExpandLevel(2);
112  fillTreeView(openViewer, this.openPicks);
113 
114  btnPush = new Button(main, SWT.CHECK);
115  btnPush.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
116  btnPush.addSelectionListener(new SelectionListener() {
117 
118  @Override
119  public void widgetSelected(SelectionEvent event) {
120  if (!btnPush.getSelection()) {
121  lblHint.setText("Hint: Dont't forget to push your picked branch to the remote Repository!");
122  } else {
123  lblHint.setText("");
124  }
125  main.layout(true);
126  }
127 
128  @Override
129  public void widgetDefaultSelected(SelectionEvent event) {
130  widgetSelected(event);
131  }
132  });
133  btnPush.setText("Push automatically to server");
134  btnPush.setSelection(true);
135 
136  /* Reconcile settings */
137 
138  Group grpCheckoutSettings = new Group(main, SWT.NONE);
139  grpCheckoutSettings.setText("Reconcile settings");
140  grpCheckoutSettings.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
141  grpCheckoutSettings.setLayout(new GridLayout(1, false));
142  Combo cmbCheckoutAction = new Combo(grpCheckoutSettings, SWT.CHECK);
143  cmbCheckoutAction.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
144  cmbCheckoutAction.addSelectionListener(new SelectionListener() {
145  public void widgetSelected(SelectionEvent selectedItem) {
146  currentAction = null;
147  if (selectedItem != null) {
148  currentAction = CheckoutAction.getAction(((Combo) selectedItem.getSource()).getText());
149  }
150  }
151 
152  @Override
153  public void widgetDefaultSelected(SelectionEvent e) {
154  // We don't need this
155  }
156  });
157  cmbCheckoutAction.setBounds(76, 22, 300, 16);
158  String[] checkoutActions = CheckoutAction.getActions();
159  cmbCheckoutAction.setItems(checkoutActions);
160 
161  int selectionIndex = determineSelectionIndex(checkoutActions, CheckoutAction.LOAD_XML.getValue());
162  cmbCheckoutAction.select(selectionIndex);
163 
164  lblHint = new Label(main, SWT.NONE);
165  lblHint.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
166 
167  if (result.getStatus() == CherryPickResult.CherryPickStatus.FAILED) {
168  lblHint.setText(
169  "Something wicked happended!\nThe cherrypick terribly failed :-/\nGo to history view to reset your branch.");
170  text.setForeground(parent.getDisplay().getSystemColor(SWT.COLOR_RED));
171  }
172  if (result.getStatus() == CherryPickResult.CherryPickStatus.OK) {
173  text.setForeground(parent.getDisplay().getSystemColor(SWT.COLOR_DARK_GREEN));
174  }
175  if (result.getStatus() == CherryPickResult.CherryPickStatus.CONFLICTING) {
176  lblHint.setText(
177  "There is a conflict in your pick.\nPlease resolve the conflict in Git Staging view,\ncommit your change and resume the cherrypick!");
178  text.setForeground(parent.getDisplay().getSystemColor(SWT.COLOR_BLUE));
179  }
180 
181  return main;
182  }
183 
184  private int determineSelectionIndex(String[] checkoutActions, String searchValue) {
185  int selectionIndex = 0;
186  for (String action : checkoutActions) {
187 
188  if (action.equals(searchValue)) {
189  break;
190  }
191  selectionIndex++;
192  }
193  return selectionIndex;
194  }
195 
201  protected CherryPickResultDialog(Shell parentShell, IProgressMonitor monitor, PaCherryPickResult cpResult) {
202  super(parentShell);
203  this.monitor = monitor;
204  this.result = cpResult;
205  this.successfulPicks = cpResult.getSuccessfulPicks();
206  this.openPicks = cpResult.getOpenPicks();
207  }
208 
209  @Override
210  protected void okPressed() {
211  CherryPickStatus status = this.result.getStatus();
212 
213  if (status == CherryPickStatus.OK && btnPush.getSelection()) {
214  try {
215  final Repository repository = PaGit.getInstance().getGit().getRepository();
216  PushBranchWizard wizard = null;
217  Ref ref = null;
218  String fullBranch = repository.getFullBranch();
219 
220  if (fullBranch != null && fullBranch.startsWith(Constants.R_HEADS))
221  ref = repository.exactRef(fullBranch);
222 
223  if (ref != null) {
224  wizard = new PushBranchWizard(repository, ref);
225  } else {
226  ObjectId id = repository.resolve(repository.getFullBranch());
227  wizard = new PushBranchWizard(repository, id);
228  }
229 
230  new PushWizardDialog(PlatformUI.getWorkbench().getDisplay().getActiveShell(), wizard).open();
231 
232  // only start an operation if the checkout-action is not "NONE"
233  if (currentAction != CheckoutAction.NONE) {
234  performReconcileJob(repository, currentAction, monitor);
235  }
236 
237  } catch (IOException ex) {
238  PlatformUI.getWorkbench().getDisplay().asyncExec(
239  () -> MessageDialog.openError(PlatformUI.getWorkbench().getDisplay().getActiveShell(),
240  ex.getMessage(), ex.getLocalizedMessage()));
241  }
242  }
243  super.okPressed();
244  }
245 
246  private void performReconcileJob(Repository repository, CheckoutAction checkoutAction, IProgressMonitor monitor) {
247 
248  IProject[] projects = ProjectUtil.getProjects(repository);
249  ArrayList<IProject> affectedProjects = new ArrayList<>(Arrays.asList(projects));
250  SubMonitor subMonitorArtifacts = SubMonitor.convert(monitor, "database reconcile and artifact update", 10);
251 
252  if (checkoutAction.equals(CheckoutAction.FULL_RECONCILE))
253  Activator.getDefault().getProjectManager().closeProjects(affectedProjects, subMonitorArtifacts);
254 
255  executeDbBinaryOperation(repository, checkoutAction, subMonitorArtifacts);
256 
257  if (checkoutAction.equals(CheckoutAction.FULL_RECONCILE))
258  Activator.getDefault().getProjectManager().openProjects(affectedProjects, subMonitorArtifacts);
259  }
260 
261  public void executeDbBinaryOperation(Repository repository, CheckoutAction checkoutAction,
262  SubMonitor subMonitorArtifacts) {
263  DbAndBinaryOperation updateDbBinaryOp = new DbAndBinaryOperation(repository,
264  new ReconcileActionSettings(checkoutAction));
265  updateDbBinaryOp.setProgressMonitor(subMonitorArtifacts);
266  updateDbBinaryOp.execute();
267  }
268 
269  @Override
270  protected void createButtonsForButtonBar(Composite parent) {
271  createButton(parent, IDialogConstants.OK_ID, IDialogConstants.CLOSE_LABEL, true);
272  }
273 
274  @Override
275  protected void configureShell(Shell newShell) {
276  super.configureShell(newShell);
277  newShell.setText("CherryPick Result: " + result.getStatus());
278  }
279 
280  private void fillTreeView(TreeViewer treeViewer, List<RevCommit> commits) {
281 
282  TreeViewerColumn treeViewerColumn_2 = new TreeViewerColumn(treeViewer, SWT.NONE);
283  TreeColumn trclmnNewColumn_2 = treeViewerColumn_2.getColumn();
284  trclmnNewColumn_2.setWidth(80);
285  trclmnNewColumn_2.setText("CommitID");
286  treeViewerColumn_2.setLabelProvider(new DelegatingStyledCellLabelProvider(new NameLabelProvider()));
287 
288  TreeViewerColumn treeViewerColumn_1 = new TreeViewerColumn(treeViewer, SWT.NONE);
289  TreeColumn trclmnNewColumn_1 = treeViewerColumn_1.getColumn();
290  trclmnNewColumn_1.setWidth(200);
291  trclmnNewColumn_1.setText("Message");
292  trclmnNewColumn_1.setAlignment(SWT.LEFT);
293  treeViewerColumn_1.setLabelProvider(new DelegatingStyledCellLabelProvider(new MessageLabelProvider()));
294 
295  TreeViewerColumn treeViewerColumn = new TreeViewerColumn(treeViewer, SWT.NONE);
296  TreeColumn trclmnNewColumn = treeViewerColumn.getColumn();
297  trclmnNewColumn.setWidth(100);
298  trclmnNewColumn.setText("Author");
299  trclmnNewColumn.setAlignment(SWT.LEFT);
300  treeViewerColumn.setLabelProvider(new DelegatingStyledCellLabelProvider(new AuthorLabelProvider()));
301 
302  TreeViewerColumn dateColumn = new TreeViewerColumn(treeViewer, SWT.NONE);
303  TreeColumn trclmnDateColumn = dateColumn.getColumn();
304  trclmnDateColumn.setWidth(70);
305  trclmnDateColumn.setText("Date");
306  trclmnDateColumn.setAlignment(SWT.LEFT);
307  dateColumn.setLabelProvider(new DelegatingStyledCellLabelProvider(new DateLabelProvider()));
308 
309  treeViewer.setContentProvider(new CommitProvider());
310  treeViewer.setInput(commits);
311 
312  treeViewer.getTree().setHeaderVisible(true);
313  treeViewer.getTree().setLinesVisible(true);
314  }
315 
316  public class CommitProvider implements ITreeContentProvider {
317 
318  public void inputChanged(Viewer v, Object oldInput, Object newInput) {
319  }
320 
321  @Override
322  public void dispose() {
323  }
324 
325  @Override
326  public Object[] getElements(Object inputElement) {
327  if (inputElement instanceof List) {
328  return ((List<?>) inputElement).toArray();
329  }
330  return new Object[0];
331  }
332 
333  @Override
334  public Object[] getChildren(Object parentElement) {
335  return null;
336  }
337 
338  @Override
339  public Object getParent(Object element) {
340  return null;
341  }
342 
343  @Override
344  public boolean hasChildren(Object element) {
345  return false;
346  }
347 
348  }
349 
350  class NameLabelProvider extends LabelProvider implements IStyledLabelProvider {
351 
352  @Override
353  public StyledString getStyledText(Object element) {
354  if (element instanceof RevCommit) {
355  RevCommit commit = (RevCommit) element;
356  return new StyledString(commit.getName().substring(0, 8));
357  }
358  return null;
359  }
360 
361  }
362 
363  class MessageLabelProvider extends LabelProvider implements IStyledLabelProvider {
364 
365  @Override
366  public StyledString getStyledText(Object element) {
367  if (element instanceof RevCommit) {
368  RevCommit commit = (RevCommit) element;
369  return new StyledString(commit.getFullMessage().replace("\n", " "));
370  }
371  return null;
372  }
373 
374  }
375 
376  class AuthorLabelProvider extends LabelProvider implements IStyledLabelProvider {
377 
378  @Override
379  public StyledString getStyledText(Object element) {
380  if (element instanceof RevCommit) {
381  RevCommit commit = (RevCommit) element;
382  return new StyledString(commit.getAuthorIdent().getName());
383  }
384  return null;
385  }
386 
387  }
388 
389  class DateLabelProvider extends LabelProvider implements IStyledLabelProvider {
390 
391  private DateFormat dateLabelFormat;
392 
393  public DateLabelProvider() {
394  dateLabelFormat = DateFormat.getDateInstance();
395  }
396 
397  @Override
398  public StyledString getStyledText(Object element) {
399  if (element instanceof RevCommit) {
400  RevCommit commit = (RevCommit) element;
401  return new StyledString(dateLabelFormat.format(commit.getCommitterIdent().getWhen()));
402  }
403  return null;
404  }
405 
406  }
407 }
CherryPickResultDialog(Shell parentShell, IProgressMonitor monitor, PaCherryPickResult cpResult)
static CheckoutAction getAction(String value)