    001    /*
    002     * SideKickPlugin.java
    003     * :tabSize=8:indentSize=8:noTabs=false:
    004     * :folding=explicit:collapseFolds=1:
    005     *
    006     * Copyright (C) 2003 Slava Pestov
    007     *
    008     * This program is free software; you can redistribute it and/or
    009     * modify it under the terms of the GNU General Public License
    010     * as published by the Free Software Foundation; either version 2
    011     * of the License, or any later version.
    012     *
    013     * This program is distributed in the hope that it will be useful,
    014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
    015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    016     * GNU General Public License for more details.
    017     *
    018     * You should have received a copy of the GNU General Public License
    019     * along with this program; if not, write to the Free Software
    020     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
    021     */
    022
    023    package sidekick;
    024
    025    //{{{ Imports
    026    import java.util.*;
    027
    028    import org.gjt.sp.jedit.gui.DockableWindowManager;
    029    import org.gjt.sp.jedit.msg.*;
    030    import org.gjt.sp.jedit.textarea.*;
    031    import org.gjt.sp.jedit.*;
    032    import org.gjt.sp.util.*;
    033    //}}}
    034
    035    /**
    036     * SideKick plugin core which tracks registered parsers.
    037     *
    038     * @version $Id: SideKickPlugin.java,v 1.33 2006/06/20 04:15:56 ezust Exp $
    039     */
    040    public class SideKickPlugin extends EBPlugin
    041    {
    042            //{{{ Some constants
    043            public static final String PARSER_PROPERTY = "sidekick.parser";
    044            public static final String PARSED_DATA_PROPERTY = "sidekick.parsed-data";
    045            public static final String PARSE_COUNT = "sidekick.parse-count";
    046            //}}}
    047
    048            //{{{ start() method
    049            public void start()
    050            {
    051                    View view = jEdit.getFirstView();
    052                    while(view != null)
    053                    {
    054                            initView(view);
    055                            EditPane[] panes = view.getEditPanes();
    056                            for(int i = 0; i < panes.length; i++)
    057                                    initTextArea(panes[i].getTextArea());
    058                            view = view.getNext();
    059                    }
    060                    SideKickActions.propertiesChanged();
    061            } //}}}
    062
    063            //{{{ stop() method
    064            public void stop()
    065            {
    066                    View view = jEdit.getFirstView();
    067                    while(view != null)
    068                    {
    069                            uninitView(view);
    070                            SideKickParsedData.setParsedData(view,null);
    071
    072                            EditPane[] panes = view.getEditPanes();
    073                            for(int i = 0; i < panes.length; i++)
    074                                    uninitTextArea(panes[i].getTextArea());
    075                            view = view.getNext();
    076                    }
    077
    078                    Buffer buffer = jEdit.getFirstBuffer();
    079                    while(buffer != null)
    080                    {
    081                            buffer.setProperty(PARSED_DATA_PROPERTY,null);
    082                            buffer = buffer.getNext();
    083                    }
    084            } //}}}
    085
    086            //{{{ handleMessage() method
    087            public void handleMessage(EBMessage msg)
    088            {
    089                    if(msg instanceof ViewUpdate)
    090                    {
    091                            ViewUpdate vu = (ViewUpdate)msg;
    092                            View view = vu.getView();
    093
    094                            if(vu.getWhat() == ViewUpdate.CREATED)
    095                                    initView(view);
    096                            else if(vu.getWhat() == ViewUpdate.CLOSED)
    097                                    uninitView(view);
    098                    }
    099                    else if(msg instanceof EditPaneUpdate)
    100                    {
    101                            EditPaneUpdate epu = (EditPaneUpdate)msg;
    102                            EditPane editPane = epu.getEditPane();
    103
    104                            if(epu.getWhat() == EditPaneUpdate.CREATED)
    105                                    initTextArea(editPane.getTextArea());
    106                            else if(epu.getWhat() == EditPaneUpdate.DESTROYED)
    107                                    uninitTextArea(editPane.getTextArea());
    108                    }
    109                    else if(msg instanceof BufferUpdate)
    110                    {
    111                            BufferUpdate bu = (BufferUpdate)msg;
    112                            if(bu.getWhat() == BufferUpdate.CLOSED)
    113                                    finishParsingBuffer(bu.getBuffer());
    114                    }
    115                    else if(msg instanceof PropertiesChanged)
    116                            SideKickActions.propertiesChanged();
    117
    118            } //}}}
    119
    120            //{{{ registerParser() method
    121            /**
    122             * @deprecated Write a <code>services.xml</code> file instead.
    123             * @see SideKickParser
    124             */
    125            public static void registerParser(SideKickParser parser)
    126            {
    127                    parsers.put(parser.getName(),parser);
    128            } //}}}
    129
    130            //{{{ unregisterParser() method
    131            /**
    132             * @deprecated Write a <code>services.xml</code> file instead.
    133             * @see SideKickParser
    134             */
    135            public static void unregisterParser(SideKickParser parser)
    136            {
    137                    parsers.remove(parser.getName());
    138            } //}}}
    139
    140            //{{{ getParser() method
    141            public static SideKickParser getParser(String name)
    142            {
    143                    SideKickParser parser = (SideKickParser)ServiceManager
    144                            .getService(SideKickParser.SERVICE,name);
    145                    if(parser != null)
    146                            return parser;
    147                    else
    148                            return (SideKickParser)parsers.get(name);
    149            } //}}}
    150
    151            //{{{ getParserForView() method
    152            public static SideKickParser getParserForView(View view)
    153            {
    154                    SideKick sidekick = (SideKick)sidekicks.get(view);
    155                    if(sidekick == null)
    156                            return null;
    157                    else
    158                            return sidekick.getParser();
    159            } //}}}
    160
    161            /**
    162             *
    163             * @param buffer
    164             * @param parserName the new parser we want to use
    165             * @since Sidekick 0.6
    166             */
    167            private static String oldName = null;
    168            public static void setParserForBuffer(Buffer buffer, String parserName)
    169            {
    170                    if (parserName==null) parserName = "";
    171                    if (parserName.equals(oldName)) return;
    172                    oldName = parserName;
    173                    if (parserName.equals("")) return;
    174                    SideKickParser newParser = getParser(parserName);
    175                    if (newParser != null) {
    176                            buffer.setStringProperty(PARSER_PROPERTY, parserName);
    177                    }
    178                    else throw new RuntimeException("Unknown parser: " + parserName);
    179            }
    180
    181            //{{{ getParserForBuffer() method
    182            public static SideKickParser getParserForBuffer(Buffer buffer)
    183            {
    184                    String parserName = buffer.getStringProperty(PARSER_PROPERTY);
    185                    if(parserName == null)
    186                            return null;
    187                    else
    188                            return getParser(parserName);
    189            } //}}}
    190
    191            //{{{ parse() method
    192            /**
    193             * Immediately begins parsing the current buffer in a background thread.
    194             * @param view The view
    195             * @param showParsingMessage Clear the tree and show a status message
    196             * there?
    197             */
    198            public static void parse(View view, boolean showParsingMessage)
    199            {
    200                    SideKick sidekick = (SideKick)sidekicks.get(view);
    201                    if (sidekick == null) return;
    202                    // Had to remove this
    203                    sidekick.setParser(view.getBuffer());
    204                    sidekick.parse(showParsingMessage);
    205            } //}}}
    206
    207            //{{{ getErrorSource() method
    208            /**
    209             * Returns the error source used by the given view.
    210             * @param view The view
    211             * @since SideKick 0.3
    212             */
    213            /* public static DefaultErrorSource getErrorSource(View view)
    214            {
    215                    return ((SideKick)sidekicks.get(view)).getErrorSource();
    216            } */ //}}}
    217
    218            //{{{ addWorkRequest() method
    219            public static void addWorkRequest(Runnable run, boolean inAWT)
    220            {
    221                    if(worker == null)
    222                    {
    223                            worker = new WorkThreadPool("SideKick",1);
    224                            worker.start();
    225                    }
    226                    worker.addWorkRequest(run,inAWT);
    227            } //}}}
    228
    229            //{{{ isParsingBuffer()
    230            public static boolean isParsingBuffer(Buffer buffer)
    231            {
    232                    return parsedBufferSet.contains(buffer);
    233            } //}}}
    234
    235            //{{{ Package-private members
    236
    237            //{{{ startParsingBuffer()
    238            static void startParsingBuffer(Buffer buffer)
    239            {
    240                    parsedBufferSet.add(buffer);
    241            } //}}}
    242
    243            //{{{ finishParsingBuffer()
    244            static void finishParsingBuffer(Buffer buffer)
    245            {
    246                    parsedBufferSet.remove(buffer);
    247            } //}}}
    248
    249            //}}}
    250
    251            //{{{ Private members
    252            private static HashMap sidekicks = new HashMap();
    253            private static HashMap parsers = new HashMap();
    254            private static WorkThreadPool worker;
    255            private static HashSet parsedBufferSet = new HashSet();
    256
    257            //{{{ initView() method
    258            private void initView(View view)
    259            {
    260                    sidekicks.put(view,new SideKick(view));
    261            } //}}}
    262
    263            //{{{ uninitView() method
    264            private void uninitView(View view)
    265            {
    266                    SideKick sidekick = (SideKick)sidekicks.get(view);
    267                    sidekick.dispose();
    268                    sidekicks.remove(view);
    269            } //}}}
    270
    271            //{{{ initTextArea() method
    272            private void initTextArea(JEditTextArea textArea)
    273            {
    274                    SideKickBindings b = new SideKickBindings();
    275                    textArea.putClientProperty(SideKickBindings.class,b);
    276                    textArea.addKeyListener(b);
    277            } //}}}
    278
    279            //{{{ uninitTextArea() method
    280            private void uninitTextArea(JEditTextArea textArea)
    281            {
    282                    SideKickBindings b = (SideKickBindings)
    283                            textArea.getClientProperty(
    284                            SideKickBindings.class);
    285                    textArea.putClientProperty(SideKickBindings.class,null);
    286                    textArea.removeKeyListener(b);
    287            } //}}}
    288
    289            //}}}
    290    }