1 package com.stateofflow.eclipse.metrics.preferences;
2
3 import java.lang.reflect.InvocationTargetException;
4 import java.util.HashSet;
5 import java.util.Iterator;
6 import java.util.Set;
7
8 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
9 import org.eclipse.jface.preference.BooleanFieldEditor;
10 import org.eclipse.jface.preference.FieldEditor;
11 import org.eclipse.jface.preference.IntegerFieldEditor;
12 import org.eclipse.jface.preference.PreferencePage;
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.widgets.Composite;
15 import org.eclipse.swt.widgets.Control;
16 import org.eclipse.swt.widgets.TabFolder;
17 import org.eclipse.ui.IWorkbench;
18 import org.eclipse.ui.IWorkbenchPreferencePage;
19
20 import com.stateofflow.eclipse.metrics.MetricsPlugin;
21 import com.stateofflow.eclipse.metrics.preferences.builder.CyclomaticComplexityTabBuilder;
22 import com.stateofflow.eclipse.metrics.preferences.builder.LackOfCohesionTabBuilder;
23 import com.stateofflow.eclipse.metrics.preferences.builder.LinesOfCodeTabBuilder;
24 import com.stateofflow.eclipse.metrics.preferences.builder.MiscellaneousTabBuilder;
25 import com.stateofflow.eclipse.metrics.preferences.builder.NumberOfParametersTabBuilder;
26
27 public final class MetricsPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
28 private final Set<FieldEditor> fieldEditors = new HashSet<FieldEditor>();
29
30 @Override
31 protected Control createContents(final Composite parent) {
32 final TabFolder folder = new TabFolder(parent, SWT.NULL);
33
34 new CyclomaticComplexityTabBuilder(fieldEditors, getPreferenceStore()).create(folder);
35 new LackOfCohesionTabBuilder(fieldEditors, getPreferenceStore()).create(folder);
36 new LinesOfCodeTabBuilder(fieldEditors, getPreferenceStore()).create(folder);
37 new NumberOfParametersTabBuilder(fieldEditors, getPreferenceStore()).create(folder);
38 new MiscellaneousTabBuilder(fieldEditors, getPreferenceStore()).create(folder);
39
40 return folder;
41 }
42
43 private boolean hasBooleanValueChanged(final BooleanFieldEditor editor) {
44 return getPreferenceStore().getBoolean(editor.getPreferenceName()) != editor.getBooleanValue();
45 }
46
47 private boolean hasEditorValueChanged(final FieldEditor editor) {
48 if (editor instanceof BooleanFieldEditor) {
49 return hasBooleanValueChanged((BooleanFieldEditor) editor);
50 } else if (editor instanceof IntegerFieldEditor) {
51 return hasIntegerValueChanged((IntegerFieldEditor) editor);
52 } else {
53 throw new IllegalArgumentException("Unknown editor type " + editor.getClass().getName() + " for preference " + editor.getPreferenceName());
54 }
55 }
56
57 private boolean hasIntegerValueChanged(final IntegerFieldEditor editor) {
58 return getPreferenceStore().getInt(editor.getPreferenceName()) != editor.getIntValue();
59 }
60
61 public void init(final IWorkbench workbench) {
62 setPreferenceStore(MetricsPlugin.getDefault().getPreferenceStore());
63 }
64
65 @Override
66 protected void performApply() {
67 saveAndRebuild();
68 super.performApply();
69 }
70
71 @Override
72 protected void performDefaults() {
73 final Iterator<FieldEditor> iter = fieldEditors.iterator();
74 while (iter.hasNext()) {
75 iter.next().loadDefault();
76 }
77
78 super.performDefaults();
79 }
80
81 @Override
82 public boolean performOk() {
83 saveAndRebuild();
84 return super.performOk();
85 }
86
87 private void rebuild() throws InvocationTargetException, InterruptedException {
88 new ProgressMonitorDialog(getShell()).run(true, true, new ProjectBuilder());
89 }
90
91 private void saveAndRebuild() {
92 if (storePreferences()) {
93 try {
94 rebuild();
95 } catch (final Exception ex) {
96 MetricsPlugin.log(ex);
97 }
98 }
99 }
100
101 private boolean storePreference(final FieldEditor editor) {
102 if (hasEditorValueChanged(editor)) {
103 editor.store();
104 return true;
105 }
106 return false;
107 }
108
109 private boolean storePreferences() {
110 boolean changes = false;
111 final Iterator<FieldEditor> iter = fieldEditors.iterator();
112 while (iter.hasNext()) {
113 changes |= storePreference(iter.next());
114 }
115
116 return changes;
117 }
118 }