过滤RCP(富客户端)透视图

来源:互联网 发布:宽带网络 编辑:程序博客网 时间:2024/05/19 20:22
 

RCP的访问控制在应用中非常的有必要,因为大多数的应用都是多用户多极别。

客户端至少需要在三个层次上进行控制:

一是SWT组件级别,包括按钮、快捷菜单等等。

二是视图级别的控制,这个比较容易,只要在透视图中控制视图就可以了。

三是透视图的控制。

在透视图中包含视图。可以通过Open perspective对话框选择打开透视图,但是这里有这样一个问题,Open perspective对话框中显示的是RCP系统中所有的透视图,而且无法进行配置。在工具栏上可以显示透视图工具栏,非常的方便于用户的使用,一般都需要显示出来。但是这个工具栏带有一个other按钮,打开就是Open perspective对话框。有一个方法是从透视图注册器中删除不可访问的透视图,但是这样会弹出一个错误提示框,据说式Eclipse老的版本的一个虫子。或者Eclipse开发者就是这样设定的,不允许二次开发者操作内部方法。其方法如下:

1    /** *//**
 2     * 在RCP程序的ApplicationWorkbenchWindowAdvisor类的postWindowCreate()方法中调用,用于删除无权限操作的Perspective
 3     * @param pr
 4     */

 5    public static void perspectiveFiltration(PerspectiveRegistry pr) {
 6        IPerspectiveDescriptor[] _ipd = pr.getPerspectives();
 7        Object [] _objs = new Object[_ipd.length];
 8        for(int i=0; i<_ipd.length; i++{
 9            if(!isSecurityPerspective(_ipd[i].getId())) {
10                _objs[i] = _ipd[i];
11            }

12        }

13        log.debug("从已注册的Perspective列表中删除Perspectives为: " + _objs.toString());
14        pr.removeExtension(null, _objs);
15        log.debug("需要重新注册新的默认Perspective页");
16        setDefautlPerspectice(pr);
17    }

 尝试了很多方法,最后决定到Eclipse的源码中查一查,看看能不能找到行得通的方法。终于找到了一种过滤透视图的方法,修改的代码不多,几行的样子。

在org.eclipse.ui.workbench_3.2.2.M20070119-0800包中加入了一个类FunctionFilter:

package org.eclipse.ui.internal;

import org.eclipse.ui.PlatformUI;

/**
 *
 * @author Administrator
 *
 */
public class FunctionFilter {

 public final static String FILETERED_PERSPECTIVES = "FILETERED_PERSPECTIVES";

 public static Object[] fileteredPerpectives() {
  return (Object[]) PlatformUI.getWorkbench().getDisplay().getData(
    FILETERED_PERSPECTIVES);
 }
}

你可能已经注意到,使用了Display保存过滤了的透视图。

然后在eclipse内部类PerspContentProvider修改了几行代码:

/*******************************************************************************
 * Copyright (c) 2000, 2005 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.ui.internal.dialogs;

import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.ui.IPerspectiveRegistry;
import org.eclipse.ui.internal.FunctionFilter;

public class PerspContentProvider implements IStructuredContentProvider {

    /**
     * Create a new PerspContentProvider.
     */
    public PerspContentProvider() {
        //no-op
    }

    /*
     * (non-Javadoc)
     *
     * @see org.eclipse.jface.viewers.IContentProvider#dispose()
     */
    public void dispose() {
        //no-op
    }

    /*
     * (non-Javadoc)
     *
     * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
     */
    public Object[] getElements(Object element) {
        if (element instanceof IPerspectiveRegistry) {
         
            // return ((IPerspectiveRegistry) element).getPerspectives();
         
         return FunctionFilter.fileteredPerpectives();
        }
        return null;
    }

    /*
     * (non-Javadoc)
     *
     * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer,
     *      java.lang.Object, java.lang.Object)
     */
    public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
        //no-op
    }
}

这样就过滤了不需要的透视图。

原创粉丝点击