/*
* Copyright 2009-2010 junithelper.org.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
package org.junithelper.plugin.action;
import java.io.File;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IEditorActionDelegate;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.junithelper.core.config.Configuration;
import org.junithelper.core.constant.RegExp;
import org.junithelper.core.constant.StringValue;
import org.junithelper.core.util.ThreadUtil;
import org.junithelper.plugin.constant.Preference;
import org.junithelper.plugin.io.PropertiesLoader;
public class OpenTestTargetAction extends AbstractAction implements IActionDelegate, IEditorActionDelegate {
ISelection selection;
public void selectionChanged(IAction action, ISelection selection) {
this.selection = selection;
}
public void setActiveEditor(IAction action, IEditorPart targetEditor) {
}
public void run(IAction action) {
store = getIPreferenceStore();
Configuration config = getConfiguration(store, selection);
PropertiesLoader props = getPropertiesLoader(store.getString(Preference.lang));
StructuredSelection structuredSelection = null;
try {
if (selection instanceof StructuredSelection) {
// viewer
structuredSelection = (StructuredSelection) selection;
}
if (isNotSelected(structuredSelection)) {
// required selecttion
openWarningForRequired(props);
return;
} else if (isSelectedSeveral(structuredSelection)) {
// select only one
openWarningForSelectOneOnly(props);
return;
}
// ----------------------------------------
// get project path, resource path
String resourcePathForTestClassFile = getResourcePathForTargetClassFile(structuredSelection);
String projectName = getProjectName(structuredSelection);
String projectRootPath = getWorkspaceRootAbsolutePath(getIWorkspaceRoot())
+ StringValue.DirectorySeparator.General + projectName + StringValue.DirectorySeparator.General;
// ----------------------------------------
// check selection
if (!resourcePathForTestClassFile.matches(".*" + RegExp.FileExtension.JavaFile)) {
openWarningForSelectJavaFile(props);
return;
}
// ----------------------------------------
// check the target file existence
String resourcePathForTargetClassFile = resourcePathForTestClassFile.replaceFirst(
config.directoryPathOfTestSourceCode, config.directoryPathOfProductSourceCode);
File openTargetIOFile = new File(projectRootPath + resourcePathForTargetClassFile);
if (!openTargetIOFile.exists()) {
// skip
return;
}
// ----------------------------------------
// try to open file
int retryCount = 0;
IEditorPart editorPart = null;
while (true) {
try {
IProject project = getIProject(projectName);
IWorkbenchPage page = getIWorkbenchPage();
IFile targetClassFile = getIFile(project, resourcePathForTargetClassFile);
editorPart = getIEditorPart(page, targetClassFile);
editorPart.setFocus();
} catch (Exception e) {
e.printStackTrace();
retryCount++;
if (retryCount > 10) {
break;
}
ThreadUtil.sleep(1500);
}
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}