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: package org.netbeans.modules.bpel.editors.api.utils;
020:
021: import java.util.HashMap;
022: import java.util.List;
023: import java.util.Map;
024: import java.util.regex.Matcher;
025: import java.util.regex.Pattern;
026: import javax.swing.SwingUtilities;
027: import javax.swing.text.StyledDocument;
028: import org.netbeans.core.api.multiview.MultiViewHandler;
029: import org.netbeans.core.api.multiview.MultiViewPerspective;
030: import org.netbeans.core.api.multiview.MultiViews;
031: import org.netbeans.modules.xml.xam.ui.highlight.Highlight;
032: import org.netbeans.modules.xml.xam.ui.highlight.HighlightGroup;
033: import org.netbeans.modules.xml.xam.ui.highlight.HighlightManager;
034: import org.netbeans.modules.xml.schema.ui.basic.SchemaColumnsView;
035: import org.netbeans.modules.xml.schema.ui.basic.SchemaTreeView;
036: import org.netbeans.modules.xml.validation.ShowCookie;
037: import org.netbeans.modules.xml.xam.spi.Validator.ResultItem;
038: import org.netbeans.modules.bpel.editors.api.BpelEditorConstants;
039: import org.netbeans.modules.bpel.editors.api.Constants.VariableStereotype;
040: import org.netbeans.modules.bpel.editors.api.nodes.FactoryAccess;
041: import org.netbeans.modules.bpel.editors.api.nodes.NodeType;
042: import org.netbeans.modules.bpel.model.api.AbstractVariableDeclaration;
043: import org.netbeans.modules.bpel.model.api.Assign;
044: import org.netbeans.modules.bpel.model.api.BooleanExpr;
045: import org.netbeans.modules.bpel.model.api.BpelEntity;
046: import org.netbeans.modules.bpel.model.api.Catch;
047: import org.netbeans.modules.bpel.model.api.CompensatableActivityHolder;
048: import org.netbeans.modules.bpel.model.api.Compensate;
049: import org.netbeans.modules.bpel.model.api.CompensateScope;
050: import org.netbeans.modules.bpel.model.api.CompensationHandler;
051: import org.netbeans.modules.bpel.model.api.CompletionCondition;
052: import org.netbeans.modules.bpel.model.api.CompositeActivity;
053: import org.netbeans.modules.bpel.model.api.Copy;
054: import org.netbeans.modules.bpel.model.api.Correlation;
055: import org.netbeans.modules.bpel.model.api.CorrelationSet;
056: import org.netbeans.modules.bpel.model.api.CorrelationSetContainer;
057: import org.netbeans.modules.bpel.model.api.Else;
058: import org.netbeans.modules.bpel.model.api.ElseIf;
059: import org.netbeans.modules.bpel.model.api.Empty;
060: import org.netbeans.modules.bpel.model.api.EventHandlers;
061: import org.netbeans.modules.bpel.model.api.Exit;
062: import org.netbeans.modules.bpel.model.api.FaultHandlers;
063: import org.netbeans.modules.bpel.model.api.Flow;
064: import org.netbeans.modules.bpel.model.api.ForEach;
065: import org.netbeans.modules.bpel.model.api.From;
066: import org.netbeans.modules.bpel.model.api.FromPart;
067: import org.netbeans.modules.bpel.model.api.If;
068: import org.netbeans.modules.bpel.model.api.Import;
069: import org.netbeans.modules.bpel.model.api.Invoke;
070: import org.netbeans.modules.bpel.model.api.MessageExchange;
071: import org.netbeans.modules.bpel.model.api.MessageExchangeContainer;
072: import org.netbeans.modules.bpel.model.api.OnAlarmEvent;
073: import org.netbeans.modules.bpel.model.api.OnAlarmPick;
074: import org.netbeans.modules.bpel.model.api.OnEvent;
075: import org.netbeans.modules.bpel.model.api.OnMessage;
076: import org.netbeans.modules.bpel.model.api.Process;
077: import org.netbeans.modules.bpel.model.api.PartnerLink;
078: import org.netbeans.modules.bpel.model.api.PatternedCorrelation;
079: import org.netbeans.modules.bpel.model.api.Pick;
080: import org.netbeans.modules.bpel.model.api.Receive;
081: import org.netbeans.modules.bpel.model.api.RepeatUntil;
082: import org.netbeans.modules.bpel.model.api.Reply;
083: import org.netbeans.modules.bpel.model.api.Scope;
084: import org.netbeans.modules.bpel.model.api.Sequence;
085: import org.netbeans.modules.bpel.model.api.TerminationHandler;
086: import org.netbeans.modules.bpel.model.api.Throw;
087: import org.netbeans.modules.bpel.model.api.To;
088: import org.netbeans.modules.bpel.model.api.ToPart;
089: import org.netbeans.modules.bpel.model.api.Variable;
090: import org.netbeans.modules.bpel.model.api.VariableContainer;
091: import org.netbeans.modules.bpel.model.api.Wait;
092: import org.netbeans.modules.bpel.model.api.While;
093: import org.netbeans.modules.bpel.model.api.BpelModel;
094: import org.netbeans.modules.bpel.model.api.ReThrow;
095: import org.netbeans.modules.bpel.model.api.references.SchemaReference;
096: import org.netbeans.modules.bpel.model.api.references.WSDLReference;
097: import org.netbeans.modules.xml.schema.model.GlobalElement;
098: import org.netbeans.modules.xml.schema.model.GlobalType;
099: import org.netbeans.modules.xml.xam.Component;
100: import org.netbeans.modules.xml.xam.Model;
101: import org.netbeans.modules.xml.xam.ModelSource;
102: import org.netbeans.modules.xml.xam.Reference;
103: import org.netbeans.modules.xml.xam.Referenceable;
104: import org.netbeans.modules.xml.xam.dom.DocumentComponent;
105: import org.openide.ErrorManager;
106: import org.openide.cookies.EditCookie;
107: import org.openide.cookies.LineCookie;
108: import org.openide.filesystems.FileObject;
109: import org.openide.loaders.DataObject;
110: import org.openide.loaders.DataObjectNotFoundException;
111: import org.openide.nodes.Node;
112: import org.openide.text.CloneableEditorSupport;
113: import org.openide.text.Line;
114: import org.openide.text.NbDocument;
115: import org.openide.util.Lookup;
116: import org.openide.windows.TopComponent;
117: import org.openide.windows.WindowManager;
118: import org.netbeans.modules.xml.schema.model.SchemaComponent;
119: import org.netbeans.modules.xml.schema.model.SchemaModel;
120: import org.netbeans.modules.xml.wsdl.model.Message;
121: import org.netbeans.modules.xml.wsdl.model.Part;
122: import org.netbeans.modules.xml.wsdl.model.WSDLModel;
123: import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
124: import org.w3c.dom.Element;
125:
126: /**
127: *
128: * @author vb160295
129: * @version 1.0
130: */
131: public final class Util {
132:
133: private Util() {
134: }
135:
136: private static Map<Class<? extends Component>, NodeType> ENTITY_NODETYPE_MAP;
137:
138: static {
139: ENTITY_NODETYPE_MAP = new HashMap<Class<? extends Component>, NodeType>();
140:
141: ENTITY_NODETYPE_MAP.put(Assign.class, NodeType.ASSIGN);
142:
143: ENTITY_NODETYPE_MAP.put(BooleanExpr.class,
144: NodeType.BOOLEAN_EXPR);
145:
146: ENTITY_NODETYPE_MAP.put(Catch.class, NodeType.CATCH);
147: ENTITY_NODETYPE_MAP.put(CompensatableActivityHolder.class,
148: NodeType.CATCH_ALL);
149: ENTITY_NODETYPE_MAP.put(CompensationHandler.class,
150: NodeType.COMPENSATION_HANDLER);
151: ENTITY_NODETYPE_MAP.put(Compensate.class, NodeType.COMPENSATE);
152: ENTITY_NODETYPE_MAP.put(CompensateScope.class,
153: NodeType.COMPENSATE_SCOPE);
154: ENTITY_NODETYPE_MAP.put(CompletionCondition.class,
155: NodeType.COMPLETION_CONDITION);
156: ENTITY_NODETYPE_MAP.put(Copy.class, NodeType.COPY);
157: ENTITY_NODETYPE_MAP
158: .put(Correlation.class, NodeType.CORRELATION);
159: ENTITY_NODETYPE_MAP.put(CorrelationSet.class,
160: NodeType.CORRELATION_SET);
161: ENTITY_NODETYPE_MAP.put(CorrelationSetContainer.class,
162: NodeType.CORRELATION_SET_CONTAINER);
163:
164: ENTITY_NODETYPE_MAP.put(Else.class, NodeType.ELSE);
165: ENTITY_NODETYPE_MAP.put(ElseIf.class, NodeType.ELSE_IF);
166: ENTITY_NODETYPE_MAP.put(Empty.class, NodeType.EMPTY);
167: ENTITY_NODETYPE_MAP.put(EventHandlers.class,
168: NodeType.EVENT_HANDLERS);
169: ENTITY_NODETYPE_MAP.put(Exit.class, NodeType.EXIT);
170:
171: ENTITY_NODETYPE_MAP.put(FaultHandlers.class,
172: NodeType.FAULT_HANDLERS);
173: ENTITY_NODETYPE_MAP.put(Flow.class, NodeType.FLOW);
174: ENTITY_NODETYPE_MAP.put(ForEach.class, NodeType.FOR_EACH);
175: ENTITY_NODETYPE_MAP.put(From.class, NodeType.FROM);
176: ENTITY_NODETYPE_MAP.put(FromPart.class, NodeType.FROM_PART);
177:
178: ENTITY_NODETYPE_MAP.put(If.class, NodeType.IF);
179: ENTITY_NODETYPE_MAP.put(Import.class, NodeType.IMPORT);
180: ENTITY_NODETYPE_MAP.put(Invoke.class, NodeType.INVOKE);
181:
182: ENTITY_NODETYPE_MAP.put(MessageExchange.class,
183: NodeType.MESSAGE_EXCHANGE);
184: ENTITY_NODETYPE_MAP.put(MessageExchangeContainer.class,
185: NodeType.MESSAGE_EXCHANGE_CONTAINER);
186:
187: ENTITY_NODETYPE_MAP.put(OnAlarmEvent.class,
188: NodeType.ALARM_EVENT_HANDLER);
189: ENTITY_NODETYPE_MAP.put(OnAlarmPick.class,
190: NodeType.ALARM_HANDLER);
191: ENTITY_NODETYPE_MAP.put(OnEvent.class, NodeType.ON_EVENT);
192: ENTITY_NODETYPE_MAP.put(OnMessage.class,
193: NodeType.MESSAGE_HANDLER);
194:
195: ENTITY_NODETYPE_MAP.put(PartnerLink.class,
196: NodeType.PARTNER_LINK);
197: ENTITY_NODETYPE_MAP.put(PatternedCorrelation.class,
198: NodeType.CORRELATION_P);
199: ENTITY_NODETYPE_MAP.put(Pick.class, NodeType.PICK);
200: ENTITY_NODETYPE_MAP.put(Process.class, NodeType.PROCESS);
201:
202: ENTITY_NODETYPE_MAP.put(Receive.class, NodeType.RECEIVE);
203: ENTITY_NODETYPE_MAP.put(RepeatUntil.class,
204: NodeType.REPEAT_UNTIL);
205: ENTITY_NODETYPE_MAP.put(Reply.class, NodeType.REPLY);
206:
207: ENTITY_NODETYPE_MAP.put(Scope.class, NodeType.SCOPE);
208: ENTITY_NODETYPE_MAP.put(Sequence.class, NodeType.SEQUENCE);
209:
210: ENTITY_NODETYPE_MAP.put(TerminationHandler.class,
211: NodeType.TERMINATION_HANDLER);
212: ENTITY_NODETYPE_MAP.put(Throw.class, NodeType.THROW);
213: ENTITY_NODETYPE_MAP.put(ReThrow.class, NodeType.RETHROW);
214: ENTITY_NODETYPE_MAP.put(To.class, NodeType.TO);
215: ENTITY_NODETYPE_MAP.put(ToPart.class, NodeType.TO_PART);
216:
217: ENTITY_NODETYPE_MAP.put(Wait.class, NodeType.WAIT);
218: ENTITY_NODETYPE_MAP.put(While.class, NodeType.WHILE);
219:
220: ENTITY_NODETYPE_MAP.put(Variable.class, NodeType.VARIABLE);
221: ENTITY_NODETYPE_MAP.put(VariableContainer.class,
222: NodeType.VARIABLE_CONTAINER);
223: }
224:
225: /**
226: *
227: * @param entityClass class which represents primary entity interface
228: * @return generally used NodeType
229: */
230: public static NodeType getBasicNodeType(
231: Class<? extends Component> entityClass) {
232: NodeType entityType = ENTITY_NODETYPE_MAP.get(entityClass);
233: return entityType == null ? NodeType.UNKNOWN_TYPE : entityType;
234: }
235:
236: public static boolean isNavigatorShowableNodeType(NodeType nodeType) {
237: if (nodeType == null) {
238: return false;
239: }
240: boolean isShowable = true;
241: switch (nodeType) {
242: case UNKNOWN_TYPE:
243: case DEFAULT_BPEL_ENTITY_NODE:
244: case FROM:
245: case TO:
246: case BOOLEAN_EXPR:
247: isShowable = false;
248: break;
249: default:
250: isShowable = true;
251: }
252:
253: return isShowable;
254: }
255:
256: /**
257: *
258: * @param entity
259: * @param lookup
260: * @return the closest node
261: */
262: public static Node getClosestNavigatorNode(BpelEntity entity,
263: Lookup lookup) {
264: if (entity == null || entity.getElementType() == null) {
265: return null;
266: }
267: Node basicNode = null;
268: BpelEntity curEntity = entity;
269: NodeType basicNodeType = getBasicNodeType(entity);
270:
271: while (!isNavigatorShowableNodeType(basicNodeType)
272:
273: && curEntity != null) {
274: curEntity = curEntity.getParent();
275: basicNodeType = getBasicNodeType(curEntity);
276: }
277:
278: if (curEntity != null
279: && !(NodeType.UNKNOWN_TYPE.equals(basicNodeType))) {
280: basicNode = FactoryAccess.getPropertyNodeFactory()
281: .createNode(basicNodeType, curEntity, lookup);
282: }
283: return basicNode;
284: }
285:
286: // vlv
287: public static Component getRoot(Model model) {
288: if (model instanceof BpelModel) {
289: return ((BpelModel) model).getProcess();
290: }
291: if (model instanceof SchemaModel) {
292: return ((SchemaModel) model).getSchema();
293: }
294: if (model instanceof WSDLModel) {
295: return ((WSDLModel) model).getDefinitions();
296: }
297: return null;
298: }
299:
300: /**
301: * This method don't aware about bpelModel lock
302: * @param component Component
303: * @return generally used NodeType, if entity or
304: * enity#getElementType is null then return null
305: */
306: public static NodeType getBasicNodeType(Component component) {
307: if (!(component instanceof BpelEntity)) {
308: // todo m
309: return null;
310: }
311: BpelEntity bpelEntity = (BpelEntity) component;
312:
313: if (bpelEntity == null || bpelEntity.getElementType() == null) {
314: return null;
315: }
316: return getBasicNodeType(bpelEntity.getElementType());
317: }
318:
319: public static void goToSource(Component component) {
320: if (!(component instanceof DocumentComponent)) {
321: return;
322: }
323: DocumentComponent document = (DocumentComponent) component;
324: FileObject fo = getFileObjectByModel(component.getModel());
325:
326: if (fo == null) {
327: return;
328: }
329: try {
330: DataObject d = DataObject.find(fo);
331: LineCookie lc = d.getCookie(LineCookie.class);
332: if (lc == null) {
333: return;
334: }
335: int lineNum = getLineNum(document);
336: if (lineNum < 0) {
337: return;
338: }
339:
340: final Line l = lc.getLineSet().getCurrent(lineNum);
341: final int column = getColumnNum(document);
342: if (column < 0) {
343: return;
344: }
345:
346: javax.swing.SwingUtilities.invokeLater(new Runnable() {
347: public void run() {
348: l.show(Line.SHOW_GOTO, column);
349: openActiveSourceEditor();
350: }
351: });
352: } catch (DataObjectNotFoundException ex) {
353: ex.printStackTrace();
354: ErrorManager.getDefault().notify(ex);
355: }
356: }
357:
358: public static void goToLoggingAlerting(Component component) {
359: assert component instanceof BpelEntity;
360:
361: final BpelEntity bpelEntity = (BpelEntity) component;
362: FileObject fo = getFileObjectByModel(bpelEntity.getBpelModel());
363:
364: if (fo == null) {
365: return;
366: }
367: try {
368: DataObject d = DataObject.find(fo);
369: final Lookup lookup = d.getLookup();
370:
371: final EditCookie ec = d.getCookie(EditCookie.class);
372: if (ec == null) {
373: return;
374: }
375:
376: javax.swing.SwingUtilities.invokeLater(new Runnable() {
377: public void run() {
378: ec.edit();
379: openActiveLoggingEditor();
380: }
381: });
382: } catch (DataObjectNotFoundException ex) {
383: ErrorManager.getDefault().notify(ex);
384: }
385: }
386:
387: public static void goToBusinessRules(Component component) {
388: assert component instanceof BpelEntity;
389:
390: final BpelEntity bpelEntity = (BpelEntity) component;
391: FileObject fo = getFileObjectByModel(bpelEntity.getBpelModel());
392:
393: if (fo == null) {
394: return;
395: }
396: try {
397: DataObject d = DataObject.find(fo);
398: final Lookup lookup = d.getLookup();
399:
400: final EditCookie ec = d.getCookie(EditCookie.class);
401: if (ec == null) {
402: return;
403: }
404:
405: javax.swing.SwingUtilities.invokeLater(new Runnable() {
406: public void run() {
407: ec.edit();
408: openActiveMapperEditor();
409: if (lookup != null || bpelEntity != null) {
410: NodeType nodeType = getBasicNodeType(bpelEntity);
411: if (nodeType == null
412: || NodeType.UNKNOWN_TYPE
413: .equals(nodeType)) {
414: return;
415: }
416: Node bpelNode = FactoryAccess
417: .getPropertyNodeFactory().createNode(
418: nodeType, bpelEntity, lookup);
419: //TODO m
420: TopComponent mapperTc = WindowManager
421: .getDefault().getRegistry()
422: .getActivated();
423: if (mapperTc != null) {
424: mapperTc.setActivatedNodes(new Node[0]);
425: mapperTc
426: .setActivatedNodes(new Node[] { bpelNode });
427: }
428: }
429: }
430: });
431: } catch (DataObjectNotFoundException ex) {
432: ErrorManager.getDefault().notify(ex);
433: }
434: }
435:
436: public static void goToDesign(Component component) {
437: goToDesign(component, null, null);
438: }
439:
440: public static void goToDesign(final Component component,
441: Object cookie, Object view) {
442: // vlv
443: if (!(component instanceof BpelEntity)) {
444: HighlightManager manager = HighlightManager.getDefault();
445: List<HighlightGroup> groups = manager
446: .getHighlightGroups(HighlightGroup.SEARCH);
447:
448: if (groups != null) {
449: for (HighlightGroup group : groups) {
450: manager.removeHighlightGroup(group);
451: }
452: }
453: HighlightGroup group = new HighlightGroup(
454: HighlightGroup.SEARCH);
455: Highlight highlight = new Highlight(component,
456: Highlight.SEARCH_RESULT);
457: group.addHighlight(highlight);
458: manager.addHighlightGroup(group);
459:
460: if (view instanceof SchemaTreeView
461: && component instanceof SchemaComponent) {
462: ((SchemaTreeView) view)
463: .showComponent((SchemaComponent) component);
464: return;
465: }
466: if (view instanceof SchemaColumnsView
467: && component instanceof SchemaComponent) {
468: ((SchemaColumnsView) view)
469: .showComponent((SchemaComponent) component);
470: return;
471: }
472: if (cookie instanceof ShowCookie) {
473: ((ShowCookie) cookie).show(new ResultItem(null, null,
474: component, null));
475: return;
476: }
477: return;
478: }
479: final BpelEntity bpelEntity = (BpelEntity) component;
480: FileObject fo = getFileObjectByModel(bpelEntity.getBpelModel());
481:
482: if (fo == null) {
483: return;
484: }
485: try {
486: DataObject d = DataObject.find(fo);
487: final Lookup lookup = d != null ? d.getLookup() : null;
488:
489: final EditCookie ec = d.getCookie(EditCookie.class);
490: if (ec == null) {
491: return;
492: }
493:
494: javax.swing.SwingUtilities.invokeLater(new Runnable() {
495: public void run() {
496: ec.edit();
497: openActiveDesignEditor();
498: if (lookup != null || bpelEntity != null) {
499: NodeType nodeType = getBasicNodeType(bpelEntity);
500: if (nodeType == null) {
501: return;
502: } else if (NodeType.UNKNOWN_TYPE
503: .equals(nodeType)) {
504: nodeType = NodeType.DEFAULT_BPEL_ENTITY_NODE;
505: }
506: Node bpelNode = FactoryAccess
507: .getPropertyNodeFactory().createNode(
508: nodeType, bpelEntity, lookup);
509: //TODO m
510: TopComponent designTc = WindowManager
511: .getDefault().getRegistry()
512: .getActivated();
513: if (designTc != null) {
514: designTc.setActivatedNodes(new Node[0]);
515: designTc
516: .setActivatedNodes(new Node[] { bpelNode });
517: }
518: }
519: }
520: });
521: } catch (DataObjectNotFoundException ex) {
522: ErrorManager.getDefault().notify(ex);
523: }
524: }
525:
526: public static FileObject getFileObjectByModel(Model model) {
527: if (model != null) {
528:
529: ModelSource src = model.getModelSource();
530: if (src != null) {
531:
532: Lookup lookup = src.getLookup();
533: if (lookup != null) {
534: return lookup.lookup(FileObject.class);
535: }
536: }
537: }
538: return null;
539: }
540:
541: // TODO r|m
542: public static String getTextForBpelEntity(final Component comp) {
543: BpelEntity entity = null;
544: if (comp instanceof BpelEntity) {
545: entity = BpelEntity.class.cast(comp);
546: }
547: if (entity == null) {
548: return ""; // NOI18N
549: }
550:
551: FileObject fo = getFileObjectByModel(entity.getBpelModel());
552: if (fo == null) {
553: return ""; // NOI18N
554: }
555:
556: // TODO - if the line doesn't contain the target (query component name) string, keep searcing subsequent lines
557:
558: DataObject dobj = null;
559: try {
560: dobj = DataObject.find(fo);
561: } catch (DataObjectNotFoundException ex) {
562: ErrorManager.getDefault().notify(ex);
563: }
564:
565: int line = getLineNum(entity);
566: int col = getColumnNum(entity);
567: ModelSource modelSource = entity.getBpelModel()
568: .getModelSource();
569: assert modelSource != null;
570: Lookup lookup = modelSource.getLookup();
571:
572: StyledDocument document = lookup.lookup(StyledDocument.class);
573: if (document == null) {
574: return ""; // NOI18N
575: }
576:
577: CloneableEditorSupport editor = (CloneableEditorSupport) dobj
578: .getCookie(org.openide.cookies.EditorCookie.class);
579: Line.Set s = editor.getLineSet();
580:
581: Line xmlLine = s.getCurrent(line);
582: String nodeLabel = xmlLine.getText().trim();
583: // substitute xml angle brackets <> for < and >
584: Pattern lt = Pattern.compile("<"); //NOI18N
585: Matcher mlt = lt.matcher(nodeLabel);
586: nodeLabel = mlt.replaceAll("<"); //NOI18N
587: Pattern gt = Pattern.compile(">"); //NOI18N
588: Matcher mgt = gt.matcher(nodeLabel);
589: nodeLabel = mgt.replaceAll(">"); //NOI18N
590: return boldenRefOrType(nodeLabel);
591: }
592:
593: // private methods
594: private static int getLineNum(DocumentComponent entity) {
595: int position = entity.findPosition();
596: ModelSource modelSource = entity.getModel().getModelSource();
597: assert modelSource != null;
598: Lookup lookup = modelSource.getLookup();
599:
600: StyledDocument document = lookup.lookup(StyledDocument.class);
601: if (document == null) {
602: return -1;
603: }
604: return NbDocument.findLineNumber(document, position);
605: }
606:
607: private static int getColumnNum(DocumentComponent entity) {
608: int position = entity.findPosition();
609: ModelSource modelSource = entity.getModel().getModelSource();
610: assert modelSource != null;
611: Lookup lookup = modelSource.getLookup();
612:
613: StyledDocument document = lookup.lookup(StyledDocument.class);
614: if (document == null) {
615: return -1;
616: }
617: return NbDocument.findLineColumn(document, position);
618: }
619:
620: private static void openActiveDesignEditor() {
621: openActiveMVEditor(BpelEditorConstants.BPEL_DESIGNMV_PREFFERED_ID);
622: }
623:
624: private static void openActiveMapperEditor() {
625: openActiveMVEditor(BpelEditorConstants.BPEL_MAPPERMV_PREFFERED_ID);
626: }
627:
628: private static void openActiveLoggingEditor() {
629: openActiveMVEditor(BpelEditorConstants.BPEL_LOGGINGMV_PREFFERED_ID);
630: }
631:
632: private static void openActiveSourceEditor() {
633: openActiveMVEditor(BpelEditorConstants.BPEL_SOURCEMV_PREFFERED_ID);
634: }
635:
636: private static void openActiveMVEditor(String mvPreferedID) {
637: if (mvPreferedID == null) {
638: return;
639: }
640:
641: TopComponent tc = WindowManager.getDefault().getRegistry()
642: .getActivated();
643:
644: MultiViewHandler mvh = MultiViews.findMultiViewHandler(tc);
645: if (mvh == null) {
646: return;
647: }
648:
649: MultiViewPerspective[] mvps = mvh.getPerspectives();
650: if (mvps != null && mvps.length > 0) {
651: for (MultiViewPerspective mvp : mvps) {
652: if (mvp.preferredID().equals(mvPreferedID)) { // NOI18N
653: mvh.requestVisible(mvp);
654: mvh.requestActive(mvp);
655: }
656: }
657: }
658: }
659:
660: // TODO get xml snippet for line that contains the
661: // query component name
662: /**
663: * If the label contains ref= or type=
664: * the substring containing the named portion of the attribute
665: * will be surrounded with html bold tags
666: * e.g.,
667: * input param <xsd:element ref="comment" minOccurs="0"/>
668: * return <xsd:element ref="<b>comment</b>" minOccurs="0"/>
669: *
670: *
671: */
672: private static String boldenRefOrType(String label) {
673: // find index of type or ref
674: // find 1st occurence of " from index
675: // find 1st occurence of : after ", if any
676: // insert <b>
677: // find closing "
678: // insert </b>
679: int it = label.indexOf(" type"); //NOI18N
680: if (it < 0) {
681: it = label.indexOf(" ref"); //NOI18N
682: }
683: if (it < 0) {
684: // no type or ref found
685: return label;
686: }
687: int iq1 = label.indexOf('"', it);
688: if (iq1 < it) {
689: // no begin quote
690: return label;
691: }
692: int ic = label.indexOf(':', iq1);
693: if (ic < iq1) {
694: // no colon
695: }
696: int iq2 = label.indexOf('"', iq1 + 1);
697: if (iq2 < iq1 || ic > iq2) {
698: // couldn't find closing quote for tag
699: return label;
700: }
701: int ib1 = -1;
702: if (ic > -1) {
703: ib1 = ic + 1;
704: } else {
705: ib1 = iq1 + 1;
706: }
707: StringBuffer l = new StringBuffer(label);
708: l.insert(ib1, "<b>");
709: // the close quote has now been pushed right 3 spaces
710: l.insert(iq2 + 3, "</b>");
711: return l.toString();
712:
713: }
714:
715: public static void goToReferenceSource(
716: Reference<Referenceable> reference) {
717: Referenceable referenceable = reference.get();
718: if (referenceable == null)
719: return;
720: if (!(referenceable instanceof DocumentComponent))
721: return;
722: goToDocumentComponentSource((DocumentComponent<DocumentComponent>) referenceable);
723: }
724:
725: public static boolean canGoToDocumentComponentSource(
726: DocumentComponent<DocumentComponent> component) {
727: if (component == null)
728: return false;
729:
730: Model model = component.getModel();
731: if (model == null)
732: return false;
733:
734: ModelSource modelSource = model.getModelSource();
735: if (modelSource == null)
736: return false;
737:
738: Lookup lookup = modelSource.getLookup();
739: if (lookup == null)
740: return false;
741:
742: FileObject fileObject = lookup.lookup(FileObject.class);
743: if (fileObject == null)
744: return false;
745:
746: DataObject dataObject = null;
747: try {
748: dataObject = DataObject.find(fileObject);
749: } catch (DataObjectNotFoundException e) {
750: }
751: if (dataObject == null)
752: return false;
753:
754: LineCookie lineCookie = dataObject.getCookie(LineCookie.class);
755: if (lineCookie == null)
756: return false;
757:
758: Line.Set lineSet = lineCookie.getLineSet();
759: if (lineSet == null)
760: return false;
761:
762: StyledDocument document = lookup.lookup(StyledDocument.class);
763: if (document == null)
764: return false;
765:
766: Line line = null;
767: int column = 0;
768:
769: try {
770: int pos = component.findPosition();
771: line = lineSet.getCurrent(NbDocument.findLineNumber(
772: document, pos));
773: column = NbDocument.findLineColumn(document, pos);
774: } catch (IndexOutOfBoundsException e) {
775: }
776:
777: if (line == null) {
778: try {
779: line = lineCookie.getLineSet().getCurrent(0);
780: } catch (IndexOutOfBoundsException e) {
781: }
782: }
783: if (line == null)
784: return false;
785:
786: return true;
787: }
788:
789: public static void goToDocumentComponentSource(
790: DocumentComponent<DocumentComponent> component) {
791: if (component == null)
792: return;
793:
794: Model model = component.getModel();
795: if (model == null)
796: return;
797:
798: ModelSource modelSource = model.getModelSource();
799: if (modelSource == null)
800: return;
801:
802: Lookup lookup = modelSource.getLookup();
803: if (lookup == null)
804: return;
805:
806: FileObject fileObject = lookup.lookup(FileObject.class);
807: if (fileObject == null)
808: return;
809:
810: DataObject dataObject = null;
811: try {
812: dataObject = DataObject.find(fileObject);
813: } catch (DataObjectNotFoundException e) {
814: }
815: if (dataObject == null)
816: return;
817:
818: LineCookie lineCookie = dataObject.getCookie(LineCookie.class);
819: if (lineCookie == null)
820: return;
821:
822: Line.Set lineSet = lineCookie.getLineSet();
823: if (lineSet == null)
824: return;
825:
826: StyledDocument document = lookup.lookup(StyledDocument.class);
827: if (document == null)
828: return;
829:
830: Line line = null;
831: int column = 0;
832:
833: try {
834: int pos = component.findPosition();
835: line = lineSet.getCurrent(NbDocument.findLineNumber(
836: document, pos));
837: column = NbDocument.findLineColumn(document, pos);
838: } catch (IndexOutOfBoundsException e) {
839: }
840:
841: if (line == null) {
842: try {
843: line = lineCookie.getLineSet().getCurrent(0);
844: } catch (IndexOutOfBoundsException e) {
845: }
846: }
847: if (line == null)
848: return;
849:
850: final Line fLine = line;
851: final int fColumn = column;
852:
853: SwingUtilities.invokeLater(new Runnable() {
854: public void run() {
855: fLine.show(Line.SHOW_GOTO, fColumn);
856: }
857: });
858: }
859:
860: public static int getChildIndex(BpelEntity child,
861: CompositeActivity parent) {
862: assert child != null && parent != null;
863: int childIndex = -1;
864: for (int i = 0; i < parent.sizeOfActivities(); i++) {
865: if (child.equals(parent.getActivity(i))) {
866: childIndex = i;
867: break;
868: }
869: }
870: return childIndex;
871: }
872:
873: public static final String getCorrectedHtmlRenderedString(
874: String htmlString) {
875: if (htmlString == null) {
876: return null;
877: }
878: htmlString = htmlString.replaceAll("&", "&"); // NOI18n
879: htmlString = htmlString.replaceAll(">", ">;"); // NOI18n
880: htmlString = htmlString.replaceAll("<", "<"); // NOI18n
881:
882: htmlString = htmlString.replaceAll("&", "&"); // NOI18n
883: htmlString = htmlString.replaceAll(">", ">"); // NOI18n
884: htmlString = htmlString.replaceAll("<", "<"); // NOI18n
885: return htmlString;
886: }
887:
888: public static String getTagName(DocumentComponent component) {
889: if (component == null) {
890: return null;
891: }
892:
893: Element enEl = component.getPeer();
894: return enEl == null ? null : enEl.getTagName();
895: }
896:
897: public static VariableStereotype getVariableStereotype(
898: AbstractVariableDeclaration var) {
899: if (var == null) {
900: return null;
901: }
902: // if (currentStereotype != null) return currentStereotype;
903: VariableStereotype currentStereotype = null;
904: //
905: SchemaReference<GlobalType> typeRef = var.getType();
906: if (typeRef != null) {
907: GlobalType type = typeRef.get();
908: if (type != null) {
909: currentStereotype = VariableStereotype
910: .recognizeStereotype(type);
911: } else {
912: currentStereotype = VariableStereotype.GLOBAL_TYPE;
913: }
914: } else if (var.getMessageType() != null) {
915: currentStereotype = VariableStereotype.MESSAGE;
916: } else if (var.getElement() != null) {
917: currentStereotype = VariableStereotype.GLOBAL_ELEMENT;
918: } else {
919: currentStereotype = VariableStereotype.MESSAGE;
920: }
921: return currentStereotype;
922: }
923:
924: public static Reference getVariableType(
925: AbstractVariableDeclaration variable) {
926: NamedComponentReference<GlobalElement> elemRef = variable
927: .getElement();
928: if (elemRef != null) {
929: return elemRef;
930: }
931: //
932: NamedComponentReference<GlobalType> typeRef = variable
933: .getType();
934: if (typeRef != null) {
935: return typeRef;
936: }
937: //
938: WSDLReference<Message> msgRef = variable.getMessageType();
939: if (msgRef != null) {
940: return msgRef;
941: }
942: //
943: return null;
944: }
945:
946: public static SchemaComponent getVariableSchemaType(
947: AbstractVariableDeclaration variable) {
948: NamedComponentReference<GlobalElement> elemRef = variable
949: .getElement();
950: if (elemRef != null) {
951: GlobalElement gElem = elemRef.get();
952: if (gElem != null) {
953: return gElem;
954: }
955: }
956: //
957: NamedComponentReference<GlobalType> typeRef = variable
958: .getType();
959: if (typeRef != null) {
960: GlobalType gType = typeRef.get();
961: if (gType != null) {
962: return gType;
963: }
964: }
965: return null;
966: }
967:
968: public static SchemaComponent getPartType(Part part) {
969: NamedComponentReference<GlobalElement> elemRef = part
970: .getElement();
971: if (elemRef != null) {
972: GlobalElement gElem = elemRef.get();
973: if (gElem != null) {
974: return gElem;
975: }
976: }
977: //
978: NamedComponentReference<GlobalType> typeRef = part.getType();
979: if (typeRef != null) {
980: GlobalType gType = typeRef.get();
981: if (gType != null) {
982: return gType;
983: }
984: }
985: //
986: return null;
987: }
988:
989: }
|