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-2007 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: * PageContentProviderImpl.java
043: *
044: * Created on April 12, 2007, 9:23 AM
045: *
046: * To change this template, choose Tools | Template Manager
047: * and open the template in the editor.
048: */
049:
050: package org.netbeans.modules.visualweb.navigation;
051:
052: import java.lang.ref.Reference;
053: import java.lang.ref.WeakReference;
054: import java.util.Collections;
055: import java.util.Map;
056: import java.util.Map.Entry;
057: import java.util.Set;
058: import java.util.WeakHashMap;
059: import org.netbeans.modules.visualweb.insync.models.FacesModel;
060: import org.netbeans.modules.visualweb.insync.models.FacesModelSet;
061: import org.netbeans.modules.visualweb.project.jsf.api.JsfProjectUtils;
062: import org.netbeans.modules.web.jsf.navigation.pagecontentmodel.PageContentModel;
063: import org.netbeans.modules.web.jsf.navigation.pagecontentmodel.PageContentModelProvider;
064: import org.openide.filesystems.FileChangeAdapter;
065: import org.openide.filesystems.FileEvent;
066: import org.openide.filesystems.FileObject;
067:
068: /**
069: *
070: * @author joelle
071: */
072: public class VWPContentModelProvider implements
073: PageContentModelProvider {
074:
075: private Map<FileObject, Reference<VWPContentModel>> map = Collections
076: .synchronizedMap(new WeakHashMap<FileObject, Reference<VWPContentModel>>());
077:
078: /** Creates a new instance of PageContentProviderImpl */
079: public VWPContentModelProvider() {
080: // System.out.println("You found me.");
081: }
082:
083: public PageContentModel getPageContentModel(FileObject fileObject) {
084: Reference<VWPContentModel> ref = map.get(fileObject);
085: VWPContentModel model = null;
086: if (ref != null) {
087: model = ref.get();
088: if (model != null) {
089: return model;
090: }
091: }
092: if (JsfProjectUtils.isJsfProjectFile(fileObject)) {
093: FacesModelSet modelset = FacesModelSet
094: .getInstance(fileObject);
095: FacesModel facesModel = null;
096: if (modelset != null) {
097: synchronized (modelset) {
098: facesModel = modelset.getFacesModel(fileObject);
099: if (facesModel == null) {
100: return null;
101: }
102: }
103: } else {
104: return null; //REMINDER
105: }
106: model = new VWPContentModel(this , facesModel);
107: if (model != null) {
108: map.put(fileObject, new WeakReference<VWPContentModel>(
109: model));
110: fileObject
111: .addFileChangeListener(new FileChangeAdapter() {
112:
113: @Override
114: public void fileDeleted(FileEvent fe) {
115: FileObject fileObj = fe.getFile();
116: map.remove(fileObj);
117: super .fileDeleted(fe);
118: }
119: });
120: }
121: }
122: return model;
123: }
124:
125: public void removeModel(VWPContentModel model) {
126: Set<Entry<FileObject, Reference<VWPContentModel>>> entrySet = map
127: .entrySet();
128: for (Entry<FileObject, Reference<VWPContentModel>> entry : entrySet) {
129: if (entry != null && entry.getValue() != null
130: && entry.getValue().get() != null
131: && entry.getValue().get().equals(model)) {
132: map.remove(entry.getKey());
133: break;
134: }
135: }
136: }
137:
138: public FileObject isNewPageContentModel(FileObject fileObject) {
139: FileObject jsp = JsfProjectUtils.getJspForJava(fileObject);
140: if (map.get(jsp) == null) {
141: return jsp;
142: }
143: return null;
144: }
145: }
|