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-2006 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:
042: package org.netbeans.modules.identity.server.manager.ui;
043:
044: import java.awt.Color;
045: import javax.swing.JComponent;
046: import javax.swing.JLabel;
047: import javax.swing.JTextField;
048: import javax.swing.event.ChangeEvent;
049: import javax.swing.event.ChangeListener;
050: import org.openide.DialogDescriptor;
051: import org.openide.util.HelpCtx;
052: import org.openide.util.NbBundle;
053:
054: /**
055: * This class is an extension of the DialogDescriptor for creating Edit
056: * dialogs.
057: *
058: * Created on July 26, 2006, 10:51 PM
059: *
060: * @author ptliu
061: */
062: public abstract class EditDialogDescriptor extends DialogDescriptor
063: implements ChangeListener {
064: public static final String STATUS_PREFIX = "Status:"; //NOI18N
065:
066: /** Creates a new instance of EditDialog */
067: public EditDialogDescriptor(javax.swing.JPanel panel, String title,
068: boolean add, JComponent[] components, HelpCtx helpCtx) {
069: this (new InnerPanel(panel), title, add, components, helpCtx);
070: }
071:
072: private EditDialogDescriptor(InnerPanel innerPanel, String title,
073: boolean add, JComponent[] components, HelpCtx helpCtx) {
074: super (innerPanel, getTitle(add, title), true,
075: DialogDescriptor.OK_CANCEL_OPTION,
076: DialogDescriptor.OK_OPTION,
077: DialogDescriptor.BOTTOM_ALIGN, helpCtx, null);
078:
079: if (components != null) {
080: DocListener listener = new DocListener(this );
081: for (JComponent component : components) {
082: if (component instanceof JTextField) {
083: ((JTextField) component).getDocument()
084: .addDocumentListener(listener);
085: }
086: }
087: }
088:
089: checkValues();
090: }
091:
092: private static String getTitle(boolean add, String title) {
093: return (add ? NbBundle.getMessage(EditDialogDescriptor.class,
094: "TTL_Add", title) : NbBundle.getMessage(
095: EditDialogDescriptor.class, "TTL_Edit", title));
096: }
097:
098: /** Calls validation of panel components, displays or removes the error message
099: * Should be called from listeners listening to component changes.
100: */
101: public final void checkValues() {
102: String errorMessage = validate();
103: if (errorMessage == null) {
104: setValid(true);
105: } else {
106: setValid(false);
107: }
108: javax.swing.JLabel errorLabel = ((InnerPanel) getMessage())
109: .getErrorLabel();
110:
111: if (errorMessage != null) {
112: if (errorMessage.startsWith(STATUS_PREFIX)) {
113: errorMessage = errorMessage.substring(STATUS_PREFIX
114: .length());
115: errorLabel.setForeground(Color.BLACK);
116: } else {
117: errorLabel.setForeground(Color.RED);
118: }
119: }
120:
121: errorLabel.setText(errorMessage == null ? " " : errorMessage); //NOI18N
122: }
123:
124: /** Provides validation for panel components */
125: protected abstract String validate();
126:
127: public void stateChanged(ChangeEvent e) {
128: checkValues();
129:
130: //innerPanel.repaint();
131: }
132:
133: public JLabel getErrorLabel() {
134: return ((InnerPanel) getMessage()).getErrorLabel();
135: }
136:
137: private static class InnerPanel extends javax.swing.JPanel {
138: javax.swing.JLabel errorLabel;
139:
140: InnerPanel(javax.swing.JPanel panel) {
141: /*
142: super(new java.awt.BorderLayout());
143: errorLabel = new javax.swing.JLabel(" "); //NOI18N
144: errorLabel.setBorder(new javax.swing.border.EmptyBorder(12,12,0,0));
145: errorLabel.setForeground(Color.RED);
146: add(panel, java.awt.BorderLayout.CENTER);
147: add(errorLabel, java.awt.BorderLayout.SOUTH);
148: */
149:
150: javax.swing.JSeparator separator = new javax.swing.JSeparator();
151: errorLabel = new javax.swing.JLabel(" "); //NOI18N
152: errorLabel.setForeground(Color.RED);
153:
154: org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
155: this );
156: this .setLayout(layout);
157: layout
158: .setHorizontalGroup(layout
159: .createParallelGroup(
160: org.jdesktop.layout.GroupLayout.LEADING)
161: .add(
162: layout
163: .createSequentialGroup()
164: .addContainerGap()
165: .add(
166: separator,
167: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
168: 380,
169: Short.MAX_VALUE)
170: .addContainerGap())
171: .add(
172: panel,
173: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
174: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
175: Short.MAX_VALUE)
176: .add(
177: layout
178: .createSequentialGroup()
179: .addContainerGap()
180: .add(
181: errorLabel,
182: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
183: 380,
184: Short.MAX_VALUE)
185: .addContainerGap()));
186: layout
187: .setVerticalGroup(layout
188: .createParallelGroup(
189: org.jdesktop.layout.GroupLayout.LEADING)
190: .add(
191: org.jdesktop.layout.GroupLayout.TRAILING,
192: layout
193: .createSequentialGroup()
194: .add(
195: panel,
196: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
197: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
198: Short.MAX_VALUE)
199: .addPreferredGap(
200: org.jdesktop.layout.LayoutStyle.RELATED)
201: .add(
202: separator,
203: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
204: 10,
205: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
206: .addPreferredGap(
207: org.jdesktop.layout.LayoutStyle.RELATED)
208: .add(
209: errorLabel,
210: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
211: 14,
212: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
213: .addContainerGap()));
214: }
215:
216: void setErrorMessage(String message) {
217: errorLabel.setText(message);
218: }
219:
220: javax.swing.JLabel getErrorLabel() {
221: return errorLabel;
222: }
223: }
224:
225: /** Useful DocumentListener class that can be added to the panel's text compoents */
226: public static class DocListener implements
227: javax.swing.event.DocumentListener {
228: EditDialogDescriptor dialog;
229:
230: public DocListener(EditDialogDescriptor dialog) {
231: this .dialog = dialog;
232: }
233:
234: /**
235: * Method from DocumentListener
236: */
237: public void changedUpdate(javax.swing.event.DocumentEvent evt) {
238: dialog.checkValues();
239: }
240:
241: /**
242: * Method from DocumentListener
243: */
244: public void insertUpdate(javax.swing.event.DocumentEvent evt) {
245: dialog.checkValues();
246: }
247:
248: /**
249: * Method from DocumentListener
250: */
251: public void removeUpdate(javax.swing.event.DocumentEvent evt) {
252: dialog.checkValues();
253: }
254: }
255:
256: public interface Panel {
257: JComponent[] getEditableComponents();
258: }
259: }
|