001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.drjava.model.definitions;
038:
039: import javax.swing.text.*;
040: import edu.rice.cs.drjava.model.GlobalEventNotifier;
041:
042: /**
043: * This is an editor kit for editing Java source files.
044: * It functions as the controller in the MVC arrangement.
045: * It implements a factory for new documents, and it also
046: * has a factory for Views (the things that render the document).
047: * @version $Id: DefinitionsEditorKit.java 4255 2007-08-28 19:17:37Z mgricken $
048: */
049: public class DefinitionsEditorKit extends StyledEditorKit {
050:
051: private GlobalEventNotifier _notifier;
052:
053: /** Creates a new editor kit with the given listeners.
054: * @param notifier Keeps track of the listeners to the model
055: */
056: public DefinitionsEditorKit(GlobalEventNotifier notifier) {
057: _notifier = notifier;
058: }
059:
060: private static ViewFactory _factory = new ViewFactory() {
061: public View create(Element elem) {
062: // The following line is for performance analysis only!
063: // return new WrappedPlainView(elem, true);
064: return new ColoringView(elem);
065: }
066: };
067:
068: /** Creates a new DefinitionsDocument. This used to be named createDefaultDocument() so that the view
069: * (DefinitionsPane) would create a DefinitionsDocument by default when it was constructed. However,
070: * we already have an existing DefinitionsDocument we want to use when the DefinitionsPane is constructed,
071: * so this default one was being created and thrown away (very expensive). Ideally, we would have the
072: * DefinitionsPane use our existing document from the beginning, but the JEditorPane constructor does
073: * not take in a Document. The only possible approach would be to have this EditorKit return the desired
074: * existing document when the JEditorPane requests a new one, but since the EditorKit must be kept as a
075: * static field on DefinitionsPane since we can't set one until after JEditorPane's constructor is
076: * finished), there's no clean way to tell the EditorKit which document to return at which time. (It
077: * would require a large synchronization effort each time a DefinitionsPane is constructed.)
078: *
079: * As an easier alternative, we just let the DefaultEditorKit return a PlainDocument (much lighter weight),
080: * which can then be thrown away when the true DefinitionsDocument is assigned.
081: *
082: * Improvements to this approach are welcome... :)
083: */
084: public DefinitionsDocument createNewDocument() {
085: return _createDefaultTypedDocument();
086: }
087:
088: /** Creates a new DefinitionsDocument.
089: * @return a new DefinitionsDocument.
090: */
091: private DefinitionsDocument _createDefaultTypedDocument() {
092: return new DefinitionsDocument(_notifier);
093: }
094:
095: /** Get the MIME content type of the document.
096: * @return "text/java"
097: */
098: public String getContentType() {
099: return "text/java";
100: }
101:
102: /** We want to use our ColoringView to render text, so here we return
103: * a factory that creates ColoringViews.
104: */
105: public final ViewFactory getViewFactory() {
106: return _factory;
107: }
108: }
|