KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > welcome > content > ContentPanel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.welcome.content;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.Graphics JavaDoc;
24 import java.awt.Graphics2D JavaDoc;
25 import java.awt.GridBagConstraints JavaDoc;
26 import java.awt.GridBagLayout JavaDoc;
27 import java.awt.Insets JavaDoc;
28 import java.awt.KeyboardFocusManager JavaDoc;
29 import java.awt.event.MouseEvent JavaDoc;
30 import java.awt.event.MouseListener JavaDoc;
31 import java.beans.PropertyChangeEvent JavaDoc;
32 import java.beans.PropertyChangeListener JavaDoc;
33 import javax.swing.ImageIcon JavaDoc;
34 import javax.swing.JComponent JavaDoc;
35 import javax.swing.JLabel JavaDoc;
36 import javax.swing.JPanel JavaDoc;
37 import javax.swing.SwingUtilities JavaDoc;
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 /**
45  *
46  * @author S. Aubrecht
47  */

48 public class ContentPanel extends JPanel JavaDoc implements Constants, PropertyChangeListener JavaDoc, MouseListener JavaDoc {
49     
50     private JLabel JavaDoc lblTitle;
51     private JComponent JavaDoc content;
52     private JComponent JavaDoc bottomContent;
53     
54     protected final ImageIcon JavaDoc hTopLeftInFocus = new ImageIcon JavaDoc( Utilities.loadImage( SEL_HEADER_TOP_LEFT_IMAGE ) );
55     protected final ImageIcon JavaDoc hTopRightInFocus = new ImageIcon JavaDoc( Utilities.loadImage( SEL_HEADER_TOP_RIGHT_IMAGE ) );
56     protected final ImageIcon JavaDoc hBottomLeftInFocus = new ImageIcon JavaDoc( Utilities.loadImage( SEL_HEADER_BOTTOM_LEFT_IMAGE ) );
57     protected final ImageIcon JavaDoc hBottomRightInFocus = new ImageIcon JavaDoc( Utilities.loadImage( SEL_HEADER_BOTTOM_RIGHT_IMAGE ) );
58     protected final ImageIcon JavaDoc hTopInFocus = new ImageIcon JavaDoc( Utilities.loadImage( SEL_HEADER_TOP_IMAGE ) );
59     protected final ImageIcon JavaDoc hBottomInFocus = new ImageIcon JavaDoc( Utilities.loadImage( SEL_HEADER_BOTTOM_IMAGE ) );
60     protected final ImageIcon JavaDoc hLeftInFocus = new ImageIcon JavaDoc( Utilities.loadImage( SEL_HEADER_LEFT_IMAGE ) );
61     protected final ImageIcon JavaDoc hRightInFocus = new ImageIcon JavaDoc( Utilities.loadImage( SEL_HEADER_RIGHT_IMAGE ) );
62
63     protected final ImageIcon JavaDoc leftSideInFocus = new ImageIcon JavaDoc( Utilities.loadImage( SEL_LEFT_SIDE_IMAGE ) );
64     protected final ImageIcon JavaDoc rightSideInFocus = new ImageIcon JavaDoc( Utilities.loadImage( SEL_RIGHT_SIDE_IMAGE ) );
65
66     protected final ImageIcon JavaDoc fTopLeftInFocus = new ImageIcon JavaDoc( Utilities.loadImage( SEL_FOOTER_TOP_LEFT_IMAGE ) );
67     protected final ImageIcon JavaDoc fTopRightInFocus = new ImageIcon JavaDoc( Utilities.loadImage( SEL_FOOTER_TOP_RIGHT_IMAGE ) );
68     protected final ImageIcon JavaDoc fBottomLeftInFocus = new ImageIcon JavaDoc( Utilities.loadImage( SEL_FOOTER_BOTTOM_LEFT_IMAGE ) );
69     protected final ImageIcon JavaDoc fBottomRightInFocus = new ImageIcon JavaDoc( Utilities.loadImage( SEL_FOOTER_BOTTOM_RIGHT_IMAGE ) );
70     protected final ImageIcon JavaDoc fTopInFocus = new ImageIcon JavaDoc( Utilities.loadImage( SEL_FOOTER_TOP_IMAGE ) );
71     protected final ImageIcon JavaDoc fBottomInFocus = new ImageIcon JavaDoc( Utilities.loadImage( SEL_FOOTER_BOTTOM_IMAGE ) );
72     protected final ImageIcon JavaDoc fLeftInFocus = new ImageIcon JavaDoc( Utilities.loadImage( SEL_FOOTER_LEFT_IMAGE ) );
73     protected final ImageIcon JavaDoc fRightInFocus = new ImageIcon JavaDoc( Utilities.loadImage( SEL_FOOTER_RIGHT_IMAGE ) );
74
75     protected final ImageIcon JavaDoc hTopLeftDeselect = new ImageIcon JavaDoc( Utilities.loadImage( DESEL_HEADER_TOP_LEFT_IMAGE ) );
76     protected final ImageIcon JavaDoc hTopRightDeselect = new ImageIcon JavaDoc( Utilities.loadImage( DESEL_HEADER_TOP_RIGHT_IMAGE ) );
77     protected final ImageIcon JavaDoc hBottomLeftDeselect = new ImageIcon JavaDoc( Utilities.loadImage( DESEL_HEADER_BOTTOM_LEFT_IMAGE ) );
78     protected final ImageIcon JavaDoc hBottomRightDeselect = new ImageIcon JavaDoc( Utilities.loadImage( DESEL_HEADER_BOTTOM_RIGHT_IMAGE ) );
79     protected final ImageIcon JavaDoc hTopDeselect = new ImageIcon JavaDoc( Utilities.loadImage( DESEL_HEADER_TOP_IMAGE ) );
80     protected final ImageIcon JavaDoc hBottomDeselect = new ImageIcon JavaDoc( Utilities.loadImage( DESEL_HEADER_BOTTOM_IMAGE ) );
81     protected final ImageIcon JavaDoc hLeftDeselect = new ImageIcon JavaDoc( Utilities.loadImage( DESEL_HEADER_LEFT_IMAGE ) );
82     protected final ImageIcon JavaDoc hRightDeselect = new ImageIcon JavaDoc( Utilities.loadImage( DESEL_HEADER_RIGHT_IMAGE ) );
83
84     protected final ImageIcon JavaDoc leftSideDeselect = new ImageIcon JavaDoc( Utilities.loadImage( DESEL_LEFT_SIDE_IMAGE ) );
85     protected final ImageIcon JavaDoc rightSideDeselect = new ImageIcon JavaDoc( Utilities.loadImage( DESEL_RIGHT_SIDE_IMAGE ) );
86
87     protected final ImageIcon JavaDoc fTopLeftDeselect = new ImageIcon JavaDoc( Utilities.loadImage( DESEL_FOOTER_TOP_LEFT_IMAGE ) );
88     protected final ImageIcon JavaDoc fTopRightDeselect = new ImageIcon JavaDoc( Utilities.loadImage( DESEL_FOOTER_TOP_RIGHT_IMAGE ) );
89     protected final ImageIcon JavaDoc fBottomLeftDeselect = new ImageIcon JavaDoc( Utilities.loadImage( DESEL_FOOTER_BOTTOM_LEFT_IMAGE ) );
90     protected final ImageIcon JavaDoc fBottomRightDeselect = new ImageIcon JavaDoc( Utilities.loadImage( DESEL_FOOTER_BOTTOM_RIGHT_IMAGE ) );
91     protected final ImageIcon JavaDoc fTopDeselect = new ImageIcon JavaDoc( Utilities.loadImage( DESEL_FOOTER_TOP_IMAGE ) );
92     protected final ImageIcon JavaDoc fBottomDeselect = new ImageIcon JavaDoc( Utilities.loadImage( DESEL_FOOTER_BOTTOM_IMAGE ) );
93     protected final ImageIcon JavaDoc fLeftDeselect = new ImageIcon JavaDoc( Utilities.loadImage( DESEL_FOOTER_LEFT_IMAGE ) );
94     protected final ImageIcon JavaDoc fRightDeselect = new ImageIcon JavaDoc( Utilities.loadImage( DESEL_FOOTER_RIGHT_IMAGE ) );
95
96     private boolean focusedBorder = false;
97
98     public ContentPanel( String JavaDoc title ) {
99         super( new GridBagLayout JavaDoc() );
100         lblTitle = new JLabel JavaDoc( 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 JavaDoc( 0,0,1,1,1.0,0.0,
110                 GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,
111                 new Insets JavaDoc(vertFill,TEXT_INSETS_LEFT,vertFill,TEXT_INSETS_RIGHT),0,0) );
112
113         setBackground( Utils.getColor(DEFAULT_BACKGROUND_COLOR) );
114         setOpaque( false );
115         KeyboardFocusManager JavaDoc kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
116         kfm.addPropertyChangeListener( WeakListeners.propertyChange( this, kfm ) );
117
118         addMouseListener( this );
119     }
120     
121     public void setContent( JComponent JavaDoc 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 JavaDoc( 0,1,1,1,1.0,1.0,
129                 GridBagConstraints.CENTER,GridBagConstraints.BOTH,
130                 new Insets JavaDoc(0,3,0,3),0,0) );
131             content.addMouseListener( this );
132         }
133     }
134
135     public void setBottomContent( JComponent JavaDoc 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 JavaDoc( 0,2,1,1,1.0,0.0,
148                 GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,
149                 new Insets JavaDoc(topFill,TEXT_INSETS_LEFT,bottomFill,TEXT_INSETS_RIGHT),0,0) );
150
151             bottomContent.addMouseListener( this );
152         }
153     }
154
155     protected void paintComponent(Graphics JavaDoc g) {
156         Graphics2D JavaDoc 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 JavaDoc g2) {
167         int width = getWidth();
168         int contentHeight = getHeaderContentHeight();
169
170         ImageIcon JavaDoc hTop = focusedBorder ? hTopInFocus : hTopDeselect;
171         ImageIcon JavaDoc hBottom = focusedBorder ? hBottomInFocus : hBottomDeselect;
172         ImageIcon JavaDoc hTopLeft = focusedBorder ? hTopLeftInFocus : hTopLeftDeselect;
173         ImageIcon JavaDoc hBottomLeft = focusedBorder ? hBottomLeftInFocus : hBottomLeftDeselect;
174         ImageIcon JavaDoc hTopRight = focusedBorder ? hTopRightInFocus : hTopRightDeselect;
175         ImageIcon JavaDoc hBottomRight = focusedBorder ? hBottomRightInFocus : hBottomRightDeselect;
176         ImageIcon JavaDoc hRight = focusedBorder ? hRightInFocus : hRightDeselect;
177         ImageIcon JavaDoc 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         //left side
188
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         //right side
195
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         //top
202
g2.drawImage( hTop.getImage(), hTopLeft.getIconWidth(), 0, width-hTopLeft.getIconWidth()-hTopRight.getIconWidth(), hTop.getIconHeight(), null );
203
204         //bottom
205
g2.drawImage( hBottom.getImage(), hBottomLeft.getIconWidth(), headerHeight-hBottom.getIconHeight(), width-hBottomLeft.getIconWidth()-hBottomRight.getIconWidth(), hBottom.getIconHeight(), null );
206
207         //fill
208
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 JavaDoc g2) {
215         int width = getWidth();
216         int height = getHeight();
217
218         ImageIcon JavaDoc fTop = focusedBorder ? fTopInFocus : fTopDeselect;
219         ImageIcon JavaDoc fBottom = focusedBorder ? fBottomInFocus : fBottomDeselect;
220         ImageIcon JavaDoc fTopLeft = focusedBorder ? fTopLeftInFocus : fTopLeftDeselect;
221         ImageIcon JavaDoc fBottomLeft = focusedBorder ? fBottomLeftInFocus : fBottomLeftDeselect;
222         ImageIcon JavaDoc fTopRight = focusedBorder ? fTopRightInFocus : fTopRightDeselect;
223         ImageIcon JavaDoc fBottomRight = focusedBorder ? fBottomRightInFocus : fBottomRightDeselect;
224         ImageIcon JavaDoc fRight = focusedBorder ? fRightInFocus : fRightDeselect;
225         ImageIcon JavaDoc 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         //left side
237
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         //right side
244
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         //bottom
251
g2.drawImage( fBottom.getImage(), fBottomLeft.getIconWidth(), height-fBottom.getIconHeight(), width-fBottomLeft.getIconWidth()-fBottomRight.getIconWidth(), fBottom.getIconHeight(), null );
252
253         //fill
254
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             //top
259
g2.drawImage( fTop.getImage(), fTopLeft.getIconWidth(), height-footerHeight, width-fTopLeft.getIconWidth()-fTopRight.getIconWidth(), fTop.getIconHeight(), null );
260         }
261     }
262
263     private void paintSides(Graphics2D JavaDoc g2) {
264         ImageIcon JavaDoc leftSide = focusedBorder ? leftSideInFocus : leftSideDeselect;
265         ImageIcon JavaDoc 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 JavaDoc 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 JavaDoc constr = ((GridBagLayout JavaDoc)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 JavaDoc lastFocusOwner = null;
296     private boolean isFocusOwner = false;
297     public void propertyChange( PropertyChangeEvent JavaDoc evt ) {
298         if( "focusOwner".equals( evt.getPropertyName() ) && evt.getNewValue() instanceof Component JavaDoc ) { //NOI18N
299
Component JavaDoc focusOwner = (Component JavaDoc)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         //also take input focus when the welcome screen is the active window
328
TopComponent activeTc = WindowManager.getDefault().getRegistry().getActivated();
329         if( null != activeTc && activeTc.equals( WelcomeComponent.findComp() ) ) {
330             switchFocus();
331         }
332     }
333
334     public void mouseClicked(MouseEvent JavaDoc e) {
335     }
336
337     public void mousePressed(MouseEvent JavaDoc e) {
338         lastFocusOwner = null;
339         switchFocus();
340     }
341
342     public void mouseReleased(MouseEvent JavaDoc e) {
343     }
344
345     public void mouseEntered(MouseEvent JavaDoc e) {
346     }
347
348     public void mouseExited(MouseEvent JavaDoc e) {
349     }
350 }
351
Popular Tags