1 19 20 package org.netbeans.modules.j2ee.persistence.util; 21 22 import java.awt.Color ; 23 import java.awt.Component ; 24 import java.awt.Dimension ; 25 import java.awt.event.ActionEvent ; 26 import java.awt.event.ActionListener ; 27 import java.awt.event.InputEvent ; 28 import java.awt.event.KeyAdapter ; 29 import java.awt.event.KeyEvent ; 30 import java.util.Collections ; 31 import java.util.List ; 32 import javax.swing.DefaultComboBoxModel ; 33 import javax.swing.DefaultListCellRenderer ; 34 import javax.swing.JComboBox ; 35 import javax.swing.JList ; 36 import javax.swing.JSeparator ; 37 import org.netbeans.api.project.Project; 38 import org.netbeans.api.project.libraries.LibrariesCustomizer; 39 import org.netbeans.modules.j2ee.persistence.provider.DefaultProvider; 40 import org.netbeans.modules.j2ee.persistence.provider.Provider; 41 import org.netbeans.modules.j2ee.persistence.provider.ProviderUtil; 42 import org.netbeans.modules.j2ee.persistence.spi.provider.PersistenceProviderSupplier; 43 import org.netbeans.modules.j2ee.persistence.wizard.library.PersistenceLibraryCustomizer; 44 import org.netbeans.modules.j2ee.persistence.wizard.library.PersistenceLibrarySupport; 45 import org.openide.util.NbBundle; 46 import org.openide.util.Parameters; 47 48 56 public final class PersistenceProviderComboboxHelper { 57 58 private final static String SEPARATOR = "PersistenceProviderComboboxHelper.SEPARATOR"; 59 private final static String EMPTY = "PersistenceProviderComboboxHelper.EMPTY"; 60 61 private final PersistenceProviderSupplier providerSupplier; 62 63 70 public PersistenceProviderComboboxHelper(Project project) { 71 Parameters.notNull("project", project); 72 73 PersistenceProviderSupplier aProviderSupplier =project.getLookup().lookup(PersistenceProviderSupplier.class); 74 75 if (aProviderSupplier == null){ 76 aProviderSupplier = new DefaultPersistenceProviderSupplier(); 78 } 79 80 this.providerSupplier = aProviderSupplier; 81 } 82 83 90 public void connect(final JComboBox providerCombo) { 91 providerCombo.setEditable(false); 92 initCombo(providerCombo); 93 providerCombo.addKeyListener(new KeyAdapter () { 95 public void keyPressed(KeyEvent e) { 96 int keyCode = e.getKeyCode(); 97 if (KeyEvent.VK_ENTER == keyCode) { 98 Object selectedItem = providerCombo.getSelectedItem(); 99 if (selectedItem instanceof LibraryItem) { 100 ((LibraryItem) selectedItem).performAction(); 101 providerCombo.setPopupVisible(false); 102 e.consume(); 103 initCombo(providerCombo); 104 } 105 } 106 } 107 }); 108 109 providerCombo.addActionListener(new ActionListener () { 110 111 Object currentItem = providerCombo.getSelectedItem(); 112 int currentIndex = providerCombo.getSelectedIndex(); 113 114 public void actionPerformed(ActionEvent e) { 115 Object selectedItem = providerCombo.getSelectedItem(); 116 if (SEPARATOR.equals(selectedItem)) { 118 int selectedIndex = providerCombo.getSelectedIndex(); 119 if (selectedIndex > currentIndex) { 120 currentIndex = selectedIndex + 1; 121 currentItem = providerCombo.getItemAt(currentIndex); 122 } else { 123 currentIndex = selectedIndex - 1; 124 currentItem = providerCombo.getItemAt(currentIndex); 125 } 126 providerCombo.setSelectedItem(currentItem); 127 } else if ((e.getModifiers() & InputEvent.BUTTON1_MASK) != 0) { 129 if (selectedItem instanceof LibraryItem) { 130 ((LibraryItem) selectedItem).performAction(); 131 providerCombo.setPopupVisible(false); 132 initCombo(providerCombo); 133 } 134 } else { 135 currentItem = selectedItem; 136 currentIndex = providerCombo.getSelectedIndex(); 137 } 138 } 139 }); 140 } 141 142 private void initCombo(JComboBox providerCombo) { 143 144 DefaultComboBoxModel providers = new DefaultComboBoxModel (); 145 146 for(Provider each : providerSupplier.getSupportedProviders()){ 147 providers.addElement(each); 148 } 149 150 if (providers.getSize() == 0 && providerSupplier.supportsDefaultProvider()){ 151 providers.addElement(ProviderUtil.DEFAULT_PROVIDER); 152 } 153 154 addProvidersFromLibraries(providers); 155 156 providerCombo.setModel(providers); 157 providerCombo.addItem(SEPARATOR); 158 providerCombo.addItem(new NewPersistenceLibraryItem()); 159 providerCombo.addItem(new ManageLibrariesItem()); 160 providerCombo.setRenderer(new PersistenceProviderCellRenderer((Provider)providers.getElementAt(0))); 161 providerCombo.setSelectedIndex(0); 162 } 163 164 165 168 private void addProvidersFromLibraries(DefaultComboBoxModel model){ 169 for (Provider each : PersistenceLibrarySupport.getProvidersFromLibraries()){ 170 boolean found = false; 171 for (int i = 0; i < model.getSize(); i++) { 172 Object elem = model.getElementAt(i); 173 if (elem instanceof Provider && each.equals(elem)){ 174 found = true; 175 break; 176 } 177 } 178 if (!found){ 179 model.addElement(each); 180 } 181 } 182 } 183 184 185 public static interface LibraryItem { 186 String getText(); 187 void performAction(); 188 } 189 190 private static class NewPersistenceLibraryItem implements LibraryItem { 191 public String getText() { 192 return NbBundle.getMessage(PersistenceProviderComboboxHelper.class, "LBL_NewPersistenceLibrary"); 193 } 194 public void performAction() { 195 PersistenceLibraryCustomizer.showCustomizer(); 196 } 197 } 198 199 private static class ManageLibrariesItem implements LibraryItem { 200 public String getText() { 201 return NbBundle.getMessage(PersistenceProviderComboboxHelper.class, "LBL_ManageLibraries"); 202 } 203 public void performAction() { 204 LibrariesCustomizer.showCustomizer(null); 205 } 206 } 207 208 private static class PersistenceProviderCellRenderer extends DefaultListCellRenderer { 209 210 Provider defaultProvider; 211 212 PersistenceProviderCellRenderer(Provider defaultProvider) { 213 this.defaultProvider = defaultProvider; 214 } 215 216 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 217 218 if (isSelected) { 219 setBackground(list.getSelectionBackground()); 220 setForeground(list.getSelectionForeground()); 221 } else { 222 setBackground(list.getBackground()); 223 setForeground(list.getForeground()); 224 } 225 226 if (value instanceof Provider) { 227 Provider provider = (Provider)value; 228 String text = provider.getDisplayName(); 229 if (value.equals(defaultProvider) && (!(value instanceof DefaultProvider))) { 230 text += NbBundle.getMessage(PersistenceProviderComboboxHelper.class, "LBL_DEFAULT_PROVIDER"); 231 } 232 setText(text); 233 234 } else if (SEPARATOR.equals(value)) { 235 JSeparator s = new JSeparator (); 236 s.setPreferredSize(new Dimension (s.getWidth(), 1)); 237 s.setForeground(Color.BLACK); 238 return s; 239 240 } else if (EMPTY.equals(value)) { 241 setText(" "); 242 243 } else if (value instanceof LibraryItem) { 244 setText(((LibraryItem) value).getText()); 245 246 } else { 247 setText(value != null ? value.toString() : ""); } 249 250 return this; 251 } 252 253 } 254 255 261 private static class DefaultPersistenceProviderSupplier implements PersistenceProviderSupplier{ 262 263 public List <Provider> getSupportedProviders() { 264 return Collections.<Provider>emptyList(); 265 } 266 267 public boolean supportsDefaultProvider() { 268 return false; 269 } 270 } 271 } 272 | Popular Tags |