X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Ftodesbaum%2Fjsite%2Fgui%2FProjectFilesPage.java;h=de117e45aab15134befe7846db90c002345a512a;hb=8db42d2121e8ee465ab8380a66febde1949a0106;hp=67287b76cf5ccf4f8325e5345f4d70e99674e893;hpb=9abfce460662cf2860cc989f49d1425fd2d482bf;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 67287b7..de117e4 100644 --- a/src/main/java/de/todesbaum/jsite/gui/ProjectFilesPage.java +++ b/src/main/java/de/todesbaum/jsite/gui/ProjectFilesPage.java @@ -1,5 +1,5 @@ /* - * 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 @@ -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; @@ -85,6 +89,9 @@ public class ProjectFilesPage extends TWizardPage implements ActionListener, Lis /** The “scan files” action. */ private Action scanAction; + /** The “always force insert” checkbox. */ + private JCheckBox alwaysForceInsertCheckBox; + /** The “ignore hidden files” checkbox. */ private JCheckBox ignoreHiddenFilesCheckBox; @@ -179,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")); @@ -208,16 +214,22 @@ public class ProjectFilesPage extends TWizardPage implements ActionListener, Lis JPanel fileOptionsPanel = new JPanel(new GridBagLayout()); fileOptionsAlignmentPanel.add(fileOptionsPanel, BorderLayout.PAGE_START); + alwaysForceInsertCheckBox = new JCheckBox(I18n.getMessage("jsite.project-files.always-force-insert")); + alwaysForceInsertCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.always-force-insert.tooltip")); + alwaysForceInsertCheckBox.setName("always-force-insert"); + alwaysForceInsertCheckBox.addActionListener(this); + fileOptionsPanel.add(alwaysForceInsertCheckBox, new GridBagConstraints(0, 0, 5, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); + ignoreHiddenFilesCheckBox = new JCheckBox(I18n.getMessage("jsite.project-files.ignore-hidden-files")); ignoreHiddenFilesCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.ignore-hidden-files.tooltip")); ignoreHiddenFilesCheckBox.setName("ignore-hidden-files"); ignoreHiddenFilesCheckBox.addActionListener(this); - fileOptionsPanel.add(ignoreHiddenFilesCheckBox, new GridBagConstraints(0, 0, 5, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); + fileOptionsPanel.add(ignoreHiddenFilesCheckBox, new GridBagConstraints(0, 1, 5, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); - fileOptionsPanel.add(new JButton(scanAction), new GridBagConstraints(0, 1, 5, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 0, 0, 0), 0, 0)); + fileOptionsPanel.add(new JButton(scanAction), new GridBagConstraints(0, 2, 5, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 0, 0, 0), 0, 0)); final JLabel fileOptionsLabel = new JLabel("" + I18n.getMessage("jsite.project-files.file-options") + ""); - fileOptionsPanel.add(fileOptionsLabel, new GridBagConstraints(0, 2, 5, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 0, 0, 0), 0, 0)); + fileOptionsPanel.add(fileOptionsLabel, new GridBagConstraints(0, 3, 5, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 0, 0, 0), 0, 0)); defaultFileCheckBox = new JCheckBox(I18n.getMessage("jsite.project-files.default")); defaultFileCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.default.tooltip")); @@ -225,7 +237,7 @@ public class ProjectFilesPage extends TWizardPage implements ActionListener, Lis defaultFileCheckBox.addActionListener(this); defaultFileCheckBox.setEnabled(false); - fileOptionsPanel.add(defaultFileCheckBox, new GridBagConstraints(0, 3, 5, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 18, 0, 0), 0, 0)); + fileOptionsPanel.add(defaultFileCheckBox, new GridBagConstraints(0, 4, 5, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 18, 0, 0), 0, 0)); fileOptionsInsertCheckBox = new JCheckBox(I18n.getMessage("jsite.project-files.insert"), true); fileOptionsInsertCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.insert.tooltip")); @@ -234,7 +246,7 @@ public class ProjectFilesPage extends TWizardPage implements ActionListener, Lis fileOptionsInsertCheckBox.addActionListener(this); fileOptionsInsertCheckBox.setEnabled(false); - fileOptionsPanel.add(fileOptionsInsertCheckBox, new GridBagConstraints(0, 4, 5, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0)); + fileOptionsPanel.add(fileOptionsInsertCheckBox, new GridBagConstraints(0, 5, 5, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0)); fileOptionsForceInsertCheckBox = new JCheckBox(I18n.getMessage("jsite.project-files.force-insert")); fileOptionsForceInsertCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.force-insert.tooltip")); @@ -243,7 +255,7 @@ public class ProjectFilesPage extends TWizardPage implements ActionListener, Lis fileOptionsForceInsertCheckBox.addActionListener(this); fileOptionsForceInsertCheckBox.setEnabled(false); - fileOptionsPanel.add(fileOptionsForceInsertCheckBox, new GridBagConstraints(0, 5, 5, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0)); + fileOptionsPanel.add(fileOptionsForceInsertCheckBox, new GridBagConstraints(0, 6, 5, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0)); fileOptionsCustomKeyTextField = new JTextField(45); fileOptionsCustomKeyTextField.setToolTipText(I18n.getMessage("jsite.project-files.custom-key.tooltip")); @@ -258,9 +270,9 @@ public class ProjectFilesPage extends TWizardPage implements ActionListener, Lis fileOptionsInsertRedirectCheckBox.setEnabled(false); final TLabel customKeyLabel = new TLabel(I18n.getMessage("jsite.project-files.custom-key") + ":", KeyEvent.VK_K, fileOptionsCustomKeyTextField); - fileOptionsPanel.add(fileOptionsInsertRedirectCheckBox, new GridBagConstraints(0, 6, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0)); - fileOptionsPanel.add(customKeyLabel, new GridBagConstraints(1, 6, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 6, 0, 0), 0, 0)); - fileOptionsPanel.add(fileOptionsCustomKeyTextField, new GridBagConstraints(2, 6, 3, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0)); + fileOptionsPanel.add(fileOptionsInsertRedirectCheckBox, new GridBagConstraints(0, 7, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0)); + fileOptionsPanel.add(customKeyLabel, new GridBagConstraints(1, 7, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 6, 0, 0), 0, 0)); + fileOptionsPanel.add(fileOptionsCustomKeyTextField, new GridBagConstraints(2, 7, 3, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0)); fileOptionsRenameCheckBox = new JCheckBox(I18n.getMessage("jsite.project-files.rename"), false); fileOptionsRenameCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.rename.tooltip")); @@ -271,43 +283,10 @@ public class ProjectFilesPage extends TWizardPage implements ActionListener, Lis fileOptionsRenameTextField = new JTextField(); fileOptionsRenameTextField.setEnabled(false); - fileOptionsRenameTextField.getDocument().addDocumentListener(new DocumentListener() { + fileOptionsRenameTextField.getDocument().addDocumentListener(new StoreDocument(this::updateChangedName)); - @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); - } - - }); - - fileOptionsPanel.add(fileOptionsRenameCheckBox, new GridBagConstraints(0, 7, 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, 7, 3, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0)); + 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 = new JComboBox(MimeTypes.getAllMimeTypes().toArray()); fileOptionsMIMETypeComboBox.setToolTipText(I18n.getMessage("jsite.project-files.mime-type.tooltip")); @@ -315,10 +294,13 @@ 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, 8, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0)); - fileOptionsPanel.add(fileOptionsMIMETypeComboBox, new GridBagConstraints(1, 8, 4, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0)); + 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)); + fileOptionsPanel.add(fileOptionsMIMETypeComboBox, new GridBagConstraints(1, 9, 4, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0)); /* create dialog to show while scanning. */ scanningFilesDialog = new JDialog(wizard); @@ -329,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); @@ -355,6 +337,8 @@ public class ProjectFilesPage extends TWizardPage implements ActionListener, Lis @Override @SuppressWarnings("synthetic-access") public void run() { + alwaysForceInsertCheckBox.setText(I18n.getMessage("jsite.project-files.always-force-insert")); + alwaysForceInsertCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.always-force-insert.tooltip")); ignoreHiddenFilesCheckBox.setText(I18n.getMessage("jsite.project-files.ignore-hidden-files")); ignoreHiddenFilesCheckBox.setToolTipText(I18n.getMessage("jsite.projet-files.ignore-hidden-files.tooltip")); fileOptionsLabel.setText("" + I18n.getMessage("jsite.project-files.file-options") + ""); @@ -390,6 +374,7 @@ public class ProjectFilesPage extends TWizardPage implements ActionListener, Lis setHeading(MessageFormat.format(I18n.getMessage("jsite.project-files.heading"), project.getName())); setDescription(I18n.getMessage("jsite.project-files.description")); ignoreHiddenFilesCheckBox.setSelected(project.isIgnoreHiddenFiles()); + alwaysForceInsertCheckBox.setSelected(project.isAlwaysForceInsert()); I18nContainer.getInstance().registerRunnable(new Runnable() { @Override @@ -433,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() { @@ -458,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 @@ -507,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())); } // @@ -532,10 +505,17 @@ public class ProjectFilesPage extends TWizardPage implements ActionListener, Lis @Override public void actionPerformed(ActionEvent actionEvent) { Object source = actionEvent.getSource(); - if ((source instanceof JCheckBox) && ("ignore-hidden-files".equals(((JCheckBox) source).getName()))) { - project.setIgnoreHiddenFiles(((JCheckBox) source).isSelected()); - actionScan(); - return; + if (source instanceof JCheckBox) { + String checkboxName = ((JCheckBox) source).getName(); + if ("ignore-hidden-files".equals(checkboxName)) { + project.setIgnoreHiddenFiles(((JCheckBox) source).isSelected()); + actionScan(); + return; + } else if ("always-force-insert".equals(checkboxName)) { + project.setAlwaysForceInsert(((JCheckBox) source).isSelected()); + valueChanged(null); + return; + } } ScannedFile scannedFile = (ScannedFile) projectFileList.getSelectedValue(); if (scannedFile == null) { @@ -577,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 // @@ -603,15 +591,15 @@ public class ProjectFilesPage extends TWizardPage implements ActionListener, Lis FileOption fileOption = project.getFileOption(filename); defaultFileCheckBox.setSelected(filename.equals(project.getIndexFile())); fileOptionsInsertCheckBox.setSelected(fileOption.isInsert()); - fileOptionsForceInsertCheckBox.setEnabled(scannedFile.getHash().equals(fileOption.getLastInsertHash())); + fileOptionsForceInsertCheckBox.setEnabled(!project.isAlwaysForceInsert() && scannedFile.getHash().equals(fileOption.getLastInsertHash())); fileOptionsForceInsertCheckBox.setSelected(fileOption.isForceInsert()); fileOptionsInsertRedirectCheckBox.setEnabled(!fileOption.isInsert()); fileOptionsInsertRedirectCheckBox.setSelected(fileOption.isInsertRedirect()); fileOptionsCustomKeyTextField.setEnabled(fileOption.isInsertRedirect()); fileOptionsCustomKeyTextField.setText(fileOption.getCustomKey()); - fileOptionsRenameCheckBox.setSelected(fileOption.hasChangedName()); - fileOptionsRenameTextField.setEnabled(fileOption.hasChangedName()); - fileOptionsRenameTextField.setText(fileOption.getChangedName()); + fileOptionsRenameCheckBox.setSelected(fileOption.getChangedName().isPresent()); + fileOptionsRenameTextField.setEnabled(fileOption.getChangedName().isPresent()); + fileOptionsRenameTextField.setText(fileOption.getChangedName().orElse("")); fileOptionsMIMETypeComboBox.getModel().setSelectedItem(fileOption.getMimeType()); } else { defaultFileCheckBox.setSelected(false); @@ -680,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); + } + + } + }