1 19 20 package org.netbeans.modules.welcome.content; 21 22 import java.awt.Component ; 23 import java.awt.Graphics ; 24 import java.awt.Graphics2D ; 25 import java.awt.GridBagConstraints ; 26 import java.awt.GridBagLayout ; 27 import java.awt.Insets ; 28 import java.awt.KeyboardFocusManager ; 29 import java.awt.event.MouseEvent ; 30 import java.awt.event.MouseListener ; 31 import java.beans.PropertyChangeEvent ; 32 import java.beans.PropertyChangeListener ; 33 import javax.swing.ImageIcon ; 34 import javax.swing.JComponent ; 35 import javax.swing.JLabel ; 36 import javax.swing.JPanel ; 37 import javax.swing.SwingUtilities ; 38 import org.netbeans.modules.welcome.WelcomeComponent; 39 import org.openide.util.Utilities; 40 import org.openide.util.WeakListeners; 41 import org.openide.windows.TopComponent; 42 import org.openide.windows.WindowManager; 43 44 48 public class ContentPanel extends JPanel implements Constants, PropertyChangeListener , MouseListener { 49 50 private JLabel lblTitle; 51 private JComponent content; 52 private JComponent bottomContent; 53 54 protected final ImageIcon hTopLeftInFocus = new ImageIcon ( Utilities.loadImage( SEL_HEADER_TOP_LEFT_IMAGE ) ); 55 protected final ImageIcon hTopRightInFocus = new ImageIcon ( Utilities.loadImage( SEL_HEADER_TOP_RIGHT_IMAGE ) ); 56 protected final ImageIcon hBottomLeftInFocus = new ImageIcon ( Utilities.loadImage( SEL_HEADER_BOTTOM_LEFT_IMAGE ) ); 57 protected final ImageIcon hBottomRightInFocus = new ImageIcon ( Utilities.loadImage( SEL_HEADER_BOTTOM_RIGHT_IMAGE ) ); 58 protected final ImageIcon hTopInFocus = new ImageIcon ( Utilities.loadImage( SEL_HEADER_TOP_IMAGE ) ); 59 protected final ImageIcon hBottomInFocus = new ImageIcon ( Utilities.loadImage( SEL_HEADER_BOTTOM_IMAGE ) ); 60 protected final ImageIcon hLeftInFocus = new ImageIcon ( Utilities.loadImage( SEL_HEADER_LEFT_IMAGE ) ); 61 protected final ImageIcon hRightInFocus = new ImageIcon ( Utilities.loadImage( SEL_HEADER_RIGHT_IMAGE ) ); 62 63 protected final ImageIcon leftSideInFocus = new ImageIcon ( Utilities.loadImage( SEL_LEFT_SIDE_IMAGE ) ); 64 protected final ImageIcon rightSideInFocus = new ImageIcon ( Utilities.loadImage( SEL_RIGHT_SIDE_IMAGE ) ); 65 66 protected final ImageIcon fTopLeftInFocus = new ImageIcon ( Utilities.loadImage( SEL_FOOTER_TOP_LEFT_IMAGE ) ); 67 protected final ImageIcon fTopRightInFocus = new ImageIcon ( Utilities.loadImage( SEL_FOOTER_TOP_RIGHT_IMAGE ) ); 68 protected final ImageIcon fBottomLeftInFocus = new ImageIcon ( Utilities.loadImage( SEL_FOOTER_BOTTOM_LEFT_IMAGE ) ); 69 protected final ImageIcon fBottomRightInFocus = new ImageIcon ( Utilities.loadImage( SEL_FOOTER_BOTTOM_RIGHT_IMAGE ) ); 70 protected final ImageIcon fTopInFocus = new ImageIcon ( Utilities.loadImage( SEL_FOOTER_TOP_IMAGE ) ); 71 protected final ImageIcon fBottomInFocus = new ImageIcon ( Utilities.loadImage( SEL_FOOTER_BOTTOM_IMAGE ) ); 72 protected final ImageIcon fLeftInFocus = new ImageIcon ( Utilities.loadImage( SEL_FOOTER_LEFT_IMAGE ) ); 73 protected final ImageIcon fRightInFocus = new ImageIcon ( Utilities.loadImage( SEL_FOOTER_RIGHT_IMAGE ) ); 74 75 protected final ImageIcon hTopLeftDeselect = new ImageIcon ( Utilities.loadImage( DESEL_HEADER_TOP_LEFT_IMAGE ) ); 76 protected final ImageIcon hTopRightDeselect = new ImageIcon ( Utilities.loadImage( DESEL_HEADER_TOP_RIGHT_IMAGE ) ); 77 protected final ImageIcon hBottomLeftDeselect = new ImageIcon ( Utilities.loadImage( DESEL_HEADER_BOTTOM_LEFT_IMAGE ) ); 78 protected final ImageIcon hBottomRightDeselect = new ImageIcon ( Utilities.loadImage( DESEL_HEADER_BOTTOM_RIGHT_IMAGE ) ); 79 protected final ImageIcon hTopDeselect = new ImageIcon ( Utilities.loadImage( DESEL_HEADER_TOP_IMAGE ) ); 80 protected final ImageIcon hBottomDeselect = new ImageIcon ( Utilities.loadImage( DESEL_HEADER_BOTTOM_IMAGE ) ); 81 protected final ImageIcon hLeftDeselect = new ImageIcon ( Utilities.loadImage( DESEL_HEADER_LEFT_IMAGE ) ); 82 protected final ImageIcon hRightDeselect = new ImageIcon ( Utilities.loadImage( DESEL_HEADER_RIGHT_IMAGE ) ); 83 84 protected final ImageIcon leftSideDeselect = new ImageIcon ( Utilities.loadImage( DESEL_LEFT_SIDE_IMAGE ) ); 85 protected final ImageIcon rightSideDeselect = new ImageIcon ( Utilities.loadImage( DESEL_RIGHT_SIDE_IMAGE ) ); 86 87 protected final ImageIcon fTopLeftDeselect = new ImageIcon ( Utilities.loadImage( DESEL_FOOTER_TOP_LEFT_IMAGE ) ); 88 protected final ImageIcon fTopRightDeselect = new ImageIcon ( Utilities.loadImage( DESEL_FOOTER_TOP_RIGHT_IMAGE ) ); 89 protected final ImageIcon fBottomLeftDeselect = new ImageIcon ( Utilities.loadImage( DESEL_FOOTER_BOTTOM_LEFT_IMAGE ) ); 90 protected final ImageIcon fBottomRightDeselect = new ImageIcon ( Utilities.loadImage( DESEL_FOOTER_BOTTOM_RIGHT_IMAGE ) ); 91 protected final ImageIcon fTopDeselect = new ImageIcon ( Utilities.loadImage( DESEL_FOOTER_TOP_IMAGE ) ); 92 protected final ImageIcon fBottomDeselect = new ImageIcon ( Utilities.loadImage( DESEL_FOOTER_BOTTOM_IMAGE ) ); 93 protected final ImageIcon fLeftDeselect = new ImageIcon ( Utilities.loadImage( DESEL_FOOTER_LEFT_IMAGE ) ); 94 protected final ImageIcon fRightDeselect = new ImageIcon ( Utilities.loadImage( DESEL_FOOTER_RIGHT_IMAGE ) ); 95 96 private boolean focusedBorder = false; 97 98 public ContentPanel( String title ) { 99 super( new GridBagLayout () ); 100 lblTitle = new JLabel ( title ); 101 lblTitle.setFont( HEADER_FONT ); 102 lblTitle.setForeground( Utils.getColor(HEADER_TEXT_COLOR) ); 103 lblTitle.setHorizontalAlignment( JLabel.LEFT ); 104 lblTitle.setOpaque( false ); 105 int vertFill = 3; 106 if( lblTitle.getPreferredSize().height+2*vertFill < hTopInFocus.getIconHeight()+hBottomInFocus.getIconHeight() ) { 107 vertFill = (hTopInFocus.getIconHeight()+hBottomInFocus.getIconHeight()-lblTitle.getPreferredSize().height)/2; 108 } 109 add( lblTitle, new GridBagConstraints ( 0,0,1,1,1.0,0.0, 110 GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL, 111 new Insets (vertFill,TEXT_INSETS_LEFT,vertFill,TEXT_INSETS_RIGHT),0,0) ); 112 113 setBackground( Utils.getColor(DEFAULT_BACKGROUND_COLOR) ); 114 setOpaque( false ); 115 KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager(); 116 kfm.addPropertyChangeListener( WeakListeners.propertyChange( this, kfm ) ); 117 118 addMouseListener( this ); 119 } 120 121 public void setContent( JComponent c ) { 122 if( null != content ) { 123 remove( content ); 124 content.removeMouseListener( this ); 125 } 126 this.content = c; 127 if( null != content ) { 128 add( content, new GridBagConstraints ( 0,1,1,1,1.0,1.0, 129 GridBagConstraints.CENTER,GridBagConstraints.BOTH, 130 new Insets (0,3,0,3),0,0) ); 131 content.addMouseListener( this ); 132 } 133 } 134 135 public void setBottomContent( JComponent c ) { 136 bottomContent = c; 137 if( null != bottomContent ) { 138 139 int topFill = 5; 140 int bottomFill = 5+BOTTOM_MARGIN; 141 int imageHeight = fTopInFocus.getIconHeight()+fBottomInFocus.getIconHeight(); 142 int prefHeight = bottomContent.getPreferredSize().height; 143 if( prefHeight+topFill+bottomFill < imageHeight ) { 144 topFill = (imageHeight-prefHeight)/2; 145 bottomFill = (imageHeight-prefHeight)/2 + (imageHeight-prefHeight)%2; 146 } 147 add( bottomContent, new GridBagConstraints ( 0,2,1,1,1.0,0.0, 148 GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL, 149 new Insets (topFill,TEXT_INSETS_LEFT,bottomFill,TEXT_INSETS_RIGHT),0,0) ); 150 151 bottomContent.addMouseListener( this ); 152 } 153 } 154 155 protected void paintComponent(Graphics g) { 156 Graphics2D g2 = Utils.prepareGraphics( g ); 157 g2.setColor( Utils.getColor(DEFAULT_BACKGROUND_COLOR) ); 158 int header = getHeaderContentHeight(); 159 int footer = getFooterHeight(); 160 g2.fillRect( 0, header, getWidth(), getHeight()-header-footer ); 161 paintTop( g2 ); 162 paintBottom( g2 ); 163 paintSides( g2 ); 164 } 165 166 private void paintTop(Graphics2D g2) { 167 int width = getWidth(); 168 int contentHeight = getHeaderContentHeight(); 169 170 ImageIcon hTop = focusedBorder ? hTopInFocus : hTopDeselect; 171 ImageIcon hBottom = focusedBorder ? hBottomInFocus : hBottomDeselect; 172 ImageIcon hTopLeft = focusedBorder ? hTopLeftInFocus : hTopLeftDeselect; 173 ImageIcon hBottomLeft = focusedBorder ? hBottomLeftInFocus : hBottomLeftDeselect; 174 ImageIcon hTopRight = focusedBorder ? hTopRightInFocus : hTopRightDeselect; 175 ImageIcon hBottomRight = focusedBorder ? hBottomRightInFocus : hBottomRightDeselect; 176 ImageIcon hRight = focusedBorder ? hRightInFocus : hRightDeselect; 177 ImageIcon hLeft = focusedBorder ? hLeftInFocus : hLeftDeselect; 178 179 int imageHeight = hTop.getIconHeight()+hBottom.getIconHeight(); 180 int headerHeight = getHeaderContentHeight(); 181 int fillHeight = 0; 182 if( imageHeight > contentHeight ) { 183 headerHeight = imageHeight; 184 } else { 185 fillHeight = headerHeight - imageHeight; 186 } 187 g2.drawImage( hTopLeft.getImage(), 0, 0, null ); 189 g2.drawImage( hBottomLeft.getImage(), 0, headerHeight-hBottomLeft.getIconHeight(), null ); 190 if( fillHeight > 0 ) { 191 g2.drawImage( hLeft.getImage(), 0, hTopLeft.getIconHeight(), hLeft.getIconWidth(), fillHeight, null ); 192 } 193 194 g2.drawImage( hTopRight.getImage(), width-hTopRight.getIconWidth(), 0, null ); 196 g2.drawImage( hBottomRight.getImage(), width-hBottomRight.getIconWidth(), headerHeight-hBottomRight.getIconHeight(), null ); 197 if( fillHeight > 0 ) { 198 g2.drawImage( hRight.getImage(), width-hBottomRight.getIconWidth(), hTopRight.getIconHeight(), hRight.getIconWidth(), fillHeight, null ); 199 } 200 201 g2.drawImage( hTop.getImage(), hTopLeft.getIconWidth(), 0, width-hTopLeft.getIconWidth()-hTopRight.getIconWidth(), hTop.getIconHeight(), null ); 203 204 g2.drawImage( hBottom.getImage(), hBottomLeft.getIconWidth(), headerHeight-hBottom.getIconHeight(), width-hBottomLeft.getIconWidth()-hBottomRight.getIconWidth(), hBottom.getIconHeight(), null ); 206 207 if( fillHeight > 0 ) { 209 g2.setColor( Utils.getColor(HEADER_FILL_COLOR) ); 210 g2.fillRect( hLeft.getIconWidth(), hTopLeft.getIconHeight(), width-hLeft.getIconWidth()-hRight.getIconWidth(), fillHeight ); 211 } 212 } 213 214 private void paintBottom(Graphics2D g2) { 215 int width = getWidth(); 216 int height = getHeight(); 217 218 ImageIcon fTop = focusedBorder ? fTopInFocus : fTopDeselect; 219 ImageIcon fBottom = focusedBorder ? fBottomInFocus : fBottomDeselect; 220 ImageIcon fTopLeft = focusedBorder ? fTopLeftInFocus : fTopLeftDeselect; 221 ImageIcon fBottomLeft = focusedBorder ? fBottomLeftInFocus : fBottomLeftDeselect; 222 ImageIcon fTopRight = focusedBorder ? fTopRightInFocus : fTopRightDeselect; 223 ImageIcon fBottomRight = focusedBorder ? fBottomRightInFocus : fBottomRightDeselect; 224 ImageIcon fRight = focusedBorder ? fRightInFocus : fRightDeselect; 225 ImageIcon fLeft = focusedBorder ? fLeftInFocus : fLeftDeselect; 226 227 int contentHeight = getFooterContentHeight(); 228 int imageHeight = null == bottomContent ? fBottom.getIconHeight() : fTop.getIconHeight()+fBottom.getIconHeight(); 229 int footerHeight = getFooterContentHeight(); 230 int fillHeight = null == bottomContent ? 0 : 1; 231 if( imageHeight > contentHeight ) { 232 footerHeight = imageHeight; 233 } else { 234 fillHeight = footerHeight - imageHeight; 235 } 236 g2.drawImage( fBottomLeft.getImage(), 0, height-fBottomLeft.getIconHeight(), null ); 238 if( fillHeight > 0 ) { 239 g2.drawImage( fTopLeft.getImage(), 0, height-footerHeight, null ); 240 g2.drawImage( fLeft.getImage(), 0, height-footerHeight+fTopLeft.getIconHeight(), fLeft.getIconWidth(), fillHeight, null ); 241 } 242 243 g2.drawImage( fBottomRight.getImage(), width-fTopRight.getIconWidth(), height-fBottomRight.getIconHeight(), null ); 245 if( fillHeight > 0 ) { 246 g2.drawImage( fTopRight.getImage(), width-fTopRight.getIconWidth(), height-footerHeight, null ); 247 g2.drawImage( fRight.getImage(), width-fRight.getIconWidth(), height-footerHeight+fTopRight.getIconHeight(), fRight.getIconWidth(), fillHeight, null ); 248 } 249 250 g2.drawImage( fBottom.getImage(), fBottomLeft.getIconWidth(), height-fBottom.getIconHeight(), width-fBottomLeft.getIconWidth()-fBottomRight.getIconWidth(), fBottom.getIconHeight(), null ); 252 253 if( fillHeight > 0 ) { 255 g2.setColor( Utils.getColor(FOOTER_FILL_COLOR) ); 256 g2.fillRect( fLeft.getIconWidth(), height-footerHeight+fTop.getIconHeight(), width-fLeft.getIconWidth()-fRight.getIconWidth(), fillHeight ); 257 258 g2.drawImage( fTop.getImage(), fTopLeft.getIconWidth(), height-footerHeight, width-fTopLeft.getIconWidth()-fTopRight.getIconWidth(), fTop.getIconHeight(), null ); 260 } 261 } 262 263 private void paintSides(Graphics2D g2) { 264 ImageIcon leftSide = focusedBorder ? leftSideInFocus : leftSideDeselect; 265 ImageIcon rightSide = focusedBorder ? rightSideInFocus : rightSideDeselect; 266 267 g2.drawImage( leftSide.getImage(), 0, getHeaderHeight(), 268 leftSide.getIconWidth(), getHeight()-getHeaderHeight()-getFooterHeight(), null ); 269 g2.drawImage( rightSide.getImage(), getWidth()-rightSide.getIconWidth(), getHeaderHeight(), 270 rightSide.getIconWidth(), getHeight()-getHeaderHeight()-getFooterHeight(), null ); 271 } 272 273 private int getHeaderContentHeight() { 274 Insets insets = lblTitle.getInsets(); 275 return lblTitle.getHeight()+insets.top+insets.bottom; 276 } 277 278 private int getHeaderHeight() { 279 return Math.max( getHeaderContentHeight(), hTopInFocus.getIconHeight()+hBottomInFocus.getIconHeight() ); 280 } 281 282 private int getFooterContentHeight() { 283 if( null == bottomContent ) 284 return 0; 285 GridBagConstraints constr = ((GridBagLayout )getLayout()).getConstraints( bottomContent ); 286 return bottomContent.getHeight()+constr.insets.top+constr.insets.bottom; 287 } 288 289 private int getFooterHeight() { 290 if( null == bottomContent ) 291 return fBottomInFocus.getIconHeight(); 292 return Math.max( getFooterContentHeight(), fTopInFocus.getIconHeight()+fBottomInFocus.getIconHeight() ); 293 } 294 295 private static Component lastFocusOwner = null; 296 private boolean isFocusOwner = false; 297 public void propertyChange( PropertyChangeEvent evt ) { 298 if( "focusOwner".equals( evt.getPropertyName() ) && evt.getNewValue() instanceof Component ) { Component focusOwner = (Component )evt.getNewValue(); 300 ContentPanel focusAncestor = (ContentPanel)SwingUtilities.getAncestorOfClass( ContentPanel.class, focusOwner ); 301 isFocusOwner = null != focusAncestor && ContentPanel.this == focusAncestor; 302 if( isFocusOwner ) { 303 lastFocusOwner = focusOwner; 304 } 305 if( isFocusOwner != focusedBorder ) { 306 focusedBorder = isFocusOwner; 307 lblTitle.setForeground( Utils.getColor(focusedBorder ? SEL_HEADER_TEXT_COLOR : HEADER_TEXT_COLOR) ); 308 getParent().repaint(); 309 } 310 } 311 } 312 313 public void switchFocus() { 314 if( isFocusOwner ) 315 return; 316 if( null == lastFocusOwner || !lastFocusOwner.isShowing() ) { 317 KeyboardFocusManager.getCurrentKeyboardFocusManager().focusNextComponent( this ); 318 } else { 319 lastFocusOwner.requestFocusInWindow(); 320 } 321 } 322 323 protected void requestAttention() { 324 focusedBorder = true; 325 lblTitle.setForeground( Utils.getColor(SEL_HEADER_TEXT_COLOR) ); 326 getParent().repaint(); 327 TopComponent activeTc = WindowManager.getDefault().getRegistry().getActivated(); 329 if( null != activeTc && activeTc.equals( WelcomeComponent.findComp() ) ) { 330 switchFocus(); 331 } 332 } 333 334 public void mouseClicked(MouseEvent e) { 335 } 336 337 public void mousePressed(MouseEvent e) { 338 lastFocusOwner = null; 339 switchFocus(); 340 } 341 342 public void mouseReleased(MouseEvent e) { 343 } 344 345 public void mouseEntered(MouseEvent e) { 346 } 347 348 public void mouseExited(MouseEvent e) { 349 } 350 } 351 | Popular Tags |