Source Code Cross Referenced for BindOptionsToDataProviderPanel.java in  » IDE-Netbeans » visualweb.api.designer » org » netbeans » modules » visualweb » propertyeditors » binding » data » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE Netbeans » visualweb.api.designer » org.netbeans.modules.visualweb.propertyeditors.binding.data 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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.data;
042:
043:        import java.awt.event.ActionEvent;
044:        import java.awt.event.ActionListener;
045:        import java.awt.event.ComponentAdapter;
046:        import java.awt.event.ComponentEvent;
047:        import java.util.ArrayList;
048:        import java.util.HashMap;
049:        import java.util.Iterator;
050:        import java.util.List;
051:        import java.awt.Component;
052:        import java.awt.Dimension;
053:        import java.awt.GridBagConstraints;
054:        import java.awt.GridBagLayout;
055:        import java.awt.Insets;
056:        import java.awt.event.ItemEvent;
057:        import java.awt.event.ItemListener;
058:        import javax.swing.DefaultListCellRenderer;
059:        import javax.swing.DefaultListModel;
060:        import javax.swing.JButton;
061:        import javax.swing.JComboBox;
062:        import javax.swing.JLabel;
063:        import javax.swing.JList;
064:        import javax.swing.JScrollPane;
065:        import javax.swing.JTextPane;
066:        import javax.swing.ListModel;
067:        import javax.swing.ListSelectionModel;
068:        import javax.swing.SwingUtilities;
069:        import javax.swing.UIManager;
070:        import javax.swing.event.ListSelectionEvent;
071:        import javax.swing.event.ListSelectionListener;
072:        import com.sun.data.provider.DataProvider;
073:        import com.sun.data.provider.FieldKey;
074:        import com.sun.rave.designtime.Constants;
075:        import com.sun.rave.designtime.DesignBean;
076:        import com.sun.rave.designtime.DesignContext;
077:        import com.sun.rave.designtime.DesignProperty;
078:        import com.sun.rave.designtime.faces.FacesDesignContext;
079:        import com.sun.rave.designtime.event.DesignContextListener;
080:        import com.sun.rave.designtime.Position;
081:        import com.sun.rave.designtime.DesignEvent;
082:        import com.sun.rave.designtime.DesignProject;
083:        import com.sun.rave.designtime.faces.FacesDesignProject;
084:        import org.netbeans.modules.visualweb.propertyeditors.binding.BindingTargetCallback;
085:        import org.netbeans.modules.visualweb.propertyeditors.binding.PropertyBindingHelper;
086:        import org.netbeans.modules.visualweb.propertyeditors.util.Bundle;
087:        import org.openide.ErrorManager;
088:
089:        // Modified to support Add Data Provider from this panel- Winston
090:        // Lots of duplication between BindOptionsToDataProviderPanel & BindValueToDataProviderPanel
091:        // XXX Needs clean up
092:
093:        /*
094:         * @authors
095:         *            Winston Prakash (cleaned up lots of mess)
096:         */
097:
098:        public class BindOptionsToDataProviderPanel extends DataBindingPanel
099:                implements  DesignContextListener {
100:
101:            private static final Bundle bundle = Bundle
102:                    .getBundle(BindOptionsToDataProviderPanel.class);
103:
104:            protected JLabel dpLabel = new JLabel();
105:            protected JComboBox dpCombo = new JComboBox();
106:
107:            protected HashMap valListModelHash = new HashMap();
108:            protected HashMap dispListModelHash = new HashMap();
109:
110:            protected HashMap valSelectedItemHash = new HashMap();
111:            protected HashMap dispSelectedItemHash = new HashMap();
112:
113:            protected JLabel valueListLabel = new JLabel();
114:            protected JList valueList = new JList();
115:            protected JScrollPane valueListScroll = new JScrollPane(valueList);
116:
117:            protected JLabel displayListLabel = new JLabel();
118:            protected JList displayList = new JList();
119:            protected JScrollPane displayListScroll = new JScrollPane(
120:                    displayList);
121:
122:            protected GridBagLayout gridbag = new GridBagLayout();
123:            protected String parentName = null;
124:
125:            protected JTextPane noneText = new JTextPane();
126:
127:            private JButton addDataProviderButton = new JButton();
128:
129:            DesignContext[] contexts;
130:
131:            private List brokenDataProvider = new ArrayList();
132:
133:            DesignProperty designProperty;
134:            DesignBean selectedBean = null;
135:
136:            protected ListItem val_none = new ListItem();
137:            protected ListItem disp_none = new ListItem();
138:
139:            private String newExpression = null;
140:
141:            protected class ListItem {
142:                public String display;
143:                public FieldKey field;
144:                public String type;
145:
146:                public String toString() {
147:                    return field != null ? "<html><body><b>"
148:                            + field.getDisplayName() + "</b> &nbsp; <i>" + type
149:                            + "</i></body></html>" : display;
150:                }
151:            }
152:
153:            boolean stopDataProviderThread = false;
154:
155:            public BindOptionsToDataProviderPanel(
156:                    BindingTargetCallback callback, DesignProperty prop) {
157:                super (callback, prop);
158:                try {
159:                    jbInit();
160:                } catch (Exception e) {
161:                    e.printStackTrace();
162:                }
163:
164:                // For Shortfin we removed the Server Navigator window.
165:                // Add Data provider dialogs depends on it. So hide it for Shortfin - Winston
166:                addDataProviderButton.setVisible(false);
167:
168:                designProperty = prop;
169:                val_none.display = bundle.getMessage("noneBrackets"); //NOI18N
170:                disp_none.display = bundle.getMessage("useValueBrackets"); //NOI18N
171:                dpCombo.addItem(bundle.getMessage("dpRetrievingMessage"));
172:                Thread dataProviderNodeThread = new Thread(new Runnable() {
173:                    //SwingUtilities.invokeLater(new Runnable() {
174:                    public void run() {
175:                        updateDataProvider(designProperty);
176:                    }
177:                });
178:                dataProviderNodeThread.setPriority(Thread.MIN_PRIORITY);
179:                dataProviderNodeThread.start();
180:
181:                addComponentListener(new ComponentAdapter() {
182:
183:                    public void componentShown(ComponentEvent e) {
184:                        if (newExpression != null) {
185:                            if ((newExpression != null) && isShowing()) {
186:                                bindingCallback
187:                                        .setNewExpressionText(newExpression);
188:                            }
189:                        }
190:                    }
191:                });
192:            }
193:
194:            // For performance improvement. No need to get all the contexts in the project
195:            private DesignContext[] getDesignContexts(DesignBean designBean) {
196:                DesignProject designProject = designBean.getDesignContext()
197:                        .getProject();
198:                DesignContext[] contexts;
199:                if (designProject instanceof  FacesDesignProject) {
200:                    contexts = ((FacesDesignProject) designProject)
201:                            .findDesignContexts(new String[] { "request",
202:                                    "session", "application" });
203:                } else {
204:                    contexts = new DesignContext[0];
205:                }
206:                DesignContext[] designContexts = new DesignContext[contexts.length + 1];
207:                designContexts[0] = designBean.getDesignContext();
208:                System.arraycopy(contexts, 0, designContexts, 1,
209:                        contexts.length);
210:                return designContexts;
211:            }
212:
213:            protected void updateDataProvider(DesignProperty prop) {
214:                dpLabel.setText(bundle.getMessage("chooseDpToBind", prop
215:                        .getDesignBean().getInstanceName())); //NOI18N
216:
217:                if (prop != null) {
218:                    //contexts = prop.getDesignBean().getDesignContext().getProject().getDesignContexts();
219:                    contexts = getDesignContexts(prop.getDesignBean());
220:
221:                    // find the current value
222:                    String currentRef = prop.getValueSource();
223:                    String contextName = null;
224:                    String modelName = null;
225:                    String valueField = null;
226:                    String displayField = null;
227:                    if (currentRef != null && currentRef.startsWith("#{") && //NOI18N
228:                            currentRef.endsWith("}")) { //NOI18N
229:
230:                        String optionsKey = getPropertyResolverKey();
231:                        List parts = new ArrayList();
232:
233:                        //MBOHM fix 5086833
234:                        //there could be internal dots within, say, the options['travel.person.personid,travel.person.name']
235:                        String strippedRef = currentRef.substring(2, currentRef
236:                                .length() - 1);
237:                        int bracesOpen = 0, currStart = 0;
238:                        String part;
239:                        for (int i = 0; i < strippedRef.length(); i++) {
240:                            char c = strippedRef.charAt(i);
241:                            if (c == '[') {
242:                                bracesOpen++;
243:                            } else if (c == ']') {
244:                                bracesOpen--;
245:                            } else if (c == '.' && bracesOpen < 1) {
246:                                part = strippedRef.substring(currStart, i);
247:                                if (part.length() > 0) {
248:                                    parts.add(part);
249:                                }
250:                                currStart = i + 1;
251:                            }
252:                        }
253:                        //get the remaining stuff after the last period
254:                        if (currStart < strippedRef.length()) {
255:                            part = strippedRef.substring(currStart);
256:                            parts.add(part);
257:                        }
258:
259:                        if (parts.size() > 0) {
260:                            contextName = "" + parts.get(0);
261:                        }
262:                        if (parts.size() > 1) {
263:                            modelName = "" + parts.get(1);
264:                        }
265:                        if (parts.size() > 2
266:                                && ((String) parts.get(2))
267:                                        .startsWith(optionsKey + "['")) { //NOI18N
268:                            String siText = "" + parts.get(2);
269:                            String fieldList = siText.substring(siText
270:                                    .indexOf(optionsKey + "['")
271:                                    + (optionsKey + "['").length()); //NOI18N
272:                            fieldList = fieldList.substring(0, fieldList
273:                                    .indexOf("']")); //NOI18N
274:
275:                            ArrayList fields = new ArrayList();
276:
277:                            //MBOHM fix 5086833
278:                            //could have internal commas, in say, selectItems['employee.employeeid, employee.firstname || \' , \' || employee.lastname']
279:                            boolean quoteOpen = false;
280:                            currStart = 0;
281:                            String field;
282:                            for (int i = 0; i < fieldList.length(); i++) {
283:                                char c = fieldList.charAt(i);
284:                                if (c == '\'') {
285:                                    quoteOpen = !quoteOpen;
286:                                } else if (c == ',' && !quoteOpen) {
287:                                    field = fieldList.substring(currStart, i);
288:                                    if (field.length() > 0) {
289:                                        fields.add(field);
290:                                    }
291:                                    currStart = i + 1;
292:                                }
293:                            }
294:                            //get the remaining stuff after the last period
295:                            if (currStart < fieldList.length()) {
296:                                field = fieldList.substring(currStart);
297:                                fields.add(field);
298:                            }
299:
300:                            valueField = fields.size() > 0 ? "" + fields.get(0)
301:                                    : null; //NOI18N
302:                            displayField = fields.size() > 1 ? ""
303:                                    + fields.get(1) : null; //NOI18N
304:                        }
305:                    }
306:
307:                    // then scan for all data providers
308:                    ArrayList dpBeans = new ArrayList();
309:                    for (int i = 0; i < contexts.length; i++) {
310:                        String scope = (String) contexts[i]
311:                                .getContextData(Constants.ContextData.SCOPE);
312:                        if ("request".equals(scope)
313:                                && contexts[i] != prop.getDesignBean()
314:                                        .getDesignContext()) { //NOI18N
315:                            continue;
316:                        }
317:                        DesignBean[] dpbs = contexts[i]
318:                                .getBeansOfType(DataProvider.class);
319:                        for (int j = 0; j < dpbs.length; j++) {
320:                            dpBeans.add(dpbs[j]);
321:                        }
322:                    }
323:
324:                    if (dpBeans.size() > 0) {
325:                        for (int i = 0; i < dpBeans.size(); i++) {
326:                            if (stopDataProviderThread)
327:                                return;
328:                            DesignBean dpb = (DesignBean) dpBeans.get(i);
329:                            try {
330:                                if (selectedBean == null) {
331:                                    DesignContext dpc = dpb.getDesignContext();
332:                                    if (dpc instanceof  FacesDesignContext) {
333:                                        if (((FacesDesignContext) dpc)
334:                                                .getReferenceName().equals(
335:                                                        contextName)
336:                                                && dpb.getInstanceName()
337:                                                        .equals(modelName)) {
338:                                            selectedBean = dpb;
339:                                        }
340:                                    } else {
341:                                        if (dpc.getDisplayName().equals(
342:                                                contextName)
343:                                                && dpb.getInstanceName()
344:                                                        .equals(modelName)) {
345:                                            selectedBean = dpb;
346:                                        }
347:                                    }
348:                                }
349:                                DataProvider dp = (DataProvider) dpb
350:                                        .getInstance();
351:                                if (dp != null) {
352:                                    DefaultListModel val_dlm = new DefaultListModel();
353:                                    val_dlm.addElement(val_none);
354:
355:                                    DefaultListModel disp_dlm = new DefaultListModel();
356:                                    disp_dlm.addElement(disp_none);
357:
358:                                    try {
359:                                        FieldKey[] fkeys = dp.getFieldKeys();
360:                                        for (int f = 0; f < fkeys.length; f++) {
361:                                            ListItem li = new ListItem();
362:                                            li.field = fkeys[f];
363:                                            Class t = dp.getType(fkeys[f]);
364:                                            li.type = PropertyBindingHelper
365:                                                    .getPrettyTypeName(t
366:                                                            .getName());
367:                                            val_dlm.addElement(li);
368:                                            disp_dlm.addElement(li);
369:                                            if (dpb == selectedBean) {
370:                                                String liColumnEscapeApos = li.field
371:                                                        .getFieldId()
372:                                                        .replaceAll("\\'",
373:                                                                "\\\\'"); //NOI18N
374:                                                if (!valSelectedItemHash
375:                                                        .containsKey(dpb)
376:                                                        && liColumnEscapeApos
377:                                                                .equals(valueField)) {
378:                                                    valSelectedItemHash.put(
379:                                                            dpb, li);
380:                                                }
381:                                                if (!dispSelectedItemHash
382:                                                        .containsKey(dpb)
383:                                                        && liColumnEscapeApos
384:                                                                .equals(displayField)) {
385:                                                    dispSelectedItemHash.put(
386:                                                            dpb, li);
387:                                                }
388:                                            } else {
389:                                                if (!valSelectedItemHash
390:                                                        .containsKey(dpb)
391:                                                        && isTypeOf(
392:                                                                Integer.class,
393:                                                                "int",
394:                                                                dp
395:                                                                        .getType(fkeys[f]))) {
396:                                                    valSelectedItemHash.put(
397:                                                            dpb, li);
398:                                                }
399:                                                if (!dispSelectedItemHash
400:                                                        .containsKey(dpb)
401:                                                        && dp
402:                                                                .getType(
403:                                                                        fkeys[f])
404:                                                                .isAssignableFrom(
405:                                                                        String.class)) {
406:                                                    dispSelectedItemHash.put(
407:                                                            dpb, li);
408:                                                }
409:                                            }
410:                                        }
411:                                    } catch (Exception exc) {
412:                                        ErrorManager.getDefault().notify(exc);
413:                                        brokenDataProvider.add(dpb);
414:                                    }
415:                                    if (!valSelectedItemHash.containsKey(dpb)) {
416:                                        valSelectedItemHash.put(dpb, val_none);
417:                                    }
418:                                    if (!dispSelectedItemHash.containsKey(dpb)) {
419:                                        valSelectedItemHash.put(dpb, disp_none);
420:                                    }
421:
422:                                    valListModelHash.put(dpb, val_dlm);
423:                                    dispListModelHash.put(dpb, disp_dlm);
424:                                }
425:                            } catch (Exception x) {
426:                                x.printStackTrace();
427:                            }
428:                        }
429:                    }
430:                }
431:
432:                SwingUtilities.invokeLater(new Runnable() {
433:                    public void run() {
434:                        dpCombo.removeAllItems();
435:                        Iterator iter = valListModelHash.keySet().iterator();
436:                        while (iter.hasNext()) {
437:                            dpCombo.addItem(iter.next());
438:                        }
439:                        attachListeners();
440:                        if (dpCombo.getItemCount() > 0) {
441:                            if (selectedBean != null) {
442:                                dpCombo.setSelectedItem(selectedBean);
443:                            } else {
444:                                dpCombo.setSelectedIndex(0);
445:                            }
446:                            updateLists();
447:                        }
448:                        repaint(100);
449:                    }
450:                });
451:            }
452:
453:            private static boolean isTypeOf(Class ofType, String primitiveType,
454:                    Class tobecheckType) {
455:                if (tobecheckType.isAssignableFrom(ofType)
456:                        || (tobecheckType.isPrimitive() && tobecheckType
457:                                .getName().equals(primitiveType)))
458:                    return true;
459:                else
460:                    return false;
461:            }
462:
463:            public void addNotify() {
464:                super .addNotify();
465:                contexts = designProperty.getDesignBean().getDesignContext()
466:                        .getProject().getDesignContexts();
467:                for (int i = 0; i < contexts.length; i++) {
468:                    //System.out.println("Adding context Listeners - " + contexts[i].getDisplayName());
469:                    contexts[i]
470:                            .addDesignContextListener(BindOptionsToDataProviderPanel.this );
471:                }
472:            }
473:
474:            public void removeNotify() {
475:                // Make sure the added listeners to contexts are removed
476:                // Sigh! if only design time provide a neater way to clean up.
477:                for (int i = 0; i < contexts.length; i++) {
478:                    //System.out.println("Removing context Listeners - " + contexts[i].getDisplayName());
479:                    contexts[i].removeDesignContextListener(this );
480:                }
481:
482:                stopDataProviderThread = true;
483:
484:                super .removeNotify();
485:            }
486:
487:            private void jbInit() throws Exception {
488:
489:                valueList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
490:                valueList.getAccessibleContext().setAccessibleName(
491:                        bundle.getMessage("VALUE_FIELD_LIST_ACCESS_NAME"));
492:                valueList.getAccessibleContext().setAccessibleDescription(
493:                        bundle.getMessage("VALUE_FIELD_LIST_ACCESS_DESC"));
494:                displayList
495:                        .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
496:                displayList.getAccessibleContext().setAccessibleName(
497:                        bundle.getMessage("DISPLAY_FIELD_LIST_ACCESS_NAME"));
498:                displayList.getAccessibleContext().setAccessibleDescription(
499:                        bundle.getMessage("DISPLAY_FIELD_LIST_ACCESS_DESC"));
500:                noneText.setEditable(false);
501:                noneText.setFont(dpLabel.getFont());
502:                noneText.setBorder(UIManager.getBorder("TextField.border")); //NOI18N
503:                noneText.setText(bundle.getMessage("noDps")); //NOI18N
504:
505:                valueListLabel.setText(bundle.getMessage("valField")); //NOI18N
506:                valueListLabel.setDisplayedMnemonic(bundle.getMessage(
507:                        "valFieldDisplayedMnemonic").charAt(0)); //NOI18N
508:                valueListLabel.setLabelFor(valueList);
509:                displayListLabel.setText(bundle.getMessage("displayField")); //NOI18N        
510:                displayListLabel.setDisplayedMnemonic(bundle.getMessage(
511:                        "displayFieldDisplayedMnemonic").charAt(0)); //NOI18N        
512:                displayListLabel.setLabelFor(displayList);
513:                valueListScroll.setPreferredSize(new Dimension(200, 200));
514:
515:                dpLabel.setDisplayedMnemonic(bundle.getMessage(
516:                        "chooseDpToBindSimpleDisplayedMnemonic").charAt(0)); //NOI18N
517:                dpLabel.setLabelFor(dpCombo);
518:
519:                dpCombo.getAccessibleContext().setAccessibleName(
520:                        bundle.getMessage("DP_COMBO_ACCESS_NAME"));
521:                dpCombo.getAccessibleContext().setAccessibleDescription(
522:                        bundle.getMessage("DP_COMBO_ACCESS_DESC"));
523:                dpCombo.setRenderer(new DPComboRenderer());
524:
525:                this .setPreferredSize(new Dimension(400, 200));
526:                this .setLayout(gridbag);
527:
528:                addDataProviderButton.setText(bundle
529:                        .getMessage("ADD_DP_BUTTON_LBL"));
530:                addDataProviderButton.addActionListener(new ActionListener() {
531:                    public void actionPerformed(ActionEvent evt) {
532:                        new AddDataProviderDialog().showDialog();
533:                    }
534:                });
535:
536:                this .add(dpLabel, new GridBagConstraints(0, 0, 2, 1, 0.0, 0.0,
537:                        GridBagConstraints.WEST, GridBagConstraints.NONE,
538:                        new Insets(8, 8, 2, 8), 0, 0));
539:                this .add(dpCombo, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
540:                        GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
541:                        new Insets(0, 8, 8, 8), 0, 0));
542:                this .add(addDataProviderButton, new GridBagConstraints(1, 1, 1,
543:                        1, 1.0, 0.0, GridBagConstraints.WEST,
544:                        GridBagConstraints.HORIZONTAL, new Insets(0, 8, 8, 8),
545:                        0, 0));
546:
547:                this .add(valueListLabel, new GridBagConstraints(0, 2, 1, 1,
548:                        0.0, 0.0, GridBagConstraints.WEST,
549:                        GridBagConstraints.NONE, new Insets(0, 8, 2, 8), 0, 0));
550:                this .add(valueListScroll, new GridBagConstraints(0, 3, 1, 1,
551:                        1.0, 1.0, GridBagConstraints.CENTER,
552:                        GridBagConstraints.BOTH, new Insets(0, 8, 8, 8), 0, 0));
553:
554:                this .add(displayListLabel, new GridBagConstraints(1, 2, 1, 1,
555:                        0.0, 0.0, GridBagConstraints.WEST,
556:                        GridBagConstraints.NONE, new Insets(0, 0, 2, 8), 0, 0));
557:                this .add(displayListScroll, new GridBagConstraints(1, 3, 1, 1,
558:                        1.0, 1.0, GridBagConstraints.CENTER,
559:                        GridBagConstraints.BOTH, new Insets(0, 0, 8, 8), 0, 0));
560:            }
561:
562:            /**
563:             * Private method to attach listeners to Data provider combobox and
564:             * value list items and display list items after the data providers
565:             */
566:            private void attachListeners() {
567:
568:                dpCombo.addItemListener(new ItemListener() {
569:                    public void itemStateChanged(ItemEvent e) {
570:                        if (e.getStateChange() == e.SELECTED) {
571:                            updateLists();
572:                        }
573:                    }
574:                });
575:                valueList.addListSelectionListener(new ListSelectionListener() {
576:                    public void valueChanged(ListSelectionEvent e) {
577:                        if (!e.getValueIsAdjusting()) {
578:                            updateSelection();
579:                        }
580:                    }
581:                });
582:                displayList
583:                        .addListSelectionListener(new ListSelectionListener() {
584:                            public void valueChanged(ListSelectionEvent e) {
585:                                if (!e.getValueIsAdjusting()) {
586:                                    updateSelection();
587:                                }
588:                            }
589:                        });
590:            }
591:
592:            public String getDataBindingTitle() {
593:                return bundle.getMessage("bindToDp"); // NOI18N
594:            }
595:
596:            protected void updateLists() {
597:                ListModel vlm = (ListModel) valListModelHash.get(dpCombo
598:                        .getSelectedItem());
599:                valueList.setModel(vlm);
600:                ListModel dlm = (ListModel) dispListModelHash.get(dpCombo
601:                        .getSelectedItem());
602:                displayList.setModel(dlm);
603:                valueList.setSelectedValue(valSelectedItemHash.get(dpCombo
604:                        .getSelectedItem()), true);
605:                displayList.setSelectedValue(dispSelectedItemHash.get(dpCombo
606:                        .getSelectedItem()), true);
607:            }
608:
609:            protected void updateSelection() {
610:                FieldKey valField = null;
611:                FieldKey dispField = null;
612:                ListItem valItem = (ListItem) valueList.getSelectedValue();
613:                ListItem dispItem = (ListItem) displayList.getSelectedValue();
614:                if (valItem != null) {
615:                    valField = valItem.field;
616:                }
617:                if (dispItem != null) {
618:                    dispField = dispItem.field;
619:                }
620:
621:                newExpression = calcValueRef((DesignBean) dpCombo
622:                        .getSelectedItem(), valField, dispField);
623:
624:                if ((newExpression != null) && isShowing()) {
625:                    bindingCallback.setNewExpressionText(newExpression);
626:                }
627:            }
628:
629:            protected String calcValueRef(DesignBean dpBean,
630:                    FieldKey valueField, FieldKey labelField) {
631:                if (dpBean == null || valueField == null) {
632:                    return null;
633:                }
634:                String optionsKey = getPropertyResolverKey();
635:                String ref = valueField.getFieldId()
636:                        + (labelField != null ? "," + labelField.getFieldId()
637:                                : "");
638:                //MBOHM fix 5086833
639:                ref = ref.replaceAll("\\'", "\\\\'"); //NOI18N
640:                DesignContext c = dpBean.getDesignContext();
641:                if (c instanceof  FacesDesignContext) {
642:                    return "#{" + ((FacesDesignContext) c).getReferenceName()
643:                            + "." + dpBean.getInstanceName() + "." + optionsKey
644:                            + "['" + ref + "']}"; //NOI18N
645:                } else {
646:                    return "#{" + c.getDisplayName() + "."
647:                            + dpBean.getInstanceName() + "." + optionsKey
648:                            + "['" + ref + "']}"; //NOI18N
649:                }
650:            }
651:
652:            class DPComboRenderer extends DefaultListCellRenderer {
653:                public Component getListCellRendererComponent(JList list,
654:                        Object value, int index, boolean isSelected,
655:                        boolean cellHasFocus) {
656:                    super .getListCellRendererComponent(list, value, index,
657:                            isSelected, cellHasFocus);
658:                    if (value instanceof  DesignBean) {
659:                        DesignBean dpBean = (DesignBean) value;
660:                        if (dpBean != null) {
661:                            if (brokenDataProvider.contains(dpBean)) {
662:                                setText(bundle.getMessage(
663:                                        "dpErrorDisplayTextPattern", dpBean
664:                                                .getInstanceName(), dpBean
665:                                                .getDesignContext()
666:                                                .getDisplayName())); //NOI18N
667:                            } else {
668:                                setText(bundle.getMessage(
669:                                        "dpDisplayTextPattern", dpBean
670:                                                .getInstanceName(), dpBean
671:                                                .getDesignContext()
672:                                                .getDisplayName())); //NOI18N
673:                            }
674:                        }
675:                    } else if (value instanceof  String) {
676:                        setText((String) value);
677:                    }
678:                    return this ;
679:                }
680:            }
681:
682:            protected String getPropertyResolverKey() {
683:                return "options"; // NOI18N
684:            }
685:
686:            // Implementation of DesignContextListener
687:
688:            public void beanCreated(DesignBean designBean) {
689:                if (designBean.getInstance() instanceof  DataProvider) {
690:                    //System.out.println("Bean Created - " + designBean.getInstanceName());
691:                }
692:            }
693:
694:            public void instanceNameChanged(DesignBean designBean,
695:                    String oldInstanceName) {
696:                if (designBean.getInstance() instanceof  DataProvider) {
697:                    //System.out.println("Instance Name changed - " + oldInstanceName + " to " + designBean.getInstanceName());
698:                    dpCombo.repaint();
699:                }
700:            }
701:
702:            public void propertyChanged(DesignProperty prop, Object oldValue) {
703:                DesignBean dpb = prop.getDesignBean();
704:                if ((dpb.getInstance() instanceof  DataProvider)) {
705:                    //System.out.println("Bean property Changed - "  + prop.getDesignBean().getInstanceName());
706:                    //System.out.println("Property Name - "  + prop.getPropertyDescriptor().getDisplayName());
707:                    if (prop.getPropertyDescriptor().getName().equals(
708:                            "CachedRowSet")) {
709:                        DataProvider dp = (DataProvider) dpb.getInstance();
710:                        if (dp != null) {
711:                            DefaultListModel val_dlm = new DefaultListModel();
712:                            val_dlm.addElement(val_none);
713:
714:                            DefaultListModel disp_dlm = new DefaultListModel();
715:                            disp_dlm.addElement(disp_none);
716:
717:                            valListModelHash.put(prop.getDesignBean(), val_dlm);
718:                            dispListModelHash.put(prop.getDesignBean(),
719:                                    disp_dlm);
720:
721:                            try {
722:                                FieldKey[] fkeys = dp.getFieldKeys();
723:                                for (int f = 0; f < fkeys.length; f++) {
724:                                    ListItem li = new ListItem();
725:                                    li.field = fkeys[f];
726:                                    Class t = dp.getType(fkeys[f]);
727:                                    li.type = PropertyBindingHelper
728:                                            .getPrettyTypeName(t.getName());
729:                                    val_dlm.addElement(li);
730:                                    disp_dlm.addElement(li);
731:                                    if (!valSelectedItemHash.containsKey(dpb)
732:                                            && isTypeOf(Integer.class, "int",
733:                                                    dp.getType(fkeys[f]))) {
734:                                        valSelectedItemHash.put(dpb, li);
735:                                    }
736:                                    if (!dispSelectedItemHash.containsKey(dpb)
737:                                            && dp.getType(fkeys[f])
738:                                                    .isAssignableFrom(
739:                                                            String.class)) {
740:                                        dispSelectedItemHash.put(dpb, li);
741:                                    }
742:                                }
743:                            } catch (Exception exc) {
744:                                ErrorManager.getDefault().notify(exc);
745:                            }
746:                            dpCombo.addItem(prop.getDesignBean());
747:                            dpCombo.setSelectedItem(dpb);
748:                        }
749:                    }
750:                }
751:            }
752:
753:            public void beanChanged(DesignBean designBean) {
754:            }
755:
756:            public void contextActivated(DesignContext context) {
757:            }
758:
759:            public void contextDeactivated(DesignContext context) {
760:            }
761:
762:            public void contextChanged(DesignContext context) {
763:            }
764:
765:            public void beanDeleted(DesignBean designBean) {
766:            }
767:
768:            public void beanMoved(DesignBean designBean, DesignBean oldParent,
769:                    Position pos) {
770:            }
771:
772:            public void beanContextActivated(DesignBean designBean) {
773:            }
774:
775:            public void beanContextDeactivated(DesignBean designBean) {
776:            }
777:
778:            public void eventChanged(DesignEvent event) {
779:            }
780:        }
w_w___w_.__j___a_v__a__2_s._c__o___m___ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.