Search in sources :

Example 1 with XJFileChooser

use of gate.swing.XJFileChooser in project gate-core by GateNLP.

the class DocumentExportMenu method getSelectedFile.

private File getSelectedFile(List<List<Parameter>> params, DocumentExporter de, FeatureMap options) {
    File selectedFile = null;
    Document document = (handle.getTarget() instanceof Document ? (Document) handle.getTarget() : null);
    // are we looking for a file or a directory?
    boolean singleFile = (document != null) || (de instanceof CorpusExporter);
    if (document != null && document.getSourceUrl() != null) {
        String fileName = "";
        try {
            fileName = document.getSourceUrl().toURI().getPath().trim();
        } catch (URISyntaxException e) {
            fileName = document.getSourceUrl().getPath().trim();
        }
        if (fileName.equals("") || fileName.equals("/")) {
            if (document.getNamedAnnotationSets().containsKey("Original markups") && !document.getAnnotations("Original markups").get("title").isEmpty()) {
                // use the title annotation if any
                try {
                    fileName = document.getContent().getContent(document.getAnnotations("Original markups").get("title").firstNode().getOffset(), document.getAnnotations("Original markups").get("title").lastNode().getOffset()).toString();
                } catch (InvalidOffsetException e) {
                    e.printStackTrace();
                }
            } else {
                fileName = document.getSourceUrl().toString();
            }
            // cleans the file name
            fileName = fileName.replaceAll("/", "_");
        } else {
            // replaces the extension with the default
            fileName = fileName.replaceAll("\\.[a-zA-Z]{1,4}$", "." + de.getDefaultExtension());
        }
        // cleans the file name
        fileName = fileName.replaceAll("[^/a-zA-Z0-9._-]", "_");
        fileName = fileName.replaceAll("__+", "_");
        // adds the default extension if not present
        if (!fileName.endsWith("." + de.getDefaultExtension())) {
            fileName += "." + de.getDefaultExtension();
        }
        selectedFile = new File(fileName);
    }
    if (params == null || params.isEmpty()) {
        XJFileChooser fileChooser = MainFrame.getFileChooser();
        fileChooser.resetChoosableFileFilters();
        fileChooser.setFileFilter(de.getFileFilter());
        fileChooser.setMultiSelectionEnabled(false);
        fileChooser.setDialogTitle("Save as " + de.getFileType());
        fileChooser.setFileSelectionMode(singleFile ? JFileChooser.FILES_ONLY : JFileChooser.DIRECTORIES_ONLY);
        if (selectedFile != null) {
            fileChooser.ensureFileIsVisible(selectedFile);
            fileChooser.setSelectedFile(selectedFile);
        }
        if (fileChooser.showSaveDialog(MainFrame.getInstance()) != JFileChooser.APPROVE_OPTION)
            return null;
        selectedFile = fileChooser.getSelectedFile();
    } else {
        if (!dialog.show(de, params, singleFile, selectedFile != null ? selectedFile.getAbsolutePath() : ""))
            return null;
        options.putAll(dialog.getSelectedParameters());
        selectedFile = new File(dialog.getSelectedFileName());
    }
    return selectedFile;
}
Also used : XJFileChooser(gate.swing.XJFileChooser) CorpusExporter(gate.CorpusExporter) InvalidOffsetException(gate.util.InvalidOffsetException) URISyntaxException(java.net.URISyntaxException) Document(gate.Document) File(java.io.File)

Example 2 with XJFileChooser

use of gate.swing.XJFileChooser 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)2 File (java.io.File)2 CorpusExporter (gate.CorpusExporter)1 Document (gate.Document)1 ExtensionFileFilter (gate.util.ExtensionFileFilter)1 InvalidOffsetException (gate.util.InvalidOffsetException)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 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 URISyntaxException (java.net.URISyntaxException)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