php.smarty/src/org/netbeans/modules/php/smarty/editor/TplKit.java
author Martin Fousek <marfous@netbeans.org>
Thu, 03 Mar 2011 00:03:37 +0100
changeset 17309 60f4cc6038b3
parent 16264 0bcd1b2b366f
child 17565 d9cb4ed472d7
permissions -rw-r--r--
removing UI freezing by change of default Smarty delimiters
     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved.
     5  *
     6  * The contents of this file are subject to the terms of either the GNU
     7  * General Public License Version 2 only ("GPL") or the Common
     8  * Development and Distribution License("CDDL") (collectively, the
     9  * "License"). You may not use this file except in compliance with the
    10  * License. You can obtain a copy of the License at
    11  * http://www.netbeans.org/cddl-gplv2.html
    12  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    13  * specific language governing permissions and limitations under the
    14  * License.  When distributing the software, include this License Header
    15  * Notice in each file and include the License file at
    16  * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    17  * particular file as subject to the "Classpath" exception as provided
    18  * by Sun in the GPL Version 2 section of the License file that
    19  * accompanied this code. If applicable, add the following below the
    20  * License Header, with the fields enclosed by brackets [] replaced by
    21  * your own identifying information:
    22  * "Portions Copyrighted [year] [name of copyright owner]"
    23  *
    24  * Contributor(s):
    25  *
    26  * The Original Software is NetBeans. The Initial Developer of the Original
    27  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
    28  * Microsystems, Inc. All Rights Reserved.
    29  *
    30  * If you wish your version of this file to be governed by only the CDDL
    31  * or only the GPL Version 2, indicate your decision by adding
    32  * "[Contributor] elects to include this software in this distribution
    33  * under the [CDDL or GPL Version 2] license." If you do not indicate a
    34  * single choice of license, a recipient has the option to distribute
    35  * your version of this file under either the CDDL, the GPL Version 2 or
    36  * to extend the choice of license to its licensees as provided above.
    37  * However, if you add GPL Version 2 code and therefore, elected the GPL
    38  * Version 2 license, then the option applies only if the new code is
    39  * made subject to such option by the copyright holder.
    40  */
    41 
    42 package org.netbeans.modules.php.smarty.editor;
    43 
    44 
    45 import org.netbeans.editor.ext.ExtKit;
    46 import org.netbeans.modules.editor.NbEditorKit;
    47 import javax.swing.Action;
    48 import javax.swing.SwingUtilities;
    49 import javax.swing.text.*;
    50 import org.netbeans.modules.editor.NbEditorUtilities;
    51 import org.openide.filesystems.FileObject;
    52 import org.openide.loaders.DataObject;
    53 import org.netbeans.api.lexer.InputAttributes;
    54 import org.netbeans.modules.csl.api.InstantRenameAction;
    55 import org.netbeans.modules.csl.api.SelectCodeElementAction;
    56 import org.netbeans.modules.csl.api.ToggleBlockCommentAction;
    57 import org.netbeans.modules.editor.NbEditorDocument;
    58 import org.netbeans.modules.php.smarty.editor.lexer.TplTopTokenId;
    59 import org.netbeans.modules.php.smarty.editor.utlis.TplUtils;
    60 import org.netbeans.spi.lexer.MutableTextInput;
    61 
    62 /**
    63  * Editor kit implementation for TPL content type
    64  *
    65  * @author Martin Fousek
    66  */
    67 public class TplKit extends NbEditorKit implements org.openide.util.HelpCtx.Provider{ // NbEditorKit implements org.openide.util.HelpCtx.Provider{
    68 
    69     public static final String TPL_MIME_TYPE = "text/x-tpl"; // NOI18N
    70 
    71     /** serialVersionUID */
    72     private static final long serialVersionUID = 8922234837050367142L;
    73 
    74     public TplKit() {
    75         this(TPL_MIME_TYPE);
    76     }
    77 
    78     public TplKit(String mimeType) {
    79         super();
    80      //   createDefaultDocument();
    81     }
    82 
    83     @Override
    84     public String getContentType() {
    85         return TPL_MIME_TYPE;
    86     }
    87 
    88     @Override
    89     public Object clone() {
    90         return new TplKit();
    91     }
    92 
    93     @Override
    94     public Document createDefaultDocument() {
    95         final Document doc = super.createDefaultDocument();
    96         // see TplEditorSupport.createStyledDocument;
    97         doc.putProperty("postInitRunnable", new Runnable() { //NOI18N
    98             public void run() {
    99                 initLexerColoringListener(doc);
   100             }
   101         });
   102         return doc;
   103     }
   104 
   105     @Override
   106     protected Action[] createActions() {
   107         Action[] javaActions = new Action[] {
   108             new SelectCodeElementAction(SelectCodeElementAction.selectNextElementAction, true),
   109             new SelectCodeElementAction(SelectCodeElementAction.selectPreviousElementAction, false),
   110             new InstantRenameAction(),
   111             new ToggleBlockCommentAction(),
   112             new ExtKit.CommentAction(""), //NOI18N
   113             new ExtKit.UncommentAction("") //NOI18N
   114         };
   115 
   116         return TextAction.augmentList(super.createActions(), javaActions);
   117     }
   118 
   119     public void initLexerColoringListener(final Document doc) {
   120         DataObject dobj = NbEditorUtilities.getDataObject(doc);
   121         FileObject fobj = (dobj != null) ? dobj.getPrimaryFile() : null;
   122 
   123         TplMetaData tplMetaData = TplUtils.getProjectPropertiesForFileObject(fobj);
   124 
   125         //add an instance of InputAttributes to the document property,
   126         //lexer will use it to read coloring information
   127         InputAttributes inputAttributes = new InputAttributes();
   128         inputAttributes.setValue(TplTopTokenId.language(), TplMetaData.class, tplMetaData, false);
   129         doc.putProperty(InputAttributes.class, inputAttributes);
   130 
   131         SwingUtilities.invokeLater(new Runnable() {
   132 
   133             @Override
   134             public void run() {
   135                 NbEditorDocument nbdoc = (NbEditorDocument) doc;
   136                 nbdoc.runAtomic(new Runnable() {
   137 
   138                     @Override
   139                     public void run() {
   140                         MutableTextInput mti = (MutableTextInput) doc.getProperty(MutableTextInput.class);
   141                         if (mti != null) {
   142                             mti.tokenHierarchyControl().rebuild();
   143                         }
   144                     }
   145                 });
   146             }
   147         });
   148     }
   149 
   150     @Override
   151     public org.openide.util.HelpCtx getHelpCtx() {
   152         return new org.openide.util.HelpCtx(TplKit.class);
   153     }
   154 
   155 }
   156