001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * Portions Copyrighted 2007 Sun Microsystems, Inc.
027: */
028: package org.netbeans.modules.ruby.hints;
029:
030: import java.lang.reflect.Method;
031: import java.util.Collections;
032: import java.util.List;
033: import java.util.Set;
034: import java.util.prefs.Preferences;
035: import javax.swing.JComponent;
036: import org.jruby.ast.Node;
037: import org.jruby.ast.NodeTypes;
038: import org.jruby.ast.types.INameNode;
039: import org.netbeans.modules.gsf.api.CompilationInfo;
040: import org.netbeans.modules.gsf.api.OffsetRange;
041: import org.netbeans.api.project.FileOwnerQuery;
042: import org.netbeans.api.project.Project;
043: import org.netbeans.modules.ruby.Arity;
044: import org.netbeans.modules.ruby.AstUtilities;
045: import org.netbeans.modules.ruby.RubyUtils;
046: import org.netbeans.modules.ruby.hints.spi.AstRule;
047: import org.netbeans.modules.ruby.hints.spi.Description;
048: import org.netbeans.modules.ruby.hints.spi.Fix;
049: import org.netbeans.modules.ruby.hints.spi.HintSeverity;
050: import org.netbeans.modules.ruby.hints.spi.RuleContext;
051: import org.openide.filesystems.FileObject;
052: import org.openide.util.Exceptions;
053: import org.openide.util.NbBundle;
054: import org.openide.util.actions.SystemAction;
055:
056: /**
057: * Check existence of view files for actions (and offer to fix it)
058: *
059: * @author Tor Norbye
060: */
061: public class RailsViews implements AstRule {
062: public RailsViews() {
063: }
064:
065: public boolean appliesTo(CompilationInfo info) {
066: return info.getFileObject().getName().endsWith("_controller"); // NOI18N
067: }
068:
069: public Set<Integer> getKinds() {
070: return Collections.singleton(NodeTypes.DEFNNODE);
071: }
072:
073: public void run(RuleContext context, List<Description> result) {
074: Node node = context.node;
075: CompilationInfo info = context.compilationInfo;
076:
077: // See if this ia an action method and see if it has a view
078: FileObject file = info.getFileObject();
079: assert file.getName().endsWith("_controller"); // NOI18N
080:
081: // Methods with arguments aren't actions
082: Arity arity = Arity.getDefArity(node);
083: if (arity.getMinArgs() != 0 || arity.getMaxArgs() != 0) {
084: return;
085: }
086:
087: String name = ((INameNode) node).getName();
088:
089: FileObject view = RubyUtils.getRailsViewFor(file, name, false,
090: true);
091:
092: if (view == null && shouldHaveView(info, node)) {
093: String displayName = NbBundle.getMessage(RailsViews.class,
094: "MissingView");
095: OffsetRange range = AstUtilities.getNameRange(node);
096: List<Fix> fixList = Collections
097: .<Fix> singletonList(new CreateViewFix(file, name));
098: Description desc = new Description(this , displayName, file,
099: range, fixList, 400);
100: result.add(desc);
101: }
102: }
103:
104: /**
105: * Determine whether an action method should have an associated view file.
106: * For example, methods that contain a redirect method probably don't need one.
107: */
108: private boolean shouldHaveView(CompilationInfo info, Node node) {
109: if (node.nodeId == NodeTypes.FCALLNODE) {
110: String method = ((INameNode) node).getName();
111:
112: if (method.startsWith("redirect_")) { // NOI18N
113: return false;
114: }
115: }
116:
117: @SuppressWarnings("unchecked")
118: List<Node> list = node.childNodes();
119:
120: for (Node child : list) {
121: boolean result = shouldHaveView(info, child);
122:
123: if (!result) {
124: return result;
125: }
126: }
127:
128: return true;
129: }
130:
131: public String getId() {
132: return "Rails_Views"; // NOI18N
133: }
134:
135: public String getDisplayName() {
136: return NbBundle.getMessage(RailsViews.class, "FindActionViews");
137: }
138:
139: public String getDescription() {
140: return NbBundle.getMessage(RailsViews.class,
141: "FindActionViewsDesc");
142: }
143:
144: public boolean getDefaultEnabled() {
145: return false;
146: }
147:
148: public HintSeverity getDefaultSeverity() {
149: return HintSeverity.WARNING;
150: }
151:
152: public boolean showInTasklist() {
153: return false;
154: }
155:
156: public JComponent getCustomizer(Preferences node) {
157: return null;
158: }
159:
160: private static class CreateViewFix implements Fix {
161:
162: private FileObject controller;
163: private String action;
164:
165: CreateViewFix(FileObject controller, String action) {
166: this .controller = controller;
167: this .action = action;
168: }
169:
170: public String getDescription() {
171: return NbBundle.getMessage(NestedLocal.class, "CreateView");
172: }
173:
174: public void implement() throws Exception {
175: String controllerName = RubyUtils
176: .getControllerName(controller);
177: Project project = FileOwnerQuery.getOwner(controller);
178: if (project == null) {
179: return;
180: }
181: try {
182: Class c = Class
183: .forName(
184: "org.netbeans.modules.ruby.railsprojects.GenerateAction",
185: true, // NOI18N
186: Thread.currentThread()
187: .getContextClassLoader());
188: if (c != null) {
189: //Object generateAction = c.newInstance();
190: @SuppressWarnings("unchecked")
191: SystemAction generateAction = SystemAction.get(c);
192: @SuppressWarnings("unchecked")
193: Method m = c.getMethod("generate", // NOI18N
194: new Class[] { Project.class, String.class,
195: String.class, String.class });
196: m.invoke(generateAction, new Object[] { project,
197: "controller", controllerName, action }); // NOI18N
198: }
199: } catch (Exception ex) {
200: Exceptions.printStackTrace(ex);
201: }
202: }
203:
204: public boolean isSafe() {
205: return false;
206: }
207:
208: public boolean isInteractive() {
209: return true;
210: }
211: }
212: }
|