19 package org.sleuthkit.autopsy.centralrepository;
 
   21 import javax.swing.text.AbstractDocument;
 
   22 import org.openide.util.NbBundle.Messages;
 
   23 import org.openide.windows.WindowManager;
 
   24 import javax.swing.text.AttributeSet;
 
   25 import javax.swing.text.BadLocationException;
 
   26 import javax.swing.text.DocumentFilter;
 
   33 @Messages({
"CentralRepoCommentDialog.title.addEditCentralRepoComment=Add/Edit Central Repository Comment"})
 
   34 @SuppressWarnings(
"PMD.SingularField") 
 
   35 final class CentralRepoCommentDialog extends javax.swing.JDialog {
 
   37     private final CorrelationAttributeInstance correlationAttributeInstance;
 
   38     private boolean commentUpdated = 
false;
 
   39     private String currentComment = 
"";
 
   47     CentralRepoCommentDialog(CorrelationAttributeInstance correlationAttributeInstance) {
 
   48         super(WindowManager.getDefault().getMainWindow(), Bundle.CentralRepoCommentDialog_title_addEditCentralRepoComment());
 
   52         CorrelationAttributeInstance instance = correlationAttributeInstance;
 
   55         if (instance.getComment() != null) {
 
   56             currentComment = instance.getComment();
 
   62         commentTextArea.setText(instance.getComment());
 
   64         this.correlationAttributeInstance = correlationAttributeInstance;
 
   72         setSize(getPreferredSize());
 
   73         setLocationRelativeTo(this.getParent());
 
   74         setAlwaysOnTop(
false);
 
   84     boolean isCommentUpdated() {
 
   85         return commentUpdated;
 
   95         return currentComment;
 
  104         private final Integer MAX_CHARACTERS = 500;
 
  105         private Integer remainingCharacters = MAX_CHARACTERS;
 
  126         public void insertString(FilterBypass filter, 
int offset, String input,
 
  127                 AttributeSet attrSet) 
throws BadLocationException {
 
  129             this.replace(filter, offset, 0, input, attrSet);
 
  145         public void remove(FilterBypass filter, 
int offset, 
int length)
 
  146                 throws BadLocationException {
 
  147             super.remove(filter, offset, length);
 
  148             remainingCharacters += length;
 
  168         public void replace(FilterBypass filter, 
int offset, 
int length, String input,
 
  169                 AttributeSet attrSet) 
throws BadLocationException {
 
  171             String truncatedText = input;
 
  172             if ((filter.getDocument().getLength() + input.length() - length) > MAX_CHARACTERS) {
 
  173                 truncatedText = input.substring(0, MAX_CHARACTERS - 
 
  174                         filter.getDocument().getLength() - length);
 
  176             super.replace(filter, offset, length, truncatedText, attrSet);
 
  177             remainingCharacters -= truncatedText.length() - length;
 
  187             if (remainingCharacters == 0) {
 
  188                 remainingCharactersLabel.setText(String.format(
 
  189                         "<html><font color=\"red\">%d</font> %s</html>",
 
  190                     remainingCharacters, 
"characters remaining")); 
 
  192                 remainingCharactersLabel.setText(String.format(
 
  193                         "<html><font color=\"black\">%d</font> %s</html>",
 
  194                     remainingCharacters, 
"characters remaining")); 
 
  204     @SuppressWarnings(
"unchecked")
 
  206     private 
void initComponents() {
 
  208         jScrollPane1 = 
new javax.swing.JScrollPane();
 
  209         commentTextArea = 
new javax.swing.JTextArea();
 
  210         okButton = 
new javax.swing.JButton();
 
  211         cancelButton = 
new javax.swing.JButton();
 
  212         commentLabel = 
new javax.swing.JLabel();
 
  213         remainingCharactersLabel = 
new javax.swing.JLabel();
 
  215         setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
 
  216         setSize(getPreferredSize());
 
  218         commentTextArea.setColumns(20);
 
  219         commentTextArea.setLineWrap(
true);
 
  220         commentTextArea.setRows(5);
 
  221         commentTextArea.setTabSize(4);
 
  222         commentTextArea.setWrapStyleWord(
true);
 
  223         ((AbstractDocument)commentTextArea.getDocument())
 
  224                 .setDocumentFilter(
new CentralRepoCommentLengthFilter());
 
  225         jScrollPane1.setViewportView(commentTextArea);
 
  227         org.openide.awt.Mnemonics.setLocalizedText(okButton, 
org.openide.util.NbBundle.getMessage(CentralRepoCommentDialog.class, 
"CentralRepoCommentDialog.okButton.text")); 
 
  228         okButton.addActionListener(
new java.awt.event.ActionListener() {
 
  229             public void actionPerformed(java.awt.event.ActionEvent evt) {
 
  230                 okButtonActionPerformed(evt);
 
  234         org.openide.awt.Mnemonics.setLocalizedText(cancelButton, 
org.openide.util.NbBundle.getMessage(CentralRepoCommentDialog.class, 
"CentralRepoCommentDialog.cancelButton.text")); 
 
  235         cancelButton.addActionListener(
new java.awt.event.ActionListener() {
 
  236             public void actionPerformed(java.awt.event.ActionEvent evt) {
 
  237                 cancelButtonActionPerformed(evt);
 
  241         org.openide.awt.Mnemonics.setLocalizedText(commentLabel, 
org.openide.util.NbBundle.getMessage(CentralRepoCommentDialog.class, 
"CentralRepoCommentDialog.commentLabel.text")); 
 
  243         javax.swing.GroupLayout layout = 
new javax.swing.GroupLayout(getContentPane());
 
  244         getContentPane().setLayout(layout);
 
  245         layout.setHorizontalGroup(
 
  246             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 
  247             .addGroup(layout.createSequentialGroup()
 
  249                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 
  250                     .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
 
  251                     .addGroup(layout.createSequentialGroup()
 
  252                         .addComponent(commentLabel)
 
  253                         .addGap(0, 451, Short.MAX_VALUE))
 
  254                     .addGroup(layout.createSequentialGroup()
 
  255                         .addComponent(remainingCharactersLabel)
 
  256                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
 
  257                         .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
 
  258                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 
  259                         .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)))
 
  262         layout.setVerticalGroup(
 
  263             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 
  264             .addGroup(layout.createSequentialGroup()
 
  266                 .addComponent(commentLabel)
 
  267                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 
  268                 .addComponent(jScrollPane1)
 
  269                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
 
  270                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 
  271                     .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
 
  272                         .addComponent(cancelButton)
 
  273                         .addComponent(okButton))
 
  274                     .addComponent(remainingCharactersLabel))
 
  281     private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {
 
  285     private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {
 
  286         currentComment = commentTextArea.getText();
 
  287         correlationAttributeInstance.setComment(currentComment);
 
  288         commentUpdated = 
true;
 
  294     private javax.swing.JButton cancelButton;
 
  295     private javax.swing.JLabel commentLabel;
 
  296     private javax.swing.JTextArea commentTextArea;
 
  297     private javax.swing.JLabel remainingCharactersLabel;
 
  298     private javax.swing.JScrollPane jScrollPane1;
 
  299     private javax.swing.JButton okButton;