X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Ftodesbaum%2Fjsite%2Fgui%2FProjectFilesPage.java;h=ccd749fdd6f35288a6a50f11c055c6f011e1520c;hb=37618fed55c2d4afe2fdcb9b4b8de1539fdf163d;hp=0e700c9e94ad1ba20246030ede0197f0ed7915a4;hpb=668e00b821dce97504e8afed5038d4266ac67ce9;p=jSite.git diff --git a/src/main/java/de/todesbaum/jsite/gui/ProjectFilesPage.java b/src/main/java/de/todesbaum/jsite/gui/ProjectFilesPage.java index 0e700c9..ccd749f 100644 --- a/src/main/java/de/todesbaum/jsite/gui/ProjectFilesPage.java +++ b/src/main/java/de/todesbaum/jsite/gui/ProjectFilesPage.java @@ -18,6 +18,8 @@ package de.todesbaum.jsite.gui; +import static java.util.Optional.ofNullable; + import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.GridBagConstraints; @@ -29,11 +31,12 @@ import java.awt.event.KeyEvent; 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; @@ -59,13 +62,14 @@ import javax.swing.event.ListSelectionEvent; 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; @@ -182,8 +186,7 @@ public class ProjectFilesPage extends TWizardPage implements ActionListener, Lis @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")); @@ -280,40 +283,7 @@ public class ProjectFilesPage extends TWizardPage implements ActionListener, Lis 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)); @@ -324,6 +294,9 @@ public class ProjectFilesPage extends TWizardPage implements ActionListener, Lis 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)); @@ -338,7 +311,7 @@ public class ProjectFilesPage extends TWizardPage implements ActionListener, Lis 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); @@ -445,7 +418,7 @@ public class ProjectFilesPage extends TWizardPage implements ActionListener, Lis scanningFilesDialog.setVisible(false); } }, 2000); - new Thread(fileScanner).start(); + fileScanner.startInBackground(); new Thread(delayedNotification).start(); new Thread(new Runnable() { @@ -470,11 +443,9 @@ public class ProjectFilesPage extends TWizardPage implements ActionListener, Lis * Updates the file list. */ @Override - public void fileScannerFinished(FileScanner fileScanner) { + public void fileScannerFinished(boolean error, Collection files) { delayedNotification.finish(); - final boolean error = fileScanner.isError(); if (!error) { - final List files = fileScanner.getFiles(); SwingUtilities.invokeLater(new Runnable() { @Override @@ -519,19 +490,9 @@ public class ProjectFilesPage extends TWizardPage implements ActionListener, Lis }); } - /** - * 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 getSelectedFile() { + return ofNullable(((ScannedFile) projectFileList.getSelectedValue())) + .map(scannedFile -> project.getFileOption(scannedFile.getFilename())); } // @@ -596,11 +557,19 @@ public class ProjectFilesPage extends TWizardPage implements ActionListener, Lis } 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 // @@ -630,7 +599,7 @@ public class ProjectFilesPage extends TWizardPage implements ActionListener, Lis 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); @@ -699,4 +668,37 @@ public class ProjectFilesPage extends TWizardPage implements ActionListener, Lis processDocumentUpdate(documentEvent); } + private static class StoreDocument implements DocumentListener { + + private final Consumer consumer; + + public StoreDocument(Consumer 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); + } + + } + }