KickJava   Java API By Example, From Geeks To Geeks.

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


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
21 package org.netbeans.modules.j2ee.ddloaders.web.multiview;
22
23 import org.netbeans.modules.j2ee.dd.api.web.SecurityConstraint;
24 import org.netbeans.modules.j2ee.dd.api.web.WebApp;
25 import org.netbeans.modules.j2ee.dd.api.web.WebResourceCollection;
26 import org.netbeans.modules.j2ee.ddloaders.web.DDDataObject;
27 import org.netbeans.modules.xml.multiview.ui.DefaultTablePanel;
28 import org.netbeans.modules.xml.multiview.ui.EditDialog;
29 import org.openide.util.NbBundle;
30
31 /**
32  * WebResourceCollectionTablePanel.java
33  *
34  * Panel for displaying the web resource collection table.
35  *
36  * @author ptliu
37  */

38 public class WebResourceCollectionTablePanel extends DefaultTablePanel {
39     private DDDataObject dObj;
40     private WebResourceCollectionTableModel model;
41     private SecurityConstraint constraint;
42     
43     /**
44      * Creates new form WebResourceCollectionTablePanel
45      */

46     public WebResourceCollectionTablePanel(final DDDataObject dObj,
47             final WebResourceCollectionTableModel model) {
48         super(model);
49         this.dObj = dObj;
50         this.model = model;
51         
52         removeButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
53             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
54                 dObj.modelUpdatedFromUI();
55                 dObj.setChangedFromUI(true);
56                 int row = getTable().getSelectedRow();
57                 model.removeRow(row);
58                 dObj.setChangedFromUI(false);
59             }
60         });
61         
62         editButton.addActionListener(new TableActionListener(false));
63         addButton.addActionListener(new TableActionListener(true));
64     }
65     
66     void setModel(WebApp webApp, SecurityConstraint constraint,
67             WebResourceCollection[] collections) {
68         model.setData(constraint, collections);
69         model.setWebApp(webApp);
70         this.constraint = constraint;
71     }
72     
73     private class TableActionListener implements java.awt.event.ActionListener JavaDoc {
74         private boolean add;
75         
76         TableActionListener(boolean add) {
77             this.add=add;
78         }
79         
80         public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
81             final int row = (add?-1:getTable().getSelectedRow());
82             final WebResourceCollectionPanel dialogPanel = new WebResourceCollectionPanel();
83             
84             if (!add) {
85                 WebResourceCollection col = model.getWebResourceCollection(row);
86                 dialogPanel.setResourceName(col.getWebResourceName());
87                 dialogPanel.setDescription(col.getDefaultDescription());
88                 dialogPanel.setUrlPatterns(col.getUrlPattern());
89                 dialogPanel.setHttpMethods(col.getHttpMethod());
90             }
91             
92             EditDialog dialog = new EditDialog(dialogPanel,NbBundle.getMessage(EjbRefsTablePanel.class,"TTL_WebResource"),add) {
93                 protected String JavaDoc validate() {
94                     String JavaDoc name = dialogPanel.getResourceName().trim();
95                     WebResourceCollection webResource = null;
96                     
97                     if (row != -1)
98                         webResource = model.getWebResourceCollection(row);
99                     
100                     if (name.length()==0) {
101                         return NbBundle.getMessage(WebResourceCollectionTablePanel.class,"TXT_EmptyWebResourceName");
102                     } else {
103                         WebResourceCollection[] col = constraint.getWebResourceCollection();
104                         
105                         for (int i = 0; i < col.length; i++) {
106                             if (col[i] != webResource && name.equals(col[i].getWebResourceName())) {
107                                 return NbBundle.getMessage(WebResourceCollectionTablePanel.class,"TXT_WebResourceNameExists",name);
108                             }
109                         }
110                     }
111                     
112                     String JavaDoc[] urlPatterns = dialogPanel.getUrlPatterns();
113                     if (urlPatterns.length == 0) {
114                         return NbBundle.getMessage(WebResourceCollectionTablePanel.class,"TXT_EmptyUrlPatterns",name);
115                     }
116                     return null;
117                 }
118             };
119             
120             if (add)
121                 dialog.setValid(false); // disable OK button
122

123             javax.swing.event.DocumentListener JavaDoc docListener = new EditDialog.DocListener(dialog);
124             dialogPanel.getResourceNameTF().getDocument().addDocumentListener(docListener);
125             dialogPanel.getDescriptionTF().getDocument().addDocumentListener(docListener);
126             dialogPanel.getUrlPatternsTF().getDocument().addDocumentListener(docListener);
127         
128             java.awt.Dialog JavaDoc d = org.openide.DialogDisplayer.getDefault().createDialog(dialog);
129             d.setVisible(true);
130             
131             dialogPanel.getResourceNameTF().getDocument().removeDocumentListener(docListener);
132             dialogPanel.getDescriptionTF().getDocument().removeDocumentListener(docListener);
133             dialogPanel.getUrlPatternsTF().getDocument().removeDocumentListener(docListener);
134             
135             if (dialog.getValue().equals(EditDialog.OK_OPTION)) {
136                 dObj.modelUpdatedFromUI();
137                 dObj.setChangedFromUI(true);
138                 
139                 String JavaDoc resourceName = dialogPanel.getResourceName();
140                 String JavaDoc[] urlPatterns = dialogPanel.getUrlPatterns();
141                 String JavaDoc[] httpMethods = dialogPanel.getSelectedHttpMethods();
142                 String JavaDoc description = dialogPanel.getDescription();
143                 
144                 
145                 if (add)
146                     model.addRow(new Object JavaDoc[] {resourceName, urlPatterns,
147                     httpMethods, description});
148                 else
149                     model.editRow(row, new Object JavaDoc[]{resourceName,
150                     urlPatterns, httpMethods, description});
151               
152                 dObj.setChangedFromUI(false);
153             }
154         }
155     }
156     
157     /** This method is called from within the constructor to
158      * initialize the form.
159      * WARNING: Do NOT modify this code. The content of this method is
160      * always regenerated by the Form Editor.
161      */

162     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
163
private void initComponents() {
164
165         setLayout(new java.awt.GridBagLayout JavaDoc());
166
167     }// </editor-fold>//GEN-END:initComponents
168

169     
170     // Variables declaration - do not modify//GEN-BEGIN:variables
171
// End of variables declaration//GEN-END:variables
172

173 }
174
Popular Tags