1 19 20 21 package org.netbeans.modules.i18n.wizard; 22 23 24 import java.awt.Component ; 25 import java.beans.BeanInfo ; 26 import java.util.Iterator ; 27 import java.util.Map ; 28 import javax.swing.DefaultListCellRenderer ; 29 import javax.swing.event.ListSelectionEvent ; 30 import javax.swing.event.ListSelectionListener ; 31 import javax.swing.ImageIcon ; 32 import javax.swing.JLabel ; 33 import javax.swing.JList ; 34 import javax.swing.JPanel ; 35 import org.netbeans.api.java.classpath.ClassPath; 36 import org.netbeans.api.project.Project; 37 38 import org.netbeans.modules.i18n.FactoryRegistry; 39 import org.netbeans.modules.i18n.I18nUtil; 40 41 import org.openide.loaders.DataFolder; 42 import org.openide.loaders.DataObject; 43 import org.openide.nodes.Node; 44 import org.openide.nodes.NodeAcceptor; 45 import org.openide.nodes.NodeOperation; 46 import org.openide.util.HelpCtx; 47 import org.openide.util.NbBundle; 48 import org.openide.util.UserCancelException; 49 import org.netbeans.modules.i18n.SelectorUtils; 50 51 57 final class SourceWizardPanel extends JPanel { 58 59 60 private final Map sourceMap = Util.createWizardSourceMap(); 61 62 65 private final Panel descPanel; 66 67 70 private boolean testRole = false; 71 72 74 private SourceWizardPanel(Panel descPanel, boolean testRole) { 75 this.descPanel = descPanel; 76 this.testRole = testRole; 77 78 initComponents(); 79 80 initAccessibility (); 81 82 setPreferredSize(I18nWizardDescriptor.PREFERRED_DIMENSION); 83 84 initList(); 85 86 putClientProperty("WizardPanel_contentSelectedIndex", new Integer (0)); 88 if (testRole) { 89 setName(Util.getString("TXT_SelecTestSources")); 90 } else { 91 setName(Util.getString("TXT_SelectSources")); 92 } 93 } 94 95 96 97 public Map getSourceMap() { 98 return sourceMap; 99 } 100 101 102 public void setSourceMap(Map sourceMap) { 103 this.sourceMap.clear(); 104 this.sourceMap.putAll(sourceMap); 105 106 sourcesList.setListData(sourceMap.keySet().toArray()); 107 108 descPanel.fireStateChanged(); 109 } 110 111 114 private String getPanelDescription() { 115 if (testRole == false) { 116 return Util.getString("MSG_SourcesPanel_desc"); 117 } else { 118 return Util.getString("MSG_SourcesPanel_test_desc"); 119 } 120 } 121 122 125 private String getAccessibleListDescription() { 126 if (testRole == false) { 127 return Util.getString("ACSD_sourcesList"); 128 } else { 129 return Util.getString("ACSD_sourcesList_test"); 130 } 131 } 132 133 134 137 private void initList() { 138 sourcesList.getSelectionModel().addListSelectionListener( 139 new ListSelectionListener () { 140 public void valueChanged(ListSelectionEvent evt) { 141 removeButton.setEnabled(!sourcesList.isSelectionEmpty()); 142 } 143 } 144 ); 145 146 removeButton.setEnabled(!sourcesList.isSelectionEmpty()); 147 } 148 149 private void initAccessibility() { 150 getAccessibleContext().setAccessibleDescription(getPanelDescription()); 151 152 addButton.setToolTipText(Util.getString("CTL_AddSource_desc")); 153 154 removeButton.setToolTipText(Util.getString("CTL_RemoveSource_desc")); 155 156 sourcesList.getAccessibleContext().setAccessibleName(Util.getString("ACSN_sourcesList")); 157 sourcesList.getAccessibleContext().setAccessibleDescription(getAccessibleListDescription()); 158 } 159 160 165 private void initComponents() { 167 java.awt.GridBagConstraints gridBagConstraints; 168 169 descTextArea = new javax.swing.JTextArea (); 170 jScrollPane1 = new javax.swing.JScrollPane (); 171 sourcesList = new javax.swing.JList (); 172 addButton = new javax.swing.JButton (); 173 removeButton = new javax.swing.JButton (); 174 175 setLayout(new java.awt.GridBagLayout ()); 176 177 descTextArea.setEditable(false); 178 descTextArea.setLineWrap(true); 179 descTextArea.setText(getPanelDescription()); 180 descTextArea.setWrapStyleWord(true); 181 descTextArea.setDisabledTextColor(new JLabel ().getForeground()); 182 descTextArea.setEnabled(false); 183 descTextArea.setOpaque(false); 184 gridBagConstraints = new java.awt.GridBagConstraints (); 185 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 186 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 187 add(descTextArea, gridBagConstraints); 188 189 sourcesList.setCellRenderer(new DataObjectListCellRenderer()); 190 jScrollPane1.setViewportView(sourcesList); 191 192 gridBagConstraints = new java.awt.GridBagConstraints (); 193 gridBagConstraints.gridx = 0; 194 gridBagConstraints.gridy = 1; 195 gridBagConstraints.gridheight = 2; 196 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 197 gridBagConstraints.weightx = 1.0; 198 gridBagConstraints.weighty = 1.0; 199 gridBagConstraints.insets = new java.awt.Insets (5, 0, 0, 0); 200 add(jScrollPane1, gridBagConstraints); 201 202 org.openide.awt.Mnemonics.setLocalizedText(addButton, NbBundle.getBundle(SourceWizardPanel.class).getString("CTL_AddSource")); addButton.addActionListener(new java.awt.event.ActionListener () { 204 public void actionPerformed(java.awt.event.ActionEvent evt) { 205 addButtonActionPerformed(evt); 206 } 207 }); 208 gridBagConstraints = new java.awt.GridBagConstraints (); 209 gridBagConstraints.gridx = 1; 210 gridBagConstraints.gridy = 1; 211 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 212 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 213 gridBagConstraints.insets = new java.awt.Insets (5, 11, 0, 0); 214 add(addButton, gridBagConstraints); 215 216 org.openide.awt.Mnemonics.setLocalizedText(removeButton, NbBundle.getBundle(SourceWizardPanel.class).getString("CTL_RemoveSource")); removeButton.addActionListener(new java.awt.event.ActionListener () { 218 public void actionPerformed(java.awt.event.ActionEvent evt) { 219 removeButtonActionPerformed(evt); 220 } 221 }); 222 gridBagConstraints = new java.awt.GridBagConstraints (); 223 gridBagConstraints.gridx = 1; 224 gridBagConstraints.gridy = 2; 225 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 226 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH; 227 gridBagConstraints.weighty = 1.0; 228 gridBagConstraints.insets = new java.awt.Insets (5, 11, 0, 0); 229 add(removeButton, gridBagConstraints); 230 } 232 private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) { Object [] selected = sourcesList.getSelectedValues(); 234 235 for(int i=0; i<selected.length; i++) { 236 sourceMap.remove(selected[i]); 237 } 238 239 sourcesList.setListData(sourceMap.keySet().toArray()); 240 241 descPanel.fireStateChanged(); 242 } 244 private void addButtonActionPerformed(java.awt.event.ActionEvent evt) { 246 248 Project prj = descPanel.getProject(); 249 250 try { 252 Node[] selectedNodes= NodeOperation.getDefault().select( 253 Util.getString("LBL_SelectSources"), 254 Util.getString("LBL_Filesystems"), 255 SelectorUtils.sourcesNode(prj, SelectorUtils.ALL_FILTER), 256 new NodeAcceptor() { 257 public boolean acceptNodes(Node[] nodes) { 258 if(nodes == null || nodes.length == 0) { 259 return false; 260 } 261 262 for(int i=0; i<nodes.length; i++) { 263 Object dataObject = nodes[i].getCookie(DataObject.class); 265 if (dataObject == null) { 266 return false; 267 } 268 if (dataObject instanceof DataFolder) { 270 if (I18nUtil.containsAcceptedDataObject((DataFolder) dataObject)) { 271 return true; 272 } 273 } else if (FactoryRegistry.hasFactory(dataObject.getClass())) { 274 return true; 276 } 277 } 278 279 return false; 280 } 281 } 282 ); 283 284 for(int i=0; i<selectedNodes.length; i++) { 285 DataObject dataObject = (DataObject)selectedNodes[i].getCookie(DataObject.class); 286 287 if (dataObject instanceof DataFolder) { 288 Iterator it = I18nUtil.getAcceptedDataObjects((DataFolder)dataObject).iterator(); 290 while (it.hasNext()) { 291 Util.addSource(sourceMap, (DataObject)it.next()); 292 } 293 } else { 294 Util.addSource(sourceMap, dataObject); 295 } 296 } 297 298 sourcesList.setListData(sourceMap.keySet().toArray()); 299 300 descPanel.fireStateChanged(); 301 } catch (UserCancelException uce) { 302 } 305 } 307 private javax.swing.JButton addButton; 309 private javax.swing.JTextArea descTextArea; 310 private javax.swing.JScrollPane jScrollPane1; 311 private javax.swing.JButton removeButton; 312 private javax.swing.JList sourcesList; 313 315 316 317 public static class DataObjectListCellRenderer extends DefaultListCellRenderer { 318 public Component getListCellRendererComponent( 319 JList list, 320 Object value, int index, boolean isSelected, boolean cellHasFocus) { 325 JLabel label = (JLabel )super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); 326 327 DataObject dataObject = (DataObject)value; 328 329 if(dataObject != null) { 330 ClassPath cp = ClassPath.getClassPath( dataObject.getPrimaryFile(), ClassPath.SOURCE ); 331 332 label.setText(cp.getResourceName( dataObject.getPrimaryFile(), '.', false )); label.setIcon(new ImageIcon (dataObject.getNodeDelegate().getIcon(BeanInfo.ICON_COLOR_16x16))); 334 } else { 335 label.setText(""); label.setIcon(null); 337 } 338 339 return label; 340 } 341 } 342 343 344 345 348 public static class Panel extends I18nWizardDescriptor.Panel { 349 350 351 private final boolean testWizard; 352 353 354 355 public Panel() { 356 this(false); 357 } 358 359 360 public Panel(boolean testWizard) { 361 this.testWizard = testWizard; 362 } 363 364 365 367 protected Component createComponent() { 368 Component component = new SourceWizardPanel(this, testWizard); 369 370 return component; 371 } 372 373 374 public boolean isValid() { 375 return !((SourceWizardPanel)getComponent()).getSourceMap().isEmpty(); 376 } 377 378 379 public void readSettings(Object settings) { 380 super.readSettings(settings); 381 ((SourceWizardPanel)getComponent()).setSourceMap(getMap()); 382 } 383 384 385 public void storeSettings(Object settings) { 386 super.storeSettings(settings); 387 super.storeSettings(settings); 388 getMap().clear(); 390 getMap().putAll(((SourceWizardPanel)getComponent()).getSourceMap()); 391 } 392 393 394 public HelpCtx getHelp() { 395 if(testWizard) 396 return new HelpCtx(I18nUtil.HELP_ID_TESTING); 397 else 398 return new HelpCtx(I18nUtil.HELP_ID_WIZARD); 399 } 400 401 } 403 } 404 | Popular Tags |