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: package org.netbeans.modules.bpel.core.util;
042:
043: import java.util.ArrayList;
044: import java.util.HashMap;
045: import java.util.LinkedList;
046: import java.util.List;
047: import java.util.Map;
048: import java.util.Timer;
049: import java.util.TimerTask;
050: import java.util.WeakHashMap;
051:
052: import org.openide.text.Line;
053: import org.netbeans.modules.xml.xam.Model.State;
054: import org.netbeans.modules.xml.xam.spi.Validation;
055: import org.netbeans.modules.xml.xam.spi.Validation.ValidationType;
056: import org.netbeans.modules.xml.xam.spi.Validator.ResultItem;
057: import org.netbeans.modules.xml.xam.spi.Validator.ResultType;
058:
059: import org.netbeans.modules.bpel.model.api.BpelModel;
060: import org.netbeans.modules.bpel.model.api.events.ChangeEvent;
061: import org.netbeans.modules.bpel.model.api.events.ChangeEventListenerAdapter;
062: import org.netbeans.modules.bpel.model.api.support.Util;
063: import static org.netbeans.modules.soa.ui.util.UI.*;
064:
065: /**
066: * @author Vladimir Yaroslavskiy
067: * @version 2008.01.17
068: */
069: public class BPELValidationController extends
070: ChangeEventListenerAdapter {
071:
072: public BPELValidationController(BpelModel bpelModel) {
073: myTimer = new Timer();
074: myBpelModel = bpelModel;
075: myWeaklisteners = new WeakHashMap<BPELValidationListener, Object>();
076: myTrigger = new ExternalModelsValidationTrigger(this );
077: myAnnotations = new ArrayList<BPELValidationAnnotation>();
078: myValidationResult = new ArrayList<ResultItem>();
079: }
080:
081: public void attach() {
082: if (myBpelModel != null) {
083: myBpelModel.addEntityChangeListener(this );
084: myBpelModel.addEntityChangeListener(getTrigger());
085: getTrigger().loadImports();
086: }
087: }
088:
089: public void detach() {
090: if (myBpelModel != null) {
091: myBpelModel.removeEntityChangeListener(this );
092: myBpelModel.removeEntityChangeListener(getTrigger());
093: getTrigger().clearTrigger();
094: }
095: }
096:
097: public void addValidationListener(BPELValidationListener listener) {
098: synchronized (myWeaklisteners) {
099: myWeaklisteners.put(listener, null);
100: }
101: }
102:
103: public void removeValidationListener(BPELValidationListener listener) {
104: synchronized (myWeaklisteners) {
105: myWeaklisteners.remove(listener);
106: }
107: }
108:
109: BpelModel getModel() {
110: return myBpelModel;
111: }
112:
113: private void modelChanged(ChangeEvent event) {
114: if (event.isLastInAtomic()) {
115: startValidation();
116: }
117: }
118:
119: public void triggerValidation() {
120: triggerValidation(false);
121: }
122:
123: public void triggerValidation(boolean checkExternallyTriggered) {
124: if (checkExternallyTriggered && getTrigger().isTriggerDirty()) {
125: startValidation();
126: } else if (!checkExternallyTriggered) {
127: startValidation();
128: }
129: }
130:
131: @Override
132: public void notifyEvent(ChangeEvent changeEvent) {
133: if (!State.VALID.equals(myBpelModel.getState())) {
134: return;
135: }
136: modelChanged(changeEvent);
137: }
138:
139: public void notifyCompleteValidationResults(List<ResultItem> result) {
140: notifyListeners(result);
141: }
142:
143: private synchronized void startValidation() {
144: TimerTask task = new TimerTask() {
145: public void run() {
146: Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
147: Validation validation = new Validation();
148: log();
149: log("START VALIDATION"); // NOI18N
150: startTimeln();
151: validation
152: .validate(myBpelModel, ValidationType.PARTIAL);
153: endTime("FAST VALIDATION"); // NOI18N
154: log("END VALIDATION"); // NOI18N
155:
156: List<ResultItem> items = validation
157: .getValidationResult();
158: myValidationResult = new ArrayList<ResultItem>();
159:
160: synchronized (items) {
161: for (ResultItem item : items) {
162: myValidationResult.add(item);
163: }
164: }
165: notifyListeners(myValidationResult);
166: }
167: };
168: log();
169: log("TIMER"); // NOI18N
170: myTimer.cancel();
171: myTimer = new Timer();
172: myTimer.schedule(task, DELAY);
173: }
174:
175: private void notifyListeners(List<ResultItem> result) {
176: synchronized (myWeaklisteners) {
177: for (BPELValidationListener listener : myWeaklisteners
178: .keySet()) {
179: if (listener != null) {
180: listener.validationUpdated(result);
181: }
182: }
183: }
184: showAnnotationsInEditor(result);
185: }
186:
187: private void showAnnotationsInEditor(List<ResultItem> result) {
188: synchronized (myAnnotations) {
189: for (BPELValidationAnnotation annotation : myAnnotations) {
190: annotation.detach();
191: }
192: myAnnotations.clear();
193: //out();
194: //out("SHOW ANNOTATION IN EDITOR");
195:
196: // First we need to group the results by line. We need this to add only
197: // one annotation per line
198: Map<Line, List<ResultItem>> map = new HashMap<Line, List<ResultItem>>();
199: for (ResultItem item : result) {
200: final Line line = Util.getLine(item);
201: List<ResultItem> list = map.get(line);
202:
203: if (list == null) {
204: list = new LinkedList<ResultItem>();
205: map.put(line, list);
206: }
207: list.add(item);
208: }
209: for (Line line : map.keySet()) {
210: final StringBuilder description = new StringBuilder();
211: final List<ResultItem> list = map.get(line);
212:
213: for (int i = 0; i < list.size(); i++) {
214: description.append(list.get(i).getDescription());
215:
216: if (i < list.size() - 1) {
217: description.append("\n\n"); // NOI18N
218: }
219: }
220: BPELValidationAnnotation annotation = new BPELValidationAnnotation();
221: myAnnotations.add(annotation);
222: annotation.show(line, description.toString());
223: }
224: }
225: }
226:
227: private ExternalModelsValidationTrigger getTrigger() {
228: return myTrigger;
229: }
230:
231: public List<ResultItem> getValidationResult() {
232: return myValidationResult;
233: }
234:
235: private Timer myTimer;
236: private BpelModel myBpelModel;
237: private List<ResultItem> myValidationResult;
238: private ExternalModelsValidationTrigger myTrigger;
239: private List<BPELValidationAnnotation> myAnnotations;
240: private Map<BPELValidationListener, Object> myWeaklisteners;
241:
242: // vlv
243: private static final int DELAY = 5432;
244: }
|