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: import javax.swing.*;
046: import javax.swing.tree.TreePath;
047:
048: import org.openide.explorer.view.Visualizer;
049: import org.openide.nodes.*;
050: import org.openide.util.NbBundle;
051:
052: import com.sun.collablet.*;
053: import org.netbeans.modules.collab.core.Debug;
054: import org.openide.util.RequestProcessor;
055:
056: /**
057: *
058: *
059: * @author Todd Fast, todd.fast@sun.com
060: */
061: public class SessionsNodeChildren extends Children.Keys implements
062: /*NodeListener,*/PropertyChangeListener {
063: ////////////////////////////////////////////////////////////////////////////
064: // Instance fields
065: ////////////////////////////////////////////////////////////////////////////
066: private Collection keys;
067: private CollabExplorerPanel explorerPanel;
068:
069: /**
070: *
071: *
072: */
073: public SessionsNodeChildren(RootNode rootNode,
074: CollabExplorerPanel explorerPanel) {
075: super ();
076: this .explorerPanel = explorerPanel;
077: }
078:
079: /**
080: *
081: *
082: */
083: protected void addNotify() {
084: refreshCollabManagerListener();
085: refreshChildren();
086: }
087:
088: /**
089: *
090: *
091: */
092: protected void removeNotify() {
093: _setKeys(Collections.EMPTY_SET);
094:
095: if (CollabManager.getDefault() != null) {
096: CollabManager.getDefault().removePropertyChangeListener(
097: this );
098: }
099: }
100:
101: /**
102: * This method is part of an attempted workaround for bug 5071137:
103: * force a refresh of the listener relationship between the node and
104: * the collab manager.
105: *
106: */
107: protected void refreshCollabManagerListener() {
108: if (CollabManager.getDefault() != null) {
109: CollabManager.getDefault().removePropertyChangeListener(
110: this );
111: CollabManager.getDefault().addPropertyChangeListener(this );
112: } else {
113: Debug.debugNotify(new Exception("CollabManager was null; "
114: + "node cannot listen for sessions"));
115: }
116: }
117:
118: /**
119: *
120: *
121: */
122: protected Node[] createNodes(Object key) {
123: Node[] result = null;
124:
125: try {
126: if (key instanceof Node) {
127: result = new Node[] { (Node) key };
128: } else {
129: result = new Node[] { new SessionNode(
130: (CollabSession) key) };
131: }
132: } catch (Exception e) {
133: Debug.debugNotify(e);
134: }
135:
136: return result;
137: }
138:
139: /**
140: *
141: *
142: */
143: public Collection getKeys() {
144: return keys;
145: }
146:
147: /**
148: *
149: *
150: */
151: public void _setKeys(Collection value) {
152: keys = value;
153: super .setKeys(value);
154: }
155:
156: /**
157: *
158: *
159: */
160: public void refreshChildren() {
161: List keys = new ArrayList();
162:
163: try {
164: // TODO: Sort contacts
165: CollabSession[] sessions = (CollabManager.getDefault() != null) ? CollabManager
166: .getDefault().getSessions()
167: : new CollabSession[0];
168:
169: if (sessions.length == 0) {
170: keys.add(new MessageNode(NbBundle.getMessage(
171: SessionsNodeChildren.class,
172: "LBL_SessionsNodeChildren_NoSessions")));
173: } else {
174: Account defaultAccount = CollabManager.getDefault()
175: .getUserInterface().getDefaultAccount();
176:
177: Arrays.sort(sessions, new SessionsComparator(
178: defaultAccount));
179: keys.addAll(Arrays.asList(sessions));
180: }
181:
182: _setKeys(keys);
183: expandChildren();
184: } catch (Exception e) {
185: Debug.errorManager.notify(e);
186: }
187: }
188:
189: /**
190: *
191: *
192: */
193: public void propertyChange(PropertyChangeEvent event) {
194: if (event.getSource() instanceof CollabManager) {
195: if (CollabManager.PROP_SESSIONS.equals(event
196: .getPropertyName())) {
197: refreshChildren();
198: }
199: }
200: }
201:
202: private void expandChildren() {
203: Runnable r = new Runnable() {
204: Node[] nodes;
205:
206: public void run() {
207: if (nodes == null) {
208: nodes = getNodes(true);
209: SwingUtilities.invokeLater(this );
210: } else {
211: JTree tree = explorerPanel.getTreeViewJTree();
212: for (int i = 0; i < nodes.length; i++) {
213: // Expand all its children
214: for (int j = 0; j < tree.getRowCount(); j++) {
215: TreePath path = tree.getPathForRow(j);
216: if (Visualizer.findNode(path.getPath()[1]) == nodes[i]) {
217: tree.expandPath(path);
218: }
219: }
220: }
221: }
222: }
223: };
224: RequestProcessor.getDefault().post(r);
225: }
226:
227: ////////////////////////////////////////////////////////////////////////////
228: // Inner class
229: ////////////////////////////////////////////////////////////////////////////
230:
231: /**
232: *
233: *
234: */
235: protected static class SessionsComparator extends Object implements
236: Comparator {
237: private Account defaultAccount;
238:
239: /**
240: *
241: *
242: */
243: public SessionsComparator(Account defaultAccount) {
244: super ();
245: this .defaultAccount = defaultAccount;
246: }
247:
248: /**
249: *
250: *
251: */
252: public int compare(Object o1, Object o2) {
253: if (o1 == o2) {
254: return 0;
255: }
256:
257: if (o1 == null) {
258: return -1;
259: }
260:
261: if (o2 == null) {
262: return 1;
263: }
264:
265: Account a1 = ((CollabSession) o1).getAccount();
266: Account a2 = ((CollabSession) o2).getAccount();
267:
268: // Unilaterally sort the default account first
269: if (a1 == defaultAccount) {
270: return -1;
271: }
272:
273: if (a2 == defaultAccount) {
274: return 1;
275: }
276:
277: String s1 = a1.getDisplayName();
278:
279: if (s1 == null) {
280: s1 = "";
281: }
282:
283: String s2 = a2.getDisplayName();
284:
285: if (s2 == null) {
286: s2 = "";
287: }
288:
289: return s1.compareTo(s2);
290: }
291: }
292: }
|