KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > registry > ShadowBinding


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.registry;
21
22 import org.netbeans.core.registry.ContextImpl;
23 import org.netbeans.core.registry.ObjectBinding;
24 import org.netbeans.spi.registry.BasicContext;
25 import org.netbeans.spi.registry.SpiUtils;
26 import org.openide.filesystems.FileObject;
27
28 import java.io.IOException JavaDoc;
29
30 /**
31  * Provides ObjectBinding.Reader for ObjectBinding implementation.
32  * (copy & pasted & refactored from original org.netbeans.core.registry.ObjectBinding)
33  */

34 final class ShadowBinding {
35     private static final String JavaDoc SHADOW_EXTENSION = "shadow";//NOI18N
36
static final ObjectBinding.Reader READER = new ObjectBinding.Reader() {
37         boolean canRead(FileObject fo) {
38             return fo.getExt().equalsIgnoreCase(SHADOW_EXTENSION);
39         }
40
41         String JavaDoc getFileExtension() {
42             return SHADOW_EXTENSION;
43         }
44
45         ObjectBinding getObjectBinding(BasicContext ctx, FileObject fo) {
46             return new ObjectBindingImpl((ContextImpl) ctx, fo);
47         }
48     };
49
50     private ShadowBinding() {
51     }
52
53     private static class ObjectBindingImpl extends ObjectBinding {
54         private ContextImpl context;
55
56         protected ObjectBindingImpl(ContextImpl context, FileObject fo) {
57             super(fo);
58             this.context = context;
59         }
60
61         public Object JavaDoc createInstance() throws IOException JavaDoc {
62             // try to convert shadow file to ObjectRef instance
63
FileObject fo = getFile();
64             String JavaDoc originalFile = (String JavaDoc) fo.getAttribute("originalFile");//NOI18N
65
// if the shadow points for a folder the ObjectRef will be invalid!
66
int i = originalFile.lastIndexOf('/');//NOI18N
67
String JavaDoc contextName = "/" + originalFile.substring(0, i);//NOI18N
68
String JavaDoc bindingName = originalFile.substring(i + 1);
69             i = bindingName.lastIndexOf('.');
70             if (i > 0) {
71                 bindingName = bindingName.substring(0, i);
72             }
73             return SpiUtils.createObjectRef(context.getRootContextImpl(), contextName, bindingName);
74         }
75
76         public boolean isEnabled() {
77             return (getFile() != null && getFile().isValid());
78         }
79     }
80
81 }
82
83
Popular Tags