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-2007 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: package org.netbeans.modules.visualweb.propertyeditors.binding;
042:
043: import com.sun.rave.designtime.faces.FacesDesignContext;
044: import java.awt.BorderLayout;
045: import java.awt.Dimension;
046: import java.awt.GridBagConstraints;
047: import java.awt.GridBagLayout;
048: import java.awt.Insets;
049: import java.awt.SystemColor;
050: import java.awt.event.ActionEvent;
051: import java.awt.event.ActionListener;
052: import java.awt.event.MouseAdapter;
053: import java.awt.event.MouseEvent;
054: import javax.faces.application.Application;
055: import javax.faces.el.ValueBinding;
056: import javax.swing.JButton;
057: import javax.swing.JLabel;
058: import javax.swing.JPanel;
059: import javax.swing.JTextField;
060: import javax.swing.SwingUtilities;
061: import javax.swing.event.DocumentEvent;
062: import javax.swing.event.DocumentListener;
063: import com.sun.rave.designtime.DesignBean;
064: import com.sun.rave.designtime.DesignContext;
065: import com.sun.rave.designtime.DesignProperty;
066: import org.netbeans.modules.visualweb.propertyeditors.util.Bundle;
067:
068: public class PropertyBindingPanel extends JPanel implements
069: BindingTargetCallback {
070:
071: private static final Bundle bundle = Bundle
072: .getBundle(PropertyBindingPanel.class);
073:
074: JTextField curExprTextField = new JTextField();
075: JTextField newExprTextField = new JTextField();
076: JLabel curExprLabel = new JLabel();
077: JLabel newExprLabel = new JLabel();
078: JButton clearButton = new JButton();
079: JButton applyButton = new JButton();
080: JPanel topPanel = new JPanel();
081: BindingSourcePanel sourcePanel = new BindingSourcePanel(this );
082: BindingTargetPanel targetPanel = new BindingTargetPanel(this );
083: JPanel filler1 = new JPanel();
084: JPanel filler2 = new JPanel();
085:
086: public PropertyBindingPanel() {
087: try {
088: jbInit();
089: curExprLabel.setLabelFor(curExprTextField);
090: newExprLabel.setLabelFor(newExprTextField);
091: curExprLabel.setDisplayedMnemonic(bundle.getMessage(
092: "currBindingForPropDisplayedMnemonic").charAt(0)); //NOI18N
093: newExprLabel.setDisplayedMnemonic(bundle.getMessage(
094: "newBindingForPropDisplayedMnemonic").charAt(0)); //NOI18N
095: curExprTextField.getAccessibleContext().setAccessibleName(
096: bundle.getMessage("CURR_EXP_ACCESS_NAME"));
097: curExprTextField.getAccessibleContext()
098: .setAccessibleDescription(
099: bundle.getMessage("CURR_EXP_ACCESS_DESC"));
100: newExprTextField.getAccessibleContext().setAccessibleName(
101: bundle.getMessage("NEW_EXP_ACCESS_NAME"));
102: newExprTextField.getAccessibleContext()
103: .setAccessibleDescription(
104: bundle.getMessage("NEW_EXP_ACCESS_DESC"));
105: clearButton
106: .getAccessibleContext()
107: .setAccessibleDescription(
108: bundle
109: .getMessage("CLEAR_BUTTON_ACCESS_DESC"));
110: applyButton
111: .getAccessibleContext()
112: .setAccessibleDescription(
113: bundle
114: .getMessage("APPLY_BUTTON_ACCESS_DESC"));
115: clearButton.setMnemonic(bundle.getMessage(
116: "CLEAR_BUTTON_MNEMONIC").charAt(0));
117: applyButton.setMnemonic(bundle.getMessage(
118: "APPLY_BUTTON_MNEMONIC").charAt(0));
119:
120: } catch (Exception e) {
121: e.printStackTrace();
122: }
123: }
124:
125: public void removeNotify() {
126: super .removeNotify();
127: }
128:
129: protected boolean showSourcePanel = true;
130:
131: public void setShowSourcePanel(boolean showSource) {
132: this .showSourcePanel = showSource;
133: refresh();
134: }
135:
136: public boolean isShowSourcePanel() {
137: return showSourcePanel;
138: }
139:
140: protected PropertyBindingsCustomizer customizer;
141:
142: public void setCustomizer(PropertyBindingsCustomizer bc) {
143: this .customizer = bc;
144: }
145:
146: protected DesignContext context;
147:
148: public void setSourceContext(final DesignContext context) {
149: this .context = context;
150: sourcePanel.sourceContextChanged(context);
151: targetPanel.sourceContextChanged(context);
152: /*SwingUtilities.invokeLater(new Runnable() {
153: public void run(){
154: targetPanel.sourceContextChanged(context);
155: }
156: });*/
157: refreshCurExprText();
158: }
159:
160: public DesignContext getSourceContext() {
161: return context;
162: }
163:
164: protected DesignBean sourceBean;
165:
166: public void setSourceBean(DesignBean bean) {
167: this .sourceBean = bean;
168: if (bean != null && this .context != bean.getDesignContext()) {
169: setSourceContext(bean.getDesignContext());
170: }
171: sourcePanel.sourceBeanChanged(bean);
172: targetPanel.sourceBeanChanged(bean);
173: refreshCurExprText();
174: }
175:
176: public DesignBean getSourceBean() {
177: return sourceBean;
178: }
179:
180: protected DesignProperty prop;
181:
182: public void setSourceProperty(DesignProperty prop) {
183: this .prop = prop;
184: if (prop != null && this .sourceBean != prop.getDesignBean()) {
185: setSourceBean(prop.getDesignBean());
186: }
187: sourcePanel.sourcePropertyChanged(prop);
188: targetPanel.sourcePropertyChanged(prop);
189: refreshCurExprText();
190: }
191:
192: public DesignProperty getSourceProperty() {
193: return prop;
194: }
195:
196: public void refreshCurExprText() {
197: DesignProperty p = getSourceProperty();
198: String pt = p != null ? p.getPropertyDescriptor().getName()
199: : "<font color=\"red\">"
200: + bundle.getMessage("pickOneBrackets")
201: + "</font>"; //NOI18N
202: DesignBean b = getSourceBean();
203: // String bt = b != null ? b.getInstanceName() : "<font color=\"red\">" + bundle.getMessage("pickOneBrackets") + "</font>"; //NOI18N
204: curExprLabel.setText("<html>"
205: + bundle.getMessage("currBindingForProp", pt)
206: + "</html>"); //NOI18N
207: String nx = newExprTextField.getText();
208: applyButton.setEnabled(b != null && p != null && nx != null
209: && !"".equals(nx)); //NOI18N
210: if (p != null) {
211: String vx = p.getValueSource();
212: if (vx != null && vx.startsWith("#{") && vx.endsWith("}")) { //NOI18N
213: curExprTextField.setText(vx);
214: clearButton.setEnabled(true);
215: return;
216: }
217: }
218: curExprTextField.setText(null);
219: clearButton.setEnabled(false);
220: if (customizer != null) {
221: customizer.firePropertyChange();
222: }
223: }
224:
225: public void refreshApplyState() {
226: DesignProperty p = getSourceProperty();
227: DesignBean b = getSourceBean();
228: String nx = newExprTextField.getText();
229: applyButton.setEnabled(b != null && p != null && nx != null
230: && !"".equals(nx)); //NOI18N
231: if (customizer != null) {
232: customizer.firePropertyChange();
233: }
234: }
235:
236: public boolean isModified() {
237: String oldEx = curExprTextField.getText();
238: String newEx = newExprTextField.getText();
239: return !((oldEx == null && newEx == null) || (oldEx != null && oldEx
240: .equals(newEx)));
241: }
242:
243: public void setNewExpressionText(String newExpr) {
244: // Make sure we don't set the source bean property back as binding
245: // value to the source bean
246: if (targetPanel.getTargetBean() != getSourceBean()) {
247: newExprTextField.setText(newExpr.trim());
248: applyButton.setEnabled(true);
249: refreshCurExprText();
250: } else {
251: newExprTextField.setText(bundle
252: .getMessage("MSG_INVALID_TARGET")); //NO_I18N
253: applyButton.setEnabled(false);
254: }
255: }
256:
257: public void refresh() {
258: if (showSourcePanel) {
259: topPanel.add(sourcePanel, BorderLayout.WEST);
260: } else {
261: topPanel.remove(sourcePanel);
262: }
263: refreshCurExprText();
264: this .validate();
265: this .doLayout();
266: this .repaint(100);
267: }
268:
269: void doClearExpr() {
270: DesignProperty p = getSourceProperty();
271: if (p != null) {
272: p.setValueSource(null);
273: refresh();
274: }
275: }
276:
277: void doApplyExpr() {
278: if (applyButton.isEnabled()) {
279: DesignProperty p = getSourceProperty();
280: if (p != null) {
281: p.setValueSource(newExprTextField.getText());
282: refresh();
283: }
284: }
285: }
286:
287: private void jbInit() throws Exception {
288: this .setLayout(new GridBagLayout());
289: topPanel.setLayout(new BorderLayout(0, 0));
290: curExprTextField.setText(""); //NOI18N
291: curExprTextField.setEditable(false);
292: curExprTextField.setBackground(SystemColor.control);
293: newExprTextField.setText(""); //NOI18N
294: newExprTextField.getDocument().addDocumentListener(
295: new DocumentListener() {
296: public void insertUpdate(DocumentEvent e) {
297: refreshApplyState();
298: }
299:
300: public void removeUpdate(DocumentEvent e) {
301: refreshApplyState();
302: }
303:
304: public void changedUpdate(DocumentEvent e) {
305: refreshApplyState();
306: }
307: });
308: String boldRedPickOne = "<b><font color=\"red\">"
309: + bundle.getMessage("pickOneBrackets") + "</font></b>"; //NOI18N
310: curExprLabel.setText("<html>"
311: + bundle.getMessage("currBindingForProp",
312: boldRedPickOne) + "</html>"); //NOI18N
313: newExprLabel.setText(bundle.getMessage("newBindExpr")); //NOI18N
314: curExprLabel.addMouseListener(new MouseAdapter() {
315: public void mousePressed(MouseEvent e) {
316: if (e.getClickCount() == 3 && e.isMetaDown()) {
317: setShowSourcePanel(!isShowSourcePanel());
318: }
319: }
320: });
321: clearButton.setText(bundle.getMessage("clear")); //NOI18N
322: clearButton.addActionListener(new ActionListener() {
323: public void actionPerformed(ActionEvent e) {
324: doClearExpr();
325: }
326: });
327: applyButton.setText(bundle.getMessage("apply")); //NOI18N
328: applyButton.addActionListener(new ActionListener() {
329: public void actionPerformed(ActionEvent e) {
330: doApplyExpr();
331: }
332: });
333: filler1.setBackground(SystemColor.controlShadow);
334: filler1.setOpaque(true);
335: //filler1.setPreferredSize(new Dimension(10, 1));
336: filler2.setBackground(SystemColor.controlShadow);
337: filler2.setOpaque(true);
338: //filler2.setPreferredSize(new Dimension(10, 10));
339: //this.setPreferredSize(new Dimension(600, 400));
340: if (showSourcePanel) {
341: topPanel.add(sourcePanel, BorderLayout.WEST);
342: }
343: topPanel.add(targetPanel, BorderLayout.CENTER);
344: topPanel.add(filler1, BorderLayout.SOUTH);
345: this .add(topPanel, new GridBagConstraints(0, 0, 2, 1, 1.0, 1.0,
346: GridBagConstraints.WEST, GridBagConstraints.BOTH,
347: new Insets(0, 0, 0, 0), 0, 0));
348: this .add(curExprLabel, new GridBagConstraints(0, 1, 2, 1, 0.0,
349: 0.0, GridBagConstraints.WEST,
350: GridBagConstraints.HORIZONTAL, new Insets(6, 8, 0, 8),
351: 0, 0));
352: this .add(curExprTextField, new GridBagConstraints(0, 2, 1, 1,
353: 1.0, 0.0, GridBagConstraints.WEST,
354: GridBagConstraints.HORIZONTAL, new Insets(0, 8, 0, 0),
355: 0, 0));
356: this .add(clearButton, new GridBagConstraints(1, 2, 1, 1, 0.0,
357: 0.0, GridBagConstraints.EAST,
358: GridBagConstraints.HORIZONTAL, new Insets(0, 4, 0, 8),
359: 0, 0));
360: this .add(newExprLabel, new GridBagConstraints(0, 3, 2, 1, 0.0,
361: 0.0, GridBagConstraints.WEST,
362: GridBagConstraints.HORIZONTAL, new Insets(4, 8, 0, 8),
363: 0, 0));
364: this .add(newExprTextField, new GridBagConstraints(0, 4, 1, 1,
365: 1.0, 0.0, GridBagConstraints.WEST,
366: GridBagConstraints.HORIZONTAL, new Insets(0, 8, 0, 0),
367: 0, 0));
368: this .add(applyButton, new GridBagConstraints(1, 4, 1, 1, 0.0,
369: 0.0, GridBagConstraints.EAST,
370: GridBagConstraints.HORIZONTAL, new Insets(0, 4, 0, 8),
371: 0, 0));
372: this .add(filler2, new GridBagConstraints(0, 5, 2, 1, 0.0, 0.0,
373: GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
374: new Insets(8, 0, 0, 0), 0, -9));
375: }
376: }
|