diff --git a/org.jiayun.commons4e-feature/feature.xml b/org.jiayun.commons4e-feature/feature.xml
index 2353d6f..3670fb7 100644
--- a/org.jiayun.commons4e-feature/feature.xml
+++ b/org.jiayun.commons4e-feature/feature.xml
@@ -2,7 +2,7 @@
@@ -103,10 +103,6 @@ Everyone is permitted to copy and distribute copies of this Agreement, but in or
This Agreement is governed by the laws of the State of New York and the intellectual property laws of the United States of America. No party to this Agreement will bring a legal action under this Agreement more than one year after the cause of action arose. Each party waives its rights to a jury trial in any resulting litigation.
-
-
-
-
@@ -120,7 +116,7 @@ This Agreement is governed by the laws of the State of New York and the intellec
id="org.jiayun.commons4e"
download-size="0"
install-size="0"
- version="0.0.0"
+ version="1.2.0"
unpack="false"/>
diff --git a/org.jiayun.commons4e-site/site.xml b/org.jiayun.commons4e-site/site.xml
index e7fabe7..dfe8047 100644
--- a/org.jiayun.commons4e-site/site.xml
+++ b/org.jiayun.commons4e-site/site.xml
@@ -1,10 +1,6 @@
-
-
- Commons4E Update Site
-
-
+
+
diff --git a/org.jiayun.commons4e/META-INF/MANIFEST.MF b/org.jiayun.commons4e/META-INF/MANIFEST.MF
index 2aae049..be6b91e 100644
--- a/org.jiayun.commons4e/META-INF/MANIFEST.MF
+++ b/org.jiayun.commons4e/META-INF/MANIFEST.MF
@@ -1,7 +1,7 @@
Manifest-Version: 1.0
Bundle-Name: Commons4E
Bundle-SymbolicName: org.jiayun.commons4e; singleton=true
-Bundle-Version: 1.1.11
+Bundle-Version: 1.2.0
Bundle-ClassPath: commons4e.jar
Bundle-Activator: org.jiayun.commons4e.Commons4ePlugin
Bundle-Vendor: JiaYun
diff --git a/org.jiayun.commons4e/plugin.xml b/org.jiayun.commons4e/plugin.xml
index e7df931..9c4ed26 100644
--- a/org.jiayun.commons4e/plugin.xml
+++ b/org.jiayun.commons4e/plugin.xml
@@ -2,58 +2,24 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
@@ -67,5 +33,107 @@
point="org.eclipse.core.runtime.preferences">
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/org.jiayun.commons4e/src/org/jiayun/commons4e/internal/lang/actions/GenerateAction.java b/org.jiayun.commons4e/src/org/jiayun/commons4e/internal/lang/actions/GenerateAction.java
deleted file mode 100644
index 3642d16..0000000
--- a/org.jiayun.commons4e/src/org/jiayun/commons4e/internal/lang/actions/GenerateAction.java
+++ /dev/null
@@ -1,77 +0,0 @@
-//$Id$
-package org.jiayun.commons4e.internal.lang.actions;
-
-import org.eclipse.jdt.core.IType;
-import org.eclipse.jdt.core.JavaModelException;
-import org.eclipse.jface.action.IAction;
-import org.eclipse.jface.dialogs.MessageDialog;
-import org.eclipse.jface.viewers.ISelection;
-import org.eclipse.jface.viewers.IStructuredSelection;
-import org.eclipse.swt.widgets.Shell;
-import org.eclipse.ui.IObjectActionDelegate;
-import org.eclipse.ui.IWorkbenchPart;
-import org.jiayun.commons4e.internal.lang.generators.LangGenerators;
-
-/**
- * @author jiayun
- */
-public final class GenerateAction implements IObjectActionDelegate {
-
- private IType objectClass;
-
- private Shell parentShell;
-
- /*
- * (non-Javadoc)
- *
- * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction,
- * org.eclipse.ui.IWorkbenchPart)
- */
- public void setActivePart(IAction action, IWorkbenchPart targetPart) {
- parentShell = targetPart.getSite().getShell();
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
- */
- public void run(IAction action) {
- LangGenerators.getGenerator(action.getId()).generate(parentShell,
- objectClass);
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction,
- * org.eclipse.jface.viewers.ISelection)
- */
- public void selectionChanged(IAction action, ISelection selection) {
-
- if (selection != null && selection instanceof IStructuredSelection) {
-
- IStructuredSelection structuredSelection = (IStructuredSelection) selection;
- Object firstElement = structuredSelection.getFirstElement();
-
- if (firstElement != null && firstElement instanceof IType) {
-
- IType selected = (IType) firstElement;
- try {
- if (selected.isClass() && !selected.isReadOnly()) {
- objectClass = selected;
- action.setEnabled(true);
- return;
- }
- } catch (JavaModelException e) {
- MessageDialog.openError(parentShell, "Error", e
- .getMessage());
- }
- }
- }
-
- objectClass = null;
- action.setEnabled(false);
- }
-
-}
diff --git a/org.jiayun.commons4e/src/org/jiayun/commons4e/internal/lang/actions/ViewerContributionGenerateAction.java b/org.jiayun.commons4e/src/org/jiayun/commons4e/internal/lang/actions/ViewerContributionGenerateAction.java
deleted file mode 100644
index 87ff5b3..0000000
--- a/org.jiayun.commons4e/src/org/jiayun/commons4e/internal/lang/actions/ViewerContributionGenerateAction.java
+++ /dev/null
@@ -1,89 +0,0 @@
-//$Id$
-package org.jiayun.commons4e.internal.lang.actions;
-
-import org.eclipse.jdt.core.ICompilationUnit;
-import org.eclipse.jdt.core.IJavaElement;
-import org.eclipse.jdt.core.IType;
-import org.eclipse.jdt.core.JavaModelException;
-import org.eclipse.jdt.ui.IWorkingCopyManager;
-import org.eclipse.jdt.ui.JavaUI;
-import org.eclipse.jface.action.IAction;
-import org.eclipse.jface.dialogs.MessageDialog;
-import org.eclipse.jface.text.ITextSelection;
-import org.eclipse.jface.viewers.ISelection;
-import org.eclipse.swt.widgets.Shell;
-import org.eclipse.ui.IEditorActionDelegate;
-import org.eclipse.ui.IEditorPart;
-import org.eclipse.ui.texteditor.ITextEditor;
-import org.jiayun.commons4e.internal.lang.generators.LangGenerators;
-
-/**
- * @author jiayun
- */
-public final class ViewerContributionGenerateAction implements
- IEditorActionDelegate {
-
- private Shell parentShell;
-
- private ITextEditor editor;
-
- /*
- * (non-Javadoc)
- *
- * @see org.eclipse.ui.IEditorActionDelegate#setActiveEditor(org.eclipse.jface.action.IAction,
- * org.eclipse.ui.IEditorPart)
- */
- public void setActiveEditor(IAction action, IEditorPart targetEditor) {
- editor = (ITextEditor) targetEditor;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
- */
- public void run(IAction action) {
- parentShell = editor.getSite().getShell();
- IWorkingCopyManager manager = JavaUI.getWorkingCopyManager();
- ICompilationUnit cu = manager.getWorkingCopy(editor.getEditorInput());
-
- IType objectClass = null;
- try {
- ITextSelection selection = (ITextSelection) editor
- .getSelectionProvider().getSelection();
- IJavaElement element = cu.getElementAt(selection.getOffset());
- if (element != null) {
- objectClass = (IType) element.getAncestor(IJavaElement.TYPE);
- }
- } catch (JavaModelException e) {
- MessageDialog.openError(parentShell, "Error", e.getMessage());
- }
-
- if (objectClass == null) {
- objectClass = cu.findPrimaryType();
- }
-
- try {
- if (objectClass == null || !objectClass.isClass()) {
- MessageDialog
- .openInformation(parentShell, "Method Generation",
- "Cursor not in a class, or no class has the same name with the Java file.");
- } else {
- LangGenerators.getGenerator(action.getId()).generate(
- parentShell, objectClass);
- }
- } catch (JavaModelException e) {
- MessageDialog.openError(parentShell, "Error", e.getMessage());
- }
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction,
- * org.eclipse.jface.viewers.ISelection)
- */
- public void selectionChanged(IAction action, ISelection selection) {
- }
-
-}
diff --git a/org.jiayun.commons4e/src/org/jiayun/commons4e/internal/lang/generators/CommonsLangLibraryUtils.java b/org.jiayun.commons4e/src/org/jiayun/commons4e/internal/lang/generators/CommonsLangLibraryUtils.java
new file mode 100644
index 0000000..c79de12
--- /dev/null
+++ b/org.jiayun.commons4e/src/org/jiayun/commons4e/internal/lang/generators/CommonsLangLibraryUtils.java
@@ -0,0 +1,103 @@
+package org.jiayun.commons4e.internal.lang.generators;
+
+import org.jiayun.commons4e.internal.util.PreferenceUtils;
+
+/**
+ * Helper class to ease the construction of the commons-lang library objects to be
+ * used by the generator classes. This class cannot be instantiated since it only
+ * holds static constants and static helper methods.
+ *
+ * @author maudrain
+ */
+public final class CommonsLangLibraryUtils {
+
+ public static final String DOT_STRING = ".";
+
+ private static final String COMMONS_LANG_PREFIX = "org.apache.commons.lang";
+
+ private static final String COMPARE_TO_BUILDER_POSTFIX = ".builder.CompareToBuilder";
+ private static final String HASH_CODE_BUILDER_POSTFIX = ".builder.HashCodeBuilder";
+ private static final String EQUALS_BUILDER_POSTFIX = ".builder.EqualsBuilder";
+ private static final String TO_STRING_BUILDER_POSTFIX = ".builder.ToStringBuilder";
+ private static final String TO_STRING_STYLE_POSTFIX = ".builder.ToStringStyle";
+
+ private static final String COMMONS_LANG3_ADDON = "3";
+ private static final String EMPTY_STRING = "";
+
+ private static final String DEFAULT_STYLE_POSTFIX = DOT_STRING
+ + "DEFAULT_STYLE";
+ private static final String MULTI_LINE_STYLE_POSTFIX = DOT_STRING
+ + "MULTI_LINE_STYLE";
+ private static final String NO_FIELD_NAMES_STYLE_POSTFIX = DOT_STRING
+ + "NO_FIELD_NAMES_STYLE";
+ private static final String SHORT_PREFIX_STYLE_POSTFIX = DOT_STRING
+ + "SHORT_PREFIX_STYLE";
+ private static final String SIMPLE_STYLE_POSTFIX = DOT_STRING
+ + "SIMPLE_STYLE";
+
+ private CommonsLangLibraryUtils() {
+ /* Only static constants */
+ }
+
+ /**
+ * @return the full constructed CompareToBuilder library
+ */
+ public static String getCompareToBuilderLibrary() {
+ return createLibraryFullPath(COMPARE_TO_BUILDER_POSTFIX);
+ }
+
+ /**
+ * @return the full constructed HashCodeBuilder library
+ */
+ public static String getHashCodeBuilderLibrary() {
+ return createLibraryFullPath(HASH_CODE_BUILDER_POSTFIX);
+ }
+
+ /**
+ * @return the full constructed EqualsBuilder library
+ */
+ public static String getEqualsBuilderLibrary() {
+ return createLibraryFullPath(EQUALS_BUILDER_POSTFIX);
+ }
+
+ /**
+ * @return the full constructed ToStringBuilder library
+ */
+ public static String getToStringBuilderLibrary() {
+ return createLibraryFullPath(TO_STRING_BUILDER_POSTFIX);
+ }
+
+ /**
+ * @return the full constructed ToStringStyle library
+ */
+ public static String getToStringStyleLibrary() {
+ return createLibraryFullPath(TO_STRING_STYLE_POSTFIX);
+ }
+
+ /**
+ * @return the full constructed ToStringStyle.DEFAULT_STYLE library
+ */
+ public static String getToStringStyleLibraryDefaultStyle() {
+ return createLibraryFullPath(TO_STRING_STYLE_POSTFIX)
+ + DEFAULT_STYLE_POSTFIX;
+ }
+
+ /**
+ * @return an array containing all the fully constructed styles of the ToStringStyle library
+ */
+ public static String[] createToStringStyles() {
+ String toStringStyleLibrary = getToStringStyleLibrary();
+ return new String[] { toStringStyleLibrary + DEFAULT_STYLE_POSTFIX,
+ toStringStyleLibrary + MULTI_LINE_STYLE_POSTFIX,
+ toStringStyleLibrary + NO_FIELD_NAMES_STYLE_POSTFIX,
+ toStringStyleLibrary + SHORT_PREFIX_STYLE_POSTFIX,
+ toStringStyleLibrary + SIMPLE_STYLE_POSTFIX };
+ }
+
+ private static String createLibraryFullPath(String libraryToImportPostfix) {
+ return COMMONS_LANG_PREFIX
+ + (PreferenceUtils.getUseCommonsLang3() ? COMMONS_LANG3_ADDON
+ : EMPTY_STRING) + libraryToImportPostfix;
+ }
+
+}
diff --git a/org.jiayun.commons4e/src/org/jiayun/commons4e/internal/lang/generators/CompareToGenerator.java b/org.jiayun.commons4e/src/org/jiayun/commons4e/internal/lang/generators/CompareToGenerator.java
index 1cf6fdb..d6f1d76 100644
--- a/org.jiayun.commons4e/src/org/jiayun/commons4e/internal/lang/generators/CompareToGenerator.java
+++ b/org.jiayun.commons4e/src/org/jiayun/commons4e/internal/lang/generators/CompareToGenerator.java
@@ -139,7 +139,7 @@ private void generateCompareTo(final Shell parentShell,
objectClass, source);
objectClass.getCompilationUnit().createImport(
- "org.apache.commons.lang.builder.CompareToBuilder", null, null);
+ CommonsLangLibraryUtils.getCompareToBuilderLibrary(), null, null);
IMethod created = objectClass.createMethod(formattedContent,
insertPosition, true, null);
diff --git a/org.jiayun.commons4e/src/org/jiayun/commons4e/internal/lang/generators/EqualsHashCodeGenerator.java b/org.jiayun.commons4e/src/org/jiayun/commons4e/internal/lang/generators/EqualsHashCodeGenerator.java
index 56f4aa5..5c5854c 100644
--- a/org.jiayun.commons4e/src/org/jiayun/commons4e/internal/lang/generators/EqualsHashCodeGenerator.java
+++ b/org.jiayun.commons4e/src/org/jiayun/commons4e/internal/lang/generators/EqualsHashCodeGenerator.java
@@ -12,7 +12,6 @@
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
-import org.eclipse.jdt.core.Signature;
import org.eclipse.jdt.ui.JavaUI;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
@@ -109,6 +108,7 @@ public void generate(Shell parentShell, IType objectClass) {
boolean generateComment = dialog.getGenerateComment();
boolean compareReferences = dialog.getCompareReferences();
boolean useGettersInsteadOfFields = dialog.getUseGettersInsteadOfFields();
+ boolean useBlocksInIfStatements = dialog.getUseBlockInIfStatements();
IInitMultNumbers imNumbers = dialog.getInitMultNumbers();
IJavaElement created = generateHashCode(parentShell,
@@ -118,7 +118,8 @@ public void generate(Shell parentShell, IType objectClass) {
created = generateEquals(parentShell, objectClass,
checkedFields, created, appendSuper, generateComment,
- compareReferences, useGettersInsteadOfFields);
+ compareReferences, useGettersInsteadOfFields,
+ useBlocksInIfStatements);
ICompilationUnit cu = objectClass.getCompilationUnit();
IEditorPart javaEditor = JavaUI.openInEditor(cu);
@@ -136,8 +137,9 @@ private IJavaElement generateEquals(final Shell parentShell,
final IType objectClass, final IField[] checkedFields,
final IJavaElement insertPosition, final boolean appendSuper,
final boolean generateComment, final boolean compareReferences,
- final boolean useGettersInsteadOfFields)
- throws PartInitException, JavaModelException {
+ final boolean useGettersInsteadOfFields,
+ final boolean useBlocksInIfStatements)
+ throws PartInitException, JavaModelException {
boolean addOverride = PreferenceUtils.getAddOverride()
&& PreferenceUtils
@@ -146,13 +148,13 @@ private IJavaElement generateEquals(final Shell parentShell,
String source = createEqualsMethod(objectClass, checkedFields,
appendSuper, generateComment, compareReferences, addOverride,
- useGettersInsteadOfFields);
+ useGettersInsteadOfFields, useBlocksInIfStatements);
String formattedContent = JavaUtils.formatCode(parentShell,
objectClass, source);
objectClass.getCompilationUnit().createImport(
- "org.apache.commons.lang.builder.EqualsBuilder", null, null);
+ CommonsLangLibraryUtils.getEqualsBuilderLibrary(), null, null);
IJavaElement created = objectClass.createMethod(formattedContent,
insertPosition, true, null);
@@ -162,8 +164,8 @@ private IJavaElement generateEquals(final Shell parentShell,
private String createEqualsMethod(final IType objectClass,
final IField[] checkedFields, final boolean appendSuper,
final boolean generateComment, final boolean compareReferences,
- final boolean addOverride, final boolean useGettersInsteadOfFields)
- throws JavaModelException {
+ final boolean addOverride, final boolean useGettersInsteadOfFields,
+ final boolean useBlocksInIfStatements) throws JavaModelException {
StringBuffer content = new StringBuffer();
if (generateComment) {
@@ -177,11 +179,17 @@ private String createEqualsMethod(final IType objectClass,
}
content.append("public boolean equals(final Object other) {\n");
if (compareReferences) {
- content.append("if (this == other) return true;");
+ content.append("if (this == other)");
+ content.append(useBlocksInIfStatements ? "{\n" : "");
+ content.append(" return true;");
+ content.append(useBlocksInIfStatements ? "\n}\n" : "");
}
content.append("if ( !(other instanceof ");
content.append(objectClass.getElementName());
- content.append(") ) return false;\n");
+ content.append(") )");
+ content.append(useBlocksInIfStatements ? "{\n" : "");
+ content.append(" return false;");
+ content.append(useBlocksInIfStatements ? "\n}\n" : "");
content.append(objectClass.getElementName());
content.append(" castOther = (");
content.append(objectClass.getElementName());
@@ -226,7 +234,7 @@ private IJavaElement generateHashCode(final Shell parentShell,
objectClass, source);
objectClass.getCompilationUnit().createImport(
- "org.apache.commons.lang.builder.HashCodeBuilder", null, null);
+ CommonsLangLibraryUtils.getHashCodeBuilderLibrary(), null, null);
IJavaElement created = objectClass.createMethod(formattedContent,
insertPosition, true, null);
diff --git a/org.jiayun.commons4e/src/org/jiayun/commons4e/internal/lang/generators/ToStringGenerator.java b/org.jiayun.commons4e/src/org/jiayun/commons4e/internal/lang/generators/ToStringGenerator.java
index e8d5a2e..17a75ea 100644
--- a/org.jiayun.commons4e/src/org/jiayun/commons4e/internal/lang/generators/ToStringGenerator.java
+++ b/org.jiayun.commons4e/src/org/jiayun/commons4e/internal/lang/generators/ToStringGenerator.java
@@ -33,27 +33,6 @@
*/
public final class ToStringGenerator implements ILangGenerator {
- private static final String STYLE_PREFIX = "org.apache.commons.lang.builder.ToStringStyle";
-
- private static final String DEFAULT_STYLE = STYLE_PREFIX + "."
- + "DEFAULT_STYLE";
-
- private static final String MULTI_LINE_STYLE = STYLE_PREFIX + "."
- + "MULTI_LINE_STYLE";
-
- private static final String NO_FIELD_NAMES_STYLE = STYLE_PREFIX + "."
- + "NO_FIELD_NAMES_STYLE";
-
- private static final String SHORT_PREFIX_STYLE = STYLE_PREFIX + "."
- + "SHORT_PREFIX_STYLE";
-
- private static final String SIMPLE_STYLE = STYLE_PREFIX + "."
- + "SIMPLE_STYLE";
-
- private static final String[] STYLES = new String[] { DEFAULT_STYLE,
- MULTI_LINE_STYLE, NO_FIELD_NAMES_STYLE, SHORT_PREFIX_STYLE,
- SIMPLE_STYLE };
-
private static final ILangGenerator instance = new ToStringGenerator();
private ToStringGenerator() {
@@ -142,7 +121,7 @@ private void generateToString(final Shell parentShell,
objectClass, source);
objectClass.getCompilationUnit().createImport(
- "org.apache.commons.lang.builder.ToStringBuilder", null, null);
+ CommonsLangLibraryUtils.getToStringBuilderLibrary(), null, null);
IMethod created = objectClass.createMethod(formattedContent,
insertPosition, true, null);
@@ -166,7 +145,9 @@ private String getStyleConstantAndAddImport(final String style,
final IType objectClass) throws JavaModelException {
String styleConstant = null;
- if (!style.equals(DEFAULT_STYLE) && !style.equals("")) {
+ if (!style.equals(
+ CommonsLangLibraryUtils.getToStringStyleLibraryDefaultStyle()) &&
+ !style.equals("")) {
int lastDot = style.lastIndexOf('.');
if (lastDot != -1 && lastDot != (style.length() - 1)) {
@@ -282,8 +263,14 @@ public ToStringDialog(final Shell parentShell,
}
toStringStyle = settings.get(SETTINGS_STYLE);
- toStringStyle = toStringStyle == null ? DEFAULT_STYLE
- : toStringStyle;
+ if(toStringStyle == null) {
+ toStringStyle = CommonsLangLibraryUtils.getToStringStyleLibraryDefaultStyle();
+ }else {
+ String[] splittedToStringStyle = toStringStyle.split("\\.");
+ String chosenStyle = splittedToStringStyle[splittedToStringStyle.length-1];
+ toStringStyle = CommonsLangLibraryUtils.getToStringStyleLibrary() +
+ CommonsLangLibraryUtils.DOT_STRING + chosenStyle;
+ }
}
/*
@@ -316,7 +303,7 @@ private Composite addStyleChoices(final Composite composite) {
label.setLayoutData(data);
styleCombo = new Combo(composite, SWT.NONE);
- styleCombo.setItems(STYLES);
+ styleCombo.setItems(CommonsLangLibraryUtils.createToStringStyles());
styleCombo.setText(toStringStyle);
data = new GridData(GridData.FILL_HORIZONTAL);
diff --git a/org.jiayun.commons4e/src/org/jiayun/commons4e/internal/lang/handlers/GenerateHandler.java b/org.jiayun.commons4e/src/org/jiayun/commons4e/internal/lang/handlers/GenerateHandler.java
new file mode 100644
index 0000000..f5fc2d1
--- /dev/null
+++ b/org.jiayun.commons4e/src/org/jiayun/commons4e/internal/lang/handlers/GenerateHandler.java
@@ -0,0 +1,83 @@
+/**
+ * Copyright (c) 2014 European Organisation for Nuclear Research (CERN), All Rights Reserved.
+ */
+
+package org.jiayun.commons4e.internal.lang.handlers;
+
+import org.eclipse.core.commands.AbstractHandler;
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.commands.IHandler;
+import org.eclipse.jdt.core.ICompilationUnit;
+import org.eclipse.jdt.core.IJavaElement;
+import org.eclipse.jdt.core.IType;
+import org.eclipse.jdt.core.JavaModelException;
+import org.eclipse.jdt.ui.IWorkingCopyManager;
+import org.eclipse.jdt.ui.JavaUI;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.jface.text.ITextSelection;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.ui.IEditorActionDelegate;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.handlers.HandlerUtil;
+import org.jiayun.commons4e.internal.lang.generators.LangGenerators;
+
+/**
+ * Handler that determine which generation should be performed depending on the event commandId. It also ensures that
+ * the currently selected object in the editor is a class in order to be able to perform the generation. The
+ * {@link GenerateHandler#generate(String, ISelection, ICompilationUnit, Shell)} method is extracted from the
+ * {@link IEditorActionDelegate} written by jiayun previously in the plugin.
+ *
+ * @author jiayun, maudrain
+ */
+public class GenerateHandler extends AbstractHandler {
+
+ /**
+ * @see IHandler#execute(ExecutionEvent)
+ */
+ public Object execute(ExecutionEvent event) throws ExecutionException {
+ Shell parentShell = HandlerUtil.getActiveShell(event);
+ IEditorPart editor = HandlerUtil.getActiveEditor(event);
+ ISelection currentSelection = HandlerUtil.getCurrentSelection(event);
+ IWorkingCopyManager manager = JavaUI.getWorkingCopyManager();
+ ICompilationUnit compilationUnit = manager.getWorkingCopy(editor
+ .getEditorInput());
+ generate(event.getCommand().getId(), currentSelection, compilationUnit,
+ parentShell);
+ return null;
+ }
+
+ private void generate(String commandId, ISelection iSelection,
+ ICompilationUnit compilationUnit, Shell parentShell) {
+ IType objectClass = null;
+ try {
+ ITextSelection selection = (ITextSelection) iSelection;
+ IJavaElement element = compilationUnit.getElementAt(selection
+ .getOffset());
+ if (element != null) {
+ objectClass = (IType) element.getAncestor(IJavaElement.TYPE);
+ }
+ } catch (JavaModelException e) {
+ MessageDialog.openError(parentShell, "Error", e.getMessage());
+ }
+
+ if (objectClass == null) {
+ objectClass = compilationUnit.findPrimaryType();
+ }
+
+ try {
+ if (objectClass == null || !objectClass.isClass()) {
+ MessageDialog
+ .openInformation(parentShell, "Method Generation",
+ "Cursor not in a class, or no class has the same name with the Java file.");
+ } else {
+ LangGenerators.getGenerator(commandId).generate(parentShell,
+ objectClass);
+ }
+ } catch (JavaModelException e) {
+ MessageDialog.openError(parentShell, "Error", e.getMessage());
+ }
+ }
+
+}
diff --git a/org.jiayun.commons4e/src/org/jiayun/commons4e/internal/ui/dialogs/FieldDialog.java b/org.jiayun.commons4e/src/org/jiayun/commons4e/internal/ui/dialogs/FieldDialog.java
index 50b16be..941ad0e 100644
--- a/org.jiayun.commons4e/src/org/jiayun/commons4e/internal/ui/dialogs/FieldDialog.java
+++ b/org.jiayun.commons4e/src/org/jiayun/commons4e/internal/ui/dialogs/FieldDialog.java
@@ -72,6 +72,8 @@ public class FieldDialog extends Dialog {
private boolean generateComment;
private boolean useGettersInsteadOfFields;
+
+ private boolean useBlockInIfStatements;
private IDialogSettings settings;
@@ -264,6 +266,11 @@ protected Control createDialogArea(final Composite parent) {
data = new GridData(GridData.FILL_HORIZONTAL);
data.horizontalSpan = 2;
gettersComposite.setLayoutData(data);
+
+ Composite blocksInIfComposite = createBlocksInIfSelection(composite);
+ data = new GridData(GridData.FILL_HORIZONTAL);
+ data.horizontalSpan = 2;
+ blocksInIfComposite.setLayoutData(data);
messageLabel = new CLabel(composite, SWT.NONE);
data = new GridData(GridData.FILL_HORIZONTAL);
@@ -462,6 +469,35 @@ public void widgetDefaultSelected(SelectionEvent e) {
return gettersComposite;
}
+
+ protected Composite createBlocksInIfSelection(final Composite composite) {
+ Composite blocksInIfComposite = new Composite(composite, SWT.NONE);
+ GridLayout layout = new GridLayout();
+ layout.marginHeight = 0;
+ layout.marginWidth = 0;
+ blocksInIfComposite.setLayout(layout);
+
+ Button blocksInIfButton = new Button(blocksInIfComposite, SWT.CHECK);
+ blocksInIfButton.setText("&Use blocks in 'if' statments");
+ blocksInIfButton
+ .setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
+
+ blocksInIfButton.addSelectionListener(new SelectionListener() {
+
+ public void widgetSelected(SelectionEvent e) {
+ useBlockInIfStatements = (((Button) e.widget).getSelection());
+ }
+
+ public void widgetDefaultSelected(SelectionEvent e) {
+ widgetSelected(e);
+ }
+ });
+ useBlockInIfStatements = PreferenceUtils.getUseBlocksInIfStatements();
+ blocksInIfButton.setSelection(useBlockInIfStatements);
+
+ return blocksInIfComposite;
+ }
+
public IField[] getCheckedFields() {
return checkedFields;
@@ -485,4 +521,8 @@ public boolean getGenerateComment() {
public boolean getUseGettersInsteadOfFields() {
return useGettersInsteadOfFields;
}
+
+ public boolean getUseBlockInIfStatements() {
+ return useBlockInIfStatements;
+ }
}
diff --git a/org.jiayun.commons4e/src/org/jiayun/commons4e/internal/ui/preferences/Commons4eBasePreferencePage.java b/org.jiayun.commons4e/src/org/jiayun/commons4e/internal/ui/preferences/Commons4eBasePreferencePage.java
index 3152394..c49fbc2 100644
--- a/org.jiayun.commons4e/src/org/jiayun/commons4e/internal/ui/preferences/Commons4eBasePreferencePage.java
+++ b/org.jiayun.commons4e/src/org/jiayun/commons4e/internal/ui/preferences/Commons4eBasePreferencePage.java
@@ -18,6 +18,8 @@
public class Commons4eBasePreferencePage extends FieldEditorPreferencePage
implements IWorkbenchPreferencePage {
+ private BooleanFieldEditor useCommonsLang3;
+
private BooleanFieldEditor cacheHashCode;
private StringFieldEditor hashCodeField;
@@ -33,6 +35,8 @@ public class Commons4eBasePreferencePage extends FieldEditorPreferencePage
private BooleanFieldEditor displayFieldsOfSuperclasses;
private BooleanFieldEditor useGettersInsteadOfFields;
+
+ private BooleanFieldEditor useBlocksInIfStatements;
public Commons4eBasePreferencePage() {
super(FieldEditorPreferencePage.GRID);
@@ -45,6 +49,12 @@ public Commons4eBasePreferencePage() {
* @see org.eclipse.jface.preference.FieldEditorPreferencePage#createFieldEditors()
*/
protected void createFieldEditors() {
+ useCommonsLang3 = new BooleanFieldEditor(
+ PreferenceConstants.USE_COMMONS_LANG3,
+ "&Import commons-lang3 for all code generation",
+ getFieldEditorParent());
+ addField(useCommonsLang3);
+
cacheHashCode = new BooleanFieldEditor(
PreferenceConstants.CACHE_HASHCODE,
"Cache &hashCode when all selected fields are final",
@@ -90,6 +100,11 @@ protected void createFieldEditors() {
PreferenceConstants.USE_GETTERS_INSTEAD_OF_FIELDS,
"&Use getters instead of fields (for Hibernate)", getFieldEditorParent());
addField(useGettersInsteadOfFields);
+
+ useBlocksInIfStatements = new BooleanFieldEditor(
+ PreferenceConstants.USE_BLOCKS_IN_IF_STATEMENTS,
+ "&Use blocks in 'if' statments", getFieldEditorParent());
+ addField(useBlocksInIfStatements);
}
protected void checkState() {
diff --git a/org.jiayun.commons4e/src/org/jiayun/commons4e/internal/ui/preferences/PreferenceConstants.java b/org.jiayun.commons4e/src/org/jiayun/commons4e/internal/ui/preferences/PreferenceConstants.java
index f4b1431..6f03a58 100644
--- a/org.jiayun.commons4e/src/org/jiayun/commons4e/internal/ui/preferences/PreferenceConstants.java
+++ b/org.jiayun.commons4e/src/org/jiayun/commons4e/internal/ui/preferences/PreferenceConstants.java
@@ -11,6 +11,8 @@ public class PreferenceConstants {
private PreferenceConstants() {
}
+
+ public static final String USE_COMMONS_LANG3 = "useCommonsLang3";
public static final String CACHE_HASHCODE = "cacheHashCode";
@@ -27,10 +29,13 @@ private PreferenceConstants() {
public static final String DISPLAY_FIELDS_OF_SUPERCLASSES = "displayFieldsOfSuperclasses";
public static final String USE_GETTERS_INSTEAD_OF_FIELDS = "useGettersInsteadOfFields";
+
+ public static final String USE_BLOCKS_IN_IF_STATEMENTS = "useBlocksInIfStatements";
public static void initializeDefaultValues() {
Preferences preferences = Commons4ePlugin.getDefault()
.getPluginPreferences();
+ preferences.setDefault(USE_COMMONS_LANG3, false);
preferences.setDefault(CACHE_HASHCODE, true);
preferences.setDefault(HASHCODE_CACHING_FIELD, "hashCode");
preferences.setDefault(CACHE_TOSTRING, true);
@@ -39,5 +44,6 @@ public static void initializeDefaultValues() {
preferences.setDefault(GENERIFY_COMPARETO, true);
preferences.setDefault(DISPLAY_FIELDS_OF_SUPERCLASSES, false);
preferences.setDefault(USE_GETTERS_INSTEAD_OF_FIELDS, false);
+ preferences.setDefault(USE_BLOCKS_IN_IF_STATEMENTS, false);
}
}
diff --git a/org.jiayun.commons4e/src/org/jiayun/commons4e/internal/util/PreferenceUtils.java b/org.jiayun.commons4e/src/org/jiayun/commons4e/internal/util/PreferenceUtils.java
index d6ceac1..e520470 100644
--- a/org.jiayun.commons4e/src/org/jiayun/commons4e/internal/util/PreferenceUtils.java
+++ b/org.jiayun.commons4e/src/org/jiayun/commons4e/internal/util/PreferenceUtils.java
@@ -13,6 +13,11 @@ public final class PreferenceUtils {
private PreferenceUtils() {
}
+
+ public static boolean getUseCommonsLang3() {
+ return Commons4ePlugin.getDefault().getPluginPreferences().getBoolean(
+ PreferenceConstants.USE_COMMONS_LANG3);
+ }
public static boolean getCacheHashCode() {
return Commons4ePlugin.getDefault().getPluginPreferences().getBoolean(
@@ -53,6 +58,11 @@ public static boolean getUseGettersInsteadOfFields() {
return Commons4ePlugin.getDefault().getPluginPreferences().getBoolean(
PreferenceConstants.USE_GETTERS_INSTEAD_OF_FIELDS);
}
+
+ public static boolean getUseBlocksInIfStatements() {
+ return Commons4ePlugin.getDefault().getPluginPreferences().getBoolean(
+ PreferenceConstants.USE_BLOCKS_IN_IF_STATEMENTS);
+ }
public static boolean isSourceLevelGreaterThanOrEqualTo5(
IJavaProject project) {