KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > multiview > ui > EditDialog


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.xml.multiview.ui;
21 import org.openide.DialogDescriptor;
22
23 import org.openide.util.NbBundle;
24
25 /** EditDialog.java
26  *
27  * Created on November 28, 2004, 7:18 PM
28  * @author mkuchtiak
29  */

30 public abstract class EditDialog extends DialogDescriptor {
31     private javax.swing.JPanel JavaDoc panel;
32
33     /** Creates a new instance of EditDialog */
34     public EditDialog(javax.swing.JPanel JavaDoc panel, String JavaDoc title, boolean adding) {
35         super (new InnerPanel(panel),getTitle(title,adding),true,
36               DialogDescriptor.OK_CANCEL_OPTION,
37               DialogDescriptor.OK_OPTION,
38               DialogDescriptor.BOTTOM_ALIGN,
39               null,
40               null);
41         this.panel=panel;
42     }
43    
44     /** Creates a new instance of EditDialog */
45     public EditDialog(javax.swing.JPanel JavaDoc panel, String JavaDoc title) {
46         this(panel, title,false);
47     }
48     
49     private static String JavaDoc getTitle(String JavaDoc title, boolean adding) {
50         return (adding?NbBundle.getMessage(EditDialog.class,"TTL_ADD",title):
51                 NbBundle.getMessage(EditDialog.class,"TTL_EDIT",title));
52     }
53     /** Returns the dialog panel
54     * @return dialog panel
55     */

56     public final javax.swing.JPanel JavaDoc getDialogPanel() {
57         return panel;
58     }
59     
60     /** Calls validation of panel components, displays or removes the error message
61     * Should be called from listeners listening to component changes.
62     */

63     public final void checkValues() {
64         String JavaDoc errorMessage = validate();
65         if (errorMessage==null) {
66             setValid(true);
67         } else {
68             setValid(false);
69         }
70         javax.swing.JLabel JavaDoc errorLabel = ((InnerPanel)getMessage()).getErrorLabel();
71         errorLabel.setText(errorMessage==null?" ":errorMessage);
72     }
73     
74     /** Provides validation for panel components */
75     protected abstract String JavaDoc validate();
76     
77     private static class InnerPanel extends javax.swing.JPanel JavaDoc {
78         javax.swing.JLabel JavaDoc errorLabel;
79         InnerPanel(javax.swing.JPanel JavaDoc panel) {
80             super(new java.awt.BorderLayout JavaDoc());
81             errorLabel = new javax.swing.JLabel JavaDoc(" ");
82             errorLabel.setBorder(new javax.swing.border.EmptyBorder JavaDoc(12,12,0,0));
83             errorLabel.setForeground(SectionVisualTheme.getErrorLabelColor());
84             add(panel, java.awt.BorderLayout.CENTER);
85             add(errorLabel, java.awt.BorderLayout.SOUTH);
86         }
87         
88         void setErrorMessage(String JavaDoc message) {
89             errorLabel.setText(message);
90         }
91         
92         javax.swing.JLabel JavaDoc getErrorLabel() {
93             return errorLabel;
94         }
95     }
96     
97     /** Useful DocumentListener class that can be added to the panel's text compoents */
98     public static class DocListener implements javax.swing.event.DocumentListener JavaDoc {
99         EditDialog dialog;
100         
101         public DocListener(EditDialog dialog) {
102             this.dialog=dialog;
103         }
104         /**
105          * Method from DocumentListener
106          */

107         public void changedUpdate(javax.swing.event.DocumentEvent JavaDoc evt) {
108             dialog.checkValues();
109         }
110
111         /**
112          * Method from DocumentListener
113          */

114         public void insertUpdate(javax.swing.event.DocumentEvent JavaDoc evt) {
115             dialog.checkValues();
116         }
117
118         /**
119          * Method from DocumentListener
120          */

121         public void removeUpdate(javax.swing.event.DocumentEvent JavaDoc evt) {
122             dialog.checkValues();
123         }
124     }
125 }
126
Popular Tags