KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > project > uiapi > CustomizerPane


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 package org.netbeans.modules.project.uiapi;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.GridBagConstraints JavaDoc;
25 import java.awt.Insets JavaDoc;
26 import java.beans.PropertyChangeEvent JavaDoc;
27 import java.beans.PropertyChangeListener JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import javax.swing.BorderFactory JavaDoc;
30 import javax.swing.JComponent JavaDoc;
31 import javax.swing.JPanel JavaDoc;
32 import javax.swing.JTextArea JavaDoc;
33 import javax.swing.UIManager JavaDoc;
34 import org.netbeans.spi.project.ui.support.ProjectCustomizer;
35 import org.openide.util.HelpCtx;
36 import org.openide.util.NbBundle;
37
38 /**
39  *
40  * @author phrebejk
41  */

42 public class CustomizerPane extends JPanel JavaDoc
43         implements HelpCtx.Provider, PropertyChangeListener JavaDoc {
44     
45     public static final String JavaDoc HELP_CTX_PROPERTY = "helpCtxProperty";
46     
47     private Component JavaDoc currentCustomizer;
48     private JTextArea JavaDoc errorMessageValue = new JTextArea JavaDoc();
49     private HelpCtx currentHelpCtx;
50     
51     private GridBagConstraints JavaDoc fillConstraints;
52     private GridBagConstraints JavaDoc errMessConstraints = new GridBagConstraints JavaDoc();
53     
54     private ProjectCustomizer.CategoryComponentProvider componentProvider;
55     
56     private HashMap JavaDoc<ProjectCustomizer.Category, JComponent JavaDoc> panelCache = new HashMap JavaDoc<ProjectCustomizer.Category, JComponent JavaDoc>();
57     
58     //private DialogDescriptor dialogDescriptor;
59

60     /** Creates new form J2SECustomizer */
61     public CustomizerPane( JPanel JavaDoc categoryView, CategoryModel categoryModel, ProjectCustomizer.CategoryComponentProvider componentProvider ) {
62         initComponents();
63         // HelpCtx.setHelpIDString( customizerPanel, "org.netbeans.modules.java.j2seproject.ui.customizer.J2SECustomizer" ); // NOI18N
64
this.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(CustomizerPane.class,"AD_CustomizerPane")); // NOI18N
65
this.componentProvider = componentProvider;
66         fillConstraints = new GridBagConstraints JavaDoc();
67         fillConstraints.gridwidth = GridBagConstraints.REMAINDER;
68         fillConstraints.gridheight = 1;
69         fillConstraints.fill = GridBagConstraints.BOTH;
70         fillConstraints.weightx = 1.0;
71         fillConstraints.weighty = 1.0;
72         categoryModel.addPropertyChangeListener( new CategoryChangeListener() );
73         categoryPanel.add( categoryView, fillConstraints );
74         
75         // init errorMessageValue
76
errorMessageValue.setWrapStyleWord(true);
77         errorMessageValue.setLineWrap(true);
78         errorMessageValue.setBorder(BorderFactory.createEmptyBorder());
79         errorMessageValue.setEditable(false);
80         errorMessageValue.setForeground(UIManager.getColor("nb.errorForeground")); // NOI18N
81
errorMessageValue.setBackground(customizerPanel.getBackground());
82         
83         // put it into under categoryView
84
errMessConstraints = new GridBagConstraints JavaDoc();
85         errMessConstraints.gridx = 0;
86         errMessConstraints.gridy = 1;
87         errMessConstraints.gridwidth = 1;
88         errMessConstraints.gridheight = 1;
89         errMessConstraints.insets = new Insets JavaDoc(12, 0, 0, 0);
90         errMessConstraints.fill = GridBagConstraints.HORIZONTAL;
91         customizerPanel.add(errorMessageValue, errMessConstraints);
92         
93         setCategory( categoryModel.getCurrentCategory() );
94     }
95     
96     /** This method is called from within the constructor to
97      * initialize the form.
98      * WARNING: Do NOT modify this code. The content of this method is
99      * always regenerated by the Form Editor.
100      */

101     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
102
private void initComponents() {
103         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
104
105         jLabel1 = new javax.swing.JLabel JavaDoc();
106         categoryPanel = new javax.swing.JPanel JavaDoc();
107         customizerPanel = new javax.swing.JPanel JavaDoc();
108
109         setLayout(new java.awt.GridBagLayout JavaDoc());
110
111         jLabel1.setLabelFor(categoryPanel);
112         org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(CustomizerPane.class, "LBL_Customizer_Categories")); // NOI18N
113
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
114         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
115         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
116         gridBagConstraints.insets = new java.awt.Insets JavaDoc(8, 11, 0, 0);
117         add(jLabel1, gridBagConstraints);
118
119         categoryPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
120         categoryPanel.setMinimumSize(new java.awt.Dimension JavaDoc(220, 4));
121         categoryPanel.setPreferredSize(new java.awt.Dimension JavaDoc(220, 4));
122         categoryPanel.setLayout(new java.awt.GridBagLayout JavaDoc());
123         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
124         gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
125         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
126         gridBagConstraints.weighty = 1.0;
127         gridBagConstraints.insets = new java.awt.Insets JavaDoc(3, 11, 8, 11);
128         add(categoryPanel, gridBagConstraints);
129
130         customizerPanel.setLayout(new java.awt.GridBagLayout JavaDoc());
131         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
132         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
133         gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
134         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
135         gridBagConstraints.weightx = 1.0;
136         gridBagConstraints.weighty = 1.0;
137         gridBagConstraints.insets = new java.awt.Insets JavaDoc(3, 0, 8, 11);
138         add(customizerPanel, gridBagConstraints);
139     }// </editor-fold>//GEN-END:initComponents
140

141     
142     // Variables declaration - do not modify//GEN-BEGIN:variables
143
private javax.swing.JPanel JavaDoc categoryPanel;
144     private javax.swing.JPanel JavaDoc customizerPanel;
145     private javax.swing.JLabel JavaDoc jLabel1;
146     // End of variables declaration//GEN-END:variables
147

148     
149     public void clearPanelComponentCache() {
150         //should only happen when closign teh customizer..
151
panelCache.clear();
152     }
153     
154     public Dimension JavaDoc getPreferredSize() {
155         if (isPreferredSizeSet()) {
156             return super.getPreferredSize();
157         }
158         Dimension JavaDoc dim = super.getPreferredSize();
159         if (dim == null) {
160             return new Dimension JavaDoc(750, 450);
161         }
162         if (dim.getWidth() < 750 || dim.getHeight() < 450) {
163             return new Dimension JavaDoc(750, 450);
164         }
165         if (dim.getWidth() > 850) {
166             dim.width = 850;
167         }
168         if (dim.getHeight() > 550) {
169             dim.height = 550;
170         }
171         return dim;
172     }
173     
174     // HelpCtx.Provider implementation -----------------------------------------
175

176     public HelpCtx getHelpCtx() {
177         return currentHelpCtx;
178         /*
179         System.out.println("Get Help Ctx");
180         Thread.dumpStack();
181         
182         if ( currentCustomizer != null ) {
183             // System.out.println( "C " + HelpCtx.findHelp( currentCustomizer ) );
184             return HelpCtx.findHelp( currentCustomizer );
185         }
186         /*
187         else {
188             // System.out.println( "P " + HelpCtx.findHelp( currentCustomizer ) );
189             return HelpCtx.findHelp( customizerPanel );
190         }
191               
192         // XXX
193         return null;
194         */

195     }
196     
197     
198     // Private methods ---------------------------------------------------------
199

200     private void setCategory( ProjectCustomizer.Category newCategory) {
201         if ( newCategory == null ) {
202             return;
203         }
204
205         if ( currentCustomizer != null ) {
206             customizerPanel.remove( currentCustomizer );
207         }
208
209         JComponent JavaDoc newCustomizer = panelCache.get(newCategory);
210         if (newCustomizer == null && !panelCache.containsKey(newCustomizer)) {
211             newCustomizer = componentProvider.create( newCategory );
212             panelCache.put(newCategory, newCustomizer);
213         }
214
215         if ( newCustomizer != null ) {
216             Utilities.getCategoryChangeSupport(newCategory).addPropertyChangeListener(this);
217             currentCustomizer = newCustomizer;
218             currentHelpCtx = HelpCtx.findHelp( currentCustomizer );
219             
220             /*
221             if ( currentCustomizer instanceof javax.swing.JComponent ) {
222                 ((javax.swing.JComponent)currentCustomizer).setPreferredSize( new java.awt.Dimension( 600, 0 ) );
223             }
224             */

225             customizerPanel.add( currentCustomizer, fillConstraints );
226             customizerPanel.validate();
227             customizerPanel.repaint();
228             
229             setErrorMessage(newCategory.getErrorMessage());
230             firePropertyChange( HELP_CTX_PROPERTY, null, getHelpCtx() );
231         }
232         else {
233             currentCustomizer = null;
234         }
235
236     }
237
238     private void setErrorMessage(String JavaDoc errMessage) {
239         errorMessageValue.setText(errMessage);
240         if (errMessage == null || errMessage.trim().equals("")) {
241             customizerPanel.remove(errorMessageValue);
242         } else {
243             customizerPanel.add(errorMessageValue, errMessConstraints);
244         }
245         customizerPanel.revalidate();
246     }
247
248     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
249         if (evt.getPropertyName() == CategoryChangeSupport.ERROR_MESSAGE_PROPERTY) {
250             String JavaDoc errMessage = (String JavaDoc) evt.getNewValue();
251             setErrorMessage(errMessage);
252         }
253     }
254     
255     // Private innerclasses ----------------------------------------------------
256

257     /** Listens to selection change and shows the customizers as
258      * panels
259      */

260     private class CategoryChangeListener implements PropertyChangeListener JavaDoc {
261         
262         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
263             
264             if ( CategoryModel.PROP_CURRENT_CATEGORY.equals( evt.getPropertyName() ) ) {
265                 ProjectCustomizer.Category newCategory = (ProjectCustomizer.Category)evt.getNewValue();
266                 setCategory( newCategory );
267             }
268         }
269     }
270
271 }
272
Popular Tags