/*
* 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-2005 Sun
* Microsystems, Inc. All Rights Reserved.
*/
package org.netbeans.spi.project.ui.support;
import java.awt.Dialog;
import java.awt.Image;
import java.awt.event.ActionListener;
import javax.swing.JComponent;
import javax.swing.JPanel;
import org.netbeans.modules.project.uiapi.CategoryModel;
import org.netbeans.modules.project.uiapi.CategoryView;
import org.netbeans.modules.project.uiapi.CustomizerDialog;
import org.netbeans.modules.project.uiapi.CustomizerPane;
/** Support for creating dialogs which can be used as project
* customizers. The dialog may display multiple panels or categories.
* @see org.netbeans.spi.project.ui.CustomizerProvider
*
* @author Petr Hrebejk
*/
public final class ProjectCustomizer {
/** Factory/Namespace class only. */
private ProjectCustomizer() {
}
/** Creates standard which can be used for implementation
* of {@link org.netbeans.spi.project.ui.CustomizerProvider}. You don't need
* to call pack()
method on the dialog. The resulting dialog will
* be non-modal.
* Call show()
on the dialog to make it visible. If you wnat the dialog to be
* closed after user presses the "OK" button you have to call hide() and dispose() on it.
* (Usually in the actionPerformed(...)
method of the listener
* you provided as a parameter. In case of the click on the "Cancel" button
* the dialog will be closed automatically.
* @param categories array of descriptions of categories to be shown in
* the dialog.
* @param componentProvider creator of GUI components for categories in the
* customizer dialog.
* @param preselectedCategory name of one of the supplied categories or null.
* Category with given name will be selected. If null
* or if the category of given name does not exist the first category will
* be selected.
* @param okOptionListener listener which will be notified when the user presses
* the OK button.
* @return standard project customizer dialog.
*/
public static Dialog createCustomizerDialog( Category[] categories,
CategoryComponentProvider componentProvider,
String preselectedCategory,
ActionListener okOptionListener ) {
JPanel innerPane = createCustomizerPane( categories, componentProvider, preselectedCategory );
Dialog dialog = CustomizerDialog.createDialog( okOptionListener, innerPane );
return dialog;
}
/** Creates standard innerPane for customizer dialog.
*/
private static JPanel createCustomizerPane( Category[] categories,
CategoryComponentProvider componentProvider,
String preselectedCategory ) {
CategoryModel categoryModel = new CategoryModel( categories );
JPanel categoryView = new CategoryView( categoryModel );
JPanel customizerPane = new CustomizerPane( categoryView, categoryModel, componentProvider );
if ( preselectedCategory == null ) {
preselectedCategory = categories[0].getName();
}
Category c = categoryModel.getCategory( preselectedCategory );
if ( c != null ) {
categoryModel.setCurrentCategory( c );
}
return customizerPane;
}
/** Provides components for categories.
*/
public static interface CategoryComponentProvider {
/** Creates component which has to be shown for given category.
* @param category The Category
* @return UI component for category customization
*/
JComponent create( Category category );
}
/** Describes category of properties to be customized by given component
*/
public static final class Category {
private String name;
private String displayName;
private Image icon;
private Category[] subcategories;
/** Private constructor. See the factory method.
*/
private Category( String name,
String displayName,
Image icon,
Category[] subcategories ) {
this.name = name;
this.displayName = displayName;
this.icon = icon;
this.subcategories = subcategories;
}
/** Factory method which creates new category description.
* @param name Prograatic name of the category
* @param displayName Name to be shown to the user
* @param icon Icon for given category. Will use default icon if null.
* @param subcategories Subcategories to be shown under given category.
* Category won't be expandable if null or empty array.
*/
public static Category create( String name,
String displayName,
Image icon,
Category[] subcategories ) {
return new Category( name, displayName, icon, subcategories );
}
// Public methods ------------------------------------------------------
/** Gets programmatic name of given category.
* @return Programmatic name of the category
*/
public String getName() {
return this.name;
}
/** Gets display name of given category.
* @return Display name of the category
*/
public String getDisplayName() {
return this.displayName;
}
/** Gets icon of given category.
* @return Icon name of the category or null
*/
public Image getIcon() {
return this.icon;
}
/** Gets subcategories of given category.
* @return Subcategories of the category or null
*/
public Category[] getSubcategories() {
return this.subcategories;
}
}
}