KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.swing.Action JavaDoc;
27 import javax.swing.BorderFactory JavaDoc;
28 import javax.swing.JComponent JavaDoc;
29 import javax.swing.JLabel JavaDoc;
30 import javax.swing.JPanel JavaDoc;
31 import javax.swing.JScrollPane JavaDoc;
32 import javax.swing.UIManager JavaDoc;
33 import org.netbeans.modules.welcome.content.ContentPanel;
34 import org.netbeans.modules.welcome.content.BundleSupport;
35 import org.netbeans.modules.welcome.content.Constants;
36 import org.netbeans.modules.welcome.content.LinkButton;
37 import org.netbeans.modules.welcome.content.SampleProjectLink;
38 import org.netbeans.modules.welcome.content.Utils;
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 import org.openide.loaders.DataObjectNotFoundException;
44 import org.openide.loaders.DataShadow;
45
46 /**
47  *
48  * @author S. Aubrecht
49  */

50 public class Samples extends ContentPanel implements Constants {
51
52     protected int row = 0;
53
54     /** Creates a new instance of RecentProjects */
55     public Samples() {
56         super( BundleSupport.getLabel( "Samples" ) ); // NOI18N
57
setOpaque( true );
58         setBackground( Utils.getColor(DEFAULT_BACKGROUND_COLOR) );
59         
60         setContent( buildContent() );
61
62         NewProjectButton button = new NewProjectButton();
63
64         JPanel JavaDoc panel = new JPanel JavaDoc( new GridBagLayout JavaDoc() );
65         panel.setOpaque( false );
66
67         panel.add( button, new GridBagConstraints JavaDoc(0,1,1,1,0.0,0.0,
68                 GridBagConstraints.SOUTHWEST,GridBagConstraints.HORIZONTAL,
69                 new Insets JavaDoc(5,5,0,5),0,0) );
70         panel.add( new JLabel JavaDoc(), new GridBagConstraints JavaDoc(1,1,1,1,1.0,0.0,
71                 GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,
72                 new Insets JavaDoc(0,0,0,0),0,0) );
73
74         setBottomContent( panel );
75     }
76     
77     private JComponent JavaDoc buildContent() {
78         JPanel JavaDoc panel = new JPanel JavaDoc( new GridBagLayout JavaDoc() );
79         panel.setOpaque( false );
80
81         createLinks( panel );
82
83         panel.add( new JLabel JavaDoc(), new GridBagConstraints JavaDoc(0, row++, 1, 1, 0.0, 1.0,
84                 GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets JavaDoc(0,0,0,0), 0, 0 ) );
85
86         JScrollPane JavaDoc scroll = new RelativeSizeScrollPane( panel, 0.30f, 30 );
87         scroll.getViewport().setOpaque( false );
88         scroll.setOpaque( false );
89         scroll.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );
90         scroll.setBorder( BorderFactory.createEmptyBorder() );
91         return scroll;
92     }
93
94     protected void addLink( JPanel JavaDoc panel, String JavaDoc category, String JavaDoc title ) {
95         SampleProjectLink link = new SampleProjectLink( category, null, title );
96         link.setForeground( Utils.getColor(HEADER_TEXT_COLOR) );
97         panel.add( link, new GridBagConstraints JavaDoc( 0, row++, 1, 1, 1.0, 0.0,
98                 GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
99                 new Insets JavaDoc(row==1 ? UNDER_HEADER_MARGIN : ROW_MARGIN,TEXT_INSETS_LEFT+3,
100                 0,TEXT_INSETS_RIGHT/2+UIManager.getInt("ScrollBar.width")), 0, 0 ) ); // NOI18N
101
}
102
103     protected void createLinks( JPanel JavaDoc panel ) {
104         FileObject root = Repository.getDefault().getDefaultFileSystem().findResource( "Templates/Project/Samples" ); // NOI18N
105
DataFolder df = DataFolder.findFolder( root );
106         DataObject[] children = df.getChildren();
107         for( int i=0; i<children.length; i++ ) {
108             try {
109                 createLinkForCategory( panel, children[i] );
110             } catch (DataObjectNotFoundException ex) {
111                 ex.printStackTrace();
112             }
113         }
114     }
115
116     protected void createLinkForCategory( JPanel JavaDoc panel, DataObject categoryDO ) throws DataObjectNotFoundException {
117         if( categoryDO instanceof DataShadow ) {
118             categoryDO = ((DataShadow)categoryDO).getOriginal();
119         }
120
121         String JavaDoc category = null;
122
123         FileObject origFile = categoryDO.getPrimaryFile();
124         DataFolder df = DataFolder.findFolder( origFile );
125         if( null != df ) {
126             DataObject[] categoryChildren = df.getChildren();
127             boolean hasSubFoldersOnly = true;
128             for( int i=0; i<categoryChildren.length; i++ ) {
129                 if( !categoryChildren[i].getPrimaryFile().isFolder() ) {
130                     hasSubFoldersOnly = false;
131                     break;
132                 }
133             }
134             if( hasSubFoldersOnly && categoryChildren.length > 0 ) {
135                 origFile = categoryChildren[0].getPrimaryFile();
136             }
137         }
138         category = origFile.getPath();
139         category = category.replaceFirst( "Templates/Project/", "" ); // NOI18N // NOI18N
140

141
142         String JavaDoc label = categoryDO.getNodeDelegate().getDisplayName();
143
144         addLink( panel, category, label );
145     }
146
147     private static class NewProjectButton extends LinkButton {
148         public NewProjectButton() {
149             super( BundleSupport.getLabel( "NewProject" ), false ); // NOI18N
150
BundleSupport.setAccessibilityProperties( this, "NewProject" ); //NOI18N
151
setFont( HEADER_FONT );
152         }
153
154         public void actionPerformed(ActionEvent JavaDoc e) {
155             Action JavaDoc a = Utils.findAction( "Actions/Project/org-netbeans-modules-project-ui-NewProject.instance" ); // NOI18N
156
if( null != a ) {
157                 a.actionPerformed( e );
158             }
159         }
160     }
161 }
162
Popular Tags