1 32 33 package it.businesslogic.ireport.gui; 34 35 import java.awt.Color ; 36 import java.awt.Component ; 37 import javax.swing.JList ; 38 import javax.swing.JTextPane ; 39 import javax.swing.ListCellRenderer ; 40 import javax.swing.text.Style ; 41 import javax.swing.text.StyleConstants ; 42 import javax.swing.text.StyledDocument ; 43 import javax.swing.text.DefaultStyledDocument ; 44 45 public class TextAreaCellRenderer extends JTextPane implements ListCellRenderer { 46 private Color selectionBackground; 47 private Color background; 48 49 Style methodStyle = null; 51 Style returnTypeStyle = null; 52 53 public TextAreaCellRenderer(JList list) { 54 super(); 55 selectionBackground = list.getSelectionBackground(); 56 background = list.getBackground(); 57 StyledDocument doc = new DefaultStyledDocument (); 58 this.setDocument( doc ); 59 methodStyle = doc.addStyle("methodStyle", null); 60 StyleConstants.setBold(methodStyle, true); 61 returnTypeStyle = doc.addStyle("returnType", null); 62 StyleConstants.setForeground(returnTypeStyle, Color.gray); 63 } 64 public Component getListCellRendererComponent(JList list, Object object, 65 int index, boolean isSelected, boolean cellHasFocus) { 66 67 String s = (String )object; 68 this.setText(""); 69 StyledDocument doc = (StyledDocument )this.getDocument(); 70 try { 71 doc.insertString(doc.getLength(), s.substring(0, s.indexOf("(")), methodStyle); 72 doc.insertString(doc.getLength(), s.substring(s.indexOf("("), s.lastIndexOf(")")+1), null); 73 doc.insertString(doc.getLength(), s.substring(s.lastIndexOf(")")+1), returnTypeStyle); 74 } catch (Exception ex){} 75 setBackground(isSelected ? selectionBackground : background); 76 return this; 77 } 78 } 79 80 | Popular Tags |