zoukankan      html  css  js  c++  java
  • the relationship among AdapterFactory, Adapter, Adaptee and Viewer

    let's pick the Treeviewer as the case:

    If you want to show a tree in a dialog, you'd better use the Treeviewer instance.

    But how to fill in treeviewer, Implementing this requires you have to do many steps as follows:

    1、You need a object used to fill in the treeviewer, and this object have a reference to the information showed in the treeviewer.

      here we call this object as Inputobject

    2、Set the InputObject into the treeviewer through invoking the method setInput() :

    treeViewer.setInput(session.getRoot());

    3、when the treeviewer wants to draw its information to the display, it has to know what adapter factory it needs, the factory will give the treeviewer some

    adapter.Doing this requires:(1)We have to tell the platform that  the type of Inputobject need what adapter factory, so  we register the adapter factory and

    the type of Inputobject to the platform:

    Platform.getAdapterManager().registerAdapters(adapterFactory, Inputobject.class);

    Now that  the treeviewer knows the adaptee type (the type of Inputobject) and the adapterfactory, it's the time for the factory to give the treeviewer some

    adapters. when treeviewer encounters Inputobject , the platform will tell it which adapterfactory it needs, and the adapterfactory will tell the treeviewer what  adapter it needs , and the adapter will tell treeviewer how to get the label, children, parent , and the other information, as follows:

    /*******************************************************************************
     * Copyright (c) 2005 Jean-Michel Lemieux, Jeff McAffer 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
     *
     * Hyperbola is an RCP application developed for the book
     *     Eclipse Rich Client Platform - 
     *         Designing, Coding, and Packaging Java Applications
     * See http://eclipsercp.org
     *
     * Contributors:
     *     Jean-Michel Lemieux and Jeff McAffer - initial API and implementation
     *******************************************************************************/
    package org.eclipsercp.hyperbola;
    
    import org.eclipse.core.runtime.IAdapterFactory;
    import org.eclipse.jface.resource.ImageDescriptor;
    import org.eclipse.ui.model.IWorkbenchAdapter;
    import org.eclipse.ui.plugin.AbstractUIPlugin;
    import org.eclipsercp.hyperbola.model.Contact;
    import org.eclipsercp.hyperbola.model.ContactsEntry;
    import org.eclipsercp.hyperbola.model.ContactsGroup;
    import org.eclipsercp.hyperbola.model.Presence;
    
    public class HyperbolaAdapterFactory implements IAdapterFactory {
    
        private IWorkbenchAdapter groupAdapter = new IWorkbenchAdapter() {
            public Object getParent(Object o) {
                return ((ContactsGroup) o).getParent();
            }
            
            public String getLabel(Object o) {
                ContactsGroup group = ((ContactsGroup) o);
                int available = 0;
                Contact[] entries = group.getEntries();
                for (int i = 0; i < entries.length; i++) {
                    Contact contact = entries[i];
                    if (contact instanceof ContactsEntry) {
                        if (((ContactsEntry) contact).getPresence() != Presence.INVISIBLE)
                            available++;
                    }
                }
                return group.getName() + " (" + available + "/" + entries.length
                        + ")";
            }
    
            public ImageDescriptor getImageDescriptor(Object object) {
                return AbstractUIPlugin.imageDescriptorFromPlugin(
                        Application.PLUGIN_ID, IImageKeys.GROUP);
            }
    
            public Object[] getChildren(Object o) {
                return ((ContactsGroup) o).getEntries();
            }
        };
    
        private IWorkbenchAdapter entryAdapter = new IWorkbenchAdapter() {
            public Object getParent(Object o) {
                return ((ContactsEntry) o).getParent();
            }
    
            public String getLabel(Object o) {
                ContactsEntry entry = ((ContactsEntry) o);
                return entry.getNickname() + " (" + entry.getName() + "@"
                        + entry.getServer() + ")";
            }
    
            public ImageDescriptor getImageDescriptor(Object object) {
                ContactsEntry entry = ((ContactsEntry) object);
                String key = presenceToKey(entry.getPresence());
                return AbstractUIPlugin.imageDescriptorFromPlugin(
                        Application.PLUGIN_ID, key);
            }
    
            public Object[] getChildren(Object o) {
                return new Object[0];
            }
        };
    
        private String presenceToKey(Presence presence) {
            if (presence == Presence.ONLINE)
                return IImageKeys.ONLINE;
            if (presence == Presence.AWAY)
                return IImageKeys.AWAY;
            if (presence == Presence.DO_NOT_DISTURB)
                return IImageKeys.DO_NOT_DISTURB;
            if (presence == Presence.INVISIBLE)
                return IImageKeys.OFFLINE;
            return "";
        }
    
        public Object getAdapter(Object adaptableObject, Class adapterType) {
            if (adapterType == IWorkbenchAdapter.class
                    && adaptableObject instanceof ContactsGroup)
                return groupAdapter;
            if (adapterType == IWorkbenchAdapter.class
                    && adaptableObject instanceof ContactsEntry)
                return entryAdapter;
            return null;
        }
    
        public Class[] getAdapterList() {
            return new Class[] { IWorkbenchAdapter.class };
        }
    }

     now the treeviewer has parsed the root element completely, then it will encounter the root's children, and how it deal with the children,

    it will go from the first step again.

  • 相关阅读:
    SSM后台管理开发日志(三)
    文件权限
    adb详细教学
    adb基础命令001
    SQL训练题库002(建议copy到sqlserver里实战练习,多做一下)
    SQL增删改查,列的更改,更改列名表名,运算符连接符,注释
    SQL增加约束
    SQL 建表、删表和数据,增删约束
    The firstday i join in cnblogs..."Hello everyone"...
    C#日期时间格式化
  • 原文地址:https://www.cnblogs.com/silentjesse/p/3110072.html
Copyright © 2011-2022 走看看