|
What this is
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-2003 Sun * Microsystems, Inc. All Rights Reserved. */ package org.netbeans.modules.form.palette; import java.awt.*; import java.awt.event.MouseListener; import javax.swing.*; import javax.swing.event.ListSelectionListener; import javax.swing.border.Border; import java.util.*; import java.util.List; import org.openide.nodes.Node; /** * A panel that presents palette categories using CategoryList * components. Just one - selected - category is shown at a time. * * @author Tomas Pavek */ class CategoryPresentPanel extends JPanel { /** List of components (CategoryList) showing content of categories. * We keep this list because CategoryList components are created lazily. */ private List categoryComponents = new ArrayList(); /** Whether to show names of items in categories (passed to CategoryList). */ private boolean showNames; /** Size of icons shown for items in categories (passed to CategoryList). */ private int iconSize = CategoryList.BASIC_ICONSIZE; /** External listener attached to each created CategoryList to watch for * selected item. */ private ListSelectionListener selectionListener; /** External mouse listener attached to each created CategoryList to handle * popup menu invocation. */ private MouseListener mouseListener; /** Constructor. */ CategoryPresentPanel(ListSelectionListener selectionListener, MouseListener mouseListener) { setLayout(new CardLayout()); // [having such hardcoded listeners isn't very flexible...] this.selectionListener = selectionListener; this.mouseListener = mouseListener; } // ------- void addCategory(Node node, int index) { if (index < 0) index = categoryComponents.size(); // remember the node - CategoryList will be created when needed categoryComponents.add(index, node); JScrollPane categoryPane = new JScrollPane() { public void setBorder(Border border) {} // keep the border null }; add(categoryPane, Integer.toString(categoryPane.hashCode()), index); } void removeCategory(int index) { remove(index); categoryComponents.remove(index); } void clearCategories() { removeAll(); categoryComponents.clear(); } void showCategory(int index) { Object category = categoryComponents.get(index); if (!(category instanceof CategoryList)) category = createCategoryList(index); // create and add the list ((CardLayout)getLayout()).show( this, Integer.toString(getComponent(index).hashCode())); } boolean getShowNames() { return showNames; } void setShowNames(boolean show) { showNames = show; for (int i=0; i < categoryComponents.size(); i++) { Object category = categoryComponents.get(i); if (category instanceof CategoryList) ((CategoryList)category).setShowNames(show); } } int getIconSize() { return iconSize; } void setIconSize(int size) { iconSize = size; for (int i=0; i < categoryComponents.size(); i++) { Object category = categoryComponents.get(i); if (category instanceof CategoryList) ((CategoryList)category).setIconSize(size); } } int getCategoryCount() { return categoryComponents.size(); } CategoryList getCategoryList(int index) { Object category = categoryComponents.get(index); return category instanceof CategoryList ? (CategoryList) category : createCategoryList(index); } // -------- private CategoryList createCategoryList(int index) { Node node = (Node) categoryComponents.get(index); // create CategoryList component final CategoryList list = new CategoryList(); list.setListData(CPManager.getItemNodes(node)); list.setShowNames(showNames); list.setIconSize(iconSize); list.addListSelectionListener(selectionListener); list.addMouseListener(mouseListener); list.getAccessibleContext().setAccessibleName( java.text.MessageFormat.format( CPManager.getBundle().getString("ACS_PaletteBeansList"), // NOI18N new Object[] { node.getDisplayName() } ) ); list.getAccessibleContext().setAccessibleDescription( CPManager.getBundle().getString("ACSD_PaletteBeansList")); // NOI18N ToolTipManager.sharedInstance().registerComponent(list); // remember the list at given index (instead of category node which // is no longer needed categoryComponents.set(index, list); JScrollPane categoryPane = (JScrollPane) getComponent(index); // Avoid multiple repaints (see issue 39037) categoryPane.setLayout(new ScrollPaneLayout() { public void layoutContainer(Container parent) { // Don't use our code when the list already has been resized if (list.getWidth()<=0) { JScrollPane scrollPane = (JScrollPane)parent; Rectangle availR = scrollPane.getBounds(); Insets insets = scrollPane.getInsets(); Insets vpInsets = scrollPane.getViewport().getInsets(); availR.x = insets.left; availR.y = insets.top; availR.width -= insets.left + insets.right; availR.height -= insets.top + insets.bottom; // Check whether scrollbar is needed JScrollBar scrollBar = scrollPane.getVerticalScrollBar(); if (list.getPreferredHeight(availR.width) > availR.height) { scrollBar.setVisible(true); int scrollBarWidth = Math.max(0, Math.min(scrollBar.getPreferredSize().width, availR.width)); availR.width -= scrollBarWidth; Rectangle vsbR = new Rectangle(availR.x + availR.width, availR.y, scrollBarWidth, availR.height); scrollBar.setBounds(vsbR); } else { scrollBar.setVisible(false); } scrollPane.getViewport().setBounds(availR); } else { super.layoutContainer(parent); } } }); // add the list to scroll pane categoryPane.setViewportView(list); return list; } } |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.