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:
042: package org.apache.jmeter.module.nodes;
043:
044: import java.text.Collator;
045: import java.util.Collection;
046: import java.util.Collections;
047: import java.util.Comparator;
048: import java.util.SortedSet;
049: import java.util.TreeSet;
050: import org.apache.jmeter.module.cookies.JMeterCookie;
051: import org.apache.jmeter.testelement.TestElement;
052: import org.apache.jorphan.collections.HashTree;
053: import org.openide.nodes.Children;
054: import org.openide.nodes.Node;
055:
056: /**
057: *
058: * @author Jaroslav Bachorik
059: */
060: public class JMeterElementChildren extends Children.Keys implements
061: Comparator<TestElement> {
062: private static Collator SORTER = Collator.getInstance();
063:
064: private SortedSet<TestElement> allTestElements;
065: private HashTree nodeTree = null;
066:
067: public JMeterElementChildren(final HashTree tree) {
068: nodeTree = tree;
069: }
070:
071: public void rebind(final HashTree tree) {
072: nodeTree = tree;
073: allTestElements = null;
074: refreshKeys(true);
075: }
076:
077: @Override
078: protected void addNotify() {
079: super .addNotify();
080: refreshKeys(true);
081: }
082:
083: @Override
084: protected void removeNotify() {
085: super .removeNotify();
086: setKeys(Collections.EMPTY_SET);
087: synchronized (this ) {
088: allTestElements = null;
089: }
090: }
091:
092: @Override
093: protected Node[] createNodes(Object key) {
094: TestElement t = (TestElement) key;
095:
096: // return new Node[] {new JMeterElementNode(new JMeterCookie(t, cookie.getPath(), cookie.getScriptSource()))};
097: return new Node[] { new JMeterElementNode(t, nodeTree
098: .getTree(t)) };
099: }
100:
101: public int compare(TestElement t1, TestElement t2) {
102: int x = SORTER.compare(t1.getProperty("TestElement.name")
103: .getStringValue(), t2.getProperty("TestElement.name")
104: .getStringValue());
105: if (x != 0 || t1 == t2) {
106: return x;
107: } else {
108: // #44491: was not displaying overridden targets.
109: return System.identityHashCode(t1)
110: - System.identityHashCode(t2);
111: }
112: }
113:
114: private void refreshKeys(boolean createKeys) {
115: Collection keys = null;
116: synchronized (this ) {
117: if (allTestElements == null && !createKeys) {
118: // Aynch refresh after removeNotify; ignore. (#44428)
119: return;
120: }
121: allTestElements = new TreeSet<TestElement>(this );
122: // try {
123: for (Object elementObj : nodeTree.list()) {
124: allTestElements.add((TestElement) elementObj);
125: }
126: // allTestElements.addAll(JMeterIntegrationEngine.getDefault().getChildren(cookie.getPath(), cookie.getScriptSource()));
127: keys = allTestElements;
128: // } catch (InitializationException e) {
129: // e.printStackTrace();
130: // }
131: }
132: if (keys != null) { // #65235
133: setKeys(keys);
134: }
135: }
136:
137: }
|