1 19 package org.openide.explorer.propertysheet; 20 21 import javax.accessibility.Accessible ; 22 import javax.accessibility.AccessibleContext ; 23 import org.openide.util.Utilities; 24 25 import java.awt.Dimension ; 26 import java.awt.Font ; 27 import java.awt.Graphics ; 28 import java.awt.Image ; 29 import java.awt.Insets ; 30 import java.awt.event.ActionEvent ; 31 import java.awt.event.ActionListener ; 32 import java.awt.event.MouseEvent ; 33 import java.awt.event.MouseListener ; 34 import javax.accessibility.AccessibleRole ; 35 36 import javax.swing.BorderFactory ; 37 import javax.swing.ImageIcon ; 38 import javax.swing.JButton ; 39 import javax.swing.JComponent ; 40 import javax.swing.JComponent.AccessibleJComponent; 41 import javax.swing.JLabel ; 42 import javax.swing.JScrollPane ; 43 import javax.swing.JTextArea ; 44 import javax.swing.SwingUtilities ; 45 import javax.swing.UIManager ; 46 import org.openide.util.NbBundle; 47 48 49 54 class DescriptionComponent extends JComponent implements ActionListener , MouseListener , Accessible { 55 private static int fontHeight = -1; 56 private JTextArea jta; 57 private JLabel lbl; 58 private JButton btn; 59 private JScrollPane jsc; 60 61 62 public DescriptionComponent() { 63 init(); 64 } 65 66 private void init() { 67 setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2)); 68 69 jta = new JTextArea (); 70 jta.setLineWrap(true); 71 jta.setWrapStyleWord(true); 72 jta.setOpaque(false); 73 jta.setBackground(getBackground()); 74 jta.setEditable(false); 75 jta.setOpaque(false); 76 jta.getAccessibleContext().setAccessibleName( NbBundle.getMessage(DescriptionComponent.class, "ACS_Description") ); 77 jta.getAccessibleContext().setAccessibleDescription( NbBundle.getMessage(DescriptionComponent.class, "ACSD_Description") ); 78 79 jsc = new JScrollPane (jta); 84 jsc.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 85 jsc.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER); 86 jsc.setBorder(BorderFactory.createEmptyBorder()); 87 jsc.setViewportBorder(jsc.getBorder()); 88 jsc.setOpaque(false); 89 jsc.setBackground(getBackground()); 90 jsc.getViewport().setOpaque(false); 91 92 Font f = UIManager.getFont("Tree.font"); 94 if (f != null) { 95 jta.setFont(f); 96 } 97 98 btn = new JButton (); 99 btn.addActionListener(this); 100 101 Image help = Utilities.loadImage("org/openide/resources/propertysheet/propertySheetHelp.gif", true); 103 ImageIcon helpIcon = new ImageIcon (help); btn.setIcon(helpIcon); 105 btn.setPreferredSize(new Dimension (helpIcon.getIconWidth(), helpIcon.getIconHeight())); 106 btn.setBorder(BorderFactory.createEmptyBorder()); 107 btn.setBorderPainted(false); 108 btn.setFocusable(false); 109 110 lbl = new JLabel ("Label"); 112 lbl.setFont(new Font (lbl.getFont().getName(), Font.BOLD, lbl.getFont().getSize())); 113 114 add(jsc); 115 add(lbl); 116 add(btn); 117 jta.addMouseListener(this); 118 jsc.addMouseListener(this); 119 lbl.addMouseListener(this); 120 btn.addMouseListener(this); 121 jsc.getViewport().addMouseListener(this); 122 } 123 124 public void doLayout() { 125 Insets ins = getInsets(); 126 Dimension bttn = btn.getMinimumSize(); 127 Dimension lbll = lbl.getPreferredSize(); 128 129 int height = Math.max(bttn.height, lbll.height); 130 int right = getWidth() - (ins.right + bttn.width); 131 132 btn.setBounds(right, ins.top, bttn.width, height); 133 134 lbl.setBounds(ins.left, ins.top, right, height); 135 136 jsc.setBounds(ins.left, height, getWidth() - (ins.left + ins.right), getHeight() - height); 137 } 138 139 public void setDescription(String title, String txt) { 140 if (title == null) { 141 title = ""; 142 } 143 144 if (txt == null) { 145 txt = ""; 146 } 147 148 lbl.setText(title); 149 150 if (title.equals(txt)) { 151 jta.setText(""); 152 } else { 153 jta.setText(txt); 154 } 155 } 156 157 public void setHelpEnabled(boolean val) { 158 btn.setEnabled(val); 159 } 160 161 164 public void paint(Graphics g) { 165 if (fontHeight == -1) { 166 fontHeight = g.getFontMetrics(lbl.getFont()).getHeight(); 167 } 168 169 super.paint(g); 170 } 171 172 174 public Dimension getPreferredSize() { 175 Dimension d = new Dimension (super.getPreferredSize()); 176 177 if (fontHeight > 0) { 178 Insets ins = getInsets(); 179 d.height = Math.max(50, Math.max(d.height, (4 * fontHeight) + ins.top + ins.bottom + 12)); 180 } else { 181 d.height = Math.min(d.height, 64); 182 } 183 184 return d; 185 } 186 187 public Dimension getMinimumSize() { 188 if (fontHeight < 0) { 189 return super.getMinimumSize(); 190 } 191 192 Dimension d = new Dimension (4 * fontHeight, 4 * fontHeight); 193 194 return d; 195 } 196 197 public void actionPerformed(ActionEvent actionEvent) { 198 PSheet sheet = (PSheet) SwingUtilities.getAncestorOfClass(PSheet.class, this); 199 200 if (sheet != null) { 201 sheet.helpRequested(); 202 } 203 } 204 205 private PSheet findSheet() { 206 return (PSheet) SwingUtilities.getAncestorOfClass(PSheet.class, this); 207 } 208 209 public void mouseClicked(MouseEvent e) { 210 } 211 212 public void mouseEntered(MouseEvent e) { 213 } 214 215 public void mouseExited(MouseEvent e) { 216 } 217 218 221 public void mousePressed(MouseEvent e) { 222 PSheet sh = findSheet(); 223 224 if (sh != null) { 225 sh.mousePressed(e); 226 } 227 } 228 229 232 public void mouseReleased(MouseEvent e) { 233 PSheet sh = findSheet(); 234 235 if (sh != null) { 236 sh.mousePressed(e); 237 } 238 } 239 240 public AccessibleContext getAccessibleContext() { 241 242 if( null == accessibleContext ) { 243 accessibleContext = new AccessibleJComponent() { 244 public AccessibleRole getAccessibleRole() { 245 return AccessibleRole.SWING_COMPONENT; 246 } 247 }; 248 249 accessibleContext.setAccessibleName( NbBundle.getMessage(DescriptionComponent.class, "ACS_Description") ); 250 accessibleContext.setAccessibleDescription( NbBundle.getMessage(DescriptionComponent.class, "ACSD_Description") ); 251 } 252 253 return accessibleContext; 254 } 255 } 256 | Popular Tags |