1   package gate.gui.jape;
2   
3   import java.io.*;
4   import javax.swing.*;
5   import java.net.*;
6   import java.awt.*;
7   
8   import gate.*;
9   import gate.creole.*;
10  
11  /**
12   * <p>Title: Gate2</p>
13   * <p>Description: </p>
14   * <p>Copyright: Copyright (c) 2000</p>
15   * <p>Company: University Of Sheffield</p>
16   * @author not attributable
17   * @version 1.0
18   */
19  
20  
21  public class JapeViewer extends AbstractVisualResource implements ANNIEConstants {
22  
23    public JapeViewer() {
24    }
25  
26    //GUI components
27    /** The text display.*/
28    protected JTextArea textArea;
29  
30    /** Scroller used for the text diaplay*/
31    protected JScrollPane textScroll;
32  
33    /** The toolbar displayed on the top part of the component */
34    protected JToolBar toolbar;
35  
36    /**Should this component bahave as an editor as well as an viewer*/
37    private boolean editable = false;
38  
39    /** A Button for saving the contents in a Jape file */
40    private JButton saveButton;
41  
42    /** A Button for reverting contents */
43    private JButton revertButton;
44  
45    /** A field that holds the jape file name */
46    private URL japeFileURL;
47  
48    /** a field that holds the jape file contents */
49    private String japeFileContents;
50  
51    /** Transducer */
52    private Transducer transducer;
53  
54    /** An Init method */
55    public Resource init() {
56      initGuiComponents();
57      japeFileContents = new String();
58      return this;
59    }
60  
61    private void initGuiComponents() {
62      setLayout(new BorderLayout());
63      textArea = new JTextArea();
64      textArea.setEditable(editable);
65      textScroll = new JScrollPane(textArea,
66                                   JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
67                                   JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
68      add(textScroll, BorderLayout.CENTER);
69    }
70  
71    public void setTarget(Object target) {
72      if(!(target instanceof  Transducer)) {
73        throw new IllegalArgumentException(
74          "The GATE jape editor can only be used with a GATE jape transducer!\n" +
75          target.getClass().toString() + " is not a GATE Jape Transducer!");
76      }
77  
78      this.transducer = (Transducer) target;
79      //Transducer inst = (Transducer)((Gate.getCreoleRegister().getPrInstances("gate.creole.Transducer")).get(0));
80      japeFileURL = transducer.getGrammarURL();
81      // reading japeFile
82      try {
83        BufferedReader br = new BufferedReader(new InputStreamReader(japeFileURL.
84            openStream()));
85        String content = br.readLine();
86        while(content != null) {
87          japeFileContents += content + "\n";
88          content = br.readLine();
89        }
90        textArea.setText(japeFileContents);
91        br.close();
92      } catch (IOException ioe) {
93      }
94    }
95  }