Làm cách nào để lấy tên của dự án nhật thực hiện tại? Tôi đang ở chế độ xem GMF và cần tên dự án khi một số menu con của menu bật lên được sử dụng.Cách lấy tên dự án trong nhật thực?
Trả lời
GMF class này có một câu trả lời đơn giản, nếu bạn có quyền truy cập vào tên ResourcesPlugin:
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(myBundleName);
Câu trả lời chung chung (từ một potentially outdated code) có thể là tương tự (nếu bạn có một trình soạn thảo mở):
IEditorPart editorPart =
getSite().getWorkbenchWindow().getActivePage().getActiveEditor();
if(editorPart != null)
{
IFileEditorInput input = (IFileEditorInput)editorPart.getEditorInput() ;
IFile file = input.getFile();
IProject activeProject = file.getProject();
String activeProjectName = activeProject.getName();
//... use activeProjectName
}
Nếu không có biên tập được mở ra:
IViewPart [] parts =
MyPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage().getViews();
IProject activeProject = null;
for(int i=0;i<parts.length;i++)
{
if(parts[i] instanceof ResourceNavigator)
{
ResourceNavigator navigator = (ResourceNavigator)parts[i];
StructuredSelection sel =
(StructuredSelection)navigator.getTreeViewer().getSelection();
IResource resource = (IResource)sel.getFirstElement();
activeProject = resource.getProject();
break;
}
}
String activeProjectName = activeProject .getName();
Sử dụng dịch vụ lựa chọn sẽ cung cấp cho bạn đối tượng được chọn hiện tại, sau đó bạn có thể kiểm tra loại lựa chọn và nhận dự án dựa trên lựa chọn.
Nếu bạn tạo ISelectionListener và đăng ký làm người nghe trên ISelectionService, bạn sẽ được thông báo bất cứ khi nào lựa chọn hoạt động thay đổi và được tham chiếu đến phần lựa chọn và phần sở hữu.
ISelectionListener listener = new ISelectionListener() {
public void selectionChanged(IWorkbenchPart sourcePart, ISelection selection) {
setSourcePart(sourcePart);
setSelection(selection);
}
};
...
//register the listener
selectionService.addSelectionListener(listener);
...
//either get the selection service and get the selection, or use the stored version from the listener
ISelectionService selectionService =
Workbench.getInstance().getActiveWorkbenchWindow().getSelectionService();
ISelection selection = selectionService.getSelection();
if(selection instanceof IStructuredSelection) {
Object element = ((IStructuredSelection)selection).getFirstElement();
IProject project;
if (element instanceof IResource) {
project= ((IResource)element).getProject();
} else if (element instanceof PackageFragmentRootContainer) {
IJavaProject jProject =
((PackageFragmentRootContainer)element).getJavaProject();
project = jProject.getProject();
} else if (element instanceof IJavaElement) {
IJavaProject jProject= ((IJavaElement)element).getJavaProject();
project = jProject.getProject();
}
} else if (selection instanceof ITextSelection) {
if(sourcePart instanceof JavaEditor) {
IJavaElement element = SelectionConverter.resolveEnclosingElement(sourcePart, selection);
project = element.getJavaProject().getProject();
}
}
Xem chi tiết article on the Eclipse selection service để biết thêm chi tiết.
Có lẽ câu trả lời chính xác hơn tôi. +1 – VonC
+1 nhưng sử dụng tốt hơn PlatformUI.getWorkbench() thay vì Workbench.getInstance(). –
Người bán và VonC có cả hai cung cấp câu trả lời hay nhưng có vẻ như họ không hoàn thành đủ/sử dụng các lớp nội bộ. Tôi đã đưa ra những điều sau đây.
@Override
public void createPartControl(Composite parent)
{
....
// Register to be notified about selections
getSite().getWorkbenchWindow().getSelectionService()
.addPostSelectionListener(this);
// Detect the current selection
detectCurrentSelection();
}
@Override
public void selectionChanged(IWorkbenchPart part, ISelection selection)
{
IProject project = convertSelection(part, selection);
setProject(project);
}
private void detectCurrentSelection()
{
IProject project = null;
IWorkbenchWindow window = getSite().getWorkbenchWindow();
IWorkbenchPage activePage = window.getActivePage();
if (activePage != null)
{
IEditorPart part = activePage.getActiveEditor();
if (part != null)
{
project = convertSelection(part);
}
else
{
IViewReference[] viewReferences = activePage.getViewReferences();
for (IViewReference viewRef : viewReferences)
{
IViewPart view = viewRef.getView(false);
ISelection selection = null;
if (view instanceof IPackagesViewPart)
{
IPackagesViewPart viewPart = (IPackagesViewPart) view;
TreeViewer treeViewer = viewPart.getTreeViewer();
selection = treeViewer.getSelection();
}
else if (view instanceof CommonNavigator)
{
CommonNavigator navigator = (CommonNavigator) view;
CommonViewer commonViewer = navigator.getCommonViewer();
selection = commonViewer.getSelection();
}
if (selection instanceof IStructuredSelection)
{
IStructuredSelection structuredSelection = (IStructuredSelection) selection;
project = convertSelection(structuredSelection);
if (project != null)
break;
}
}
}
}
setProject(project);
}
private IProject convertSelection(IWorkbenchPart part, ISelection selection)
{
IProject project = null;
if (selection instanceof IStructuredSelection)
{
IStructuredSelection structuredSelection = (IStructuredSelection) selection;
project = convertSelection(structuredSelection);
}
else if (selection instanceof ITextSelection)
{
if (part instanceof IEditorPart)
{
IEditorPart editorPart = (IEditorPart) part;
IResource resource = (IResource)editorPart.getEditorInput().getAdapter(IResource.class);
if (resource != null)
{
project = resource.getProject();
}
}
}
return project;
}
private IProject convertSelection(IEditorPart part)
{
IProject project = null;
IResource resource = (IResource)part.getEditorInput().getAdapter(IResource.class);
if (resource != null)
{
project = resource.getProject();
}
return project;
}
private IProject convertSelection(IStructuredSelection structuredSelection)
{
IProject project = null;
Object element = structuredSelection.getFirstElement();
if (element instanceof IResource)
{
project = ((IResource) element).getProject();
}
else if (element instanceof IJavaElement)
{
IJavaElement javaElement = (IJavaElement) element;
project = javaElement.getJavaProject().getProject();
}
else if (element instanceof IAdaptable)
{
IAdaptable adaptable = (IAdaptable) element;
IWorkbenchAdapter adapter = (IWorkbenchAdapter) adaptable.getAdapter(IWorkbenchAdapter.class);
if (adapter != null)
{
Object parent = adapter.getParent(adaptable);
if (parent instanceof IJavaProject)
{
IJavaProject javaProject = (IJavaProject) parent;
project = javaProject.getProject();
}
}
}
return project;
}
Tôi đã tạo một chức năng đẹp từ các bài đăng ở trên, cộng thêm một số cập nhật. Làm việc với nhật thực hiện tại.
public static IProject getCurrentSelectedProject() {
IProject project = null;
ISelectionService selectionService =
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService();
ISelection selection = selectionService.getSelection();
if(selection instanceof IStructuredSelection) {
Object element = ((IStructuredSelection)selection).getFirstElement();
if (element instanceof IResource) {
project= ((IResource)element).getProject();
} else if (element instanceof PackageFragmentRoot) {
IJavaProject jProject =
((PackageFragmentRoot)element).getJavaProject();
project = jProject.getProject();
} else if (element instanceof IJavaElement) {
IJavaProject jProject= ((IJavaElement)element).getJavaProject();
project = jProject.getProject();
}
}
return project;
}
- 1. Lấy tên dự án của dự án WPF
- 2. dự án trong nhật thực là gì?
- 3. Tìm số lỗi trong dự án nhật thực
- 4. Cách lấy đường dẫn tuyệt đối của các tệp dự án trong nhật thực bằng cách sử dụng plugin
- 5. Làm cách nào để xác thực dự án ứng dụng web nhanh trong IDE nhật thực?
- 6. Nhập dự án vào nhật thực chỉ nhập dự án Android
- 7. Cách lấy qmake để tạo "phụ thuộc dự án" trong dự án Visual Studio .sln
- 8. Cách lấy Tài nguyên từ một dự án bên trong một dự án thư viện
- 9. lấy id dự án asana
- 10. Nhập dự án Android AOSP vào nhật thực
- 11. Làm mới các dự án nhật thực qua dòng lệnh
- 12. Cách lấy vùng tên mặc định của dự án csproj (VS 2008)
- 13. Cách lấy đường dẫn dự án từ dự án thử nghiệm?
- 14. Sử dụng T4MVC trong dự án thực
- 15. Cách tạo hai nguồn điện trong một dự án nhật thực C++
- 16. Cách tích hợp xác thực xã hội trong dự án
- 17. cách đổi tên thư mục bên trong mẫu dự án thành tên dự án? (tùy chỉnh mẫu studio trực quan)
- 18. Dự án Xcode bên trong dự án
- 19. Cách tổ chức các dự án Java lớn hơn - Dự án so với Không gian tên?
- 20. Làm thế nào để tạo tệp chiến tranh của dự án gwt trong nhật thực?
- 21. Lỗi khi cập nhật một dự án
- 22. Cách tạo dự án java thực thi trong eclipse
- 23. Android Studio - cách sử dụng thư viện (từ các dự án nhật thực)
- 24. nhật thực: tiết kiệm một cách rõ ràng dự án/không gian làm việc
- 25. Tổ chức dự án trong C Thực tiễn tốt nhất
- 26. Cập nhật tệp .settings/org.eclipse.wst.common.component trong dự án maven
- 27. Thiết lập dự án cho nhật thực bằng cách sử dụng maven
- 28. Xcode, cách cập nhật các dự án "Gần đây"
- 29. Làm thế nào để thêm một dự án vào một EAR hiện có trong nhật thực?
- 30. Cách lấy vị trí con trỏ trong nhật thực TextEditor
Đối với GMF thay đổi một phần MyPlugin để:.... 'IViewPart [] phần = {} yourPluginName DiagramEditorPlugin.getInstance() getWorkbench() getActiveWorkbenchWindow() getActivePage() getViews(); ' Tên plugin GMF của bạn có thể được tìm thấy trong .genmodel của bạn. – Ring