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;
}
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;
}
Aggregations