KickJava   Java API By Example, From Geeks To Geeks.

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


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.ErrorPage;
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 ErrorPagesTablePanel extends DefaultTablePanel {
36     private ErrorPagesTableModel model;
37     private WebApp webApp;
38     private DDDataObject dObj;
39     
40     /** Creates new form ErrorPagesTablePanel */
41     public ErrorPagesTablePanel(final DDDataObject dObj, final ErrorPagesTableModel model) {
42         super(model);
43         this.model=model;
44         this.dObj=dObj;
45         webApp = dObj.getWebApp();
46         removeButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
47             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
48                 dObj.modelUpdatedFromUI();
49                 dObj.setChangedFromUI(true);
50                 int row = getTable().getSelectedRow();
51                 model.removeRow(row);
52                 dObj.setChangedFromUI(false);
53             }
54         });
55         addButton.addActionListener(new TableActionListener(true));
56         editButton.addActionListener(new TableActionListener(false));
57     }
58
59     void setModel(WebApp webApp, ErrorPage[] pages) {
60         model.setData(webApp,pages);
61         this.webApp=webApp;
62     }
63     
64     private class TableActionListener implements java.awt.event.ActionListener JavaDoc {
65         private boolean add;
66         TableActionListener(boolean add) {
67             this.add=add;
68         }
69         
70         public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
71             final int row = (add?-1:getTable().getSelectedRow());
72             String JavaDoc[] labels = new String JavaDoc[]{
73                 NbBundle.getMessage(ErrorPagesTablePanel.class,"LBL_errorPage"),
74                 NbBundle.getMessage(ErrorPagesTablePanel.class,"LBL_errorCode"),
75                 NbBundle.getMessage(ErrorPagesTablePanel.class,"LBL_exceptionType")
76             };
77
78             char[] mnem = new char[] {
79                 NbBundle.getMessage(ErrorPagesTablePanel.class,"LBL_errorPage_mnem").charAt(0),
80                 NbBundle.getMessage(ErrorPagesTablePanel.class,"LBL_errorCode_mnem").charAt(0),
81                 NbBundle.getMessage(ErrorPagesTablePanel.class,"LBL_exceptionType_mnem").charAt(0)
82             };
83             SimpleDialogPanel.DialogDescriptor descriptor = new SimpleDialogPanel.DialogDescriptor(labels);
84             if (!add) {
85                 Integer JavaDoc val = (Integer JavaDoc)model.getValueAt(row,1);
86                 String JavaDoc[] initValues = new String JavaDoc[] {
87                     (String JavaDoc)model.getValueAt(row,0),
88                     val==null?"":((Integer JavaDoc)val).toString(),
89                     (String JavaDoc)model.getValueAt(row,2)
90                 };
91                 descriptor.setInitValues(initValues);
92             }
93             descriptor.setButtons(new boolean[]{true,false,false});
94             descriptor.setMnemonics(mnem);
95             final SimpleDialogPanel dialogPanel = new SimpleDialogPanel(descriptor);
96             dialogPanel.getTextComponents()[0].setEditable(false);
97             dialogPanel.getCustomizerButtons()[0].addActionListener(new java.awt.event.ActionListener JavaDoc() {
98                 public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
99                     try {
100                         org.netbeans.api.project.SourceGroup[] groups = DDUtils.getDocBaseGroups(dObj);
101                         org.openide.filesystems.FileObject fo = BrowseFolders.showDialog(groups);
102                         if (fo!=null) {
103                             String JavaDoc res = DDUtils.getResourcePath(groups,fo,'/',true);
104                             dialogPanel.getTextComponents()[0].setText("/"+res);
105                         }
106                     } catch (java.io.IOException JavaDoc ex) {}
107                 }
108             });
109             EditDialog dialog = new EditDialog(dialogPanel,NbBundle.getMessage(ErrorPagesTablePanel.class,"TTL_ErrorPage"),add) {
110                 protected String JavaDoc validate() {
111                     String JavaDoc[] values = dialogPanel.getValues();
112                     String JavaDoc page = values[0].trim();
113                     String JavaDoc code = values[1].trim();
114                     String JavaDoc exc = values[2].trim();
115                     if (page.length()==0) {
116                         return NbBundle.getMessage(ErrorPagesTablePanel.class,"TXT_EmptyErrorPageLocation");
117                     }
118                     if (code.length()==0 && exc.length()==0) {
119                         return NbBundle.getMessage(ErrorPagesTablePanel.class,"TXT_EP_BothMissing");
120                     } else if (code.length()>0 && exc.length()>0) {
121                         return NbBundle.getMessage(ErrorPagesTablePanel.class,"TXT_EP_BothSpecified");
122                     } else if (code.length()>0) {
123                         Integer JavaDoc c = null;
124                         try {
125                             c = new Integer JavaDoc(code);
126                         } catch (NumberFormatException JavaDoc ex) {}
127                         if (c==null) {
128                             return NbBundle.getMessage(ErrorPagesTablePanel.class,"TXT_EP_wrongNumber",code);
129                         } else {
130                             ErrorPage[] pages = webApp.getErrorPage();
131                             boolean exists=false;
132                             for (int i=0;i<pages.length;i++) {
133                                 if (row!=i && c.equals(pages[i].getErrorCode())) {
134                                     exists=true;
135                                     break;
136                                 }
137                             }
138                             if (exists) {
139                                 return NbBundle.getMessage(ErrorPagesTablePanel.class,"TXT_ErrorCodeExists",c);
140                             }
141                         }
142                     } else {
143                         ErrorPage[] pages = webApp.getErrorPage();
144                         boolean exists=false;
145                         for (int i=0;i<pages.length;i++) {
146                             if (row!=i && exc.equals(pages[i].getExceptionType())) {
147                                 exists=true;
148                                 break;
149                             }
150                         }
151                         if (exists) {
152                             return NbBundle.getMessage(ErrorPagesTablePanel.class,"TXT_ExcTypeExists",exc);
153                         }
154                     }
155                     return null;
156                 }
157             };
158             if (add) dialog.setValid(false); // disable OK button
159

160             javax.swing.event.DocumentListener JavaDoc docListener = new EditDialog.DocListener(dialog);
161             dialogPanel.getTextComponents()[0].getDocument().addDocumentListener(docListener);
162             dialogPanel.getTextComponents()[1].getDocument().addDocumentListener(docListener);
163             dialogPanel.getTextComponents()[2].getDocument().addDocumentListener(docListener);
164             
165             java.awt.Dialog JavaDoc d = org.openide.DialogDisplayer.getDefault().createDialog(dialog);
166             d.setVisible(true);
167             dialogPanel.getTextComponents()[0].getDocument().removeDocumentListener(docListener);
168             dialogPanel.getTextComponents()[1].getDocument().removeDocumentListener(docListener);
169             dialogPanel.getTextComponents()[2].getDocument().removeDocumentListener(docListener);
170             
171             if (dialog.getValue().equals(EditDialog.OK_OPTION)) {
172                 dObj.modelUpdatedFromUI();
173                 dObj.setChangedFromUI(true);
174                 String JavaDoc[] values = dialogPanel.getValues();
175                 String JavaDoc page = values[0].trim();
176                 String JavaDoc code = values[1].trim();
177                 String JavaDoc exc = values[2].trim();
178                 if (add)
179                     model.addRow(new Object JavaDoc[]{page,(code.length()==0?null:new Integer JavaDoc(code)),(exc.length()==0?null:exc)});
180                 else
181                     model.editRow(row,new Object JavaDoc[]{page,(code.length()==0?null:new Integer JavaDoc(code)),(exc.length()==0?null:exc)});
182                 dObj.setChangedFromUI(false);
183             }
184         }
185     }
186 }
187
Popular Tags