/*
- * jSite - ProjectFilesPage.java - Copyright © 2006–2012 David Roden
+ * jSite - ProjectFilesPage.java - Copyright © 2006–2019 David Roden
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
package de.todesbaum.jsite.gui;
+import static java.util.Optional.ofNullable;
+
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.MessageFormat;
+import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
-import java.util.List;
+import java.util.Optional;
import java.util.Set;
-
+import java.util.function.Consumer;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.event.ListSelectionListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
+import javax.swing.text.JTextComponent;
import net.pterodactylus.util.io.MimeTypes;
import net.pterodactylus.util.swing.SwingUtils;
import net.pterodactylus.util.thread.StoppableDelay;
+
import de.todesbaum.jsite.application.FileOption;
import de.todesbaum.jsite.application.Project;
-import de.todesbaum.jsite.gui.FileScanner.ScannedFile;
import de.todesbaum.jsite.i18n.I18n;
import de.todesbaum.jsite.i18n.I18nContainer;
import de.todesbaum.util.swing.TLabel;
@Override
public void pageAdded(TWizard wizard) {
/* create file scanner. */
- fileScanner = new FileScanner(project);
- fileScanner.addFileScannerListener(this);
+ fileScanner = new FileScanner(project, this);
actionScan();
this.wizard.setPreviousName(I18n.getMessage("jsite.wizard.previous"));
fileOptionsRenameTextField = new JTextField();
fileOptionsRenameTextField.setEnabled(false);
- fileOptionsRenameTextField.getDocument().addDocumentListener(new DocumentListener() {
-
- @SuppressWarnings("synthetic-access")
- private void storeText(DocumentEvent documentEvent) {
- FileOption fileOption = getSelectedFile();
- if (fileOption == null) {
- /* no file selected. */
- return;
- }
- Document document = documentEvent.getDocument();
- int documentLength = document.getLength();
- try {
- fileOption.setChangedName(document.getText(0, documentLength).trim());
- } catch (BadLocationException ble1) {
- /* ignore, it should never happen. */
- }
- }
-
- @Override
- public void changedUpdate(DocumentEvent documentEvent) {
- storeText(documentEvent);
- }
-
- @Override
- public void insertUpdate(DocumentEvent documentEvent) {
- storeText(documentEvent);
- }
-
- @Override
- public void removeUpdate(DocumentEvent documentEvent) {
- storeText(documentEvent);
- }
-
- });
+ fileOptionsRenameTextField.getDocument().addDocumentListener(new StoreDocument(this::updateChangedName));
fileOptionsPanel.add(fileOptionsRenameCheckBox, new GridBagConstraints(0, 8, 2, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
fileOptionsPanel.add(fileOptionsRenameTextField, new GridBagConstraints(2, 8, 3, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
fileOptionsMIMETypeComboBox.addActionListener(this);
fileOptionsMIMETypeComboBox.setEditable(true);
fileOptionsMIMETypeComboBox.setEnabled(false);
+ ((JTextComponent) fileOptionsMIMETypeComboBox.getEditor().getEditorComponent())
+ .getDocument()
+ .addDocumentListener(new StoreDocument(this::updateMimeType));
final TLabel mimeTypeLabel = new TLabel(I18n.getMessage("jsite.project-files.mime-type") + ":", KeyEvent.VK_M, fileOptionsMIMETypeComboBox);
fileOptionsPanel.add(mimeTypeLabel, new GridBagConstraints(0, 9, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
scanningFilesDialog.getContentPane().add(progressPanel, BorderLayout.CENTER);
progressPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
- final TLabel scanningLabel = new TLabel(I18n.getMessage("jsite.project-files.scanning"), SwingConstants.CENTER);
+ final JLabel scanningLabel = new JLabel(I18n.getMessage("jsite.project-files.scanning"), SwingConstants.CENTER);
progressPanel.add(scanningLabel, BorderLayout.NORTH);
progressBar = new JProgressBar(SwingConstants.HORIZONTAL);
progressPanel.add(progressBar, BorderLayout.SOUTH);
scanningFilesDialog.setVisible(false);
}
}, 2000);
- new Thread(fileScanner).start();
+ fileScanner.startInBackground();
new Thread(delayedNotification).start();
new Thread(new Runnable() {
* Updates the file list.
*/
@Override
- public void fileScannerFinished(FileScanner fileScanner) {
+ public void fileScannerFinished(boolean error, Collection<ScannedFile> files) {
delayedNotification.finish();
- final boolean error = fileScanner.isError();
if (!error) {
- final List<ScannedFile> files = fileScanner.getFiles();
SwingUtilities.invokeLater(new Runnable() {
@Override
});
}
- /**
- * Returns the {@link FileOption file options} for the currently selected
- * file.
- *
- * @return The {@link FileOption}s for the selected file, or {@code null} if
- * no file is selected
- */
- private FileOption getSelectedFile() {
- ScannedFile scannedFile = (ScannedFile) projectFileList.getSelectedValue();
- if (scannedFile == null) {
- return null;
- }
- return project.getFileOption(scannedFile.getFilename());
+ private Optional<FileOption> getSelectedFile() {
+ return ofNullable(((ScannedFile) projectFileList.getSelectedValue()))
+ .map(scannedFile -> project.getFileOption(scannedFile.getFilename()));
}
//
} else if (source instanceof JComboBox) {
JComboBox comboBox = (JComboBox) source;
if ("project-files.mime-type".equals(comboBox.getName())) {
- fileOption.setMimeType((String) comboBox.getSelectedItem());
+ updateMimeType((String) comboBox.getSelectedItem());
}
}
}
+ private void updateMimeType(String mimeType) {
+ getSelectedFile().ifPresent(fileOption -> fileOption.setMimeType(mimeType));
+ }
+
+ private void updateChangedName(String changedName) {
+ getSelectedFile().ifPresent(fileOption -> fileOption.setChangedName(changedName));
+ }
+
//
// INTERFACE ListSelectionListener
//
fileOptionsCustomKeyTextField.setText(fileOption.getCustomKey());
fileOptionsRenameCheckBox.setSelected(fileOption.getChangedName().isPresent());
fileOptionsRenameTextField.setEnabled(fileOption.getChangedName().isPresent());
- fileOptionsRenameTextField.setText(fileOption.getChangedName().or(""));
+ fileOptionsRenameTextField.setText(fileOption.getChangedName().orElse(""));
fileOptionsMIMETypeComboBox.getModel().setSelectedItem(fileOption.getMimeType());
} else {
defaultFileCheckBox.setSelected(false);
processDocumentUpdate(documentEvent);
}
+ private static class StoreDocument implements DocumentListener {
+
+ private final Consumer<String> consumer;
+
+ public StoreDocument(Consumer<String> consumer) {
+ this.consumer = consumer;
+ }
+
+ private void storeDocument(DocumentEvent e) {
+ try {
+ consumer.accept(e.getDocument().getText(0, e.getDocument().getLength()));
+ } catch (BadLocationException e1) {
+ e1.printStackTrace();
+ }
+ }
+
+ @Override
+ public void insertUpdate(DocumentEvent e) {
+ storeDocument(e);
+ }
+
+ @Override
+ public void removeUpdate(DocumentEvent e) {
+ storeDocument(e);
+ }
+
+ @Override
+ public void changedUpdate(DocumentEvent e) {
+ storeDocument(e);
+ }
+
+ }
+
}