001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.bpel.design.decoration.providers;
021:
022: import java.awt.Color;
023: import java.util.ArrayList;
024: import java.util.List;
025: import javax.swing.SwingUtilities;
026: import org.netbeans.modules.bpel.core.util.BPELValidationController;
027: import org.netbeans.modules.bpel.core.util.BPELValidationListener;
028: import org.netbeans.modules.bpel.core.util.ValidationUtil;
029: import org.netbeans.modules.bpel.design.DesignView;
030: import org.netbeans.modules.bpel.design.decoration.ComponentsDescriptor;
031: import org.netbeans.modules.bpel.design.decoration.Decoration;
032: import org.netbeans.modules.bpel.design.decoration.DecorationProvider;
033: import org.netbeans.modules.bpel.design.decoration.Descriptor;
034: import org.netbeans.modules.bpel.design.decoration.StripeDescriptor;
035: import org.netbeans.modules.bpel.design.decoration.TextstyleDescriptor;
036: import org.netbeans.modules.bpel.design.decoration.components.ShowGlassPaneButton;
037: import org.netbeans.modules.bpel.design.model.DiagramModel;
038: import org.netbeans.modules.bpel.design.model.patterns.Pattern;
039: import org.netbeans.modules.bpel.model.api.BpelEntity;
040: import org.netbeans.modules.bpel.model.api.BpelModel;
041:
042: import org.netbeans.modules.xml.xam.Component;
043: import org.netbeans.modules.xml.xam.spi.Validator.ResultItem;
044: import org.netbeans.modules.xml.xam.spi.Validator.ResultType;
045:
046: /**
047: *
048: * @author aa160298
049: */
050: public class ValidationDecorationProvider extends DecorationProvider
051: implements BPELValidationListener {
052:
053: private Object list_key = new Object();
054: private Object decoration_key = new Object();
055:
056: private List<ResultItem> results = new ArrayList<ResultItem>();
057:
058: /** Creates a new instance of ValidationDecorationProvider */
059: public ValidationDecorationProvider(DesignView designView) {
060: super (designView);
061:
062: final BPELValidationController vc = getDesignView()
063: .getValidationController();
064:
065: vc.addValidationListener(this );
066:
067: SwingUtilities.invokeLater(new Runnable() {
068: public void run() {
069: vc.triggerValidation();
070: }
071: });
072:
073: }
074:
075: public void release() {
076: // Removed validation listener.
077: getDesignView().getValidationController()
078: .removeValidationListener(this );
079:
080: }
081:
082: public Decoration getDecoration(BpelEntity entity) {
083: return (Decoration) entity.getCookie(decoration_key);
084:
085: }
086:
087: public void updateDecorations() {
088:
089: final List<ResultItem> resultsFiltered = ValidationUtil
090: .filterBpelResultItems(results);
091:
092: SwingUtilities.invokeLater(new Runnable() {
093: public void run() {
094: if (getDesignView().getBPELModel().getState() != BpelModel.State.VALID) {
095: return; //ignore notifications while model is in broken state
096: }
097:
098: //group resultitem by entities and put list to temporary cookies
099: final Object new_listkey = new Object();
100: for (ResultItem item : resultsFiltered) {
101: Component component = item.getComponents();
102: BpelEntity entity = getDiagramEntity((BpelEntity) component);
103: if (entity != null) {
104: ArrayList<ResultItem> items = (ArrayList) entity
105: .getCookie(new_listkey);
106: if (items == null) {
107: items = new ArrayList<ResultItem>();
108: entity.setCookie(new_listkey, items);
109: }
110: items.add(item);
111: }
112: }
113:
114: //iterate over all diagram elements and update decoration if result was changed
115:
116: new ModelIterator() {
117: public void visit(BpelEntity entity) {
118: ArrayList<ResultItem> new_results = (ArrayList) entity
119: .getCookie(new_listkey);
120: ArrayList<ResultItem> old_results = (ArrayList) entity
121: .getCookie(list_key);
122: if (new_results == null) {
123: entity.removeCookie(decoration_key);
124: entity.removeCookie(list_key);
125: } else if (!compareLists(old_results,
126: new_results)) {
127: ShowGlassPaneButton showGlassPaneButton = getShowGlassPaneButton(entity);
128: if (showGlassPaneButton == null) {
129: showGlassPaneButton = new ShowGlassPaneButton(
130: new_results);
131: } else {
132: showGlassPaneButton
133: .setResultItems(new_results);
134: }
135:
136: ComponentsDescriptor cd = new ComponentsDescriptor();
137: cd.add(showGlassPaneButton,
138: ComponentsDescriptor.RIGHT_TB);
139:
140: StripeDescriptor sd = StripeDescriptor
141: .createValidation(new_results);
142:
143: Decoration decoration = new Decoration(
144: (sd != null) ? new Descriptor[] {
145: cd, TEXT_STYLE, sd }
146: : new Descriptor[] { cd,
147: TEXT_STYLE });
148:
149: entity
150: .setCookie(decoration_key,
151: decoration);
152: entity.setCookie(list_key, new_results);
153: }
154:
155: }
156: }.run(getDesignView().getProcessModel());
157:
158: //notify DM to update all elements
159: fireDecorationChanged();
160: }
161: });
162:
163: }
164:
165: private ShowGlassPaneButton getShowGlassPaneButton(BpelEntity entity) {
166: Decoration decoration = (Decoration) entity
167: .getCookie(decoration_key);
168: if (decoration == null)
169: return null;
170: ComponentsDescriptor components = decoration.getComponents();
171: if (components == null)
172: return null;
173:
174: for (int i = components.getComponentCount() - 1; i >= 0; i--) {
175: java.awt.Component c = components.getComponent(i);
176: if (c instanceof ShowGlassPaneButton) {
177: return (ShowGlassPaneButton) c;
178: }
179: }
180:
181: return null;
182: }
183:
184: public void validationUpdated(List<ResultItem> results) {
185: this .results = results;
186: updateDecorations();
187: }
188:
189: private abstract class ModelIterator {
190: public abstract void visit(BpelEntity entity);
191:
192: public void run(BpelEntity entity) {
193: visit(entity);
194: for (BpelEntity e : entity.getChildren()) {
195: run(e);
196: }
197: }
198: }
199:
200: private boolean compareLists(ArrayList<ResultItem> list1,
201: ArrayList<ResultItem> list2) {
202:
203: if (list1 == null || list2 == null) {
204: return false;
205: }
206:
207: if (list1.size() != list2.size()) {
208: return false;
209: }
210:
211: for (ResultItem item1 : list1) {
212: boolean found = false;
213: for (ResultItem item2 : list2) {
214: if (ValidationUtil.equals(item1, item2)) {
215: found = true;
216: break;
217: }
218: }
219: if (!found) {
220: return false;
221: }
222: }
223:
224: return true;
225: }
226:
227: private BpelEntity getDiagramEntity(BpelEntity entity) {
228: if (entity == null || entity.getModel() == null) {
229: return null;
230: }
231:
232: DesignView designView = getDesignView();
233: DiagramModel diagramModel = null;
234: while (entity != null) {
235: diagramModel = designView.getModel();
236: if (diagramModel == null) {
237: break;
238: }
239:
240: Pattern p = diagramModel.getPattern(entity);
241:
242: //do not show badges on "invisible elements"
243: //put badges on their parents instead
244: if (p != null && p.isSelectable() && p.isInModel()) {
245: return entity;
246: }
247:
248: entity = entity.getParent();
249:
250: }
251: return null;
252:
253: }
254:
255: Descriptor TEXT_STYLE = new TextstyleDescriptor(new Color(0xff0000));
256: }
|