Mercurial Hosting > junotu
annotate src/junotu/TabEdit.java @ 20:981d634a05ac
Started on my own layout manager for TabEdit
Current problems:
1. If window is resized to be smaller than it was, my layout manager isn't refreshed. Resulting in the appearance of horizontal scrollbar.
2. Tags (flow layout) seem to lie about their preffered size.
author | Fox |
---|---|
date | Wed, 04 May 2022 18:55:18 +0200 |
parents | 736bcfba1d2d |
children | b66fc08656c1 |
rev | line source |
---|---|
1 | 1 package junotu; |
2 | |
3 import java.awt.Dimension; | |
4 import java.awt.Font; | |
8 | 5 import java.awt.event.ActionEvent; |
6 import java.awt.event.ActionListener; | |
15 | 7 import java.awt.event.FocusEvent; |
8 import java.awt.event.FocusListener; | |
1 | 9 |
13 | 10 import javax.swing.SwingUtilities; |
9
dd51276f95a2
Dynamically change window title on card edit tab to reflect card title
Fox
parents:
8
diff
changeset
|
11 import javax.swing.event.DocumentListener; |
dd51276f95a2
Dynamically change window title on card edit tab to reflect card title
Fox
parents:
8
diff
changeset
|
12 import javax.swing.event.DocumentEvent; |
dd51276f95a2
Dynamically change window title on card edit tab to reflect card title
Fox
parents:
8
diff
changeset
|
13 |
1 | 14 import javax.swing.JPanel; |
15 | |
16 import java.awt.BorderLayout; | |
15 | 17 import java.awt.FlowLayout; |
1 | 18 |
19 import javax.swing.Box; | |
20 import javax.swing.JButton; | |
21 import javax.swing.JTextField; | |
22 import javax.swing.JTextArea; | |
23 import javax.swing.JScrollPane; | |
13 | 24 import javax.swing.JScrollBar; |
1 | 25 |
8 | 26 import junotu.Main; |
27 import junotu.Database; | |
28 import junotu.Window.Tab; | |
15 | 29 import junotu.GUIToolbox; |
14 | 30 import junotu.Card; |
8 | 31 |
1 | 32 public class TabEdit extends JPanel { |
8 | 33 |
15 | 34 private class TagWidget extends JButton { |
35 | |
36 public String tag; | |
37 public Object value; | |
38 | |
39 public TagWidget( String tag, Object value ) { | |
40 super(); | |
41 this.tag = tag; | |
42 this.value = value; | |
43 TagWidget ref = this; | |
44 | |
45 addActionListener( | |
46 new ActionListener() { | |
47 @Override | |
48 public void actionPerformed( ActionEvent e ) | |
49 { | |
50 tagEdit( ref ); | |
51 } | |
52 } | |
53 ); | |
54 | |
55 update(); | |
56 | |
57 } | |
58 | |
59 public void update() | |
60 { | |
61 if( value != null ) { | |
62 setText( tag+": "+value.toString() ); | |
63 } else { | |
64 setText( tag ); | |
65 } | |
66 } | |
67 | |
68 } | |
69 | |
70 private Card card = null; | |
8 | 71 private boolean newCard = true; |
15 | 72 |
73 private TagWidget editedTag = null; | |
74 private JTextField editedTagField; | |
8 | 75 |
13 | 76 private JScrollPane scroll; |
77 | |
8 | 78 private JTextField title; |
79 private JTextArea content; | |
15 | 80 private JPanel tags; |
81 private JButton addTag; | |
12 | 82 |
13 | 83 private JButton delete; |
8 | 84 |
1 | 85 public TabEdit() |
86 { | |
87 this.setLayout( new BorderLayout() ); | |
88 | |
20 | 89 JPanel scrollContent = new JPanel( new GUIToolbox.CardEditLayout() ); |
15 | 90 scroll = new JScrollPane( scrollContent ); |
91 title = new JTextField(); | |
92 content = new JTextArea(); | |
93 tags = new JPanel(); | |
94 editedTagField = new JTextField(); | |
95 addTag = new JButton("+"); | |
1 | 96 |
97 Box bottom = Box.createHorizontalBox(); | |
12 | 98 JButton back = new JButton("Cancel"); |
99 delete = new JButton("Delete"); | |
100 JButton save = new JButton("Save"); | |
1 | 101 |
15 | 102 tags.setLayout( new FlowLayout() ); |
103 | |
1 | 104 title.setFont( new Font( "Monospaced", Font.PLAIN, 32 ) ); |
105 content.setFont( new Font( "Monospaced", Font.PLAIN, 16 ) ); | |
15 | 106 editedTagField.setFont( new Font( "Monospaced", Font.PLAIN, 12 ) ); |
1 | 107 |
108 scroll.getVerticalScrollBar().setUnitIncrement(16); | |
109 | |
15 | 110 title.setMaximumSize( new Dimension( Integer.MAX_VALUE, 64 ) ); |
111 //content.setMaximumSize( new Dimension( Integer.MAX_VALUE, Integer.MAX_VALUE ) ); | |
20 | 112 content.setLineWrap( true ); |
15 | 113 /* TODO: Figure out tags layout mess. */ |
114 tags.setPreferredSize( new Dimension( 16, 256 ) ); | |
115 tags.setMaximumSize( new Dimension( Integer.MAX_VALUE, Integer.MAX_VALUE ) ); | |
1 | 116 |
117 this.add( scroll, BorderLayout.CENTER ); | |
118 scrollContent.add( title ); | |
20 | 119 //scrollContent.add( Box.createVerticalStrut(10) ); |
1 | 120 scrollContent.add( content ); |
15 | 121 scrollContent.add( tags ); |
1 | 122 |
123 this.add( bottom, BorderLayout.SOUTH ); | |
124 bottom.add( back ); | |
125 bottom.add( Box.createHorizontalGlue() ); | |
12 | 126 bottom.add( delete ); |
1 | 127 bottom.add( save ); |
128 | |
129 //scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); | |
9
dd51276f95a2
Dynamically change window title on card edit tab to reflect card title
Fox
parents:
8
diff
changeset
|
130 |
8 | 131 back.addActionListener( |
132 new ActionListener() { | |
133 @Override | |
134 public void actionPerformed( ActionEvent e ) | |
135 { | |
136 buttonClickedBack(); | |
137 } | |
138 } | |
139 ); | |
140 | |
12 | 141 delete.addActionListener( |
142 new ActionListener() { | |
143 @Override | |
144 public void actionPerformed( ActionEvent e ) | |
145 { | |
146 buttonClickedDelete(); | |
147 } | |
148 } | |
15 | 149 ); |
12 | 150 |
8 | 151 save.addActionListener( |
152 new ActionListener() { | |
153 @Override | |
154 public void actionPerformed( ActionEvent e ) | |
155 { | |
156 buttonClickedSave(); | |
157 } | |
158 } | |
15 | 159 ); |
160 | |
161 addTag.addActionListener( | |
162 new ActionListener() { | |
163 @Override | |
164 public void actionPerformed( ActionEvent e ) | |
165 { | |
166 tagAdd(); | |
167 } | |
168 } | |
169 ); | |
170 | |
171 editedTagField.addFocusListener( | |
172 new FocusListener() | |
173 { | |
174 @Override | |
175 public void focusGained(FocusEvent e) | |
176 { | |
177 } | |
178 | |
179 @Override | |
180 public void focusLost(FocusEvent e) | |
181 { | |
182 tagCommit(); | |
183 } | |
184 } | |
185 ); | |
9
dd51276f95a2
Dynamically change window title on card edit tab to reflect card title
Fox
parents:
8
diff
changeset
|
186 |
dd51276f95a2
Dynamically change window title on card edit tab to reflect card title
Fox
parents:
8
diff
changeset
|
187 title.getDocument().addDocumentListener( |
dd51276f95a2
Dynamically change window title on card edit tab to reflect card title
Fox
parents:
8
diff
changeset
|
188 new DocumentListener() |
dd51276f95a2
Dynamically change window title on card edit tab to reflect card title
Fox
parents:
8
diff
changeset
|
189 { |
dd51276f95a2
Dynamically change window title on card edit tab to reflect card title
Fox
parents:
8
diff
changeset
|
190 @Override |
dd51276f95a2
Dynamically change window title on card edit tab to reflect card title
Fox
parents:
8
diff
changeset
|
191 public void changedUpdate( DocumentEvent e ) |
dd51276f95a2
Dynamically change window title on card edit tab to reflect card title
Fox
parents:
8
diff
changeset
|
192 { |
dd51276f95a2
Dynamically change window title on card edit tab to reflect card title
Fox
parents:
8
diff
changeset
|
193 updateTitle(); |
dd51276f95a2
Dynamically change window title on card edit tab to reflect card title
Fox
parents:
8
diff
changeset
|
194 } |
dd51276f95a2
Dynamically change window title on card edit tab to reflect card title
Fox
parents:
8
diff
changeset
|
195 @Override |
dd51276f95a2
Dynamically change window title on card edit tab to reflect card title
Fox
parents:
8
diff
changeset
|
196 public void removeUpdate( DocumentEvent e ) |
dd51276f95a2
Dynamically change window title on card edit tab to reflect card title
Fox
parents:
8
diff
changeset
|
197 { |
dd51276f95a2
Dynamically change window title on card edit tab to reflect card title
Fox
parents:
8
diff
changeset
|
198 updateTitle(); |
dd51276f95a2
Dynamically change window title on card edit tab to reflect card title
Fox
parents:
8
diff
changeset
|
199 } |
dd51276f95a2
Dynamically change window title on card edit tab to reflect card title
Fox
parents:
8
diff
changeset
|
200 @Override |
dd51276f95a2
Dynamically change window title on card edit tab to reflect card title
Fox
parents:
8
diff
changeset
|
201 public void insertUpdate( DocumentEvent e ) |
dd51276f95a2
Dynamically change window title on card edit tab to reflect card title
Fox
parents:
8
diff
changeset
|
202 { |
dd51276f95a2
Dynamically change window title on card edit tab to reflect card title
Fox
parents:
8
diff
changeset
|
203 updateTitle(); |
dd51276f95a2
Dynamically change window title on card edit tab to reflect card title
Fox
parents:
8
diff
changeset
|
204 } |
dd51276f95a2
Dynamically change window title on card edit tab to reflect card title
Fox
parents:
8
diff
changeset
|
205 } |
dd51276f95a2
Dynamically change window title on card edit tab to reflect card title
Fox
parents:
8
diff
changeset
|
206 ); |
1 | 207 |
208 } | |
8 | 209 |
13 | 210 private void scrollTop() |
211 { | |
212 JScrollBar scrollbar = scroll.getVerticalScrollBar(); | |
213 scrollbar.setValue(0); | |
214 } | |
215 | |
216 private void scrollBottom() | |
217 { | |
218 JScrollBar scrollbar = scroll.getVerticalScrollBar(); | |
219 int maximum = scrollbar.getMaximum()-scrollbar.getVisibleAmount(); | |
220 scrollbar.setValue(maximum); | |
221 } | |
222 | |
8 | 223 public void cardCreate() |
224 { | |
225 newCard = true; | |
15 | 226 card = new Card(); |
9
dd51276f95a2
Dynamically change window title on card edit tab to reflect card title
Fox
parents:
8
diff
changeset
|
227 updateTitle(); |
12 | 228 delete.setVisible(false); |
15 | 229 updateTags(); |
8 | 230 } |
231 | |
232 public void cardEdit( Card card ) | |
233 { | |
234 newCard = false; | |
15 | 235 this.card = card; |
14 | 236 title.setText( card.titleGet() ); |
237 content.setText( card.contentGet() ); | |
9
dd51276f95a2
Dynamically change window title on card edit tab to reflect card title
Fox
parents:
8
diff
changeset
|
238 updateTitle(); |
12 | 239 delete.setVisible(true); |
15 | 240 updateTags(); |
13 | 241 SwingUtilities.invokeLater( |
242 new Runnable() | |
243 { | |
244 public void run() | |
245 { | |
246 scrollTop(); | |
247 } | |
248 } | |
249 ); | |
8 | 250 } |
251 | |
15 | 252 private void reset() |
8 | 253 { |
254 title.setText(""); | |
255 content.setText(""); | |
15 | 256 card = null; |
257 } | |
258 | |
16 | 259 private void updateTitle() |
260 { | |
261 Window window = (Window)this.getTopLevelAncestor(); | |
262 | |
263 String text = title.getText(); | |
264 String action = newCard ? "Create" : "Edit"; | |
265 | |
266 if( text.length() > 0 ) { | |
267 window.setTitle( window.preferredTitle( action+": "+text ) ); | |
268 } else { | |
269 window.setTitle( window.preferredTitle( action ) ); | |
270 } | |
271 | |
272 } | |
273 | |
15 | 274 private void updateTags() |
275 { | |
276 tags.removeAll(); | |
277 for( String tag : card.tagNames() ) { | |
16 | 278 if( tag.startsWith("_") ) { |
279 continue; | |
280 } | |
15 | 281 for( Object value : card.tagValues( tag ) ) { |
282 tags.add( new TagWidget( tag, value ) ); | |
283 } | |
284 } | |
285 tags.add( addTag ); | |
286 tags.validate(); | |
287 tags.repaint(); | |
288 } | |
289 | |
290 private void tagAdd() | |
291 { | |
292 tags.add( editedTagField, GUIToolbox.componentGetIndex( addTag ) ); | |
293 editedTagField.setText( "" ); | |
294 editedTagField.setColumns(12); | |
295 editedTagField.grabFocus(); | |
296 tags.validate(); | |
297 tags.repaint(); | |
298 System.out.print( "Opened new tag for editing.\n" ); | |
299 } | |
300 | |
301 private void tagEdit( TagWidget tagWidget ) | |
302 { | |
303 editedTag = tagWidget; | |
304 tags.add( editedTagField, GUIToolbox.componentGetIndex( editedTag ) ); | |
305 if( editedTag.value != null ) { | |
306 editedTagField.setText( editedTag.tag+":"+editedTag.value.toString() ); | |
307 } else { | |
308 editedTagField.setText( editedTag.tag ); | |
309 } | |
310 editedTagField.setColumns(0); | |
311 editedTagField.grabFocus(); | |
312 editedTag.setVisible(false); | |
313 tags.validate(); | |
314 tags.repaint(); | |
315 System.out.print( "Opened existing tag for editing.\n" ); | |
316 } | |
317 | |
318 private void tagCommit() | |
319 { | |
320 | |
321 String newTag; | |
322 Object newValue; | |
323 | |
324 { | |
325 String[] split = editedTagField.getText().split( ":", 2 ); | |
326 newTag = split[0]; | |
327 newValue = split.length > 1 ? split[1] : null; | |
328 } | |
329 | |
330 /* Either editing tag, or adding a new one. */ | |
331 if( editedTag != null ) { | |
332 | |
333 String oldTag = editedTag.tag; | |
334 Object oldValue = editedTag.value; | |
335 | |
336 logTagChange( oldTag, oldValue, newTag, newValue ); | |
337 | |
338 if( oldTag.equals(newTag) ) { | |
339 card.tagValueReplace( oldTag, oldValue, newValue ); | |
340 editedTag.value = newValue; | |
341 editedTag.update(); | |
342 } else { /* Replace tag with another one. */ | |
343 | |
344 card.tagValueRemove( oldTag, oldValue ); | |
345 card.tagValueAdd( newTag, newValue ); | |
346 | |
347 if( !newTag.equals("") ) { | |
348 editedTag.tag = newTag; | |
349 editedTag.value = newValue; | |
350 editedTag.update(); | |
351 } else { | |
352 tags.remove( editedTag ); | |
353 } | |
354 | |
355 } | |
356 | |
357 editedTag.setVisible( true ); | |
358 | |
359 } else { /* Adding new tag (value). */ | |
360 if( !newTag.equals("") ) { | |
361 card.tagValueAdd( newTag, newValue ); | |
362 tags.add( new TagWidget( newTag, newValue ), GUIToolbox.componentGetIndex( addTag )-1 ); | |
363 logTagChange( "", null, newTag, newValue ); | |
364 } else { | |
365 logTagChange( "", null, "", null ); | |
366 } | |
367 } | |
368 | |
369 editedTagField.setText( "" ); | |
370 tags.remove( editedTagField ); | |
371 | |
372 editedTag = null; | |
373 | |
374 tags.validate(); | |
375 tags.repaint(); | |
376 | |
377 } | |
378 | |
379 private void logTagChange( String oldTag, Object oldValue, String newTag, Object newValue ) | |
380 { | |
381 System.out.print( "Comitted changes to tag: " ); | |
382 if( oldTag.equals("") ) { /* Creating tag. */ | |
383 if( !newTag.equals("") ) { | |
384 if( newValue == null ) { /* No value. */ | |
385 System.out.print( "Added tag '"+newTag+"' (with no value)\n" ); | |
386 } else { /* Has value. */ | |
387 System.out.print( "Added tag '"+newTag+"' with value '"+newValue.toString()+"'\n" ); | |
388 } | |
389 } else { | |
390 System.out.print( "No changes.\n" ); | |
391 } | |
392 } else { /* Editing tag. */ | |
393 if( oldTag.equals(newTag) ) { /* Same tag. */ | |
394 if( oldValue == null ) { | |
395 if( newValue == null ) { /* Clear before, clear now. */ | |
396 System.out.print( "No changes (tag '"+oldTag+"' with no value)\n" ); | |
397 } else { /* Assigned value. */ | |
398 System.out.print( "Assigned value of tag '"+oldTag+"' to '"+newValue.toString()+"'\n" ); | |
399 } | |
400 } else { | |
401 if( newValue == null ) { /* Clearing value. */ | |
402 System.out.print( "Cleared value of tag '"+oldTag+"' (was '" | |
403 +oldValue.toString()+"')\n" ); | |
404 } else if( oldValue.equals(newValue) ) { /* Value is the same. */ | |
405 System.out.print( "No changes (tag '"+oldTag+"' with value '"+oldValue+"')\n" ); | |
406 } else { /* Changing value. */ | |
407 System.out.print( "Changed value of tag '"+oldTag+"' from '" | |
408 +oldValue.toString()+"' to '"+newValue.toString()+"'\n" ); | |
409 } | |
410 } | |
411 } else { /* Replaced tag. */ | |
412 if( newTag.equals("") ) { /* Removing tag. */ | |
413 if( oldValue == null ) { | |
414 System.out.print( "Removed tag '"+oldTag+"' (with no value)\n" ); | |
415 } else { | |
416 System.out.print( "Removed tag '"+oldTag+"' with value '"+oldValue.toString()+"'\n" ); | |
417 } | |
418 } else { | |
419 if( oldValue == null ) { | |
420 if( newValue == null ) { | |
421 System.out.print( | |
422 "Renamed tag '"+oldTag+"' -> '" | |
423 +newTag+"'\n" ); | |
424 } else { | |
425 System.out.print( | |
426 "Changed tag '"+oldTag+"' -> '" | |
427 +newTag+"': '"+newValue.toString()+"'\n" ); | |
428 } | |
429 } else { | |
430 if( newValue == null ) { | |
431 System.out.print( | |
432 "Changed tag '"+oldTag+"': '"+oldValue.toString()+"' -> '" | |
433 +newTag+"'\n" ); | |
434 } else { | |
435 System.out.print( | |
436 "Changed tag '"+oldTag+"': '"+oldValue.toString()+"' -> '" | |
437 +newTag+"': '"+newValue.toString()+"'\n" ); | |
438 } | |
439 } | |
440 } | |
441 } | |
442 } | |
8 | 443 } |
9
dd51276f95a2
Dynamically change window title on card edit tab to reflect card title
Fox
parents:
8
diff
changeset
|
444 |
16 | 445 |
8 | 446 private void buttonClickedBack() |
447 { | |
11 | 448 Window window = (Window)this.getTopLevelAncestor(); |
15 | 449 reset(); |
450 window.tabSwitch( Tab.SEARCH ); | |
8 | 451 } |
12 | 452 |
453 private void buttonClickedDelete() | |
454 { | |
455 | |
456 if( newCard ) { | |
457 return; | |
458 } | |
459 | |
460 try { | |
15 | 461 Main.database.cardDelete( card.identifierGet() ); |
12 | 462 } catch( Exception e ) { |
463 throw new RuntimeException(e); | |
464 } | |
465 | |
466 Window window = (Window)this.getTopLevelAncestor(); | |
467 window.tabSearch.search(); | |
15 | 468 reset(); |
12 | 469 window.tabSwitch( Tab.SEARCH ); |
470 | |
471 } | |
8 | 472 |
473 private void buttonClickedSave() | |
474 { | |
475 | |
14 | 476 card.titleSet( title.getText() ); |
477 card.contentSet( content.getText() ); | |
8 | 478 |
479 try { | |
480 if( newCard ) { | |
481 Main.database.cardAdd( card ); | |
482 } else { | |
483 Main.database.cardUpdate( card ); | |
484 } | |
485 } catch( Exception e ) { | |
12 | 486 throw new RuntimeException(e); |
8 | 487 } |
488 | |
11 | 489 Window window = (Window)this.getTopLevelAncestor(); |
490 window.tabSearch.search(); | |
15 | 491 reset(); |
11 | 492 window.tabSwitch( Tab.SEARCH ); |
8 | 493 } |
494 | |
1 | 495 } |