KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > welcome > ui > GetStarted


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.ui;
21
22 import java.awt.GridBagConstraints JavaDoc;
23 import java.awt.GridBagLayout JavaDoc;
24 import java.awt.Insets JavaDoc;
25 import java.awt.event.ActionEvent JavaDoc;
26 import java.lang.reflect.Method JavaDoc;
27 import javax.swing.AbstractAction JavaDoc;
28 import javax.swing.BorderFactory JavaDoc;
29 import javax.swing.JComponent JavaDoc;
30 import javax.swing.JLabel JavaDoc;
31 import javax.swing.JPanel JavaDoc;
32 import javax.swing.JScrollPane JavaDoc;
33 import org.netbeans.modules.welcome.content.BundleSupport;
34 import org.netbeans.modules.welcome.content.ContentPanel;
35 import org.netbeans.modules.welcome.content.ActionButton;
36 import org.netbeans.modules.welcome.content.Utils;
37 import org.openide.cookies.InstanceCookie;
38 import org.openide.cookies.OpenCookie;
39 import org.openide.filesystems.FileObject;
40 import org.openide.filesystems.Repository;
41 import org.openide.loaders.DataFolder;
42 import org.openide.loaders.DataObject;
43
44 /**
45  *
46  * @author S. Aubrecht
47  */

48 public class GetStarted extends ContentPanel {
49
50     private int row;
51
52     /** Creates a new instance of RecentProjects */
53     public GetStarted() {
54         super( BundleSupport.getLabel( "GettingStarted" ) ); // NOI18N
55

56         setContent( buildContent() );
57     }
58
59     private JComponent JavaDoc buildContent() {
60         int row = 0;
61         JPanel JavaDoc panel = new JPanel JavaDoc( new GridBagLayout JavaDoc() );
62         panel.setOpaque( false );
63         FileObject root = Repository.getDefault().getDefaultFileSystem().findResource( "WelcomePage/GettingStartedLinks" ); // NOI18N
64
DataFolder folder = DataFolder.findFolder( root );
65         DataObject[] children = folder.getChildren();
66         for( int i=0; i<children.length; i++ ) {
67             if( children[i].getPrimaryFile().isFolder() ) {
68                 String JavaDoc headerText = children[i].getNodeDelegate().getDisplayName();
69                 JLabel JavaDoc lblTitle = new JLabel JavaDoc( headerText );
70                 lblTitle.setFont( HEADER_FONT );
71                 lblTitle.setForeground( Utils.getColor(SECTION_TEXT_COLOR) );
72                 lblTitle.setBackground( Utils.getColor(SECTION_BACKGROUND_COLOR) );
73                 lblTitle.setHorizontalAlignment( JLabel.LEFT );
74                 lblTitle.setOpaque( true );
75                 lblTitle.setBorder( HEADER_TEXT_BORDER );
76                 panel.add( lblTitle, new GridBagConstraints JavaDoc( 0,row++,1,1,1.0,0.0,
77                     GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL,
78                     new Insets JavaDoc(row==1 ? UNDER_HEADER_MARGIN : SECTION_MARGIN,0,
79                         UNDER_SECTION_MARGIN,1), 0, 0 ) );
80
81                 DataFolder subFolder = DataFolder.findFolder( children[i].getPrimaryFile() );
82                 DataObject[] subFolderChildren = subFolder.getChildren();
83                 for( int j=0; j<subFolderChildren.length; j++ ) {
84                     row = addLink( panel, row, subFolderChildren[j] );
85                 }
86                     
87             } else {
88                 row = addLink( panel, row, children[i] );
89             }
90         }
91
92         panel.add( new JLabel JavaDoc(), new GridBagConstraints JavaDoc(0, row++, 1, 1, 0.0, 1.0,
93                 GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets JavaDoc(0,0,15,0), 0, 0 ) );
94
95         JScrollPane JavaDoc scroll = new RelativeSizeScrollPane( panel, 0.70f, 50 );
96         scroll.getViewport().setOpaque( false );
97         scroll.setOpaque( false );
98         scroll.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );
99         scroll.setBorder( BorderFactory.createEmptyBorder(0,0,20,0) );
100         return scroll;
101     }
102
103     private int addLink( JPanel JavaDoc panel, int row, DataObject dob ) {
104         OpenCookie oc = (OpenCookie)dob.getCookie( InstanceCookie.class );
105         if( null != oc ) {
106             LinkAction la = new LinkAction( dob );
107             ActionButton lb = new ActionButton( la, true, getUrlString( dob ) );
108             lb.getAccessibleContext().setAccessibleName( lb.getText() );
109             lb.getAccessibleContext().setAccessibleDescription(
110                     BundleSupport.getAccessibilityDescription( "GettingStarted", lb.getText() ) ); //NOI18N
111
panel.add( lb, new GridBagConstraints JavaDoc( 0,row++,1,1,1.0,0.0,
112                 GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
113                 new Insets JavaDoc(row==1? UNDER_HEADER_MARGIN : ROW_MARGIN,TEXT_INSETS_LEFT+3,0,2*TEXT_INSETS_RIGHT), 0, 0 ) );
114         }
115         return row;
116     }
117
118     /**
119      * Try to extract the URL from the given DataObject using reflection.
120      * (The DataObject should be URLDataObject in most cases)
121      */

122     private String JavaDoc getUrlString(DataObject dob) {
123         try {
124             Method JavaDoc m = dob.getClass().getDeclaredMethod( "getURLString", new Class JavaDoc[] {} );
125             m.setAccessible( true );
126             Object JavaDoc res = m.invoke( dob );
127             if( null != res ) {
128                 return res.toString();
129             }
130         } catch (Exception JavaDoc ex) {
131             //ignore
132
}
133         return null;
134     }
135
136     private static class LinkAction extends AbstractAction JavaDoc {
137         private DataObject dob;
138         public LinkAction( DataObject dob ) {
139             super( dob.getNodeDelegate().getDisplayName() );
140             this.dob = dob;
141         }
142
143         public void actionPerformed(ActionEvent JavaDoc e) {
144             OpenCookie oc = (OpenCookie)dob.getCookie( OpenCookie.class );
145             if( null != oc )
146                 oc.open();
147         }
148     }
149 }
150
Popular Tags