|
What this is
Other links
The source code
/*
* Sun Public License Notice
*
* The contents of this file are subject to the Sun Public License
* Version 1.0 (the "License"). You may not use this file except in
* compliance with the License. A copy of the License is available at
* http://www.sun.com/
*
* The Original Code is NetBeans. The Initial Developer of the Original
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
* Microsystems, Inc. All Rights Reserved.
*/
package org.netbeans.modules.xml.text.completion;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.LinkedList;
import java.util.Collections;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.Position;
import org.netbeans.api.xml.parsers.DocumentInputSource;
import org.netbeans.modules.xml.core.lib.Convertors;
import org.netbeans.modules.xml.api.model.GrammarEnvironment;
import org.netbeans.modules.xml.api.model.GrammarQuery;
import org.netbeans.modules.xml.api.model.GrammarQueryManager;
import org.netbeans.modules.xml.spi.model.EmptyQuery;
import org.netbeans.modules.xml.text.syntax.SyntaxElement;
import org.netbeans.modules.xml.text.syntax.XMLSyntaxSupport;
import org.netbeans.modules.xml.text.syntax.dom.SyntaxNode;
import org.openide.filesystems.FileObject;
import org.openide.loaders.DataObject;
import org.openide.text.NbDocument;
import org.openide.util.RequestProcessor;
import org.openide.util.Task;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import org.openide.awt.StatusDisplayer;
/**
* Manages grammar to text editor association. It is able to
* dynamically switch among providers.
*
* @author Petr Kuzel
*/
class GrammarManager implements DocumentListener {
// current cache state
private int state = INVALID;
static final int VALID = 1;
static final int INVALID = 3;
// cache entry
private GrammarQuery grammar;
// grammar is provided for this document
private final XMLSyntaxSupport syntax;
private final Document doc;
// guarded positions pairs
private Position[] guarded;
// maximal gurded offset
private Position maxGuarded;
/**
* Create new manager.
*/
public GrammarManager(Document doc, XMLSyntaxSupport syntax) {
this.doc = doc;
this.syntax = syntax;
}
/**
* Return any suitable grammar that you can get
* till expires given timeout.
*/
public synchronized GrammarQuery getGrammar() {
switch (state) {
case VALID:
return grammar;
case INVALID:
loadGrammar();
return grammar;
default:
throw new IllegalStateException();
}
}
/**
* Notification from invalidator thread, the grammar need to be reloaded.
*/
public synchronized void invalidateGrammar() {
// make current loader a zombie
if (state == VALID) {
String msg = Util.THIS.getString("MSG_loading_cancel");
StatusDisplayer.getDefault().setStatusText(msg);
}
doc.removeDocumentListener(this);
guarded = new Position[0];
state = INVALID;
}
public void insertUpdate(DocumentEvent e) {
// !!! handle that adding new SyntaxElement at root
// level may change enableness rule, syntax element
// count should be enough
if (isGuarded(e.getOffset(), e.getLength())) {
invalidateGrammar();
}
}
public void removeUpdate(DocumentEvent e) {
if (isGuarded(e.getOffset(), e.getLength())) {
invalidateGrammar();
}
}
public void changedUpdate(DocumentEvent e) {
// not interested
}
private boolean isGuarded(int offset, int length) {
// optimalization for common case
if (offset > maxGuarded.getOffset()) {
return false;
}
// slow loop matchibng range overlaps
for (int i = 0; i
|
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.