ModuleCommentPrompt.java

00001 /*
00002  * ModuleCommentPrompt.java
00003  *
00004  */
00005 
00006 package DisplayGUI;
00007 import java.util.Vector;
00008 import java.util.regex.Pattern;
00009 import java.util.regex.Matcher;
00010 import java.io.*;
00011 import Preferences.*;
00012 import GuiComponents.System.*;
00013 import javax.swing.JMenu;
00014 import javax.swing.JMenuItem;
00015 import java.util.HashMap;
00016 import java.util.Map;
00017 
00022 public class ModuleCommentPrompt extends javax.swing.JFrame implements displayParams {
00023     private guiTableAdapter commentTable;
00024     private javax.swing.JTable table;
00025     private javax.swing.ButtonGroup buttonGroup1;
00026     private javax.swing.JButton cancelButton;
00027     private javax.swing.JLabel jLabel1,jLabel2;
00028     private javax.swing.JScrollPane jScrollPane1;
00029     private javax.swing.JButton okButton;
00030     
00031     private javax.swing.JTextArea comment;
00032     private javax.swing.JTextField username;
00033 
00034     private JMenu optionsMenu;
00035 
00036     private String defaultString="Enter your comment(s) here.";
00037 
00038     String serialNo=null;
00039     
00040     boolean resetFields=true;
00041 
00042     
00043     int updateIndex=-1;
00044     int deleteIndex=-1;
00045     int rowSelect=-1;
00046     
00048     public ModuleCommentPrompt(String serialNo) {
00049         this.serialNo=serialNo;
00050 
00051         createMenus();
00052         initComponents();
00053 
00054         fillTable();
00055 
00056     }
00057     
00058 
00059     public int getNoRuns() {
00060          return commentTable.recordCount;
00061          }
00062 
00063     
00064 
00065     private void initComponents() {
00066 
00067         getContentPane().setBackground(backgroundColor);
00068 
00069         commentTable = new guiTableAdapter();
00070         table = new javax.swing.JTable(commentTable);
00071         jScrollPane1 = new javax.swing.JScrollPane(table);
00072         table.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
00073 
00074         java.awt.GridBagConstraints gridBagConstraints;
00075 
00076 //        buttonGroup1 = new javax.swing.ButtonGroup();
00077         jLabel1 = new javax.swing.JLabel();
00078         jLabel2 = new javax.swing.JLabel();
00079         jScrollPane1 = new javax.swing.JScrollPane(table);
00080         okButton = new javax.swing.JButton();
00081         cancelButton = new javax.swing.JButton();
00082 
00083         getContentPane().setLayout(new java.awt.GridBagLayout());
00084 
00085         addWindowListener(new java.awt.event.WindowAdapter() {
00086             public void windowClosing(java.awt.event.WindowEvent evt) {
00087                 closeDialog(evt);
00088             }
00089         });
00090 
00091         jLabel1.setText("Add Comments for "+serialNo);
00092         jLabel1.setFont(displayTitleFont);
00093         jLabel1.setForeground(darkBlueColor);
00094         gridBagConstraints = new java.awt.GridBagConstraints();
00095         gridBagConstraints.gridx = 0;
00096         gridBagConstraints.gridy = 0;
00097         gridBagConstraints.gridwidth = 2;
00098         gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
00099         gridBagConstraints.weighty = 0.1;
00100         getContentPane().add(jLabel1, gridBagConstraints);
00101 
00102         jLabel2.setText("Existing comments:");
00103         jLabel2.setFont(displayScaleFont);
00104         gridBagConstraints = new java.awt.GridBagConstraints();
00105         gridBagConstraints.gridx = 0;
00106         gridBagConstraints.gridy = 1;
00107         gridBagConstraints.gridwidth = 2;
00108         gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
00109         gridBagConstraints.weighty = 0.1;
00110         gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHWEST;
00111         getContentPane().add(jLabel2, gridBagConstraints);
00112 
00113         jScrollPane1.setBackground(new java.awt.Color(255, 255, 255));
00114         table.setBackground(new java.awt.Color(255, 255, 255));
00115         table.setFont(displayScaleFont);
00116         jScrollPane1.setBorder(new javax.swing.border.BevelBorder(javax.swing.border.BevelBorder.LOWERED));
00117         jScrollPane1.setMaximumSize(new java.awt.Dimension(600, 300));
00118         jScrollPane1.setMinimumSize(new java.awt.Dimension(50, 50));
00119         jScrollPane1.setPreferredSize(new java.awt.Dimension(200, 100));
00120         gridBagConstraints = new java.awt.GridBagConstraints();
00121         gridBagConstraints.gridx = 0;
00122         gridBagConstraints.gridy = 2;
00123         gridBagConstraints.gridwidth = 2;
00124         gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 5);
00125         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
00126         gridBagConstraints.weighty = 0.3;
00127         getContentPane().add(jScrollPane1, gridBagConstraints);
00128         
00129         table.addMouseListener(new tableMouseAdapter());  // popup menu on second table only
00130 
00131         
00132         javax.swing.JLabel nll = new javax.swing.JLabel("Your name:");
00133         nll.setFont(displayScaleFont);
00134         gridBagConstraints = new java.awt.GridBagConstraints();
00135         gridBagConstraints.gridx = 0;
00136         gridBagConstraints.gridy = 3;
00137                gridBagConstraints.weightx = 0.5;
00138         gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
00139         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
00140         getContentPane().add(nll, gridBagConstraints);
00141 
00142         username = new javax.swing.JTextField(10);
00143         username.setFont(displayScaleFont);
00144         gridBagConstraints = new java.awt.GridBagConstraints();
00145         gridBagConstraints.gridx = 1;
00146         gridBagConstraints.gridy = 3;
00147         gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
00148         gridBagConstraints.weighty = 0.1;
00149                gridBagConstraints.weightx = 0.5;
00150         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
00151         getContentPane().add(username, gridBagConstraints);
00152         
00153         comment = new javax.swing.JTextArea();
00154         comment.setFont(displayScaleFont);
00155         comment.setLineWrap(true);
00156         comment.setWrapStyleWord(true);
00157         javax.swing.JScrollPane areaScrollPane = new javax.swing.JScrollPane(comment);
00158         areaScrollPane.setVerticalScrollBarPolicy(
00159                         javax.swing.JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
00160         areaScrollPane.setPreferredSize(new java.awt.Dimension(600, 100));
00161         gridBagConstraints = new java.awt.GridBagConstraints();
00162         gridBagConstraints.gridx = 0;
00163         gridBagConstraints.gridy = 4;
00164         gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
00165         gridBagConstraints.gridwidth = 2;
00166         gridBagConstraints.weighty = 0.3;
00167         gridBagConstraints.anchor = java.awt.GridBagConstraints.CENTER;
00168         getContentPane().add(areaScrollPane, gridBagConstraints);
00169         comment.setText(defaultString);
00170         comment.selectAll();
00171         comment.setCaretPosition(0);
00172 
00173 
00174         okButton.setText("Add Comment");
00175         okButton.addActionListener(new java.awt.event.ActionListener() {
00176             public void actionPerformed(java.awt.event.ActionEvent evt) {
00177                 okButtonActionPerformed(evt);
00178             }
00179         });
00180         gridBagConstraints = new java.awt.GridBagConstraints();
00181         gridBagConstraints.gridx = 0;
00182         gridBagConstraints.gridy = 5;
00183         gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
00184         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
00185         getContentPane().add(okButton, gridBagConstraints);
00186         okButton.requestFocusInWindow();
00187 
00188         cancelButton.setText("Close");
00189         cancelButton.addActionListener(new java.awt.event.ActionListener() {
00190             public void actionPerformed(java.awt.event.ActionEvent evt) {
00191                 cancelButtonActionPerformed(evt);
00192             }
00193         });
00194         gridBagConstraints = new java.awt.GridBagConstraints();
00195         gridBagConstraints.gridx = 1;
00196         gridBagConstraints.gridy = 5;
00197         gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
00198         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
00199         gridBagConstraints.weighty = 0.1;
00200         getContentPane().add(cancelButton, gridBagConstraints);
00201 
00202 
00203         pack();
00204         java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
00205         java.awt.Dimension WSize = getSize();
00206         setLocation((screenSize.width-(int)WSize.getWidth())/2,(screenSize.height-(int)WSize.getHeight())/2);
00207         
00208         show();
00209     }
00210 
00211 
00212     private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bOKActionPerformed
00213         updateComments();
00214         fillTable();
00215         if(resetFields) {
00216            comment.setText(defaultString);
00217            comment.selectAll();
00218            comment.setCaretPosition(0);
00219            }
00220         resetFields=true;
00221     }//GEN-LAST:event_bOKActionPerformed
00222 
00223     private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bOKActionPerformed
00224         closeDialog(null);
00225     }//GEN-LAST:event_bOKActionPerformed
00226     
00228     private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
00229         setVisible(false);
00230         dispose();
00231     }//GEN-LAST:event_closeDialog
00232 
00233  public void update(String serialNo) {
00234     this.serialNo=serialNo;
00235     jLabel1.setText("Add Comments for "+serialNo);
00236     fillTable();
00237     setVisible(true);
00238     }
00239 
00240   private void fillTable() {
00241        int cCount=0;
00242           Vector runVector = new Vector();
00243           Vector theLine = new Vector();
00244           theLine.addElement("Date");
00245           theLine.addElement("User");
00246           theLine.addElement("Comment(s)");
00247           runVector.addElement(theLine);
00248           theLine=null;
00249 
00250        try {
00251 
00252           File commentFile = new File(PreferencesInterface.getInstance().getPreference(PreferencesInterface.WEB_LOOKUP_DIR)+"/ModuleComments",serialNo+".txt");
00253           Pattern iPattern = Pattern.compile("^INDEX=(\\d+);USER=(.*);DATE=(.*)$");
00254 
00255           if(commentFile.exists()) {
00256               BufferedReader in = new BufferedReader(new FileReader(commentFile));
00257               String line;
00258               StringBuffer commentLine=null;
00259               while((line=in.readLine())!=null) {
00260                 Matcher matcher = iPattern.matcher(line);
00261                 if(matcher.matches()) {
00262                    if(commentLine!=null) {
00263                        theLine.addElement(commentLine.toString());
00264                        runVector.addElement(theLine);
00265                        }
00266                    commentLine = new StringBuffer();
00267                    cCount++;
00268                    theLine=new Vector();
00269                    theLine.addElement(line.substring(matcher.start(3),matcher.end(3)));
00270                    theLine.addElement(line.substring(matcher.start(2),matcher.end(2)));
00271                    }
00272                 else {
00273                    if(commentLine!=null) {
00274                       if(commentLine.length()!=0) commentLine.append("<br>");
00275                       commentLine.append(line);
00276                       }
00277                 }
00278               }
00279 
00280               if(commentLine!=null && commentLine.length()>0) {
00281                  theLine.addElement(commentLine.toString());
00282                  runVector.addElement(theLine);
00283                  }
00284 
00285               in.close();
00286             }
00287         }catch(Exception ioE){System.err.println("Exception building table: "+ioE.toString());}
00288 
00289         commentTable.reTable(runVector);
00290         table.getColumnModel().getColumn(0).setPreferredWidth(30);
00291         table.getColumnModel().getColumn(1).setPreferredWidth(80);
00292         if(cCount==0) jLabel2.setText("There are no comments uploaded for this module yet:");
00293         else jLabel2.setText("There are "+Integer.toString(cCount)+" previously uploaded comments. Right mouse-click on a row for edit options:");
00294 
00295 
00296 
00297    }
00298    
00299    private void updateComments() {
00300        if(deleteIndex<0) {
00301           if(!username.getText().matches(".*[a-zA-Z0-9]+.*[\\s]*.*")) {
00302              javax.swing.JOptionPane.showMessageDialog(null,"Unvalid username. Please try again.");
00303              resetFields=false;
00304              return;
00305              }
00306           if(!comment.getText().matches(".*[a-zA-Z0-9]+.*[\\s]*.*") || comment.getText().equals(defaultString)) {
00307              javax.swing.JOptionPane.showMessageDialog(null,"Unvalid comment. Please try again.");
00308              resetFields=false;
00309              return;
00310              }
00311            }
00312        try {
00313           File topDir = new File(PreferencesInterface.getInstance().getPreference(PreferencesInterface.WEB_LOOKUP_DIR)+"/ModuleComments");
00314           if(!topDir.exists()) {
00315             javax.swing.JOptionPane.showMessageDialog(null,"Missing directory. Check that you have defined your 'book-keeping directory\nin your Preferences, and check it has a subdirectory named 'ModuleComments'");
00316             return;
00317             }
00318           File commentFile = new File(topDir,serialNo+".txt");
00319           File oFile = new File(topDir,"sctGUI.txt");
00320           Pattern iPattern = Pattern.compile("^INDEX=(\\d+);USER=(.*);DATE=(.*)$");
00321 
00322           int count=0;
00323               String theDate = guiUtilities.DaveUtils.getTodaysDate(false);
00324               BufferedReader in = (commentFile.exists()) ? new BufferedReader(new FileReader(commentFile)) : null;
00325               String line;
00326               StringBuffer commentLine=null;
00327               Map tempHash = new HashMap();
00328               Map commentHash = new HashMap();
00329               if(in!=null) {
00330                  while((line=in.readLine())!=null) {
00331                     System.out.println("line="+line);
00332                     Matcher matcher = iPattern.matcher(line);
00333                     if(matcher.matches()) {
00334                        if(tempHash.size()>0) {
00335                           tempHash.put("COMMENT",commentLine.toString());
00336                           System.out.println("adding key "+count);
00337                           commentHash.put(new Double(count),tempHash);
00338                           tempHash = new HashMap();
00339                           count++;
00340                           }
00341                        commentLine = new StringBuffer();
00342                        tempHash.put("USER",line.substring(matcher.start(2),matcher.end(2)));
00343                        tempHash.put("DATE",line.substring(matcher.start(3),matcher.end(3)));
00344                        }
00345                     else {
00346                        if(commentLine.length()!=0) commentLine.append("\n");
00347                        commentLine.append(line);
00348                        }
00349                     }
00350                  if(commentLine!=null) {
00351                     tempHash.put("COMMENT",commentLine.toString());
00352                     System.out.println("adding key "+count);
00353                     commentHash.put(new Double(count),tempHash);
00354                     }
00355                  in.close();
00356               }
00357         //      System.out.println("now writing");
00358               BufferedWriter out = new BufferedWriter(new FileWriter(oFile));
00359 
00360               int outCount=0;
00361               for(int i=0;i<commentHash.size();i++) {
00362                 Double key = new Double(i);
00363                 if(!commentHash.containsKey(key)) {
00364                    System.err.println("missing key "+key.toString());
00365                    continue;
00366                    }
00367                 if(i==deleteIndex) continue;
00368                 outCount++;
00369                 tempHash = (HashMap) commentHash.get(key);
00370                 String user = (String)tempHash.get("USER");
00371                 String date = (String)tempHash.get("DATE");
00372                 String cmt = (String)tempHash.get("COMMENT");
00373                 if(updateIndex==i) out.write("INDEX="+Integer.toString(i)+";USER="+username.getText()+";DATE="+guiUtilities.DaveUtils.getTodaysDate(false));
00374                 else out.write("INDEX="+Integer.toString(i)+";USER="+user+";DATE="+date);
00375                 out.newLine();
00376                 if(updateIndex==i) out.write(comment.getText());
00377                 else out.write(cmt);
00378                 out.newLine();
00379                 }
00380               if(updateIndex<0 && deleteIndex<0) {
00381                 outCount++;
00382                 out.write("INDEX="+Integer.toString(count)+";USER="+username.getText()+";DATE="+guiUtilities.DaveUtils.getTodaysDate(false));
00383                 out.newLine();
00384                 out.write(comment.getText());
00385                 out.newLine();
00386                 }
00387               out.close();
00388               
00389               if(outCount==0) commentFile.delete();
00390               else {
00391 
00392                 boolean deleted = (commentFile.exists()) ? commentFile.delete() : true;
00393                 if(deleted) {
00394                     oFile.renameTo(commentFile);
00395                     if(commentFile.exists()) oFile.delete();
00396                     else javax.swing.JOptionPane.showMessageDialog(null,"Comment file lost!! Hopefully there is a backup at "+oFile.getAbsolutePath());
00397                     }
00398                 }
00399         }catch(Exception e) {
00400            javax.swing.JOptionPane.showMessageDialog(null,"Exception saving comments: "+e.toString());
00401            }
00402         updateIndex=-1;
00403         deleteIndex=-1;
00404         okButton.setText("Add Comment");
00405 }
00406 
00407 
00408 
00409     class tableMouseAdapter extends java.awt.event.MouseAdapter {
00410             public void mouseClicked(java.awt.event.MouseEvent e) {
00411                switch(e.getModifiers()) {
00412                  case java.awt.event.InputEvent.BUTTON2_MASK:
00413                  case java.awt.event.InputEvent.BUTTON3_MASK:
00414                        rowSelect = table.rowAtPoint(new java.awt.Point(e.getX(),e.getY()));
00415                        table.getSelectionModel().setSelectionInterval(rowSelect,rowSelect);
00416                        javax.swing.JPopupMenu pMenu;
00417                        pMenu = optionsMenu.getPopupMenu();
00418                        pMenu.show(e.getComponent(),e.getX(),e.getY());
00419                        pMenu.setInvoker(optionsMenu);
00420                  default:
00421                  }
00422             }
00423     }
00424 
00425 
00426 public void createMenus() {
00427     optionsMenu = new JMenu("Options");
00428     JMenuItem item = new JMenuItem("Edit...");
00429     item.setFont(displayScaleFont);
00430     item.addActionListener(new java.awt.event.ActionListener() {
00431                 public void actionPerformed(java.awt.event.ActionEvent evt) {
00432                   username.setText((String)table.getValueAt(rowSelect,1));
00433                   String theComment = (String)table.getValueAt(rowSelect,2);
00434                   theComment =  theComment.replaceAll("<br>","\n");
00435                   comment.setText(theComment);
00436                   updateIndex=rowSelect;
00437                   okButton.setText("Change comment");
00438                      }
00439                 });
00440     optionsMenu.add(item);
00441     item = new JMenuItem("Delete");
00442     item.setFont(displayScaleFont);
00443     item.addActionListener(new java.awt.event.ActionListener() {
00444                 public void actionPerformed(java.awt.event.ActionEvent evt) {
00445                   System.out.println("delete");
00446       //            int response = javax.swing.JOptionPane.showConfirmDialog(null,"Are you sure you want to delete the comment?",javax.swing.JOptionPane.YES_NO_OPTION,javax.swing.JOptionPane.QUESTION_MESSAGE);
00447       //            if(response==javax.swing.JOptionPane.NO_OPTION) return;
00448                   deleteIndex=rowSelect;
00449                   updateComments();
00450                   fillTable();
00451                      }
00452                 });
00453     optionsMenu.add(item);
00454     }
00455 
00456 
00457 
00458 
00459 
00460     
00461     
00462 }

Generated on Mon Feb 6 14:12:14 2006 for SCT DAQ/DCS Software - Java by  doxygen 1.4.6