~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to java/editor/src/org/netbeans/modules/java/editor/codegen/ImplementOverrideMethodGenerator.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common
 
8
 * Development and Distribution License("CDDL") (collectively, the
 
9
 * "License"). You may not use this file except in compliance with the
 
10
 * License. You can obtain a copy of the License at
 
11
 * http://www.netbeans.org/cddl-gplv2.html
 
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
13
 * specific language governing permissions and limitations under the
 
14
 * License.  When distributing the software, include this License Header
 
15
 * Notice in each file and include the License file at
 
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
17
 * particular file as subject to the "Classpath" exception as provided
 
18
 * by Sun in the GPL Version 2 section of the License file that
 
19
 * accompanied this code. If applicable, add the following below the
 
20
 * License Header, with the fields enclosed by brackets [] replaced by
 
21
 * your own identifying information:
 
22
 * "Portions Copyrighted [year] [name of copyright owner]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * The Original Software is NetBeans. The Initial Developer of the Original
 
27
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
 
28
 * Microsystems, Inc. All Rights Reserved.
 
29
 *
 
30
 * If you wish your version of this file to be governed by only the CDDL
 
31
 * or only the GPL Version 2, indicate your decision by adding
 
32
 * "[Contributor] elects to include this software in this distribution
 
33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 
34
 * single choice of license, a recipient has the option to distribute
 
35
 * your version of this file under either the CDDL, the GPL Version 2 or
 
36
 * to extend the choice of license to its licensees as provided above.
 
37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 
38
 * Version 2 license, then the option applies only if the new code is
 
39
 * made subject to such option by the copyright holder.
 
40
 */
 
41
package org.netbeans.modules.java.editor.codegen;
 
42
 
 
43
import com.sun.source.tree.ClassTree;
 
44
import com.sun.source.tree.Tree;
 
45
import com.sun.source.util.TreePath;
 
46
import java.awt.Dialog;
 
47
import java.io.IOException;
 
48
import java.util.ArrayList;
 
49
import java.util.Collections;
 
50
import java.util.LinkedHashMap;
 
51
import java.util.List;
 
52
import java.util.Map;
 
53
import javax.lang.model.element.Element;
 
54
import javax.lang.model.element.ExecutableElement;
 
55
import javax.lang.model.element.TypeElement;
 
56
import javax.swing.text.JTextComponent;
 
57
import org.netbeans.api.java.source.Task;
 
58
import org.netbeans.api.java.source.CompilationController;
 
59
import org.netbeans.api.java.source.ElementHandle;
 
60
import org.netbeans.api.java.source.JavaSource;
 
61
import org.netbeans.api.java.source.ModificationResult;
 
62
import org.netbeans.api.java.source.WorkingCopy;
 
63
import org.netbeans.modules.editor.java.Utilities;
 
64
import org.netbeans.modules.java.editor.codegen.ui.ElementNode;
 
65
import org.netbeans.modules.java.editor.codegen.ui.ImplementOverridePanel;
 
66
import org.openide.DialogDescriptor;
 
67
import org.openide.DialogDisplayer;
 
68
import org.openide.util.Exceptions;
 
69
import org.openide.util.NbBundle;
 
70
 
 
71
/**
 
72
 *
 
73
 * @author Dusan Balek
 
74
 */
 
75
public class ImplementOverrideMethodGenerator implements CodeGenerator {
 
76
 
 
77
    public static class Factory implements CodeGenerator.Factory {
 
78
        
 
79
        Factory() {            
 
80
        }
 
81
        
 
82
        public Iterable<? extends CodeGenerator> create(CompilationController controller, TreePath path) throws IOException {
 
83
            List<CodeGenerator> ret = new ArrayList<CodeGenerator>();
 
84
            path = Utilities.getPathElementOfKind(Tree.Kind.CLASS, path);
 
85
            if (path == null)
 
86
                return ret;
 
87
            controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
 
88
            TypeElement typeElement = (TypeElement)controller.getTrees().getElement(path);
 
89
            if (typeElement == null || !typeElement.getKind().isClass())
 
90
                return Collections.emptySet();
 
91
            Map<Element, List<ElementNode.Description>> map = new LinkedHashMap<Element, List<ElementNode.Description>>();
 
92
            for (ExecutableElement method : GeneratorUtils.findUndefs(controller, typeElement)) {
 
93
                List<ElementNode.Description> descriptions = map.get(method.getEnclosingElement());
 
94
                if (descriptions == null) {
 
95
                    descriptions = new ArrayList<ElementNode.Description>();
 
96
                    map.put(method.getEnclosingElement(), descriptions);
 
97
                }
 
98
                descriptions.add(ElementNode.Description.create(method, null, true, false));
 
99
            }
 
100
            List<ElementNode.Description> implementDescriptions = new ArrayList<ElementNode.Description>();
 
101
            for (Map.Entry<Element, List<ElementNode.Description>> entry : map.entrySet())
 
102
                implementDescriptions.add(ElementNode.Description.create(entry.getKey(), entry.getValue(), false, false));
 
103
            if (!implementDescriptions.isEmpty())
 
104
                ret.add(new ImplementOverrideMethodGenerator(ElementNode.Description.create(implementDescriptions), true));
 
105
            map = new LinkedHashMap<Element, List<ElementNode.Description>>();
 
106
            ArrayList<Element> orderedElements = new ArrayList<Element>();
 
107
            for (ExecutableElement method : GeneratorUtils.findOverridable(controller, typeElement)) {
 
108
                List<ElementNode.Description> descriptions = map.get(method.getEnclosingElement());
 
109
                if (descriptions == null) {
 
110
                    descriptions = new ArrayList<ElementNode.Description>();
 
111
                    Element e = method.getEnclosingElement();
 
112
                    map.put(e, descriptions);
 
113
                    if( !orderedElements.contains( e ) )
 
114
                        orderedElements.add( e );
 
115
                }
 
116
                descriptions.add(ElementNode.Description.create(method, null, true, false));
 
117
            }
 
118
            List<ElementNode.Description> overrideDescriptions = new ArrayList<ElementNode.Description>();
 
119
            for (Element e : orderedElements)
 
120
                overrideDescriptions.add(ElementNode.Description.create(e, map.get( e ), false, false));
 
121
            if (!overrideDescriptions.isEmpty())
 
122
                ret.add(new ImplementOverrideMethodGenerator(ElementNode.Description.create(overrideDescriptions), false));
 
123
            return ret;
 
124
        }
 
125
    }
 
126
    
 
127
    private ElementNode.Description description;
 
128
    private boolean isImplement;
 
129
    
 
130
    /** Creates a new instance of OverrideMethodGenerator */
 
131
    private ImplementOverrideMethodGenerator(ElementNode.Description description, boolean isImplement) {
 
132
        this.description = description;
 
133
        this.isImplement = isImplement;
 
134
    }
 
135
 
 
136
    public String getDisplayName() {
 
137
        return org.openide.util.NbBundle.getMessage(ImplementOverrideMethodGenerator.class, isImplement ? "LBL_implement_method" : "LBL_override_method"); //NOI18N
 
138
    }
 
139
 
 
140
    public void invoke(JTextComponent component) {
 
141
        final ImplementOverridePanel panel = new ImplementOverridePanel(description, isImplement);
 
142
        DialogDescriptor dialogDescriptor = GeneratorUtils.createDialogDescriptor(panel, 
 
143
                NbBundle.getMessage(ConstructorGenerator.class, isImplement ?  "LBL_generate_implement" : "LBL_generate_override")); //NOI18N  //NOI18N
 
144
        Dialog dialog = DialogDisplayer.getDefault().createDialog(dialogDescriptor);
 
145
        dialog.setVisible(true);
 
146
        if (dialogDescriptor.getValue() == dialogDescriptor.getDefaultValue()) {
 
147
            JavaSource js = JavaSource.forDocument(component.getDocument());
 
148
            if (js != null) {
 
149
                try {
 
150
                    final int caretOffset = component.getCaretPosition();
 
151
                    ModificationResult mr = js.runModificationTask(new Task<WorkingCopy>() {
 
152
                        public void run(WorkingCopy copy) throws IOException {
 
153
                            copy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
 
154
                            TreePath path = copy.getTreeUtilities().pathFor(caretOffset);
 
155
                            path = Utilities.getPathElementOfKind(Tree.Kind.CLASS, path);
 
156
                            int idx = GeneratorUtils.findClassMemberIndex(copy, (ClassTree)path.getLeaf(), caretOffset);
 
157
                            ArrayList<ExecutableElement> methodElements = new ArrayList<ExecutableElement>();
 
158
                            for (ElementHandle<? extends Element> elementHandle : panel.getSelectedMethods())
 
159
                                methodElements.add((ExecutableElement)elementHandle.resolve(copy));
 
160
                            if (isImplement)
 
161
                                GeneratorUtils.generateAbstractMethodImplementations(copy, path, methodElements, idx);
 
162
                            else
 
163
                                GeneratorUtils.generateMethodOverrides(copy, path, methodElements, idx);
 
164
                        }
 
165
                    });
 
166
                    GeneratorUtils.guardedCommit(component, mr);
 
167
                } catch (IOException ex) {
 
168
                    Exceptions.printStackTrace(ex);
 
169
                }
 
170
            }
 
171
        }
 
172
    }
 
173
}