1 19 20 package org.netbeans.modules.j2ee.persistence.wizard.library; 21 22 import java.awt.Color ; 23 import java.beans.PropertyChangeEvent ; 24 import java.beans.PropertyChangeListener ; 25 import javax.swing.ImageIcon ; 26 import javax.swing.UIManager ; 27 import javax.swing.event.DocumentEvent ; 28 import javax.swing.event.DocumentListener ; 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 37 public class PersistenceLibraryPanel extends javax.swing.JPanel { 38 39 public static final String IS_VALID = "PersistenceLibraryPanel_isValid"; 41 private LibraryImplementation libImpl; 42 private Color nbErrorForeground; 43 private Color 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)); tabbedPane.setMnemonicAt(1, NbBundle.getMessage(PersistenceLibraryPanel.class, "MNE_SourcesTab").charAt(0)); tabbedPane.setMnemonicAt(2, NbBundle.getMessage(PersistenceLibraryPanel.class, "MNE_JavadocTab").charAt(0)); nbErrorForeground = UIManager.getColor("nb.errorForeground"); if (nbErrorForeground == null) { 63 nbErrorForeground = new Color (255, 0, 0); } 65 nbWarningForeground = UIManager.getColor("nb.warningForeground"); if (nbWarningForeground == null) { 68 nbWarningForeground = new Color (51, 51, 51); } 70 LibraryManager lm = LibraryManager.getDefault(); 72 String libraryName = "PersistenceLibrary"; 73 int index = 1; 74 while (lm.getLibrary(libraryName + index) != null) { 75 index++; 76 } 77 libraryNameTextField.setText(libraryName + index); 78 libraryNameTextField.getDocument().addDocumentListener(new DocumentListener () { 80 public void changedUpdate(DocumentEvent e) { 81 checkValidity(); 82 } 83 public void insertUpdate(DocumentEvent e) { 84 checkValidity(); 85 } 86 public void removeUpdate(DocumentEvent e) { 87 checkValidity(); 88 } 89 }); 90 libImpl.addPropertyChangeListener(new PropertyChangeListener () { 92 public void propertyChange(PropertyChangeEvent 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 libraryName = libraryNameTextField.getText(); 105 if (libraryName.trim().equals("")) { 106 setErrorMessage(NbBundle.getMessage(PersistenceLibrarySupport.class, "ERR_EmptyName"), false); firePropertyChange(IS_VALID, true, false); 108 } else if (LibraryManager.getDefault().getLibrary(libraryName) != null) { 109 setErrorMessage(NbBundle.getMessage(PersistenceLibrarySupport.class, "ERR_LibraryExists"), false); firePropertyChange(IS_VALID, true, false); 111 } else if (!containsEntityManager()) { 112 setErrorMessage(NbBundle.getMessage(PersistenceLibrarySupport.class, "ERR_NoEntityManager"), false); firePropertyChange(IS_VALID, true, false); 114 } else if (!containsPersistenceProvider()) { 115 setErrorMessage(NbBundle.getMessage(PersistenceLibrarySupport.class, "ERR_NoPersistenceProvider"), false); firePropertyChange(IS_VALID, true, false); 117 } else { 118 setErrorMessage("", true); 119 firePropertyChange(IS_VALID, false, true); 120 } 121 } 122 123 private void setErrorMessage(String msg, Boolean canContinue) { 124 errorMessage.setForeground(nbErrorForeground); 125 if (msg != null && msg.trim().length() > 0 && canContinue != null) { 126 if (canContinue.booleanValue()) { 127 errorMessage.setIcon(new ImageIcon (Utilities.loadImage("org/openide/resources/warning.gif"))); errorMessage.setForeground(nbWarningForeground); 129 } else { 130 errorMessage.setIcon(new ImageIcon (Utilities.loadImage("org/openide/resources/error.gif"))); } 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"); } 144 145 private boolean containsPersistenceProvider() { 146 return PersistenceLibrarySupport.containsService(libImpl, "javax.persistence.spi.PersistenceProvider"); } 148 149 154 private void initComponents() { 156 jLabel1 = new javax.swing.JLabel (); 157 libraryNameTextField = new javax.swing.JTextField (); 158 tabbedPane = new javax.swing.JTabbedPane (); 159 errorMessage = new javax.swing.JLabel (); 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 } 200 201 private javax.swing.JLabel errorMessage; 203 private javax.swing.JLabel jLabel1; 204 private javax.swing.JTextField libraryNameTextField; 205 private javax.swing.JTabbedPane tabbedPane; 206 208 } 209 | Popular Tags |