- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestDialogFallback.java
More file actions
Latest commit
315 lines (275 loc) · 14.2 KB
/
Copy pathRequestDialogFallback.java
File metadata and controls
315 lines (275 loc) · 14.2 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
/*
* RequestDialogFallback.java — Manual place-ID request dialog.
*
* Used when JavaFX WebView is not available (module path missing, headless env, etc.).
* Opens roblox.com/charts in the system browser, then the child copies the numeric
* place ID from the URL bar and pastes it here to look up and request the game.
*
* Flow:
* 1. System browser opens automatically to Roblox charts.
* 2. Child finds a game, copies the ID from the URL.
* 3. Child pastes ID → clicks "Look Up" → name + thumbnail appear.
* 4. Child adds an optional note → "Send Request" saves to the requests file.
*
* Mirrors Python RequestDialogFallback(tk.Toplevel).
*/
importjava.awt.Color;
importjava.awt.Component;
importjava.awt.Dimension;
importjava.awt.FlowLayout;
importjava.awt.Image;
importjava.awt.image.BufferedImage;
importjava.io.ByteArrayInputStream;
importjava.net.URI;
importjava.net.http.HttpRequest;
importjava.net.http.HttpResponse;
importjava.time.Duration;
importjava.util.concurrent.CompletableFuture;
importjavax.imageio.ImageIO;
importjavax.swing.BorderFactory;
importjavax.swing.Box;
importjavax.swing.BoxLayout;
importjavax.swing.ImageIcon;
importjavax.swing.JButton;
importjavax.swing.JDialog;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JOptionPane;
importjavax.swing.JPanel;
importjavax.swing.JTextField;
importjavax.swing.SwingConstants;
importjavax.swing.SwingUtilities;
publicclassRequestDialogFallbackextendsJDialog {
// ── State — set after a successful lookup ─────────────────────────────────
privateStringfetchedPlaceId = null; // Validated, digit-only place ID
privateStringfetchedGameName = null; // Display name from Roblox API
// ── UI components ─────────────────────────────────────────────────────────
privateJTextFieldidEntry; // Child types or pastes the place ID here
privateJLabelthumbLabel; // Thumbnail preview (spinner → image)
privateJLabelnameLabel; // Game display name
privateJLabelstatusLabel; // "Looking up…" / "✅ Found!" / error messages
privateJTextFieldnoteEntry; // Optional note from the child
privateJButtonsubmitBtn; // Disabled until lookup succeeds
// ── Constructor ───────────────────────────────────────────────────────────
RequestDialogFallback(JFrameparent) {
super(parent, "Request a Game", true); // true = modal
setResizable(false);
getContentPane().setBackground(Start.BG_COLOR);
buildUI();
pack();
setLocationRelativeTo(parent);
}
// ── UI construction ───────────────────────────────────────────────────────
privatevoidbuildUI() {
JPanelpanel = newJPanel();
panel.setLayout(newBoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBackground(Start.BG_COLOR);
panel.setBorder(BorderFactory.createEmptyBorder(20, 24, 20, 24));
// ── Title ─────────────────────────────────────────────────────────────
JLabeltitle = newJLabel("Request a New Game");
title.setFont(newjava.awt.Font("Georgia", java.awt.Font.BOLD, 16));
title.setForeground(Start.TEXT_COLOR);
title.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(title);
panel.add(Box.createVerticalStrut(4));
// ── Instructions — tell child how to find the place ID ────────────────
JLabelinstructions = newJLabel(
"<html><div style='text-align:center'>" +
"Find a game in the browser that just opened.<br>" +
"Copy the number from the URL bar:<br>" +
"<span style='color:#888888'>roblox.com/games/<b>185655149</b>/...</span>" +
"</div></html>",
SwingConstants.CENTER
);
instructions.setFont(Start.FONT_SMALL);
instructions.setForeground(Start.SUBTEXT_COLOR);
instructions.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(instructions);
panel.add(Box.createVerticalStrut(10));
// ── Place ID entry + Look Up button ───────────────────────────────────
JPanelentryRow = newJPanel(newFlowLayout(FlowLayout.LEFT, 0, 0));
entryRow.setBackground(Start.BG_COLOR);
idEntry = newJTextField(20);
idEntry.setFont(Start.FONT_SMALL);
idEntry.setBackground(Color.decode("#252540"));
idEntry.setForeground(Start.TEXT_COLOR);
idEntry.setCaretColor(Start.TEXT_COLOR);
idEntry.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6));
// Enter key triggers lookup — convenience for keyboard users
idEntry.addActionListener(e -> onLookup());
JButtonlookupBtn = Start.makeButton("Look Up", Start.REQUEST_COLOR);
lookupBtn.addActionListener(e -> onLookup());
entryRow.add(idEntry);
entryRow.add(Box.createHorizontalStrut(8));
entryRow.add(lookupBtn);
panel.add(entryRow);
panel.add(Box.createVerticalStrut(10));
// ── Thumbnail preview — empty until lookup succeeds ───────────────────
thumbLabel = newJLabel("", SwingConstants.CENTER);
thumbLabel.setFont(newjava.awt.Font("Segoe UI Emoji", java.awt.Font.PLAIN, 28));
thumbLabel.setForeground(Start.SUBTEXT_COLOR);
thumbLabel.setBackground(Start.BG_COLOR);
thumbLabel.setOpaque(true);
thumbLabel.setPreferredSize(newDimension(100, 100));
thumbLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(thumbLabel);
panel.add(Box.createVerticalStrut(4));
// ── Game name from API ────────────────────────────────────────────────
nameLabel = newJLabel("", SwingConstants.CENTER);
nameLabel.setFont(Start.FONT_CARD);
nameLabel.setForeground(Start.TEXT_COLOR);
nameLabel.setBackground(Start.BG_COLOR);
nameLabel.setOpaque(true);
nameLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(nameLabel);
// ── Status feedback label ─────────────────────────────────────────────
statusLabel = newJLabel("", SwingConstants.CENTER);
statusLabel.setFont(Start.FONT_SMALL);
statusLabel.setForeground(Start.SUBTEXT_COLOR);
statusLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(statusLabel);
panel.add(Box.createVerticalStrut(10));
// ── Optional note ─────────────────────────────────────────────────────
JLabelnoteLbl = newJLabel("Why do you want to play it? (optional):");
noteLbl.setFont(Start.FONT_SMALL);
noteLbl.setForeground(Start.TEXT_COLOR);
panel.add(noteLbl);
panel.add(Box.createVerticalStrut(4));
noteEntry = newJTextField(30);
noteEntry.setFont(Start.FONT_SMALL);
noteEntry.setBackground(Color.decode("#252540"));
noteEntry.setForeground(Start.TEXT_COLOR);
noteEntry.setCaretColor(Start.TEXT_COLOR);
noteEntry.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6));
panel.add(noteEntry);
panel.add(Box.createVerticalStrut(16));
// ── Buttons — Submit disabled until a valid lookup has succeeded ──────
JPanelbtnRow = newJPanel(newFlowLayout(FlowLayout.CENTER, 8, 0));
btnRow.setBackground(Start.BG_COLOR);
// Start disabled + grey — enabled with blue once game is found
submitBtn = Start.makeButton("Send Request", Color.decode("#444444"));
submitBtn.setForeground(Color.decode("#888888"));
submitBtn.setEnabled(false);
submitBtn.addActionListener(e -> onSubmit());
JButtoncancelBtn = Start.makeButton("Cancel", Color.decode("#333333"));
cancelBtn.addActionListener(e -> dispose());
btnRow.add(submitBtn);
btnRow.add(cancelBtn);
panel.add(btnRow);
add(panel);
}
// ── Lookup flow ───────────────────────────────────────────────────────────
/**
* Validate the typed place ID and kick off async name + thumbnail lookup.
* Shows a spinner state while the background thread runs.
*/
privatevoidonLookup() {
StringplaceId = idEntry.getText().strip();
// Place IDs are always numeric — reject anything else immediately
if (!placeId.matches("\\d+")) {
statusLabel.setForeground(Start.ACCENT_COLOR);
statusLabel.setText("⚠️ Place ID must be a number (digits only).");
return;
}
// Reset preview area to "loading" state
statusLabel.setForeground(Start.SUBTEXT_COLOR);
statusLabel.setText("Looking up game…");
nameLabel.setText("");
thumbLabel.setIcon(null);
thumbLabel.setText("⏳");
disableSubmit();
// Run network calls off the EDT — keep the dialog responsive
CompletableFuture.runAsync(() -> {
StringthumbUrl = Start.fetchThumbnailUrl(placeId);
StringgameName = Start.fetchGameName(placeId);
// Return to EDT to update UI
SwingUtilities.invokeLater(() -> showPreview(placeId, gameName, thumbUrl));
});
}
/**
* Update the preview area with lookup results.
* Enables Submit if at least a name was found; shows error if nothing found.
* Must be called on the EDT.
*/
privatevoidshowPreview(StringplaceId, StringgameName, StringthumbUrl) {
if (gameName == null && thumbUrl == null) {
// Roblox returned nothing — bad ID or network failure
statusLabel.setForeground(Start.ACCENT_COLOR);
statusLabel.setText("⚠️ Game not found. Double-check the Place ID.");
thumbLabel.setIcon(null);
thumbLabel.setText("❓");
return;
}
// Stash validated result — used by onSubmit()
fetchedPlaceId = placeId;
fetchedGameName = (gameName != null) ? gameName : "Game " + placeId;
nameLabel.setText(fetchedGameName);
if (thumbUrl != null) {
// Download and display thumbnail in the background
finalStringurl = thumbUrl;
CompletableFuture.runAsync(() -> {
try {
HttpRequestreq = HttpRequest.newBuilder(URI.create(url))
.timeout(Duration.ofSeconds(8)).GET().build();
byte[] data = Start.HTTP.send(req, HttpResponse.BodyHandlers.ofByteArray()).body();
BufferedImageimg = ImageIO.read(newByteArrayInputStream(data));
if (img != null) {
Imagescaled = img.getScaledInstance(100, 100, Image.SCALE_SMOOTH);
SwingUtilities.invokeLater(() -> {
thumbLabel.setIcon(newImageIcon(scaled));
thumbLabel.setText("");
});
}
} catch (Exceptione) {
// Thumbnail failed — emoji fallback, don't block the submit
SwingUtilities.invokeLater(() -> { thumbLabel.setIcon(null); thumbLabel.setText("🎮"); });
}
});
} else {
// No thumbnail URL — show emoji placeholder
thumbLabel.setIcon(null);
thumbLabel.setText("🎮");
}
// Success — enable Submit button
statusLabel.setForeground(Color.decode("#4caf50"));
statusLabel.setText("✅ Game found!");
enableSubmit();
}
// ── Submit flow ───────────────────────────────────────────────────────────
/**
* Save the request and close the dialog.
* Only reachable if a valid lookup has already succeeded (submitBtn enabled).
*/
privatevoidonSubmit() {
if (fetchedPlaceId == null) return; // Guard — shouldn't happen but be safe
booleanok = Start.saveRequest(
fetchedPlaceId,
fetchedGameName,
noteEntry.getText().strip(),
""// No URL in the manual flow
);
dispose();
if (ok) {
JOptionPane.showMessageDialog(getParent(),
"'" + fetchedGameName + "' has been requested.\nAsk a parent to review it!",
"Request Sent! 🎮", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(getParent(),
"Permission error saving your request.\nAsk a parent to check the requests file.",
"Could Not Save", JOptionPane.ERROR_MESSAGE);
}
}
// ── Submit button state helpers ───────────────────────────────────────────
/** Enable Submit with the blue active colour. */
privatevoidenableSubmit() {
submitBtn.setEnabled(true);
submitBtn.setBackground(Start.REQUEST_COLOR);
submitBtn.setForeground(Color.WHITE);
}
/** Disable Submit with the grey inactive colour. */
privatevoiddisableSubmit() {
submitBtn.setEnabled(false);
submitBtn.setBackground(Color.decode("#444444"));
submitBtn.setForeground(Color.decode("#888888"));
}
}