View Javadoc
1   /*******************************************************************************
2    * Copyhacked (H) 2012-2025.
3    * This program and the accompanying materials
4    * are made available under no term at all, use it like
5    * you want, but share and discuss it
6    * every time possible with every body.
7    *
8    * Contributors:
9    *      ron190 at ymail dot com - initial implementation
10   *******************************************************************************/
11  package com.jsql.view.swing.list;
12  
13  import com.jsql.util.I18nUtil;
14  import com.jsql.util.LogLevelUtil;
15  import com.jsql.view.swing.manager.ManagerScan;
16  import com.jsql.view.swing.text.JPopupTextArea;
17  import com.jsql.view.swing.util.I18nViewUtil;
18  import org.apache.commons.lang3.StringUtils;
19  import org.apache.logging.log4j.LogManager;
20  import org.apache.logging.log4j.Logger;
21  
22  import javax.swing.*;
23  import java.awt.*;
24  import java.awt.event.ActionEvent;
25  import java.awt.event.ActionListener;
26  import java.awt.event.MouseAdapter;
27  import java.awt.event.MouseEvent;
28  import java.util.List;
29  
30  /**
31   * Action to add a new item to a JList.
32   */
33  public class MenuActionNewValue implements ActionListener {
34      
35      private static final Logger LOGGER = LogManager.getRootLogger();
36      
37      /**
38       * List to add new items.
39       */
40      private final DnDList myList;
41      
42      /**
43       * Create action to add new item list.
44       * @param myList List to add new items.
45       */
46      public MenuActionNewValue(DnDList myList) {
47          this.myList = myList;
48      }
49      
50      @Override
51      public void actionPerformed(ActionEvent actionEvent) {
52          var panel = new JPanel(new BorderLayout());
53          final JTextArea textarea = new JPopupTextArea(new JTextArea()).getProxy();
54          var labelAddValue = new JLabel(I18nUtil.valueByKey("LIST_ADD_VALUE_LABEL") + ":");
55          panel.add(labelAddValue, BorderLayout.NORTH);
56          I18nViewUtil.addComponentForKey("LIST_ADD_VALUE_LABEL", labelAddValue);
57          panel.add(new JScrollPane(textarea));
58  
59          panel.setPreferredSize(new Dimension(600, 400));
60          panel.setMinimumSize(new Dimension(600, 400));
61          
62          textarea.addMouseListener(new MouseAdapter() {
63              @Override
64              public void mousePressed(MouseEvent e) {
65                  super.mousePressed(e);
66                  textarea.requestFocusInWindow();
67              }
68          });
69  
70          int result = -1;
71                 
72          // Unhandled NullPointerException #92858 on showOptionDialog()
73          // Unhandled IllegalArgumentException #92859 on showOptionDialog()
74          // Fix #70832: ClassCastException on showOptionDialog()
75          try {
76              result = JOptionPane.showOptionDialog(
77                  this.myList.getTopLevelAncestor(),
78                  panel,
79                  I18nUtil.valueByKey("LIST_ADD_VALUE_TITLE"),
80                  JOptionPane.OK_CANCEL_OPTION,
81                  JOptionPane.QUESTION_MESSAGE,
82                  null,
83                  new String[] {
84                      I18nUtil.valueByKey("LIST_ADD_VALUE_OK"),
85                      I18nUtil.valueByKey("LIST_ADD_VALUE_CANCEL")
86                  },
87                  I18nUtil.valueByKey("LIST_ADD_VALUE_CANCEL")
88              );
89          } catch (NullPointerException | IllegalArgumentException | ClassCastException e) {
90              LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
91          }
92  
93          if (StringUtils.isEmpty(textarea.getText()) || result != JOptionPane.YES_OPTION) {
94              return;
95          }
96  
97          var lastIndex = Math.max(this.myList.getSelectedIndex(), 0);
98          int firstIndex = lastIndex;
99          
100         if (ManagerScan.NAME.equals(this.myList.getName())) {
101             lastIndex = this.addToScanList(textarea, lastIndex);
102         } else {
103             lastIndex = this.addToList(textarea, lastIndex);
104         }
105 
106         this.myList.setSelectionInterval(firstIndex, lastIndex - 1);
107         this.myList.scrollRectToVisible(
108             this.myList.getCellBounds(
109                 this.myList.getMinSelectionIndex(),
110                 this.myList.getMaxSelectionIndex()
111             )
112         );
113         textarea.setText(null);
114     }
115 
116     private int addToList(final JTextArea textarea, int index) {
117         int lastIndex = index;
118         
119         for (String newItem: textarea.getText().split("\\n")) {
120             if (StringUtils.isNotEmpty(newItem)) {
121                 ((DefaultListModel<ItemList>) this.myList.getModel()).add(
122                     lastIndex++,
123                     new ItemList(newItem.replace("\\", "/"))
124                 );
125             }
126         }
127         return lastIndex;
128     }
129 
130     private int addToScanList(final JTextArea textarea, int index) {
131         int lastIndex = index;
132         List<ItemListScan> listParsedItems = ListTransfertHandlerScan.parse(textarea.getText().replace("\\", "/"));
133         
134         for (ItemListScan item: listParsedItems) {
135             ((DefaultListModel<ItemList>) this.myList.getModel()).add(lastIndex++, item);
136         }
137         return lastIndex;
138     }
139 }