1 7 8 package org.gjt.jclasslib.browser; 9 10 import org.gjt.jclasslib.structures.*; 11 import org.gjt.jclasslib.util.ExtendedJLabel; 12 13 import javax.swing.*; 14 import javax.swing.tree.TreePath ; 15 import java.awt.*; 16 import java.awt.event.MouseListener ; 17 import java.util.HashMap ; 18 19 26 public abstract class AbstractDetailPane extends JPanel { 27 28 29 public static final String CPINFO_LINK_TEXT = "cp_info #"; 30 31 public static final Color COLOR_LINK = new Color(0, 128, 0); 32 33 34 protected static final Color COLOR_HIGHLIGHT = new Color(128, 0, 0); 35 36 37 protected BrowserServices services; 38 39 private HashMap labelToMouseListener = new HashMap (); 40 41 45 protected AbstractDetailPane(BrowserServices services) { 46 this.services = services; 47 setupComponent(); 48 } 49 50 54 public BrowserServices getBrowserServices() { 55 return services; 56 } 57 58 64 public abstract void show(TreePath treePath); 65 66 69 protected abstract void setupComponent(); 70 71 75 protected ExtendedJLabel normalLabel() { 76 return normalLabel(""); 77 } 78 79 84 protected ExtendedJLabel normalLabel(String text) { 85 ExtendedJLabel label = new ExtendedJLabel(text); 86 return label; 87 } 88 89 93 protected ExtendedJLabel highlightLabel() { 94 ExtendedJLabel label = normalLabel(); 95 label.setForeground(COLOR_HIGHLIGHT); 96 return label; 97 } 98 99 103 protected ExtendedJLabel linkLabel() { 104 ExtendedJLabel label = normalLabel(); 105 label.setForeground(COLOR_LINK); 106 label.setRequestFocusEnabled(true); 107 label.setUnderlined(true); 108 return label; 109 } 110 111 117 protected int getIndex(TreePath treePath) { 118 return ((BrowserTreeNode)treePath.getLastPathComponent()).getIndex(); 119 } 120 121 126 protected AttributeInfo findAttribute(TreePath path) { 127 128 TreePath parentPath = path.getParentPath(); 129 BrowserTreeNode parentNode = (BrowserTreeNode)parentPath.getLastPathComponent(); 130 String parentNodeType = parentNode.getType(); 131 132 ClassFile classFile = services.getClassFile(); 133 int parentIndex = getIndex(parentPath); 134 int index = getIndex(path); 135 136 if (parentNodeType.equals(BrowserTreeNode.NODE_NO_CONTENT)) { 137 return classFile.getAttributes()[index]; 138 139 } else if (parentNodeType.equals(BrowserTreeNode.NODE_FIELD)) { 140 return classFile.getFields()[parentIndex].getAttributes()[index]; 141 142 } else if (parentNodeType.equals(BrowserTreeNode.NODE_METHOD)) { 143 return classFile.getMethods()[parentIndex].getAttributes()[index]; 144 145 } else { 146 return findAttribute(parentPath).getAttributes()[index]; 147 } 148 } 149 150 155 protected String getConstantPoolEntryName(int constantPoolIndex) { 156 157 try { 158 return services.getClassFile().getConstantPoolEntryName(constantPoolIndex); 159 } catch (InvalidByteCodeException ex) { 160 return "invalid constant pool reference"; 161 } 162 } 163 164 172 protected void constantPoolHyperlink(ExtendedJLabel value, 173 ExtendedJLabel comment, 174 int constantPoolIndex) { 175 176 value.setText(CPINFO_LINK_TEXT + constantPoolIndex); 177 setupMouseListener(value, constantPoolIndex); 178 value.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); 179 180 if (comment != null) { 181 comment.setToolTipText(comment.getText()); 182 comment.setText("<" + getConstantPoolEntryName(constantPoolIndex) + ">"); 183 } 184 185 } 186 187 private void setupMouseListener(ExtendedJLabel value, int constantPoolIndex) { 188 189 MouseListener oldListener = (MouseListener )labelToMouseListener.get(value); 190 if (oldListener != null) { 191 value.removeMouseListener(oldListener); 192 } 193 MouseListener newListener = new ConstantPoolHyperlinkListener( 194 services, 195 constantPoolIndex); 196 197 value.addMouseListener(newListener); 198 labelToMouseListener.put(value, newListener); 199 } 200 } 201 202 | Popular Tags |