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.
195 lines
5.4 KiB
195 lines
5.4 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.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 Vector<Object> categories = new Vector<>(); |
|
private Vector<Object> paths = new Vector<>(); |
|
|
|
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> scanListeners = new HashSet<>(); |
|
private HashMap<String,Component> additonalComponents = new HashMap<>(); |
|
|
|
private DateChooser date; |
|
|
|
private Configuration config; |
|
|
|
private SelectComboBox pathPicker; |
|
|
|
|
|
|
|
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(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; |
|
List<Object> knownValues = List.of(); |
|
SelectComboBox valuePicker = new SelectComboBox(knownValues).onUpdateText(newText -> updateField(name,newText)); |
|
Component input = input(name, valuePicker); |
|
add(input,getComponentCount()-2); |
|
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 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 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; |
|
} |
|
|
|
|
|
public Date getDate() { |
|
return date.getSelectedDate(); |
|
} |
|
|
|
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() { |
|
JButton scanButton = new JButton("scannen!"); |
|
scanButton.addActionListener(this::scanPressed); |
|
return scanButton; |
|
} |
|
|
|
private void scanPressed(ActionEvent evt) { |
|
scanListeners.forEach(l->l.actionPerformed(evt)); |
|
} |
|
|
|
protected void updateCat(String newCat) { |
|
LOG.debug("updateCat({})",newCat); |
|
categoryListeners.forEach(l -> l.setCategory(newCat)); |
|
if (!newCat.isEmpty()) { |
|
String path = config.get("app.categories."+newCat+".path"); |
|
LOG.debug("path: {}",path); |
|
if (path != null) { |
|
pathPicker.setSelectedItem(path); |
|
updatePath(path); |
|
} |
|
} |
|
} |
|
|
|
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)); |
|
} |
|
}
|
|
|