+ /**
+ * Constructor given the key to cached values, and the (approximate) length in
+ * characters of the input field
+ *
+ * @param newCacheKey
+ * @param length
+ */
+ public JvCacheableInputBox(String newCacheKey, int length)
+ {
+ // super();
+ cacheKey = newCacheKey;
+ prototypeDisplayValue = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
+ if (length > 0)
+ {
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < length; i++)
+ {
+ sb.append("X");
+ }
+ setPrototypeDisplayValue(sb.toString());
+ }
+ boolean useTextField = Platform.isJS();
+ // BH 2019.03 only switch for JavaScript here
+ // SwingJS TODO implement editable combo box
+ if (useTextField)
+ {
+ appCache = null;
+ textComponent = textField = new JTextField();
+ // {
+ // @Override
+ // public Dimension getPreferredSize() {
+ // return super.getPreferredSize();
+ //// FontMetrics fm = getFontMetrics(getFont());
+ //// return new Dimension(fm.stringWidth(prototypeDisplayValue),
+ // fm.getHeight());
+ // }
+ // };
+ }
+ else
+ {
+ appCache = AppCache.getInstance();
+ comboBox = new JComboBox<>();
+ textComponent = (JTextComponent) comboBox.getEditor()
+ .getEditorComponent();
+ comboBox.setEditable(true);
+ comboBox.addKeyListener(new KeyAdapter()
+ {
+ @Override
+ public void keyTyped(KeyEvent e)
+ {
+ enterWasPressed = false;
+ if (e.getKeyCode() == KeyEvent.VK_ENTER)
+ {
+ enterWasPressed = true;
+ }
+ // let event bubble up
+ }
+ });
+ comboBox.setPrototypeDisplayValue(prototypeDisplayValue);
+ initCachePopupMenu();
+ initCache(newCacheKey);
+ updateCache();
+ }
+ }
+
+ /**
+ * Method for initialising cache items for a given cache key and populating
+ * the in-memory cache with persisted cache items
+ *
+ * @param cacheKey
+ */
+ private void initCache(String cacheKey)
+ {
+ if (appCache == null)
+ {
+ return;
+ }
+ // obtain persisted cache items from properties file as a delimited string
+ String delimitedCacheStr = Cache.getProperty(cacheKey);
+ if (delimitedCacheStr == null || delimitedCacheStr.isEmpty())
+ {
+ return;
+ }
+ // convert delimited cache items to a list of strings
+ List<String> persistedCacheItems = Arrays
+ .asList(delimitedCacheStr.split(AppCache.CACHE_DELIMITER));
+
+ LinkedHashSet<String> foundCacheItems = appCache
+ .getAllCachedItemsFor(cacheKey);
+ if (foundCacheItems == null)
+ {
+ foundCacheItems = new LinkedHashSet<>();
+ }
+ // populate memory cache
+ for (String cacheItem : persistedCacheItems)
+ {
+ foundCacheItems.add(cacheItem);
+ }
+ appCache.putCache(cacheKey, foundCacheItems);
+ }
+
+ /**
+ * Initialise this cache's pop-up menu
+ */
+ private void initCachePopupMenu()
+ {
+ if (appCache == null)
+ {
+ return;
+ }
+ menuItemClearCache.setFont(new java.awt.Font("Verdana", 0, 12));
+ menuItemClearCache
+ .setText(MessageManager.getString("action.clear_cached_items"));
+ menuItemClearCache.addActionListener(new ActionListener()
+ {
+ @Override
+ public void actionPerformed(ActionEvent e)
+ {
+ // System.out.println(">>>>> Clear cache items");
+ setSelectedItem("");
+ appCache.deleteCacheItems(cacheKey);
+ updateCache();
+ }
+ });
+
+ popup.add(menuItemClearCache);
+ comboBox.setComponentPopupMenu(popup);
+ comboBox.add(popup);
+ }