Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Go To Action / View added

  • Loading branch information...
commit 6fef1cc81e1128ebb61f02d002ebb2a7cb1abee1 1 parent 5c0fde4
@tmysik tmysik authored
View
4 nbproject/genfiles.properties
@@ -1,8 +1,8 @@
-build.xml.data.CRC32=9138ee77
+build.xml.data.CRC32=7d83919b
build.xml.script.CRC32=509b06b8
build.xml.stylesheet.CRC32=a56c6a5b@1.43
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
-nbproject/build-impl.xml.data.CRC32=9138ee77
+nbproject/build-impl.xml.data.CRC32=7d83919b
nbproject/build-impl.xml.script.CRC32=66d0a2dd
nbproject/build-impl.xml.stylesheet.CRC32=238281d1@1.43
View
52 nbproject/project.xml
@@ -7,6 +7,15 @@
<standalone/>
<module-dependencies>
<dependency>
+ <code-name-base>org.netbeans.modules.csl.api</code-name-base>
+ <build-prerequisite/>
+ <compile-dependency/>
+ <run-dependency>
+ <release-version>2</release-version>
+ <specification-version>2.6.0.2.1.1.4</specification-version>
+ </run-dependency>
+ </dependency>
+ <dependency>
<code-name-base>org.netbeans.modules.extexecution</code-name-base>
<build-prerequisite/>
<compile-dependency/>
@@ -16,6 +25,15 @@
</run-dependency>
</dependency>
<dependency>
+ <code-name-base>org.netbeans.modules.parsing.api</code-name-base>
+ <build-prerequisite/>
+ <compile-dependency/>
+ <run-dependency>
+ <release-version>1</release-version>
+ <specification-version>1.34.0.4</specification-version>
+ </run-dependency>
+ </dependency>
+ <dependency>
<code-name-base>org.netbeans.modules.php.api.phpmodule</code-name-base>
<build-prerequisite/>
<compile-dependency/>
@@ -24,6 +42,15 @@
</run-dependency>
</dependency>
<dependency>
+ <code-name-base>org.netbeans.modules.project.ant</code-name-base>
+ <build-prerequisite/>
+ <compile-dependency/>
+ <run-dependency>
+ <release-version>1</release-version>
+ <specification-version>1.38</specification-version>
+ </run-dependency>
+ </dependency>
+ <dependency>
<code-name-base>org.openide.dialogs</code-name-base>
<build-prerequisite/>
<compile-dependency/>
@@ -47,7 +74,32 @@
<specification-version>8.7</specification-version>
</run-dependency>
</dependency>
+ <dependency>
+ <code-name-base>org.openide.util.lookup</code-name-base>
+ <build-prerequisite/>
+ <compile-dependency/>
+ <run-dependency>
+ <specification-version>8.4</specification-version>
+ </run-dependency>
+ </dependency>
</module-dependencies>
+ <test-dependencies>
+ <test-type>
+ <name>unit</name>
+ <test-dependency>
+ <code-name-base>org.netbeans.insane</code-name-base>
+ <compile-dependency/>
+ </test-dependency>
+ <test-dependency>
+ <code-name-base>org.netbeans.libs.junit4</code-name-base>
+ <compile-dependency/>
+ </test-dependency>
+ <test-dependency>
+ <code-name-base>org.netbeans.modules.nbjunit</code-name-base>
+ <compile-dependency/>
+ </test-dependency>
+ </test-type>
+ </test-dependencies>
<public-packages/>
</data>
</configuration>
View
26 src/org/cakephp/netbeans/CakePhpActionsExtender.java
@@ -7,8 +7,14 @@
import java.util.Collections;
import java.util.List;
import javax.swing.Action;
+import org.cakephp.netbeans.ui.actions.CakePhpGoToActionAction;
+import org.cakephp.netbeans.ui.actions.CakePhpGoToViewAction;
import org.cakephp.netbeans.ui.actions.RunBakeAction;
+import org.cakephp.netbeans.util.CakePhpUtils;
+import org.netbeans.modules.php.spi.actions.GoToActionAction;
+import org.netbeans.modules.php.spi.actions.GoToViewAction;
import org.netbeans.modules.php.spi.phpmodule.PhpModuleActionsExtender;
+import org.openide.filesystems.FileObject;
import org.openide.util.NbBundle;
public class CakePhpActionsExtender extends PhpModuleActionsExtender {
@@ -23,4 +29,24 @@ public String getMenuName() {
public List<? extends Action> getActions() {
return ACTIONS;
}
+
+ @Override
+ public boolean isViewWithAction(FileObject fo) {
+ return CakePhpUtils.isView(fo);
+ }
+
+ @Override
+ public boolean isActionWithView(FileObject fo) {
+ return CakePhpUtils.isController(fo);
+ }
+
+ @Override
+ public GoToActionAction getGoToActionAction(FileObject fo, int offset) {
+ return new CakePhpGoToActionAction(fo);
+ }
+
+ @Override
+ public GoToViewAction getGoToViewAction(FileObject fo, int offset) {
+ return new CakePhpGoToViewAction(fo, offset);
+ }
}
View
52 src/org/cakephp/netbeans/ui/actions/CakePhpGoToActionAction.java
@@ -0,0 +1,52 @@
+/*
+ * TODO: add license
+ */
+
+package org.cakephp.netbeans.ui.actions;
+
+import org.cakephp.netbeans.util.CakePhpUtils;
+import org.netbeans.modules.csl.api.UiUtils;
+import org.netbeans.modules.php.api.editor.EditorSupport;
+import org.netbeans.modules.php.api.editor.PhpClass;
+import org.netbeans.modules.php.spi.actions.GoToActionAction;
+import org.openide.filesystems.FileObject;
+import org.openide.util.Lookup;
+
+public final class CakePhpGoToActionAction extends GoToActionAction {
+ private static final long serialVersionUID = 309456937957136545L;
+
+ private final FileObject view;
+
+ public CakePhpGoToActionAction(FileObject view) {
+ assert CakePhpUtils.isView(view);
+ this.view = view;
+ }
+
+ @Override
+ public boolean goToAction() {
+ FileObject controller = CakePhpUtils.getController(view);
+ if (controller != null) {
+ UiUtils.open(controller, getActionMethodOffset(controller));
+ return true;
+ }
+ return false;
+ }
+
+ private int getActionMethodOffset(FileObject controller) {
+ String actionMethodName = CakePhpUtils.getActionName(view);
+ EditorSupport editorSupport = Lookup.getDefault().lookup(EditorSupport.class);
+ for (PhpClass phpClass : editorSupport.getClasses(controller)) {
+ if (CakePhpUtils.isControllerName(phpClass.getName())) {
+ if (actionMethodName != null) {
+ for (PhpClass.Method method : phpClass.getMethods()) {
+ if (actionMethodName.equals(method.getName())) {
+ return method.getOffset();
+ }
+ }
+ }
+ return phpClass.getOffset();
+ }
+ }
+ return DEFAULT_OFFSET;
+ }
+}
View
41 src/org/cakephp/netbeans/ui/actions/CakePhpGoToViewAction.java
@@ -0,0 +1,41 @@
+/*
+ * TODO: add license
+ */
+
+package org.cakephp.netbeans.ui.actions;
+
+import org.cakephp.netbeans.util.CakePhpUtils;
+import org.netbeans.modules.csl.api.UiUtils;
+import org.netbeans.modules.php.api.editor.EditorSupport;
+import org.netbeans.modules.php.api.editor.PhpBaseElement;
+import org.netbeans.modules.php.spi.actions.GoToViewAction;
+import org.openide.filesystems.FileObject;
+import org.openide.util.Lookup;
+
+public final class CakePhpGoToViewAction extends GoToViewAction {
+ private static final long serialVersionUID = 9834759234756237L;
+
+ private final FileObject controller;
+ private final int offset;
+
+ public CakePhpGoToViewAction(FileObject controller, int offset) {
+ assert CakePhpUtils.isController(controller);
+ this.controller = controller;
+ this.offset = offset;
+ }
+
+ @Override
+ public boolean goToView() {
+ EditorSupport editorSupport = Lookup.getDefault().lookup(EditorSupport.class);
+ PhpBaseElement phpElement = editorSupport.getElement(controller, offset);
+ if (phpElement == null) {
+ return false;
+ }
+ FileObject view = CakePhpUtils.getView(controller, phpElement);
+ if (view != null) {
+ UiUtils.open(view, DEFAULT_OFFSET);
+ return true;
+ }
+ return false;
+ }
+}
View
139 src/org/cakephp/netbeans/util/CakePhpUtils.java
@@ -0,0 +1,139 @@
+/*
+ * TODO: add license
+ */
+
+package org.cakephp.netbeans.util;
+
+import java.io.File;
+import java.util.regex.Pattern;
+import org.netbeans.modules.php.api.editor.PhpBaseElement;
+import org.netbeans.modules.php.api.editor.PhpClass;
+import org.netbeans.spi.project.support.ant.PropertyUtils;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+
+public final class CakePhpUtils {
+ private static final String CONTROLLER_CLASS_SUFFIX = "Controller"; // NOI18N
+ private static final String CONTROLLER_FILE_SUFIX = "_controller"; // NOI18N
+ private static final String DIR_CONTROLLERS = "controllers"; // NOI18N
+ private static final String DIR_VIEWS = "views"; // NOI18N
+ private static final String FILE_VIEW_EXT = "ctp"; // NOI18N
+ private static final String UNDERSCORE = "_"; // NOI18N
+
+ private static final String FILE_CONTROLLER_RELATIVE = "../../" + DIR_CONTROLLERS + "/%s.php"; // NOI18N
+ private static final String FILE_VIEW_RELATIVE = "../" + DIR_VIEWS + "/%s/%s." + FILE_VIEW_EXT; // NOI18N
+
+ private CakePhpUtils() {
+ }
+
+ public static boolean isView(FileObject fo) {
+ if (!fo.isData() || !fo.getExt().equals(FILE_VIEW_EXT)) {
+ return false;
+ }
+ File file = FileUtil.toFile(fo);
+ File parent = file.getParentFile(); // controller
+ if (parent == null) {
+ return false;
+ }
+ parent = parent.getParentFile(); // scripts
+ if (parent == null) {
+ return false;
+ }
+ return DIR_VIEWS.equals(parent.getName());
+ }
+
+ public static FileObject getView(FileObject controller, PhpBaseElement phpElement) {
+ FileObject view = null;
+ if (phpElement instanceof PhpClass.Method) {
+ view = getView(controller, getViewFileName(phpElement.getName()));
+ }
+ return view;
+ }
+
+ private static FileObject getView(FileObject controller, String viewName) {
+ File parent = FileUtil.toFile(controller).getParentFile();
+ File view = PropertyUtils.resolveFile(parent, String.format(FILE_VIEW_RELATIVE, getViewFolderName(controller.getName()), viewName));
+ if (view.isFile()) {
+ return FileUtil.toFileObject(view);
+ }
+ return null;
+ }
+
+ public static boolean isControllerName(String name) {
+ return name.endsWith(CakePhpUtils.CONTROLLER_CLASS_SUFFIX);
+ }
+
+ public static boolean isControllerFileName(String filename) {
+ return filename.endsWith(CakePhpUtils.CONTROLLER_FILE_SUFIX);
+ }
+
+ public static boolean isController(FileObject fo) {
+ return fo.isData()
+ && isControllerFileName(fo.getName())
+ && fo.getParent().getNameExt().equals(DIR_CONTROLLERS);
+ }
+
+ public static FileObject getController(FileObject view) {
+ File parent = FileUtil.toFile(view).getParentFile();
+ File action = PropertyUtils.resolveFile(parent, String.format(FILE_CONTROLLER_RELATIVE, getControllerFileName(parent.getName())));
+ if (action.isFile()) {
+ return FileUtil.toFileObject(action);
+ }
+ return null;
+ }
+
+ public static String getActionName(FileObject view) {
+ return getActionName(view.getName());
+ }
+
+ // unit tests
+ static String getActionName(String viewName) {
+ return toCamelCase(viewName, true);
+ }
+
+ private static String getControllerFileName(String viewName) {
+ return viewName + CONTROLLER_FILE_SUFIX;
+ }
+
+ // unit tests
+ static String getViewFileName(String actionName) {
+ return toUnderscore(actionName);
+ }
+
+ private static String getViewFolderName(String controllerFileName) {
+ return controllerFileName.replace(CONTROLLER_FILE_SUFIX, ""); // NOI18N
+ }
+
+ // my_posts -> MyPosts or myPosts
+ private static String toCamelCase(String underscored, boolean firstLowerCase) {
+ StringBuilder sb = new StringBuilder(underscored.length());
+ boolean first = firstLowerCase;
+ for (String part : underscored.split(Pattern.quote(UNDERSCORE))) { // NOI18N
+ if (first) {
+ first = false;
+ sb.append(part);
+ } else {
+ sb.append(part.substring(0, 1).toUpperCase());
+ sb.append(part.substring(1));
+ }
+ }
+ return sb.toString();
+ }
+
+ // MyPosts -> my_posts
+ private static String toUnderscore(String input) {
+ StringBuilder sb = new StringBuilder(2 * input.length());
+ for (int i = 0; i < input.length(); ++i) {
+ char ch = input.charAt(i);
+ if (Character.isUpperCase(ch)) {
+ if (i != 0) {
+ sb.append(UNDERSCORE);
+ }
+ sb.append(Character.toLowerCase(ch));
+ } else {
+ sb.append(ch);
+ }
+ }
+ return sb.toString();
+ }
+}
View
27 test/unit/src/org/cakephp/netbeans/util/CakePhpUtilsTest.java
@@ -0,0 +1,27 @@
+/*
+ * TODO: add license
+ */
+
+package org.cakephp.netbeans.util;
+
+import org.junit.Test;
+import org.netbeans.junit.NbTestCase;
+
+public class CakePhpUtilsTest extends NbTestCase {
+
+ public CakePhpUtilsTest(String name) {
+ super(name);
+ }
+
+ @Test
+ public void testActionName() {
+ assertEquals("index", CakePhpUtils.getActionName("index"));
+ assertEquals("myIndex", CakePhpUtils.getActionName("my_index"));
+ }
+
+ @Test
+ public void testViewName() {
+ assertEquals("index", CakePhpUtils.getViewFileName("index"));
+ assertEquals("my_index", CakePhpUtils.getViewFileName("myIndex"));
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.