swing-ui 0.1.25 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (116) hide show
  1. package/LICENSE +5 -4
  2. package/README.md +144 -7
  3. package/SwingUI.js +2611 -0
  4. package/classes/Arc.js +185 -0
  5. package/classes/Button.js +55 -0
  6. package/classes/CheckBox.js +33 -0
  7. package/classes/CheckBoxMenuItem.js +24 -0
  8. package/classes/ComboBox.js +329 -0
  9. package/classes/ComponentContainer.js +828 -0
  10. package/classes/ContextMenu.js +141 -0
  11. package/classes/DocumentEditor.js +168 -0
  12. package/classes/Ellipse.js +76 -0
  13. package/classes/InternalWindow.js +397 -0
  14. package/classes/InternalWindowContainer.js +93 -0
  15. package/classes/Label.js +52 -0
  16. package/classes/Line.js +105 -0
  17. package/classes/List.js +452 -0
  18. package/classes/MediaPlayer.js +504 -0
  19. package/classes/Menu.js +74 -0
  20. package/classes/MenuBar.js +56 -0
  21. package/classes/MenuItem.js +92 -0
  22. package/classes/Panel.js +50 -0
  23. package/classes/PasswordField.js +47 -0
  24. package/classes/Polygon.js +236 -0
  25. package/classes/Polyline.js +240 -0
  26. package/classes/ProgressBar.js +62 -0
  27. package/classes/RadioButton.js +110 -0
  28. package/classes/RadioButtonMenuItem.js +106 -0
  29. package/classes/Rectangle.js +199 -0
  30. package/classes/ScrollPane.js +53 -0
  31. package/classes/ScrollableComponent.js +141 -0
  32. package/classes/Separator.js +24 -0
  33. package/classes/Shape.js +584 -0
  34. package/classes/ShapeContainer.js +231 -0
  35. package/classes/Slider.js +172 -0
  36. package/classes/Spinner.js +172 -0
  37. package/classes/SplitView.js +82 -0
  38. package/classes/Stroke.js +115 -0
  39. package/classes/Table.js +982 -0
  40. package/classes/Tabs.js +228 -0
  41. package/classes/TextArea.js +125 -0
  42. package/classes/TextComponent.js +207 -0
  43. package/classes/TextField.js +128 -0
  44. package/classes/TextShape.js +123 -0
  45. package/classes/ToggleButton.js +147 -0
  46. package/classes/ToolBar.js +120 -0
  47. package/classes/TrayIcon.js +54 -0
  48. package/classes/Tree.js +569 -0
  49. package/classes/UIComponent.js +2154 -0
  50. package/classes/Window.js +609 -0
  51. package/enums.js +298 -0
  52. package/fonts.conf +32 -0
  53. package/functions.js +254 -0
  54. package/icon.png +0 -0
  55. package/index.js +109 -4
  56. package/jar/gui-11.jar +0 -0
  57. package/jar/lib/flatlaf.jar +0 -0
  58. package/jar/lib/jna.jar +0 -0
  59. package/package.json +58 -33
  60. package/typedefs.js +973 -0
  61. package/types/SwingUI.d.ts +727 -0
  62. package/types/classes/Arc.d.ts +111 -0
  63. package/types/classes/Button.d.ts +38 -0
  64. package/types/classes/CheckBox.d.ts +22 -0
  65. package/types/classes/CheckBoxMenuItem.d.ts +18 -0
  66. package/types/classes/ComboBox.d.ts +172 -0
  67. package/types/classes/ComponentContainer.d.ts +131 -0
  68. package/types/classes/ContextMenu.d.ts +74 -0
  69. package/types/classes/DocumentEditor.d.ts +95 -0
  70. package/types/classes/Ellipse.d.ts +34 -0
  71. package/types/classes/InternalWindow.d.ts +163 -0
  72. package/types/classes/InternalWindowContainer.d.ts +36 -0
  73. package/types/classes/Label.d.ts +33 -0
  74. package/types/classes/Line.d.ts +48 -0
  75. package/types/classes/List.d.ts +223 -0
  76. package/types/classes/MediaPlayer.d.ts +381 -0
  77. package/types/classes/Menu.d.ts +43 -0
  78. package/types/classes/MenuBar.d.ts +26 -0
  79. package/types/classes/MenuItem.d.ts +57 -0
  80. package/types/classes/Panel.d.ts +28 -0
  81. package/types/classes/PasswordField.d.ts +22 -0
  82. package/types/classes/Polygon.d.ts +93 -0
  83. package/types/classes/Polyline.d.ts +96 -0
  84. package/types/classes/ProgressBar.d.ts +36 -0
  85. package/types/classes/RadioButton.d.ts +70 -0
  86. package/types/classes/RadioButtonMenuItem.d.ts +71 -0
  87. package/types/classes/Rectangle.d.ts +82 -0
  88. package/types/classes/ScrollPane.d.ts +23 -0
  89. package/types/classes/ScrollableComponent.d.ts +81 -0
  90. package/types/classes/Separator.d.ts +14 -0
  91. package/types/classes/Shape.d.ts +223 -0
  92. package/types/classes/ShapeContainer.d.ts +92 -0
  93. package/types/classes/Slider.d.ts +107 -0
  94. package/types/classes/Spinner.d.ts +129 -0
  95. package/types/classes/SplitView.d.ts +47 -0
  96. package/types/classes/Stroke.d.ts +99 -0
  97. package/types/classes/Table.d.ts +652 -0
  98. package/types/classes/Tabs.d.ts +118 -0
  99. package/types/classes/TextArea.d.ts +45 -0
  100. package/types/classes/TextComponent.d.ts +136 -0
  101. package/types/classes/TextField.d.ts +94 -0
  102. package/types/classes/TextShape.d.ts +73 -0
  103. package/types/classes/ToggleButton.d.ts +84 -0
  104. package/types/classes/ToolBar.d.ts +82 -0
  105. package/types/classes/TrayIcon.d.ts +43 -0
  106. package/types/classes/Tree.d.ts +354 -0
  107. package/types/classes/UIComponent.d.ts +966 -0
  108. package/types/classes/Window.d.ts +334 -0
  109. package/types/enums.d.ts +437 -0
  110. package/types/functions.d.ts +109 -0
  111. package/types/index.d.ts +188 -0
  112. package/types/typedefs.d.ts +1626 -0
  113. package/ui-process-manager.js +317 -0
  114. package/video-devices-linux.sh +72 -0
  115. package/video-devices-mac.sh +33 -0
  116. package/CHANGES.md +0 -3
package/enums.js ADDED
@@ -0,0 +1,298 @@
1
+
2
+ /**
3
+ * A keyboard shortcut binding scope. Used to set when a keyboard shortcut should emit a KeyEvent for onShortcutAction() event handler using a {@link UIComponent#shortcuts|shortcuts()} method.
4
+ *
5
+ * @readonly
6
+ * @global
7
+ * @enum {number}
8
+ * @property {number} Focused - When the component has focus (default for non-container components).
9
+ * @property {number} Ancestor - When any child or their children have a focus (default for container components, other than Window).
10
+ * @property {number} Window - When component is in focused window (default for Window component).
11
+ * @see UIComponent#shortcuts
12
+ * @see UIComponent#onShortcut
13
+ */
14
+ export const ShortcutScope = Object.freeze({Focused: 0, Ancestor: 1, Window: 2});
15
+
16
+ /**
17
+ * Enum for drag and drop actions.
18
+ * Used with `dragAction()` and `dropAction()`.
19
+ *
20
+ * @readonly
21
+ * @global
22
+ * @enum {string}
23
+ * @property {string} None - No action.
24
+ * @property {string} Copy - Copy action.
25
+ * @property {string} Move - Move action.
26
+ * @property {string} CopyOrMove - Copy or move action.
27
+ * @property {string} Link - Link action.
28
+ * @see UIComponent#dragAction
29
+ * @see UIComponent#dropAction
30
+ * @example
31
+ * list.dragAction(DragDropAction.CopyOrMove);
32
+ * list.dropAction(DragDropAction.CopyOrMove);
33
+ */
34
+ export const DragDropAction = Object.freeze({None: "None", Copy: "Copy", Move: "Move", CopyOrMove: "CopyOrMove", Link: "Link"});
35
+
36
+ /**
37
+ * Used with dropMode() in Table, List, and Tree
38
+ *
39
+ * @readonly
40
+ * @global
41
+ * @enum {string}
42
+ * @property {string} On - Drop on items.
43
+ * @property {string} Insert - Insert between items
44
+ * @property {string} OnOrInsert - Drop on items and between items
45
+ * @see {@link List#dropMode|List.dropMode()}
46
+ * @see {@link Table#dropMode|Table.dropMode()}
47
+ * @see {@link Tree#dropMode|Tree.dropMode()}
48
+ * @example
49
+ * table.dropMode(DropMode.OnOrInsert);
50
+ */
51
+ export const DropMode = Object.freeze({On: "On", Insert: "Insert", OnOrInsert: "OnOrInsert"});
52
+
53
+ /**
54
+ * Enum for mouse actions.
55
+ * Used with `ui.mouseAction(action, button)`.
56
+ *
57
+ * @readonly
58
+ * @global
59
+ * @enum {number}
60
+ * @property {number} Press - Mouse button press.
61
+ * @property {number} Release - Mouse button release.
62
+ * @property {number} Click - Single click.
63
+ * @property {number} DoubleClick - Double click.
64
+ * @property {number} VerticalScroll - Vertical wheel scroll.
65
+ * @property {number} HorizontalScroll - Horizontal wheel scroll.
66
+ * @see SwingUI#mouseAction
67
+ * @example <h2>Performing a mouse click:</h2>
68
+ * ui.mouseAction(MouseAction.Click, MouseButton.Left);
69
+ */
70
+ export const MouseAction = Object.freeze({Press: 0, Release: 1, Click: 2, DoubleClick: 3, VerticalScroll: 4, HorizontalScroll: 5});
71
+
72
+ /**
73
+ * Enum for mouse buttons.
74
+ * Compare to `mouseEvent.button` on mouse events or use with `ui.mouseAction(action, button)`.
75
+ *
76
+ * @readonly
77
+ * @global
78
+ * @enum {number}
79
+ * @property {number} Left - Left mouse button.
80
+ * @property {number} Right - Right mouse button.
81
+ * @property {number} Middle - Middle mouse button.
82
+ * @see SwingUI#mouseAction
83
+ * @see UIComponent#onMouseEvents
84
+ * @example <h2>Checking if right mouse button was clicked:</h2>
85
+ * if(mouseEvent.button === MouseButton.Right)
86
+ * {
87
+ * console.log("Right click);
88
+ * }
89
+ * @example <h2>Performing a mouse click:</h2>
90
+ * ui.mouseAction(MouseAction.Click, MouseButton.Left);
91
+ */
92
+ export const MouseButton = Object.freeze({Left: 1, Right: 3, Middle: 2});
93
+
94
+ /**
95
+ * Keyboard key actions to be performed on the computer. Use it only with `ui.keyboardAction(action, value)` to perform key actions.
96
+ *
97
+ * @readonly
98
+ * @global
99
+ * @enum {number}
100
+ * @property {number} Press - Key press.
101
+ * @property {number} Release - Key release.
102
+ * @property {number} Type - Key typed (typed character input).
103
+ * @see SwingUI#keyboardAction
104
+ * @example <h2>Performing a key type action:</h2>
105
+ * ui.keyboardAction(KeyAction.Type, "A");
106
+ */
107
+ export const KeyAction = Object.freeze({Press: 0, Release: 1, Type: 2});
108
+
109
+ /**
110
+ * Enum for standard message dialog icons. Used in `alert()`, `dialog()` and `TrayIcon.showNotification()`.
111
+ *
112
+ * @readonly
113
+ * @global
114
+ * @enum {number}
115
+ * @property {number} Error - Error icon.
116
+ * @property {number} Info - Information icon.
117
+ * @property {number} Warning - Warning icon.
118
+ * @property {number} Question - Question icon.
119
+ * @see alert
120
+ * @see dialog
121
+ * @see TrayIcon#showNotification
122
+ * @example
123
+ * alert("This file doesn't exist!", "File Absent", MessageIcon.Warning);
124
+ */
125
+ export const MessageIcon = Object.freeze({Error: 0, Info: 1, Warning: 2, Question: 3});
126
+
127
+ /**
128
+ * Enum for selection modes in UI components like Table, List, Tree. Used with `selectionMode()` to set of get a selection mode of a list of items or rows.
129
+ *
130
+ * @readonly
131
+ * @global
132
+ * @enum {number}
133
+ * @property {number} Single - Single item selection.
134
+ * @property {number} Range - Range selection (shift-click).
135
+ * @property {number} Multiple - Multiple selection (ctrl/cmd-click).
136
+ * @see Table#selectionMode
137
+ * @see List#selectionMode
138
+ * @see Tree#selectionMode
139
+ * @example
140
+ * table.selectionMode(SelectionMode.Single);
141
+ */
142
+ export const SelectionMode = Object.freeze({Single: 0, Range: 1, Multiple: 2});
143
+
144
+ /**
145
+ * An enum for generic font family names to use instead of a font name that may not be installed on target operating systems.
146
+ * Use with `component.font()`, `Text()` shape, `font` property of a List item, Tree item, ComboBox item, Table column, and everywhere else where font can be set.
147
+ *
148
+ * @readonly
149
+ * @global
150
+ * @enum {string}
151
+ * @property {string} Serif - Proportional font with small decorative strokes at the ends of letters (e.g., Times New Roman, Georgia, Garamond, Cambria.).
152
+ * @property {string} SansSerif - Proportional font without decorative strokes; clean and modern (e.g., Arial, Helvetica, Verdana, Calibri, Open Sans).
153
+ * @property {string} Monospaced - Fixed-width font where every character has the same width (e.g., Courier New, Consolas, Lucida Console, Menlo).
154
+ * @property {string} Cursive - Flowing, handwritten-style font (e.g., Brush Script, Comic Sans MS, Pacifico, Lobster).
155
+ * @property {string} System - The system UI font provided by the operating system. (e.g., Segoe UI (Windows), San Francisco (macOS/iOS), Roboto (Android/Linux)).
156
+ * @property {string} Fantasy - Decorative or themed font (e.g., Impact, Papyrus, Jokerman, Copperplate).
157
+ * @see UIComponent#font
158
+ * @see Text
159
+ * @example <h2>Setting component font:</h2>
160
+ * textarea.font({name: FontFamily.SansSerif});
161
+ *
162
+ * @example <h2>Setting Text shape font:</h2>
163
+ * let text = new TextShape("Hello World", {name: FontFamily.Fantasy, size: 30});
164
+ */
165
+ export const FontFamily = Object.freeze({Serif: "serif", SansSerif: "sans-serif", Monospaced: "monospace", Cursive: "cursive", System: "system-ui", Fantasy: "fantasy"});
166
+
167
+ /**
168
+ * Enum for data types used with drag and drop.
169
+ * Used with `dragDataType()`, `dropDataType()`, and to check if certain data type was dragged onto component.
170
+ *
171
+ * @readonly
172
+ * @global
173
+ * @enum {string}
174
+ * @property {string} None - No data.
175
+ * @property {string} All - Accept any type. Used only for dropDataType().
176
+ * @property {string} Text - Plain text.
177
+ * @property {string} HTML - HTML content.
178
+ * @property {string} Image - Image data.
179
+ * @property {string} Files - Files copied to system clipboard or dragged/dropped.
180
+ * @property {string} URIList - List of URIs.
181
+ * @property {string} URL - A single URL string.
182
+ * @property {string} JSON - JSON-encoded data.
183
+ * @property {string} TextEncoding - Text encoding used to encode the Text and/or HTML values.
184
+ * @property {string} Components - UI components (`component/*`).
185
+ * @example <h2>Sets the type of data list items represent when dragged:</h2>
186
+ * list.dragDataType(DataType.Files);
187
+ * @example <h2>Sets the type of data Table component accepts:</h2>
188
+ * table.dropDataType([DataType.Files, DataType.URIList, DataType.URL]);
189
+ * @example <h2>Check if expected data type was dragged onto Label:</h2>
190
+ * label.dropAction(DragDropAction.CopyOrMove).dropDataType(DataType.Files).onDropEvents(e => {
191
+ * if(e.type === "drop" && Array.isArray(e.data[DataType.Filest]))
192
+ * {
193
+ * // set first file as label's icon image
194
+ * e.target.icon(e.data[DataType.Files][0]);
195
+ * }
196
+ * });
197
+ */
198
+ export const DataType = Object.freeze({None: "None", All: "All", Text: "Text", HTML: "HTML", Image: "Image", Files: "Files", URIList: "URIList", URL: "URL", JSON: "JSON", Components: "component/*"});
199
+
200
+ /**
201
+ * Enum for actions on text components (TextField, TextArea, etc.). To be used in text() method.
202
+ *
203
+ * @readonly
204
+ * @global
205
+ * @enum {number}
206
+ * @property {number} Set - Replace all text.
207
+ * @property {number} Prepend - Add text at the beginning.
208
+ * @property {number} Append - Add text at the end.
209
+ * @property {number} Insert - Insert at current caret position.
210
+ * @property {number} ReplaceSelection - Replace selected text.
211
+ * @see UIComponent#text
212
+ * @example <h2>Appending text to TextArea component:</h2>
213
+ * let textarea = new TextArea("Hello").text(" World!", TextAction.Append);
214
+ * // text area will contain text "Hello World!"
215
+ */
216
+ export const TextAction = Object.freeze({Set: 0, Prepend: 1, Append: 2, Insert: 3, ReplaceSelection: 4});
217
+
218
+ /**
219
+ * Enum for available mouse cursors.
220
+ *
221
+ * @readonly
222
+ * @global
223
+ * @enum {number}
224
+ * @property {number} Default - Default cursor.
225
+ * @property {number} Crosshair - Crosshair.
226
+ * @property {number} Text - Text input cursor.
227
+ * @property {number} Wait - Wait/busy cursor.
228
+ * @property {number} ResizeSW - Resize south-west.
229
+ * @property {number} ResizeSE - Resize south-east.
230
+ * @property {number} ResizeNW - Resize north-west.
231
+ * @property {number} ResizeNE - Resize north-east.
232
+ * @property {number} ResizeN - Resize north.
233
+ * @property {number} ResizeS - Resize south.
234
+ * @property {number} ResizeW - Resize west.
235
+ * @property {number} ResizeE - Resize east.
236
+ * @property {number} Hand - Pointing hand.
237
+ * @property {number} Move - Move cursor.
238
+ * @see UIComponent#cursor
239
+ * @example <h2>Sets "Wait" cursor to Panel component:</h2>
240
+ * panel.cursor(Cursor.Wait);
241
+ */
242
+ export const Cursor = Object.freeze({Default: 0, Crosshair: 1, Text: 2, Wait: 3, ResizeSW: 4, ResizeSE: 5, ResizeNW: 6, ResizeNE: 7, ResizeN: 8, ResizeS: 9, ResizeW: 10, ResizeE: 11, Hand: 12, Move: 13});
243
+
244
+ /**
245
+ * Enum for component orientation, such as Slider or Toolbar.
246
+ *
247
+ * @readonly
248
+ * @global
249
+ * @enum {number}
250
+ * @property {number} Horizontal - Horizontal orientation.
251
+ * @property {number} Vertical - Vertical orientation.
252
+ * @see SplitView#orientation
253
+ * @see ToolBar#orientation
254
+ * @example <h2>Setting orientation to a component:</h2>
255
+ * toolbar.orientation(Orientation.Vertical);
256
+ */
257
+ export const Orientation = Object.freeze({Horizontal: 0, Vertical: 1});
258
+
259
+ /**
260
+ * Border style enum. Use in a border object..
261
+ *
262
+ * @readonly
263
+ * @global
264
+ * @enum {string}
265
+ * @property {string} None - No border.
266
+ * @property {string} Line - Solid line.
267
+ * @property {string} Dashed - Dashed border.
268
+ * @property {string} Metal - Metallic style.
269
+ * @property {string} EtchedRaised - Etched raised border.
270
+ * @property {string} EtchedLowered - Etched lowered border.
271
+ * @property {string} BevelRaised - Bevel raised border.
272
+ * @property {string} BevelLowered - Bevel lowered border.
273
+ * @property {string} SoftRaised - Soft raised border.
274
+ * @property {string} SoftLowered - Soft lowered border.
275
+ * @see UIComponent#border
276
+ * @example <h2>Setting a new border:</h2>
277
+ * let border = {style: BorderStyle.Dashed, size: 2, color: "#002288"}
278
+ * panel.border(border);
279
+ */
280
+ export const BorderStyle = Object.freeze({None: "none", Line: "line", Dashed: "dashed", Metal: "metal", EtchedRaised: "etched_raised", EtchedLowered: "etched_lowered", BevelRaised: "bevel_raised", BevelLowered: "bevel_lowered", SoftRaised: "soft_raised", SoftLowered: "soft_lowered"});
281
+
282
+ /**
283
+ * Enum for word wrapping styles.
284
+ * Used with `lineWrap()` method in TextArea and DocumentEditor to control how text is wrapped.
285
+ *
286
+ * @readonly
287
+ * @global
288
+ * @enum {number}
289
+ * @property {number} None - Disable line wrapping.
290
+ * @property {number} Word - Wrap line when a full word doesn't fit.
291
+ * @property {number} Character - Wrap line when a character doesn't fit.
292
+ * @see TextArea#lineWrapping
293
+ * @see DocumentEditor#lineWrapping
294
+ * @example <h2>Setting line wrapping style:</h2>
295
+ * textarea.lineWrap(LineWrap.Word);
296
+ */
297
+ export const LineWrap = Object.freeze({None: 0, Word: 1, Character: 2});
298
+
package/fonts.conf ADDED
@@ -0,0 +1,32 @@
1
+ <?xml version="1.0"?>
2
+ <!DOCTYPE fontconfig SYSTEM "urn:fontconfig:fonts.dtd">
3
+ <fontconfig>
4
+
5
+ <!-- This file is required for ffmpeg to draw text -->
6
+
7
+ <!-- Windows font directory -->
8
+ <dir>C:/Windows/Fonts</dir>
9
+
10
+ <!-- Linux font directories -->
11
+ <dir>/usr/share/fonts</dir>
12
+ <dir>/usr/local/share/fonts</dir>
13
+ <dir>~/.fonts</dir>
14
+
15
+ <!-- macOS font directories -->
16
+ <dir>/System/Library/Fonts</dir>
17
+ <dir>/Library/Fonts</dir>
18
+ <dir>~/Library/Fonts</dir>
19
+
20
+ <!-- Cache directory -->
21
+ <cachedir>/tmp/fontconfig/cache</cachedir>
22
+
23
+ <!-- Default font fallback -->
24
+ <match target="pattern">
25
+ <test name="family" qual="any">
26
+ <string>sans-serif</string>
27
+ </test>
28
+ <edit name="family" mode="prepend">
29
+ <string>Arial</string>
30
+ </edit>
31
+ </match>
32
+ </fontconfig>
package/functions.js ADDED
@@ -0,0 +1,254 @@
1
+ /**
2
+ * Utility Functions
3
+ */
4
+
5
+ import "./classes/UIComponent.js";
6
+ import {UIComponent} from "./classes/UIComponent.js";
7
+
8
+ /**
9
+ * Displays a dialog windows with error message and error icon.
10
+ *
11
+ * @param {string|Error} message - Error message to display or Error object to display stack trace.
12
+ * @param {boolean|Error} [show_stack_trace=true] - Whether to show stack trace to be able to trace the error (applies only when first parameter is not an Error object).
13
+ * @return {void} Does not return a value
14
+ * @global
15
+ * @example <h2>Showing an error message only:</h2>
16
+ * error("Invalid parameters passed.", false);
17
+ *
18
+ * @example <h2>Showing an error message with a stack trace:</h2>
19
+ * error("Invalid parameters passed.");
20
+ *
21
+ * @example <h2>Showing an error message by passing an Error object:</h2>
22
+ * try
23
+ * {
24
+ * // some code that may throw an error
25
+ * }
26
+ * catch(ex)
27
+ * {
28
+ * error(ex);
29
+ * }
30
+ *
31
+ */
32
+ export function error(message, show_stack_trace = true)
33
+ {
34
+ if(message instanceof Error)
35
+ {
36
+ message = show_stack_trace ? message.stack.toString() : message.message;
37
+ }
38
+ else if(show_stack_trace)
39
+ {
40
+ message += "\r\n" + (show_stack_trace instanceof Error ? show_stack_trace.stack.toString() : new Error().stack.toString());
41
+ }
42
+
43
+ if(typeof ui !== "undefined")
44
+ {
45
+ if(message instanceof Error) message = message.stack;
46
+ alert(message, "JavaScript Error", 0);
47
+ }
48
+ else
49
+ {
50
+ console.error(message);
51
+ }
52
+ }
53
+
54
+ /**
55
+ * Returns a text representation of passed variable. Similar to JSON.stringify().
56
+ *
57
+ * @internal
58
+ * @ignore
59
+ * @global
60
+ * @param {any} variable - Any variable to be converted to a string representation
61
+ * @return {string} Serialized object as a String
62
+ */
63
+ export function stringify(variable)
64
+ {
65
+ const cache = new Set();
66
+
67
+ function recursiveStringify(value, indent = 0)
68
+ {
69
+ const ind_str = " ";
70
+
71
+ if(value === undefined)
72
+ {
73
+ return ind_str.repeat(indent) + "UNDEFINED";
74
+ }
75
+ else if(value === null)
76
+ {
77
+ return ind_str.repeat(indent) + "NULL";
78
+ }
79
+ else if(typeof value === "string")
80
+ {
81
+ return `"${value}"`;
82
+ }
83
+ else if(typeof value === "function")
84
+ {
85
+ return "Function()"; //ind_str.repeat(indent) + value.toString();
86
+ }
87
+ if(typeof value === "object")
88
+ {
89
+ if(cache.has(value))
90
+ {
91
+ return ind_str.repeat(indent) + value.constructor.name + "{} [Circular]";
92
+ }
93
+ cache.add(value);
94
+
95
+ if(Array.isArray(value))
96
+ {
97
+ return value.length === 0 ? `[]` : `[\n` + ind_str.repeat(indent) + `${value.map(item => recursiveStringify(item, indent + 1)).join(",\n" + ind_str.repeat(indent))}\n${ind_str.repeat(Math.max(0, indent - 1))}]\n`;
98
+ }
99
+ else if(value.constructor && value.constructor.name !== "Object")
100
+ {
101
+ // Handles custom classes or instances of built-in classes (like Date)
102
+ const className = value.constructor.name;
103
+ const props = Object.keys(value).map(key => ind_str.repeat(indent) + `${key}: ${recursiveStringify(value[key], indent + 1)}`);
104
+ return props.length === 0 ? `${className}{}` : `${className} {\n${props.join("\n")}\n${ind_str.repeat(Math.max(0, indent - 1))}}`;
105
+ }
106
+ else
107
+ {
108
+ // Handles plain objects
109
+ const props = Object.keys(value).map(key => ind_str.repeat(indent) + `${key}: ${recursiveStringify(value[key], indent + 1)}`);
110
+ return `{\n${props.join("\n")}\n${ind_str.repeat(Math.max(0, indent - 1))}}`;
111
+ }
112
+ }
113
+
114
+ // Handles primitives, strings, and everything else
115
+ return JSON.stringify(value);
116
+ }
117
+
118
+ return recursiveStringify(variable);
119
+ }
120
+
121
+ /**
122
+ * Displays a dialog window with a specified error message and an error icon.
123
+ *
124
+ * @param {string} message - A message to be displayed in the dialog window
125
+ * @param {string} [title="Message"] - Dialog window title bar text
126
+ * @param {MessageIcon} [icon=MessageIcon.Info] - Icon to show in the dialog window.
127
+ *
128
+ * Valid value are:
129
+ * - MessageIcon.Error
130
+ * - MessageIcon.Info
131
+ * - MessageIcon.Warning
132
+ * - MessageIcon.Question
133
+ *
134
+ * @param {Window} [ownerWindow=null] - If set, the message dialog window will attempts to be centered at specified window
135
+ * @return {void} Does not return a value
136
+ * @global
137
+ *
138
+ * @example <h2>Simple usage with only one parameter:</h2>
139
+ * alert("File name is invalid. Please try again");
140
+ *
141
+ * @example <h2>Usage example when passing all parameters:</h2>
142
+ * alert("File name is invalid. Please try again","Invalid File", MessageIcon.Warning, this);
143
+ */
144
+ export function alert(message, title = "Message", icon = 1, ownerWindow = null)
145
+ {
146
+ if(ownerWindow instanceof globalThis.UIComponent) ownerWindow = ownerWindow.window;
147
+
148
+ ui.showMessage(message, title, icon, ownerWindow);
149
+ }
150
+
151
+ /**
152
+ * Requests user for text input in a dialog window. Returns a string as user's input or undefined when user declined to input.
153
+ *
154
+ * @param {string} message - Message to user about this text input.
155
+ * @param {string} [default_value=""] - Default string that will be already present in the dialog window.
156
+ * @param {Window} [ownerWindow=null] - Window that will be overlaid by the dialog window.
157
+ * @returns {string|undefined} Returns user provided string, or `undefined` if user canceled input.
158
+ * @global
159
+ * @example
160
+ * let response = prompt("Please enter your name:", "John Doe", this);
161
+ * console.log("You entered: " + response);
162
+ */
163
+ export function prompt(message, default_value = "", ownerWindow = null)
164
+ {
165
+ const req = {
166
+ java: "showInputDialog",
167
+ message: message,
168
+ default: default_value
169
+ };
170
+
171
+ if(ownerWindow instanceof globalThis.Window)
172
+ {
173
+ req.owner = ownerWindow.id;
174
+ }
175
+ else if(ownerWindow instanceof globalThis.UIComponent)
176
+ {
177
+ ownerWindow = ownerWindow.window;
178
+ if(ownerWindow instanceof globalThis.Window) req.owner = ownerWindow.id;
179
+ }
180
+
181
+ return ui.sendToGUI(req, function(r){return r.value;}, true);
182
+ }
183
+
184
+ /**
185
+ * Displays a confirmation dialog to user and returns user's response as boolean.
186
+ *
187
+ * @param {string} message - Message to display to user
188
+ * @param {Window} [window=null] - Window which dialog window will overlay. If omitted, last created visible Window that's not PopUp will be used.
189
+ * @return {boolean} Returns `true` if user confirms the message, or `false` otherwise.
190
+ * @global
191
+ * @example
192
+ * if(confirm("Delete this file?", this))
193
+ * {
194
+ * // delete code
195
+ * }
196
+ */
197
+ export function confirm(message, window = null)
198
+ {
199
+ // find first visible window
200
+ if(window == null)
201
+ {
202
+ window = UIComponent.getVisibleWindow();
203
+ }
204
+ else if(window instanceof UIComponent) window = window.window;
205
+ if(window == null) throw new Error("Window must be specified where to show confirmation dialog, or at least one window must be visible.");
206
+
207
+ return window.confirm(message);
208
+ }
209
+
210
+ /**
211
+ * Shows a custom dialog window that allows the user to respond by clicking one of provided buttons.
212
+ *
213
+ * @param {string} message - String message to user. It can be plain text or HTML.
214
+ * @param {string} [title=""] - Dialog window title shown in the title bar of the window
215
+ * @param {string[]} [buttons=["OK","Cancel"]] - An Array of button names. Indicate a default button by surrounding the text with square brackets like so: "[Cancel]"
216
+ * @param {MessageIcon|string} [icon=null] - One of MessageIcon icons, or an absolute path to an image from a file system.
217
+ * @param {Window} [window=null] - Owner window that will be overlaid by this dialog window. If omitted, last created visible Window that's not PopUp will be used.
218
+ * @return {number|undefined} Returns a zero-based number of a clicked button, or `null` if the dialog window was closed without clicking provided buttons.
219
+ * @global
220
+ * @example <h2>Usage example</h2>
221
+ * let response = dialog("Are you ready?", "Confirm readiness", ["Yes", "No", "[Maybe]"], MessageIcon.Question, window);
222
+ * switch(response)
223
+ * {
224
+ * case 0: return "Yes";
225
+ * case 1: return "No";
226
+ * case 2: return "Maybe";
227
+ * }
228
+ *
229
+ * @example <h2>Using custom image</h2>
230
+ * let response = dialog("Is this you?", "Question #5", ["Yes","[No]"], "C://User/John/profile_icon.jpg", window);
231
+ */
232
+ export function dialog(message, title = "", buttons = ["OK", "Cancel"], icon = null, window = null)
233
+ {
234
+ // find first visible window
235
+ if(window == null)
236
+ {
237
+ window = UIComponent.getVisibleWindow();
238
+ }
239
+ else if(window instanceof UIComponent) window = window.window;
240
+
241
+ if(!Array.isArray(buttons)) throw new Error(`Invalid argument "buttons" in dialog() function. String array is expected, but received ${typeof buttons}.`);
242
+
243
+ return ui.sendToGUI({
244
+ java: "showDialog",
245
+ title: title,
246
+ message: String(message),
247
+ buttons: buttons,
248
+ icon: icon,
249
+ frame: window != null ? window.id : null
250
+ },
251
+ r => r.value,
252
+ true
253
+ );
254
+ }
package/icon.png ADDED
Binary file