alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 *
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 *
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.examples.modules.minicomposer.registryutils.backcompatible;

import org.netbeans.api.registry.Context;
import org.netbeans.api.registry.ObjectRef;
import org.netbeans.examples.modules.minicomposer.registryutils.DefaultContextLook;
import org.openide.actions.PasteAction;
import org.openide.util.Lookup;
import org.openide.util.actions.SystemAction;
import org.openide.util.datatransfer.PasteType;

import javax.swing.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class ObjectRefContextLook extends DefaultContextLook {
    private Class refType;

    public ObjectRefContextLook(String name, Class refType) {
        super("ObjectRefContextLook_" + name, ObjectRef.class);
        this.refType = refType;
    }

    public String getDisplayName(Object o, Lookup l) {
        // XXX I18N?
        return "Choosing " + refType.getName() + "'s";
    }

    public List getChildObjects(Object o, Lookup l) {
        List items = new ArrayList();
        Context ctx = (Context) o;
        Iterator it = ctx.getOrderedNames().iterator();
        while (it.hasNext()) {
            String name = (String) it.next();
            if (name.endsWith("/")) {
                // ignore subfolders
                continue;
            }
            Object obj = ctx.getObject(name, null);
            assert obj != null;
            if (refType.isInstance(obj)) {
                ObjectRef ref = new ObjectRef(ctx, name);
                items.add(ref);
            }
        }
        return items;
    }

    public PasteType[] getPasteTypes(Object o, Transferable t, Lookup l) {
        DataFlavor[] flavors = t.getTransferDataFlavors();
        for (int i = 0; i < flavors.length; i++) {
            DataFlavor f = flavors[i];
            if (f.isMimeTypeEqual(DataFlavor.javaJVMLocalObjectMimeType) &&
                    f.getRepresentationClass().equals(ObjectRef.class)) {
                try {
                    ObjectRef ref = (ObjectRef) t.getTransferData(f);
                    if (ref.isValid() && refType.isInstance(ref.getObject())) {
                        return new PasteType[]{new CopyObjectRefPasteType((Context) o, t, f)};
                    }
                } catch (IOException e) {
                    // ignore
                } catch (UnsupportedFlavorException ex) {
                    // ignore
                }
            }
        }
        return null;
    }

    protected Object cloneBinding(Object object) {
        return null;
    }

    public Action[] getActions(Object o, Lookup l) {
        return new Action[]{
            SystemAction.get(PasteAction.class),
        };
    }


    private class CopyObjectRefPasteType extends PasteType {

        private final Context c;
        private final Transferable t;
        private final DataFlavor f;

        public CopyObjectRefPasteType(Context c, Transferable t, DataFlavor f) {
            this.c = c;
            this.t = t;
            this.f = f;
        }

        public String getName() {
            return "Duplicate"; // XXX I18N?
        }

        private static final String SUFFIX = "-copy-";

        public Transferable paste() throws IOException {
            ObjectRef ref;
            try {
                ref = (ObjectRef) t.getTransferData(f);
            } catch (UnsupportedFlavorException e) {
                throw (IOException) new IOException(e.toString()).initCause(e);
            }
            Object o =  cloneBinding(ref.getObject());
            if (o != null) {
                Collection existingNames = c.getBindingNames();
                int x = 0;
                while (existingNames.contains(ref.getBindingName() + SUFFIX + x)) {
                    x++;
                }
                c.putObject(ref.getBindingName() + SUFFIX + x, o);
            }
            return (o != null) ? t : null;
        }
    }
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.