Skip to content

Commit 635743e

Browse files
mplieskeDeath111
authored andcommitted
added feature: if there's a config.xml add projects to database (#7)
* added feature: if there's a config.xml add projects to database * moved configTest.xml to test/resources * fix: ConfigParser doesn't parse at start but if you go to Settings and press the button 'parse config.xml' * Linux support: on Linux systems default backgroundcolors are fully visible * configTest.xml anonymisiert * repared SettingsController.java (cancelButton)
1 parent 9fa44e3 commit 635743e

File tree

9 files changed

+359
-8
lines changed

9 files changed

+359
-8
lines changed

pom.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,22 @@
4545
<artifactId>jnativehook</artifactId>
4646
<version>2.1.0</version>
4747
</dependency>
48+
<dependency>
49+
<groupId>org.w3c</groupId>
50+
<artifactId>dom</artifactId>
51+
<version>2.3.0-jaxb-1.0.6</version>
52+
</dependency>
53+
<dependency>
54+
<groupId>org.mockito</groupId>
55+
<artifactId>mockito-core</artifactId>
56+
<version>2.21.0</version>
57+
</dependency>
58+
<dependency>
59+
<groupId>org.apache.commons</groupId>
60+
<artifactId>commons-lang3</artifactId>
61+
<version>3.8</version>
62+
</dependency>
63+
4864
</dependencies>
4965

5066
<build>

src/main/java/de/doubleslash/keeptime/Main.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public void init() throws Exception {
6161

6262
@Override
6363
public void start(final Stage primaryStage) throws Exception {
64+
6465
stage = primaryStage;
6566

6667
Log.debug("Reading configuration");
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package de.doubleslash.keeptime.common;
2+
3+
import java.io.File;
4+
5+
import javax.xml.parsers.DocumentBuilder;
6+
import javax.xml.parsers.DocumentBuilderFactory;
7+
8+
import org.w3c.dom.Document;
9+
import org.w3c.dom.Element;
10+
import org.w3c.dom.Node;
11+
import org.w3c.dom.NodeList;
12+
13+
import de.doubleslash.keeptime.controller.Controller;
14+
import de.doubleslash.keeptime.model.Model;
15+
import de.doubleslash.keeptime.model.Project;
16+
import javafx.scene.paint.Color;
17+
18+
public class ConfigParser {
19+
20+
Controller controller;
21+
Model model;
22+
23+
public ConfigParser(final Model model, final Controller controller) {
24+
this.model = model;
25+
this.controller = controller;
26+
}
27+
28+
public static boolean hasConfigFile(final String fileName) {
29+
final File f = new File(fileName);
30+
return f.exists();
31+
}
32+
33+
public void parserConfig(final File inputFile) {
34+
try {
35+
final DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
36+
final DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
37+
final Document doc = dBuilder.parse(inputFile);
38+
doc.getDocumentElement().normalize();
39+
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
40+
final NodeList nList = doc.getElementsByTagName("project");
41+
System.out.println("----------------------------");
42+
43+
// index makes sure to add new projects at the end
44+
int index = controller.getAvailableProjects().size();
45+
for (int temp = 0; temp < nList.getLength(); temp++) {
46+
final Node nNode = nList.item(temp);
47+
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
48+
final Element eElement = (Element) nNode;
49+
// get element
50+
final String name = eElement.getElementsByTagName("name").item(0).getTextContent();
51+
final String isWork = eElement.getElementsByTagName("isWork").item(0).getTextContent();
52+
final String color = eElement.getElementsByTagName("color").item(0).getTextContent();
53+
54+
System.out.println("checking for: " + name);
55+
boolean exists = false;
56+
57+
for (final Project p : controller.getAvailableProjects()) {
58+
if (name.equals(p.getName())) {
59+
System.out.println(name + " " + Boolean.toString(exists));
60+
exists = true;
61+
}
62+
}
63+
if (!exists) {
64+
final Color colorTemp = Color.valueOf(color);
65+
controller.addNewProject(name, Boolean.parseBoolean(isWork), colorTemp, index);
66+
index++;
67+
}
68+
69+
}
70+
}
71+
72+
} catch (final Exception e) {
73+
e.printStackTrace();
74+
}
75+
}
76+
}

src/main/java/de/doubleslash/keeptime/controller/Controller.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import de.doubleslash.keeptime.model.Project;
1919
import de.doubleslash.keeptime.model.Settings;
2020
import de.doubleslash.keeptime.model.Work;
21+
import javafx.collections.ObservableList;
2122
import javafx.scene.paint.Color;
2223

2324
@Service
@@ -149,13 +150,13 @@ public void editProject(final Project p, final String newName, final Color newCo
149150
* Changes the indexes of the originalList parameter to have a consistent order.
150151
*
151152
* @param originalList
152-
* list of all projects to adapt the indexes for
153+
* list of all projects to adapt the indexes for
153154
* @param changedProject
154-
* the project which has changed which already has the new index
155+
* the project which has changed which already has the new index
155156
* @param oldIndex
156-
* the old index of the changed project
157+
* the old index of the changed project
157158
* @param newIndex
158-
* the new index of the changed project (which the projects also already has)
159+
* the new index of the changed project (which the projects also already has)
159160
* @return all projects whose index has been adapted
160161
*/
161162
List<Project> resortProjectIndexes(final List<Project> originalList, final Project changedProject,
@@ -197,9 +198,9 @@ List<Project> resortProjectIndexes(final List<Project> originalList, final Proje
197198
* Decreases all indexes by one, after the removed index
198199
*
199200
* @param originalList
200-
* list of all projects to adapt the indexes for
201+
* list of all projects to adapt the indexes for
201202
* @param removedIndex
202-
* the index which has been removed
203+
* the index which has been removed
203204
* @return all projects whose index has been adapted
204205
*/
205206
List<Project> adaptProjectIndexesAfterRemoving(final List<Project> originalList, final int removedIndex) {
@@ -255,4 +256,7 @@ public long calcTodaysSeconds() {
255256
}).sum();
256257
}
257258

259+
public ObservableList<Project> getAvailableProjects() {
260+
return this.model.availableProjects;
261+
}
258262
}

src/main/java/de/doubleslash/keeptime/model/Model.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.Comparator;
44

5+
import org.apache.commons.lang3.SystemUtils;
56
import org.springframework.stereotype.Component;
67

78
import javafx.beans.property.ObjectProperty;
@@ -13,9 +14,13 @@
1314

1415
@Component
1516
public class Model {
16-
public static final Color originalHoverBackgroundColor = new Color(54 / 255., 143 / 255., 179 / 255., .1);
17+
public static final Color originalHoverBackgroundColor = (SystemUtils.IS_OS_LINUX)
18+
? new Color(54 / 255., 143 / 255., 179 / 255., 1.)
19+
: new Color(54 / 255., 143 / 255., 179 / 255., 0.01);
1720
public static final Color originalHoverFontColor = Color.BLACK;
18-
public static final Color originalDefaultBackgroundColor = new Color(54 / 255., 143 / 255., 179 / 255., 0.01);
21+
public static final Color originalDefaultBackgroundColor = (SystemUtils.IS_OS_LINUX)
22+
? new Color(54 / 255., 143 / 255., 179 / 255., 1.)
23+
: new Color(54 / 255., 143 / 255., 179 / 255., .1);
1924
public static final Color originalDefaultFontColor = Color.BLACK;
2025

2126
public static final Color originalTaskBarFontColor = Color.BLACK;

src/main/java/de/doubleslash/keeptime/view/SettingsController.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package de.doubleslash.keeptime.view;
22

3+
import java.io.File;
4+
35
import org.slf4j.Logger;
46
import org.slf4j.LoggerFactory;
57

8+
import de.doubleslash.keeptime.common.ConfigParser;
69
import de.doubleslash.keeptime.controller.Controller;
710
import de.doubleslash.keeptime.model.Model;
11+
import javafx.event.ActionEvent;
12+
import javafx.event.EventHandler;
813
import javafx.fxml.FXML;
914
import javafx.scene.control.Button;
1015
import javafx.scene.control.CheckBox;
@@ -47,11 +52,15 @@ public class SettingsController {
4752
@FXML
4853
private Button cancelButton;
4954

55+
@FXML
56+
private Button parseConfigButton;
57+
5058
private final Logger Log = LoggerFactory.getLogger(this.getClass());
5159

5260
private Model model;
5361
private Controller controller;
5462
private Stage thisStage;
63+
private static final String INPUT_FILE = "config.xml";
5564

5665
@FXML
5766
private void initialize() {
@@ -85,6 +94,16 @@ private void initialize() {
8594
taskBarColor.setValue(Model.originalTaskBarFontColor);
8695
});
8796

97+
parseConfigButton.setOnAction(new EventHandler<ActionEvent>() {
98+
@Override
99+
public void handle(final ActionEvent actionEvent) {
100+
if (ConfigParser.hasConfigFile(INPUT_FILE)) {
101+
final ConfigParser parser = new ConfigParser(model, controller);
102+
parser.parserConfig(new File(INPUT_FILE));
103+
}
104+
}
105+
});
106+
88107
}
89108

90109
public void setModelAndController(final Model model, final Controller controller) {

src/main/resources/settings.fxml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
<children>
119119
<Button fx:id="saveButton" mnemonicParsing="false" text="Save" />
120120
<Button fx:id="cancelButton" layoutX="10.0" layoutY="10.0" mnemonicParsing="false" text="Cancel" />
121+
<Button fx:id="parseConfigButton" mnemonicParsing="false" text="pars config.xml" />
121122
</children>
122123
</HBox>
123124
</children>

0 commit comments

Comments
 (0)