Eclipseplugins
CheckTreeView.java
1 package com.proalpha.pds.checks;
2 
3 import java.io.File;
4 import java.text.MessageFormat;
5 import java.text.SimpleDateFormat;
6 import java.util.Calendar;
7 import java.util.Date;
8 import java.util.List;
9 import java.util.Set;
10 import java.util.TreeSet;
11 
12 import javax.xml.bind.JAXBContext;
13 import javax.xml.bind.JAXBException;
14 import javax.xml.bind.Unmarshaller;
15 
16 import org.eclipse.core.resources.IProject;
17 import org.eclipse.core.resources.IWorkspaceRoot;
18 import org.eclipse.core.resources.ResourcesPlugin;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IPath;
21 import org.eclipse.core.runtime.Path;
22 import org.eclipse.core.runtime.QualifiedName;
23 import org.eclipse.jface.action.Action;
24 import org.eclipse.jface.action.IToolBarManager;
25 import org.eclipse.jface.dialogs.MessageDialog;
26 import org.eclipse.jface.resource.ImageDescriptor;
27 import org.eclipse.jface.viewers.StructuredSelection;
28 import org.eclipse.jface.viewers.TreeViewer;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.events.SelectionEvent;
31 import org.eclipse.swt.events.SelectionListener;
32 import org.eclipse.swt.graphics.Cursor;
33 import org.eclipse.swt.layout.GridData;
34 import org.eclipse.swt.layout.GridLayout;
35 import org.eclipse.swt.layout.RowLayout;
36 import org.eclipse.swt.widgets.Button;
37 import org.eclipse.swt.widgets.Combo;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.swt.widgets.Display;
40 import org.eclipse.swt.widgets.Group;
41 import org.eclipse.swt.widgets.Label;
42 import org.eclipse.ui.PlatformUI;
43 import org.eclipse.ui.forms.widgets.Form;
44 import org.eclipse.ui.forms.widgets.FormToolkit;
45 import org.eclipse.ui.part.ViewPart;
46 
47 import com.proalpha.pds.jaxb.DsDSConsistencyChecks;
48 import com.proalpha.pds.jaxb.DsDSConsistencyChecks.TtDSCheck;
49 import com.proalpha.pds.jaxb.DsDSConsistencyChecks.TtDSCheck.TtDSSubCheck;
50 import com.proalpha.pds.jaxb.DsDSConsistencyChecks.TtDSCheck.TtDSSubCheck.TtDSLogEntryRepos;
51 import com.proalpha.pds.jaxb.DsDSConsistencyChecks.TtDSCheck.TtDSSubCheck.TtDSLogEntrySrc;
52 import com.proalpha.pds.paconnector.PaProject;
53 
63 public class CheckTreeView extends ViewPart {
64 
65  private static final String LOCAL = "Local";
66  public static final String ID = "com.proalpha.pds.checks.CheckTreeView";
67  private static final String CHECK_ALL = "<ALL>";
68 
69  private DsDSConsistencyChecks currentChecks;
70  private String currentTeam = CHECK_ALL;
71  private String currentModule = CHECK_ALL;
72  private CheckFilter filter;
73  private Form form;
74  private String location;
75  private Combo projectViewer;
76  private Combo teamViewer;
77  private Combo moduleViewer;
78  private TreeViewer viewer;
79  private File xmlFile;
80  private Button serverButton;
81  private Button localButton;
82 
83  public String getCurrentTeam() {
84  return currentTeam;
85  }
86 
87  public String getCurrentModule() {
88  return currentModule;
89  }
90 
97  public class CollapseAllAction extends Action {
98  public CollapseAllAction() {
99  setImageDescriptor(ImageDescriptor.createFromURL(getClass().getResource("/images/collapse_all.png")));
100  setToolTipText("Collapse all");
101  }
102 
103  @Override
104  public void run() {
105  viewer.collapseAll();
106  }
107  }
108 
115  public class ExpandAllAction extends Action {
116  public ExpandAllAction() {
117  setImageDescriptor(ImageDescriptor.createFromURL(getClass().getResource("/images/expand_all.gif")));
118  setToolTipText("Expand all");
119  }
120 
121  @Override
122  public void run() {
123  viewer.expandAll();
124  }
125  }
126 
130  public void createPartControl(Composite parent) {
131 
132  FormToolkit toolkit = new FormToolkit(parent.getDisplay());
133  form = toolkit.createForm(parent);
134  form.getBody().setLayout(new GridLayout(4, false));
135 
136  toolkit.decorateFormHeading(form);
137 
138  Label lblProject = new Label(form.getBody(), SWT.NONE);
139  GridData gridData = new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1);
140  gridData.widthHint = 50;
141  lblProject.setLayoutData(gridData);
142  toolkit.adapt(lblProject, true, true);
143  lblProject.setText("Project");
144 
145  projectViewer = new Combo(form.getBody(), SWT.DROP_DOWN | SWT.READ_ONLY);
146  GridData gridDataCombo = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
147  gridDataCombo.widthHint = 200;
148  projectViewer.setLayoutData(gridDataCombo);
149  projectViewer.setItems(getCurrentProjectList());
150  projectViewer.addSelectionListener(projectSelectedListener());
151 
152  toolkit.adapt(projectViewer);
153  toolkit.paintBordersFor(projectViewer);
154 
155  Group group = new Group(form.getBody(), SWT.NONE);
156  group.setText("Location");
157  group.setLayout(new RowLayout(SWT.VERTICAL));
158  group.setLayoutData(new GridData(SWT.LEFT, -7, false, false, 1, 2));
159  toolkit.adapt(group);
160  toolkit.paintBordersFor(group);
161 
162  serverButton = new Button(group, SWT.RADIO);
163  serverButton.setText("Server");
164  serverButton.setImage(
165  ImageDescriptor.createFromURL(getClass().getResource("/images/server_earth.png")).createImage());
166  serverButton.addSelectionListener(locationSelectedListener());
167  serverButton.setEnabled(false);
168  toolkit.adapt(serverButton, true, true);
169 
170  localButton = new Button(group, SWT.RADIO);
171  localButton.setText(LOCAL);
172  localButton
173  .setImage(ImageDescriptor.createFromURL(getClass().getResource("/images/text_tree.png")).createImage());
174  localButton.addSelectionListener(locationSelectedListener());
175  localButton.setEnabled(false);
176  localButton.setSelection(true);
177  toolkit.adapt(localButton, true, true);
178 
179  Label lblModule = new Label(form.getBody(), SWT.NONE);
180  GridData gridData3 = new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1);
181  gridData3.widthHint = 50;
182  lblModule.setLayoutData(gridData3);
183  toolkit.adapt(lblModule, true, true);
184  lblModule.setText("Module");
185 
186  teamViewer = new Combo(form.getBody(), SWT.DROP_DOWN | SWT.READ_ONLY);
187  teamViewer.setVisibleItemCount(12);
188  teamViewer.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false, 1, 1));
189  teamViewer.addSelectionListener(teamSelectedListener());
190 
191  toolkit.adapt(teamViewer);
192  toolkit.paintBordersFor(teamViewer);
193 
194  moduleViewer = new Combo(form.getBody(), SWT.DROP_DOWN | SWT.READ_ONLY);
195  moduleViewer.setVisibleItemCount(12);
196  moduleViewer.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, false, false, 1, 1));
197  moduleViewer.addSelectionListener(moduleSelectedListener());
198 
199  toolkit.adapt(moduleViewer);
200  toolkit.paintBordersFor(moduleViewer);
201 
202  location = LOCAL;
203  form.setText("select an installed project to show the results of the consistency checks");
204  form.update();
205 
206  viewer = new TreeViewer(form.getBody(), SWT.NONE);
207  filter = new CheckFilter();
208  viewer.setContentProvider(new CheckTreeViewContentProvider());
209  viewer.setLabelProvider(new CheckTreeViewLabelProvider(filter));
210  viewer.addFilter(filter);
211  viewer.getTree().setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true, 4, 1));
212 
213  getSite().setSelectionProvider(viewer);
214 
215  IToolBarManager toolBarManager = getViewSite().getActionBars().getToolBarManager();
216  toolBarManager.add(new ExpandAllAction());
217  toolBarManager.add(new CollapseAllAction());
218  }
219 
220  private SelectionListener projectSelectedListener() {
221  return new SelectionListener() {
222  public void widgetSelected(SelectionEvent selectedItem) {
223  if (selectedItem != null) {
224  String selectedProjectName = ((Combo) selectedItem.getSource()).getText();
225  if (!selectedProjectName.isEmpty()) {
226  localButton.setEnabled(true);
227  serverButton.setEnabled(true);
228  }
229  checksXmlFile(selectedProjectName);
230  }
231  refreshChecks();
232  }
233 
234  public void widgetDefaultSelected(SelectionEvent selectedItem) {
235  // ignored
236  }
237  };
238  }
239 
240  private SelectionListener locationSelectedListener() {
241  return new SelectionListener() {
242  public void widgetSelected(SelectionEvent selectedItem) {
243 
244  if (selectedItem != null && selectedItem.widget instanceof Button
245  && ((Button) selectedItem.widget).getSelection() && projectViewer.getSelectionIndex() != -1) {
246  location = ((Button) selectedItem.widget).getText();
247  String selectedProjectName = projectViewer.getText();
248  checksXmlFile(selectedProjectName);
249  }
250  refreshChecks();
251  }
252 
253  public void widgetDefaultSelected(SelectionEvent selectedItem) {
254  // ignored
255  }
256  };
257  }
258 
259  private SelectionListener teamSelectedListener() {
260  return new SelectionListener() {
261 
262  public void widgetSelected(SelectionEvent selectedItem) {
263  if (selectedItem != null) {
264  currentTeam = ((Combo) selectedItem.getSource()).getText();
265  currentModule = CHECK_ALL;
266  filter.setSearchFilter(currentTeam, currentModule);
267  viewer.setSelection(StructuredSelection.EMPTY);
268  moduleViewer.setItems(getModules());
269  moduleViewer.select(moduleViewer.indexOf(CHECK_ALL));
270  viewer.refresh();
271  viewer.expandAll();
272  }
273  }
274 
275  @Override
276  public void widgetDefaultSelected(SelectionEvent selectedItem) {
277  // ignored
278  }
279  };
280  }
281 
282  private SelectionListener moduleSelectedListener() {
283  return new SelectionListener() {
284 
285  public void widgetSelected(SelectionEvent selectedItem) {
286  if (selectedItem != null) {
287  currentModule = ((Combo) selectedItem.getSource()).getText();
288  filter.setSearchFilter(currentTeam, currentModule);
289  viewer.setSelection(StructuredSelection.EMPTY);
290  viewer.refresh();
291  viewer.expandAll();
292  }
293  }
294 
295  @Override
296  public void widgetDefaultSelected(SelectionEvent selectedItem) {
297  // ignored
298  }
299  };
300  }
301 
302  public void checksXmlFile(String selectedProjectName) {
303 
304  if (selectedProjectName != null) {
305  xmlFile = getXMLFile(selectedProjectName);
306  displayCheckDate();
307  } else {
308  xmlFile = null;
309  MessageDialog.openWarning(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
310  "Consistency checks not available",
311  "The selected project is not open. Consistency checks are only accessible "
312  + "if the selected project is available.");
313  }
314  }
315 
316  public String[] getCurrentProjectList() {
317 
318  IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
319 
320  IProject[] projects = workspaceRoot.getProjects();
321  String[] projectNames = new String[projects.length];
322  for (int i = 0; i < projects.length; i++) {
323  IProject project = projects[i];
324  projectNames[i] = project.getName();
325  }
326 
327  return projectNames;
328  }
329 
330  public String[] getModules() {
331  Set<String> modules = new TreeSet<>();
332  modules.add(CHECK_ALL);
333  List<TtDSCheck> filteredList = currentChecks.getTtDSCheck();
334  for (TtDSCheck check : filteredList) {
335  List<TtDSSubCheck> filteredCheck = check.getTtDSSubCheck();
336  for (DsDSConsistencyChecks.TtDSCheck.TtDSSubCheck filteredSubCheck : filteredCheck) {
337  List<TtDSLogEntrySrc> filteredSrcEntries = filteredSubCheck.getTtDSLogEntrySrc();
338  if (!filteredSrcEntries.isEmpty()) {
339  for (TtDSLogEntrySrc filteredSrcEntry : filteredSrcEntries) {
340  if (filteredSrcEntry.getModule() != null && !filteredSrcEntry.getModule().isEmpty()
341  && filter.select(null, null, filteredSrcEntry)) {
342  modules.add(filteredSrcEntry.getModule());
343  }
344  }
345  }
346  List<TtDSLogEntryRepos> filteredRepoEntries = filteredSubCheck.getTtDSLogEntryRepos();
347  if (!filteredRepoEntries.isEmpty()) {
348  for (TtDSLogEntryRepos filteredRepoEntry : filteredRepoEntries) {
349  if (filteredRepoEntry.getModule() != null && !filteredRepoEntry.getModule().isEmpty()
350  && filter.select(null, null, filteredRepoEntry)) {
351  modules.add(filteredRepoEntry.getModule());
352  }
353  }
354  }
355  }
356  }
357  return modules.stream().toArray(String[]::new);
358  }
359 
360  private void displayCheckDate() {
361  if (xmlFile != null && xmlFile.exists() && projectViewer.getSelectionIndex() != -1) {
362  Date date = new Date();
363  date.setTime(xmlFile.lastModified());
364  Calendar calendar = Calendar.getInstance();
365  calendar.setTime(date);
366  SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
367  String heading = MessageFormat.format("{0} ({1})", location, dateFormat.format(calendar.getTime()));
368  form.setText(heading);
369  form.update();
370  }
371  }
372 
373  private void refreshChecks() {
374 
375  if ((xmlFile != null) && (xmlFile.exists())) {
376 
377  Unmarshaller unmarshaller = null;
378  try {
379  JAXBContext context = JAXBContext.newInstance(DsDSConsistencyChecks.class);
380  unmarshaller = context.createUnmarshaller();
381  if (unmarshaller != null) {
382  this.currentChecks = (DsDSConsistencyChecks) unmarshaller.unmarshal(xmlFile);
383  }
384  } catch (JAXBException e) {
385  throw new IllegalStateException(e);
386  }
387 
388  currentTeam = CHECK_ALL;
389  currentModule = CHECK_ALL;
390  filter.setSearchFilter(currentTeam, currentModule);
391  teamViewer.setItems(filter.getTeams());
392  moduleViewer.setItems(getModules());
393  teamViewer.select(teamViewer.indexOf(CHECK_ALL));
394  moduleViewer.select(moduleViewer.indexOf(CHECK_ALL));
395  } else {
396  currentChecks = null;
397  teamViewer.removeAll();
398  moduleViewer.removeAll();
399  }
400  refreshTree();
401  }
402 
403  private void refreshTree() {
404  if (currentChecks != null) {
405  List<TtDSCheck> filteredList = currentChecks.getTtDSCheck();
406  viewer.setInput(filteredList);
407  } else {
408  viewer.setInput(null);
409  }
410  Object[] expandedElements = null;
411  expandedElements = viewer.getExpandedElements();
412  if (expandedElements != null)
413  viewer.setExpandedElements(expandedElements);
414 
415  viewer.refresh();
416  }
417 
418  @Override
419  public void showBusy(boolean busy) {
420  Cursor systemCursor = busy ? Display.getCurrent().getSystemCursor(SWT.CURSOR_WAIT)
421  : Display.getCurrent().getSystemCursor(SWT.CURSOR_ARROW);
422  viewer.getTree().setCursor(systemCursor);
423  }
424 
425  public String getSelectedProject() {
426  return projectViewer.getText();
427  }
428 
429  public void setSelectedProject(PaProject paProject) {
430 
431  projectViewer.select(projectViewer.indexOf(paProject.getName()));
432  checksXmlFile(paProject.getName());
433  try {
434  // TODO do better synch for showing the results
435  Thread.sleep(500);
436  } catch (InterruptedException e) {
437  e.printStackTrace();
438  Thread.currentThread().interrupt();
439  }
440  refreshChecks();
441  viewer.refresh(true);
442  }
443 
444  private File getXMLFile(String selectedProjectName) {
445  IPath pathToXMLFile = null;
446  String fileName = "ConsistencyCheck.xml";
447  Path localLocation = null;
448  Path serverLocation = null;
449  try {
450  IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
451  localLocation = new Path(root
452  .getPersistentProperty(new QualifiedName(selectedProjectName, "ConsistencyChecksLocationLocal")));
453  serverLocation = new Path(root
454  .getPersistentProperty(new QualifiedName(selectedProjectName, "ConsistencyChecksLocationServer")));
455  } catch (CoreException e1) {
456  e1.printStackTrace();
457  }
458  if (location.equals(LOCAL)) {
459  pathToXMLFile = localLocation;
460  } else {
461  pathToXMLFile = serverLocation;
462  }
463  File resultFile = null;
464  if (pathToXMLFile != null) {
465  resultFile = pathToXMLFile.append(fileName).toFile();
466  } else {
467  resultFile = new File("");
468  }
469  return resultFile;
470  }
471 
472  @Override
473  public void setFocus() {
474  // Ignored
475 
476  }
477 }
void createPartControl(Composite parent)
List< DsDSConsistencyChecks.TtDSCheck > getTtDSCheck()