⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cakeconvention.java

📁 CakePHP的Eclipse插件
💻 JAVA
字号:
package org.xicabin.radcake.core.util;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IPath;
import org.xicabin.radcake.core.CakePlugin;
import org.xicabin.radcake.core.preferences.PreferenceConstants;

/**
 * Convert between MVC
 * 
 * @author Darcy Young
 */
public abstract class CakeConvention {

	private static final String PHP = ".php";

	private static final String MODELS = "models";
	private static final String VIEWS = "views";
	private static final String CONTROLLERS = "controllers";
	private static final String CONTROLLER_SUFFIX = "_controller";
	private static final String CONTROLLER_FILE_SUFFIX = CONTROLLER_SUFFIX
			+ PHP;

	public static IFile getModelFromController(IFile controllerFile) {
		if (!looksLikeController(controllerFile))
			return null;
		IPath controllerFilePath = controllerFile.getProjectRelativePath();
		String controllerFilename = controllerFilePath.lastSegment();

		IPath model = getModelFromController(controllerFilePath,
				getModelName(controllerFilename));
		IFile file = controllerFile.getProject().getFile(model);
		return file;
	}

	private static IPath getModelFromController(IPath controllerFilePath,
			String singular) {
		return getAppFolderFromController(controllerFilePath).append(MODELS)
				.append(singular);
	}

	private static String getModelName(String controllerFilename) {
		String singular = Inflector
				.singularize(getControllerBaseName(controllerFilename));
		singular += PHP;
		return singular;
	}

	public static IFile getModelFromView(IFile viewFile) {
		if (!looksLikeView(viewFile))
			return null;
		IPath viewFilePath = viewFile.getProjectRelativePath();
		IPath model = getModelFromView(viewFilePath);
		return viewFile.getProject().getFile(model);
	}

	private static IPath getModelFromView(IPath viewFilePath) {
		String singular = getModelNameFromViewPath(viewFilePath);
		return getAppFolderFromView(viewFilePath).append(MODELS).append(
				singular);
	}

	private static String getModelNameFromViewPath(IPath viewFilePath) {
		String singular = Inflector
				.singularize(getPluralResourceNameFromViewPath(viewFilePath));
		singular += PHP;
		return singular;
	}

	public static IFile getControllerFromModel(IFile modelFile) {
		IPath modelFilePath = modelFile.getProjectRelativePath();
		String modelFilename = modelFilePath.lastSegment();
		if (!looksLikeModel(modelFile))
			return null;
		String singular = modelFilename
				.substring(0, modelFilename.indexOf('.'));
		String plural = Inflector.pluralize(singular);

		String controllerName = plural + CONTROLLER_FILE_SUFFIX;
		IPath controllerPath = getAppFolderFromModel(modelFilePath).append(
				CONTROLLERS).append(controllerName);

		IFile file = modelFile.getProject().getFile(controllerPath);
		return file;
	}

	private static String getControllerBaseName(String controllerFilename) {
		return controllerFilename.substring(0, controllerFilename.length()
				- CONTROLLER_FILE_SUFFIX.length());
	}

	private static String getPluralResourceNameFromViewPath(IPath viewFilePath) {
		return viewFilePath.segment(viewFilePath.segmentCount() - 2);
	}

	public static IFile getControllerFromView(IFile viewFile) {
		if (!looksLikeView(viewFile))
			return null;
		IPath viewFilePath = viewFile.getProjectRelativePath();
		String plural = getPluralResourceNameFromViewPath(viewFilePath);

		String controllerName = plural + CONTROLLER_FILE_SUFFIX;
		IPath controllerPath = getAppFolderFromView(viewFilePath).append(
				CONTROLLERS).append(controllerName);

		IFile file = viewFile.getProject().getFile(controllerPath);
		return file;
	}

	public static IFile getViewFromAction(IFile controllerFile, String action) {
		if (!looksLikeController(controllerFile))
			return null;
		IPath controllerFilePath = controllerFile.getProjectRelativePath();
		String plural = getControllerBaseName(controllerFilePath.lastSegment());
		IPath viewPath = getAppFolderFromController(controllerFilePath).append(
				VIEWS).append(plural);
		// normal view
		String viewExt = CakePlugin.getInstance().getPreferenceStore()
				.getString(PreferenceConstants.VIEW_EXTENSION);
		IPath viewFilePath = viewPath.append(action + viewExt);
		IFile file = controllerFile.getProject().getFile(viewFilePath);
		if (!file.exists()) {
			// convert camelCased action to underscore format view name
			viewFilePath = viewPath.append(Inflector.underscorize(action)
					+ viewExt);

			file = controllerFile.getProject().getFile(viewFilePath);
		}
		return file;
	}

	private static IPath getAppFolderFromModel(IPath modelFilePath) {
		return modelFilePath.removeLastSegments(2);
	}

	private static IPath getAppFolderFromController(IPath controllerFilePath) {
		return controllerFilePath.removeLastSegments(2);
	}

	private static IPath getAppFolderFromView(IPath viewFilePath) {
		return viewFilePath.removeLastSegments(3);
	}

	public static boolean looksLikeModel(IFile currentFile) {
		if (currentFile == null)
			return false;
		IPath modelFilePath = currentFile.getProjectRelativePath();
		if (modelFilePath.segmentCount() < 2)
			return false;
		return modelFilePath.segment(modelFilePath.segmentCount() - 2).equals(
				MODELS)
				&& modelFilePath.lastSegment().endsWith(PHP);
	}

	public static boolean looksLikeController(IFile currentFile) {
		if (currentFile == null)
			return false;
		IPath controllerFilePath = currentFile.getProjectRelativePath();
		if (controllerFilePath.segmentCount() < 2)
			return false;
		return controllerFilePath
				.segment(controllerFilePath.segmentCount() - 2).equals(
						CONTROLLERS)
				&& controllerFilePath.lastSegment().endsWith(
						CONTROLLER_FILE_SUFFIX);
	}

	public static boolean looksLikeView(IFile currentFile) {
		if (currentFile == null)
			return false;
		IPath viewFilePath = currentFile.getProjectRelativePath();
		if (viewFilePath.segmentCount() < 3)
			return false;
		String viewExt = CakePlugin.getInstance().getPreferenceStore()
				.getString(PreferenceConstants.VIEW_EXTENSION);
		return viewFilePath.segment(viewFilePath.segmentCount() - 3).equals(
				VIEWS)
				&& viewFilePath.lastSegment().endsWith(viewExt);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -