001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package threaddemo.data;
043:
044: import java.util.Map;
045: import java.util.WeakHashMap;
046: import org.openide.cookies.SaveCookie;
047: import org.openide.util.Lookup;
048: import org.openide.util.lookup.AbstractLookup;
049: import org.openide.util.lookup.InstanceContent;
050: import threaddemo.locking.RWLock;
051: import threaddemo.model.Phadhail;
052:
053: // XXX this is inefficient - e.g. LookNode.getIcon will force the PhadhailLookup
054: // to be created! PhadhailLook should just ask for any special lookup items, e.g.
055: // the SaveCookie, otherwise return a simple list with the editor support. There
056: // should be a way to listen to any phadhails with one listener.
057:
058: /**
059: * Serves "cookies" for phadhails.
060: * @author Jesse Glick
061: */
062: public class PhadhailLookups {
063:
064: /** no instances */
065: private PhadhailLookups() {
066: }
067:
068: private static final Map<Phadhail, PhadhailLookup> lookups = new WeakHashMap<Phadhail, PhadhailLookup>();
069:
070: // XXX rather than being synch, should be readAccess, and modified/saved should be writeAccess
071: public static synchronized Lookup getLookup(Phadhail ph) {
072: PhadhailLookup l = lookups.get(ph);
073: if (l == null) {
074: l = new PhadhailLookup(ph);
075: lookups.put(ph, l);
076: }
077: return l;
078: }
079:
080: // Access from PhadhailEditorSupport
081: static void modified(Phadhail ph, SaveCookie s) {
082: ((PhadhailLookup) getLookup(ph)).modified(s);
083: }
084:
085: static void saved(Phadhail ph, SaveCookie s) {
086: ((PhadhailLookup) getLookup(ph)).saved(s);
087: }
088:
089: // XXX #32203 would be really helpful here!
090: private static final class PhadhailLookup extends AbstractLookup
091: implements InstanceContent.Convertor<Object, Object> {
092:
093: private static final Object KEY_EDITOR = "editor";
094: private static final Object KEY_DOM_PROVIDER = "domProvider";
095:
096: private final Phadhail ph;
097: // XXX Have to keep the InstanceContent separately; it is a field in AbstractLookup
098: // but we cannot access it!
099: private final InstanceContent c;
100: private PhadhailEditorSupport ed = null;
101:
102: public PhadhailLookup(Phadhail ph) {
103: this (ph, new InstanceContent());
104: }
105:
106: private PhadhailLookup(Phadhail ph, InstanceContent c) {
107: super (c);
108: this .ph = ph;
109: this .c = c;
110: }
111:
112: protected void initialize() {
113: if (!ph.hasChildren()) {
114: c.add(KEY_EDITOR, this );
115: if (ph.getName().endsWith(".xml")) {
116: c.add(KEY_DOM_PROVIDER, this );
117: }
118: }
119: super .initialize();
120: }
121:
122: public void modified(SaveCookie s) {
123: c.add(s);
124: }
125:
126: public void saved(SaveCookie s) {
127: c.remove(s);
128: }
129:
130: private PhadhailEditorSupport getEd() {
131: if (ed == null) {
132: ed = new PhadhailEditorSupport(ph);
133: }
134: return ed;
135: }
136:
137: public Object convert(Object obj) {
138: if (obj == KEY_EDITOR) {
139: return getEd();
140: } else {
141: assert obj == KEY_DOM_PROVIDER;
142: RWLock m = ph.lock(); // XXX may need a different lock...
143: return new DomSupport(ph, getEd(), m);
144: }
145: }
146:
147: public Class<?> type(Object obj) {
148: if (obj == KEY_EDITOR) {
149: return PhadhailEditorSupport.class; // a bunch of interfaces
150: } else {
151: assert obj == KEY_DOM_PROVIDER;
152: return DomProvider.class;
153: }
154: }
155:
156: public String displayName(Object obj) {
157: throw new UnsupportedOperationException();
158: }
159:
160: public String id(Object obj) {
161: return "PhadhailLookup[" + ph + "," + obj + "]";
162: }
163:
164: }
165:
166: }
|