You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
179 lines
4.9 KiB
179 lines
4.9 KiB
package de.srsoftware.belegscanner.gui; |
|
|
|
import java.awt.Component; |
|
import java.awt.Dimension; |
|
import java.sql.Struct; |
|
import java.util.Arrays; |
|
import java.util.HashMap; |
|
import java.util.List; |
|
import java.util.Map; |
|
import java.util.Vector; |
|
import java.util.stream.Collectors; |
|
|
|
import javax.swing.*; |
|
import javax.swing.border.EmptyBorder; |
|
|
|
import de.srsoftware.belegscanner.Constants; |
|
import de.srsoftware.belegscanner.Worker; |
|
import org.json.JSONObject; |
|
import org.slf4j.Logger; |
|
import org.slf4j.LoggerFactory; |
|
|
|
import de.srsoftware.tools.gui.DateChooser; |
|
import de.srsoftware.tools.gui.SelectComboBox; |
|
|
|
import static de.srsoftware.belegscanner.Application.*; |
|
|
|
/** |
|
* create the application's toolbar |
|
* @author Stephan Richter <s.richter@srsoftware.de> |
|
* |
|
*/ |
|
public class Toolbar extends JPanel { |
|
|
|
private final SelectComboBox pathPicker, catPicker; |
|
private static final int OFFSET = 2; |
|
|
|
|
|
|
|
private class AdditionalInput { |
|
private final JPanel panel; |
|
private final SelectComboBox input; |
|
private boolean required; |
|
|
|
AdditionalInput(JPanel panel, SelectComboBox input, boolean required){ |
|
this.panel = panel; |
|
this.input = input; |
|
this.required = required; |
|
} |
|
|
|
public void notRequired () { |
|
required = false; |
|
} |
|
} |
|
private static final Logger LOG = LoggerFactory.getLogger(Toolbar.class); |
|
private final Worker worker; |
|
private static final long serialVersionUID = -5834326573752788233L; |
|
private static EmptyBorder BORDER = new EmptyBorder(0,5,5,5); |
|
private static Map<String, AdditionalInput> additionalInputs = new HashMap<>(); |
|
|
|
private Vector<Object> paths = new Vector<>(); |
|
|
|
|
|
private DateChooser date; |
|
|
|
|
|
private JButton scanButton; |
|
|
|
public Toolbar(Worker worker) { |
|
this.worker = worker; |
|
worker.setToolbar(this); |
|
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); |
|
add(datePicker()); |
|
add(input(t("Category"),catPicker = categoryPicker())); |
|
add(input(t("Path"),pathPicker = pathPicker())); |
|
add(new FormatSelector(worker)); |
|
add(new ResolutionSelector()); |
|
add(new ImproveSelector()); |
|
add(new TypeSelector(worker)); |
|
add(scanButton()); |
|
add(Box.createGlue()); |
|
Arrays.stream(getComponents()).filter(c -> c instanceof JPanel).map(JPanel.class::cast).forEach(p -> p.setBorder(BORDER)); |
|
} |
|
|
|
private SelectComboBox categoryPicker() { |
|
return new SelectComboBox(worker.getCategories().keySet()).onUpdateText(worker::setCat).onDelete(worker::dropCategory); |
|
} |
|
|
|
/** |
|
* create the date picker |
|
* @return |
|
*/ |
|
private Component datePicker() { |
|
date = new DateChooser(); |
|
Dimension dim = new Dimension(400, 250); |
|
date.setMaximumSize(dim); |
|
worker.setDatePicker(date); |
|
|
|
return date; |
|
} |
|
|
|
public void dropUnusedAdditionalFields() { |
|
additionalInputs.entrySet() |
|
.stream() |
|
.filter(entry -> !entry.getValue().required) |
|
.map(Map.Entry::getValue) |
|
.map(ai -> ai.panel) |
|
.forEach(this::remove); |
|
} |
|
|
|
/** |
|
* creates a labeled input field |
|
* @param caption |
|
* @param component |
|
* @return |
|
*/ |
|
private JPanel input(String caption, JComponent component) { |
|
component.setMaximumSize(new Dimension(600, 40)); |
|
component.setToolTipText(t("Press Shift + Delete to remove an entry.")); |
|
JPanel panel = new JPanel(); |
|
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS)); |
|
panel.add(new JLabel(caption+": ")); |
|
panel.add(component); |
|
return panel; |
|
} |
|
|
|
/** |
|
* creates an auto-complete text field for setting the scan target path |
|
* @return |
|
*/ |
|
private SelectComboBox pathPicker() { |
|
JSONObject cats = worker.getCategories(); |
|
for (String catName : cats.keySet()) { |
|
JSONObject catInfo = cats.getJSONObject(catName); |
|
if (catInfo.has("path")) paths.add(catInfo.get("path")); |
|
} |
|
paths.add("$"+t("HOME")+"/$"+t("YEAR")+"/$"+t("MONTH")+"-$"+t("DAY")+" - $"+t("CATEGORY")+"/$"+t("PAGE")); |
|
return worker.setPathPicker(new SelectComboBox(paths)); |
|
} |
|
|
|
|
|
public String requireField(String varName){ |
|
LOG.debug("requireField({})",varName); |
|
var additionalInput = additionalInputs.get(varName); |
|
if (additionalInput == null) { |
|
LOG.debug("Field missing, creating…"); |
|
var input = new SelectComboBox(new Vector<>()); |
|
if (varName.equals(t(Constants.PAGE))){ |
|
return "["+varName+"]"; |
|
} |
|
input.onUpdateText(newText -> worker.resolvePath()); |
|
JPanel panel = input(varName,input); |
|
additionalInputs.put(varName, additionalInput = new AdditionalInput(panel,input,true)); |
|
input.setBorder(BORDER); |
|
add(panel,getComponentCount()-OFFSET); |
|
} else { |
|
LOG.debug("Field preset, flagging as required"); |
|
additionalInput.required = true; |
|
if (!Arrays.asList(getComponents()).contains(additionalInput.panel)){ |
|
add(additionalInput.panel,getComponentCount()-OFFSET); |
|
} |
|
} |
|
return additionalInput.input.getText(); |
|
} |
|
|
|
public void resetAdditionalFields() { |
|
additionalInputs.values().forEach(AdditionalInput::notRequired); |
|
} |
|
|
|
|
|
/** |
|
* create scan button |
|
* @return |
|
*/ |
|
private Component scanButton() { |
|
scanButton = new JButton(t("scan!")); |
|
scanButton.addActionListener(ev -> worker.scan()); |
|
return scanButton; |
|
} |
|
}
|
|
|