1 package com.stateofflow.eclipse.metrics.export.wizard;
2
3 import java.io.File;
4
5 import org.eclipse.core.resources.IProject;
6 import org.eclipse.core.resources.IResource;
7 import org.eclipse.core.runtime.CoreException;
8 import org.eclipse.jdt.core.IJavaElement;
9 import org.eclipse.jdt.core.IJavaProject;
10 import org.eclipse.jdt.core.JavaCore;
11 import org.eclipse.jface.util.IPropertyChangeListener;
12 import org.eclipse.jface.util.PropertyChangeEvent;
13 import org.eclipse.jface.viewers.ISelection;
14 import org.eclipse.jface.viewers.IStructuredSelection;
15 import org.eclipse.jface.wizard.WizardPage;
16 import org.eclipse.swt.events.SelectionEvent;
17 import org.eclipse.swt.events.SelectionListener;
18 import org.eclipse.swt.widgets.Composite;
19
20 import com.stateofflow.eclipse.metrics.MetricsPlugin;
21 import com.stateofflow.eclipse.metrics.export.html.HtmlExportConfiguration;
22 import com.stateofflow.eclipse.metrics.swt.SWTBuilder;
23
24 final class MetricsExportWizardPage extends WizardPage implements IPropertyChangeListener, SelectionListener, StatusUpdateable {
25 private CommonControlsAgent commonControlsFragment;
26 private HtmlExportAgent htmlExportFragment;
27 private CsvExportAgent csvExportFragment;
28 private final ISelection selection;
29
30 public MetricsExportWizardPage(final ISelection selection) {
31 super("Metrics Export");
32 setTitle("Metrics Export");
33 setDescription("This wizard exports the metrics of a Java project");
34 this.selection = selection;
35 }
36
37 public void createControl(final Composite parent) {
38 final Composite rootComposite = createControlsContainer(parent);
39
40 try {
41 initialize();
42 } catch (final RuntimeException rex) {
43 throw rex;
44 } catch (final CoreException cex) {
45 MetricsPlugin.log(cex);
46 throw new RuntimeException("Caught CoreException. See log for details.");
47 }
48 dialogChanged();
49 setControl(rootComposite);
50 }
51
52 private Composite createControlsContainer(final Composite parent) {
53 final SWTBuilder builder = new SWTBuilder(parent).addComposite().setGridLayout(1);
54 commonControlsFragment = new CommonControlsAgent(builder, this, getShell());
55 htmlExportFragment = new HtmlExportAgent(builder, this);
56 csvExportFragment = new CsvExportAgent(builder, this);
57 return builder.peek();
58 }
59
60 private void dialogChanged() {
61 clearStatus();
62 commonControlsFragment.updateStatus(this);
63 htmlExportFragment.updateStatus(this);
64 if (!isAtLeastOneExportFormatSpecified()) {
65 updateStatus(false, "At least one export format must be specified");
66 }
67 }
68
69 File getExportDirectory() {
70 return commonControlsFragment.getExportDirectory();
71 }
72
73 public HtmlExportConfiguration getHtmlExportConfiguration() {
74 return new HtmlExportConfiguration(getExportDirectory(), getProject(), isHtmlImageProductionEnabled(), getHtmlRowsPerPage(), isHtmlJavaExportEnabled());
75 }
76
77 public int getHtmlRowsPerPage() {
78 return htmlExportFragment.getRowsPerPage();
79 }
80
81 public String getProject() {
82 return commonControlsFragment.getProject();
83 }
84
85 private void initialiseFromJavaProject(final IJavaProject javaProject) throws CoreException {
86 final ProjectProperties properties = new ProjectProperties(javaProject);
87 commonControlsFragment.initialise(properties);
88 htmlExportFragment.initialise(properties);
89 csvExportFragment.initialise(properties);
90 }
91
92 private void initialiseFromProject(final IProject project) throws CoreException {
93 final IJavaProject javaProject = JavaCore.create(project);
94 if (javaProject != null && javaProject.exists()) {
95 initialiseFromJavaProject(javaProject);
96 }
97 }
98
99 private void initialiseFromSelectedObject(final Object obj) throws CoreException {
100 if (obj instanceof IJavaElement) {
101 initialiseFromJavaProject(((IJavaElement) obj).getJavaProject());
102 } else if (obj instanceof IResource) {
103 initialiseFromProject(((IResource) obj).getProject());
104 }
105 }
106
107 private void initialize() throws CoreException {
108 if (selection == null || selection.isEmpty() || !(selection instanceof IStructuredSelection)) {
109 return;
110 }
111
112 final IStructuredSelection ssel = (IStructuredSelection) selection;
113 if (ssel.size() == 1) {
114 initialiseFromSelectedObject(ssel.getFirstElement());
115 }
116 }
117
118 private boolean isAtLeastOneExportFormatSpecified() {
119 return isCsvExportEnabled() || isHtmlExportEnabled();
120 }
121
122 public boolean isCsvExportEnabled() {
123 return csvExportFragment.isEnabled();
124 }
125
126 public boolean isHtmlExportEnabled() {
127 return htmlExportFragment.isEnabled();
128 }
129
130 public boolean isHtmlImageProductionEnabled() {
131 return htmlExportFragment.isImageProductionEnabled();
132 }
133
134 public boolean isHtmlJavaExportEnabled() {
135 return htmlExportFragment.isJavaExportEnabled();
136 }
137
138 public void propertyChange(final PropertyChangeEvent event) {
139 dialogChanged();
140 }
141
142 private void clearStatus() {
143 setErrorMessage(null);
144 setPageComplete(true);
145 }
146
147 public void updateStatus(final boolean value, final String message) {
148 if (!value && isPageComplete()) {
149 setErrorMessage(message);
150 setPageComplete(false);
151 }
152 }
153
154 public void widgetDefaultSelected(final SelectionEvent e) {
155 dialogChanged();
156 }
157
158 public void widgetSelected(final SelectionEvent e) {
159 dialogChanged();
160 }
161 }
162