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.std;
042:
043: import java.awt.Component;
044: import java.awt.Dimension;
045: import java.awt.GridBagConstraints;
046: import java.awt.GridBagLayout;
047: import java.awt.Insets;
048: import java.util.ArrayList;
049: import java.util.Iterator;
050: import javax.sql.RowSet;
051: import javax.swing.DefaultListCellRenderer;
052: import javax.swing.DefaultListModel;
053: import javax.swing.JCheckBox;
054: import javax.swing.JLabel;
055: import javax.swing.JList;
056: import javax.swing.JPanel;
057: import javax.swing.JScrollPane;
058: import javax.swing.JTextPane;
059: import javax.swing.ListSelectionModel;
060: import javax.swing.UIManager;
061: import javax.swing.event.ListSelectionEvent;
062: import javax.swing.event.ListSelectionListener;
063: import org.netbeans.modules.visualweb.faces.dt.std.table.HtmlDataTableState;
064: import org.netbeans.modules.visualweb.faces.dt.util.ComponentBundle;
065: import com.sun.rave.designtime.Constants;
066: import com.sun.rave.designtime.DesignBean;
067: import com.sun.rave.designtime.DesignContext;
068: import com.sun.rave.designtime.DesignProject;
069: import com.sun.rave.designtime.DesignProperty;
070: import com.sun.rave.designtime.faces.FacesDesignContext;
071: import com.sun.rave.designtime.faces.FacesDesignProject;
072:
073: public class DataModelBindingPanel extends JPanel {
074:
075: private static final ComponentBundle bundle = ComponentBundle
076: .getBundle(DataModelBindingPanel.class);
077:
078: protected DesignProperty prop;
079: protected ValueBindingPanel vbp;
080: protected JScrollPane listScroll = new JScrollPane();
081: protected JList list = new JList();
082: protected JLabel listLabel = new JLabel();
083: protected GridBagLayout gridbag = new GridBagLayout();
084: protected JTextPane noneText = new JTextPane();
085: protected JCheckBox chkGenColumns = new JCheckBox();
086: private HtmlDataTableState tableState;
087:
088: protected int modelIndex = -1;
089:
090: protected boolean initializing = true;
091:
092: protected DataModelBindingCustomizer customizer;
093:
094: public DataModelBindingPanel(ValueBindingPanel vbp,
095: DataModelBindingCustomizer dmbc, DesignProperty prop) {
096: this .vbp = vbp;
097: this .customizer = dmbc;
098: this .prop = prop;
099: try {
100: jbInit();
101: } catch (Exception e) {
102: e.printStackTrace();
103: }
104: DefaultListModel dlm = new DefaultListModel();
105: if (prop != null) {
106: listLabel.setText(bundle.getMessage("chooseRs", prop
107: .getDesignBean().getInstanceName())); //NOI18N
108:
109: tableState = new HtmlDataTableState(prop.getDesignBean());
110: tableState.varName = HtmlDataTableState.DEFAULT_VAR_NAME;
111:
112: /*
113: // find the current value
114: String currentRef = prop.getValueSource();
115: newRef = currentRef;
116: String contextName = null;
117: String rsName = null;
118: if (currentRef != null && currentRef.startsWith("#{") && //NOI18N
119: currentRef.endsWith("}")) { //NOI18N
120:
121: ArrayList parts = new ArrayList();
122: StringTokenizer st = new StringTokenizer(currentRef.substring(2, currentRef.length() - 1), ".");
123: while (st.hasMoreElements()) {
124: parts.add(st.nextElement());
125: }
126: if (parts.size() > 0) {
127: contextName = "" + parts.get(0);
128: }
129: if (parts.size() > 1) {
130: rsName = "" + parts.get(1);
131: }
132: }
133: */
134:
135: DesignContext this Context = prop.getDesignBean()
136: .getDesignContext();
137:
138: //DesignContext[] contexts = thisContext.getProject().getDesignContexts();
139: DesignContext[] contexts = getDesignContexts(prop
140: .getDesignBean());
141:
142: java.util.List sortedContexts = new ArrayList();
143: sortedContexts.add(this Context);
144: java.util.List sessionContexts = new ArrayList();
145: java.util.List applicationContexts = new ArrayList();
146: for (int i = 0; i < contexts.length; i++) {
147: if (contexts[i] == this Context) {
148: continue;
149: }
150: Object scope = contexts[i]
151: .getContextData(Constants.ContextData.SCOPE);
152: if ("session".equals(scope)) { //NOI18N
153: sessionContexts.add(contexts[i]);
154: } else if ("application".equals(scope)) { //NOI18N
155: applicationContexts.add(contexts[i]);
156: }
157: }
158: sortedContexts.addAll(sessionContexts);
159: sortedContexts.addAll(applicationContexts);
160:
161: // get rowset live beans
162: java.util.List rowsetBeans = new ArrayList();
163: for (Iterator iter = sortedContexts.iterator(); iter
164: .hasNext();) {
165: DesignContext aContext = (DesignContext) iter.next();
166: DesignBean[] rsbs = aContext
167: .getBeansOfType(RowSet.class);
168: for (int j = 0; j < rsbs.length; j++) {
169: rowsetBeans.add(rsbs[j]);
170: }
171: }
172:
173: dlm.addElement(null);
174: if (rowsetBeans.size() > 0) {
175: for (int i = 0; i < rowsetBeans.size(); i++) {
176: DesignBean rsBean = (DesignBean) rowsetBeans.get(i);
177:
178: dlm.addElement(rsBean);
179: if (modelIndex < 0) {
180: if (tableState.getSourceBean() == rsBean) {
181: modelIndex = i + 1;
182: }
183: }
184: }
185: } else {
186: this .remove(listScroll);
187: this .remove(chkGenColumns);
188: this .add(noneText, listScrollConstraints);
189: }
190: }
191: list.setModel(dlm);
192: repaint(100);
193: if (modelIndex > -1) {
194: list.setSelectedIndex(modelIndex);
195: chkGenColumns.setEnabled(true);
196: } else {
197: list.setSelectedIndex(0);
198: chkGenColumns.setEnabled(false);
199: tableState.setSourceBean(null); //if table is not bound to a rowset in "scope," then make it bound to nothing
200: if (vbp != null) {
201: vbp.setValueBinding(""); //if none is selected, remove the value binding on this table. NOTE: currently this does not work!
202: }
203: }
204: initializing = false;
205: }
206:
207: // For performance improvement. No need to get all the contexts in the project
208: private DesignContext[] getDesignContexts(DesignBean designBean) {
209: DesignProject designProject = designBean.getDesignContext()
210: .getProject();
211: DesignContext[] contexts;
212: if (designProject instanceof FacesDesignProject) {
213: contexts = ((FacesDesignProject) designProject)
214: .findDesignContexts(new String[] { "request",
215: "session", "application" });
216: } else {
217: contexts = new DesignContext[0];
218: }
219: DesignContext[] designContexts = new DesignContext[contexts.length + 1];
220: designContexts[0] = designBean.getDesignContext();
221: System.arraycopy(contexts, 0, designContexts, 1,
222: contexts.length);
223: return designContexts;
224: }
225:
226: GridBagConstraints listScrollConstraints = new GridBagConstraints(
227: 0, 1, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER,
228: GridBagConstraints.BOTH, new Insets(0, 8, 8, 8), 0, 0);
229:
230: private void jbInit() throws Exception {
231: noneText.setEditable(false);
232: noneText.setFont(listLabel.getFont());
233: noneText.setBorder(UIManager.getBorder("TextField.border")); //NOI18N
234: noneText.setText(bundle.getMessage("noRss")); //NOI18N
235:
236: chkGenColumns.setText(bundle.getMessage("autoGenCols")); //NOI18N
237: chkGenColumns.setSelected(true);
238:
239: list.setCellRenderer(new RSListRenderer());
240:
241: this .setPreferredSize(new Dimension(400, 200));
242: this .setLayout(gridbag);
243:
244: listLabel.setText(bundle.getMessage("chooseRsSimple")); //NOI18N
245: this .add(listLabel, new GridBagConstraints(0, 0, 1, 1, 1.0,
246: 0.0, GridBagConstraints.WEST,
247: GridBagConstraints.HORIZONTAL, new Insets(8, 8, 2, 8),
248: 0, 0));
249: this .add(listScroll, listScrollConstraints);
250: if (customizer != null) {
251: this .add(chkGenColumns, new GridBagConstraints(0, 2, 1, 1,
252: 1.0, 0.0, GridBagConstraints.WEST,
253: GridBagConstraints.HORIZONTAL, new Insets(0, 8, 8,
254: 8), 0, 0));
255: }
256: listScroll.setPreferredSize(new Dimension(200, 200));
257: listScroll.getViewport().add(list, null);
258: list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
259: list.addListSelectionListener(new ListSelectionListener() {
260: public void valueChanged(ListSelectionEvent e) {
261: if (initializing) {
262: return;
263: }
264: Object o = list.getSelectedValue();
265: if (o == null || o instanceof DesignBean) {
266: DesignBean b = (DesignBean) o;
267: String calcRef = calcValueRef(b);
268: if (vbp != null) {
269: vbp.setValueBinding(calcRef == null ? ""
270: : calcRef);
271: } else if (customizer != null) {
272: customizer.setModified(true);
273: chkGenColumns.setEnabled(o == null ? false
274: : true);
275: } else {
276: prop.setValueSource(calcRef);
277: }
278:
279: }
280: }
281: });
282: }
283:
284: public void customizerApply() {
285: Object selectedObj = list.getSelectedValue();
286: tableState.setSourceBean((DesignBean) selectedObj);
287: if (chkGenColumns.isSelected()) {
288: tableState.refreshColumnInfo();
289: }
290: tableState.saveState();
291: //newRef = null;
292: }
293:
294: protected String calcValueRef(DesignBean rowsetBean) {
295: if (rowsetBean == null) {
296: return null;
297: }
298: DesignContext c = rowsetBean.getDesignContext();
299: if (c instanceof FacesDesignContext) {
300: return "#{" + ((FacesDesignContext) c).getReferenceName()
301: + "." + rowsetBean.getInstanceName() + "}"; //NOI18N
302: }
303: return "#{" + c.getDisplayName() + "."
304: + rowsetBean.getInstanceName() + "}"; //NOI18N
305: }
306:
307: class RSListRenderer extends DefaultListCellRenderer {
308: public Component getListCellRendererComponent(JList list,
309: Object value, int index, boolean isSelected,
310: boolean cellHasFocus) {
311: super .getListCellRendererComponent(list, value, index,
312: isSelected, cellHasFocus);
313: if (value instanceof DesignBean) {
314: DesignBean rsBean = (DesignBean) value;
315: this .setText(bundle.getMessage("rsDisplayTextPattern",
316: rsBean.getInstanceName(), //NOI18N
317: rsBean.getDesignContext().getDisplayName())); //NOI18N
318: } else if (value == null) {
319: this .setText(bundle.getMessage("noneBrackets")); //NOI18N
320: }
321: return this;
322: }
323: }
324: }
|