Search in sources :

Example 1 with ExtensionFileFilter

use of gate.util.ExtensionFileFilter in project gate-core by GateNLP.

the class LogArea method getComponentToDisplay.

// LogArea
public JComponent getComponentToDisplay() {
    // don't build the display more than once
    if (logTab != null)
        return logTab;
    // create the main panel we will return so we can add things to it
    logTab = new JPanel(new BorderLayout());
    JToolBar toolbar = new JToolBar(JToolBar.HORIZONTAL);
    toolbar.setFloatable(false);
    JButton btnClear = new JButton(MainFrame.getIcon("ClearLog"));
    btnClear.setToolTipText("Clear Log");
    btnClear.addActionListener(clearAllAction);
    btnScrollLock = new JToggleButton(MainFrame.getIcon("ScrollLock"), userConfig.getBoolean("ScrollLock", Boolean.FALSE));
    btnScrollLock.setToolTipText("Scroll Lock");
    btnScrollLock.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            userConfig.put("ScrollLock", btnScrollLock.isSelected());
        }
    });
    logSizeModel = new SpinnerNumberModel(userConfig.getInt("LogSize", 80000).intValue(), 0, Integer.MAX_VALUE, 1);
    logSizeModel.addChangeListener(new ChangeListener() {

        @Override
        public void stateChanged(ChangeEvent arg0) {
            userConfig.put("LogSize", logSizeModel.getValue());
        }
    });
    final JSpinner spinLogSize = new JSpinner(logSizeModel);
    if (spinLogSize.getEditor() instanceof JSpinner.DefaultEditor) {
        JTextField textField = ((JSpinner.DefaultEditor) spinLogSize.getEditor()).getTextField();
        textField.setColumns(5);
    }
    cboLogSize = new JCheckBox("Max Log Size (chars)", userConfig.getBoolean("LimitLogSize", Boolean.TRUE));
    cboLogSize.setOpaque(false);
    spinLogSize.setEnabled(cboLogSize.isSelected());
    cboLogSize.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            spinLogSize.setEnabled(cboLogSize.isSelected());
            userConfig.put("LimitLogSize", cboLogSize.isSelected());
        }
    });
    toolbar.add(cboLogSize);
    toolbar.add(spinLogSize);
    toolbar.addSeparator();
    Icon fileIcon = MainFrame.getIcon("OpenFile");
    final JButton btnLogFile = new JButton(fileIcon);
    BufferedImage disabledIcon = new BufferedImage(fileIcon.getIconWidth(), fileIcon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = disabledIcon.createGraphics();
    g2d.setComposite(AlphaComposite.SrcOver.derive(0.1f));
    fileIcon.paintIcon(null, g2d, 0, 0);
    btnLogFile.setDisabledIcon(new ImageIcon(disabledIcon));
    btnLogFile.setToolTipText("Select Log File");
    btnLogFile.setEnabled(userConfig.getBoolean("LogToFile", Boolean.FALSE));
    final JTextField txtLogFile = new JTextField(20);
    logFile = userConfig.getFile("LogFile");
    if (logFile != null) {
        try {
            if (btnLogFile.isEnabled())
                logFileWriter = new PrintWriter(new FileWriter(logFile, true));
            txtLogFile.setText(logFile.getAbsolutePath());
        } catch (IOException ioe) {
            logFile = null;
        }
    }
    btnLogFile.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            XJFileChooser fileChooser = MainFrame.getFileChooser();
            ExtensionFileFilter filter = new ExtensionFileFilter("Log Files (*.txt)", "txt");
            fileChooser.addChoosableFileFilter(filter);
            fileChooser.setMultiSelectionEnabled(false);
            fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
            fileChooser.setDialogTitle("Log File");
            if (fileChooser.showSaveDialog(MainFrame.getInstance()) != JFileChooser.APPROVE_OPTION)
                return;
            File logFile = fileChooser.getSelectedFile();
            if (logFile == null)
                return;
            try {
                logFileWriter = new PrintWriter(new FileWriter(logFile, true));
                userConfig.put("LogFile", logFile);
                txtLogFile.setText(logFile.getAbsolutePath());
            } catch (IOException ioe) {
                logFile = null;
                logFileWriter = null;
                txtLogFile.setText("");
                userConfig.remove("LogFile");
                ioe.printStackTrace();
            }
        }
    });
    txtLogFile.setEditable(false);
    txtLogFile.setEnabled(btnLogFile.isEnabled());
    cboAppend = new JCheckBox("Append To", btnLogFile.isEnabled());
    cboAppend.setOpaque(false);
    cboAppend.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            try {
                logFileWriter = cboAppend.isSelected() && logFile != null ? new PrintWriter(new FileWriter(logFile, true)) : null;
                btnLogFile.setEnabled(cboAppend.isSelected());
                txtLogFile.setEnabled(cboAppend.isSelected());
                userConfig.put("LogToFile", cboAppend.isSelected());
            } catch (IOException e) {
                logFile = null;
                logFileWriter = null;
                txtLogFile.setText("");
                userConfig.remove("LogFile");
                e.printStackTrace();
            }
        }
    });
    toolbar.add(cboAppend);
    toolbar.add(txtLogFile);
    toolbar.add(btnLogFile);
    toolbar.addSeparator();
    toolbar.add(Box.createHorizontalGlue());
    toolbar.add(btnClear);
    toolbar.add(btnScrollLock);
    // add the text area as the main component, inside a scroller
    logTab.add(new JScrollPane(this), BorderLayout.CENTER);
    logTab.add(toolbar, BorderLayout.SOUTH);
    // return the display
    return logTab;
}
Also used : JPanel(javax.swing.JPanel) ImageIcon(javax.swing.ImageIcon) ActionEvent(java.awt.event.ActionEvent) XJFileChooser(gate.swing.XJFileChooser) FileWriter(java.io.FileWriter) JButton(javax.swing.JButton) JTextField(javax.swing.JTextField) BufferedImage(java.awt.image.BufferedImage) SpinnerNumberModel(javax.swing.SpinnerNumberModel) BorderLayout(java.awt.BorderLayout) JToggleButton(javax.swing.JToggleButton) ChangeListener(javax.swing.event.ChangeListener) PrintWriter(java.io.PrintWriter) JScrollPane(javax.swing.JScrollPane) JToolBar(javax.swing.JToolBar) IOException(java.io.IOException) Graphics2D(java.awt.Graphics2D) JCheckBox(javax.swing.JCheckBox) ActionListener(java.awt.event.ActionListener) ChangeEvent(javax.swing.event.ChangeEvent) JSpinner(javax.swing.JSpinner) Icon(javax.swing.Icon) ImageIcon(javax.swing.ImageIcon) ExtensionFileFilter(gate.util.ExtensionFileFilter) File(java.io.File)

Aggregations

XJFileChooser (gate.swing.XJFileChooser)1 ExtensionFileFilter (gate.util.ExtensionFileFilter)1 BorderLayout (java.awt.BorderLayout)1 Graphics2D (java.awt.Graphics2D)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 BufferedImage (java.awt.image.BufferedImage)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 Icon (javax.swing.Icon)1 ImageIcon (javax.swing.ImageIcon)1 JButton (javax.swing.JButton)1 JCheckBox (javax.swing.JCheckBox)1 JPanel (javax.swing.JPanel)1 JScrollPane (javax.swing.JScrollPane)1 JSpinner (javax.swing.JSpinner)1 JTextField (javax.swing.JTextField)1 JToggleButton (javax.swing.JToggleButton)1