import java.io.*; import java.net.*; import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class Annotator_GUI extends JPanel{ /** Files Path in the cyberworld*/ static String filesPath = "http://www-ciir.cs.umass.edu/~giridhar/dir/files/"; /** Button to save comments*/ byte vistsWriteJudgements = 0; /** Button to save comments*/ JButton btnSave; /** List for file selection */ JList files, terms; /** display area for each of the files */ JEditorPane file1, file2, upperPane; JTextArea textar; /** panel for user inputs */ JPanel inputs; /** Map a string to File[2] */ Map listEntryToFiles; /** vectors of file list */ Vector ascending, descending; /** Previously selected value */ String lastSelection; /** stores current user's name*/ static String userName; /** holds the terms common to both stories*/ Vector termsV = new Vector(); /* public void getConnected(){ try{ } catch (IOException e){ System.out.println("could not write out comment file"); } }*/ /** last term index, stops resection */ int lastTermIndex=1000, lastFileIndex=1000; public void writeOutJudgements(){ String preComment = ""; try{ StringTokenizer st = new StringTokenizer(files.getSelectedValue().toString()); st.nextToken(); st.nextToken(); st.nextToken(); st.nextToken(); String fileOne = st.nextToken(); String fileTwo = st.nextToken(); BufferedWriter out = new BufferedWriter(new FileWriter("comments/"+fileOne +"_"+fileTwo +".Comments."+userName)); out.write(textar.getText()); out.close(); } catch (IOException e){ System.out.println("could not write out comment file"); } } public void loadFiles(){ String currentName = (String)files.getSelectedValue(); //System.out.println("Here: "+currentName); if (currentName != null){ File [] filesToOpen = (File[])listEntryToFiles.get(currentName); for (int x = 0; x < filesToOpen.length; x++){ readTextFile(filesToOpen[x],x); } } } public void readInJudgements(){ try { String preComment = ""; StringTokenizer st = new StringTokenizer(files.getSelectedValue().toString()); st.nextToken(); st.nextToken(); st.nextToken(); st.nextToken(); String fileOne = st.nextToken(); String fileTwo = st.nextToken(); BufferedReader in = new BufferedReader(new FileReader("comments/"+fileOne +"_"+fileTwo +".Comments."+userName)); String line; while ((line = in.readLine())!= null){ line = line.trim(); preComment += line; } textar.setText(preComment); } catch (IOException e){ try{ String preComment = ""; StringTokenizer st = new StringTokenizer(files.getSelectedValue().toString()); st.nextToken(); st.nextToken(); st.nextToken(); st.nextToken(); String fileOne = st.nextToken(); String fileTwo = st.nextToken(); URL hp= new URL(filesPath+"comments/"+fileOne +"_"+fileTwo +".Comments"); URLConnection hpcon = hp.openConnection(); BufferedReader in = new BufferedReader (new InputStreamReader (hpcon.getInputStream())); String line; while ((line = in.readLine())!= null){ line = line.trim(); preComment += line; } textar.setText(preComment); } catch(IOException e1){ } } } public void readLocalTextFile(File text, int frame){ JEditorPane jep = null; if (frame == 0){ jep = file1; } else { jep = file2; } StringBuffer sb = new StringBuffer(); // read in the entire file try { BufferedReader in = new BufferedReader(new FileReader("files/"+text.getName())); String line =""; while ((line = in.readLine())!= null){ sb.append(line + "\n"); } } catch (IOException e2){ e2.printStackTrace(); } // set the text jep.setText(sb.toString()); jep.setCaretPosition(0); } /** frame should be 0 or 1 */ public void readTextFile(File text, int frame){ JEditorPane jep = null; if (frame == 0){ jep = upperPane; } else if (frame == 1){ jep = file1; } else { jep = file2; } StringBuffer sb = new StringBuffer(); // read in the entire file if(frame == 0){ try { URL hp= new URL(filesPath+"topics/"+text.getName()); URLConnection hpcon = hp.openConnection(); BufferedReader in = new BufferedReader (new InputStreamReader (hpcon.getInputStream())); String line =""; while ((line = in.readLine())!= null){ sb.append(line + "\n"); } } catch (IOException e2){ //e2.printStackTrace(); } } else{ try { URL hp= new URL(filesPath+text.getName()); URLConnection hpcon = hp.openConnection(); BufferedReader in = new BufferedReader (new InputStreamReader (hpcon.getInputStream())); String line =""; while ((line = in.readLine())!= null){ sb.append(line + "\n"); } } catch (IOException e2){ e2.printStackTrace(); } } // set the text jep.setText(sb.toString()); jep.setCaretPosition(0); } public void readTermsFile(){ File[] names = (File[]) listEntryToFiles.get(lastSelection); try { URL hp= new URL(filesPath+"terms/"+names[1].toString()+"_"+names[2].getName()+".terms"); URLConnection hpcon = hp.openConnection(); BufferedReader in = new BufferedReader (new InputStreamReader (hpcon.getInputStream())); String line =""; //read in file names termsV.removeAllElements(); while ((line = in.readLine())!= null){ line = line.trim(); termsV.add(line); } } catch (IOException e3){ System.err.println("Could not read terms data for given selection" + e3); } terms.setListData(termsV); } public Vector readFileList(String fileName, String dataFileDirectory) throws IOException { Vector names = new Vector(); //System.out.println("Now here: "+dataFileDirectory+fileName); URL hp= new URL(dataFileDirectory+fileName); URLConnection hpcon = hp.openConnection(); BufferedReader in = new BufferedReader (new InputStreamReader (hpcon.getInputStream())); String line; while ((line = in.readLine())!= null){ if ((line.indexOf('<')==-1)&&(line.indexOf('>')==-1)) { line = line.trim(); names.add(line); StringTokenizer st = new StringTokenizer(line); st.nextToken(); st.nextToken(); File ftopic = new File(st.nextToken()); st.nextToken(); File f1 = new File(st.nextToken()); File f2 = new File(st.nextToken()); listEntryToFiles.put(line, new File[]{ftopic,f1,f2}); } } return names; } public Annotator_GUI(String fileName, String desFileName, String dataFileDirectory) throws IOException{ super(); setLayout(new BorderLayout()); setSize(new Dimension(400,600)); listEntryToFiles = new HashMap(); ascending = readFileList(fileName, dataFileDirectory); descending = readFileList(desFileName, dataFileDirectory); // put together gui files = new JList(ascending); files.setPrototypeCellValue(" "); terms = new JList(); terms.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); terms.addListSelectionListener(new ListSelectionListener(){ public void valueChanged(ListSelectionEvent evt){ Vector bgcolors = new Vector(); bgcolors.addElement(new String("yellow")); bgcolors.addElement(new String("aqua")); bgcolors.addElement(new String("silver")); bgcolors.addElement(new String("#FFCCCC")); bgcolors.addElement(new String("#CCCC00")); bgcolors.addElement(new String("#CCFF99")); bgcolors.addElement(new String("#CCCC99")); bgcolors.addElement(new String("#99CCCC")); bgcolors.addElement(new String("#FF9933")); bgcolors.addElement(new String("#FF99FF")); bgcolors.addElement(new String("#00FFFF")); Object [] indices = terms.getSelectedValues(); int minIndex = terms.getMinSelectionIndex(); String fileOne,fileTwo; int index; StringTokenizer st = new StringTokenizer(lastSelection); st.nextToken(); st.nextToken(); st.nextToken(); st.nextToken(); fileOne = st.nextToken(); fileTwo = st.nextToken(); try{ BufferedWriter out = new BufferedWriter(new FileWriter("files/"+fileOne+".highlighted")); URL hp= new URL(filesPath+fileOne); URLConnection hpcon = hp.openConnection(); BufferedReader in = new BufferedReader (new InputStreamReader (hpcon.getInputStream())); String line,line1,st1; StringBuffer sb; int pos = 0; while ((line1 = in.readLine())!= null){ line1.trim(); line = line1.toLowerCase(); sb = new StringBuffer(line1); for (index =0;index"+currentSelection.trim()+""; sb.replace(pos,pos+currentSelection.length(),st1); pos += st1.length(); line1 = new String(sb); line = line1.toLowerCase(); } } out.write( new String(sb)+"\n"); } out.close(); } catch (Exception e){ System.err.println("Could not open "+fileOne+" for modifying" + e); } try{ BufferedWriter out = new BufferedWriter(new FileWriter("files/"+fileTwo+".highlighted")); URL hp= new URL(filesPath+fileTwo); URLConnection hpcon = hp.openConnection(); BufferedReader in = new BufferedReader (new InputStreamReader (hpcon.getInputStream())); String line,line1,st1; StringBuffer sb; int pos = 0; while ((line1 = in.readLine())!= null){ line1.trim(); line = line1.toLowerCase(); sb = new StringBuffer(line1); for (index =0;index"+currentSelection+""; sb.replace(pos,pos+currentSelection.length(),st1); pos += st1.length(); line1 = new String(sb); line = line1.toLowerCase(); } } out.write( new String(sb)+"\n"); } out.close(); } catch (Exception e){ System.err.println("Could not open "+fileTwo+" for modifying" + e); } File f11 = new File("files/"+fileOne+".highlighted"); File f21 = new File("files/"+fileTwo+".highlighted"); readLocalTextFile(f11,0); readLocalTextFile(f21,1); } }); DefaultListSelectionModel mod = new DefaultListSelectionModel(); mod.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION); files.setSelectionModel(mod); // take care of changing the files once the selection changes files.addListSelectionListener(new ListSelectionListener(){ public void valueChanged(ListSelectionEvent evt){ // this should save any judgements made if (files.getSelectedIndex() == lastFileIndex){ return; } lastFileIndex = files.getSelectedIndex(); readInJudgements(); loadFiles(); lastSelection = files.getSelectedValue().toString(); readTermsFile(); writeOutJudgements(); } }); // put the file list to the left JPanel north = new JPanel(new BorderLayout()); JScrollPane temp = new JScrollPane(files); north.add(temp, BorderLayout.WEST); upperPane = new JEditorPane("text/html",""); temp = new JScrollPane(upperPane, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); north.add(temp, BorderLayout.CENTER); add(north, BorderLayout.NORTH); // init the text areas file1 = new JEditorPane("text/html", ""); /* file1.setColumns(35); file1.setRows(35); */ file1.setEditable(false); /* file1.setLineWrap(true); file1.setWrapStyleWord(true); */ file2 = new JEditorPane("text/html",""); /* file2.setColumns(35); file2.setRows(35); file2.setLineWrap(true); file2.setWrapStyleWord(true); */ file2.setEditable(false); // center is to put the text areas and user inputs into JPanel center = new JPanel(); center.setLayout(new BorderLayout()); add(center, BorderLayout.CENTER); // inside center will contain the two text displays JPanel insideCenter = new JPanel(); center.add(insideCenter, BorderLayout.CENTER); //input will contain annotators dialogs inputs = new JPanel(); center.add(inputs, BorderLayout.SOUTH); // should set up the text areas so they are equal in size insideCenter.setLayout(new GridLayout(1,3)); temp = new JScrollPane(file1, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); insideCenter.add(temp); terms.setSize(new Dimension(50,50)); JPanel termsPanel = new JPanel(new BorderLayout()); temp = new JScrollPane(terms); termsPanel.add(new JScrollPane(terms), BorderLayout.CENTER); JButton clearSelection = new JButton("Clear Selections"); clearSelection.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ terms.clearSelection(); loadFiles(); } }); termsPanel.add(clearSelection, BorderLayout.NORTH); center.add(termsPanel, BorderLayout.WEST); terms.setPrototypeCellValue(" "); temp = new JScrollPane(file2, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); insideCenter.add(temp); JLabel label = new JLabel("Comments:"); btnSave = new JButton("Save Comments"); textar = new JTextArea(5,70); label.setLabelFor(textar); textar.setLineWrap(true); textar.setWrapStyleWord(true); inputs.add(label); inputs.add(new JScrollPane(textar,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER)); inputs.add(btnSave); btnSave.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent a) { writeOutJudgements(); } }); //buttons JPanel north_north = new JPanel(); north.add(north_north, BorderLayout.NORTH); JButton asc = new JButton("Ascending"), des = new JButton("Descending"); north_north.add(asc); north_north.add(des); asc.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ writeOutJudgements(); files.setListData(ascending); } }); des.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ writeOutJudgements(); files.setListData(descending); } }); } public static void main(String args[]) throws Exception{ UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); if (args.length!=1) { System.err.println("usage:java Annotator_GUI "); return; }; System.out.print("Please enter the password: "); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String pw = in.readLine(); if (!"ragtag".equals(pw)){ System.out.println("incorrect password"); System.exit(1); } userName = args[0]; Runtime r = Runtime.getRuntime(); Process p2=null; try{ p2 = r.exec("mkdir comments"); }catch(Exception e9){ } Runtime s = Runtime.getRuntime(); Process p3 = null; try{ p3 = s.exec("mkdir files"); }catch(Exception e9){ } final Annotator_GUI ag = new Annotator_GUI("ascending.lst","descending.lst",filesPath); final String[] args1 = args; JFrame f = new JFrame("CIIR Annotation Tool for New Event Detection. Currently being used by "+args[0]); final JPanel p = new JPanel(); p.setLayout(new BorderLayout()); JPanel north = new JPanel(); f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ ag.writeOutJudgements(); } }); f.setContentPane(ag); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.pack(); f.show(); } }