1 19 20 package org.netbeans.modules.apisupport.project.ui.platform; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.net.URL ; 25 import java.util.Locale ; 26 import javax.swing.JFileChooser ; 27 import javax.swing.JPanel ; 28 import javax.swing.event.ListDataEvent ; 29 import javax.swing.event.ListDataListener ; 30 import javax.swing.event.ListSelectionEvent ; 31 import javax.swing.event.ListSelectionListener ; 32 import javax.swing.filechooser.FileFilter ; 33 import org.netbeans.modules.apisupport.project.Util; 34 import org.netbeans.modules.apisupport.project.queries.GlobalSourceForBinaryImpl; 35 import org.netbeans.modules.apisupport.project.ui.ModuleUISettings; 36 import org.netbeans.modules.apisupport.project.universe.NbPlatform; 37 import org.openide.DialogDisplayer; 38 import org.openide.NotifyDescriptor; 39 import org.openide.filesystems.FileUtil; 40 import org.openide.util.NbBundle; 41 42 47 final class NbPlatformCustomizerSources extends JPanel { 48 49 private NbPlatform plaf; 50 private PlatformComponentFactory.NbPlatformSourceRootsModel model; 51 private final ListListener listListener; 52 53 54 NbPlatformCustomizerSources() { 55 initComponents(); 56 initAccessibility(); 57 this.listListener = new ListListener() { 58 void listChanged() { 59 updateEnabled(); 60 } 61 }; 62 updateEnabled(); 63 } 64 65 public void addNotify() { 66 super.addNotify(); 67 sourceList.addListSelectionListener(listListener); 68 sourceList.getModel().addListDataListener(listListener); 69 } 70 71 public void removeNotify() { 72 sourceList.removeListSelectionListener(listListener); 73 sourceList.getModel().removeListDataListener(listListener); 74 super.removeNotify(); 75 } 76 77 void setPlatform(NbPlatform plaf) { 78 this.plaf = plaf; 79 this.model = new PlatformComponentFactory.NbPlatformSourceRootsModel(plaf); 80 sourceList.setModel(model); 81 } 82 83 private void updateEnabled() { 84 removeButton.setEnabled(sourceList.getModel().getSize() > 0 && sourceList.getSelectedIndex() != -1); 86 moveUpButton.setEnabled(sourceList.getSelectionModel().getMinSelectionIndex() > 0); 87 moveDownButton.setEnabled(plaf != null && 88 sourceList.getSelectionModel().getMaxSelectionIndex() < plaf.getSourceRoots().length - 1); 89 } 90 91 96 private void initComponents() { 98 java.awt.GridBagConstraints gridBagConstraints; 99 100 sourceLabel = new javax.swing.JLabel (); 101 sourceSP = new javax.swing.JScrollPane (); 102 sourceList = new javax.swing.JList (); 103 buttonPanel = new javax.swing.JPanel (); 104 addFolderButton = new javax.swing.JButton (); 105 removeButton = new javax.swing.JButton (); 106 moveUpButton = new javax.swing.JButton (); 107 moveDownButton = new javax.swing.JButton (); 108 109 setLayout(new java.awt.GridBagLayout ()); 110 111 setBorder(javax.swing.BorderFactory.createEmptyBorder(12, 12, 12, 12)); 112 sourceLabel.setLabelFor(sourceList); 113 org.openide.awt.Mnemonics.setLocalizedText(sourceLabel, org.openide.util.NbBundle.getMessage(NbPlatformCustomizerSources.class, "LBL_PlatformSources")); 114 gridBagConstraints = new java.awt.GridBagConstraints (); 115 gridBagConstraints.gridx = 0; 116 gridBagConstraints.gridy = 0; 117 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 118 add(sourceLabel, gridBagConstraints); 119 120 sourceList.setCellRenderer(PlatformComponentFactory.getURLListRenderer()); 121 sourceSP.setViewportView(sourceList); 122 123 gridBagConstraints = new java.awt.GridBagConstraints (); 124 gridBagConstraints.gridx = 0; 125 gridBagConstraints.gridy = 1; 126 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 127 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 128 gridBagConstraints.weightx = 1.0; 129 gridBagConstraints.weighty = 1.0; 130 add(sourceSP, gridBagConstraints); 131 132 buttonPanel.setLayout(new java.awt.GridBagLayout ()); 133 134 buttonPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 12, 0, 0)); 135 org.openide.awt.Mnemonics.setLocalizedText(addFolderButton, org.openide.util.NbBundle.getMessage(NbPlatformCustomizerSources.class, "CTL_AddZipOrFolder")); 136 addFolderButton.addActionListener(new java.awt.event.ActionListener () { 137 public void actionPerformed(java.awt.event.ActionEvent evt) { 138 addZipOrFolder(evt); 139 } 140 }); 141 142 gridBagConstraints = new java.awt.GridBagConstraints (); 143 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 144 buttonPanel.add(addFolderButton, gridBagConstraints); 145 146 org.openide.awt.Mnemonics.setLocalizedText(removeButton, org.openide.util.NbBundle.getMessage(NbPlatformCustomizerSources.class, "CTL_Remove")); 147 removeButton.addActionListener(new java.awt.event.ActionListener () { 148 public void actionPerformed(java.awt.event.ActionEvent evt) { 149 removeFolder(evt); 150 } 151 }); 152 153 gridBagConstraints = new java.awt.GridBagConstraints (); 154 gridBagConstraints.gridx = 0; 155 gridBagConstraints.gridy = 1; 156 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 157 gridBagConstraints.insets = new java.awt.Insets (12, 0, 12, 0); 158 buttonPanel.add(removeButton, gridBagConstraints); 159 160 org.openide.awt.Mnemonics.setLocalizedText(moveUpButton, org.openide.util.NbBundle.getMessage(NbPlatformCustomizerSources.class, "CTL_MoveUp")); 161 moveUpButton.addActionListener(new java.awt.event.ActionListener () { 162 public void actionPerformed(java.awt.event.ActionEvent evt) { 163 moveUp(evt); 164 } 165 }); 166 167 gridBagConstraints = new java.awt.GridBagConstraints (); 168 gridBagConstraints.gridx = 0; 169 gridBagConstraints.gridy = 2; 170 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 171 gridBagConstraints.insets = new java.awt.Insets (0, 0, 2, 0); 172 buttonPanel.add(moveUpButton, gridBagConstraints); 173 174 org.openide.awt.Mnemonics.setLocalizedText(moveDownButton, org.openide.util.NbBundle.getMessage(NbPlatformCustomizerSources.class, "CTL_MoveDown")); 175 moveDownButton.addActionListener(new java.awt.event.ActionListener () { 176 public void actionPerformed(java.awt.event.ActionEvent evt) { 177 moveDown(evt); 178 } 179 }); 180 181 gridBagConstraints = new java.awt.GridBagConstraints (); 182 gridBagConstraints.gridx = 0; 183 gridBagConstraints.gridy = 3; 184 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 185 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH; 186 gridBagConstraints.weighty = 1.0; 187 buttonPanel.add(moveDownButton, gridBagConstraints); 188 189 gridBagConstraints = new java.awt.GridBagConstraints (); 190 gridBagConstraints.gridx = 1; 191 gridBagConstraints.gridy = 1; 192 gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL; 193 add(buttonPanel, gridBagConstraints); 194 195 } 197 private void moveDown(java.awt.event.ActionEvent evt) { int[] selIndices = sourceList.getSelectedIndices(); 199 model.moveSourceRootsDown(selIndices); 200 for (int i = 0; i < selIndices.length; i++) { 201 selIndices[i] = ++selIndices[i]; 202 } 203 sourceList.setSelectedIndices(selIndices); 204 } 206 private void moveUp(java.awt.event.ActionEvent evt) { int[] selIndices = sourceList.getSelectedIndices(); 208 model.moveSourceRootsUp(selIndices); 209 for (int i = 0; i < selIndices.length; i++) { 210 selIndices[i] = --selIndices[i]; 211 } 212 sourceList.setSelectedIndices(selIndices); 213 } 215 private void removeFolder(java.awt.event.ActionEvent evt) { Object [] selVals = sourceList.getSelectedValues(); 217 int toSelect = sourceList.getSelectedIndex() - 1; 218 URL [] selURLs = new URL [selVals.length]; 219 System.arraycopy(selVals, 0, selURLs, 0, selVals.length); 220 model.removeSourceRoot(selURLs); 221 sourceList.setSelectedIndex(toSelect); 222 } 224 private void addZipOrFolder(java.awt.event.ActionEvent evt) { JFileChooser chooser = new JFileChooser (ModuleUISettings.getDefault().getLastUsedNbPlatformLocation()); 226 chooser.setAcceptAllFileFilterUsed(false); 227 chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); 228 chooser.setFileFilter(new FileFilter () { 229 public boolean accept(File f) { 230 return f.isDirectory() || isValidNbSourceRoot(f); 231 } 232 public String getDescription() { 233 return getMessage("CTL_SourcesTab"); 234 } 235 }); 236 int ret = chooser.showOpenDialog(this); 237 if (ret == JFileChooser.APPROVE_OPTION) { 238 File file = FileUtil.normalizeFile(chooser.getSelectedFile()); 239 if (!file.exists() || (file.isFile() && !isValidNbSourceRoot(file))) { 240 DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message( 241 getMessage("MSG_NotValidNBSrcZIP"))); 242 } else { 243 ModuleUISettings.getDefault().setLastUsedNbPlatformLocation(file.getParentFile().getAbsolutePath()); 244 URL newUrl = Util.urlForDirOrJar(file); 245 model.addSourceRoot(newUrl); 246 sourceList.setSelectedValue(newUrl, true); 247 } 248 } 249 } 251 private static boolean isValidNbSourceRoot(final File nbSrcRoot) { 252 boolean isValid = false; 253 String lcName = nbSrcRoot.getName().toLowerCase(Locale.US); 254 if (lcName.endsWith(".jar") || lcName.endsWith(".zip")) { try { 256 isValid = GlobalSourceForBinaryImpl.NetBeansSourcesParser.getInstance(nbSrcRoot) != null; 257 } catch (IOException ex) { 258 } 260 } 261 return isValid; 262 } 263 264 private javax.swing.JButton addFolderButton; 266 private javax.swing.JPanel buttonPanel; 267 private javax.swing.JButton moveDownButton; 268 private javax.swing.JButton moveUpButton; 269 private javax.swing.JButton removeButton; 270 private javax.swing.JLabel sourceLabel; 271 private javax.swing.JList sourceList; 272 private javax.swing.JScrollPane sourceSP; 273 275 private void initAccessibility() { 276 addFolderButton.getAccessibleContext().setAccessibleDescription(getMessage("ACS_CTL_addFolderButton")); 277 sourceList.getAccessibleContext().setAccessibleDescription(getMessage("ACS_CTL_sourceList")); 278 moveDownButton.getAccessibleContext().setAccessibleDescription(getMessage("ACS_CTL_moveDownButton")); 279 moveUpButton.getAccessibleContext().setAccessibleDescription(getMessage("ACS_CTL_moveUpButton")); 280 removeButton.getAccessibleContext().setAccessibleDescription(getMessage("ACS_CTL_removeButton")); 281 } 282 283 private String getMessage(String key) { 284 return NbBundle.getMessage(NbPlatformCustomizerSources.class, key); 285 } 286 287 static abstract class ListListener implements ListDataListener , ListSelectionListener { 288 289 public void intervalAdded(final ListDataEvent e) { 290 listChanged(); 291 } 292 293 public void intervalRemoved(final ListDataEvent e) { 294 listChanged(); 295 } 296 297 public void contentsChanged(final ListDataEvent e) { 298 listChanged(); 299 } 300 301 public void valueChanged(final ListSelectionEvent e) { 302 if (!e.getValueIsAdjusting()) { 303 listChanged(); 304 } 305 } 306 307 abstract void listChanged(); 308 309 } 310 311 } 312 | Popular Tags |