gt:         Style deflt = StyleContext.getDefaultStyleContext().getStyle(
context: package marttinterface;
  
  import java.awt.*;
  import java.awt.event.*;
  import java.io.*;
  import java.util.*;
  import javax.swing.*;
  import javax.swing.text.*;
  import javax.swing.event.*;
  import javax.swing.tree.*;
  
  // For creating borders
  import javax.swing.border.EmptyBorder;
  import javax.swing.border.BevelBorder;
  import javax.swing.border.CompoundBorder;
  // regular exp
  import java.util.regex.*;
  
  import org.xml.sax.*;
  import org.w3c.dom.*;
  
  /**
   * <p>Title: User Interface of MARTT </p>
   * <p>Description: Support training example annotation and marked-up example review.</p>
   * <p>Copyright: Copyright (c) 2005</p>
   * <p>Company: </p>
   * @author hong cui
   * @version 1.0
   */
  
  public class ListController {
    InterfaceFrame frame = null;
    JScrollPane treeScrollPane;
    JSplitPane splitPane;
    JTextPane textPane; //where an xml file is displayed
    DefaultListModel listmodel = new DefaultListModel();
    JList list = new JList(listmodel);
    /*JLabel editableLabel = new JLabel();
       JLabel uneditableLabel = new JLabel();
       JButton refreshButton = new JButton();*/
    JLabel dirtyLabel = new JLabel();
    boolean dirty = false;
    int start; //the text selection range on xmlPane
    int end;
    JTree tree; //xml structure tree
    DomToTreeModelAdapter dom2tree;
    TreePath treepath; //the treepath of the node currently displayed
    String selectedText;
    String defaultnewline;
    static String newline = "\n";
    JPopupMenu classPopup = null;
    Vector records = new Vector();
    boolean adderror = true;
    /*JMenuItem jMenuItemPurgeList = new JMenuItem();
       JMenuItem jMenuItemShowList = new JMenuItem();
       JMenuItem jMenuItemImportList = new JMenuItem();
       JMenuItem jMenuItemRemoveListFile = new JMenuItem();
       JMenu jMenuToDoList = new JMenu();*/
    static final String[] stylecode = {
        "regular", "bold"};
  
    public ListController(InterfaceFrame frame) {
      this.frame = frame;
      /*java.net.URL url = InterfaceFrame.class.getResource(
          "toolbarButtonGraphics/general/Edit24.gif");
           if (url != null) {
        editableLabel.setIcon(new ImageIcon(url));
        editableLabel.setToolTipText("Editable");
           }
           else {
        editableLabel.setText("Editable");
           }
  
           url = InterfaceFrame.class.getResource(
          "toolbarButtonGraphics/general/Stop24.gif");
           if (url != null) {
        uneditableLabel.setIcon(new ImageIcon(url));
        uneditableLabel.setToolTipText("Uneditable");
           }
           else {
        uneditableLabel.setText("Uneditable");
           }*/
  
      //refreshButton = getRefreshButton();
      dirtyLabel = new JLabel("*");
  
      // Build right-side view:textPane
      textPane = new JTextPane();
      textPane.setContentType("text/plain; charset=Cp1252");
      textPane.setEditable(true);
      textPane.setMargin(new Insets(10, 10, 10, 10));
      textPane.addMouseListener(new ListController_xmlPane_mouseAdapter(this));
      //save system newline
      //defaultnewline = (String) textPane.getStyledDocument().getProperty(
      //    DefaultEditorKit.EndOfLineStringProperty);
      //use "\n" for newline for in memory text
  
      textPane.getStyledDocument().putProperty(DefaultEditorKit.
                                               EndOfLineStringProperty, "\n");
  
      frame.getSplitPane().setRightComponent(new JPanel());
      classPopup = new SchemaPopupMenu(Setting.schemapath,
                                       new
                                       ListController_classPopup_actionAdapter(this),
                                       new
                                       ListController_classPopup_mouseAdapter(this)).
          constructPopupMenu();
      if (classPopup == null) {
        JOptionPane.showMessageDialog(frame,
                                      "Please check and make sure DTD or Schema file exist and are valid");
      }
  
      //add Edit and Format menu
      //if ( ( (JMenu) frame.jMenuBar1.getComponent(1)).getText().compareTo("Edit") !=
      //    0) {
      //if (frame.jMenuBar1.getComponentCount() == 3) {
      //TextPaneController tpc = new TextPaneController(frame, textPane);
      /*JMenu jMenuEdit = tpc.createEditMenu();
             JMenu jMenuFormat = tpc.createFormatMenu();
             JMenu jMenuError = new JMenu("Error Record");
             JMenuItem jMenuItemAddRecord = new JMenuItem("Add record");
             jMenuItemAddRecord.addActionListener(new
       ListController_jMenuItemAddRecord_actionAdapter(this));
             JMenuItem jMenuItemShowStat = new JMenuItem("Show statistics");
             jMenuItemShowStat.addActionListener(new
       ListController_jMenuItemShowStat_actionAdapter(this));
             jMenuError.add(jMenuItemAddRecord);
             jMenuError.add(jMenuItemShowStat);
  
             jMenuToDoList.setText("To Do List");
             jMenuItemPurgeList.setEnabled(false);
             jMenuItemShowList.setEnabled(false);
             jMenuItemImportList.setEnabled(true);
             jMenuItemImportList.setActionCommand("Review");
             jMenuItemImportList.addActionListener(new
          InterfaceFrame_jMenuItemImportList_actionAdapter(frame));
             jMenuItemShowList.setText("Show list");
             jMenuItemImportList.setText("Import list");
             jMenuItemPurgeList.setText("Purge list");
             jMenuItemRemoveListFile.setActionCommand("RemoveListFile");
             jMenuItemRemoveListFile.setText("Remove list file");
             jMenuItemRemoveListFile.addActionListener(new
          InterfaceFrame_jMenuItemRemoveListFile_actionAdapter(frame));
             jMenuItemShowList.addActionListener(new
       InterfaceFrame_jMenuItemShowList_actionAdapter(
          frame));
             jMenuItemPurgeList.addActionListener(new
       InterfaceFrame_jMenuItemPurgeList_actionAdapter(
          frame));
             jMenuToDoList.add(jMenuItemRemoveListFile);
             jMenuToDoList.addSeparator();
             jMenuToDoList.add(jMenuItemPurgeList);
             jMenuToDoList.add(jMenuItemShowList);
             jMenuToDoList.addSeparator();
             jMenuToDoList.add(jMenuItemImportList);
             frame.jMenuBar1.add(jMenuEdit, 1);
             frame.jMenuBar1.add(jMenuFormat, 2);
             frame.jMenuBar1.add(jMenuError, 3);
             frame.jMenuBar1.add(jMenuToDoList, 4);*/
      //}
  
    }
  
    /*public JButton getRefreshButton() {
      java.net.URL url = InterfaceFrame.class.getResource(
          "toolbarButtonGraphics/general/Refresh24.gif");
      if (url != null) {
        refreshButton.setIcon(new ImageIcon(url));
        refreshButton.setToolTipText("Refresh");
      }
      else {
        refreshButton.setText("Refresh");
      }
      refreshButton.addActionListener(new
     ListController_refreshButton_actionAdapter(this));
  
      return refreshButton;
       }*/
  
    /**
     * perform certain functions according to the given setting
     * @param null
     * @return null
     */
    public void setUp() {
      //set up the list
      if (frame.getSetting().getTodoList() == null) {
        File file = new File(frame.getSetting().getFilePath());
        ArrayList mflist = new ArrayList();
        if (file.isDirectory()) { //from a directory
          File[] flist = file.listFiles();
          int count = 0;
          for (int i = 0; i < flist.length; i++) {
            if (addToList(flist[i])) {
              mflist.add(new MyFile(flist[i]));
            }
          }
        }
        else {
          if (addToList(file)) {
            mflist.add(new MyFile(file));
          }
        }
        frame.getSetting().setTodoList(new Vector(mflist));
        //if there is only 1 file in mflist, display the content in the textarea
        if (mflist.size() == 1) {
          list.setSelectedIndex(0);
          display( ( (MyFile) mflist.get(0)).getFile());
        }
      }
      //display list pane in frame
      //frame.jMenuItemSave.setEnabled(true);
      //frame.jMenuItemSaveAs.setEnabled(true);
      //frame.jMenuItemSave2Depot.setEnabled(true);
      //jMenuItemPurgeList.setEnabled(true);
      //jMenuItemShowList.setEnabled(true);
      frame.setDisplayed(-1); //to avoid to prompt for saving files from last session
      showList();
  
      //create popup menu from the schema
      /*frame.jSplitPane.setRightComponent(new JPanel());
           classPopup = new SchemaPopupMenu(frame.setting.getSchemaPath(),
                                       new
       ListController_classPopup_actionAdapter(this),
                                       new
       ListController_classPopup_mouseAdapter(this)).
          constructPopupMenu();
           if (classPopup == null) {
        JOptionPane.showMessageDialog(frame,
       "Please check and make sure DTD or Schema file exist and are valid");
           }*/
      //frame.toolBar.setListController(this);
      //frame.jToolBar = frame.toolBar.setDefaultToolBar(ToolBar.ANNOTE_MODE);
      //remove menuBar?
      //frame.menuBar.setListController(this);
      //frame.setJMenuBar(frame.menuBar.setDefaultMenuBar(MenuBar.ANNOTE_MODE));
    }
  
    public void showList() {
      list = new JList(frame.getSetting().getTodoList());
      list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
      list.setLayoutOrientation(JList.VERTICAL);
      list.setVisibleRowCount( -1);
      list.addListSelectionListener(new
                                    ListController_list_listSelectionListenerAdapter(this));
      //frame.listScrollPane = new JScrollPane(list);
      JScrollPane listScrollPane = new JScrollPane(list);
      //frame.getSplitPane().setLeftComponent(frame.listScrollPane);
      frame.getSplitPane().setLeftComponent(listScrollPane);
      frame.getSplitPane().setDividerLocation(200);
    }
  
    /**
     *  put the files that are not in the depot in the list
     * @param f
     * @return
     */
    private boolean addToList(File f) {
      boolean add = false;
      if (frame.getSetting().getDiff()) {
        File thisfile = new File(frame.getSetting().getDepotPath(), f.getName());
        add = thisfile.exists() ? false : true;
      }
      else {
        add = true;
      }
      return add;
    }
  
    public void valueChanged(ListSelectionEvent e) {
      if (!e.getValueIsAdjusting()) {
        if (textPane != null) {
          textPane.setText("");
        }
        if (tree != null) {
          tree.setVisible(false);
        }
        frame.setDisplayed(list.getSelectedIndex());
        System.out.println("new displayed: " + frame.getDisplayed());
        File file = ( (MyFile) frame.getSetting().getTodoList().get(frame.getDisplayed())).
            getFile();
        display(file);
        dirty = false;
      }
    }
  
    /**
     * display the content of the file in the textarea
     * @param file
     */
    private void display(File file) {
      String content = readFileContent(file);
      displayText(content);
      frame.getStatus().setText(file.getName() + " [" +
                              (list.getSelectedIndex() + 1) + "/" +
                              frame.getSetting().getTodoList().size() + "]");
  
    }
  
    public void displayText(String content) {
      //build and show the tree
      try {
        if (content.indexOf(DomToTreeModelAdapter.xmlin) < 0 &&
            content.indexOf("<") < 0) {
          //insert a fake element dooo to ensure the well-formedness of annotation
          //annotation
          content = content.replaceAll(System.getProperty("line.separator"), "");
          String ctext = "<description><dooo>" + content +
              "</dooo></description>";
          dom2tree = new DomToTreeModelAdapter(ctext);
        }
        else {
          dom2tree = new DomToTreeModelAdapter(content); //review
        }
        tree = new JTree(dom2tree);
        treeListener(tree);
        JScrollPane xmlScrollPane = new JScrollPane(textPane,
                                                    JScrollPane.
                                                    VERTICAL_SCROLLBAR_AS_NEEDED,
                                                    JScrollPane.
                                                    HORIZONTAL_SCROLLBAR_NEVER);
        treeScrollPane = new JScrollPane(tree);
  
        splitPane =
            new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
                           treeScrollPane,
                           xmlScrollPane);
        splitPane.setContinuousLayout(true);
        splitPane.setDividerLocation(200);
        frame.getSplitPane().setDividerLocation(200);
        frame.getSplitPane().setRightComponent(splitPane);
      }
      catch (SAXException saxe) {
        JOptionPane.showMessageDialog(frame,
                                      "File is not well-formed or valid XML");
        frame.setDisplayed(-1);
        saxe.printStackTrace();
      }
  
    }
  
    public void treeListener(JTree tree) {
      tree.addTreeSelectionListener(
          new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent e) {
          treepath = e.getNewLeadSelectionPath();
          if (treepath != null) {
            AdapterNode adpnode = (AdapterNode) treepath.getLastPathComponent();
            if (adpnode.getNode() == dom2tree.document.getDocumentElement()) {
              textPane.setEditable(true);
              //frame.toolBar.setListController(frame.listcontroller);
              //frame.toolBar.adjustToolBarEditButtons(true);
            }
            else {
              textPane.setEditable(false);
              //frame.toolBar.setListController(frame.listcontroller);
              //frame.toolBar.adjustToolBarEditButtons(false);
              //adjustToolBarEditButtons(false);
            }
            style4XmlPane(adpnode.getNode().toString());
          }
        }
      }
      );
  
    }
  
    /* public void adjustToolBarEditButtons(boolean editable) {
       frame.jToolBar.repaint(); //make any previous changes take effects
       int editableindex = frame.jToolBar.getComponentIndex(editableLabel);
       int uneditableindex = frame.jToolBar.getComponentIndex(
           uneditableLabel); //current status
       if (editable) {
         textPane.setEditable(true);
         frame.jMenuBar1.getComponent(1).setEnabled(true); //enable edit menu
         if (uneditableindex < 0 && editableindex < 0) {
           frame.jToolBar.add(refreshButton);
           frame.jToolBar.addSeparator(new Dimension(10, 34));
           frame.jToolBar.add(editableLabel);
           frame.jToolBar.repaint();
         }
         else if (uneditableindex >= 0 &&
     frame.jToolBar.getComponentAtIndex(uneditableindex - 1) instanceof
                  JToolBar.Separator) {
           frame.jToolBar.remove(uneditableLabel);
           frame.jToolBar.add(refreshButton, uneditableindex - 1);
           frame.jToolBar.add(editableLabel, uneditableindex + 1);
           frame.jToolBar.repaint();
         }
       }
       else {
         textPane.setEditable(false);
         frame.jMenuBar1.getComponent(1).setEnabled(false); //disable edit menu
         if (uneditableindex < 0 && editableindex < 0) {
           frame.jToolBar.addSeparator(new Dimension(10, 34));
           frame.jToolBar.add(uneditableLabel);
           frame.jToolBar.repaint();
         }
         else if (editableindex >= 0 &&
     frame.jToolBar.getComponentAtIndex(editableindex - 1) instanceof
                  JToolBar.Separator) { //change from editable to uneditable
           frame.jToolBar.remove(refreshButton);
           frame.jToolBar.remove(editableLabel);
           frame.jToolBar.remove(editableindex - 2); //remove the seperator
           //for some reason the following two lines have no effect on the toolbar
           frame.jToolBar.addSeparator(new Dimension(10, 34));
           frame.jToolBar.add(uneditableLabel, editableindex - 1);
  
         }
       }
  
     }*/
  
    /**
     * display tags in bold
     * so far could not get indentions work.
     * @param xmlseg
     */
    public void style4XmlPane(String xmlseg) {
  
        StyledDocument doc = textPane.getStyledDocument();
  
        try {
          doc.remove(0, doc.getLength()); //clean up whatever is left on the pane
  