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.
118 lines
3.0 KiB
118 lines
3.0 KiB
package de.srsoftware.belegscanner.gui; |
|
|
|
import java.awt.BorderLayout; |
|
import java.awt.Dimension; |
|
import java.awt.event.ActionEvent; |
|
import java.util.Date; |
|
import java.util.HashMap; |
|
import java.util.Map.Entry; |
|
import java.util.Vector; |
|
import java.util.regex.Matcher; |
|
import java.util.regex.Pattern; |
|
|
|
import javax.swing.JFrame; |
|
|
|
import org.slf4j.Logger; |
|
import org.slf4j.LoggerFactory; |
|
|
|
import de.srsoftware.belegscanner.Configuration; |
|
|
|
|
|
public class MainFrame extends JFrame { |
|
private static final Logger LOG = LoggerFactory.getLogger(MainFrame.class); |
|
|
|
private static final long serialVersionUID = 210283601541223645L; |
|
|
|
private static final String HOME = System.getProperty("user.home"); |
|
private static final Pattern MARKEN = Pattern.compile("\\$([a-zA-Z]+)"); |
|
|
|
private StatusBar statusBar; |
|
private Toolbar toolbar; |
|
|
|
private HashMap<String,String> fields = new HashMap<>(); |
|
|
|
private String path = ""; |
|
|
|
private String category =""; |
|
|
|
private Date date = new Date(); |
|
|
|
private String patchedPath = ""; |
|
|
|
private Configuration config; |
|
|
|
public MainFrame(Configuration config) { |
|
this.config = config; |
|
int width = config.getOrCreate("app.main.dimenstion.w",800); |
|
int height = config.getOrCreate("app.main.dimenstion.h",600); |
|
|
|
BorderLayout layout = new BorderLayout(); |
|
setLayout(layout); |
|
|
|
toolbar = new Toolbar(config); |
|
statusBar = new StatusBar(); |
|
|
|
add(toolbar,BorderLayout.EAST); |
|
add(statusBar,BorderLayout.SOUTH); |
|
toolbar.addCategoryListener(this::setCategory) |
|
.addDateListener(this::setDate) |
|
.addFieldListener(this::setField) |
|
.addPathListener(this::setPath) |
|
.addScanListener(this::scan); |
|
|
|
setPreferredSize(new Dimension(width,height)); |
|
pack(); |
|
setVisible(true); |
|
} |
|
|
|
private void addFieldsFor(String path) { |
|
Vector<String> marks = new Vector<>(); |
|
Matcher matches = MARKEN.matcher(path); |
|
while (matches.find()) marks.add(matches.group(1)); |
|
toolbar.addFieldsFor(marks); |
|
}; |
|
|
|
private void scan(ActionEvent ev) { |
|
LOG.debug("Scanning into '{}'",patchedPath); |
|
config.set("app.categories."+category+".path",path); |
|
} |
|
|
|
private void setCategory(String category) { |
|
this.category = category; |
|
updatePath(); |
|
|
|
} |
|
|
|
private void setDate(Date date) { |
|
this.date = date; |
|
updatePath(); |
|
} |
|
|
|
private void setField(String key, String val) { |
|
fields.put(key, val); |
|
updatePath(); |
|
} |
|
|
|
|
|
private void setPath(String path) { |
|
this.path = path; |
|
updatePath(); |
|
} |
|
|
|
@SuppressWarnings("deprecation") |
|
private void updatePath() { |
|
LOG.debug("updatePath() [path = {}]",path); |
|
int year = 1900+date.getYear(); |
|
int month = date.getMonth()+1; |
|
int day = date.getDate(); |
|
patchedPath = path.replace("$HOME",HOME) |
|
.replace("$KATEGORIE", category) |
|
.replace("$JAHR", year+"") |
|
.replace("$MONAT", month<10 ? "0"+month : ""+month) |
|
.replace("$TAG", day < 10 ? "0"+day : ""+day); |
|
|
|
addFieldsFor(patchedPath); |
|
for (Entry<String, String> entry : fields.entrySet()) patchedPath = patchedPath.replace("$"+entry.getKey(), entry.getValue()); |
|
statusBar.setPath(patchedPath); |
|
} |
|
}
|
|
|