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.faces.dt.binding;
042:
043: import javax.swing.*;
044: import java.awt.*;
045: import com.sun.rave.designtime.*;
046: import java.awt.event.*;
047: import javax.swing.event.*;
048: import org.netbeans.modules.visualweb.faces.dt.util.ComponentBundle;
049:
050: public class BindingPanel extends JPanel implements BindingCallback {
051:
052: private static final ComponentBundle bundle = ComponentBundle
053: .getBundle(BindingPanel.class);
054:
055: JTextField curExprTextField = new JTextField();
056: JTextField newExprTextField = new JTextField();
057: JLabel curExprLabel = new JLabel();
058: JLabel newExprLabel = new JLabel();
059: JButton clearButton = new JButton();
060: JButton applyButton = new JButton();
061: JPanel topPanel = new JPanel();
062: SourcePanel sourcePanel = new SourcePanel(this );
063: TargetPanel targetPanel = new TargetPanel(this );
064: JPanel filler1 = new JPanel();
065: JPanel filler2 = new JPanel();
066:
067: public BindingPanel() {
068: try {
069: jbInit();
070: } catch (Exception e) {
071: // e.printStackTrace();
072: }
073: }
074:
075: protected boolean showSourcePanel = true;
076:
077: public void setShowSourcePanel(boolean showSource) {
078: this .showSourcePanel = showSource;
079: refresh();
080: }
081:
082: public boolean isShowSourcePanel() {
083: return showSourcePanel;
084: }
085:
086: protected BindingsCustomizer customizer;
087:
088: public void setCustomizer(BindingsCustomizer bc) {
089: this .customizer = bc;
090: }
091:
092: protected DesignContext context;
093:
094: public void setSourceContext(DesignContext context) {
095: this .context = context;
096: sourcePanel.sourceContextChanged(context);
097: targetPanel.sourceContextChanged(context);
098: refreshCurExprText();
099: }
100:
101: public DesignContext getSourceContext() {
102: return context;
103: }
104:
105: protected DesignBean bean;
106:
107: public void setSourceBean(DesignBean bean) {
108: this .bean = bean;
109: if (bean != null && this .context != bean.getDesignContext()) {
110: setSourceContext(bean.getDesignContext());
111: }
112: sourcePanel.sourceBeanChanged(bean);
113: targetPanel.sourceBeanChanged(bean);
114: refreshCurExprText();
115: }
116:
117: public DesignBean getSourceBean() {
118: return bean;
119: }
120:
121: protected DesignProperty prop;
122:
123: public void setSourceProperty(DesignProperty prop) {
124: this .prop = prop;
125: if (prop != null && this .bean != prop.getDesignBean()) {
126: setSourceBean(prop.getDesignBean());
127: }
128: sourcePanel.sourcePropertyChanged(prop);
129: targetPanel.sourcePropertyChanged(prop);
130: refreshCurExprText();
131: }
132:
133: public DesignProperty getSourceProperty() {
134: return prop;
135: }
136:
137: public void refreshCurExprText() {
138: DesignProperty p = getSourceProperty();
139: String pt = p != null ? p.getPropertyDescriptor().getName()
140: : "<font color=\"red\">"
141: + bundle.getMessage("pickOneBrackets")
142: + "</font>"; //NOI18N
143: DesignBean b = getSourceBean();
144: // String bt = b != null ? b.getInstanceName() : "<font color=\"red\">" + bundle.getMessage("pickOneBrackets") + "</font>"; //NOI18N
145: curExprLabel.setText("<html>"
146: + bundle.getMessage("currBindingForProp", pt)
147: + "</html>"); //NOI18N
148: String nx = newExprTextField.getText();
149: applyButton.setEnabled(b != null && p != null && nx != null
150: && !"".equals(nx)); //NOI18N
151: if (p != null) {
152: String vx = p.getValueSource();
153: if (vx != null && vx.startsWith("#{") && vx.endsWith("}")) { //NOI18N
154: curExprTextField.setText(vx);
155: clearButton.setEnabled(true);
156: return;
157: }
158: }
159: curExprTextField.setText(null);
160: clearButton.setEnabled(false);
161: if (customizer != null) {
162: customizer.firePropertyChange();
163: }
164: }
165:
166: public void refreshApplyState() {
167: DesignProperty p = getSourceProperty();
168: DesignBean b = getSourceBean();
169: String nx = newExprTextField.getText();
170: applyButton.setEnabled(b != null && p != null && nx != null
171: && !"".equals(nx)); //NOI18N
172: if (customizer != null) {
173: customizer.firePropertyChange();
174: }
175: }
176:
177: public boolean isModified() {
178: String oldEx = curExprTextField.getText();
179: String newEx = newExprTextField.getText();
180: return !((oldEx == null && newEx == null) || (oldEx != null && oldEx
181: .equals(newEx)));
182: }
183:
184: public void setNewExpressionText(String newExpr) {
185: newExprTextField.setText(newExpr);
186: refreshCurExprText();
187: }
188:
189: public void refresh() {
190: if (showSourcePanel) {
191: topPanel.add(sourcePanel, BorderLayout.WEST);
192: } else {
193: topPanel.remove(sourcePanel);
194: }
195: refreshCurExprText();
196: this .validate();
197: this .doLayout();
198: this .repaint(100);
199: }
200:
201: void doClearExpr() {
202: DesignProperty p = getSourceProperty();
203: if (p != null) {
204: p.setValueSource(null);
205: refresh();
206: }
207: }
208:
209: void doApplyExpr() {
210: if (applyButton.isEnabled()) {
211: DesignProperty p = getSourceProperty();
212: if (p != null) {
213: p.setValueSource(newExprTextField.getText());
214: refresh();
215: }
216: }
217: }
218:
219: private void jbInit() throws Exception {
220: this .setLayout(new GridBagLayout());
221: topPanel.setLayout(new BorderLayout(0, 0));
222: curExprTextField.setText(""); //NOI18N
223: curExprTextField.setEditable(false);
224: curExprTextField.setBackground(SystemColor.control);
225: newExprTextField.setText(""); //NOI18N
226: newExprTextField.getDocument().addDocumentListener(
227: new DocumentListener() {
228: public void insertUpdate(DocumentEvent e) {
229: refreshApplyState();
230: }
231:
232: public void removeUpdate(DocumentEvent e) {
233: refreshApplyState();
234: }
235:
236: public void changedUpdate(DocumentEvent e) {
237: refreshApplyState();
238: }
239: });
240: String boldRedPickOne = "<b><font color=\"red\">"
241: + bundle.getMessage("pickOneBrackets") + "</font></b>"; //NOI18N
242: curExprLabel.setText("<html>"
243: + bundle.getMessage("currBindingForProp",
244: boldRedPickOne) + "</html>"); //NOI18N
245: newExprLabel.setText(bundle.getMessage("newBindExpr")); //NOI18N
246: curExprLabel.addMouseListener(new MouseAdapter() {
247: public void mousePressed(MouseEvent e) {
248: if (e.getClickCount() == 3 && e.isMetaDown()) {
249: setShowSourcePanel(!isShowSourcePanel());
250: }
251: }
252: });
253: clearButton.setText(bundle.getMessage("clear")); //NOI18N
254: clearButton.addActionListener(new ActionListener() {
255: public void actionPerformed(ActionEvent e) {
256: doClearExpr();
257: }
258: });
259: applyButton.setText(bundle.getMessage("apply")); //NOI18N
260: applyButton.addActionListener(new ActionListener() {
261: public void actionPerformed(ActionEvent e) {
262: doApplyExpr();
263: }
264: });
265: filler1.setBackground(SystemColor.controlShadow);
266: filler1.setOpaque(true);
267: filler1.setPreferredSize(new Dimension(10, 1));
268: filler2.setBackground(SystemColor.controlShadow);
269: filler2.setOpaque(true);
270: filler2.setPreferredSize(new Dimension(10, 10));
271: this .setPreferredSize(new Dimension(600, 400));
272: if (showSourcePanel) {
273: topPanel.add(sourcePanel, BorderLayout.WEST);
274: }
275: topPanel.add(targetPanel, BorderLayout.CENTER);
276: topPanel.add(filler1, BorderLayout.SOUTH);
277: this .add(topPanel, new GridBagConstraints(0, 0, 2, 1, 1.0, 1.0,
278: GridBagConstraints.WEST, GridBagConstraints.BOTH,
279: new Insets(0, 0, 0, 0), 0, 0));
280: this .add(curExprLabel, new GridBagConstraints(0, 1, 2, 1, 0.0,
281: 0.0, GridBagConstraints.WEST,
282: GridBagConstraints.HORIZONTAL, new Insets(6, 8, 0, 8),
283: 0, 0));
284: this .add(curExprTextField, new GridBagConstraints(0, 2, 1, 1,
285: 1.0, 0.0, GridBagConstraints.WEST,
286: GridBagConstraints.HORIZONTAL, new Insets(0, 8, 0, 0),
287: 0, 0));
288: this .add(clearButton, new GridBagConstraints(1, 2, 1, 1, 0.0,
289: 0.0, GridBagConstraints.EAST,
290: GridBagConstraints.HORIZONTAL, new Insets(0, 4, 0, 8),
291: 0, 0));
292: this .add(newExprLabel, new GridBagConstraints(0, 3, 2, 1, 0.0,
293: 0.0, GridBagConstraints.WEST,
294: GridBagConstraints.HORIZONTAL, new Insets(4, 8, 0, 8),
295: 0, 0));
296: this .add(newExprTextField, new GridBagConstraints(0, 4, 1, 1,
297: 1.0, 0.0, GridBagConstraints.WEST,
298: GridBagConstraints.HORIZONTAL, new Insets(0, 8, 0, 0),
299: 0, 0));
300: this .add(applyButton, new GridBagConstraints(1, 4, 1, 1, 0.0,
301: 0.0, GridBagConstraints.EAST,
302: GridBagConstraints.HORIZONTAL, new Insets(0, 4, 0, 8),
303: 0, 0));
304: this .add(filler2, new GridBagConstraints(0, 5, 2, 1, 0.0, 0.0,
305: GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
306: new Insets(8, 0, 0, 0), 0, -9));
307: }
308: }
|