Skip to content

Commit 69f38c4

Browse files
authored
Merge pull request #22 from neontribe/dev/move-settings-to-top-object
Dev/move settings to top object
2 parents 7023d72 + 74a3ed0 commit 69f38c4

File tree

15 files changed

+314
-269
lines changed

15 files changed

+314
-269
lines changed

src/main/java/uk/co/neontribe/kimai/Main.java

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ public static void main(String[] args) throws IOException {
3131
// UIManager.setLookAndFeel ("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
3232
frame = new KimaiUiFrame();
3333
frame.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
34-
frame.setSize(800, 600);
34+
// frame.setSize(800, 600);
35+
frame.pack();
3536
frame.setVisible(true);
3637

3738
if (!SystemTray.isSupported()) {
@@ -41,21 +42,21 @@ public static void main(String[] args) throws IOException {
4142

4243
SystemTray tray = SystemTray.getSystemTray();
4344
PopupMenu menu = new PopupMenu();
44-
MenuItem messageItem = new MenuItem("Open Kimai");
45-
messageItem.addActionListener(new ActionListener() {
46-
public void actionPerformed(ActionEvent e) {
47-
try {
48-
Settings settings = Settings.getInstance();
49-
Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
50-
if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
51-
desktop.browse(new URL(settings.getKimaiUri()).toURI());
52-
}
53-
} catch (IOException | URISyntaxException ex) {
54-
throw new RuntimeException(ex);
55-
}
56-
}
57-
});
58-
menu.add(messageItem);
45+
// MenuItem messageItem = new MenuItem("Open Kimai");
46+
// messageItem.addActionListener(new ActionListener() {
47+
// public void actionPerformed(ActionEvent e) {
48+
// try {
49+
// Settings settings = Settings.getInstance();
50+
// Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
51+
// if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
52+
// desktop.browse(new URL(settings.getKimaiUri()).toURI());
53+
// }
54+
// } catch (IOException | URISyntaxException ex) {
55+
// throw new RuntimeException(ex);
56+
// }
57+
// }
58+
// });
59+
// menu.add(messageItem);
5960
MenuItem closeItem = new MenuItem("Close");
6061
closeItem.addActionListener(new ActionListener() {
6162
public void actionPerformed(ActionEvent e) {
@@ -81,4 +82,13 @@ public void actionPerformed(ActionEvent actionEvent) {
8182
e.printStackTrace();
8283
}
8384
}
85+
86+
public static void changeFont(Component component, Font font) {
87+
component.setFont(font);
88+
if (component instanceof Container) {
89+
for (Component child : ((Container) component).getComponents()) {
90+
changeFont(child, font);
91+
}
92+
}
93+
}
8494
}

src/main/java/uk/co/neontribe/kimai/api/Activity.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import uk.co.neontribe.kimai.config.Settings;
88

99
import java.io.IOException;
10+
import java.net.MalformedURLException;
1011
import java.net.URL;
1112
import java.util.List;
1213

@@ -20,10 +21,9 @@ public int getProject() {
2021
return project;
2122
}
2223

23-
public static Activity[] getActivities(int id) throws ConfigNotInitialisedException, IOException {
24-
Settings settings = Settings.getInstance();
24+
public static Activity[] getActivities(int id, Settings settings) throws IOException {
2525
URL url = new URL(settings.getKimaiUri() + "/api/activities");
26-
String content = Entity.getApi(url);
26+
String content = Entity.getApi(url, settings);
2727
Gson gson = new Gson();
2828
TypeToken<List<Activity>> activityType = new TypeToken<List<Activity>>() {
2929
};

src/main/java/uk/co/neontribe/kimai/api/Customer.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@
1313
@AllArgsConstructor
1414
public class Customer extends Entity {
1515

16-
public static Customer[] getCustomers() throws ConfigNotInitialisedException, IOException {
17-
Settings settings = Settings.getInstance();
16+
public static Customer[] getCustomers(Settings settings) throws IOException {
1817
URL url = new URL(settings.getKimaiUri() + "/api/customers");
19-
String content = Entity.getApi(url);
18+
String content = Entity.getApi(url, settings);
2019
Gson gson = new Gson();
2120
TypeToken<List<Customer>> customerType = new TypeToken<List<Customer>>() {
2221
};

src/main/java/uk/co/neontribe/kimai/api/Entity.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ public int getId() {
2626
return id;
2727
}
2828

29-
public static HttpURLConnection makeHttpConnection(URL url) throws IOException {
30-
Settings settings = Settings.getInstance();
29+
public static HttpURLConnection makeHttpConnection(URL url, Settings settings) throws IOException {
30+
System.out.println("makeHttpConnection: " + settings.getKimaiUsername() + ", " + settings.getKimaiPassword() + ", " + ", " + settings.getKimaiUri());
3131
HttpURLConnection con = (HttpURLConnection) url.openConnection();
3232
con.setRequestProperty("X-AUTH-USER", settings.getKimaiUsername());
3333
con.setRequestProperty("X-AUTH-TOKEN", settings.getKimaiPassword());
@@ -36,25 +36,25 @@ public static HttpURLConnection makeHttpConnection(URL url) throws IOException {
3636
return con;
3737
}
3838

39-
public static String getApi(URL url) throws ConfigNotInitialisedException, IOException {
40-
return Entity.getApi(url, null);
39+
public static String getApi(URL url, Settings settings) throws IOException {
40+
return Entity.getApi(url, settings, null);
4141
}
4242

43-
public static String getApi(URL url, List<Map.Entry<String, String>> parameters) throws ConfigNotInitialisedException, IOException {
43+
public static String getApi(URL url, Settings settings, List<Map.Entry<String, String>> parameters) throws IOException {
4444
if (parameters != null && parameters.size() > 0) {
4545
StringBuilder query = new StringBuilder();
4646
for (Map.Entry<String, String> parameter : parameters) {
4747
query.append(String.format("%s=%s&", URLEncoder.encode(parameter.getKey(), "UTF-8"), URLEncoder.encode(parameter.getValue(), "UTF-8")));
4848
}
4949
url = new URL(url + "?" + query);
5050
}
51-
HttpURLConnection con = makeHttpConnection(url);
51+
HttpURLConnection con = makeHttpConnection(url, settings);
5252

5353
return callApi(con);
5454
}
5555

56-
public static String postApi(URL url, String body) throws ConfigNotInitialisedException, IOException {
57-
HttpURLConnection con = makeHttpConnection(url);
56+
public static String postApi(URL url, Settings settings, String body) throws IOException {
57+
HttpURLConnection con = makeHttpConnection(url, settings);
5858
con.setDoOutput(true);
5959
con.setRequestProperty("Content-Type", "application/json");
6060
OutputStream os = con.getOutputStream();

src/main/java/uk/co/neontribe/kimai/api/Project.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ public int getCustomer() {
2020
return customer;
2121
}
2222

23-
public static Project[] getProjects(int id) throws ConfigNotInitialisedException, IOException {
24-
Settings settings = Settings.getInstance();
23+
public static Project[] getProjects(int id, Settings settings) throws IOException {
2524
URL url = new URL(settings.getKimaiUri() + "/api/projects");
26-
String content = Entity.getApi(url);
25+
String content = Entity.getApi(url, settings);
2726
Gson gson = new Gson();
2827
TypeToken<List<Project>> projectType = new TypeToken<List<Project>>() {
2928
};

src/main/java/uk/co/neontribe/kimai/api/TimeSheet.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,12 @@ public int getUser() {
4949
return user;
5050
}
5151

52-
public static String postTimeSheet(TimeSheet timeSheet) throws IOException {
53-
Settings settings = Settings.getInstance();
52+
public static String postTimeSheet(Settings settings, TimeSheet timeSheet) throws IOException {
5453
URL url = new URL(settings.getKimaiUri() + "/api/timesheets");
5554

56-
TimeSheet[] timesheets = getTimeSheets(timeSheet.project, timeSheet.activity, timeSheet.begin, timeSheet.end);
55+
TimeSheet[] timesheets = getTimeSheets(settings, timeSheet.project, timeSheet.activity, timeSheet.begin, timeSheet.end);
5756
if (timesheets.length > 0) {
58-
DuplicateEntryModal duplicateEntryModal = new DuplicateEntryModal();
57+
DuplicateEntryModal duplicateEntryModal = new DuplicateEntryModal(settings);
5958
duplicateEntryModal.setVisible(true);
6059
if (!duplicateEntryModal.getShouldProceed()) {
6160
return null;
@@ -69,11 +68,10 @@ public static String postTimeSheet(TimeSheet timeSheet) throws IOException {
6968
Gson gson = builder.create();
7069
String postContent = gson.toJson(timeSheetDto);
7170

72-
return postApi(url, postContent);
71+
return postApi(url, settings, postContent);
7372
}
7473

75-
public static TimeSheet[] getTimeSheets(int projectId, int activityId, Date begin, Date end) throws IOException {
76-
Settings settings = Settings.getInstance();
74+
public static TimeSheet[] getTimeSheets(Settings settings, int projectId, int activityId, Date begin, Date end) throws IOException {
7775
URL url = new URL(settings.getKimaiUri() + "/api/timesheets");
7876

7977
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_FORMAT);
@@ -85,7 +83,7 @@ public static TimeSheet[] getTimeSheets(int projectId, int activityId, Date begi
8583
parameters.add(new AbstractMap.SimpleEntry<>("begin", simpleDateFormat.format(begin)));
8684
parameters.add(new AbstractMap.SimpleEntry<>("end", simpleDateFormat.format(end)));
8785

88-
String content = Entity.getApi(url, parameters);
86+
String content = Entity.getApi(url, settings, parameters);
8987
Gson gson = new Gson();
9088
TypeToken<List<TimeSheet>> projectType = new TypeToken<List<TimeSheet>>() {
9189
};

src/main/java/uk/co/neontribe/kimai/api/User.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@
1111
@AllArgsConstructor
1212
public class User extends Entity {
1313

14-
public static User getCurrentUser() throws IOException {
15-
Settings settings = Settings.getInstance();
14+
public static User getCurrentUser(Settings settings) throws IOException {
1615
URL url = new URL(settings.getKimaiUri() + "/api/users/me");
17-
String content = Entity.getApi(url);
16+
String content = Entity.getApi(url, settings);
1817
Gson gson = new Gson();
1918
TypeToken<User> userType = new TypeToken<User>() {
2019
};

src/main/java/uk/co/neontribe/kimai/config/ConfigNotInitialisedException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package uk.co.neontribe.kimai.config;
22

3-
public class ConfigNotInitialisedException extends RuntimeException {
3+
public class ConfigNotInitialisedException extends Exception {
44
public ConfigNotInitialisedException(String configNotInitialised) {
55
super(configNotInitialised);
66
}

0 commit comments

Comments
 (0)