1 19 20 package org.netbeans.modules.welcome.ui; 21 22 import java.awt.GridBagConstraints ; 23 import java.awt.GridBagLayout ; 24 import java.awt.Insets ; 25 import java.awt.event.ActionEvent ; 26 import javax.swing.Action ; 27 import javax.swing.BorderFactory ; 28 import javax.swing.JComponent ; 29 import javax.swing.JLabel ; 30 import javax.swing.JPanel ; 31 import javax.swing.JScrollPane ; 32 import javax.swing.UIManager ; 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 50 public class Samples extends ContentPanel implements Constants { 51 52 protected int row = 0; 53 54 55 public Samples() { 56 super( BundleSupport.getLabel( "Samples" ) ); setOpaque( true ); 58 setBackground( Utils.getColor(DEFAULT_BACKGROUND_COLOR) ); 59 60 setContent( buildContent() ); 61 62 NewProjectButton button = new NewProjectButton(); 63 64 JPanel panel = new JPanel ( new GridBagLayout () ); 65 panel.setOpaque( false ); 66 67 panel.add( button, new GridBagConstraints (0,1,1,1,0.0,0.0, 68 GridBagConstraints.SOUTHWEST,GridBagConstraints.HORIZONTAL, 69 new Insets (5,5,0,5),0,0) ); 70 panel.add( new JLabel (), new GridBagConstraints (1,1,1,1,1.0,0.0, 71 GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL, 72 new Insets (0,0,0,0),0,0) ); 73 74 setBottomContent( panel ); 75 } 76 77 private JComponent buildContent() { 78 JPanel panel = new JPanel ( new GridBagLayout () ); 79 panel.setOpaque( false ); 80 81 createLinks( panel ); 82 83 panel.add( new JLabel (), new GridBagConstraints (0, row++, 1, 1, 0.0, 1.0, 84 GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets (0,0,0,0), 0, 0 ) ); 85 86 JScrollPane 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 panel, String category, String title ) { 95 SampleProjectLink link = new SampleProjectLink( category, null, title ); 96 link.setForeground( Utils.getColor(HEADER_TEXT_COLOR) ); 97 panel.add( link, new GridBagConstraints ( 0, row++, 1, 1, 1.0, 0.0, 98 GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, 99 new Insets (row==1 ? UNDER_HEADER_MARGIN : ROW_MARGIN,TEXT_INSETS_LEFT+3, 100 0,TEXT_INSETS_RIGHT/2+UIManager.getInt("ScrollBar.width")), 0, 0 ) ); } 102 103 protected void createLinks( JPanel panel ) { 104 FileObject root = Repository.getDefault().getDefaultFileSystem().findResource( "Templates/Project/Samples" ); 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 panel, DataObject categoryDO ) throws DataObjectNotFoundException { 117 if( categoryDO instanceof DataShadow ) { 118 categoryDO = ((DataShadow)categoryDO).getOriginal(); 119 } 120 121 String 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/", "" ); 141 142 String 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 ); BundleSupport.setAccessibilityProperties( this, "NewProject" ); setFont( HEADER_FONT ); 152 } 153 154 public void actionPerformed(ActionEvent e) { 155 Action a = Utils.findAction( "Actions/Project/org-netbeans-modules-project-ui-NewProject.instance" ); if( null != a ) { 157 a.actionPerformed( e ); 158 } 159 } 160 } 161 } 162 | Popular Tags |