KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ddloaders > web > multiview > ListenersTablePanel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.ddloaders.web.multiview;
21
22 import org.netbeans.modules.j2ee.dd.api.web.Listener;
23 import org.netbeans.modules.j2ee.dd.api.web.WebApp;
24 import org.netbeans.modules.j2ee.ddloaders.web.DDDataObject;
25 import org.netbeans.modules.xml.multiview.ui.DefaultTablePanel;
26 import org.netbeans.modules.xml.multiview.ui.EditDialog;
27 import org.netbeans.modules.xml.multiview.ui.SimpleDialogPanel;
28 import org.openide.util.NbBundle;
29
30 /**
31  *
32  * @author mk115033
33  * Created on October 1, 2002, 3:52 PM
34  */

35 public class ListenersTablePanel extends DefaultTablePanel {
36     private ListenerTableModel model;
37     private WebApp webApp;
38     private DDDataObject dObj;
39     
40     /** Creates new form ListenersPanel */
41     public ListenersTablePanel(final DDDataObject dObj, final ListenerTableModel model) {
42         super(model);
43         this.model=model;
44         this.dObj=dObj;
45         removeButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
46             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
47                 dObj.modelUpdatedFromUI();
48                 dObj.setChangedFromUI(true);
49                 int row = getTable().getSelectedRow();
50                 model.removeRow(row);
51                 dObj.setChangedFromUI(false);
52             }
53         });
54         editButton.addActionListener(new TableActionListener(false));
55         addButton.addActionListener(new TableActionListener(true));
56     }
57
58     void setModel(WebApp webApp, Listener[] listeners) {
59         model.setData(webApp,listeners);
60         this.webApp=webApp;
61     }
62     
63     private class TableActionListener implements java.awt.event.ActionListener JavaDoc {
64         private boolean add;
65         TableActionListener(boolean add) {
66             this.add=add;
67         }
68         public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
69             final int row = (add?-1:getTable().getSelectedRow());
70             String JavaDoc[] labels = new String JavaDoc[]{
71                 NbBundle.getMessage(ListenersTablePanel.class,"LBL_listenerClass"),
72                 NbBundle.getMessage(ListenersTablePanel.class,"LBL_description")
73             };
74             char[] mnem = new char[] {
75                 NbBundle.getMessage(ListenersTablePanel.class,"LBL_listenerClass_mnem").charAt(0),
76                 NbBundle.getMessage(ListenersTablePanel.class,"LBL_description_mnem").charAt(0)
77             };
78             SimpleDialogPanel.DialogDescriptor descriptor = new SimpleDialogPanel.DialogDescriptor(labels);
79             if (!add) {
80                 String JavaDoc[] initValues = new String JavaDoc[] {
81                     (String JavaDoc)model.getValueAt(row,0),
82                     (String JavaDoc)model.getValueAt(row,1)
83                 };
84                 descriptor.setInitValues(initValues);
85             }
86             descriptor.setMnemonics(mnem);
87             descriptor.setButtons(new boolean[]{true,false});
88             descriptor.setTextField(new boolean[]{true,false});
89             final SimpleDialogPanel dialogPanel = new SimpleDialogPanel(descriptor);
90             dialogPanel.getCustomizerButtons()[0].addActionListener(new java.awt.event.ActionListener JavaDoc() {
91                 public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
92                     try {
93                         org.netbeans.api.project.SourceGroup[] groups = DDUtils.getJavaSourceGroups(dObj);
94                         org.openide.filesystems.FileObject fo = BrowseFolders.showDialog(groups);
95                         if (fo!=null) {
96                             String JavaDoc className = DDUtils.getResourcePath(groups,fo);
97                             dialogPanel.getTextComponents()[0].setText(className);
98                         }
99                     } catch (java.io.IOException JavaDoc ex) {}
100                 }
101             });
102             EditDialog dialog = new EditDialog(dialogPanel,NbBundle.getMessage(ListenersTablePanel.class,"TTL_Listener"),add) {
103                 protected String JavaDoc validate() {
104                     String JavaDoc[] values = dialogPanel.getValues();
105                     String JavaDoc name = values[0];
106                     if (name.length()==0) {
107                             return NbBundle.getMessage(ListenersTablePanel.class,"TXT_EmptyListenerClass");
108                     } else {
109                         Listener[] listeners = webApp.getListener();
110                         boolean exists=false;
111                         for (int i=0;i<listeners.length;i++) {
112                             if (row!=i && name.equals(listeners[i].getListenerClass())) {
113                                 exists=true;
114                                 break;
115                             }
116                         }
117                         if (exists) {
118                             return NbBundle.getMessage(ListenersTablePanel.class,"TXT_ListenerClassExists",name);
119                         }
120                     }
121                     return null;
122                 }
123             };
124             if (add) dialog.setValid(false); // disable OK button
125
javax.swing.event.DocumentListener JavaDoc docListener = new EditDialog.DocListener(dialog);
126             dialogPanel.getTextComponents()[0].getDocument().addDocumentListener(docListener);
127             
128             java.awt.Dialog JavaDoc d = org.openide.DialogDisplayer.getDefault().createDialog(dialog);
129             d.setVisible(true);
130             dialogPanel.getTextComponents()[0].getDocument().removeDocumentListener(docListener);
131             
132             if (dialog.getValue().equals(EditDialog.OK_OPTION)) {
133                 dObj.modelUpdatedFromUI();
134                 dObj.setChangedFromUI(true);
135                 String JavaDoc[] values = dialogPanel.getValues();
136                 String JavaDoc name = values[0];
137                 String JavaDoc description = values[1];
138                 if (add) model.addRow(new String JavaDoc[]{name,description});
139                 else model.editRow(row,new String JavaDoc[]{name,description});
140                 dObj.setChangedFromUI(false);
141             }
142         }
143     }
144 }
145
Popular Tags