/*
* Copyright 2008 Georgi Staykov
*
* This file is part of pscoder.
*
* pscoder is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* pscoder is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with pscoder. If not, see <http://www.gnu.org/licenses/>.
*/
package com.gstaykov.pscoder.util;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IPathEditorInput;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.texteditor.AbstractTextEditor;
import org.eclipse.ui.texteditor.ITextEditor;
import com.gstaykov.pscoder.editor.completion.CompletionDictionary;
import com.gstaykov.pscoder.editor.completion.FileData;
public class Util {
public static IDocument getActiveDocument(ExecutionEvent event) {
IEditorPart part = HandlerUtil.getActiveEditor(event);
IDocument doc = null;
if ((part != null) && (part instanceof AbstractTextEditor)) {
ITextEditor editor = (ITextEditor)part;
doc = editor.getDocumentProvider().getDocument(editor.getEditorInput());
}
return doc;
}
public static IEditorPart getActiveEditor() {
return PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
}
public static IResource getActiveFile() {
IEditorPart editorPart = getActiveEditor();
IEditorInput editorInput = editorPart.getEditorInput();
if (editorInput instanceof IPathEditorInput) {
IFile[] files = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocation(((IPathEditorInput)editorInput).getPath());
// if more than 1 file matches return null because it is not clear which file is wanted
return (files.length == 1)? files[0] : null;
}
return null;
}
public static IProject getActiveProject() {
return getActiveFile().getProject();
}
public static FileData getFileDataForActiveFile() {
IResource activeFile = getActiveFile();
String projectName = activeFile.getProject().getName();
try {
CompletionDictionary projectDictionary = CompletionDictionary.getInstance(projectName);
return projectDictionary.getFileData(activeFile.getLocation().toFile().getAbsolutePath());
} catch (CoreException ce) {
Logger.getInstance().logError("Error while trying to get completion dictionary object: \n", ce);
}
return null;
}
public static String convertFileNameToWindowsNotation(String fileName) {
return fileName.replace('/', '\\');
}
}