KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > palette > ui > CategoryDescriptor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package org.netbeans.modules.palette.ui;
22
23 import org.netbeans.modules.palette.Category;
24 import org.netbeans.modules.palette.CategoryListener;
25 import org.netbeans.modules.palette.Item;
26 import org.netbeans.modules.palette.Settings;
27 import org.netbeans.modules.palette.Utils;
28 import org.openide.util.Utilities;
29
30 import javax.swing.*;
31 import javax.swing.border.EmptyBorder JavaDoc;
32 import javax.swing.event.ListSelectionEvent JavaDoc;
33 import javax.swing.event.ListSelectionListener JavaDoc;
34 import java.awt.*;
35 import java.awt.event.MouseListener JavaDoc;
36 import java.awt.event.MouseEvent JavaDoc;
37 import java.awt.event.MouseAdapter JavaDoc;
38
39 /**
40  * A visual component for a single palette category. Contains expand/collapse button
41  * and a list of palette items.
42  *
43  * @author David Kaspar, Jan Stola, S. Aubrecht
44  */

45 class CategoryDescriptor implements CategoryListener {
46     private PalettePanel palettePanel;
47     private Category category;
48     private JPanel wholePanel;
49     private CategoryButton categoryButton;
50     private CategoryList itemsList;
51     private DefaultListModel itemsListModel;
52     private boolean opened;
53     private boolean resetItems = true;
54     private Settings settings;
55
56     CategoryDescriptor( PalettePanel palettePanel, Category category ) {
57         assert palettePanel != null : "No palette panel"; // NOI18N
58
assert category != null : "No category node"; // NOI18N
59
this.palettePanel = palettePanel;
60         this.category = category;
61         this.settings = palettePanel.getSettings();
62         wholePanel = new JPanel() {
63             public void addNotify() {
64                 super.addNotify();
65                 CategoryDescriptor.this.category.addCategoryListener( CategoryDescriptor.this );
66             }
67             public void removeNotify() {
68                 super.removeNotify();
69                 CategoryDescriptor.this.category.removeCategoryListener( CategoryDescriptor.this );
70             }
71         };
72         
73
74         wholePanel.setLayout (new GridBagLayout ());
75         wholePanel.setBorder (new EmptyBorder JavaDoc (0, 0, 0, 0));
76
77         MouseListener JavaDoc listener = createMouseListener();
78
79         categoryButton = new CategoryButton( this, category );
80         categoryButton.addMouseListener (listener);
81         GridBagConstraints gbc = new GridBagConstraints (0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets (1, 0, 0, 0), 0, 0);
82         wholePanel.add (categoryButton, gbc);
83
84         itemsList = new CategoryList( category, palettePanel );
85         itemsList.setModel (itemsListModel = new DefaultListModel ());
86         itemsList.setShowNames(palettePanel.getSettings().getShowItemNames());
87         itemsList.setIconSize(palettePanel.getSettings().getIconSize());
88         itemsList.addMouseListener (listener);
89         itemsList.addListSelectionListener (new ListSelectionListener JavaDoc () {
90             public void valueChanged (ListSelectionEvent JavaDoc e) {
91                 CategoryDescriptor.this.palettePanel.select( CategoryDescriptor.this.category, (Item)itemsList.getSelectedValue ());
92             }
93         });
94         gbc = new GridBagConstraints (0, 1, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets (0, 0, 0, 0), 0, 0);
95         wholePanel.add (itemsList, gbc);
96         wholePanel.setFocusTraversalPolicy( new MyFocusTraversal( this ) );
97         doSetOpened( settings.isExpanded( category ) );
98     }
99
100     private MouseListener JavaDoc createMouseListener() {
101         return new MouseAdapter JavaDoc() {
102             public void mouseClicked(MouseEvent JavaDoc event) {
103                 if (SwingUtilities.isRightMouseButton(event)) {
104                     JComponent comp = (JComponent)event.getSource();
105                     Item item = null;
106                     if (comp instanceof JList) {
107                         JList list = (JList)comp;
108                         Point p = event.getPoint();
109                         int index = list.locationToIndex(p);
110                         if (index >= 0 && !list.getCellBounds(index, index).contains(p)) {
111                             index = -1;
112                         }
113                         if (index >= 0) {
114                             item = (Item)list.getModel().getElementAt(index);
115                         }
116                     }
117                     Action[] actions = null == item ? category.getActions() : item.getActions();
118                     JPopupMenu popup = Utilities.actionsToPopup( actions, getComponent() );
119                     Utils.addCustomizationMenuItems( popup, getPalettePanel().getController(), getPalettePanel().getSettings() );
120                     popup.show(comp, event.getX(), event.getY());
121                 }
122             }
123         };
124     }
125
126     void refresh() {
127         categoryButton.updateProperties();
128         categoryButton.repaint ();
129         if( isOpened() && resetItems ) {
130             computeItems();
131         }
132     }
133     
134     void computeItems() {
135         DefaultListModel newModel = new DefaultListModel();
136         Item[] items = category.getItems();
137         if( items != null ) {
138             for( int i=0; i<items.length; i++ ) {
139                 if( settings.isVisible( items[i] ) ) {
140                     newModel.addElement( items[i] );
141                 }
142             }
143         }
144         itemsListModel = newModel;
145         itemsList.setModel( newModel );
146         resetItems = false;
147     }
148         
149     void resetItems() {
150         resetItems = true;
151     }
152
153     Category getCategory () {
154         return category;
155     }
156
157     boolean isOpened() {
158         return opened;
159     }
160     
161     boolean isSelected() {
162         return categoryButton.isFocusOwner() || itemsList.getSelectedIndex() >= 0;
163     }
164
165     void setSelectedItem( Item item ) {
166         if( itemsList.getSelectedValue () == item ) {
167             return;
168         }
169         if( item == null ) {
170             int selectedIndex = itemsList.getSelectedIndex ();
171             itemsList.removeSelectionInterval( selectedIndex, selectedIndex );
172         } else {
173             itemsList.setSelectedValue( item, true );
174         }
175     }
176
177     void setOpened( boolean b ) {
178         if( opened == b ) {
179             return;
180         }
181         doSetOpened( b );
182         settings.setExpanded( category, b );
183     }
184     
185     private void doSetOpened( boolean b ) {
186         opened = b;
187         if( opened ) {
188             if( resetItems ) {
189                 computeItems();
190             }
191         } else {
192             palettePanel.select( category, null );
193         }
194         itemsList.setVisible( opened );
195         categoryButton.setSelected( opened );
196         if( opened ) {
197             itemsList.ensureIndexIsVisible( 0 );
198         }
199     }
200
201     void setPositionY( int yPosition ) {
202         wholePanel.setLocation( 0, yPosition );
203     }
204
205     JComponent getComponent () {
206         return wholePanel;
207     }
208     
209     int getPreferredHeight( int width ) {
210         return isOpened() ?
211             itemsList.getPreferredHeight( width ) + categoryButton.getPreferredSize().height :
212             categoryButton.getPreferredSize().height;
213     }
214
215     void setWidth( int width ) {
216         wholePanel.setSize( width, wholePanel.getHeight() );
217     }
218
219     void setShowNames( boolean showNames ) {
220         itemsList.setShowNames( showNames );
221     }
222
223     void setIconSize( int iconSize ) {
224         itemsList.setIconSize( iconSize );
225     }
226
227     void setItemWidth( int itemWidth ) {
228         itemsList.setFixedCellWidth( itemWidth );
229     }
230
231     PalettePanel getPalettePanel () {
232         return palettePanel;
233     }
234
235     public void categoryModified( Category category ) {
236         resetItems();
237         palettePanel.refresh ();
238     }
239     
240     CategoryList getList() {
241         return itemsList;
242     }
243     
244     CategoryButton getButton() {
245         return categoryButton;
246     }
247
248     private static class MyFocusTraversal extends FocusTraversalPolicy {
249         private CategoryDescriptor descriptor;
250         public MyFocusTraversal( CategoryDescriptor descriptor ) {
251             this.descriptor = descriptor;
252         }
253         
254         public Component getComponentBefore(Container focusCycleRoot, Component aComponent) {
255             if( focusCycleRoot == descriptor.wholePanel && aComponent == descriptor.categoryButton )
256                 return descriptor.itemsList;
257             return null;
258         }
259
260         public Component getComponentAfter(Container focusCycleRoot, Component aComponent) {
261             if( focusCycleRoot == descriptor.wholePanel && aComponent == descriptor.itemsList )
262                 return descriptor.categoryButton;
263             return null;
264         }
265
266         public Component getLastComponent(Container focusCycleRoot) {
267             if( focusCycleRoot == descriptor.wholePanel )
268                 return descriptor.itemsList;
269             return null;
270         }
271
272         public Component getFirstComponent(Container focusCycleRoot) {
273             if( focusCycleRoot == descriptor.wholePanel )
274                 return descriptor.categoryButton;
275             return null;
276         }
277
278         public Component getDefaultComponent(Container focusCycleRoot) {
279             return getFirstComponent( focusCycleRoot );
280         }
281     }
282 }
283
Popular Tags