Eclipseplugins
ActionResultDialog.java
1 package com.proalpha.pds.gitutils.mylyn;
2 
3 import java.util.List;
4 import java.util.regex.Matcher;
5 import java.util.regex.Pattern;
6 
7 import org.eclipse.jface.dialogs.Dialog;
8 import org.eclipse.jface.dialogs.IDialogConstants;
9 import org.eclipse.jface.layout.GridDataFactory;
10 import org.eclipse.jface.layout.GridLayoutFactory;
11 import org.eclipse.jface.resource.JFaceResources;
12 import org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider;
13 import org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider.IStyledLabelProvider;
14 import org.eclipse.jface.viewers.ITreeContentProvider;
15 import org.eclipse.jface.viewers.LabelProvider;
16 import org.eclipse.jface.viewers.StyledString;
17 import org.eclipse.jface.viewers.TreeViewer;
18 import org.eclipse.jface.viewers.TreeViewerColumn;
19 import org.eclipse.jface.viewers.Viewer;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.layout.GridData;
22 import org.eclipse.swt.layout.GridLayout;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Control;
25 import org.eclipse.swt.widgets.Group;
26 import org.eclipse.swt.widgets.Label;
27 import org.eclipse.swt.widgets.Shell;
28 import org.eclipse.swt.widgets.Text;
29 import org.eclipse.swt.widgets.Tree;
30 import org.eclipse.swt.widgets.TreeColumn;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 
34 public class ActionResultDialog extends Dialog {
35 
36  private final Logger logger = LoggerFactory.getLogger(ActionResultDialog.class);
37 
38  private final List<ActionResult> results;
39  private String status = "OK";
40 
41  @Override
42  protected Control createDialogArea(Composite parent) {
43 
44  Composite main = new Composite(parent, SWT.NONE);
45  GridLayoutFactory.swtDefaults().applyTo(main);
46  GridDataFactory.fillDefaults().indent(0, 0).grab(true, true).applyTo(main);
47  main.setLayout(new GridLayout(1, false));
48 
49  Composite composite = new Composite(main, SWT.NONE);
50  composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
51  composite.setLayout(new GridLayout(2, false));
52  Label lblNewLabel = new Label(composite, SWT.NONE);
53  lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
54  lblNewLabel.setText("Result Status:");
55 
56  Text text = new Text(composite, SWT.READ_ONLY);
57  text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
58  text.setText(this.status);
59  text.setFont(JFaceResources.getFont("Courier New"));
60  if (this.status.equalsIgnoreCase("OK"))
61  text.setForeground(parent.getDisplay().getSystemColor(SWT.COLOR_DARK_GREEN));
62  else
63  text.setForeground(parent.getDisplay().getSystemColor(SWT.COLOR_RED));
64 
65  Group grpPerformedActions = new Group(main, SWT.NONE);
66  grpPerformedActions.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
67  grpPerformedActions.setText("Performed Actions");
68 
69  TreeViewer succViewer = new TreeViewer(grpPerformedActions);
70  Tree succTree = succViewer.getTree();
71  succTree.setBounds(5, 20, 460, 250);
72  fillTreeView(succViewer, this.results);
73 
74  return main;
75  }
76 
81  public ActionResultDialog(Shell parentShell, List<ActionResult> results) {
82  super(parentShell);
83  this.results = results;
84 
85  for (ActionResult result : results) {
86  if (!result.getStatus().equalsIgnoreCase("OK"))
87  this.status = "WARNING";
88  }
89  }
90 
91  @Override
92  protected void createButtonsForButtonBar(Composite parent) {
93  createButton(parent, IDialogConstants.OK_ID, IDialogConstants.CLOSE_LABEL, true);
94  }
95 
96  @Override
97  protected void configureShell(Shell newShell) {
98  super.configureShell(newShell);
99  newShell.setText("Checkout Result: " + this.status);
100  }
101 
102  private void fillTreeView(TreeViewer treeViewer, List<ActionResult> results) {
103 
104  TreeViewerColumn treeViewerColumnStatus = new TreeViewerColumn(treeViewer, SWT.NONE);
105  TreeColumn trclmnNewColumnStatus = treeViewerColumnStatus.getColumn();
106  trclmnNewColumnStatus.setWidth(75);
107  trclmnNewColumnStatus.setText("Status");
108  treeViewerColumnStatus.setLabelProvider(new DelegatingStyledCellLabelProvider(new StatusLabelProvider()));
109 
110  TreeViewerColumn treeViewerColumnAction = new TreeViewerColumn(treeViewer, SWT.NONE);
111  TreeColumn trclmnNewColumnAction = treeViewerColumnAction.getColumn();
112  trclmnNewColumnAction.setWidth(600);
113  trclmnNewColumnAction.setText("Action");
114  trclmnNewColumnAction.setAlignment(SWT.LEFT);
115  treeViewerColumnAction.setLabelProvider(new DelegatingStyledCellLabelProvider(new ActionLabelProvider()));
116 
117  treeViewer.setContentProvider(new ResultProvider());
118  treeViewer.setInput(results);
119 
120  treeViewer.getTree().setHeaderVisible(true);
121  treeViewer.getTree().setLinesVisible(true);
122  }
123 
124  public class ResultProvider implements ITreeContentProvider {
125 
126  private final Object[] emptyArray = new Object[0];
127 
128  public void inputChanged(Viewer v, Object oldInput, Object newInput) {
129  }
130 
131  @Override
132  public void dispose() {
133  }
134 
135  @Override
136  public Object[] getElements(Object inputElement) {
137  if (inputElement instanceof List) {
138  return ((List<?>) inputElement).toArray();
139  }
140  return emptyArray;
141  }
142 
143  @Override
144  public Object[] getChildren(Object parentElement) {
145  if (parentElement instanceof ActionResult) {
146  ActionResult result = (ActionResult) parentElement;
147  return result.getLog().toArray();
148  }
149  return emptyArray;
150  }
151 
152  @Override
153  public Object getParent(Object element) {
154  return null;
155  }
156 
157  @Override
158  public boolean hasChildren(Object element) {
159  if (element instanceof ActionResult) {
160  ActionResult result = (ActionResult) element;
161  return !result.getLog().isEmpty();
162  }
163  return false;
164  }
165 
166  }
167 
168  class StatusLabelProvider extends LabelProvider implements IStyledLabelProvider {
169 
170  @Override
171  public StyledString getStyledText(Object element) {
172  if (element instanceof ActionResult) {
173  ActionResult result = (ActionResult) element;
174  return new StyledString(result.getStatus());
175  } else if (element instanceof String) {
176  return new StyledString("LOG");
177  }
178  return null;
179  }
180 
181  }
182 
183  class ActionLabelProvider extends LabelProvider implements IStyledLabelProvider {
184 
185  @Override
186  public StyledString getStyledText(Object element) {
187  if (element instanceof ActionResult) {
188  ActionResult result = (ActionResult) element;
189  return new StyledString(result.getAction());
190  } else if (element instanceof String) {
191  return new StyledString(getShortLog((String) element));
192  }
193  return null;
194  }
195 
196  private String getShortLog(String logLine) {
197 
198  Pattern p = Pattern.compile("([\\d-]+) ([\\d:]+)([,\\d]+) (DEBUG|WARNING|ERROR|INFO) \\[(.*?)\\] (.*)");
199  Matcher m = p.matcher(logLine);
200 
201  if (m.find())
202  return m.group(6);
203 
204  return logLine;
205 
206  }
207 
208  }
209 
210 }
ActionResultDialog(Shell parentShell, List< ActionResult > results)