001: package net.sf.mockcreator.plugin;
002:
003: import net.sf.mockcreator.codegeneration.MockBuilder;
004:
005: import org.eclipse.core.runtime.CoreException;
006: import org.eclipse.core.runtime.QualifiedName;
007:
008: import org.eclipse.jdt.core.IJavaProject;
009:
010: import org.eclipse.swt.SWT;
011: import org.eclipse.swt.layout.GridData;
012: import org.eclipse.swt.layout.GridLayout;
013: import org.eclipse.swt.widgets.Composite;
014: import org.eclipse.swt.widgets.Control;
015: import org.eclipse.swt.widgets.Label;
016: import org.eclipse.swt.widgets.Text;
017:
018: import org.eclipse.ui.dialogs.PropertyPage;
019:
020: public class MockCreatorPropertyPage extends PropertyPage {
021: private static final String OUTDIR_PROPERTY = "OUTDIR";
022: private static final String PACKAGE_PROPERTY = "PACKAGE";
023: private static final String FILE_PROPERTY = "FILE";
024:
025: static final String DEFAULT_OUTDIR = "mocks";
026: static final String DEFAULT_PACKAGE = "*";
027: static final String DEFAULT_FILE = "";
028:
029: private static final int TEXT_FIELD_WIDTH = 50;
030: final static QualifiedName outdirPropertyKey = new QualifiedName(
031: "MockCreator", OUTDIR_PROPERTY);
032: final static QualifiedName packagePropertyKey = new QualifiedName(
033: "MockCreator", PACKAGE_PROPERTY);
034: final static QualifiedName filePropertyKey = new QualifiedName(
035: "MockCreator", FILE_PROPERTY);
036:
037: private Text outdirText;
038: private Text packageText;
039: private Text fileText;
040:
041: public MockCreatorPropertyPage() {
042: super ();
043: }
044:
045: private void addOutdirSectionDescription(Composite parent) {
046: addText(
047: parent,
048: "Specify the location of the generated mock sources.\n"
049: + "By default mocks are placed under the root directory of the project.\n\n"
050: + "Location is relative to the project root, i.e. for MockCreator\n"
051: + "project it could be 'mocks' which means '/MockCreator/mocks'.");
052: }
053:
054: private void addPackageSectionDescription(Composite parent) {
055: addText(
056: parent,
057: "Specify the package the mocks to be generated in.\n"
058: + "By default, mocks are in the same package as source files.\n\n"
059: + "Star symbol is replaced with the package of the source file. For example,\n"
060: + "for package net.sf.mockcreator notation *.tests will put the mocks into\n"
061: + "net.sf.mockcreator.tests package, and tests.* will put them into\n"
062: + "tests.net.sf.mockcreator one. Empty package means default java package.");
063: }
064:
065: private void addFileSectionDescription(Composite parent) {
066: addText(
067: parent,
068: "Specify the location of the file to be updated when a new Mock is created.\n"
069: + "The file will be appended with full class name of mocked class.\n\n"
070: + "This way you can synchronize mock generation in Eclipse and ant build.\n"
071: + "If file doesn't exist it will be created. File location is relative to the project root.\n"
072: + "Default (empty line) is not to update any file.");
073: }
074:
075: private void addCopyrightClause(Composite parent) {
076: addText(
077: parent,
078: "Initial development by Abstrakt (http://www.abstrakt.de)\n"
079: + "Parts of the code by Vladimir Dozen (http://dozen.ru)\n\n"
080: + "Download latest version from http://mockcreator.sf.net/download");
081: }
082:
083: private void addOutdirSection(Composite parent) {
084: Composite composite = createDefaultComposite(parent);
085:
086: Label ownerLabel = new Label(composite, SWT.NONE);
087: ownerLabel.setText("Mocks Location:");
088:
089: outdirText = new Text(composite, SWT.MULTI | SWT.BORDER);
090:
091: GridData gd = new GridData();
092: gd.widthHint = convertWidthInCharsToPixels(TEXT_FIELD_WIDTH);
093: outdirText.setLayoutData(gd);
094:
095: IJavaProject ijp = (IJavaProject) getElement();
096:
097: try {
098: outdirText.setText(getProperty(ijp, outdirPropertyKey,
099: DEFAULT_OUTDIR));
100: } catch (CoreException e) {
101: }
102: }
103:
104: private void addPackageSection(Composite parent) {
105: Composite composite = createDefaultComposite(parent);
106:
107: Label ownerLabel = new Label(composite, SWT.NONE);
108: ownerLabel.setText("Mocks Package:");
109:
110: packageText = new Text(composite, SWT.MULTI | SWT.BORDER);
111:
112: GridData gd = new GridData();
113: gd.widthHint = convertWidthInCharsToPixels(TEXT_FIELD_WIDTH);
114: packageText.setLayoutData(gd);
115:
116: IJavaProject ijp = (IJavaProject) getElement();
117:
118: try {
119: packageText.setText(getProperty(ijp, packagePropertyKey,
120: DEFAULT_PACKAGE));
121: } catch (CoreException e) {
122: }
123: }
124:
125: private void addFileSection(Composite parent) {
126: Composite composite = createDefaultComposite(parent);
127:
128: Label ownerLabel = new Label(composite, SWT.NONE);
129: ownerLabel.setText("Mocked Classes File:");
130:
131: fileText = new Text(composite, SWT.MULTI | SWT.BORDER);
132:
133: GridData gd = new GridData();
134: gd.widthHint = convertWidthInCharsToPixels(TEXT_FIELD_WIDTH);
135: fileText.setLayoutData(gd);
136:
137: IJavaProject ijp = (IJavaProject) getElement();
138:
139: try {
140: fileText.setText(getProperty(ijp, filePropertyKey,
141: DEFAULT_FILE));
142: } catch (CoreException e) {
143: }
144: }
145:
146: private void addSeparator(Composite parent) {
147: Label separator = new Label(parent, SWT.SEPARATOR
148: | SWT.HORIZONTAL);
149: GridData gridData = new GridData();
150: gridData.horizontalAlignment = GridData.FILL;
151: gridData.grabExcessHorizontalSpace = true;
152: separator.setLayoutData(gridData);
153: }
154:
155: private void addText(Composite parent, String txt) {
156: Composite composite = createDefaultComposite(parent);
157:
158: Text text = new Text(composite, SWT.MULTI | SWT.WRAP
159: | SWT.READ_ONLY);
160: text.setText(txt);
161: }
162:
163: protected Control createContents(Composite parent) {
164: Composite composite = new Composite(parent, SWT.NONE);
165:
166: GridLayout layout = new GridLayout();
167: composite.setLayout(layout);
168:
169: GridData data = new GridData(GridData.FILL);
170: data.grabExcessHorizontalSpace = true;
171: composite.setLayoutData(data);
172:
173: addText(composite, "MockCreator version " + MockBuilder.version);
174:
175: addSeparator(composite);
176:
177: addOutdirSectionDescription(composite);
178: addOutdirSection(composite);
179:
180: addPackageSectionDescription(composite);
181: addPackageSection(composite);
182:
183: addFileSectionDescription(composite);
184: addFileSection(composite);
185:
186: addSeparator(composite);
187:
188: addCopyrightClause(composite);
189:
190: return composite;
191: }
192:
193: private Composite createDefaultComposite(Composite parent) {
194: Composite composite = new Composite(parent, SWT.NULL);
195:
196: GridLayout layout = new GridLayout();
197: layout.numColumns = 2;
198: composite.setLayout(layout);
199:
200: GridData data = new GridData();
201: data.verticalAlignment = GridData.FILL;
202: data.horizontalAlignment = GridData.BEGINNING;
203: composite.setLayoutData(data);
204:
205: return composite;
206: }
207:
208: protected void performDefaults() {
209: outdirText.setText(DEFAULT_OUTDIR);
210: packageText.setText(DEFAULT_PACKAGE);
211: fileText.setText(DEFAULT_FILE);
212: }
213:
214: public boolean performOk() {
215: IJavaProject ijp = (IJavaProject) getElement();
216:
217: try {
218: ijp.getProject().setPersistentProperty(outdirPropertyKey,
219: outdirText.getText());
220: ijp.getProject().setPersistentProperty(packagePropertyKey,
221: packageText.getText());
222: ijp.getProject().setPersistentProperty(filePropertyKey,
223: fileText.getText());
224: } catch (CoreException e) {
225: }
226:
227: return true;
228: }
229:
230: static String getProperty(IJavaProject ijp, QualifiedName qn,
231: String defaultValue) throws CoreException {
232: String prop = ijp.getProject().getPersistentProperty(qn);
233:
234: if (prop == null) {
235: return defaultValue == null ? null : defaultValue.trim();
236: }
237:
238: return prop.trim();
239: }
240:
241: public static String getTargetPackage(IJavaProject prj)
242: throws CoreException {
243: return getProperty(prj, packagePropertyKey, DEFAULT_PACKAGE);
244: }
245:
246: public static String getOutputDir(IJavaProject prj)
247: throws CoreException {
248: return getProperty(prj, outdirPropertyKey, DEFAULT_OUTDIR);
249: }
250:
251: public static String getFile(IJavaProject prj) throws CoreException {
252: return getProperty(prj, filePropertyKey, DEFAULT_FILE);
253: }
254: }
|