001: package net.sourceforge.squirrel_sql.plugins.syntax.netbeans;
002:
003: import net.sourceforge.squirrel_sql.client.session.ISession;
004: import net.sourceforge.squirrel_sql.client.session.SQLTokenListener;
005: import net.sourceforge.squirrel_sql.fw.id.IIdentifier;
006: import org.netbeans.editor.LocaleSupport;
007: import org.netbeans.editor.StatusBar;
008: import org.netbeans.editor.Syntax;
009:
010: import javax.swing.text.Document;
011: import java.util.ArrayList;
012: import java.util.Hashtable;
013: import java.util.ResourceBundle;
014: import java.util.Vector;
015:
016: /**
017: * This class is used in the SQLKit.createSyntax() method.
018: * In this method the right SQLSyntax object initialized
019: * with the right session must be returned because
020: * usually different sessions have different key words, tables, ...
021: *
022: * The only key we are given in SQLKit.createSyntax() is the document
023: * of the JEditorPane of the session. This class provides the mapping
024: * of document to session. This mapping is initialized through the
025: * calls of putEditorPane() and putDocument() in the constructor of
026: * NetbeansSQLEditorPane.
027: */
028: public class SyntaxFactory {
029: private Hashtable<Document, DocumentAssignedObjects> _documentAssignedObjectsByDocument = new Hashtable<Document, DocumentAssignedObjects>();
030: private Hashtable<IIdentifier, ArrayList<NetbeansSQLEditorPane>> _editorPanesBySessionID = new Hashtable<IIdentifier, ArrayList<NetbeansSQLEditorPane>>();
031: private Hashtable<IIdentifier, Vector<SQLSyntax>> _syntaxesBySessionID = new Hashtable<IIdentifier, Vector<SQLSyntax>>();
032: private Hashtable<IIdentifier, Vector<SQLTokenListener>> _sqlTokenListenersBySession = new Hashtable<IIdentifier, Vector<SQLTokenListener>>();
033: private Hashtable<IIdentifier, ArrayList<Document>> _documentsBySessionID = new Hashtable<IIdentifier, ArrayList<Document>>();
034:
035: public SyntaxFactory() {
036:
037: final ResourceBundle bundle = ResourceBundle
038: .getBundle("org.netbeans.editor.Bundle");
039:
040: LocaleSupport.addLocalizer(new LocaleSupport.Localizer() {
041: public String getString(String key) {
042: if (StatusBar.INSERT_LOCALE.equals(key)) {
043: return "INS";
044: } else if (StatusBar.OVERWRITE_LOCALE.equals(key)) {
045: return "OVR";
046: } else {
047: return bundle.getString(key);
048: }
049: }
050: });
051: }
052:
053: public Syntax getSyntax(Document doc) {
054: DocumentAssignedObjects docAssigendObjs = _documentAssignedObjectsByDocument
055: .get(doc);
056:
057: if (null == docAssigendObjs) {
058: // Once and again the Netbeans editor calls createSyntax() after
059: // sessionEnding() was called. Then sess is null and the code below
060: // would break.
061: return new Syntax();
062: }
063:
064: ArrayList<NetbeansSQLEditorPane> editors = _editorPanesBySessionID
065: .get(docAssigendObjs.getSession().getIdentifier());
066:
067: NetbeansSQLEditorPane editorMatchingDocument = null;
068: for (NetbeansSQLEditorPane editor : editors) {
069: if (doc.equals(editor.getDocument())) {
070: editorMatchingDocument = editor;
071: break;
072: }
073: }
074:
075: if (null == editorMatchingDocument) {
076: throw new IllegalStateException(
077: "No Editor matching document found");
078: }
079:
080: SQLSyntax syntax = new SQLSyntax(docAssigendObjs.getSession(),
081: editorMatchingDocument, docAssigendObjs.getProperties());
082:
083: Vector<SQLTokenListener> tokenListeners = _sqlTokenListenersBySession
084: .get(docAssigendObjs.getSession().getIdentifier());
085:
086: if (null != tokenListeners) {
087: for (int i = 0; i < tokenListeners.size(); i++) {
088: SQLTokenListener tl = tokenListeners.elementAt(i);
089: syntax.addSQLTokenListener(tl);
090: }
091: }
092:
093: Vector<SQLSyntax> syntaxes = _syntaxesBySessionID
094: .get(docAssigendObjs.getSession().getIdentifier());
095: if (null == syntaxes) {
096: syntaxes = new Vector<SQLSyntax>();
097: _syntaxesBySessionID.put(docAssigendObjs.getSession()
098: .getIdentifier(), syntaxes);
099: }
100: syntaxes.add(syntax);
101:
102: return syntax;
103:
104: }
105:
106: public void addSQLTokenListeners(ISession sess, SQLTokenListener tl) {
107: Vector<SQLTokenListener> tokenListeners = _sqlTokenListenersBySession
108: .get(sess.getIdentifier());
109:
110: if (null == tokenListeners) {
111: tokenListeners = new Vector<SQLTokenListener>();
112: _sqlTokenListenersBySession.put(sess.getIdentifier(),
113: tokenListeners);
114: }
115: tokenListeners.add(tl);
116:
117: Vector<SQLSyntax> syntaxes = _syntaxesBySessionID.get(sess
118: .getIdentifier());
119:
120: if (null != syntaxes) {
121: for (int i = 0; i < syntaxes.size(); i++) {
122: SQLSyntax syntax = syntaxes.elementAt(i);
123: syntax.addSQLTokenListener(tl);
124: }
125: }
126: }
127:
128: public void removeSQLTokenListeners(ISession sess,
129: SQLTokenListener tl) {
130: Vector<SQLTokenListener> tokenListeners = _sqlTokenListenersBySession
131: .get(sess.getIdentifier());
132:
133: if (null == tokenListeners) {
134: return;
135: }
136:
137: tokenListeners.remove(tl);
138:
139: Vector<SQLSyntax> syntaxes = _syntaxesBySessionID.get(sess
140: .getIdentifier());
141:
142: for (int i = 0; i < syntaxes.size(); i++) {
143: SQLSyntax syntax = syntaxes.elementAt(i);
144: syntax.removeSQLTokenListener(tl);
145: }
146: }
147:
148: public void putEditorPane(ISession sess,
149: NetbeansSQLEditorPane editor) {
150: ArrayList<NetbeansSQLEditorPane> buf = _editorPanesBySessionID
151: .get(sess.getIdentifier());
152:
153: if (null == buf) {
154: buf = new ArrayList<NetbeansSQLEditorPane>();
155: _editorPanesBySessionID.put(sess.getIdentifier(), buf);
156:
157: }
158:
159: buf.remove(editor);
160: buf.add(editor);
161: }
162:
163: public void putDocument(ISession session,
164: NetbeansPropertiesWrapper wrp, Document document) {
165: _documentAssignedObjectsByDocument.put(document,
166: new DocumentAssignedObjects(session, wrp));
167:
168: ArrayList<Document> docs = _documentsBySessionID.get(session
169: .getIdentifier());
170:
171: if (null == docs) {
172: docs = new ArrayList<Document>();
173: _documentsBySessionID.put(session.getIdentifier(), docs);
174: }
175: docs.add(document);
176: }
177:
178: public void sessionEnding(ISession sess) {
179: ArrayList<Document> docs = _documentsBySessionID.remove(sess
180: .getIdentifier());
181:
182: if (null != docs) {
183: for (Document doc : docs) {
184: _documentAssignedObjectsByDocument.remove(doc);
185: }
186: }
187:
188: _editorPanesBySessionID.remove(sess.getIdentifier());
189: _syntaxesBySessionID.remove(sess.getIdentifier());
190: _sqlTokenListenersBySession.remove(sess.getIdentifier());
191: }
192:
193: private static class DocumentAssignedObjects {
194: private ISession _session;
195: private NetbeansPropertiesWrapper _wrp;
196:
197: public DocumentAssignedObjects(ISession session,
198: NetbeansPropertiesWrapper wrp) {
199: _session = session;
200: _wrp = wrp;
201: }
202:
203: public ISession getSession() {
204: return _session;
205: }
206:
207: public NetbeansPropertiesWrapper getProperties() {
208: return _wrp;
209: }
210: }
211: }
|