diff --git a/i18n/src/org/netbeans/modules/i18n/Bundle.properties b/i18n/src/org/netbeans/modules/i18n/Bundle.properties --- a/i18n/src/org/netbeans/modules/i18n/Bundle.properties +++ b/i18n/src/org/netbeans/modules/i18n/Bundle.properties @@ -100,6 +100,8 @@ LBL_Replace=Replace: ACS_I18nPanel=This panel allows you to choose or create a bundle, set the replace string format, and set property keys and values. CTL_CloseButton=&Close +CTL_AddCommentButton=&AddNOI18N +ACS_CTL_AddCommentButton=N/A #I18nUtil TXT_Alnum=any character of [:digit:] or [:alpha:] class diff --git a/i18n/src/org/netbeans/modules/i18n/HardCodedString.java b/i18n/src/org/netbeans/modules/i18n/HardCodedString.java --- a/i18n/src/org/netbeans/modules/i18n/HardCodedString.java +++ b/i18n/src/org/netbeans/modules/i18n/HardCodedString.java @@ -62,13 +62,26 @@ private Position endPosition; + /** End of line position of hard coded string. */ + private Position endOfLinePosition; + + /** Creates new HardCodedString. */ public HardCodedString(String text, Position startPosition, Position endPosition) { this.text = text; this.startPosition = startPosition; this.endPosition = endPosition; + this.endOfLinePosition = null; } + + /** Creates new HardCodedString. */ + public HardCodedString(String text, Position startPosition, Position endPosition, Position endOfLinePosition) { + this.text = text; + this.startPosition = startPosition; + this.endPosition = endPosition; + this.endOfLinePosition = endOfLinePosition; + } /** Getter for hard coded value. Text without double quotes. */ public String getText() { @@ -89,4 +102,10 @@ public int getLength() { return endPosition.getOffset() - startPosition.getOffset(); } + + /** Getter for the end of line position. */ + public Position getEndOfLinePosition() { + return endOfLinePosition; + } + } diff --git a/i18n/src/org/netbeans/modules/i18n/I18nManager.java b/i18n/src/org/netbeans/modules/i18n/I18nManager.java --- a/i18n/src/org/netbeans/modules/i18n/I18nManager.java +++ b/i18n/src/org/netbeans/modules/i18n/I18nManager.java @@ -69,6 +69,8 @@ * @author Peter Zavadsky */ public class I18nManager { + /** Constans value of comment*/ + private final String noi18n=" //NOI18N"; /** Singleton instance of I18nManager. */ private static I18nManager manager; @@ -220,7 +222,7 @@ showDialog(); } - + /** Replaces current found hard coded string and continue the search for next one. */ private void replace() { I18nString i18nString = null; @@ -253,6 +255,37 @@ } }); } + /** Add "NOI18N" comment to the end of the line */ + private void addComment(){ + I18nString i18nString = null; + + try { + // To call weak without check have to be save here cause strong reference in the top component have to exist. + i18nString = i18nPanelWRef.get().getI18nString(); + + } catch (IllegalStateException e) { + NotifyDescriptor.Message nd = new NotifyDescriptor.Message( + I18nUtil.getBundle().getString("EXC_BadKey"), //NOI18N + NotifyDescriptor.ERROR_MESSAGE); + DialogDisplayer.getDefault().notify(nd); + return; + } + + if (support.hasAdditionalCustomizer()) { + support.performAdditionalChanges(); + } + + support.getReplacer().replace(hcString, i18nString, noi18n); + + // Replace hardcoded string. + + + SwingUtilities.invokeLater(new Runnable() { + public void run() { + skip(); + } + }); + } /** Skips foudn hard coded string and conitnue to search for next one. */ private void skip() { @@ -322,7 +355,10 @@ showInfo(); } else if (source == panel.getCancelButton()) { cancel(); + } else if (source == panel.getAddCommentButton()) { + addComment(); } + } }; @@ -330,6 +366,7 @@ i18nPanel.getSkipButton().addActionListener(listener); i18nPanel.getInfoButton().addActionListener(listener); i18nPanel.getCancelButton().addActionListener(listener); + i18nPanel.getAddCommentButton().addActionListener(listener); // Reset weak reference. i18nPanelWRef = new WeakReference(i18nPanel); diff --git a/i18n/src/org/netbeans/modules/i18n/I18nPanel.java b/i18n/src/org/netbeans/modules/i18n/I18nPanel.java --- a/i18n/src/org/netbeans/modules/i18n/I18nPanel.java +++ b/i18n/src/org/netbeans/modules/i18n/I18nPanel.java @@ -102,13 +102,14 @@ private FileObject file; - static final long ALL_BUTTONS = 0xffffff; + static final long ALL_BUTTONS = 0xfffffff; static final long NO_BUTTONS = 0x0; - static final long REPLACE_BUTTON = 0xf0000; - static final long SKIP_BUTTON = 0x0f000; - static final long INFO_BUTTON = 0x00f00; - static final long CANCEL_BUTTON = 0x000f0; - static final long HELP_BUTTON = 0x0000f; + static final long REPLACE_BUTTON = 0xf00000; + static final long SKIP_BUTTON = 0x0f0000; + static final long INFO_BUTTON = 0x00f000; + static final long CANCEL_BUTTON = 0x000f00; + static final long HELP_BUTTON = 0x0000f0; + static final long ADDCOMMET_BUTTON = 0x00000f; @@ -253,6 +254,12 @@ return cancelButton; } + /** addComment button accessor. */ + JButton getAddCommentButton() { + assert withButtons; + return addCommentButton; + } + /** Enables/disables buttons based on the contents of the dialog. */ private void buttonsEnableDisable() { if (!withButtons) { @@ -319,6 +326,8 @@ bundle.getString("ACS_CTL_InfoButton")); //NOI18N helpButton.getAccessibleContext().setAccessibleDescription( bundle.getString("ACS_CTL_HelpButton")); //NOI18N + addCommentButton.getAccessibleContext().setAccessibleDescription( + bundle.getString("ACS_CTL_AddCommentButton")); //NOI18N } } @@ -338,12 +347,15 @@ infoButton = new JButton(); cancelButton = new JButton(); helpButton = new JButton(); + addCommentButton = new JButton(); Mnemonics.setLocalizedText(replaceButton, bundle.getString("CTL_ReplaceButton")); // NOI18N Mnemonics.setLocalizedText(skipButton, bundle.getString("CTL_SkipButton")); // NOI18N Mnemonics.setLocalizedText(infoButton, bundle.getString("CTL_InfoButton")); // NOI18N Mnemonics.setLocalizedText(cancelButton, bundle.getString("CTL_CloseButton")); // NOI18N Mnemonics.setLocalizedText(helpButton, bundle.getString("CTL_HelpButton")); // NOI18N +Mnemonics.setLocalizedText(addCommentButton, bundle.getString("CTL_AddCommentButton")); // NOI18N + helpButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { @@ -370,12 +382,15 @@ .addPreferredGap(RELATED) .add(cancelButton) .addPreferredGap(RELATED) - .add(helpButton))); - layout.linkSize(new Component[] {cancelButton, - helpButton, + .add(helpButton) + .addPreferredGap(RELATED) + .add(addCommentButton) )); + layout.linkSize(new Component[] {cancelButton, + helpButton, infoButton, replaceButton, - skipButton}, + skipButton, + addCommentButton}, GroupLayout.HORIZONTAL); } else { horizGroup.add(contentsPanelPlaceholder); @@ -394,7 +409,8 @@ .add(cancelButton) .add(infoButton) .add(skipButton) - .add(replaceButton)); + .add(replaceButton) + .add(addCommentButton)); } vertGroup.addContainerGap(); layout.setVerticalGroup(vertGroup); @@ -461,6 +477,7 @@ infoButton.setEnabled((buttonMask & INFO_BUTTON) != 0); cancelButton.setEnabled((buttonMask & CANCEL_BUTTON) != 0); helpButton.setEnabled((buttonMask & HELP_BUTTON) != 0); + addCommentButton.setEnabled((buttonMask & ADDCOMMET_BUTTON) != 0); } @@ -473,6 +490,7 @@ private JButton infoButton; private JButton replaceButton; private JButton skipButton; + private JButton addCommentButton; // End of variables declaration private EmptyPropertyPanel emptyPanel; diff --git a/i18n/src/org/netbeans/modules/i18n/I18nSupport.java b/i18n/src/org/netbeans/modules/i18n/I18nSupport.java --- a/i18n/src/org/netbeans/modules/i18n/I18nSupport.java +++ b/i18n/src/org/netbeans/modules/i18n/I18nSupport.java @@ -216,6 +216,9 @@ /** Replaces hard coded string using settigns encapsulated by I18nString typically customized by user. */ public void replace(HardCodedString hardString, I18nString i18nString); + + /** Add //NOI18N string to the end of the line*/ + public void replace(HardCodedString hardString, I18nString i18nString, String comment); } /** Factory inteface for creating {@code I18nSupport} instances. */ diff --git a/i18n/src/org/netbeans/modules/i18n/java/JavaI18nFinder.java b/i18n/src/org/netbeans/modules/i18n/java/JavaI18nFinder.java --- a/i18n/src/org/netbeans/modules/i18n/java/JavaI18nFinder.java +++ b/i18n/src/org/netbeans/modules/i18n/java/JavaI18nFinder.java @@ -405,6 +405,8 @@ if (endOfLine == -1) { endOfLine = restBuffer.length(); } + Position hardStringEndOfLine = document.createPosition(endOfLine); + lastJavaString.append(document.getText(currentStringStart + 1, hardString.length())); @@ -423,7 +425,8 @@ // Search was successful -> return. return new HardCodedString(extractString(hardString), hardStringStart, - hardStringEnd); + hardStringEnd, + hardStringEndOfLine); } } catch (BadLocationException ble) { ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, diff --git a/i18n/src/org/netbeans/modules/i18n/java/JavaI18nSupport.java b/i18n/src/org/netbeans/modules/i18n/java/JavaI18nSupport.java --- a/i18n/src/org/netbeans/modules/i18n/java/JavaI18nSupport.java +++ b/i18n/src/org/netbeans/modules/i18n/java/JavaI18nSupport.java @@ -675,6 +675,42 @@ * @param hcString found hard coded string to-be replaced * @param rbString holds replacing values */ public void replace(final HardCodedString hcString, + final I18nString i18nString, final String comment) { + if (!(i18nString instanceof JavaI18nString)) { + throw new IllegalArgumentException( + "I18N module: i18nString have to be an instance of JavaI18nString.");//NOI18N + } + + final String newCode = i18nString.getReplaceString(); + + final StyledDocument document = i18nString.getSupport().getDocument(); + + // Call runAtomic method to break guarded flag if it is necessary. (For non-guarded works as well). + NbDocument.runAtomic( + document, + new Runnable() { + public void run() { + try { + if (hcString.getEndOfLinePosition()!=null && hcString.getLength() > 0) { + document.insertString(hcString.getEndPosition().getOffset()+hcString.getEndOfLinePosition().getOffset(), + comment, null); + } + } catch (BadLocationException ble) { + NotifyDescriptor.Message message + = new NotifyDescriptor.Message( + NbBundle.getMessage(JavaI18nSupport.class, + "MSG_CouldNotReplace"),//NOI18N + NotifyDescriptor.ERROR_MESSAGE); + DialogDisplayer.getDefault().notify(message); + } + } + }); + } + + /** Replaces found hard coded string in source. + * @param hcString found hard coded string to-be replaced + * @param rbString holds replacing values */ + public void replace(final HardCodedString hcString, final I18nString i18nString) { if (!(i18nString instanceof JavaI18nString)) { throw new IllegalArgumentException( @@ -682,7 +718,7 @@ } final String newCode = i18nString.getReplaceString(); - + final StyledDocument document = i18nString.getSupport().getDocument(); // Call runAtomic method to break guarded flag if it is necessary. (For non-guarded works as well).