Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 5323 Details for
Bug 39188
Feature Request: Constructor Wizard
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read
this important communication.
[patch]
proposed enhancement.
patch39188-1.txt (text/plain), 39.16 KB, created by
Randy Faust
on 2003-07-01 06:53:09 EDT
(
hide
)
Description:
proposed enhancement.
Filename:
MIME Type:
Creator:
Randy Faust
Created:
2003-07-01 06:53:09 EDT
Size:
39.16 KB
patch
obsolete
>Index: core extension/org/eclipse/jdt/internal/corext/codemanipulation/CodeGenerationMessages.properties >=================================================================== >RCS file: /home/eclipse/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/codemanipulation/CodeGenerationMessages.properties,v >retrieving revision 1.10 >diff -u -r1.10 CodeGenerationMessages.properties >--- core extension/org/eclipse/jdt/internal/corext/codemanipulation/CodeGenerationMessages.properties 26 May 2003 10:41:53 -0000 1.10 >+++ core extension/org/eclipse/jdt/internal/corext/codemanipulation/CodeGenerationMessages.properties 1 Jul 2003 10:38:43 -0000 >@@ -17,6 +17,8 @@ > > AddUnimplementedMethodsOperation.description=Evaluating and adding unimplemented methods... > >+AddCustomConstructorOperation.description=Evaluating and adding new constructor... >+ > OrganizeImportsOperation.description=Organizing imports of {0}... > > AddJavaDocStubOperation.description=Create Javadoc stub... >Index: ui/org/eclipse/jdt/internal/ui/IJavaHelpContextIds.java >=================================================================== >RCS file: /home/eclipse/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/IJavaHelpContextIds.java,v >retrieving revision 1.74 >diff -u -r1.74 IJavaHelpContextIds.java >--- ui/org/eclipse/jdt/internal/ui/IJavaHelpContextIds.java 20 Jun 2003 14:24:11 -0000 1.74 >+++ ui/org/eclipse/jdt/internal/ui/IJavaHelpContextIds.java 1 Jul 2003 10:38:51 -0000 >@@ -30,6 +30,7 @@ > public static final String ADD_METHODSTUB_ACTION= PREFIX + "add_methodstub_action_context"; //$NON-NLS-1$ > public static final String ADD_UNIMPLEMENTED_METHODS_ACTION= PREFIX + "add_unimplemented_methods_action_context"; //$NON-NLS-1$ > public static final String ADD_UNIMPLEMENTED_CONSTRUCTORS_ACTION= PREFIX + "add_unimplemented_constructors_action_context"; //$NON-NLS-1$ >+ public static final String CREATE_NEW_CONSTRUCTOR_ACTION= PREFIX + "create_new_constructor_action_context"; //$NON-NLS-1$ > public static final String SHOW_IN_PACKAGEVIEW_ACTION= PREFIX + "show_in_packageview_action_context"; //$NON-NLS-1$ > public static final String SHOW_IN_HIERARCHYVIEW_ACTION= PREFIX + "show_in_hierarchyview_action_context"; //$NON-NLS-1$ > public static final String FOCUS_ON_SELECTION_ACTION= PREFIX + "focus_on_selection_action"; //$NON-NLS-1$ >Index: ui/org/eclipse/jdt/internal/ui/actions/ActionMessages.properties >=================================================================== >RCS file: /home/eclipse/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/actions/ActionMessages.properties,v >retrieving revision 1.90 >diff -u -r1.90 ActionMessages.properties >--- ui/org/eclipse/jdt/internal/ui/actions/ActionMessages.properties 23 Jun 2003 14:42:56 -0000 1.90 >+++ ui/org/eclipse/jdt/internal/ui/actions/ActionMessages.properties 1 Jul 2003 10:38:52 -0000 >@@ -82,7 +82,7 @@ > AddGetterSetterAction.error.actionfailed=Generating Getters and Setters Failed. > AddGetterSetterAction.error.fieldNotExisting=The field ''{0}'' has been removed in the editor. > AddGetterSetterAction.not_applicable=Operation not applicable to current text selection. A field has to be selected or a type that declares fields. >-AddGetterSetterAction.interface_not_applicable=The Generate Getters and Setters operation is not applicable on interfaces. >+AddGetterSetterAction.interface_not_applicable=The Generate Getters and Setters operation is not applicable to interfaces. > AddGetterSetterAction.read_only=The compilation unit in which the field ''{0}'' is declared is read only. > > AddGetterSetterAction.QueryDialog.title=Generate Getters and Setters >@@ -119,6 +119,21 @@ > AddUnimplementedConstructorsAction.error.nothing_found=There are no constructors from the superclass which may be added. > AddUnimplementedConstructorsAction.dialog.title=Add Constructors from Superclass > AddUnimplementedConstructorsAction.dialog.label=&Select constructors to implement: >+ >+CreateNewConstructorAction.label=Create New &Constructor using Fields... >+CreateNewConstructorAction.description=Create New Constructor using fields >+CreateNewConstructorAction.tooltip=Create New Constructor using Fields >+CreateNewConstructorAction.error.title=Create New Constructor using Fields >+CreateNewConstructorAction.error.type_removed_in_editor=Input type has been removed in editor. >+CreateNewConstructorAction.not_applicable=The operation is not applicable to the current selection. >+CreateNewConstructorAction.fields_selected={0} of {1} selected. >+CreateNewConstructorAction.dialog.title=Create New Constructor using Fields >+CreateNewConstructorAction.dialog.label=&Select fields to initialize: >+CreateNewConstructorAction.interface_not_applicable=The Create New Constructor operation is not applicable to interfaces. >+CreateNewConstructorAction.typeContainsNoFields.message=The type contains no fields. >+ >+CreateNewConstructorSelectionDialog.up_button=&Up >+CreateNewConstructorSelectionDialog.down_button=&Down > > AddJavaDocStubAction.label=Add &Javadoc Comment > AddJavaDocStubAction.description=Add a Javadoc comment stub to the member element >Index: ui/org/eclipse/jdt/ui/actions/GenerateActionGroup.java >=================================================================== >RCS file: /home/eclipse/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/actions/GenerateActionGroup.java,v >retrieving revision 1.46 >diff -u -r1.46 GenerateActionGroup.java >--- ui/org/eclipse/jdt/ui/actions/GenerateActionGroup.java 16 May 2003 15:08:22 -0000 1.46 >+++ ui/org/eclipse/jdt/ui/actions/GenerateActionGroup.java 1 Jul 2003 10:39:04 -0000 >@@ -35,13 +35,13 @@ > import org.eclipse.ui.texteditor.ConvertLineDelimitersAction; > import org.eclipse.ui.texteditor.IUpdate; > >+import org.eclipse.jdt.ui.IContextMenuConstants; >+ > import org.eclipse.jdt.internal.ui.actions.ActionMessages; > import org.eclipse.jdt.internal.ui.actions.AddTaskAction; > import org.eclipse.jdt.internal.ui.javaeditor.AddImportOnSelectionAction; > import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor; > >-import org.eclipse.jdt.ui.IContextMenuConstants; >- > /** > * Action group that adds the source and generate actions to a part's context > * menu and installs handlers for the corresponding global menu actions. >@@ -64,6 +64,7 @@ > private AddGetterSetterAction fAddGetterSetter; > private AddDelegateMethodsAction fAddDelegateMethods; > private AddUnimplementedConstructorsAction fAddUnimplementedConstructors; >+ private CreateNewConstructorAction fCreateNewConstructor; > private AddJavaDocStubAction fAddJavaDocStub; > private AddBookmarkAction fAddBookmark; > private AddTaskAction fAddTaskAction; >@@ -116,11 +117,15 @@ > fAddDelegateMethods= new AddDelegateMethodsAction(editor); > fAddDelegateMethods.setActionDefinitionId(IJavaEditorActionDefinitionIds.CREATE_DELEGATE_METHODS); > editor.setAction("AddDelegateMethods", fAddDelegateMethods); //$NON-NLS-1$ >- >+ > fAddUnimplementedConstructors= new AddUnimplementedConstructorsAction(editor); > fAddUnimplementedConstructors.setActionDefinitionId(IJavaEditorActionDefinitionIds.ADD_UNIMPLEMENTED_CONTRUCTORS); > editor.setAction("AddUnimplementedConstructors", fAddUnimplementedConstructors); //$NON-NLS-1$ >- >+ >+ fCreateNewConstructor= new CreateNewConstructorAction(editor); >+ fCreateNewConstructor.setActionDefinitionId(IJavaEditorActionDefinitionIds.CREATE_NEW_CONSTRUCTOR); >+ editor.setAction("CreateNewConstructor", fCreateNewConstructor); //$NON-NLS-1$ >+ > fAddJavaDocStub= new AddJavaDocStubAction(editor); > fAddJavaDocStub.setActionDefinitionId(IJavaEditorActionDefinitionIds.ADD_JAVADOC_COMMENT); > editor.setAction("AddJavadocComment", fAddJavaDocStub); //$NON-NLS-1$ >@@ -183,6 +188,7 @@ > fAddGetterSetter= new AddGetterSetterAction(site); > fAddDelegateMethods= new AddDelegateMethodsAction(site); > fAddUnimplementedConstructors= new AddUnimplementedConstructorsAction(site); >+ fCreateNewConstructor= new CreateNewConstructorAction(site); > fAddJavaDocStub= new AddJavaDocStubAction(site); > fAddBookmark= new AddBookmarkAction(site.getShell()); > fAddToClasspathAction= new AddToClasspathAction(site); >@@ -197,6 +203,7 @@ > fAddGetterSetter.update(selection); > fAddDelegateMethods.update(selection); > fAddUnimplementedConstructors.update(selection); >+ fCreateNewConstructor.update(selection); > fAddJavaDocStub.update(selection); > fAddToClasspathAction.update(selection); > fRemoveFromClasspathAction.update(selection); >@@ -216,6 +223,7 @@ > registerSelectionListener(provider, fAddGetterSetter); > registerSelectionListener(provider, fAddDelegateMethods); > registerSelectionListener(provider, fAddUnimplementedConstructors); >+ registerSelectionListener(provider, fCreateNewConstructor); > registerSelectionListener(provider, fAddJavaDocStub); > registerSelectionListener(provider, fAddBookmark); > registerSelectionListener(provider, fAddToClasspathAction); >@@ -288,6 +296,7 @@ > added+= addAction(result, fAddGetterSetter); > added+= addAction(result, fAddDelegateMethods); > added+= addAction(result, fAddUnimplementedConstructors); >+ added+= addAction(result, fCreateNewConstructor); > added+= addAction(result, fAddJavaDocStub); > result.add(new Separator()); > added+= addAction(result, fSurroundWithTryCatch); >@@ -308,6 +317,7 @@ > added+= addAction(result, fAddGetterSetter); > added+= addAction(result, fAddDelegateMethods); > added+= addAction(result, fAddUnimplementedConstructors); >+ added+= addAction(result, fCreateNewConstructor); > added+= addAction(result, fAddJavaDocStub); > added+= addAction(result, fAddToClasspathAction); > added+= addAction(result, fRemoveFromClasspathAction); >@@ -342,6 +352,7 @@ > actionBar.setGlobalActionHandler(JdtActionConstants.GENERATE_GETTER_SETTER, fAddGetterSetter); > actionBar.setGlobalActionHandler(JdtActionConstants.GENERATE_DELEGATE_METHODS, fAddDelegateMethods); > actionBar.setGlobalActionHandler(JdtActionConstants.ADD_CONSTRUCTOR_FROM_SUPERCLASS, fAddUnimplementedConstructors); >+ actionBar.setGlobalActionHandler(JdtActionConstants.CREATE_NEW_CONSTRUCTOR, fCreateNewConstructor); > actionBar.setGlobalActionHandler(JdtActionConstants.ADD_JAVA_DOC_COMMENT, fAddJavaDocStub); > actionBar.setGlobalActionHandler(JdtActionConstants.EXTERNALIZE_STRINGS, fExternalizeStrings); > actionBar.setGlobalActionHandler(JdtActionConstants.FIND_STRINGS_TO_EXTERNALIZE, fFindStringsToExternalize); >Index: ui/org/eclipse/jdt/ui/actions/IJavaEditorActionDefinitionIds.java >=================================================================== >RCS file: /home/eclipse/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/actions/IJavaEditorActionDefinitionIds.java,v >retrieving revision 1.30 >diff -u -r1.30 IJavaEditorActionDefinitionIds.java >--- ui/org/eclipse/jdt/ui/actions/IJavaEditorActionDefinitionIds.java 27 May 2003 08:42:18 -0000 1.30 >+++ ui/org/eclipse/jdt/ui/actions/IJavaEditorActionDefinitionIds.java 1 Jul 2003 10:39:04 -0000 >@@ -183,6 +183,12 @@ > public static final String ADD_UNIMPLEMENTED_CONTRUCTORS= "org.eclipse.jdt.ui.edit.text.java.add.unimplemented.constructors"; //$NON-NLS-1$ > > /** >+ * Action definition ID of the source ->create new constructor action >+ * (value <code>"org.eclipse.jdt.ui.edit.text.java.create.new.constructor"</code>). >+ */ >+ public static final String CREATE_NEW_CONSTRUCTOR= "org.eclipse.jdt.ui.edit.text.java.create.new.constructor"; //$NON-NLS-1$ >+ >+ /** > * Action definition ID of the source -> generate setter/getter action > * (value <code>"org.eclipse.jdt.ui.edit.text.java.create.getter.setter"</code>). > */ >Index: ui/org/eclipse/jdt/ui/actions/JdtActionConstants.java >=================================================================== >RCS file: /home/eclipse/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/actions/JdtActionConstants.java,v >retrieving revision 1.27 >diff -u -r1.27 JdtActionConstants.java >--- ui/org/eclipse/jdt/ui/actions/JdtActionConstants.java 24 Apr 2003 07:44:45 -0000 1.27 >+++ ui/org/eclipse/jdt/ui/actions/JdtActionConstants.java 1 Jul 2003 10:39:04 -0000 >@@ -176,6 +176,12 @@ > public static final String ADD_CONSTRUCTOR_FROM_SUPERCLASS= "org.eclipse.jdt.ui.actions.AddConstructorFromSuperclass"; //$NON-NLS-1$ > > /** >+ * Source menu: name of standard create New Constructor global action >+ * (value <code>"org.eclipse.jdt.ui.actions.CreateNewConstructor"</code>). >+ */ >+ public static final String CREATE_NEW_CONSTRUCTOR= "org.eclipse.jdt.ui.actions.CreateNewConstructor"; //$NON-NLS-1$ >+ >+ /** > * Source menu: name of standard Add Javadoc Comment global action > * (value <code>"org.eclipse.jdt.ui.actions.AddJavaDocComment"</code>). > */ >Index: core extension/org/eclipse/jdt/internal/corext/codemanipulation/AddCustomConstructorOperation.java >=================================================================== >RCS file: core extension/org/eclipse/jdt/internal/corext/codemanipulation/AddCustomConstructorOperation.java >diff -N core extension/org/eclipse/jdt/internal/corext/codemanipulation/AddCustomConstructorOperation.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ core extension/org/eclipse/jdt/internal/corext/codemanipulation/AddCustomConstructorOperation.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,151 @@ >+/******************************************************************************* >+ * Copyright (c) 2000, 2003 IBM Corporation and others. >+ * All rights reserved. This program and the accompanying materials >+ * are made available under the terms of the Common Public License v1.0 >+ * which accompanies this distribution, and is available at >+ * http://www.eclipse.org/legal/cpl-v10.html >+ * >+ * Contributors: >+ * IBM Corporation - initial API and implementation >+ *******************************************************************************/ >+package org.eclipse.jdt.internal.corext.codemanipulation; >+ >+import org.eclipse.core.resources.IWorkspaceRunnable; >+import org.eclipse.core.runtime.CoreException; >+import org.eclipse.core.runtime.IProgressMonitor; >+import org.eclipse.core.runtime.NullProgressMonitor; >+import org.eclipse.core.runtime.OperationCanceledException; >+import org.eclipse.core.runtime.SubProgressMonitor; >+ >+import org.eclipse.jdt.core.Flags; >+import org.eclipse.jdt.core.ICompilationUnit; >+import org.eclipse.jdt.core.IField; >+import org.eclipse.jdt.core.IJavaElement; >+import org.eclipse.jdt.core.IJavaProject; >+import org.eclipse.jdt.core.IMethod; >+import org.eclipse.jdt.core.IType; >+import org.eclipse.jdt.core.ITypeHierarchy; >+import org.eclipse.jdt.core.JavaModelException; >+import org.eclipse.jdt.core.NamingConventions; >+import org.eclipse.jdt.core.Signature; >+ >+import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility.GenStubSettings; >+import org.eclipse.jdt.internal.corext.util.JavaModelUtil; >+ >+/** >+ * Creates a custom constructor with fields initialized. >+ * If the type is open in an editor, be sure to pass over the types working working copy. >+ */ >+public class AddCustomConstructorOperation implements IWorkspaceRunnable { >+ private IJavaElement fInsertPosition; >+ private IField[] fSelected; >+ private IType fType; >+ private IMethod fConstructorCreated; >+ private boolean fDoSave; >+ private CodeGenerationSettings fSettings; >+ >+ public AddCustomConstructorOperation(IType type, CodeGenerationSettings settings, IField[] selected, boolean save, IJavaElement insertPosition) { >+ super(); >+ fType= type; >+ fDoSave= save; >+ fConstructorCreated= null; >+ fSettings= settings; >+ fSelected= selected; >+ fInsertPosition= insertPosition; >+ } >+ >+ /** >+ * Runs the operation. >+ * @throws OperationCanceledException Runtime error thrown when operation is cancelled. >+ */ >+ public void run(IProgressMonitor monitor) throws CoreException, OperationCanceledException { >+ try { >+ if (monitor == null) { >+ monitor= new NullProgressMonitor(); >+ } >+ monitor.setTaskName(CodeGenerationMessages.getString("AddCustomConstructorOperation.description")); //$NON-NLS-1$ >+ monitor.beginTask("", 3); //$NON-NLS-1$ >+ >+ ITypeHierarchy hierarchy= fType.newSupertypeHierarchy(new SubProgressMonitor(monitor, 1)); >+ monitor.worked(1); >+ >+ ImportsStructure imports= new ImportsStructure(fType.getCompilationUnit(), fSettings.importOrder, fSettings.importThreshold, true); >+ String defaultConstructor= getDefaultConstructor(fType, hierarchy, imports); >+ int firstParenIndex= defaultConstructor.indexOf("(") + 1; //$NON-NLS-1$ >+ int closingBraceIndex= defaultConstructor.indexOf("}"); //$NON-NLS-1$ >+ StringBuffer buf= new StringBuffer(defaultConstructor.substring(0, firstParenIndex)); >+ String[] params= new String[fSelected.length]; >+ for (int i= 0; i < fSelected.length; i++) { >+ buf.append(Signature.toString(fSelected[i].getTypeSignature())); >+ buf.append(" "); //$NON-NLS-1$ >+ IJavaProject project= fSelected[i].getJavaProject(); >+ String accessName= NamingConventions.removePrefixAndSuffixForFieldName(project, fSelected[i].getElementName(), fSelected[i].getFlags()); >+ if (accessName.length() > 0) { >+ char first= accessName.charAt(0); >+ if (Character.isLowerCase(first)) { >+ accessName= Character.toUpperCase(first) + accessName.substring(1); >+ } >+ } >+ >+ String paramName= StubUtility.guessArgumentName(project, accessName, new String[0]); >+ >+ buf.append(params[i]= paramName); >+ if (i != fSelected.length - 1) >+ buf.append(","); //$NON-NLS-1$ >+ } >+ buf.append(defaultConstructor.substring(firstParenIndex, closingBraceIndex)); >+ for (int i= 0; i < fSelected.length; i++) { >+ String fieldName= fSelected[i].getElementName(); >+ boolean isStatic= Flags.isStatic(fSelected[i].getFlags()); >+ if (isStatic) { >+ buf.append(fSelected[i].getParent().getElementName()); >+ buf.append("."); //$NON-NLS-1$ >+ } >+ else if ((fSettings.useKeywordThis) || params[i].equals(fieldName)) >+ buf.append("this."); //$NON-NLS-1$ >+ buf.append(fieldName); >+ buf.append(" = "); //$NON-NLS-1$ >+ buf.append(params[i]); >+ buf.append(";\n"); //$NON-NLS-1$ >+ } >+ buf.append("}"); //$NON-NLS-1$ >+ >+ String lineDelim= StubUtility.getLineDelimiterUsed(fType); >+ int indent= StubUtility.getIndentUsed(fType) + 1; >+ >+ String formattedContent= StubUtility.codeFormat(buf.toString(), indent, lineDelim) + lineDelim; >+ fConstructorCreated= fType.createMethod(formattedContent, fInsertPosition, true, null); >+ >+ monitor.worked(1); >+ imports.create(fDoSave, null); >+ monitor.worked(1); >+ } finally { >+ monitor.done(); >+ } >+ } >+ >+ private String getDefaultConstructor(IType type, ITypeHierarchy hierarchy, ImportsStructure imports) { >+ try { >+ ICompilationUnit cu= type.getCompilationUnit(); >+ GenStubSettings genStubSettings= new GenStubSettings(fSettings); >+ >+ IType objectType= type.getJavaProject().findType("java.lang.Object"); //$NON-NLS-1$ >+ IMethod curr= objectType.getMethod("Object", new String[0]); //$NON-NLS-1$ >+ IMethod defaultConstructor= JavaModelUtil.findMethodImplementationInHierarchy(hierarchy, type, curr.getElementName(), curr.getParameterTypes(), curr.isConstructor()); >+ IMethod desc= JavaModelUtil.findMethodDeclarationInHierarchy(hierarchy, type, curr.getElementName(), curr.getParameterTypes(), curr.isConstructor()); >+ String str= StubUtility.genStub(cu, type.getElementName(), defaultConstructor, desc.getDeclaringType(), genStubSettings, imports); >+ return str; >+ >+ } catch (JavaModelException e) { >+ } catch (CoreException e) { >+ } >+ return null; >+ } >+ >+ /** >+ * Returns the created constructor. To be called after a sucessful run. >+ */ >+ public IMethod getCreatedConstructor() { >+ return fConstructorCreated; >+ } >+} >Index: ui/org/eclipse/jdt/ui/actions/CreateNewConstructorAction.java >=================================================================== >RCS file: ui/org/eclipse/jdt/ui/actions/CreateNewConstructorAction.java >diff -N ui/org/eclipse/jdt/ui/actions/CreateNewConstructorAction.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ ui/org/eclipse/jdt/ui/actions/CreateNewConstructorAction.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,545 @@ >+/******************************************************************************* >+ * Copyright (c) 2000, 2003 IBM Corporation and others. >+ * All rights reserved. This program and the accompanying materials >+ * are made available under the terms of the Common Public License v1.0 >+ * which accompanies this distribution, and is available at >+ * http://www.eclipse.org/legal/cpl-v10.html >+ * >+ * Contributors: >+ * IBM Corporation - initial API and implementation >+ *******************************************************************************/ >+package org.eclipse.jdt.ui.actions; >+ >+import java.lang.reflect.InvocationTargetException; >+import java.util.ArrayList; >+import java.util.List; >+ >+import org.eclipse.core.runtime.CoreException; >+import org.eclipse.core.runtime.IStatus; >+ >+import org.eclipse.swt.layout.GridLayout; >+import org.eclipse.swt.widgets.Composite; >+import org.eclipse.swt.widgets.Shell; >+ >+import org.eclipse.jface.dialogs.IDialogConstants; >+import org.eclipse.jface.dialogs.MessageDialog; >+import org.eclipse.jface.operation.IRunnableContext; >+import org.eclipse.jface.text.IRewriteTarget; >+import org.eclipse.jface.text.ITextSelection; >+import org.eclipse.jface.viewers.CheckboxTreeViewer; >+import org.eclipse.jface.viewers.ILabelProvider; >+import org.eclipse.jface.viewers.IStructuredSelection; >+import org.eclipse.jface.viewers.ITreeContentProvider; >+import org.eclipse.jface.viewers.Viewer; >+import org.eclipse.jface.window.Window; >+ >+import org.eclipse.ui.IEditorPart; >+import org.eclipse.ui.IWorkbenchSite; >+import org.eclipse.ui.dialogs.ISelectionStatusValidator; >+import org.eclipse.ui.help.WorkbenchHelp; >+ >+import org.eclipse.jdt.core.ICompilationUnit; >+import org.eclipse.jdt.core.IField; >+import org.eclipse.jdt.core.IJavaElement; >+import org.eclipse.jdt.core.IMethod; >+import org.eclipse.jdt.core.IType; >+import org.eclipse.jdt.core.JavaModelException; >+ >+import org.eclipse.jdt.ui.JavaElementLabelProvider; >+ >+import org.eclipse.jdt.internal.corext.codemanipulation.AddCustomConstructorOperation; >+import org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings; >+import org.eclipse.jdt.internal.corext.util.JavaModelUtil; >+import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; >+import org.eclipse.jdt.internal.ui.JavaPlugin; >+import org.eclipse.jdt.internal.ui.actions.ActionMessages; >+import org.eclipse.jdt.internal.ui.actions.ActionUtil; >+import org.eclipse.jdt.internal.ui.actions.SelectionConverter; >+import org.eclipse.jdt.internal.ui.actions.WorkbenchRunnableAdapter; >+import org.eclipse.jdt.internal.ui.dialogs.SourceActionDialog; >+import org.eclipse.jdt.internal.ui.dialogs.StatusInfo; >+import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor; >+import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility; >+import org.eclipse.jdt.internal.ui.preferences.JavaPreferencesSettings; >+import org.eclipse.jdt.internal.ui.util.BusyIndicatorRunnableContext; >+import org.eclipse.jdt.internal.ui.util.ElementValidator; >+import org.eclipse.jdt.internal.ui.util.ExceptionHandler; >+ >+ >+public class CreateNewConstructorAction extends SelectionDispatchAction { >+ >+ private CompilationUnitEditor fEditor; >+ private static final String fDialogTitle= ActionMessages.getString("CreateNewConstructorAction.error.title"); //$NON-NLS-1$ >+ >+ /** >+ * Creates a new <code>CreateNewConstructorAction</code>. The action requires >+ * that the selection provided by the site's selection provider is of type <code> >+ * org.eclipse.jface.viewers.IStructuredSelection</code>. >+ * >+ * @param site the site providing context information for this action >+ */ >+ public CreateNewConstructorAction(IWorkbenchSite site) { >+ super(site); >+ setText(ActionMessages.getString("CreateNewConstructorAction.label")); //$NON-NLS-1$ >+ setDescription(ActionMessages.getString("CreateNewConstructorAction.description")); //$NON-NLS-1$ >+ setToolTipText(ActionMessages.getString("CreateNewConstructorAction.tooltip")); //$NON-NLS-1$ >+ >+ WorkbenchHelp.setHelp(this, IJavaHelpContextIds.CREATE_NEW_CONSTRUCTOR_ACTION); >+ } >+ >+ /** >+ * Note: This constructor is for internal use only. Clients should not call this constructor. >+ */ >+ public CreateNewConstructorAction(CompilationUnitEditor editor) { >+ this(editor.getEditorSite()); >+ fEditor= editor; >+ setEnabled(checkEnabledEditor()); >+ } >+ >+ //---- Structured Viewer ----------------------------------------------------------- >+ >+ /* (non-Javadoc) >+ * Method declared on SelectionDispatchAction >+ */ >+ public void selectionChanged(IStructuredSelection selection) { >+ try { >+ setEnabled(canEnable(selection)); >+ } catch (JavaModelException e) { >+ // http://bugs.eclipse.org/bugs/show_bug.cgi?id=19253 >+ if (JavaModelUtil.filterNotPresentException(e)) >+ JavaPlugin.log(e); >+ setEnabled(false); >+ } >+ } >+ >+ private boolean canEnable(IStructuredSelection selection) throws JavaModelException { >+ if (getSelectedFields(selection) != null) >+ return true; >+ >+ if ((selection.size() == 1) && (selection.getFirstElement() instanceof IType)) { >+ IType type= (IType) selection.getFirstElement(); >+ return type.getCompilationUnit() != null && type.isClass(); // look if class: not cheap but done by all source generation actions >+ } >+ >+ if ((selection.size() == 1) && (selection.getFirstElement() instanceof ICompilationUnit)) >+ return true; >+ >+ return false; >+ } >+ >+ /* (non-Javadoc) >+ * Method declared on SelectionDispatchAction >+ */ >+ public void run(IStructuredSelection selection) { >+ try { >+ IField[] selectedFields= getSelectedFields(selection); >+ // open an editor and work on a working copy >+ IEditorPart editor= EditorUtility.openInEditor(getSelectedType(selection)); >+ >+ if (canRunOn(selectedFields)){ >+ run((IType)EditorUtility.getWorkingCopy(selectedFields[0].getDeclaringType()), selectedFields, editor, false); >+ return; >+ } >+ Object firstElement= selection.getFirstElement(); >+ >+ if (firstElement instanceof IType) >+ run((IType)EditorUtility.getWorkingCopy((IType)firstElement), new IField[0], editor, false); >+ else if (firstElement instanceof ICompilationUnit) { >+ IType type= ((ICompilationUnit) firstElement).findPrimaryType(); >+ if (type.isInterface()) { >+ MessageDialog.openInformation(getShell(), fDialogTitle, ActionMessages.getString("CreateNewConstructorAction.interface_not_applicable")); //$NON-NLS-1$ >+ return; >+ } >+ else >+ run((IType)EditorUtility.getWorkingCopy(((ICompilationUnit) firstElement).findPrimaryType()), new IField[0], editor, false); >+ } >+ } catch (CoreException e) { >+ ExceptionHandler.handle(e, getShell(), fDialogTitle, ActionMessages.getString("CreateNewConstructorAction.error.actionfailed")); //$NON-NLS-1$ >+ } >+ } >+ >+ private IType getSelectedType(IStructuredSelection selection) throws JavaModelException { >+ Object[] elements= selection.toArray(); >+ if (elements.length == 1 && (elements[0] instanceof IType)) { >+ IType type= (IType) elements[0]; >+ if (type.getCompilationUnit() != null && type.isClass()) { >+ return type; >+ } >+ } >+ else if (elements[0] instanceof ICompilationUnit) { >+ ICompilationUnit cu= (ICompilationUnit) elements[0]; >+ IType type= cu.findPrimaryType(); >+ if (!type.isInterface()) >+ return type; >+ } >+ return null; >+ } >+ >+ private static boolean canRunOn(IField[] fields) throws JavaModelException { >+ return fields != null && fields.length > 0; >+ } >+ >+ /* >+ * Returns fields in the selection or <code>null</code> if the selection is >+ * empty or not valid. >+ */ >+ private IField[] getSelectedFields(IStructuredSelection selection) { >+ List elements= selection.toList(); >+ int nElements= elements.size(); >+ if (nElements > 0) { >+ IField[] res= new IField[nElements]; >+ ICompilationUnit cu= null; >+ for (int i= 0; i < nElements; i++) { >+ Object curr= elements.get(i); >+ if (curr instanceof IField) { >+ IField fld= (IField)curr; >+ >+ if (i == 0) { >+ // remember the cu of the first element >+ cu= fld.getCompilationUnit(); >+ if (cu == null) { >+ return null; >+ } >+ } else if (!cu.equals(fld.getCompilationUnit())) { >+ // all fields must be in the same CU >+ return null; >+ } >+ try { >+ if (fld.getDeclaringType().isInterface()) { >+ // no constructors for interfaces >+ return null; >+ } >+ } catch (JavaModelException e) { >+ JavaPlugin.log(e); >+ return null; >+ } >+ >+ res[i]= fld; >+ } else { >+ return null; >+ } >+ } >+ return res; >+ } >+ return null; >+ } >+ >+ //---- Java Editior -------------------------------------------------------------- >+ >+ /* (non-Javadoc) >+ * Method declared on SelectionDispatchAction >+ */ >+ public void selectionChanged(ITextSelection selection) { >+ } >+ >+ /* (non-Javadoc) >+ * Method declared on SelectionDispatchAction >+ */ >+ public void run(ITextSelection selection) { >+ >+ try { >+ IJavaElement[] elements= SelectionConverter.codeResolve(fEditor); >+ if (elements.length == 1 && (elements[0] instanceof IField)) { >+ IField field= (IField)elements[0]; >+ run(field.getDeclaringType(), new IField[] {field}, fEditor, false); >+ return; >+ } >+ IJavaElement element= SelectionConverter.getElementAtOffset(fEditor); >+ >+ if (element != null){ >+ IType type= (IType)element.getAncestor(IJavaElement.TYPE); >+ if (type != null) { >+ if (type.getFields().length > 0) { >+ run(type, new IField[0], fEditor, true); >+ return; >+ } >+ } >+ } >+ MessageDialog.openInformation(getShell(), fDialogTitle, >+ ActionMessages.getString("CreateNewConstructorAction.not_applicable")); //$NON-NLS-1$ >+ } catch (CoreException e) { >+ ExceptionHandler.handle(e, getShell(), getDialogTitle(), null); >+ } >+ } >+ >+ private boolean checkEnabledEditor() { >+ return fEditor != null && SelectionConverter.canOperateOn(fEditor); >+ } >+ >+ //---- Helpers ------------------------------------------------------------------- >+ >+ private void run(IType type, IField[] preselected, IEditorPart editor, boolean activatedFromEditor) throws CoreException { >+ if (!ElementValidator.check(type, getShell(), getDialogTitle(), activatedFromEditor)) { >+ return; >+ } >+ if (!ActionUtil.isProcessable(getShell(), type)) { >+ return; >+ } >+ >+ IField[] constructorFields= type.getFields(); >+ ArrayList constructorFieldsList= new ArrayList(); >+ for (int i= 0; i < constructorFields.length; i++) { >+ constructorFieldsList.add(constructorFields[i]); >+ } >+ if (constructorFieldsList.isEmpty()){ >+ MessageDialog.openInformation(getShell(), fDialogTitle, ActionMessages.getString("CreateNewConstructorAction.typeContainsNoFields.message")); //$NON-NLS-1$ >+ return; >+ } >+ >+ JavaElementLabelProvider labelProvider= new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT); >+ CreateNewConstructorContentProvider contentProvider = new CreateNewConstructorContentProvider(constructorFieldsList); >+ CreateNewConstructorSelectionDialog dialog= new CreateNewConstructorSelectionDialog(getShell(), labelProvider, contentProvider, fEditor, type); >+ dialog.setCommentString(ActionMessages.getString("SourceActionDialog.createConstructorComment")); //$NON-NLS-1$ >+ dialog.setTitle(getDialogTitle()); >+ dialog.setInitialSelections(preselected); >+ dialog.setContainerMode(true); >+ dialog.setSize(60, 18); >+ dialog.setInput(new Object()); >+ dialog.setMessage(ActionMessages.getString("CreateNewConstructorAction.dialog.label")); //$NON-NLS-1$ >+ dialog.setValidator(createValidator(constructorFields.length)); >+ >+ IField[] selected= null; >+ int dialogResult = dialog.open(); >+ if (dialogResult == Window.OK) { >+ Object[] checkedElements = dialog.getResult(); >+ if (checkedElements == null) >+ return; >+ ArrayList result= new ArrayList(checkedElements.length); >+ for (int i= 0; i < checkedElements.length; i++) { >+ Object curr= checkedElements[i]; >+ if (curr instanceof IField) { >+ result.add(curr); >+ } >+ } >+ >+ selected= (IField[]) result.toArray(new IField[result.size()]); >+ >+ CodeGenerationSettings settings= JavaPreferencesSettings.getCodeGenerationSettings(); >+ settings.createComments= dialog.getGenerateComment(); >+ >+ IJavaElement elementPosition= dialog.getElementPosition(); >+ AddCustomConstructorOperation op= new AddCustomConstructorOperation(type, settings, selected, false, elementPosition); >+ >+ IRewriteTarget target= editor != null ? (IRewriteTarget) editor.getAdapter(IRewriteTarget.class) : null; >+ if (target != null) { >+ target.beginCompoundChange(); >+ } >+ try { >+ IRunnableContext context= JavaPlugin.getActiveWorkbenchWindow(); >+ if (context == null) { >+ context= new BusyIndicatorRunnableContext(); >+ } >+ context.run(false, true, new WorkbenchRunnableAdapter(op)); >+ IMethod res= op.getCreatedConstructor(); >+ >+ if (res.getCompilationUnit().isWorkingCopy()) { >+ synchronized(res.getCompilationUnit()) { >+ res.getCompilationUnit().reconcile(); >+ } >+ } >+ EditorUtility.revealInEditor(editor, res); >+ >+ } catch (InvocationTargetException e) { >+ ExceptionHandler.handle(e, getShell(), getDialogTitle(), null); >+ } catch (InterruptedException e) { >+ // Do nothing. Operation has been canceled by user. >+ } finally { >+ if (target != null) { >+ target.endCompoundChange(); >+ } >+ } >+ } >+ } >+ >+ private static ISelectionStatusValidator createValidator(int entries) { >+ CreateNewConstructorValidator validator= new CreateNewConstructorValidator(entries); >+ return validator; >+ } >+ >+ private String getDialogTitle() { >+ return fDialogTitle; >+ } >+ >+ private static class CreateNewConstructorContentProvider implements ITreeContentProvider { >+ >+ private List fFieldsList; >+ private static final Object[] EMPTY= new Object[0]; >+ >+ public CreateNewConstructorContentProvider(List fieldList) { >+ fFieldsList= fieldList; >+ } >+ >+ /* >+ * @see ITreeContentProvider#getChildren(Object) >+ */ >+ public Object[] getChildren(Object parentElement) { >+ return EMPTY; >+ } >+ >+ /* >+ * @see ITreeContentProvider#getParent(Object) >+ */ >+ public Object getParent(Object element) { >+ return null; >+ } >+ >+ /* >+ * @see ITreeContentProvider#hasChildren(Object) >+ */ >+ public boolean hasChildren(Object element) { >+ return getChildren(element).length > 0; >+ } >+ >+ >+ /* >+ * @see IStructuredContentProvider#getElements(Object) >+ */ >+ public Object[] getElements(Object inputElement) { >+ return fFieldsList.toArray(); >+ } >+ >+ /* >+ * @see IContentProvider#dispose() >+ */ >+ public void dispose() { >+ } >+ >+ /* >+ * @see IContentProvider#inputChanged(Viewer, Object, Object) >+ */ >+ public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { >+ } >+ >+ private List moveUp(List elements, List move) { >+ int nElements= elements.size(); >+ List res= new ArrayList(nElements); >+ Object floating= null; >+ for (int i= 0; i < nElements; i++) { >+ Object curr= elements.get(i); >+ if (move.contains(curr)) { >+ res.add(curr); >+ } else { >+ if (floating != null) { >+ res.add(floating); >+ } >+ floating= curr; >+ } >+ } >+ if (floating != null) { >+ res.add(floating); >+ } >+ return res; >+ } >+ >+ private List reverse(List p) { >+ List reverse= new ArrayList(p.size()); >+ for (int i= p.size() - 1; i >= 0; i--) { >+ reverse.add(p.get(i)); >+ } >+ return reverse; >+ } >+ >+ public void setElements(List elements, CheckboxTreeViewer tree) { >+ fFieldsList= new ArrayList(elements); >+ if (tree != null) >+ tree.refresh(); >+ } >+ >+ public void up(List checkedElements, CheckboxTreeViewer tree) { >+ if (checkedElements.size() > 0) { >+ setElements(moveUp(fFieldsList, checkedElements), tree); >+ tree.reveal(checkedElements.get(0)); >+ } >+ } >+ >+ public void down(List checkedElements, CheckboxTreeViewer tree) { >+ if (checkedElements.size() > 0) { >+ setElements(reverse(moveUp(reverse(fFieldsList), checkedElements)), tree); >+ tree.reveal(checkedElements.get(checkedElements.size() - 1)); >+ } >+ } >+ } >+ >+ private static class CreateNewConstructorValidator implements ISelectionStatusValidator { >+ private static int fEntries; >+ >+ CreateNewConstructorValidator(int entries) { >+ super(); >+ fEntries= entries; >+ } >+ >+ public IStatus validate(Object[] selection) { >+ int count= countSelectedFields(selection); >+ >+ String message= ActionMessages.getFormattedString("CreateNewConstructorAction.fields_selected", //$NON-NLS-1$ >+ new Object[] { String.valueOf(count), String.valueOf(fEntries)} ); >+ return new StatusInfo(IStatus.INFO, message); >+ } >+ >+ private int countSelectedFields(Object[] selection){ >+ int count= 0; >+ for (int i = 0; i < selection.length; i++) { >+ if (selection[i] instanceof IField) >+ count++; >+ } >+ return count; >+ } >+ } >+ >+ private static class CreateNewConstructorSelectionDialog extends SourceActionDialog { >+ private CreateNewConstructorContentProvider fContentProvider; >+ private static final int UP_BUTTON= IDialogConstants.CLIENT_ID + 1; >+ private static final int DOWN_BUTTON= IDialogConstants.CLIENT_ID + 2; >+ >+ public CreateNewConstructorSelectionDialog(Shell parent, ILabelProvider labelProvider, CreateNewConstructorContentProvider contentProvider, CompilationUnitEditor editor, IType type) { >+ super(parent, labelProvider, contentProvider, editor, type); >+ fContentProvider= contentProvider; >+ } >+ >+ protected Composite createSelectionButtons(Composite composite) { >+ Composite buttonComposite= super.createSelectionButtons(composite); >+ >+ GridLayout layout = new GridLayout(); >+ buttonComposite.setLayout(layout); >+ >+ createUpDownButtons(buttonComposite); >+ >+ layout.marginHeight= 0; >+ layout.marginWidth= 0; >+ layout.numColumns= 1; >+ >+ return buttonComposite; >+ } >+ >+ protected void createUpDownButtons(Composite buttonComposite) { >+ createButton(buttonComposite, UP_BUTTON, ActionMessages.getString("CreateNewConstructorSelectionDialog.up_button"), false); //$NON-NLS-1$ >+ createButton(buttonComposite, DOWN_BUTTON, ActionMessages.getString("CreateNewConstructorSelectionDialog.down_button"), false); //$NON-NLS-1$ >+ } >+ >+ protected void buttonPressed(int buttonId) { >+ super.buttonPressed(buttonId); >+ switch(buttonId) { >+ case UP_BUTTON: { >+ fContentProvider.up(getElementList(), getTreeViewer()); >+ updateOKStatus(); >+ break; >+ } >+ case DOWN_BUTTON: { >+ fContentProvider.down(getElementList(), getTreeViewer()); >+ updateOKStatus(); >+ break; >+ } >+ } >+ } >+ >+ private List getElementList() { >+ Object elements[]= getTreeViewer().getCheckedElements(); >+ ArrayList elementList= new ArrayList(); >+ for(int i = 0; i < elements.length; i++) >+ elementList.add(elements[i]); >+ return elementList; >+ } >+ >+ } >+ >+}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 39188
: 5323 |
5330
|
5331