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.channel.filesharing.eventlistener;
042:
043: import org.openide.cookies.EditorCookie;
044: import org.openide.filesystems.FileObject;
045: import org.openide.loaders.*;
046: import org.openide.nodes.Node;
047: import org.openide.util.*;
048: import org.openide.windows.TopComponent;
049:
050: import java.beans.*;
051:
052: import java.util.*;
053:
054: import org.netbeans.modules.collab.channel.filesharing.FilesharingCollablet;
055: import org.netbeans.modules.collab.channel.filesharing.FilesharingContext;
056: import org.netbeans.modules.collab.channel.filesharing.filehandler.CollabFileHandler;
057:
058: /**
059: */
060: public class DocumentTabMarker implements PropertyChangeListener {
061: private Set components = new HashSet();
062: private boolean inUpdate = false;
063:
064: public void DocumentTabMarker() {
065: }
066:
067: public void installListeners() {
068: TopComponent.Registry reg = TopComponent.getRegistry();
069: reg.addPropertyChangeListener(this );
070: updateListening(reg.getOpened());
071: }
072:
073: private void updateListening(Set newSet) {
074: Set added = new HashSet(newSet);
075: added.removeAll(components);
076:
077: Set removed = components;
078: components.removeAll(newSet);
079:
080: components = new HashSet(newSet);
081:
082: // stop listening on closed TCs
083: for (Iterator it = removed.iterator(); it.hasNext();) {
084: TopComponent tc = (TopComponent) it.next();
085: tc.removePropertyChangeListener(this );
086: }
087:
088: // start listening on
089: for (Iterator it = added.iterator(); it.hasNext();) {
090: TopComponent tc = (TopComponent) it.next();
091:
092: if (accept(tc)) {
093: updateDisplayName(tc, null);
094: tc.addPropertyChangeListener(this );
095: }
096: }
097: }
098:
099: private boolean accept(TopComponent tc) {
100: // filter only interresting TCs
101: return true;
102: }
103:
104: private void updateDisplayName(TopComponent tc, String propName) {
105: if (inUpdate) {
106: return;
107: }
108:
109: inUpdate = true;
110:
111: try {
112: String orig = tc.getHtmlDisplayName();
113: Node[] nodes = tc.getActivatedNodes();
114: boolean displayMark = false;
115:
116: if (nodes != null) {
117: for (int i = 0; i < nodes.length; i++) {
118: DataObject dragDataObject = (DataObject) nodes[i]
119: .getCookie(DataObject.class);
120:
121: if (dragDataObject != null) {
122: FileObject fileObject = dragDataObject
123: .getPrimaryFile();
124:
125: try {
126: if (FilesharingCollablet
127: .isShared(fileObject)) {
128: displayMark = true;
129:
130: FilesharingContext context = FilesharingCollablet
131: .getContext(fileObject);
132:
133: //also add document listener
134: if (context != null) {
135: if (!context
136: .isReadOnlyConversation()) {
137: //register document Listener
138: CollabFileHandler fh = context
139: .getSharedFileGroupManager()
140: .getFileHandler(
141: fileObject);
142:
143: if (fh != null) {
144: fh
145: .registerDocumentListener();
146: }
147: } else {
148: EditorCookie cookie = (EditorCookie) dragDataObject
149: .getCookie(EditorCookie.class);
150:
151: if ((cookie != null)
152: && (cookie
153: .getOpenedPanes() != null)) {
154: cookie.getOpenedPanes()[0]
155: .setEditable(false);
156: }
157: }
158: }
159: }
160: } catch (Exception ex) {
161: //ignore
162: }
163: }
164: }
165: }
166:
167: if (orig == null)
168: orig = tc.getDisplayName();
169: if (orig == null)
170: orig = tc.getName();
171:
172: if (orig == null)
173: return; // through finally
174:
175: if (orig.startsWith("<html>")) {
176: return;
177: }
178:
179: if (displayMark) {
180: tc.setHtmlDisplayName(NbBundle.getMessage(
181: DocumentTabMarker.class, "FMT_Mark", orig));
182: }
183: } finally {
184: inUpdate = false;
185: }
186: }
187:
188: public void propertyChange(PropertyChangeEvent pce) {
189: TopComponent.Registry reg = TopComponent.getRegistry();
190: Object source = pce.getSource();
191: String name = pce.getPropertyName();
192:
193: if (reg == source) {
194: if (TopComponent.Registry.PROP_OPENED.equals(name)) {
195: updateListening(reg.getOpened());
196: }
197: } else if (source instanceof TopComponent) {
198: if ("displayName".equals(name)
199: || "htmlDisplayName".equals(name)) {
200: updateDisplayName((TopComponent) source, name);
201: }
202: }
203: }
204: }
|