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: * Portions Copyrighted 2007 Sun Microsystems, Inc.
027: */
028: package org.netbeans.modules.editor.gsfret;
029:
030: import javax.swing.SwingUtilities;
031: import org.netbeans.modules.gsf.api.EditRegions;
032: import java.util.Set;
033: import javax.swing.JEditorPane;
034: import javax.swing.text.BadLocationException;
035: import javax.swing.text.Document;
036: import org.netbeans.modules.gsf.api.OffsetRange;
037: import org.netbeans.modules.gsf.Language;
038: import org.netbeans.modules.gsf.LanguageRegistry;
039: import org.openide.cookies.EditorCookie;
040: import org.openide.filesystems.FileObject;
041: import org.openide.loaders.DataObject;
042: import org.openide.loaders.DataObjectNotFoundException;
043: import org.openide.util.Exceptions;
044:
045: /**
046: * Provide access to document synch editing (until this is a core editing API)
047: *
048: * @author Tor Norbye
049: */
050: public class EditRegionsImpl extends EditRegions {
051:
052: public void edit(final FileObject fo,
053: final Set<OffsetRange> regions, final int caretOffset)
054: throws BadLocationException {
055:
056: // This can only be called on the SwingUtilities thread
057: if (!SwingUtilities.isEventDispatchThread()) {
058: SwingUtilities.invokeLater(new Runnable() {
059: public void run() {
060: try {
061: edit(fo, regions, caretOffset);
062: } catch (BadLocationException ble) {
063: Exceptions.printStackTrace(ble);
064: }
065: }
066: });
067:
068: return;
069: }
070:
071: // Update caret listener
072: DataObject dobj;
073:
074: try {
075: dobj = DataObject.find(fo);
076: } catch (DataObjectNotFoundException ex) {
077: return;
078: }
079:
080: EditorCookie editorCookie = dobj.getCookie(EditorCookie.class);
081:
082: if (editorCookie == null) {
083: return;
084: }
085:
086: JEditorPane[] panes = editorCookie.getOpenedPanes();
087:
088: if ((panes == null) || (panes.length == 0)) {
089: return;
090: }
091:
092: JEditorPane pane = panes[0];
093:
094: Document doc = pane.getDocument();
095:
096: Language language = LanguageRegistry.getInstance()
097: .getLanguageByMimeType(fo.getMIMEType());
098: if (language == null) {
099: return;
100: }
101:
102: if ((regions != null) && (regions.size() > 0)) {
103: InstantRenamePerformer.performInstantRename(pane, regions,
104: caretOffset);
105: }
106: }
107: }
|