|
What this is
This file is included in the DevDaily.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn
Java by Example" TM.
Other links
The source code
/*
* Sun Public License Notice
*
* The contents of this file are subject to the Sun Public License
* Version 1.0 (the "License"). You may not use this file except in
* compliance with the License. A copy of the License is available at
* http://www.sun.com/
*
* The Original Code is NetBeans. The Initial Developer of the Original
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun
* Microsystems, Inc. All Rights Reserved.
*/
package org.netbeans.modules.project.ui;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyVetoException;
import java.io.IOException;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.SortedSet;
import java.util.StringTokenizer;
import java.util.TreeSet;
import java.lang.reflect.InvocationTargetException;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ProjectUtils;
import org.netbeans.spi.project.ui.RecommendedTemplates;
import org.netbeans.api.project.ProjectInformation;
import org.openide.ErrorManager;
import org.openide.explorer.ExplorerManager;
import org.openide.explorer.view.BeanTreeView;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.Repository;
import org.openide.loaders.DataFolder;
import org.openide.loaders.DataObject;
import org.openide.loaders.DataObjectNotFoundException;
import org.openide.loaders.DataShadow;
import org.openide.nodes.Children;
import org.openide.nodes.FilterNode;
import org.openide.nodes.Node;
import org.openide.util.AsyncGUIJob;
import org.openide.util.NbBundle;
import org.openide.util.Utilities;
/** If you are looking for the non-GUI part of the panel please look
* into new file wizard
*/
/**
* Provides the GUI for the template chooser panel.
* @author Jesse Glick
*/
final class TemplateChooserPanelGUI extends javax.swing.JPanel implements PropertyChangeListener, AsyncGUIJob {
private static final ListCellRenderer PROJECT_CELL_RENDERER = new ProjectCellRenderer();
/** prefered dimmension of the panels */
private static final java.awt.Dimension PREF_DIM = new java.awt.Dimension (500, 340);
// private final String[] recommendedTypes = null;
private final List/**/ listeners = new ArrayList();
//Templates folder root
private FileObject templatesFolder;
//GUI Builder
private TemplatesPanelGUI.Builder builder;
private Project project;
private String category;
private String template;
private boolean isWarmUp = true;
public TemplateChooserPanelGUI() {
this.builder = new FileChooserBuilder ();
initComponents();
setPreferredSize( PREF_DIM );
setName (org.openide.util.NbBundle.getMessage(TemplateChooserPanelGUI.class, "LBL_TemplateChooserPanelGUI_Name")); // NOI18N
Utilities.attachInitJob (this, this);
}
public void readValues (Project p, String category, String template) {
assert p != null : "Project can not be null"; //NOI18N
boolean wf;
synchronized (this) {
this.project = p;
this.category = category;
this.template = template;
wf = this.isWarmUp;
}
if (!wf) {
this.selectProject ( project );
((TemplatesPanelGUI)this.templatesPanel).setSelectedCategoryByName (this.category);
((TemplatesPanelGUI)this.templatesPanel).setSelectedTemplateByName (this.template);
}
}
/** Called from readSettings, to initialize the GUI with proper components
*/
private void initValues( Project p ) {
// Populate the combo box with list of projects
Project openProjects[] = OpenProjectList.getDefault().getOpenProjects();
Arrays.sort( openProjects, OpenProjectList.PROJECT_BY_DISPLAYNAME );
DefaultComboBoxModel projectsModel = new DefaultComboBoxModel( openProjects );
projectsComboBox.setModel( projectsModel );
this.selectProject (p);
}
private void selectProject (Project p) {
if (p != null) {
DefaultComboBoxModel projectsModel = (DefaultComboBoxModel) projectsComboBox.getModel ();
if ( projectsModel.getIndexOf( p ) == -1 ) {
projectsModel.insertElementAt( p, 0 );
}
projectsComboBox.setSelectedItem( p );
}
}
public synchronized void addChangeListener(ChangeListener l) {
listeners.add(l);
}
public synchronized void removeChangeListener(ChangeListener l) {
listeners.remove(l);
}
private void fireChange() {
ChangeEvent e = new ChangeEvent(this);
List templist;
synchronized (this) {
templist = new ArrayList (listeners);
}
Iterator it = templist.iterator();
while (it.hasNext()) {
((ChangeListener)it.next()).stateChanged(e);
}
}
public Project getProject() {
boolean wf;
synchronized (this) {
wf = isWarmUp;
}
if (wf) {
return this.project;
}
else {
return (Project)projectsComboBox.getSelectedItem();
}
}
public FileObject getTemplate() {
return ((TemplatesPanelGUI)this.templatesPanel).getSelectedTemplate ();
}
public void propertyChange(PropertyChangeEvent evt) {
fireChange();
}
public java.awt.Dimension getPreferredSize() {
return PREF_DIM;
}
public String getCategoryName () {
return ((TemplatesPanelGUI)this.templatesPanel).getSelectedCategoryName ();
}
public String getTemplateName () {
return ((TemplatesPanelGUI)this.templatesPanel).getSelectedTemplateName ();
}
public void setCategory (String category) {
((TemplatesPanelGUI)this.templatesPanel).setSelectedCategoryByName (category);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {//GEN-BEGIN:initComponents
java.awt.GridBagConstraints gridBagConstraints;
jLabel1 = new javax.swing.JLabel();
projectsComboBox = new javax.swing.JComboBox();
templatesPanel = new TemplatesPanelGUI (this.builder);
setLayout(new java.awt.GridBagLayout());
jLabel1.setDisplayedMnemonic(org.openide.util.NbBundle.getMessage(TemplateChooserPanelGUI.class, "MNE_TemplateChooserPanelGUI_jLabel1").charAt(0));
jLabel1.setLabelFor(projectsComboBox);
jLabel1.setText(org.openide.util.NbBundle.getMessage(TemplateChooserPanelGUI.class, "LBL_TemplateChooserPanelGUI_jLabel1"));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.insets = new java.awt.Insets(0, 0, 13, 0);
add(jLabel1, gridBagConstraints);
jLabel1.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(TemplateChooserPanelGUI.class, "ACSN_jLabel1"));
jLabel1.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(TemplateChooserPanelGUI.class, "ACSD_jLabel1"));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.insets = new java.awt.Insets(0, 6, 12, 0);
add(projectsComboBox, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
add(templatesPanel, gridBagConstraints);
}//GEN-END:initComponents
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel jLabel1;
private javax.swing.JComboBox projectsComboBox;
private javax.swing.JPanel templatesPanel;
// End of variables declaration//GEN-END:variables
// private static final Comparator NATURAL_NAME_SORT = Collator.getInstance();
private final class TemplateChildren extends Children.Keys/**/ implements ActionListener {
private final DataFolder folder;
TemplateChildren(DataFolder folder) {
this.folder = folder;
}
protected void addNotify() {
super.addNotify();
projectsComboBox.addActionListener( this );
updateKeys();
}
protected void removeNotify() {
setKeys(Collections.EMPTY_SET);
projectsComboBox.removeActionListener( this );
super.removeNotify();
}
private void updateKeys() {
List l = new ArrayList();
DataObject[] kids = folder.getChildren();
for (int i = 0; i < kids.length; i++) {
DataObject d = kids[i];
FileObject prim = d.getPrimaryFile();
if ( acceptTemplate( d, prim ) ) {
// has children?
if (hasChildren ((Project)projectsComboBox.getSelectedItem (), d)) {
l.add(d);
}
}
}
setKeys(l);
}
protected Node[] createNodes(Object key) {
DataFolder d = (DataFolder)key;
DataObject[] chlds = d.getChildren();
int state = 0;
for (int i=0; i 0;
}
public void construct () {
this.templatesFolder = Repository.getDefault().getDefaultFileSystem().findResource("Templates");
((TemplatesPanelGUI)this.templatesPanel).warmUp(this.templatesFolder);
}
public void finished () {
//In the awt
Cursor cursor = null;
try {
Project p;
String c,t;
synchronized (this) {
p = this.project;
c = this.category;
t = this.template;
}
cursor = TemplateChooserPanelGUI.this.getCursor();
TemplateChooserPanelGUI.this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
projectsComboBox.setRenderer( PROJECT_CELL_RENDERER );
initValues( p );
((TemplatesPanelGUI)this.templatesPanel).doFinished (this.templatesFolder, c, t);
} finally {
synchronized (this) {
isWarmUp = false;
}
if (cursor != null) {
this.setCursor (cursor);
}
}
}
}
|