KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > persistence > wizard > library > PersistenceLibraryPanel


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.persistence.wizard.library;
21
22 import java.awt.Color JavaDoc;
23 import java.beans.PropertyChangeEvent JavaDoc;
24 import java.beans.PropertyChangeListener JavaDoc;
25 import javax.swing.ImageIcon JavaDoc;
26 import javax.swing.UIManager JavaDoc;
27 import javax.swing.event.DocumentEvent JavaDoc;
28 import javax.swing.event.DocumentListener JavaDoc;
29 import org.netbeans.api.project.libraries.LibraryManager;
30 import org.netbeans.spi.project.libraries.LibraryImplementation;
31 import org.openide.util.NbBundle;
32 import org.openide.util.Utilities;
33 /**
34  *
35  * @author Martin Adamek
36  */

37 public class PersistenceLibraryPanel extends javax.swing.JPanel JavaDoc {
38     
39     public static final String JavaDoc IS_VALID = "PersistenceLibraryPanel_isValid"; //NOI18N
40

41     private LibraryImplementation libImpl;
42     private Color JavaDoc nbErrorForeground;
43     private Color JavaDoc nbWarningForeground;
44     
45     public PersistenceLibraryPanel(LibraryImplementation libImpl) {
46         initComponents();
47         this.libImpl = libImpl;
48         J2SEVolumeCustomizer classPathCustomizer = new J2SEVolumeCustomizer(PersistenceLibrarySupport.VOLUME_TYPE_CLASSPATH);
49         classPathCustomizer.setObject(libImpl);
50         tabbedPane.add(classPathCustomizer);
51         J2SEVolumeCustomizer srcCustomizer = new J2SEVolumeCustomizer(PersistenceLibrarySupport.VOLUME_TYPE_SRC);
52         srcCustomizer.setObject(libImpl);
53         tabbedPane.add(srcCustomizer);
54         J2SEVolumeCustomizer javadocCustomizer = new J2SEVolumeCustomizer(PersistenceLibrarySupport.VOLUME_TYPE_JAVADOC);
55         javadocCustomizer.setObject(libImpl);
56         tabbedPane.add(javadocCustomizer);
57         tabbedPane.setMnemonicAt(0, NbBundle.getMessage(PersistenceLibraryPanel.class, "MNE_ClasspathTab").charAt(0)); // NOI18N
58
tabbedPane.setMnemonicAt(1, NbBundle.getMessage(PersistenceLibraryPanel.class, "MNE_SourcesTab").charAt(0)); // NOI18N
59
tabbedPane.setMnemonicAt(2, NbBundle.getMessage(PersistenceLibraryPanel.class, "MNE_JavadocTab").charAt(0)); // NOI18N
60
// set foreground color for error messages
61
nbErrorForeground = UIManager.getColor("nb.errorForeground"); //NOI18N
62
if (nbErrorForeground == null) {
63             nbErrorForeground = new Color JavaDoc(255, 0, 0); // RGB suggested by jdinga in #65358
64
}
65         // set foreground color for warning messages
66
nbWarningForeground = UIManager.getColor("nb.warningForeground"); //NOI18N
67
if (nbWarningForeground == null) {
68             nbWarningForeground = new Color JavaDoc(51, 51, 51); // Label.foreground
69
}
70         // create default name for new library
71
LibraryManager lm = LibraryManager.getDefault();
72         String JavaDoc libraryName = "PersistenceLibrary";
73         int index = 1;
74         while (lm.getLibrary(libraryName + index) != null) {
75             index++;
76         }
77         libraryNameTextField.setText(libraryName + index);
78         // listen on libray name changes
79
libraryNameTextField.getDocument().addDocumentListener(new DocumentListener JavaDoc() {
80             public void changedUpdate(DocumentEvent JavaDoc e) {
81                 checkValidity();
82             }
83             public void insertUpdate(DocumentEvent JavaDoc e) {
84                 checkValidity();
85             }
86             public void removeUpdate(DocumentEvent JavaDoc e) {
87                 checkValidity();
88             }
89         });
90         // listen on library changes (added/removed content)
91
libImpl.addPropertyChangeListener(new PropertyChangeListener JavaDoc() {
92             public void propertyChange(PropertyChangeEvent JavaDoc evt) {
93                 checkValidity();
94             }
95         });
96     }
97     
98     public void apply() {
99         libImpl.setName(libraryNameTextField.getText().trim());
100         PersistenceLibrarySupport.getDefault().addLibrary(libImpl);
101     }
102     
103     void checkValidity() {
104         String JavaDoc libraryName = libraryNameTextField.getText();
105         if (libraryName.trim().equals("")) {
106             setErrorMessage(NbBundle.getMessage(PersistenceLibrarySupport.class, "ERR_EmptyName"), false); //NOI18N
107
firePropertyChange(IS_VALID, true, false);
108         } else if (LibraryManager.getDefault().getLibrary(libraryName) != null) {
109             setErrorMessage(NbBundle.getMessage(PersistenceLibrarySupport.class, "ERR_LibraryExists"), false); //NOI18N
110
firePropertyChange(IS_VALID, true, false);
111         } else if (!containsEntityManager()) {
112             setErrorMessage(NbBundle.getMessage(PersistenceLibrarySupport.class, "ERR_NoEntityManager"), false); //NOI18N
113
firePropertyChange(IS_VALID, true, false);
114         } else if (!containsPersistenceProvider()) {
115             setErrorMessage(NbBundle.getMessage(PersistenceLibrarySupport.class, "ERR_NoPersistenceProvider"), false); //NOI18N
116
firePropertyChange(IS_VALID, true, false);
117         } else {
118             setErrorMessage("", true);
119             firePropertyChange(IS_VALID, false, true);
120         }
121     }
122     
123     private void setErrorMessage(String JavaDoc msg, Boolean JavaDoc canContinue) {
124         errorMessage.setForeground(nbErrorForeground);
125         if (msg != null && msg.trim().length() > 0 && canContinue != null) {
126             if (canContinue.booleanValue()) {
127                 errorMessage.setIcon(new ImageIcon JavaDoc(Utilities.loadImage("org/openide/resources/warning.gif"))); // NOI18N
128
errorMessage.setForeground(nbWarningForeground);
129             } else {
130                 errorMessage.setIcon(new ImageIcon JavaDoc(Utilities.loadImage("org/openide/resources/error.gif"))); // NOI18N
131
}
132             errorMessage.setToolTipText(msg);
133         } else {
134             errorMessage.setIcon(null);
135             errorMessage.setToolTipText(null);
136         }
137         
138         errorMessage.setText(msg);
139     }
140     
141     private boolean containsEntityManager() {
142         return PersistenceLibrarySupport.containsClass(libImpl, "javax.persistence.EntityManager"); //NOI18N
143
}
144     
145     private boolean containsPersistenceProvider() {
146         return PersistenceLibrarySupport.containsService(libImpl, "javax.persistence.spi.PersistenceProvider"); //NOI18N
147
}
148     
149     /** This method is called from within the constructor to
150      * initialize the form.
151      * WARNING: Do NOT modify this code. The content of this method is
152      * always regenerated by the Form Editor.
153      */

154     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
155
private void initComponents() {
156         jLabel1 = new javax.swing.JLabel JavaDoc();
157         libraryNameTextField = new javax.swing.JTextField JavaDoc();
158         tabbedPane = new javax.swing.JTabbedPane JavaDoc();
159         errorMessage = new javax.swing.JLabel JavaDoc();
160
161         jLabel1.setDisplayedMnemonic(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/persistence/wizard/library/Bundle").getString("MNE_LibraryName").charAt(0));
162         jLabel1.setLabelFor(libraryNameTextField);
163         jLabel1.setText(org.openide.util.NbBundle.getMessage(PersistenceLibraryPanel.class, "LBL_LibraryName"));
164
165         libraryNameTextField.getAccessibleContext().setAccessibleName(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/persistence/wizard/library/Bundle").getString("LBL_LibraryName"));
166         libraryNameTextField.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/persistence/wizard/library/Bundle").getString("AD_LibraryName"));
167
168         errorMessage.setText(" ");
169
170         org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
171         this.setLayout(layout);
172         layout.setHorizontalGroup(
173             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
174             .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
175                 .addContainerGap()
176                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
177                     .add(org.jdesktop.layout.GroupLayout.LEADING, tabbedPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
178                     .add(layout.createSequentialGroup()
179                         .add(jLabel1)
180                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
181                         .add(libraryNameTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 305, Short.MAX_VALUE))
182                     .add(org.jdesktop.layout.GroupLayout.LEADING, errorMessage, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE))
183                 .addContainerGap())
184         );
185         layout.setVerticalGroup(
186             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
187             .add(layout.createSequentialGroup()
188                 .addContainerGap()
189                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
190                     .add(jLabel1)
191                     .add(libraryNameTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
192                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
193                 .add(tabbedPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 230, Short.MAX_VALUE)
194                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
195                 .add(errorMessage)
196                 .addContainerGap())
197         );
198     }// </editor-fold>//GEN-END:initComponents
199

200     
201     // Variables declaration - do not modify//GEN-BEGIN:variables
202
private javax.swing.JLabel JavaDoc errorMessage;
203     private javax.swing.JLabel JavaDoc jLabel1;
204     private javax.swing.JTextField JavaDoc libraryNameTextField;
205     private javax.swing.JTabbedPane JavaDoc tabbedPane;
206     // End of variables declaration//GEN-END:variables
207

208 }
209
Popular Tags