3636import de .nmichael .efa .Daten ;
3737import de .nmichael .efa .gui .BaseDialog ;
3838import de .nmichael .efa .gui .BaseFrame ;
39+ import de .nmichael .efa .gui .EfaGuiUtils ;
3940import de .nmichael .efa .gui .ImagesAndIcons ;
4041import de .nmichael .efa .gui .util .RoundedBorder ;
4142import de .nmichael .efa .gui .util .RoundedLabel ;
@@ -124,6 +125,7 @@ public class ItemTypeItemList extends ItemType {
124125 private JScrollPane scrollPane ;
125126 private JLabel titlelabel ;
126127 private JButton addButton ;
128+ private Hashtable <JButton ,Integer > addButtons ;
127129 private Hashtable <JButton ,Integer > delButtons ;
128130 private Hashtable <JButton ,Integer > upButtons ;
129131 private Hashtable <JButton ,Integer > downButtons ;
@@ -182,16 +184,29 @@ public IItemType copyOf() {
182184 return copy ;
183185 }
184186
187+ /**
188+ * Add items at the end of the list.
189+ * @param items Items to be added.
190+ */
185191 public void addItems (IItemType [] items ) {
186- int idx = this .items .size ();
192+ addItems (items , this .items .size ());
193+ }
194+
195+ /**
196+ * Add Items below an item on the list.
197+ * @param items Items to be added.
198+ * @param afterIndex
199+ */
200+ public void addItems (IItemType [] items , int afterIndex ) {
201+ int idxForName = this .items .size ();
187202 lastItemFocus = null ;
188203 for (IItemType item : items ) {
189204 // check if the subitem's name already consists of the prefixes of the CURRENT itemlist.
190205 // only add them if neccessary. this avoids very long item names when additems is used
191206 // in the context of copyOf method.
192207 String internalName = item .getName ();
193208 if (!item .getName ().startsWith (this .getName ())){
194- internalName = getName () + "_" + idx + "_" + item .getName ();
209+ internalName = getName () + "_" + idxForName + "_" + item .getName ();
195210 }
196211 itemNameMapping .put (internalName , item .getName ());
197212 item .setName (internalName );
@@ -201,7 +216,11 @@ public void addItems(IItemType[] items) {
201216 }
202217 }
203218 }
204- this .items .add (items );
219+ if (afterIndex ==0 || afterIndex >idxForName ) {
220+ this .items .add (items );
221+ } else {
222+ this .items .add (afterIndex ,items );
223+ }
205224 }
206225
207226 public void removeItems (int idx ) {
@@ -293,7 +312,6 @@ protected void iniDisplay() {
293312 // not used, everything done in displayOnGui(...)
294313 }
295314
296-
297315 public int displayOnGui (Window dlg , JPanel panel , int x , int y ) {
298316 this .dlg = dlg ;
299317
@@ -316,7 +334,7 @@ public int displayOnGui(Window dlg, JPanel panel, int x, int y) {
316334 titlelabel .setOpaque (true );
317335 titlelabel .setFont (titlelabel .getFont ().deriveFont (Font .BOLD ));
318336 titlelabel .setText (" " + getDescription ());
319-
337+
320338 // we use a roundedPanel as base element so that we can add the caption on the left,
321339 // and highlight the add button on the right with some prominent text "new" and an extra arrow icon.
322340 int curYPos = 0 ;
@@ -358,6 +376,7 @@ public int displayOnGui(Window dlg, JPanel panel, int x, int y) {
358376 addButton .addActionListener (new java .awt .event .ActionListener () {
359377 public void actionPerformed (ActionEvent e ) { addButtonHit (e ); }
360378 });
379+ EfaGuiUtils .enableAutoScrollOnFocus (addButton );
361380
362381 panel .add (addButton , new GridBagConstraints (x +xForAddDelButtons , y , 2 , 1 , 0.0 , 0.0 ,
363382 GridBagConstraints .WEST , GridBagConstraints .NONE , new Insets (padYbefore , 2 , padYafter , padXafter ), 0 , 0 ));
@@ -368,6 +387,7 @@ public int displayOnGui(Window dlg, JPanel panel, int x, int y) {
368387 curYPos ++;
369388 }
370389
390+ addButtons = new Hashtable <JButton , Integer >();
371391 delButtons = new Hashtable <JButton ,Integer >();
372392 upButtons = new Hashtable <JButton ,Integer >();
373393 downButtons = new Hashtable <JButton ,Integer >();
@@ -395,6 +415,16 @@ public int displayOnGui(Window dlg, JPanel panel, int x, int y) {
395415 delButton .addActionListener (new java .awt .event .ActionListener () {
396416 public void actionPerformed (ActionEvent e ) { delButtonHit (e ); }
397417 });
418+ EfaGuiUtils .enableAutoScrollOnFocus (delButton );
419+
420+ JButton addButtonAtItem = new JButton ();
421+ addButtonAtItem .setIcon (BaseFrame .getIcon ("menu_plus.gif" ));
422+ addButtonAtItem .setMargin (new Insets (0 ,0 ,0 ,0 ));
423+ Dialog .setPreferredSize (addButtonAtItem , 19 , 19 );
424+ addButtonAtItem .addActionListener (new java .awt .event .ActionListener () {
425+ public void actionPerformed (ActionEvent e ) { addButtonAfterPositionHit (e ); }
426+ });
427+ EfaGuiUtils .enableAutoScrollOnFocus (addButtonAtItem );
398428
399429 JButton upButton =null ;
400430 JButton downButton = null ;
@@ -407,13 +437,17 @@ public int displayOnGui(Window dlg, JPanel panel, int x, int y) {
407437 upButton .addActionListener (new java .awt .event .ActionListener () {
408438 public void actionPerformed (ActionEvent e ) { upButtonHit (e ); }
409439 });
440+ EfaGuiUtils .enableAutoScrollOnFocus (upButton );
441+
410442 downButton = new JButton ();
411443 downButton .setIcon (ImagesAndIcons .getIcon (ImagesAndIcons .ARROW_DOWN ));
412444 downButton .setMargin (new Insets (0 ,0 ,0 ,0 ));
413445 Dialog .setPreferredSize (downButton , 19 , 19 );
414446 downButton .addActionListener (new java .awt .event .ActionListener () {
415447 public void actionPerformed (ActionEvent e ) { downButtonHit (e ); }
416448 });
449+ EfaGuiUtils .enableAutoScrollOnFocus (downButton );
450+
417451 /*
418452 * [label] [upbutton][downbutton] [delete]
419453 */
@@ -425,15 +459,19 @@ public int displayOnGui(Window dlg, JPanel panel, int x, int y) {
425459
426460 panel .add (delButton , new GridBagConstraints (x +xForAddDelButtons , y +curYPos , 1 , 1 , 0.0 , 0.0 ,
427461 GridBagConstraints .WEST , GridBagConstraints .NONE , new Insets ((label ==null ? 0 : padYbetween ), 2 , (label ==null ? 0 : padYbetween ), 0 ), 0 , 0 ));
462+
463+ panel .add (addButtonAtItem , new GridBagConstraints (x +(xForAddDelButtons *2 ), y +curYPos , 1 , 1 , 0.0 , 0.0 ,
464+ GridBagConstraints .WEST , GridBagConstraints .NONE , new Insets ((label ==null ? 0 : padYbetween ), 2 , (label ==null ? 0 : padYbetween ), 0 ), 0 , 0 ));
428465
429- panel .add (upButton , new GridBagConstraints (x +xForAddDelButtons + xForAddDelButtons , y +curYPos , 1 , 1 , 0.0 , 0.0 ,
466+ panel .add (upButton , new GridBagConstraints (x +( xForAddDelButtons * 3 ) , y +curYPos , 1 , 1 , 0.0 , 0.0 ,
430467 GridBagConstraints .WEST , GridBagConstraints .NONE , new Insets ((label ==null ? 0 : padYbetween ), 2 , (label ==null ? 0 : padYbetween ), 0 ), 0 , 0 ));
431468
432- panel .add (downButton , new GridBagConstraints (x +xForAddDelButtons + xForAddDelButtons + xForAddDelButtons , y +curYPos , 1 , 1 , 0.0 , 0.0 ,
469+ panel .add (downButton , new GridBagConstraints (x +( xForAddDelButtons * 4 ) , y +curYPos , 1 , 1 , 0.0 , 0.0 ,
433470 GridBagConstraints .WEST , GridBagConstraints .NONE , new Insets ((label ==null ? 0 : padYbetween ), 2 , (label ==null ? 0 : padYbetween ), 0 ), 0 , 0 ));
434471
435472
436473 delButtons .put (delButton , iCurrentItemListIndex );
474+ addButtons .put (addButtonAtItem , iCurrentItemListIndex );
437475 upButtons .put (upButton , iCurrentItemListIndex );
438476 downButtons .put (downButton , iCurrentItemListIndex );
439477
@@ -448,6 +486,11 @@ public int displayOnGui(Window dlg, JPanel panel, int x, int y) {
448486 panel .add (delButton , new GridBagConstraints (x +xForAddDelButtons , y +curYPos , 1 , 1 , 0.0 , 0.0 ,
449487 GridBagConstraints .WEST , GridBagConstraints .NONE , new Insets ((label ==null ? 0 : padYbetween ), 2 , (label ==null ? 0 : padYbetween ), 0 ), 0 , 0 ));
450488 delButtons .put (delButton , iCurrentItemListIndex );
489+
490+ panel .add (addButtonAtItem , new GridBagConstraints (x +(xForAddDelButtons *2 ), y +curYPos , 1 , 1 , 0.0 , 0.0 ,
491+ GridBagConstraints .WEST , GridBagConstraints .NONE , new Insets ((label ==null ? 0 : padYbetween ), 2 , (label ==null ? 0 : padYbetween ), 0 ), 0 , 0 ));
492+ addButtons .put (addButtonAtItem , iCurrentItemListIndex );
493+
451494 }
452495
453496 lastItemStart = (label != null ? label : delButton );
@@ -468,6 +511,7 @@ public int displayOnGui(Window dlg, JPanel panel, int x, int y) {
468511 item .setDescription (descr + " " + (iCurrentItemListIndex +1 ));
469512 }
470513 int plusY = item .displayOnGui (dlg , panel , myX , y +curYPos );
514+ EfaGuiUtils .enableAutoScrollOnFocus (item .getComponent ());
471515 if (item instanceof ItemTypeLabelTextfield ) { //neccessary for efaBaseFrameMultisession
472516 ((ItemTypeLabelTextfield ) item ).restoreBackgroundColor ();
473517 }
@@ -502,11 +546,12 @@ private void addButtonHit(ActionEvent e) {
502546 ((BaseDialog )dlg ).updateGui ();
503547 if (lastItemStart != null ) {
504548 ((BaseDialog )dlg ).getScrollPane ().scrollRectToVisible (lastItemStart .getBounds ());
505- }
506- if ( lastItemFocus != null ) {
507- lastItemFocus . requestFocus ();
549+ if ( lastItemFocus != null ) {
550+ lastItemFocus . requestFocus ();
551+ }
508552 }
509553 }
554+
510555 }
511556
512557 private void delButtonHit (ActionEvent e ) {
@@ -530,6 +575,34 @@ private void delButtonHit(ActionEvent e) {
530575 }
531576 }
532577
578+ private void addButtonAfterPositionHit (ActionEvent e ) {
579+ //no up action neccessary if there is no element
580+ if (items .size ()<1 ) {return ;}
581+
582+ int idx = addButtons .get (e .getSource ());
583+ if (idx < 0 || idx >= items .size ()) {
584+ return ;
585+ }
586+
587+ getValueFromGui (); //save the current element status
588+
589+ //add the new item at the bottom
590+ IItemType [] newitems = itemFactory .getDefaultItems (getName ());
591+ addItems (newitems ,idx +1 );
592+
593+ changed = true ;
594+ if (dlg instanceof BaseDialog ) {
595+ ((BaseDialog )dlg ).updateGui ();
596+ if (lastItemStart != null ) {
597+ ((BaseDialog )dlg ).getScrollPane ().scrollRectToVisible (lastItemStart .getBounds ());
598+ if (lastItemFocus != null ) {
599+ lastItemFocus .requestFocus ();
600+ }
601+ }
602+ }
603+ }
604+
605+
533606 private void upButtonHit (ActionEvent e ) {
534607 //no up action neccessary if there is only one element
535608 if (items .size ()<=1 ) {return ;}
@@ -547,6 +620,12 @@ private void upButtonHit(ActionEvent e) {
547620 ((BaseDialog )dlg ).updateGui ();
548621 }
549622
623+ try {
624+ lastItemFocus = ((IItemType [])items .get (idx ))[0 ];
625+ } catch (Exception e1 ) {
626+ Logger .logdebug (e1 );
627+ lastItemFocus = null ;
628+ }
550629 if (lastItemFocus != null ) {
551630 lastItemFocus .requestFocus ();
552631 }
0 commit comments