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.collab.ui;
042:
043: import java.beans.*;
044: import java.util.*;
045:
046: import com.sun.collablet.CollabSession;
047: import com.sun.collablet.ContactGroup;
048: import org.openide.nodes.*;
049: import org.openide.util.*;
050:
051: import org.netbeans.modules.collab.core.Debug;
052:
053: /**
054: *
055: *
056: * @author Todd Fast, todd.fast@sun.com
057: */
058: public class ContactsNodeChildren extends Children.Keys implements
059: NodeListener, PropertyChangeListener {
060: /**/
061:
062: ////////////////////////////////////////////////////////////////////////////
063: // Instance variables
064: ////////////////////////////////////////////////////////////////////////////
065: private Collection keys;
066: private CollabSession session;
067:
068: /**
069: *
070: *
071: */
072: public ContactsNodeChildren(CollabSession session) {
073: super ();
074: this .session = session;
075: }
076:
077: /**
078: *
079: *
080: */
081: public CollabSession getCollabSession() {
082: return session;
083: }
084:
085: /**
086: *
087: *
088: */
089: protected void addNotify() {
090: getCollabSession().addPropertyChangeListener(this );
091: refreshChildren();
092: }
093:
094: /**
095: *
096: *
097: */
098: protected void removeNotify() {
099: _setKeys(Collections.EMPTY_SET);
100: getCollabSession().removePropertyChangeListener(this );
101: }
102:
103: /**
104: *
105: *
106: */
107: protected Node[] createNodes(Object key) {
108: Node[] result = null;
109:
110: try {
111: if (key instanceof Node) {
112: result = new Node[] { (Node) key };
113: } else {
114: result = new Node[] { new ContactGroupNode(session,
115: (ContactGroup) key) };
116: }
117: } catch (Exception e) {
118: Debug.debugNotify(e);
119: }
120:
121: return result;
122: }
123:
124: /**
125: *
126: *
127: */
128: public Collection getKeys() {
129: return keys;
130: }
131:
132: /**
133: *
134: *
135: */
136: public void _setKeys(Collection value) {
137: keys = value;
138: super .setKeys(value);
139: }
140:
141: /**
142: *
143: *
144: */
145: public void refreshChildren() {
146: List keys = new ArrayList();
147:
148: try {
149: // Set datasourceNames=new TreeSet(Arrays.asList(
150: // getJatoWebContextCookie().getJDBCDatasourceNames()));
151: // for (Iterator i=datasourceNames.iterator(); i.hasNext(); )
152: // {
153: // nodes.add(getJatoWebContextCookie().getJDBCDatasource(
154: // (String)i.next()));
155: // }
156: // TODO: Sort groups
157: ContactGroup[] contacts = getCollabSession()
158: .getContactGroups();
159:
160: if ((contacts == null) || (contacts.length == 0)) {
161: keys.add(new MessageNode(NbBundle.getMessage(
162: ContactsNodeChildren.class,
163: "LBL_ContactsNodeChildren_NoContacts")));
164: } else {
165: Arrays.sort(contacts, new ContactGroupsComparator());
166: keys.addAll(Arrays.asList(contacts));
167: }
168:
169: _setKeys(keys);
170: } catch (Exception e) {
171: Debug.errorManager.notify(e);
172: }
173: }
174:
175: /**
176: *
177: *
178: */
179: public void propertyChange(PropertyChangeEvent event) {
180: if (event.getSource() instanceof CollabSession) {
181: if (CollabSession.PROP_VALID
182: .equals(event.getPropertyName())) {
183: // TODO: Is there any reason to listen to valid=true?
184: // I don't think there is.
185: if (event.getNewValue().equals(Boolean.FALSE)) {
186: _setKeys(Collections.EMPTY_SET);
187: getCollabSession().removePropertyChangeListener(
188: this );
189: }
190: } else if (CollabSession.PROP_CONTACT_GROUPS.equals(event
191: .getPropertyName())) {
192: refreshChildren();
193: }
194: }
195: }
196:
197: /**
198: *
199: *
200: */
201: public void childrenAdded(NodeMemberEvent ev) {
202: // Ignore
203: }
204:
205: /**
206: *
207: *
208: */
209: public void childrenRemoved(NodeMemberEvent ev) {
210: // Ignore
211: }
212:
213: /**
214: *
215: *
216: */
217: public void childrenReordered(NodeReorderEvent ev) {
218: // Ignore
219: }
220:
221: /**
222: *
223: *
224: */
225: public void nodeDestroyed(NodeEvent ev) {
226: refreshChildren();
227: }
228:
229: ////////////////////////////////////////////////////////////////////////////
230: // Inner class
231: ////////////////////////////////////////////////////////////////////////////
232:
233: /**
234: *
235: *
236: */
237: protected static class ContactGroupsComparator extends Object
238: implements Comparator {
239: /**
240: *
241: *
242: */
243: public ContactGroupsComparator() {
244: super ();
245: }
246:
247: /**
248: *
249: *
250: */
251: public int compare(Object o1, Object o2) {
252: if (o1 == o2) {
253: return 0;
254: }
255:
256: if (o1 == null) {
257: return -1;
258: }
259:
260: if (o2 == null) {
261: return 1;
262: }
263:
264: String s1 = ((ContactGroup) o1).getName();
265: String s2 = ((ContactGroup) o2).getName();
266:
267: if (s1 == null) {
268: s1 = "";
269: }
270:
271: if (s2 == null) {
272: s2 = "";
273: }
274:
275: return s1.compareTo(s2);
276: }
277: }
278: }
|