1 | !592 auto-import |
2 | |
3 | import java.awt.*; |
4 | |
5 | public class main { |
6 | public static void main(String[] args) { |
7 | JFrame frame = new JFrame("JavaX IDE!"); |
8 | |
9 | JTextArea textArea = new JTextArea(); |
10 | JButton btnGo = new JButton("Go!"); |
11 | /*btnGo.addActionListener(actionListener [ |
12 | ]);*/ |
13 | |
14 | JPanel panel = new JPanel(new LetterLayout("T", "T", "B").setBorder(10)); |
15 | panel.add("T", new JScrollPane(textArea)); |
16 | panel.add("B", new CenteredLine(btnGo)); |
17 | frame.add(panel); |
18 | |
19 | frame.setBounds(100, 100, 500, 400); |
20 | frame.setVisible(true); |
21 | exitOnFrameClose(frame); |
22 | } |
23 | |
24 | static void exitOnFrameClose(JFrame frame) { |
25 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
26 | } |
27 | } |
28 | |
29 | class LetterLayout implements LayoutManager { |
30 | private String[] lines; |
31 | private Map<String, Component> map = new TreeMap<String, Component>(); |
32 | private RC[] rows; |
33 | private RC[] cols; |
34 | private Cell[][] cells; |
35 | private int spacingX = 10, spacingY = 10; |
36 | private int insetTop, insetBottom, insetLeft, insetRight; |
37 | private int template; |
38 | private boolean formWideLeftSide, formWideRightSide; |
39 | |
40 | private static final int STALACTITE = 1, LEFT_ALIGNED_ROW = 2, CENTERED_ROW = 3, FORM = 4, RIGHT_ALIGNED_ROW = 5; |
41 | private boolean debug; |
42 | |
43 | public void setLeftBorder(int border) { |
44 | insetLeft = border; |
45 | } |
46 | |
47 | public void setRightBorder(int border) { |
48 | insetRight = border; |
49 | } |
50 | |
51 | public static JComponent withBorder(JComponent component, int border) { |
52 | JPanel panel = new JPanel(new LetterLayout("C").setBorder(border)); |
53 | panel.add("C", component); |
54 | return panel; |
55 | } |
56 | |
57 | public static JPanel panel(String... lines) { |
58 | return new JPanel(new LetterLayout(lines)); |
59 | } |
60 | |
61 | public static JPanel stalactitePanel() { |
62 | return new JPanel(stalactite()); |
63 | } |
64 | |
65 | static class DummyComponent extends JComponent { |
66 | } |
67 | |
68 | /** |
69 | * info about one matrix cell |
70 | */ |
71 | static class Cell { |
72 | boolean aux; // part of a larger cell, but not top-left corner |
73 | int minWidth, minHeight; |
74 | Component component; |
75 | int colspan, rowspan; |
76 | double weightX, weightY; |
77 | } |
78 | |
79 | /** |
80 | * info about one matrix row / column |
81 | */ |
82 | static class RC { |
83 | int min; |
84 | double weightSum; |
85 | int start; |
86 | int minEnd; |
87 | } |
88 | |
89 | private LetterLayout(int template) { |
90 | this.template = template; |
91 | } |
92 | |
93 | public LetterLayout(String... lines) { |
94 | this.lines = lines; |
95 | } |
96 | |
97 | public void removeLayoutComponent(Component component) { |
98 | map.values().remove(component); |
99 | } |
100 | |
101 | public void layoutContainer(Container container) { |
102 | prepareLayout(container); |
103 | |
104 | // do layout |
105 | |
106 | if (debug) |
107 | System.out.println("Container size: " + container.getSize()); |
108 | |
109 | Insets insets = getInsets(container); |
110 | for (int r = 0; r < rows.length; r++) { |
111 | for (int i = 0; i < cols.length;) { |
112 | Cell cell = cells[i][r]; |
113 | if (cell.aux) |
114 | ++i; |
115 | else { |
116 | if (cell.component != null) { |
117 | int x1 = cols[i].start; |
118 | int y1 = rows[r].start; |
119 | int x2 = i + cell.colspan < cols.length ? cols[i + cell.colspan].start - spacingX : container.getWidth() - insets.right; |
120 | int y2 = r + cell.rowspan < rows.length ? rows[r + cell.rowspan].start - spacingY : container.getHeight() - insets.bottom; |
121 | |
122 | if (debug) |
123 | System.out.println("Layouting ("+i+", "+r+", " + cell.component.getClass().getName() + "): "+x1+" "+y1+" "+x2+" "+y2); |
124 | |
125 | cell.component.setBounds(x1, y1, x2 - x1, y2 - y1); |
126 | } |
127 | i += cells[i][r].colspan; |
128 | } |
129 | } |
130 | } |
131 | } |
132 | |
133 | private void prepareLayout(Container container) { |
134 | applyTemplate(container); |
135 | |
136 | int numRows = lines.length, numCols = lines[0].length(); |
137 | for (int i = 1; i < numRows; i++) if (lines[i].length() != numCols) |
138 | throw new IllegalArgumentException("Lines have varying length"); |
139 | cells = new Cell[numCols][numRows]; |
140 | rows = new RC[numRows]; |
141 | cols = new RC[numCols]; |
142 | |
143 | for (int r = 0; r < numRows; r++) rows[r] = new RC(); |
144 | for (int i = 0; i < numCols; i++) cols[i] = new RC(); |
145 | for (int r = 0; r < numRows; r++) for (int i = 0; i < numCols; i++) cells[i][r] = new Cell(); |
146 | |
147 | // define cells |
148 | |
149 | for (int r = 0; r < numRows; r++) { |
150 | String line = lines[r]; |
151 | for (int i = 0; i < numCols;) { |
152 | Cell cell = cells[i][r]; |
153 | if (cell.aux) { |
154 | ++i; |
155 | continue; |
156 | } |
157 | char ch = line.charAt(i); |
158 | int iNext = i; |
159 | do ++iNext; while (iNext < numCols && ch == line.charAt(iNext)); |
160 | int rNext = r; |
161 | do ++rNext; while (rNext < numRows && ch == lines[rNext].charAt(i)); |
162 | |
163 | cell.weightX = numCols == 1 || iNext > i + 1 ? 1.0 : 0.0; |
164 | cell.weightY = numRows == 1 || rNext > r + 1 ? 1.0 : 0.0; |
165 | |
166 | Component c = map.get(String.valueOf(ch)); |
167 | cell.component = c; |
168 | if (c != null) { |
169 | cell.minWidth = c.getMinimumSize().width + spacingX; |
170 | cell.minHeight = getMinimumHeight(c) + spacingY; |
171 | } |
172 | cell.colspan = iNext - i; |
173 | cell.rowspan = rNext - r; |
174 | |
175 | if (cell.colspan == 1) |
176 | cols[i].min = Math.max(cols[i].min, cell.minWidth); |
177 | if (cell.rowspan == 1) |
178 | rows[r].min = Math.max(rows[r].min, cell.minHeight); |
179 | |
180 | for (int r2 = r; r2 < rNext; r2++) |
181 | for (int i2 = i; i2 < iNext; i2++) |
182 | if (r2 != r || i2 != i) |
183 | cells[i2][r2].aux = true; |
184 | |
185 | i = iNext; |
186 | } |
187 | } |
188 | |
189 | // determine minStarts, weightSums |
190 | |
191 | while (true) { |
192 | for (int i = 0; i < numCols; i++) { |
193 | int minStart = i == 0 ? 0 : cols[i - 1].minEnd; |
194 | double weightStart = i == 0 ? 0.0 : cols[i - 1].weightSum; |
195 | for (int r = 0; r < numRows; r++) { |
196 | Cell cell = cells[i][r]; |
197 | if (!cell.aux) { |
198 | RC rc = cols[i + cell.colspan - 1]; |
199 | rc.minEnd = Math.max(rc.minEnd, minStart + cell.minWidth); |
200 | rc.weightSum = Math.max(rc.weightSum, weightStart + cell.weightX); |
201 | } |
202 | } |
203 | } |
204 | |
205 | for (int r = 0; r < numRows; r++) { |
206 | int minStart = r == 0 ? 0 : rows[r - 1].minEnd; |
207 | double weightStart = r == 0 ? 0.0 : rows[r - 1].weightSum; |
208 | for (int i = 0; i < numCols; i++) { |
209 | Cell cell = cells[i][r]; |
210 | if (!cell.aux) { |
211 | RC rc = rows[r + cell.rowspan - 1]; |
212 | rc.minEnd = Math.max(rc.minEnd, minStart + cell.minHeight); |
213 | rc.weightSum = Math.max(rc.weightSum, weightStart + cell.weightY); |
214 | } |
215 | } |
216 | } |
217 | |
218 | if (allWeightsZero(cols)) { |
219 | for (int r = 0; r < numRows; r++) |
220 | for (int i = 0; i < numCols; i++) |
221 | cells[i][r].weightX = 1.0; |
222 | continue; |
223 | } |
224 | |
225 | if (allWeightsZero(rows)) { |
226 | for (int r = 0; r < numRows; r++) |
227 | for (int i = 0; i < numCols; i++) |
228 | cells[i][r].weightY = 1.0; |
229 | continue; |
230 | } |
231 | |
232 | break; |
233 | } |
234 | |
235 | // determine row, col starts |
236 | |
237 | Insets insets = getInsets(container); |
238 | determineStarts(cols, insets.left, container.getWidth() - insets.left - insets.right + spacingX, spacingX); |
239 | determineStarts(rows, insets.top, container.getHeight() - insets.top - insets.bottom + spacingY, spacingY); |
240 | } |
241 | |
242 | private boolean allWeightsZero(RC[] rcs) { |
243 | for (int i = 0; i < rcs.length; i++) |
244 | if (rcs[i].weightSum != 0.0) |
245 | return false; |
246 | return true; |
247 | } |
248 | |
249 | private static int getMinimumHeight(Component c) { |
250 | if (c instanceof JTextArea) { |
251 | return (int) ((JTextArea) c).getUI().getRootView((JTextArea) c).getPreferredSpan(javax.swing.text.View.Y_AXIS); |
252 | } |
253 | return c.getMinimumSize().height; |
254 | } |
255 | |
256 | private void applyTemplate(Container container) { |
257 | if (template == STALACTITE) { |
258 | Component[] components = container.getComponents(); |
259 | |
260 | lines = new String[components.length + 2]; |
261 | map.clear(); |
262 | for (int i = 0; i < components.length; i++) { |
263 | String s = String.valueOf(makeIndexChar(i)); |
264 | map.put(s, components[i]); |
265 | lines[i] = s; |
266 | } |
267 | lines[components.length] = lines[components.length + 1] = " "; |
268 | } else if (template == FORM) { |
269 | /* old method of calculating numRows: |
270 | int numRows = 0; |
271 | for (String key : map.keySet()) { |
272 | if (key.length() == 1) |
273 | numRows = Math.max(numRows, Character.toLowerCase(key.charAt(0))-'a'); |
274 | }*/ |
275 | Component[] components = container.getComponents(); |
276 | int numRows = components.length/2; |
277 | |
278 | lines = new String[numRows+2]; |
279 | map.clear(); |
280 | for (int row = 0; row < numRows; row++) { |
281 | String lower = String.valueOf(makeIndexChar(row)); |
282 | String upper = String.valueOf(makeAlternateIndexChar(row)); |
283 | Component rightComponent = components[row * 2 + 1]; |
284 | if (rightComponent instanceof DummyComponent) |
285 | upper = lower; |
286 | lines[row] = (formWideLeftSide ? lower + lower : lower) + (formWideRightSide ? upper + upper : upper); |
287 | map.put(lower, components[row*2]); |
288 | if (!(rightComponent instanceof DummyComponent)) |
289 | map.put(upper, rightComponent); |
290 | } |
291 | lines[numRows] = lines[numRows+1] = (formWideLeftSide ? " " : " ") + (formWideRightSide ? " " : " "); |
292 | } else if (template == LEFT_ALIGNED_ROW) { |
293 | lines = new String[] { makeSingleRow(container) + RIGHT_CHAR + RIGHT_CHAR }; |
294 | } else if (template == CENTERED_ROW) { |
295 | lines = new String[] { "" + LEFT_CHAR + LEFT_CHAR + makeSingleRow(container) + RIGHT_CHAR + RIGHT_CHAR }; |
296 | } else if (template == RIGHT_ALIGNED_ROW) { |
297 | lines = new String[] { "" + LEFT_CHAR + LEFT_CHAR + makeSingleRow(container) }; |
298 | } |
299 | } |
300 | |
301 | private String makeSingleRow(Container container) { |
302 | Component[] components = container.getComponents(); |
303 | StringBuffer buf = new StringBuffer(); |
304 | map.clear(); |
305 | for (int i = 0; i < components.length; i++) { |
306 | String s = String.valueOf(makeAlternateIndexChar(i)); |
307 | map.put(s, components[i]); |
308 | buf.append(s); |
309 | } |
310 | return buf.toString(); |
311 | } |
312 | |
313 | private static void determineStarts(RC[] rcs, int start, int totalSize, int spacing) { |
314 | int minTotal = rcs[rcs.length - 1].minEnd; |
315 | double weightSum = rcs[rcs.length - 1].weightSum; |
316 | //System.out.println("totalSize="+totalSize+",minTotal="+minTotal+",weightSum="+weightSum); |
317 | int spare = (int) ((totalSize - minTotal) / (weightSum == 0.0 ? 1.0 : weightSum)); |
318 | int x = start, minSum = 0; |
319 | double prevWeightSum = 0.0; |
320 | for (int i = 0; i < rcs.length; i++) { |
321 | int width = rcs[i].minEnd - minSum + (int) ((rcs[i].weightSum - prevWeightSum) * spare) - spacing; |
322 | //System.out.println("i="+i+",prevws="+prevWeightSum+",ws="+rcs[i].weightSum+",min="+rcs[i].min+",width="+width); |
323 | rcs[i].start = x; |
324 | x += width + spacing; |
325 | prevWeightSum = rcs[i].weightSum; |
326 | minSum = rcs[i].minEnd; |
327 | } |
328 | } |
329 | |
330 | public void addLayoutComponent(String s, Component component) { |
331 | map.put(s, component); |
332 | } |
333 | |
334 | public Dimension minimumLayoutSize(Container container) { |
335 | prepareLayout(container); |
336 | Insets insets = getInsets(container); |
337 | Dimension result = new Dimension( |
338 | insets.left + cols[cols.length - 1].minEnd + insets.right - spacingX, |
339 | insets.top + rows[rows.length - 1].minEnd + insets.bottom - spacingY); |
340 | return result; |
341 | } |
342 | |
343 | private Insets getInsets(Container container) { |
344 | Insets insets = container.getInsets(); |
345 | return new Insets(insets.top + insetTop, |
346 | insets.left + insetLeft, |
347 | insets.bottom + insetBottom, |
348 | insets.right + insetRight); |
349 | } |
350 | |
351 | public Dimension preferredLayoutSize(Container container) { |
352 | return minimumLayoutSize(container); |
353 | } |
354 | |
355 | public LetterLayout setSpacing(int x, int y) { |
356 | spacingX = x; |
357 | spacingY = y; |
358 | return this; |
359 | } |
360 | |
361 | public LetterLayout setSpacing(int spacing) { |
362 | return setSpacing(spacing, spacing); |
363 | } |
364 | |
365 | public LetterLayout setBorder(int top, int left, int bottom, int right) { |
366 | insetTop = top; |
367 | insetLeft = left; |
368 | insetBottom = bottom; |
369 | insetRight = right; |
370 | return this; |
371 | } |
372 | |
373 | public LetterLayout setBorder(int inset) { |
374 | return setBorder(inset, inset, inset, inset); |
375 | } |
376 | |
377 | public LetterLayout setTopBorder(int inset) { |
378 | insetTop = inset; |
379 | return this; |
380 | } |
381 | |
382 | /** |
383 | * layout components from top to bottom; add components without letters! |
384 | */ |
385 | public static LetterLayout stalactite() { |
386 | return new LetterLayout(STALACTITE); |
387 | } |
388 | |
389 | /** |
390 | * layout components from left to right; add components without letters! |
391 | */ |
392 | public static LetterLayout leftAlignedRow() { |
393 | return new LetterLayout(LEFT_ALIGNED_ROW); |
394 | } |
395 | |
396 | public static LetterLayout leftAlignedRow(int spacing) { |
397 | return leftAlignedRow().setSpacing(spacing); |
398 | } |
399 | |
400 | /** |
401 | * layout components from left to right, center in container; add components without letters! |
402 | */ |
403 | public static LetterLayout centeredRow() { |
404 | return new LetterLayout(CENTERED_ROW); |
405 | } |
406 | |
407 | public static LetterLayout rightAlignedRow() { |
408 | return new LetterLayout(RIGHT_ALIGNED_ROW); |
409 | } |
410 | |
411 | public static JPanel rightAlignedRowPanel(JComponent... components) { |
412 | return makePanel(new LetterLayout(RIGHT_ALIGNED_ROW), components); |
413 | } |
414 | |
415 | private static JPanel makePanel(LetterLayout letterLayout, JComponent[] components) { |
416 | JPanel panel = new JPanel(letterLayout); |
417 | for (JComponent component : components) { |
418 | panel.add(component); |
419 | } |
420 | return panel; |
421 | } |
422 | |
423 | /** |
424 | * layout components from top to bottom; two components per row |
425 | */ |
426 | public static LetterLayout form() { |
427 | LetterLayout letterLayout = new LetterLayout(FORM); |
428 | letterLayout.formWideLeftSide = true; |
429 | letterLayout.formWideRightSide = true; |
430 | return letterLayout; |
431 | } |
432 | |
433 | /** |
434 | * layout components from top to bottom; two components per row |
435 | * left column is small, right column is wide |
436 | */ |
437 | public static LetterLayout formWideRightSide() { |
438 | LetterLayout letterLayout = new LetterLayout(FORM); |
439 | letterLayout.formWideRightSide = true; |
440 | return letterLayout; |
441 | } |
442 | |
443 | public static Component getDummyComponent() { |
444 | return new DummyComponent(); |
445 | } |
446 | |
447 | public static JPanel newPanel(String... lines) { |
448 | return new JPanel(new LetterLayout(lines)); |
449 | } |
450 | |
451 | public boolean isDebug() { |
452 | return debug; |
453 | } |
454 | |
455 | public void setDebug(boolean debug) { |
456 | this.debug = debug; |
457 | } |
458 | |
459 | public static char makeIndexChar(int idx) { |
460 | return (char) ('a' + idx*2); |
461 | } |
462 | |
463 | public static char makeAlternateIndexChar(int idx) { |
464 | return (char) ('b' + idx*2); |
465 | } |
466 | |
467 | public static char LEFT_CHAR = ',', RIGHT_CHAR = '.'; |
468 | |
469 | public static void main(String[] args) { |
470 | System.out.println((int) makeIndexChar(0)); |
471 | System.out.println((int) makeAlternateIndexChar(0)); |
472 | System.out.println((int) makeIndexChar(32000)); |
473 | System.out.println((int) makeAlternateIndexChar(32000)); |
474 | System.out.println((int) LEFT_CHAR); |
475 | System.out.println((int) RIGHT_CHAR); |
476 | } |
477 | } |
478 | |
479 | class CenteredLine extends JPanel { |
480 | public CenteredLine(Component... components) { |
481 | setLayout(LetterLayout.centeredRow()); |
482 | for (Component component : components) |
483 | add(component); |
484 | } |
485 | |
486 | public void add(String text) { |
487 | add(new JLabel(text)); |
488 | } |
489 | } |
Began life as a copy of #613
download show line numbers debug dex old transpilations
Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1000270 |
Snippet name: | JavaX IDE v1 (non-functional, just shows the window) |
Eternal ID of this version: | #1000270/1 |
Text MD5: | df4c5fa0b31c0ba0335e1f2815a0cbdf |
Author: | stefan |
Category: | javax |
Type: | JavaX source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2015-05-10 16:50:03 |
Source code size: | 15583 bytes / 489 lines |
Pitched / IR pitched: | No / Yes |
Views / Downloads: | 781 / 630 |
Referenced in: | [show references] |