Eclipse源碼閱讀:GotoTypeAction

來源:互聯網
上載者:User

Go To Type是Eclipse的Navigate菜單下的一個子功能表的功能,如下所示:

大部分內容和{Eclipse源碼閱讀:Go To Resource點擊開啟連結}一樣.

這裡直接貼同分析的核心代碼,重點部分添加了中文注釋.

/******************************************************************************* * Copyright (c) 2000, 2008 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: *     IBM Corporation - initial API and implementation *******************************************************************************/package org.eclipse.jdt.internal.ui.packageview;import org.eclipse.swt.widgets.Shell;import org.eclipse.jface.action.Action;import org.eclipse.jface.dialogs.IDialogConstants;import org.eclipse.jface.dialogs.MessageDialog;import org.eclipse.jface.dialogs.ProgressMonitorDialog;import org.eclipse.jface.viewers.IStructuredSelection;import org.eclipse.jface.viewers.StructuredSelection;import org.eclipse.ui.PlatformUI;import org.eclipse.ui.dialogs.SelectionDialog;import org.eclipse.jdt.core.ICompilationUnit;import org.eclipse.jdt.core.IJavaElement;import org.eclipse.jdt.core.IType;import org.eclipse.jdt.core.JavaModelException;import org.eclipse.jdt.core.search.SearchEngine;import org.eclipse.jdt.internal.corext.util.Messages;import org.eclipse.jdt.ui.IJavaElementSearchConstants;import org.eclipse.jdt.ui.JavaElementLabels;import org.eclipse.jdt.ui.JavaUI;import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;import org.eclipse.jdt.internal.ui.JavaPlugin;import org.eclipse.jdt.internal.ui.util.ExceptionHandler;class GotoTypeAction extends Action {private PackageExplorerPart fPackageExplorer;GotoTypeAction(PackageExplorerPart part) {super();setText(PackagesMessages.GotoType_action_label);setDescription(PackagesMessages.GotoType_action_description);fPackageExplorer= part;PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.GOTO_TYPE_ACTION);}public void run() {Shell shell= JavaPlugin.getActiveWorkbenchShell();SelectionDialog dialog= null;//本質上是構建了FilteredTypesSelectionDialog對話方塊,可參考我的另一篇總結()try {dialog= JavaUI.createTypeDialog(shell, new ProgressMonitorDialog(shell),SearchEngine.createWorkspaceScope(), IJavaElementSearchConstants.CONSIDER_ALL_TYPES, false);} catch (JavaModelException e) {String title= getDialogTitle();String message= PackagesMessages.GotoType_error_message;ExceptionHandler.handle(e, title, message);return;}dialog.setTitle(getDialogTitle());dialog.setMessage(PackagesMessages.GotoType_dialog_message);if (dialog.open() == IDialogConstants.CANCEL_ID) {return;}Object[] types= dialog.getResult();if (types != null && types.length > 0) {gotoType((IType) types[0]);//只處理選中第一個元素}}private void gotoType(IType type) {// type.getAncestor獲得指定類型(int值在IJavaElement均有定義)的祖先ICompilationUnit cu= (ICompilationUnit) type.getAncestor(IJavaElement.COMPILATION_UNIT);IJavaElement element= null;if (cu != null) {element= cu.getPrimary();}else {element= type.getAncestor(IJavaElement.CLASS_FILE);}if (element != null) {PackageExplorerPart view= PackageExplorerPart.openInActivePerspective();if (view != null) {//在Package Explorer的樹結構中定位顯示view.selectReveal(new StructuredSelection(element));if (!element.equals(getSelectedElement(view))) {MessageDialog.openInformation(fPackageExplorer.getSite().getShell(),getDialogTitle(),Messages.format(PackagesMessages.PackageExplorer_element_not_present, JavaElementLabels.getElementLabel(element, JavaElementLabels.ALL_DEFAULT)));}}}}private Object getSelectedElement(PackageExplorerPart view) {return ((IStructuredSelection)view.getSite().getSelectionProvider().getSelection()).getFirstElement();}private String getDialogTitle() {return PackagesMessages.GotoType_dialog_title;}}

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.