1 19 20 21 package org.netbeans.modules.i18n.wizard; 22 23 24 import java.awt.BorderLayout ; 25 import java.awt.Component ; 26 import java.awt.Container ; 27 import java.awt.GridBagConstraints ; 28 import java.awt.GridBagLayout ; 29 import java.util.ArrayList ; 30 import java.util.Map ; 31 import java.util.HashSet ; 32 import java.util.Iterator ; 33 import java.util.Set ; 34 import javax.swing.DefaultComboBoxModel ; 35 import javax.swing.event.ChangeListener ; 36 import javax.swing.JComponent ; 37 import javax.swing.JLabel ; 38 import javax.swing.JPanel ; 39 import org.netbeans.api.java.classpath.ClassPath; 40 41 import org.netbeans.modules.i18n.HardCodedString; 42 import org.netbeans.modules.i18n.I18nSupport; 43 import org.netbeans.modules.i18n.I18nUtil; 44 45 import org.openide.loaders.DataObject; 46 import org.openide.util.HelpCtx; 47 import org.openide.util.NbBundle; 48 import org.openide.WizardDescriptor; 49 50 51 59 final class AdditionalWizardPanel extends JPanel { 60 61 62 private final Map sourceMap = Util.createWizardSourceMap(); 63 64 65 private final Set viewedSources = new HashSet (0); 66 67 68 private JComponent additionalComponent = EMPTY_COMPONENT; 69 70 71 private static final JLabel EMPTY_COMPONENT = 72 new JLabel (Util.getString("TXT_HasNoAdditonal")); 73 74 75 76 private AdditionalWizardPanel() { 77 initComponents(); 78 initA11Y(); 79 80 setComboModel(sourceMap); 82 } 83 84 85 private void initA11Y() { 86 sourceLabel.setLabelFor(sourceCombo); 87 sourceCombo.getAccessibleContext().setAccessibleDescription(Util.getString("ACS_sourceCombo")); 88 } 89 90 91 92 private void setComboModel(Map sourceMap) { 93 Object [] sources = sourceMap.keySet().toArray(); 94 95 ArrayList nonEmptySources = new ArrayList (); 96 97 for (int i = 0; i < sources.length; i++) { 98 if(((SourceData)sourceMap.get(sources[i])).getSupport().hasAdditionalCustomizer()) 99 nonEmptySources.add(sources[i]); 100 } 101 102 sourceCombo.setModel(new DefaultComboBoxModel (nonEmptySources.toArray())); 103 104 Object selected = sourceCombo.getSelectedItem(); 106 updateAdditionalComponent(selected); 107 108 } 109 110 111 Set getViewedSources() { 112 return viewedSources; 113 } 114 115 116 Map getSourceMap() { 117 return sourceMap; 118 } 119 120 121 void setSourceMap(Map sourceMap) { 122 this.sourceMap.clear(); 123 this.sourceMap.putAll(sourceMap); 124 125 setComboModel(sourceMap); 126 } 127 128 129 134 private void initComponents() { 136 java.awt.GridBagConstraints gridBagConstraints; 137 138 sourceLabel = new javax.swing.JLabel (); 139 sourceCombo = new javax.swing.JComboBox (); 140 141 setLayout(new java.awt.GridBagLayout ()); 142 143 org.openide.awt.Mnemonics.setLocalizedText(sourceLabel, NbBundle.getBundle(AdditionalWizardPanel.class).getString("LBL_Source")); gridBagConstraints = new java.awt.GridBagConstraints (); 145 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 146 add(sourceLabel, gridBagConstraints); 147 148 sourceCombo.setRenderer(new SourceWizardPanel.DataObjectListCellRenderer()); 149 sourceCombo.addActionListener(new java.awt.event.ActionListener () { 150 public void actionPerformed(java.awt.event.ActionEvent evt) { 151 sourceComboActionPerformed(evt); 152 } 153 }); 154 gridBagConstraints = new java.awt.GridBagConstraints (); 155 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 156 gridBagConstraints.weightx = 1.0; 157 gridBagConstraints.insets = new java.awt.Insets (0, 11, 0, 0); 158 add(sourceCombo, gridBagConstraints); 159 } 161 private void sourceComboActionPerformed(java.awt.event.ActionEvent evt) { Object selected = sourceCombo.getSelectedItem(); 163 updateAdditionalComponent(selected); 164 } 166 private void updateAdditionalComponent(Object selected) { 167 168 I18nSupport support = null; 169 170 if (selected != null) { 171 support = ((SourceData)sourceMap.get(selected)).getSupport(); 172 } 173 174 remove(additionalComponent); 176 177 if(support != null && support.hasAdditionalCustomizer()) { 178 additionalComponent = support.getAdditionalCustomizer(); 179 viewedSources.add(selected); 180 } else { 181 additionalComponent = EMPTY_COMPONENT; 182 } 183 184 GridBagConstraints gridBagConstraints = new GridBagConstraints (); 186 gridBagConstraints.gridx = 0; 187 gridBagConstraints.gridy = 1; 188 gridBagConstraints.gridwidth = 2; 189 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 190 gridBagConstraints.weightx = 1.0; 191 gridBagConstraints.weighty = 1.0; 192 add(additionalComponent, gridBagConstraints); 193 194 revalidate(); 195 } 196 197 198 private javax.swing.JComboBox sourceCombo; 200 private javax.swing.JLabel sourceLabel; 201 203 205 public static class Panel extends I18nWizardDescriptor.Panel { 206 207 208 private final JLabel emptyLabel; 209 210 211 private transient AdditionalWizardPanel additionalPanel; 212 213 Panel () { 214 emptyLabel = new JLabel (Util.getString("TXT_HasNoAdditonal")); 215 emptyLabel.setHorizontalAlignment(JLabel.CENTER); 216 emptyLabel.setVerticalAlignment(JLabel.CENTER); 217 } 218 219 220 222 protected Component createComponent() { 223 JPanel panel = new JPanel (); 224 225 panel.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(AdditionalWizardPanel.class).getString("ACS_AdditionalWizardPanel")); 227 228 panel.putClientProperty("WizardPanel_contentSelectedIndex", new Integer (2)); panel.setName(NbBundle.getBundle(getClass()).getString("TXT_ModifyAdditional")); 230 panel.setPreferredSize(I18nWizardDescriptor.PREFERRED_DIMENSION); 231 232 panel.setLayout(new GridBagLayout ()); 233 GridBagConstraints constraints = new GridBagConstraints (); 234 constraints.weightx = 1.0; 235 constraints.weighty = 1.0; 236 constraints.fill = GridBagConstraints.BOTH; 237 panel.add(getUI(), constraints); 238 239 return panel; 240 } 241 242 243 public void readSettings(Object settings) { 244 super.readSettings(settings); 245 getUI().setSourceMap(getMap()); 246 247 JPanel panel = (JPanel )getComponent(); 248 if(hasAdditional(getMap())) { 249 if(panel.isAncestorOf(emptyLabel)) { 250 panel.remove(emptyLabel); 251 GridBagConstraints constraints = new GridBagConstraints (); 252 constraints.weightx = 1.0; 253 constraints.weighty = 1.0; 254 constraints.fill = GridBagConstraints.BOTH; 255 panel.add(getUI(), constraints); 256 } 257 } else { 258 if(panel.isAncestorOf(getUI())) { 259 panel.remove(getUI()); 260 GridBagConstraints constraints = new GridBagConstraints (); 261 constraints.weightx = 1.0; 262 constraints.weighty = 1.0; 263 constraints.fill = GridBagConstraints.BOTH; 264 panel.add(emptyLabel, constraints); 265 } 266 } 267 } 268 269 270 public void storeSettings(Object settings) { 271 super.storeSettings(settings); 272 Map sourceMap = getUI().getSourceMap(); 274 Iterator it = getUI().getViewedSources().iterator(); 275 276 while(it.hasNext()) { 277 SourceData sourceData = (SourceData)sourceMap.get(it.next()); 278 279 Object [] hcStrings = sourceData.getStringMap().keySet().toArray(); 280 281 for(int i=0; i<hcStrings.length; i++) { 282 sourceData.getStringMap().put(hcStrings[i], sourceData.getSupport().getDefaultI18nString((HardCodedString)hcStrings[i])); 284 } 285 } 286 287 getMap().clear(); 289 getMap().putAll(sourceMap); 290 } 291 292 294 public void doLongTimeChanges() { 295 ProgressWizardPanel progressPanel = new ProgressWizardPanel(true); 297 298 showProgressPanel(progressPanel); 299 300 progressPanel.setMainText(NbBundle.getBundle(getClass()).getString("LBL_AdditionalIn")); 301 progressPanel.setMainProgress(0); 302 303 Map sourceMap = ((AdditionalWizardPanel)getComponent()).getSourceMap(); 305 Iterator it = ((AdditionalWizardPanel)getComponent()).getViewedSources().iterator(); 306 307 for(int i=0; it.hasNext(); i++) { 308 DataObject source = (DataObject)it.next(); 309 310 SourceData sourceData = (SourceData)sourceMap.get(source); 311 312 ClassPath cp = ClassPath.getClassPath( source.getPrimaryFile(), ClassPath.SOURCE ); 313 314 progressPanel.setMainText(NbBundle.getBundle(getClass()).getString("LBL_AdditionalIn")+" "+cp.getResourceName( source.getPrimaryFile(), '.', false ) ); 316 Object [] hcStrings = sourceData.getStringMap().keySet().toArray(); 317 318 for(int j=0; i<hcStrings.length; j++) { 319 sourceData.getStringMap().put(hcStrings[j], sourceData.getSupport().getDefaultI18nString((HardCodedString)hcStrings[j])); 321 } 323 progressPanel.setMainProgress((int)((i+1)/(float)sourceMap.size() * 100)); 324 } } 326 327 328 private void showProgressPanel(ProgressWizardPanel progressPanel) { 329 ((Container )getComponent()).remove(getUI()); 330 GridBagConstraints constraints = new GridBagConstraints (); 331 constraints.weightx = 1.0; 332 constraints.weighty = 1.0; 333 constraints.fill = GridBagConstraints.BOTH; 334 ((Container )getComponent()).add(progressPanel, constraints); 335 ((JComponent )getComponent()).revalidate(); 336 getComponent().repaint(); 337 } 338 339 340 public void reset() { 341 Container container = (Container )getComponent(); 342 343 if(!container.isAncestorOf(getUI())) { 344 container.removeAll(); 345 GridBagConstraints constraints = new GridBagConstraints (); 346 constraints.weightx = 1.0; 347 constraints.weighty = 1.0; 348 constraints.fill = GridBagConstraints.BOTH; 349 container.add(getUI(), constraints); 350 } 351 } 352 353 354 public HelpCtx getHelp() { 355 return new HelpCtx(I18nUtil.HELP_ID_WIZARD); 356 } 357 358 360 private static boolean hasAdditional(Map sourceMap) { 361 Iterator it = sourceMap.keySet().iterator(); 362 363 while(it.hasNext()) { 364 SourceData sourceData = (SourceData)sourceMap.get(it.next()); 365 if(sourceData.getSupport().hasAdditionalCustomizer()) 366 return true; 367 } 368 369 return false; 370 } 371 372 private synchronized AdditionalWizardPanel getUI() { 373 if (additionalPanel == null) { 374 additionalPanel = new AdditionalWizardPanel(); 375 } 376 return additionalPanel; 377 } 378 } 380 } 381 | Popular Tags |