001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.project.uiapi;
043:
044: import java.awt.Component;
045: import java.awt.Dimension;
046: import java.awt.GridBagConstraints;
047: import java.awt.Insets;
048: import java.awt.Toolkit;
049: import java.beans.PropertyChangeEvent;
050: import java.beans.PropertyChangeListener;
051: import java.util.HashMap;
052: import javax.swing.BorderFactory;
053: import javax.swing.JComponent;
054: import javax.swing.JPanel;
055: import javax.swing.JLabel;
056: import javax.swing.UIManager;
057: import org.netbeans.spi.project.ui.support.ProjectCustomizer;
058: import org.openide.util.HelpCtx;
059: import org.openide.util.NbBundle;
060:
061: /**
062: *
063: * @author phrebejk
064: */
065: public class CustomizerPane extends JPanel implements HelpCtx.Provider,
066: PropertyChangeListener {
067:
068: public static final String HELP_CTX_PROPERTY = "helpCtxProperty";
069:
070: private Component currentCustomizer;
071: private JLabel errorMessageValue = new JLabel();
072: private HelpCtx currentHelpCtx;
073:
074: private GridBagConstraints fillConstraints;
075: private GridBagConstraints errMessConstraints = new GridBagConstraints();
076:
077: private ProjectCustomizer.CategoryComponentProvider componentProvider;
078:
079: private HashMap<ProjectCustomizer.Category, JComponent> panelCache = new HashMap<ProjectCustomizer.Category, JComponent>();
080:
081: // maximum dimension of the customizer is 3/4 of screen size
082: private static final int MAX_HEIGHT = Toolkit.getDefaultToolkit()
083: .getScreenSize().height * 3 / 4;
084: private static final int MAX_WIDTH = Toolkit.getDefaultToolkit()
085: .getScreenSize().width * 3 / 4;
086:
087: //private DialogDescriptor dialogDescriptor;
088:
089: /** Creates new form J2SECustomizer */
090: public CustomizerPane(
091: JPanel categoryView,
092: CategoryModel categoryModel,
093: ProjectCustomizer.CategoryComponentProvider componentProvider) {
094: initComponents();
095: // HelpCtx.setHelpIDString( customizerPanel, "org.netbeans.modules.java.j2seproject.ui.customizer.J2SECustomizer" ); // NOI18N
096: this .getAccessibleContext().setAccessibleDescription(
097: NbBundle.getMessage(CustomizerPane.class,
098: "AD_CustomizerPane")); // NOI18N
099: this .componentProvider = componentProvider;
100: fillConstraints = new GridBagConstraints();
101: fillConstraints.gridwidth = GridBagConstraints.REMAINDER;
102: fillConstraints.gridheight = 1;
103: fillConstraints.fill = GridBagConstraints.BOTH;
104: fillConstraints.weightx = 1.0;
105: fillConstraints.weighty = 1.0;
106: categoryModel
107: .addPropertyChangeListener(new CategoryChangeListener());
108: categoryPanel.add(categoryView, fillConstraints);
109:
110: // init errorMessageValue
111: errorMessageValue.setBorder(BorderFactory.createEmptyBorder());
112: errorMessageValue.setForeground(UIManager
113: .getColor("nb.errorForeground")); // NOI18N
114: errorMessageValue
115: .setBackground(customizerPanel.getBackground());
116:
117: // put it into under categoryView
118: errMessConstraints = new GridBagConstraints();
119: errMessConstraints.gridx = 0;
120: errMessConstraints.gridy = 1;
121: errMessConstraints.gridwidth = 1;
122: errMessConstraints.gridheight = 1;
123: errMessConstraints.insets = new Insets(12, 0, 0, 0);
124: errMessConstraints.fill = GridBagConstraints.HORIZONTAL;
125: customizerPanel.add(errorMessageValue, errMessConstraints);
126:
127: setCategory(categoryModel.getCurrentCategory());
128: }
129:
130: /** This method is called from within the constructor to
131: * initialize the form.
132: * WARNING: Do NOT modify this code. The content of this method is
133: * always regenerated by the Form Editor.
134: */
135: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
136: private void initComponents() {
137: java.awt.GridBagConstraints gridBagConstraints;
138:
139: jLabel1 = new javax.swing.JLabel();
140: categoryPanel = new javax.swing.JPanel();
141: customizerPanel = new javax.swing.JPanel();
142:
143: setLayout(new java.awt.GridBagLayout());
144:
145: jLabel1.setLabelFor(categoryPanel);
146: org.openide.awt.Mnemonics.setLocalizedText(jLabel1,
147: org.openide.util.NbBundle.getMessage(
148: CustomizerPane.class,
149: "LBL_Customizer_Categories")); // NOI18N
150: gridBagConstraints = new java.awt.GridBagConstraints();
151: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
152: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
153: gridBagConstraints.insets = new java.awt.Insets(8, 11, 0, 0);
154: add(jLabel1, gridBagConstraints);
155:
156: categoryPanel.setBorder(javax.swing.BorderFactory
157: .createEtchedBorder());
158: categoryPanel.setMinimumSize(new java.awt.Dimension(220, 4));
159: categoryPanel.setPreferredSize(new java.awt.Dimension(220, 4));
160: categoryPanel.setLayout(new java.awt.GridBagLayout());
161: gridBagConstraints = new java.awt.GridBagConstraints();
162: gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
163: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
164: gridBagConstraints.weighty = 1.0;
165: gridBagConstraints.insets = new java.awt.Insets(3, 11, 8, 11);
166: add(categoryPanel, gridBagConstraints);
167:
168: customizerPanel.setLayout(new java.awt.GridBagLayout());
169: gridBagConstraints = new java.awt.GridBagConstraints();
170: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
171: gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
172: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
173: gridBagConstraints.weightx = 1.0;
174: gridBagConstraints.weighty = 1.0;
175: gridBagConstraints.insets = new java.awt.Insets(3, 0, 8, 11);
176: add(customizerPanel, gridBagConstraints);
177: }// </editor-fold>//GEN-END:initComponents
178:
179: // Variables declaration - do not modify//GEN-BEGIN:variables
180: private javax.swing.JPanel categoryPanel;
181: private javax.swing.JPanel customizerPanel;
182: private javax.swing.JLabel jLabel1;
183:
184: // End of variables declaration//GEN-END:variables
185:
186: public void clearPanelComponentCache() {
187: //should only happen when closign teh customizer..
188: panelCache.clear();
189: }
190:
191: public Dimension getPreferredSize() {
192: if (isPreferredSizeSet()) {
193: return super .getPreferredSize();
194: }
195:
196: int height = Math.max(450,
197: currentCustomizer.getPreferredSize().height + 50);
198: int width = Math.max(750,
199: currentCustomizer.getPreferredSize().width + 240);
200:
201: Dimension dim = super .getPreferredSize();
202: if (dim == null) {
203: return new Dimension(width, height);
204: }
205: if (dim.getWidth() < width || dim.getHeight() < height) {
206: return new Dimension(width, height);
207: }
208:
209: if (dim.getWidth() > MAX_WIDTH) {
210: dim.width = MAX_WIDTH;
211: }
212: if (dim.getHeight() > MAX_HEIGHT) {
213: dim.height = MAX_HEIGHT;
214: }
215: return dim;
216: }
217:
218: // HelpCtx.Provider implementation -----------------------------------------
219:
220: public HelpCtx getHelpCtx() {
221: return currentHelpCtx;
222: /*
223: System.out.println("Get Help Ctx");
224: Thread.dumpStack();
225:
226: if ( currentCustomizer != null ) {
227: // System.out.println( "C " + HelpCtx.findHelp( currentCustomizer ) );
228: return HelpCtx.findHelp( currentCustomizer );
229: }
230: /*
231: else {
232: // System.out.println( "P " + HelpCtx.findHelp( currentCustomizer ) );
233: return HelpCtx.findHelp( customizerPanel );
234: }
235:
236: // XXX
237: return null;
238: */
239: }
240:
241: // Private methods ---------------------------------------------------------
242:
243: private void setCategory(ProjectCustomizer.Category newCategory) {
244: if (newCategory == null) {
245: return;
246: }
247:
248: if (currentCustomizer != null) {
249: customizerPanel.remove(currentCustomizer);
250: }
251:
252: JComponent newCustomizer = panelCache.get(newCategory);
253: if (newCustomizer == null
254: && !panelCache.containsKey(newCustomizer)) {
255: newCustomizer = componentProvider.create(newCategory);
256: panelCache.put(newCategory, newCustomizer);
257: }
258:
259: if (newCustomizer != null) {
260: Utilities.getCategoryChangeSupport(newCategory)
261: .addPropertyChangeListener(this );
262: currentCustomizer = newCustomizer;
263: currentHelpCtx = HelpCtx.findHelp(currentCustomizer);
264:
265: /*
266: if ( currentCustomizer instanceof javax.swing.JComponent ) {
267: ((javax.swing.JComponent)currentCustomizer).setPreferredSize( new java.awt.Dimension( 600, 0 ) );
268: }
269: */
270: customizerPanel.add(currentCustomizer, fillConstraints);
271: customizerPanel.validate();
272: customizerPanel.repaint();
273:
274: setErrorMessage(newCategory.getErrorMessage());
275: firePropertyChange(HELP_CTX_PROPERTY, null, getHelpCtx());
276: } else {
277: currentCustomizer = null;
278: }
279:
280: }
281:
282: private void setErrorMessage(String errMessage) {
283: errorMessageValue.setText(errMessage);
284: if (errMessage == null || errMessage.trim().equals("")) {
285: customizerPanel.remove(errorMessageValue);
286: } else {
287: customizerPanel.add(errorMessageValue, errMessConstraints);
288: }
289: customizerPanel.revalidate();
290: }
291:
292: public void propertyChange(PropertyChangeEvent evt) {
293: if (evt.getPropertyName() == CategoryChangeSupport.ERROR_MESSAGE_PROPERTY) {
294: String errMessage = (String) evt.getNewValue();
295: setErrorMessage(errMessage);
296: }
297: }
298:
299: // Private innerclasses ----------------------------------------------------
300:
301: /** Listens to selection change and shows the customizers as
302: * panels
303: */
304: private class CategoryChangeListener implements
305: PropertyChangeListener {
306:
307: public void propertyChange(PropertyChangeEvent evt) {
308:
309: if (CategoryModel.PROP_CURRENT_CATEGORY.equals(evt
310: .getPropertyName())) {
311: ProjectCustomizer.Category newCategory = (ProjectCustomizer.Category) evt
312: .getNewValue();
313: setCategory(newCategory);
314: }
315: }
316: }
317:
318: }
|