📄 win32explorerview.java
字号:
/* * Copyright (c) 2007 Software Wizards Pty Ltd, Victoria, Australia. * mailto:enquires@swz.com.au. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted. See the GNU General Public License * for more details. * * Redistribution of the SWTtoCOM software is not permitted as part of any * commercial product. Licensing terms for such distribution may be * obtained from the copyright holder. * * This program 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 this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */package au.com.swz.swttocom.example.win32explorer.views;import org.eclipse.jface.action.IMenuManager;import org.eclipse.jface.action.IToolBarManager;import org.eclipse.jface.action.MenuManager;import org.eclipse.jface.action.Separator;import org.eclipse.jface.viewers.ISelection;import org.eclipse.jface.viewers.ISelectionChangedListener;import org.eclipse.jface.viewers.IStructuredSelection;import org.eclipse.jface.viewers.LabelProvider;import org.eclipse.jface.viewers.SelectionChangedEvent;import org.eclipse.jface.viewers.TreeViewer;import org.eclipse.swt.SWT;import org.eclipse.swt.graphics.Image;import org.eclipse.swt.internal.ole.win32.COM;import org.eclipse.swt.ole.win32.Variant;import org.eclipse.swt.widgets.Composite;import org.eclipse.swt.widgets.Menu;import org.eclipse.ui.IActionBars;import org.eclipse.ui.ISharedImages;import org.eclipse.ui.part.DrillDownAdapter;import org.eclipse.ui.part.ViewPart;import au.com.swz.swttocom.example.win32explorer.shell.FolderItem;import au.com.swz.swttocom.example.win32explorer.shell.FolderItemVerb;import au.com.swz.swttocom.example.win32explorer.shell.FolderItemVerbs;import au.com.swz.swttocom.example.win32explorer.shell.Shell;import au.com.swz.swttocom.example.win32explorer.shell.enums.ShellSpecialFolderConstants;public class Win32ExplorerView extends ViewPart { private TreeViewer viewer; private Shell win32Shell; private DrillDownAdapter drillDownAdapter; private VerbToolbarAction[] toolbarActions; class ViewLabelProvider extends LabelProvider { public String getText(Object obj) { if (obj instanceof AbstractTreeObject) { return ((AbstractTreeObject)obj).getText(); } return obj.toString(); } public Image getImage(Object obj) { if (obj instanceof AbstractTreeObject) { return ((AbstractTreeObject)obj).getImage(); } return null; } } /** * The constructor. */ public Win32ExplorerView() { win32Shell = Shell.create(COM.CLSCTX_INPROC_SERVER|COM.CLSCTX_LOCAL_SERVER); } /** * This is a callback that will allow us * to create the viewer and initialize it. */ public void createPartControl(Composite parent) { viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); drillDownAdapter = new DrillDownAdapter(viewer); viewer.setContentProvider(new ViewContentProvider()); viewer.addSelectionChangedListener(new ISelectionChangedListener() { public void selectionChanged(SelectionChangedEvent event) { enableToolbarActions(); } }); viewer.setLabelProvider(new ViewLabelProvider()); makeActions(); hookContextMenu(); contributeToActionBars(); } private void hookContextMenu() { MenuManager menuMgr = new MenuManager("#PopupMenu"); menuMgr.setRemoveAllWhenShown(true); menuMgr.addMenuListener(new VerbPopupMenu(viewer, drillDownAdapter)); Menu menu = menuMgr.createContextMenu(viewer.getControl()); viewer.getControl().setMenu(menu); getSite().registerContextMenu(menuMgr, viewer); } private void contributeToActionBars() { IActionBars bars = getViewSite().getActionBars(); fillLocalPullDown(bars.getMenuManager()); fillLocalToolBar(bars.getToolBarManager()); } private void fillLocalPullDown(IMenuManager manager) { manager.add(new NamespaceSelectionAction(viewer, "Desktop", 34, win32Shell, ShellSpecialFolderConstants.ssfDESKTOP)); manager.add(new NamespaceSelectionAction(viewer, "All Programs", 36, win32Shell, ShellSpecialFolderConstants.ssfPROGRAMS)); manager.add(new NamespaceSelectionAction(viewer, "Control Panel", 57, win32Shell, ShellSpecialFolderConstants.ssfCONTROLS)); manager.add(new NamespaceSelectionAction(viewer, "Printers", 16, win32Shell, ShellSpecialFolderConstants.ssfPRINTERS)); manager.add(new NamespaceSelectionAction(viewer, "My Documents", 126, win32Shell, ShellSpecialFolderConstants.ssfPERSONAL)); manager.add(new NamespaceSelectionAction(viewer, "Favourites", 208, win32Shell, ShellSpecialFolderConstants.ssfFAVORITES)); manager.add(new NamespaceSelectionAction(viewer, "Recent File List", 213, win32Shell, ShellSpecialFolderConstants.ssfRECENT)); manager.add(new NamespaceSelectionAction(viewer, "Recycle Bin", 145, win32Shell, ShellSpecialFolderConstants.ssfBITBUCKET)); manager.add(new NamespaceSelectionAction(viewer, "My Computer", 17, win32Shell, ShellSpecialFolderConstants.ssfDRIVES)); manager.add(new NamespaceSelectionAction(viewer, "Network Neighborhood", 18, win32Shell, ShellSpecialFolderConstants.ssfNETWORK)); manager.add(new NamespaceSelectionAction(viewer, "Fonts", 38, win32Shell, ShellSpecialFolderConstants.ssfFONTS)); manager.add(new NamespaceSelectionAction(viewer, "History", 146, win32Shell, ShellSpecialFolderConstants.ssfHISTORY)); manager.add(new NamespaceSelectionAction(viewer, "Program Files", 19, win32Shell, ShellSpecialFolderConstants.ssfPROGRAMFILES)); manager.add(new NamespaceSelectionAction(viewer, "My Pictures", 127, win32Shell, ShellSpecialFolderConstants.ssfMYPICTURES)); } private void fillLocalToolBar(IToolBarManager manager) { for (int i=0; i<toolbarActions.length; i++ ) { manager.add(toolbarActions[i]); } manager.add(new Separator()); drillDownAdapter.addNavigationActions(manager); } private void enableToolbarActions() { for (int i=0; i<toolbarActions.length; i++ ) { toolbarActions[i].reset(); } ISelection selection = viewer.getSelection(); Object obj = ((IStructuredSelection)selection).getFirstElement(); if (obj instanceof AbstractTreeObject) { FolderItem folderItem = ((AbstractTreeObject)obj).getFolderItem(); FolderItemVerbs verbs = folderItem.verbs(); if (verbs != null) { int verbCount = verbs.getCount(); for (int i=0; i<verbCount; i++ ) { FolderItemVerb verb = verbs.item(new Variant(i)); String name = verb.getName(); for (int x=0; x<toolbarActions.length; x++ ) { toolbarActions[x].setFolderItemVerb(name, verb); } } } } for (int i=0; i<toolbarActions.length; i++ ) { toolbarActions[i].determineEnabled(); } } private void makeActions() { toolbarActions = new VerbToolbarAction[6]; toolbarActions[0] = new VerbToolbarAction("&Open", 4); toolbarActions[1] = new VerbToolbarAction("&Print", 16); toolbarActions[2] = new VerbToolbarAction("Cu&t", ISharedImages.IMG_TOOL_CUT); toolbarActions[3] = new VerbToolbarAction("&Copy", ISharedImages.IMG_TOOL_COPY); toolbarActions[4] = new VerbToolbarAction("&Delete", ISharedImages.IMG_TOOL_DELETE); toolbarActions[5] = new VerbToolbarAction("P&roperties", 227); } /** * Passing the focus request to the viewer's control. */ public void setFocus() { viewer.getControl().setFocus(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -