-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeatherAppGUI.java
More file actions
324 lines (294 loc) · 14.7 KB
/
WeatherAppGUI.java
File metadata and controls
324 lines (294 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URI;
import java.net.URLEncoder;
import java.net.http.*;
import java.nio.charset.StandardCharsets;
import java.util.logging.*;
import java.io.IOException;
import com.google.gson.*;
import com.formdev.flatlaf.*;
public class WeatherAppGUI extends JFrame {
private JTextField cityField, latField, lonField;
private JTextArea resultArea;
private JButton btnConsultarCity, btnConsultarCoord, btnToggleTheme;
private JComboBox<String> unitsCombo;
private static final String API_KEY = "9baeee197ed91d5ebd1309a425f13eb5";
private static final Logger logger = Logger.getLogger(WeatherAppGUI.class.getName());
private boolean isDarkTheme = true;
public WeatherAppGUI() {
super("Previsão do Tempo - Java GUI Moderno");
setupLogging();
setupTheme();
initializeComponents();
setupLayout();
setupEventListeners();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);
logger.info("Aplicação iniciada com sucesso");
}
private void setupLogging() {
try {
ConsoleHandler consoleHandler = new ConsoleHandler();
consoleHandler.setLevel(Level.ALL);
consoleHandler.setFormatter(new SimpleFormatter());
FileHandler fileHandler = new FileHandler("weather_app.log", true);
fileHandler.setLevel(Level.ALL);
fileHandler.setFormatter(new SimpleFormatter());
logger.addHandler(consoleHandler);
logger.addHandler(fileHandler);
logger.setLevel(Level.ALL);
} catch (IOException e) {
System.err.println("Erro ao configurar logging: " + e.getMessage());
}
}
private void setupTheme() {
try {
if (isDarkTheme) {
UIManager.setLookAndFeel(new FlatDarkLaf());
} else {
UIManager.setLookAndFeel(new FlatLightLaf());
}
UIManager.put("Button.arc", 10);
UIManager.put("Component.focusWidth", 1);
UIManager.put("TextComponent.arc", 8);
} catch (UnsupportedLookAndFeelException e) {
logger.warning("Erro ao aplicar tema FlatLaf, usando tema padrão: " + e.getMessage());
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
logger.severe("Erro crítico ao configurar tema: " + ex.getMessage());
}
}
}
private void initializeComponents() {
cityField = new JTextField(15);
latField = new JTextField(8);
lonField = new JTextField(8);
btnConsultarCity = new JButton("🏙️ Consultar Cidade");
btnConsultarCoord = new JButton("🌍 Consultar Coordenadas");
btnToggleTheme = new JButton(isDarkTheme ? "☀️ Tema Claro" : "🌙 Tema Escuro");
String[] units = {"Celsius (°C)", "Fahrenheit (°F)", "Kelvin (K)"};
unitsCombo = new JComboBox<>(units);
resultArea = new JTextArea(25, 50);
resultArea.setEditable(false);
resultArea.setFont(new Font("Consolas", Font.PLAIN, 12));
}
private void setupLayout() {
JScrollPane scrollPane = new JScrollPane(resultArea);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JPanel cityPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
cityPanel.setBorder(BorderFactory.createTitledBorder("Consultar por Cidade"));
cityPanel.add(new JLabel("Cidade:"));
cityPanel.add(cityField);
cityPanel.add(btnConsultarCity);
JPanel coordPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
coordPanel.setBorder(BorderFactory.createTitledBorder("Consultar por Coordenadas"));
coordPanel.add(new JLabel("Latitude:"));
coordPanel.add(latField);
coordPanel.add(new JLabel("Longitude:"));
coordPanel.add(lonField);
coordPanel.add(btnConsultarCoord);
JPanel configPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
configPanel.setBorder(BorderFactory.createTitledBorder("Configurações"));
configPanel.add(new JLabel("Unidades:"));
configPanel.add(unitsCombo);
configPanel.add(btnToggleTheme);
JPanel inputPanel = new JPanel(new GridLayout(3, 1, 5, 5));
inputPanel.add(cityPanel);
inputPanel.add(coordPanel);
inputPanel.add(configPanel);
add(inputPanel, BorderLayout.NORTH);
add(scrollPane, BorderLayout.CENTER);
}
private void setupEventListeners() {
btnConsultarCity.addActionListener(_ignored -> {
String city = cityField.getText().trim();
if (city.isEmpty()) {
showError("Digite o nome da cidade.");
logger.warning("Tentativa de consulta com campo cidade vazio");
return;
}
consultarPorCidade(city);
});
btnConsultarCoord.addActionListener(_ignored -> {
try {
double lat = Double.parseDouble(latField.getText().trim());
double lon = Double.parseDouble(lonField.getText().trim());
if (lat < -90 || lat > 90 || lon < -180 || lon > 180) {
showError("Coordenadas inválidas! Latitude: -90 a 90, Longitude: -180 a 180");
return;
}
consultarPorCoordenadas(lat, lon);
} catch (NumberFormatException ex) {
showError("Formato de coordenadas inválido!");
logger.warning("Formato de coordenadas inválido: " + ex.getMessage());
}
});
btnToggleTheme.addActionListener(_ignored -> toggleTheme());
cityField.addActionListener(_ignored -> btnConsultarCity.doClick());
}
private void toggleTheme() {
isDarkTheme = !isDarkTheme;
setupTheme();
SwingUtilities.updateComponentTreeUI(this);
btnToggleTheme.setText(isDarkTheme ? "☀️ Tema Claro" : "🌙 Tema Escuro");
logger.info("Tema alterado para: " + (isDarkTheme ? "Escuro" : "Claro"));
}
private String getUnitsParam() {
switch (unitsCombo.getSelectedIndex()) {
case 1: return "imperial";
case 2: return "standard";
default: return "metric";
}
}
private String getUnitsSymbol() {
switch (unitsCombo.getSelectedIndex()) {
case 1: return "°F";
case 2: return "K";
default: return "°C";
}
}
private void consultarPorCidade(String city) {
resultArea.setText("🔍 Consultando dados para: " + city + "...\n");
logger.info("Iniciando consulta por cidade: " + city);
new SwingWorker<Void, Void>() {
protected Void doInBackground() {
try {
HttpClient client = HttpClient.newHttpClient();
Gson gson = new Gson();
String encodedCity = URLEncoder.encode(city, StandardCharsets.UTF_8);
String unitsParam = getUnitsParam();
URI uriCurrent = new URI("https", "api.openweathermap.org", "/data/2.5/weather",
"q=" + encodedCity + "&units=" + unitsParam + "&lang=pt_br&appid=" + API_KEY, null);
processarConsulta(uriCurrent, client, gson);
logger.info("Consulta por cidade concluída com sucesso: " + city);
} catch (Exception ex) {
handleException("Erro na consulta por cidade", ex);
}
return null;
}
}.execute();
}
private void consultarPorCoordenadas(double lat, double lon) {
resultArea.setText(String.format("🌐 Consultando dados para: lat=%.4f, lon=%.4f ...\n", lat, lon));
logger.info(String.format("Iniciando consulta por coordenadas: lat=%.4f, lon=%.4f", lat, lon));
new SwingWorker<Void, Void>() {
protected Void doInBackground() {
try {
HttpClient client = HttpClient.newHttpClient();
Gson gson = new Gson();
String unitsParam = getUnitsParam();
URI uriCurrent = new URI("https", "api.openweathermap.org", "/data/2.5/weather",
"lat=" + lat + "&lon=" + lon + "&units=" + unitsParam + "&lang=pt_br&appid=" + API_KEY, null);
processarConsulta(uriCurrent, client, gson);
logger.info("Consulta por coordenadas concluída com sucesso");
} catch (Exception ex) {
handleException("Erro na consulta por coordenadas", ex);
}
return null;
}
}.execute();
}
private void processarConsulta(URI uriCurrent, HttpClient client, Gson gson) {
try {
HttpRequest reqCurrent = HttpRequest.newBuilder()
.uri(uriCurrent)
.timeout(java.time.Duration.ofSeconds(10))
.build();
HttpResponse<String> respCurrent = client.send(reqCurrent, HttpResponse.BodyHandlers.ofString());
if (respCurrent.statusCode() != 200) {
SwingUtilities.invokeLater(() -> resultArea.append(
"❌ Erro HTTP " + respCurrent.statusCode() + ": " + respCurrent.body() + "\n"));
logger.warning("Erro HTTP: " + respCurrent.statusCode());
return;
}
JsonObject currentJson = gson.fromJson(respCurrent.body(), JsonObject.class);
String unitsSymbol = getUnitsSymbol();
SwingUtilities.invokeLater(() -> {
String cityName = currentJson.get("name").getAsString();
JsonObject main = currentJson.getAsJsonObject("main");
double temp = main.get("temp").getAsDouble();
int humidity = main.get("humidity").getAsInt();
String description = currentJson.getAsJsonArray("weather")
.get(0).getAsJsonObject().get("description").getAsString();
JsonObject sys = currentJson.getAsJsonObject("sys");
long sunrise = sys.get("sunrise").getAsLong() * 1000;
long sunset = sys.get("sunset").getAsLong() * 1000;
resultArea.append("🏙️ " + cityName + "\n");
resultArea.append("══════════════════════════════\n");
resultArea.append(String.format("🌡️ Temperatura: %.1f%s\n", temp, unitsSymbol));
resultArea.append("📝 Descrição: " + description + "\n");
resultArea.append("💧 Umidade: " + humidity + "%\n");
resultArea.append("🌅 Nascer do sol: " + new java.util.Date(sunrise) + "\n");
resultArea.append("🌇 Pôr do sol: " + new java.util.Date(sunset) + "\n");
JsonObject coord = currentJson.getAsJsonObject("coord");
double lat = coord.get("lat").getAsDouble();
double lon = coord.get("lon").getAsDouble();
buscarPrevisaoEstendida(lat, lon, client, gson);
});
} catch (Exception ex) {
handleException("Erro ao processar consulta", ex);
}
}
private void buscarPrevisaoEstendida(double lat, double lon, HttpClient client, Gson gson) {
try {
String unitsParam = getUnitsParam(), unitsSymbol = getUnitsSymbol();
URI uriOneCall = new URI("https", "api.openweathermap.org", "/data/2.5/onecall",
"lat=" + lat + "&lon=" + lon + "&exclude=minutely,alerts&units=" + unitsParam
+ "&lang=pt_br&appid=" + API_KEY,
null);
HttpRequest reqOneCall = HttpRequest.newBuilder()
.uri(uriOneCall)
.timeout(java.time.Duration.ofSeconds(10))
.build();
HttpResponse<String> respOneCall = client.send(reqOneCall, HttpResponse.BodyHandlers.ofString());
if (respOneCall.statusCode() == 200) {
JsonObject oneCallJson = gson.fromJson(respOneCall.body(), JsonObject.class);
JsonArray dailyArray = oneCallJson.getAsJsonArray("daily");
SwingUtilities.invokeLater(() -> {
resultArea.append("\n📅 PREVISÃO PARA OS PRÓXIMOS 7 DIAS:\n");
resultArea.append("══════════════════════════════\n");
for (int i = 0; i < Math.min(7, dailyArray.size()); i++) {
JsonObject day = dailyArray.get(i).getAsJsonObject();
long dt = day.get("dt").getAsLong() * 1000;
JsonObject temp = day.getAsJsonObject("temp");
double max = temp.get("max").getAsDouble();
double min = temp.get("min").getAsDouble();
String desc = day.getAsJsonArray("weather")
.get(0).getAsJsonObject().get("description").getAsString();
int humidity = day.get("humidity").getAsInt();
java.util.Date date = new java.util.Date(dt);
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("dd/MM - EEE");
resultArea.append(String.format("%s: %.1f%s/%.1f%s - %s (💧%d%%)\n",
sdf.format(date), max, unitsSymbol, min, unitsSymbol, desc, humidity));
}
resultArea.append("\n");
});
logger.info("Previsão estendida obtida com sucesso");
} else {
logger.warning("Erro ao obter previsão estendida: " + respOneCall.statusCode());
}
} catch (Exception ex) {
logger.warning("Erro na previsão estendida: " + ex.getMessage());
}
}
private void handleException(String message, Exception ex) {
logger.severe(message + ": " + ex.getMessage());
SwingUtilities.invokeLater(() -> resultArea.append("❌ " + message + ": " + ex.getMessage() + "\n"));
}
private void showError(String message) {
JOptionPane.showMessageDialog(this, message, "Erro", JOptionPane.ERROR_MESSAGE);
}
public static void main(String[] args) {
try {
FlatDarkLaf.setup();
} catch (Exception ex) {
System.err.println("Erro ao configurar FlatLaf: " + ex.getMessage());
}
SwingUtilities.invokeLater(() -> new WeatherAppGUI());
}
}