    001    /*
    002     * ParserOptionPane.java - Sidekick parsers configuration panel
    003     * :tabSize=8:indentSize=8:noTabs=false:
    004     * :folding=explicit:collapseFolds=1:
    005     *
    006     * Copyright (C) 2005 Matthieu Casanova
    007     * Portions copyright (C) 2000, 2003 Slava Pestov
    008     *
    009     * This program is free software; you can redistribute it and/or
    010     * modify it under the terms of the GNU General Public License
    011     * as published by the Free Software Foundation; either version 2
    012     * of the License, or any later version.
    013     *
    014     * This program is distributed in the hope that it will be useful,
    015     * but WITHOUT ANY WARRANTY; without even the implied warranty of
    016     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    017     * GNU General Public License for more details.
    018     *
    019     * You should have received a copy of the GNU General Public License
    020     * along with this program; if not, write to the Free Software
    021     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
    022     */
    023
    024    package sidekick;
    025
    026    //{{{ Imports
    027    import javax.swing.table.*;
    028    import javax.swing.*;
    029    import java.awt.*;
    030    import java.util.*;
    031    import org.gjt.sp.jedit.*;
    032    //}}}
    033
    034    //{{{ ParserOptionPane class
    035    /**
    036     * An option pane to configure the mode - parsers associations.
    037     *
    038     * @author Matthieu Casanova
    039     */
    040    public class ParserOptionPane extends AbstractOptionPane
    041    {
    042            //{{{ ParserOptionPane constructor
    043            public ParserOptionPane()
    044            {
    045                    super("sidekick.parser");
    046            } //}}}
    047
    048            //{{{ _init() method
    049            public void _init()
    050            {
    051                    setLayout(new BorderLayout());
    052                    add(BorderLayout.CENTER,createTableScroller());
    053            } //}}}
    054
    055            //{{{ _save() method
    056            public void _save()
    057            {
    058                    tableModel.save();
    059            } //}}}
    060
    061            //{{{ Private members
    062
    063            //{{{ Instance variables
    064            private JTable table;
    065            private MyTableModel tableModel;
    066            //}}}
    067
    068            //{{{ createTableScroller() method
    069            private JScrollPane createTableScroller()
    070            {
    071                    tableModel = createModel();
    072                    table = new JTable(tableModel);
    073                    table.getTableHeader().setReorderingAllowed(false);
    074                    table.setColumnSelectionAllowed(false);
    075                    table.setRowSelectionAllowed(false);
    076                    table.setCellSelectionEnabled(false);
    077                    String[] serviceNames = ServiceManager.getServiceNames(SideKickParser.SERVICE);
    078                    Vector parserList = new Vector(serviceNames.length+2);
    079                    parserList.add(null);
    080                    parserList.add(MyTableModel.DEFAULTPARSER);
    081                    for (int i = 0; i < serviceNames.length; i++)
    082                    {
    083                            parserList.add(serviceNames[i]);
    084                    }
    085                    Collections.sort(parserList, new Comparator(){
    086                            public int compare(Object a, Object b) {
    087                                    a = a == null ? "" : a;
    088                                    b = b == null ? "" : b;
    089                                    return a.toString().compareToIgnoreCase(b.toString());
    090                            }
    091                    } );
    092                    ParserCellRenderer comboBox = new ParserCellRenderer(parserList);
    093                    table.setRowHeight(comboBox.getPreferredSize().height);
    094                    TableColumn column = table.getColumnModel().getColumn(1);
    095                    column.setCellRenderer(comboBox);
    096                    column.setCellEditor(new DefaultCellEditor(new ParserCellRenderer(parserList)));
    097
    098                    Dimension d = table.getPreferredSize();
    099                    d.height = Math.min(d.height,50);
    100                    JScrollPane scroller = new JScrollPane(table);
    101                    scroller.setPreferredSize(d);
    102                    return scroller;
    103            } //}}}
    104
    105            //{{{ createWindowModel() method
    106            private MyTableModel createModel()
    107            {
    108                    return new MyTableModel();
    109            } //}}}
    110
    111            //}}}
    112
    113            //{{{ DockPositionCellRenderer class
    114            class ParserCellRenderer extends JComboBox
    115                    implements TableCellRenderer
    116            {
    117                    ParserCellRenderer(Vector vector)
    118                    {
    119                            super(vector);
    120                            ParserCellRenderer.this.setRequestFocusEnabled(false);
    121                    }
    122
    123                    public Component getTableCellRendererComponent(JTable table,
    124                            Object value, boolean isSelected, boolean hasFocus,
    125                            int row, int column)
    126                    {
    127                            setSelectedItem(value);
    128                            return this;
    129                    }
    130            } //}}}
    131    } //}}}
    132
    133    //{{{ WindowTableModel class
    134    class MyTableModel extends AbstractTableModel
    135    {
    136            private Vector modes;
    137
    138            public static final String DEFAULTPARSER = "default parser";
    139
    140            //{{{ WindowTableModel constructor
    141            MyTableModel()
    142            {
    143                    Mode[] modes = jEdit.getModes();
    144                    this.modes = new Vector(modes.length);
    145                    for(int i = 0; i < modes.length; i++)
    146                    {
    147                            this.modes.addElement(new Entry(modes[i].getName()));
    148                    }
    149                    Collections.sort(this.modes);
    150            } //}}}
    151
    152            //{{{ getColumnCount() method
    153            public int getColumnCount()
    154            {
    155                    return 2;
    156            } //}}}
    157
    158            //{{{ getRowCount() method
    159            public int getRowCount()
    160            {
    161                    return modes.size();
    162            } //}}}
    163
    164            //{{{ getColumnClass() method
    165            public Class getColumnClass(int col)
    166            {
    167                    switch(col)
    168                    {
    169                    case 0:
    170                    case 1:
    171                            return String.class;
    172                    default:
    173                            throw new InternalError();
    174                    }
    175            } //}}}
    176
    177            //{{{ getValueAt() method
    178            public Object getValueAt(int row, int col)
    179            {
    180                    Entry modeParser = (Entry)modes.elementAt(row);
    181                    switch(col)
    182                    {
    183                    case 0:
    184                            return modeParser.mode;
    185                    case 1:
    186                            return modeParser.parser;
    187                    default:
    188                            throw new InternalError();
    189                    }
    190            } //}}}
    191
    192            //{{{ isCellEditable() method
    193            public boolean isCellEditable(int row, int col)
    194            {
    195                    return col == 1;
    196            } //}}}
    197
    198            //{{{ setValueAt() method
    199            public void setValueAt(Object value, int row, int col)
    200            {
    201                    if(col == 0)
    202                            return;
    203
    204                    Entry modeParser = (Entry)modes.elementAt(row);
    205                    switch(col)
    206                    {
    207                    case 1:
    208                            modeParser.parser = (String)value;
    209                            break;
    210                    default:
    211                            throw new InternalError();
    212                    }
    213
    214                    fireTableRowsUpdated(row,row);
    215            } //}}}
    216
    217            //{{{ getColumnName() method
    218            public String getColumnName(int index)
    219            {
    220                    switch(index)
    221                    {
    222                    case 0:
    223                            return jEdit.getProperty("options.sidekick.parser.mode");
    224                    case 1:
    225                            return jEdit.getProperty("options.sidekick.parser.parser");
    226                    default:
    227                            throw new InternalError();
    228                    }
    229            } //}}}
    230
    231            //{{{ save() method
    232            public void save()
    233            {
    234                    for(int i = 0; i < modes.size(); i++)
    235                    {
    236                            ((Entry)modes.elementAt(i)).save();
    237                    }
    238            } //}}}
    239
    240            //{{{ Entry class
    241            class Entry implements Comparable
    242            {
    243                    String mode;
    244                    String parser = null;
    245
    246                    Entry(String mode)
    247                    {
    248                            this.mode = mode;
    249                            parser = jEdit.getProperty("mode."+ this.mode + ".sidekick.parser");
    250
    251                    }
    252
    253                    void save()
    254                    {
    255                            if (parser == DEFAULTPARSER)
    256                                    jEdit.resetProperty("mode." + mode + ".sidekick.parser");
    257                            else
    258                                    jEdit.setProperty("mode." + mode + ".sidekick.parser",parser);
    259                    }
    260
    261                    public int compareTo(Object a) {
    262                            return this.mode.compareToIgnoreCase(((Entry)a).mode);
    263                    }
    264            } //}}}
    265
    266    } //}}}