1 19 20 21 package org.netbeans.modules.i18n; 22 23 24 import java.beans.PropertyChangeEvent ; 25 import java.beans.PropertyChangeListener ; 26 import java.io.IOException ; 27 import java.util.ResourceBundle ; 28 import javax.swing.JButton ; 29 import javax.swing.JPanel ; 30 import javax.swing.SwingUtilities ; 31 import org.netbeans.api.java.classpath.ClassPath; 32 33 import org.openide.filesystems.FileObject; 34 import org.openide.loaders.DataObject; 35 import org.openide.util.HelpCtx; 36 import org.openide.util.WeakListeners; 37 import org.openide.util.Lookup; 38 import org.netbeans.api.javahelp.Help; 39 import org.netbeans.api.project.Project; 40 import javax.swing.BoxLayout ; 41 42 43 50 public class I18nPanel extends JPanel { 51 52 53 private I18nString i18nString; 54 55 56 private ResourceBundle bundle; 57 58 59 private PropertyChangeListener propListener; 60 61 62 static final long serialVersionUID =-6982482491017390786L; 63 64 private Project project; 65 66 private FileObject file; 67 68 static final long ALL_BUTTONS = 0xffffff; 69 static final long NO_BUTTONS = 0x0; 70 static final long REPLACE_BUTTON = 0xf0000; 71 static final long SKIP_BUTTON = 0x0f000; 72 static final long INFO_BUTTON = 0x00f00; 73 static final long CANCEL_BUTTON = 0x000f0; 74 static final long HELP_BUTTON = 0x0000f; 75 76 77 78 86 public I18nPanel(PropertyPanel propertyPanel, Project project, FileObject file) { 87 this(propertyPanel, true, project, file); 88 } 89 90 96 public I18nPanel(PropertyPanel propertyPanel, boolean withButtons, Project project, FileObject file) { 97 this.project = project; 98 this.file = file; 99 this.propertyPanel = propertyPanel; 100 this.propertyPanel.setFile(file); 101 this.propertyPanel.setEnabled(project != null); 102 103 bundle = I18nUtil.getBundle(); 105 106 initComponents(); 107 myInitComponents(); 108 initAccessibility(); 109 110 if(!withButtons) 111 remove(buttonsPanel); 112 113 emptyPanel = new EmptyPropertyPanel(); 115 contentsShown = true; 116 117 showBundleMessage("TXT_SearchingForStrings"); 118 } 119 120 121 private boolean contentsShown; 122 123 public void showBundleMessage(String bundleKey) { 124 125 emptyPanel.setBundleText(bundleKey); 126 if (contentsShown) { 127 contentsPanelPlaceholder.remove(propertyPanel); 128 contentsPanelPlaceholder.add(emptyPanel); 129 contentsPanelPlaceholder.validate(); 130 contentsPanelPlaceholder.repaint(); 131 contentsShown = false; 132 } 133 buttonsEnableDisable(); 134 } 135 136 public void showPropertyPanel() { 137 if (!contentsShown) { 138 contentsPanelPlaceholder.remove(emptyPanel); 139 contentsPanelPlaceholder.add(propertyPanel); 140 contentsPanelPlaceholder.validate(); 141 contentsPanelPlaceholder.repaint(); 142 contentsShown = true; 143 } 144 buttonsEnableDisable(); 145 } 146 147 148 149 152 162 166 public void setFile(FileObject file) { 167 propertyPanel.setFile(file); 169 } 170 171 175 public FileObject getFile() { 176 return propertyPanel.getFile(); 178 } 179 180 181 182 public void addNotify() { 183 super.addNotify(); 184 185 if(SwingUtilities.isDescendingFrom(replaceButton, this)) 186 getRootPane().setDefaultButton(replaceButton); 187 } 188 189 190 public I18nString getI18nString() { 191 return i18nString; 192 } 193 194 195 public void setI18nString(I18nString i18nString) { 196 this.i18nString = i18nString; 197 198 propertyPanel.setI18nString(i18nString); 199 201 showPropertyPanel(); 202 } 203 204 205 JButton getReplaceButton() { 206 return replaceButton; 207 } 208 209 210 JButton getSkipButton() { 211 return skipButton; 212 } 213 214 215 JButton getInfoButton() { 216 return infoButton; 217 } 218 219 220 JButton getCancelButton() { 221 return cancelButton; 222 } 223 224 225 private void buttonsEnableDisable() { 226 if (contentsShown) { 227 enableButtons(ALL_BUTTONS); 228 boolean isBundle = (i18nString != null) && (i18nString.getSupport().getResourceHolder().getResource() != null); 229 boolean keyEmpty = (getI18nString()==null || 230 getI18nString().getKey()==null || 231 getI18nString().getKey().trim().length()==0); 232 replaceButton.setEnabled(isBundle && !keyEmpty); 233 } else 234 enableButtons(CANCEL_BUTTON | HELP_BUTTON); 235 236 } 237 238 public void setDefaultResource(DataObject dataObject) { 239 if (dataObject != null) { 240 FileObject fo = dataObject.getPrimaryFile(); 242 ClassPath cp = ClassPath.getClassPath( fo, ClassPath.SOURCE ); 243 244 FileObject folder = cp.findResource( cp.getResourceName( fo.getParent() ) ); 245 246 while( folder != null && cp.contains( folder ) ) { 247 248 String rn = cp.getResourceName( folder ) + "/Bundle.properties"; 250 FileObject peer = cp.findResource( rn ); 251 if (peer != null) { 252 try { 253 DataObject peerDataObject = DataObject.find(peer); 254 propertyPanel.setResource(peerDataObject); 256 return; 257 } catch (IOException ex) { 258 } 260 } 261 folder = folder.getParent(); 262 } 263 } 264 } 265 266 267 271 private void initAccessibility() { 272 this.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_I18nPanel")); 273 skipButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_CTL_SkipButton")); 274 cancelButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_CTL_CancelButton")); 275 replaceButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_CTL_ReplaceButton")); 276 infoButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_CTL_InfoButton")); 277 helpButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_CTL_HelpButton")); 278 } 279 280 285 private void initComponents() { 287 java.awt.GridBagConstraints gridBagConstraints; 288 289 fillPanel = new javax.swing.JPanel (); 290 buttonsPanel = new javax.swing.JPanel (); 291 replaceButton = new javax.swing.JButton (); 292 skipButton = new javax.swing.JButton (); 293 infoButton = new javax.swing.JButton (); 294 cancelButton = new javax.swing.JButton (); 295 helpButton = new javax.swing.JButton (); 296 contentsPanelPlaceholder = new javax.swing.JPanel (); 297 298 setLayout(new java.awt.GridBagLayout ()); 299 gridBagConstraints = new java.awt.GridBagConstraints (); 300 gridBagConstraints.gridx = 0; 301 gridBagConstraints.gridy = 1; 302 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 303 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 304 gridBagConstraints.weightx = 1.0; 305 add(fillPanel, gridBagConstraints); 306 307 buttonsPanel.setLayout(new java.awt.GridLayout (1, 5, 5, 0)); 308 309 org.openide.awt.Mnemonics.setLocalizedText(replaceButton, bundle.getString("CTL_ReplaceButton")); buttonsPanel.add(replaceButton); 311 312 org.openide.awt.Mnemonics.setLocalizedText(skipButton, bundle.getString("CTL_SkipButton")); buttonsPanel.add(skipButton); 314 315 org.openide.awt.Mnemonics.setLocalizedText(infoButton, bundle.getString("CTL_InfoButton")); buttonsPanel.add(infoButton); 317 318 org.openide.awt.Mnemonics.setLocalizedText(cancelButton, bundle.getString("CTL_CloseButton")); buttonsPanel.add(cancelButton); 320 321 org.openide.awt.Mnemonics.setLocalizedText(helpButton, bundle.getString("CTL_HelpButton")); helpButton.addActionListener(new java.awt.event.ActionListener () { 323 public void actionPerformed(java.awt.event.ActionEvent evt) { 324 helpButtonActionPerformed(evt); 325 } 326 }); 327 buttonsPanel.add(helpButton); 328 329 gridBagConstraints = new java.awt.GridBagConstraints (); 330 gridBagConstraints.gridx = 1; 331 gridBagConstraints.gridy = 1; 332 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 333 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 334 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 335 gridBagConstraints.insets = new java.awt.Insets (17, 0, 11, 11); 336 add(buttonsPanel, gridBagConstraints); 337 338 contentsPanelPlaceholder.setLayout(new javax.swing.BoxLayout (contentsPanelPlaceholder, javax.swing.BoxLayout.Y_AXIS)); 339 gridBagConstraints = new java.awt.GridBagConstraints (); 340 gridBagConstraints.gridx = 0; 341 gridBagConstraints.gridy = 0; 342 gridBagConstraints.gridwidth = 2; 343 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 344 gridBagConstraints.weightx = 1.0; 345 gridBagConstraints.weighty = 1.0; 346 add(contentsPanelPlaceholder, gridBagConstraints); 347 } 349 private void myInitComponents() { 350 354 contentsShown = true; 357 contentsPanelPlaceholder.add(propertyPanel); 358 359 360 propertyPanel.addPropertyChangeListener(PropertyPanel.PROP_STRING, 361 new PropertyChangeListener () { 362 public void propertyChange(PropertyChangeEvent evt) { 363 buttonsEnableDisable(); 364 } 365 }); 366 367 propertyPanel.addPropertyChangeListener(PropertyPanel.PROP_RESOURCE, 368 new PropertyChangeListener () { 370 public void propertyChange(PropertyChangeEvent evt) { 371 if(PropertyPanel.PROP_RESOURCE.equals(evt.getPropertyName())) { 372 buttonsEnableDisable(); 373 } 375 } 376 } 377 ); 380 381 } 382 383 private void helpButtonActionPerformed(java.awt.event.ActionEvent evt) { HelpCtx help = new HelpCtx(I18nUtil.HELP_ID_AUTOINSERT); 385 386 String sysprop = System.getProperty("org.openide.actions.HelpAction.DEBUG"); 388 if("true".equals(sysprop) || "full".equals(sysprop)) System.err.println ("I18n module: Help button showing: " + help); 391 Help helpSystem = (Help)Lookup.getDefault().lookup(Help.class); 392 helpSystem.showHelp(help); 393 } 395 private void enableButtons(long buttonMask) { 396 replaceButton.setEnabled((buttonMask & REPLACE_BUTTON) != 0); 397 skipButton.setEnabled((buttonMask & SKIP_BUTTON) != 0); 398 infoButton.setEnabled((buttonMask & INFO_BUTTON) != 0); 399 cancelButton.setEnabled((buttonMask & CANCEL_BUTTON) != 0); 400 helpButton.setEnabled((buttonMask & HELP_BUTTON) != 0); 401 } 402 403 404 405 406 private javax.swing.JPanel buttonsPanel; 408 private javax.swing.JButton cancelButton; 409 private javax.swing.JPanel contentsPanelPlaceholder; 410 private javax.swing.JPanel fillPanel; 411 private javax.swing.JButton helpButton; 412 private javax.swing.JButton infoButton; 413 private javax.swing.JButton replaceButton; 414 private javax.swing.JButton skipButton; 415 417 private EmptyPropertyPanel emptyPanel; 418 private PropertyPanel propertyPanel; 420 422 } 423 | Popular Tags |