1 32 33 package it.businesslogic.ireport.gui.expbuilder; 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 ExpObjectCellRenderer extends JTextPane implements ListCellRenderer { 46 private Color selectionBackground; 47 private Color background; 48 49 Style typeStyle = null; 51 Style classTypeStyle = null; 52 53 Style parameterStyle = null; 54 Style variableStyle = null; 55 Style fieldStyle = null; 56 Style whiteStyle = null; 57 58 public ExpObjectCellRenderer(JList list) { 59 super(); 60 selectionBackground = list.getSelectionBackground(); 61 background = list.getBackground(); 62 StyledDocument doc = new DefaultStyledDocument (); 63 this.setDocument( doc ); 64 65 typeStyle = doc.addStyle("typeStyle", null); 66 StyleConstants.setItalic(typeStyle, true); 67 StyleConstants.setForeground(typeStyle, Color.gray); 68 69 classTypeStyle = doc.addStyle("classTypeStyle", null); 70 StyleConstants.setForeground(classTypeStyle, Color.gray); 71 72 parameterStyle = doc.addStyle("parameterStyle", null); 73 StyleConstants.setForeground(parameterStyle, Color.red.darker()); 74 75 variableStyle = doc.addStyle("variableStyle", null); 76 StyleConstants.setForeground(variableStyle, Color.blue); 77 78 fieldStyle = doc.addStyle("fieldStyle", null); 79 StyleConstants.setForeground(fieldStyle, Color.green.darker().darker()); 80 81 whiteStyle = doc.addStyle("whiteStyle", null); 82 StyleConstants.setForeground(whiteStyle, Color.white); 83 84 } 85 public Component getListCellRendererComponent(JList list, Object object, 86 int index, boolean isSelected, boolean cellHasFocus) { 87 88 this.setText(""); 89 StyledDocument doc = (StyledDocument )this.getDocument(); 90 if (object instanceof ExpObject) 91 { 92 ExpObject eo = (ExpObject)object; 93 94 try { 95 96 doc.insertString(doc.getLength(), eo.getName() + " ", (isSelected) ? whiteStyle : null); 97 98 Style s = parameterStyle; 99 String type = "Parameter"; 100 if (eo.getType() == eo.TYPE_FIELD) 101 { 102 s = fieldStyle; 103 type = "Field"; 104 } 105 else if (eo.getType() == eo.TYPE_VARIABLE) 106 { 107 s = variableStyle; 108 type = "Variable"; 109 } 110 111 if (isSelected) s = whiteStyle; 112 113 114 doc.insertString(doc.getLength(), type + " ", s); 115 116 String tp = eo.getClassType() + ""; 117 if (tp.lastIndexOf(".") > 0) tp = tp.substring(tp.lastIndexOf(".")+1); 118 119 doc.insertString(doc.getLength(), tp, classTypeStyle); 120 } catch (Exception ex){} 121 } 122 else 123 { 124 try { 125 doc.insertString(doc.getLength(), "" + object, null); 126 } catch (Exception ex){} 127 } 128 setBackground(isSelected ? selectionBackground : background); 129 return this; 130 } 131 } 132 133 | Popular Tags |