001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.welcome.ui;
043:
044: import java.awt.GridBagConstraints;
045: import java.awt.GridBagLayout;
046: import java.awt.GridLayout;
047: import java.awt.Insets;
048: import javax.swing.JLabel;
049: import javax.swing.JPanel;
050: import org.netbeans.modules.welcome.content.Constants;
051: import org.netbeans.modules.welcome.content.SampleProjectLink;
052: import org.openide.filesystems.FileObject;
053: import org.openide.filesystems.Repository;
054: import org.openide.loaders.DataFolder;
055: import org.openide.loaders.DataObject;
056: import org.openide.loaders.DataObjectNotFoundException;
057: import org.openide.loaders.DataShadow;
058:
059: /**
060: *
061: * @author S. Aubrecht
062: */
063: class Samples extends JPanel implements Constants {
064:
065: /** Creates a new instance of RecentProjects */
066: public Samples() {
067: super (new GridBagLayout());
068: setOpaque(false);
069:
070: createLinks();
071: }
072:
073: protected void addLink(JPanel panel, String category, String title) {
074: SampleProjectLink link = new SampleProjectLink(category, null,
075: title);
076: JPanel inner = new JPanel(new GridBagLayout());
077: inner.setOpaque(false);
078: inner.add(link, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
079: GridBagConstraints.WEST, GridBagConstraints.NONE,
080: new Insets(0, 0, 0, 0), 0, 0));
081: inner.add(new JLabel(), new GridBagConstraints(1, 0, 1, 1, 1.0,
082: 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE,
083: new Insets(0, 0, 0, 0), 0, 0));
084: panel.add(inner);
085: }
086:
087: protected void createLinks() {
088: FileObject root = Repository.getDefault()
089: .getDefaultFileSystem().findResource(
090: "Templates/Project/Samples"); // NOI18N
091: DataFolder df = DataFolder.findFolder(root);
092: DataObject[] children = df.getChildren();
093: JPanel panel = new JPanel(new GridLayout(0, 2, 15, 5));
094: panel.setOpaque(false);
095: for (int i = 0; i < children.length; i++) {
096: try {
097: createLinkForCategory(panel, children[i]);
098: } catch (DataObjectNotFoundException ex) {
099: ex.printStackTrace();
100: }
101: }
102:
103: add(panel, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0,
104: GridBagConstraints.NORTHWEST,
105: GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0),
106: 0, 0)); // NOI18N
107:
108: add(new JLabel(), new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0,
109: GridBagConstraints.CENTER, GridBagConstraints.BOTH,
110: new Insets(0, 0, 0, 0), 0, 0)); // NOI18N
111: }
112:
113: protected void createLinkForCategory(JPanel panel,
114: DataObject categoryDO) throws DataObjectNotFoundException {
115: if (categoryDO instanceof DataShadow) {
116: categoryDO = ((DataShadow) categoryDO).getOriginal();
117: }
118:
119: String category = null;
120:
121: FileObject origFile = categoryDO.getPrimaryFile();
122: if (!origFile.isFolder())
123: return;
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: String label = categoryDO.getNodeDelegate().getDisplayName();
142:
143: addLink(panel, category, label);
144: }
145: }
|