001    package de.mospace.swing.text;
002    
003    /* Mp3dings - manage mp3 meta-information
004     * Copyright (C) 2003 Moritz Ringler
005     * $Id: FullTextStringCellRenderer.java 12 2007-07-27 09:48:53Z ringler $
006     *
007     * This program is free software; you can redistribute it and/or
008     * modify it under the terms of the GNU General Public License
009     * as published by the Free Software Foundation; either version 2
010     * of the License, or (at your option) any later version.
011     *
012     * This program is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015     * GNU General Public License for more details.
016     *
017     * You should have received a copy of the GNU General Public License
018     * along with this program; if not, write to the Free Software
019     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
020     */
021    
022    import java.awt.Component;
023    import javax.swing.JTable;
024    import javax.swing.border.Border;
025    import javax.swing.table.DefaultTableCellRenderer;
026    
027    /**
028     * Multiline string renderer for JTable. The multiline string is abbreviated
029     * to the first at most 29 characters of its first line and three dots are
030     * appended, if necessary. The resulting string is displayed by the
031     * DefaultTableCellRenderer.
032     *
033     * @version $Revision: 12 $ ($Date: 2007-07-27 11:48:53 +0200 (Fr, 27 Jul 2007) $)
034     * @author Moritz Ringler
035     * @see de.mospace.lang.FullTextString
036     */
037    public class FullTextStringCellRenderer extends DefaultTableCellRenderer {
038        Border unselectedBorder = null;
039        Border selectedBorder = null;
040        boolean isBordered = true;
041        public FullTextStringCellRenderer() {
042            super();
043        }
044    
045        public Component getTableCellRendererComponent(
046                                JTable table, Object value,
047                                boolean isSelected, boolean hasFocus,
048                                int row, int column) {
049            String shortstr = (value==null)? "":value.toString();
050            int iend   = shortstr.length();
051            int inull  = shortstr.indexOf(0);
052            int ibreak = shortstr.indexOf('\n');
053            int i      = Math.min(iend, 29);
054            if ((inull > -1) && (inull < i)){
055                i = inull;
056            }
057            if ((ibreak > -1) && ibreak < i){
058                i = ibreak;
059            }
060            shortstr = shortstr.substring(0,i);
061            if(i<iend && shortstr.length() != 0){
062                shortstr = shortstr.concat("...");
063            }
064            return super.getTableCellRendererComponent(table, shortstr, isSelected,
065                    hasFocus, row, column);
066        }
067    }