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.
265 lines
7.2 KiB
265 lines
7.2 KiB
package de.srsoftware.belegscanner.gui; |
|
|
|
import java.awt.Component; |
|
import java.awt.Dimension; |
|
import java.awt.event.ActionEvent; |
|
import java.awt.event.ActionListener; |
|
import java.util.Date; |
|
import java.util.HashMap; |
|
import java.util.HashSet; |
|
import java.util.List; |
|
import java.util.Vector; |
|
import java.util.stream.Collectors; |
|
|
|
import javax.swing.BoxLayout; |
|
import javax.swing.JButton; |
|
import javax.swing.JLabel; |
|
import javax.swing.JPanel; |
|
|
|
import org.json.JSONArray; |
|
import org.json.JSONObject; |
|
import org.slf4j.Logger; |
|
import org.slf4j.LoggerFactory; |
|
|
|
import de.srsoftware.belegscanner.Configuration; |
|
import de.srsoftware.tools.gui.DateChooser; |
|
import de.srsoftware.tools.gui.SelectComboBox; |
|
|
|
public class Toolbar extends JPanel { |
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(Toolbar.class); |
|
|
|
public interface CategoryListener{ |
|
public void setCategory(String category); |
|
} |
|
|
|
public interface DateListener{ |
|
public void setDate(Date date); |
|
} |
|
|
|
public interface FieldListener{ |
|
public void setField(String name, String value); |
|
} |
|
|
|
public interface PathListener{ |
|
public void setPath(String path); |
|
} |
|
|
|
|
|
private static final long serialVersionUID = -5834326573752788233L; |
|
|
|
private static final int OFFSET = 4; |
|
|
|
private Vector<Object> categories = new Vector<>(); |
|
private Vector<Object> paths = new Vector<>(); |
|
|
|
private HashMap<String,JPanel> additonalComponents = new HashMap<>(); |
|
private HashSet<CategoryListener> categoryListeners = new HashSet<>(); |
|
private HashSet<DateListener> dateListeners = new HashSet<>(); |
|
private HashSet<FieldListener> fieldListeners = new HashSet<>(); |
|
private HashSet<PathListener> pathListeners = new HashSet<>(); |
|
private HashSet<ActionListener> folderListeners = new HashSet<>(); |
|
private HashSet<ActionListener> joinListeners = new HashSet<>(); |
|
private HashSet<ActionListener> scanListeners = new HashSet<>(); |
|
|
|
private DateChooser date; |
|
|
|
private Configuration config; |
|
|
|
private SelectComboBox pathPicker; |
|
|
|
private JButton scanButton; |
|
|
|
private JButton joinButton; |
|
private JButton folderButton; |
|
|
|
public Toolbar(Configuration config) { |
|
this.config = config; |
|
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); |
|
add(datePicker()); |
|
add(input("Kategorie",categoryPicker(config))); |
|
add(input("Pfad",pathPicker = pathPicker())); |
|
add(scanButton()); |
|
add(joinButton()); |
|
add(folderButton()); |
|
add(externButton()); |
|
} |
|
|
|
public void addFieldsFor(Vector<String> marks) { |
|
LOG.debug("addFieldsFor({})",marks); |
|
|
|
List<String> deleted = additonalComponents.keySet().stream().filter(key -> !marks.contains(key)).collect(Collectors.toList()); |
|
deleted.forEach(name -> remove(additonalComponents.remove(name))); |
|
|
|
for (String name : marks) { |
|
if (additonalComponents.containsKey(name)) continue; |
|
Vector<Object> knownValues = new Vector<>(); |
|
SelectComboBox valuePicker = new SelectComboBox(knownValues).onUpdateText(newText -> updateField(name,newText)); |
|
JPanel input = input(name, valuePicker); |
|
add(input,getComponentCount()-OFFSET); |
|
additonalComponents.put(name, input); |
|
} |
|
} |
|
|
|
public Toolbar addCategoryListener(CategoryListener listener) { |
|
categoryListeners.add(listener); |
|
return this; |
|
} |
|
|
|
public Toolbar addDateListener(DateListener listener) { |
|
dateListeners.add(listener); |
|
return this; |
|
} |
|
|
|
public Toolbar addFieldListener(FieldListener listener) { |
|
fieldListeners.add(listener); |
|
return this; |
|
} |
|
|
|
public Toolbar addFolderListener(ActionListener listener) { |
|
folderListeners.add(listener); |
|
return this; |
|
} |
|
|
|
public Toolbar addJoinListener(ActionListener listener) { |
|
joinListeners.add(listener); |
|
return this; |
|
} |
|
|
|
|
|
public Toolbar addPathListener(PathListener listener){ |
|
pathListeners.add(listener); |
|
return this; |
|
} |
|
|
|
public Toolbar addScanListener(ActionListener listener) { |
|
scanListeners.add(listener); |
|
return this; |
|
} |
|
|
|
|
|
private Component categoryPicker(Configuration config) { |
|
JSONObject elements = config.getOrCreate("app.categories",new JSONObject()); |
|
elements.keySet().forEach(categories::add); |
|
return new SelectComboBox(categories).onUpdateText(this::updateCat); |
|
} |
|
|
|
private Component datePicker() { |
|
date = new DateChooser(); |
|
Dimension dim = new Dimension(400, 250); |
|
date.setMaximumSize(dim); |
|
date.addActionListener(ev -> dateListeners.forEach(listener -> listener.setDate(date.getSelectedDate()))); |
|
return date; |
|
} |
|
|
|
private JButton externButton() { |
|
JButton btn = new JButton("extern öffnen"); |
|
return btn; |
|
} |
|
|
|
private Component folderButton() { |
|
folderButton = new JButton("Ordner öffnen!"); |
|
folderButton.addActionListener(this::folderPressed); |
|
return folderButton; |
|
} |
|
|
|
private void folderPressed(ActionEvent evt) { |
|
folderListeners.forEach(l->l.actionPerformed(evt)); |
|
} |
|
|
|
public Date getDate() { |
|
return date.getSelectedDate(); |
|
} |
|
|
|
|
|
public SelectComboBox getSelector(JPanel panel) { |
|
if (panel != null && panel.getComponentCount()>1) { |
|
Component child = panel.getComponent(1); |
|
if (child instanceof SelectComboBox) return (SelectComboBox) child; |
|
} |
|
return null; |
|
} |
|
|
|
|
|
private JPanel input(String caption, Component component) { |
|
JPanel panel = new JPanel(); |
|
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS)); |
|
panel.add(new JLabel(caption+" ")); |
|
panel.add(component); |
|
return panel; |
|
} |
|
|
|
private Component joinButton() { |
|
joinButton = new JButton("zusammenfügen!"); |
|
joinButton.addActionListener(this::joinPressed); |
|
return joinButton; |
|
} |
|
|
|
private void joinPressed(ActionEvent evt) { |
|
joinListeners.forEach(l->l.actionPerformed(evt)); |
|
} |
|
|
|
|
|
private SelectComboBox pathPicker() { |
|
JSONObject cats = config.getOrCreate("app.categories",new JSONObject()); |
|
for (String catName : cats.keySet()) { |
|
JSONObject catInfo = cats.getJSONObject(catName); |
|
if (catInfo.has("path")) paths.add(catInfo.get("path")); |
|
} |
|
paths.add("$HOME/$JAHR/$MONAT-$TAG - $KATEGORIE"); |
|
return new SelectComboBox(paths).onUpdateText(this::updatePath); |
|
} |
|
|
|
private Component scanButton() { |
|
scanButton = new JButton("scannen!"); |
|
scanButton.addActionListener(this::scanPressed); |
|
return scanButton; |
|
} |
|
|
|
private void scanPressed(ActionEvent evt) { |
|
scanListeners.forEach(l->l.actionPerformed(evt)); |
|
} |
|
|
|
public void readyToScan(boolean val) { |
|
scanButton.setEnabled(val); |
|
|
|
} |
|
|
|
|
|
protected void updateCat(String newCat) { |
|
LOG.debug("updateCat({})",newCat); |
|
categoryListeners.forEach(l -> l.setCategory(newCat)); |
|
if (!newCat.isEmpty()) { |
|
String prefix = "app.categories."+newCat+"."; |
|
String path = config.get(prefix+"path"); |
|
if (path != null) { |
|
pathPicker.setSelectedItem(path); |
|
updatePath(path); |
|
} |
|
|
|
JSONObject fields = config.get(prefix+"fields"); |
|
if (fields == null) return; |
|
LOG.debug("fields: {}",fields); |
|
for (String fieldName : fields.keySet()) { |
|
path = prefix+"fields."+fieldName; |
|
|
|
JSONArray arr = config.getOrCreateArray(path); |
|
HashSet<Object> values = new HashSet<>(); |
|
arr.forEach(values::add); |
|
SelectComboBox selector = getSelector(additonalComponents.get(fieldName)); |
|
if (selector != null) selector.setElements(values); |
|
} |
|
} |
|
} |
|
|
|
private void updateField(String name, String val) { |
|
LOG.debug("updateField({},{})",name,val); |
|
fieldListeners.forEach(l -> l.setField(name,val)); |
|
} |
|
|
|
|
|
protected void updatePath(String newPath) { |
|
LOG.debug("updatePath({})",newPath); |
|
pathListeners.forEach(l -> l.setPath(newPath)); |
|
} |
|
}
|
|
|