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