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: package org.netbeans.modules.visualweb.insync.live;
042:
043: import java.io.PrintWriter;
044:
045: import org.openide.filesystems.FileObject;
046:
047: import org.netbeans.modules.visualweb.insync.ParserAnnotation;
048: import org.netbeans.modules.visualweb.insync.UndoEvent;
049: import org.netbeans.modules.visualweb.insync.Unit;
050: import org.netbeans.modules.visualweb.insync.beans.BeansUnit;
051: import org.netbeans.modules.visualweb.insync.models.FacesModel;
052:
053: /**
054: * This is a class that wrapps a LiveUnit. This wrapper is to allow FacesModel to delay
055: * the resurrect operation on a LiveUnit. Since it would be very onerous to change
056: * every method in LiveUnit to ensure a resurrect had occured prior to computing there
057: * result, I create a wrapper class which holds the actual LiveUnit, and attempts to
058: * delay the requirement of the resurect for as long as possible.
059: * We lazy initialize liveUnit due to issue with possible circular sync issue
060: * Image that a new ModelSet is instantiated. This model set will have sync all of its
061: * units on creation. The process of syncing used to created the LiveUnit right then
062: * and there. Well, creating a LiveUnit ends up create LiveProperty's that have their
063: * value initialized. At design time, there is a DesignTimeVariableResolver which turns
064: * around and does a sync on the models its will attempt to resolve with.
065: * Danger Will Robinson, by definition one of those models is the one in process of
066: * syncing !!! I have also added a check in Model.sync() to make sure we detect
067: * these circular sync issues in the future.
068: *
069: * TODO
070: * More work needs to be done to make this class thread safe, however there are a number
071: * of issues with thread safety ness already within insync, that we can tackle this problem
072: * then in a more general fashion.
073: *
074: * @author eric
075: *
076: */
077: public class LiveUnitWrapper implements Unit {
078: protected FacesModel model;
079: protected BeansUnit sourceUnit;
080: protected FileObject file;
081: protected LiveUnit liveUnit;
082:
083: public LiveUnitWrapper(FacesModel model, BeansUnit sourceUnit,
084: FileObject file) {
085: this .model = model;
086: this .sourceUnit = sourceUnit;
087: this .file = file;
088: }
089:
090: public void destroy() {
091: if (liveUnit != null) {
092: liveUnit.destroy();
093: liveUnit = null;
094: }
095: liveUnit = null;
096: }
097:
098: public void dumpTo(PrintWriter w) {
099: if (liveUnit == null)
100: return;
101: liveUnit.dumpTo(w);
102: }
103:
104: protected LiveUnit getCurrentLiveUnit() {
105: return liveUnit;
106: }
107:
108: public ParserAnnotation[] getErrors() {
109: return sourceUnit.getErrors();
110: }
111:
112: public LiveUnit getLiveUnit() {
113: if (liveUnit == null) {
114: liveUnit = new LiveUnit(model, sourceUnit, file);
115: liveUnit.sync();
116: }
117: return liveUnit;
118: }
119:
120: public State getState() {
121: return sourceUnit.getState();
122: }
123:
124: public boolean isLiveUnitInstantiated() {
125: return liveUnit != null;
126: }
127:
128: public boolean isWriteLocked() {
129: if (liveUnit == null)
130: return sourceUnit.isWriteLocked();
131: return liveUnit.isWriteLocked();
132: }
133:
134: public void readLock() {
135: if (liveUnit == null) {
136: sourceUnit.readLock();
137: return;
138: }
139: liveUnit.readLock();
140: }
141:
142: public void readUnlock() {
143: if (liveUnit == null) {
144: sourceUnit.readUnlock();
145: return;
146: }
147: liveUnit.readUnlock();
148: }
149:
150: public boolean sync() {
151: boolean synced;
152: if (liveUnit == null)
153: synced = sourceUnit.sync();
154: else
155: synced = liveUnit.sync();
156: return synced;
157: }
158:
159: public void writeLock(UndoEvent event) {
160: if (liveUnit == null) {
161: sourceUnit.writeLock(event);
162: return;
163: }
164: liveUnit.writeLock(event);
165: }
166:
167: public boolean writeUnlock(UndoEvent event) {
168: if (liveUnit == null)
169: return sourceUnit.writeUnlock(event);
170: return liveUnit.writeUnlock(event);
171: }
172:
173: }
|