2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.io.cache;
23 import jalview.bin.Cache;
24 import jalview.util.MessageManager;
25 import jalview.util.Platform;
27 import java.awt.event.ActionEvent;
28 import java.awt.event.ActionListener;
29 import java.awt.event.FocusListener;
30 import java.awt.event.KeyAdapter;
31 import java.awt.event.KeyEvent;
32 import java.awt.event.KeyListener;
33 import java.util.ArrayList;
34 import java.util.Arrays;
35 import java.util.Collections;
36 import java.util.LinkedHashSet;
37 import java.util.List;
40 import javax.swing.JComboBox;
41 import javax.swing.JComponent;
42 import javax.swing.JMenuItem;
43 import javax.swing.JPopupMenu;
44 import javax.swing.JTextField;
45 import javax.swing.SwingUtilities;
46 import javax.swing.event.CaretListener;
47 import javax.swing.event.DocumentListener;
48 import javax.swing.text.JTextComponent;
51 * A class that provides an editable combobox with a memory of previous entries
52 * that may be persisted
59 * (temporary?) patches to wrap a JTextField instead when running as Javascript
61 public class JvCacheableInputBox<E>
63 protected JComboBox<String> comboBox; // used for Jalview
65 protected JTextField textField; // used for JalviewJS
67 protected JTextComponent textComponent; // used for both
69 protected String cacheKey;
71 protected AppCache appCache;
73 private JPopupMenu popup = new JPopupMenu();
75 private JMenuItem menuItemClearCache = new JMenuItem();
77 volatile boolean enterWasPressed = false;
79 private String prototypeDisplayValue;
82 * @return flag indicating if the most recent keypress was enter
84 public boolean wasEnterPressed()
86 return enterWasPressed;
94 public JvCacheableInputBox(String newCacheKey)
97 cacheKey = newCacheKey;
98 prototypeDisplayValue = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
99 boolean useTextField = Platform.isJS();
100 // BH 2019.03 only switch for JavaScript here
101 // SwingJS TODO implement editable combo box
105 textComponent = textField = new JTextField();
108 // public Dimension getPreferredSize() {
109 // return super.getPreferredSize();
110 //// FontMetrics fm = getFontMetrics(getFont());
111 //// return new Dimension(fm.stringWidth(prototypeDisplayValue),
118 appCache = AppCache.getInstance();
119 comboBox = new JComboBox<>();
120 textComponent = (JTextComponent) comboBox.getEditor()
121 .getEditorComponent();
122 comboBox.setEditable(true);
123 comboBox.addKeyListener(new KeyAdapter()
126 public void keyTyped(KeyEvent e)
128 enterWasPressed = false;
129 if (e.getKeyCode() == KeyEvent.VK_ENTER)
131 enterWasPressed = true;
133 // let event bubble up
136 comboBox.setPrototypeDisplayValue(prototypeDisplayValue);
138 initCachePopupMenu();
139 initCache(newCacheKey);
144 * Method for initialising cache items for a given cache key and populating
145 * the in-memory cache with persisted cache items
149 private void initCache(String cacheKey)
151 if (appCache == null)
155 // obtain persisted cache items from properties file as a delimited string
156 String delimitedCacheStr = Cache.getProperty(cacheKey);
157 if (delimitedCacheStr == null || delimitedCacheStr.isEmpty())
161 // convert delimited cache items to a list of strings
162 List<String> persistedCacheItems = Arrays
163 .asList(delimitedCacheStr.split(AppCache.CACHE_DELIMITER));
165 LinkedHashSet<String> foundCacheItems = appCache
166 .getAllCachedItemsFor(cacheKey);
167 if (foundCacheItems == null)
169 foundCacheItems = new LinkedHashSet<>();
171 // populate memory cache
172 for (String cacheItem : persistedCacheItems)
174 foundCacheItems.add(cacheItem);
176 appCache.putCache(cacheKey, foundCacheItems);
180 * Initialise this cache's pop-up menu
182 private void initCachePopupMenu()
184 if (appCache == null)
188 menuItemClearCache.setFont(new java.awt.Font("Verdana", 0, 12));
190 .setText(MessageManager.getString("action.clear_cached_items"));
191 menuItemClearCache.addActionListener(new ActionListener()
194 public void actionPerformed(ActionEvent e)
196 // System.out.println(">>>>> Clear cache items");
198 appCache.deleteCacheItems(cacheKey);
203 popup.add(menuItemClearCache);
204 comboBox.setComponentPopupMenu(popup);
209 * Answers true if input text is an integer
214 static boolean isInteger(String text)
218 Integer.parseInt(text);
220 } catch (NumberFormatException e)
227 * Method called to update the cache with the last user input
229 public void updateCache()
231 if (appCache == null)
235 SwingUtilities.invokeLater(new Runnable()
240 int cacheLimit = Integer.parseInt(appCache.getCacheLimit(cacheKey));
241 String userInput = getUserInput();
242 if (userInput != null && !userInput.isEmpty())
244 LinkedHashSet<String> foundCache = appCache
245 .getAllCachedItemsFor(cacheKey);
246 // remove old cache item so as to place current input at the top of
248 foundCache.remove(userInput);
249 foundCache.add(userInput);
250 appCache.putCache(cacheKey, foundCache);
253 String lastSearch = userInput;
254 if (comboBox.getItemCount() > 0)
256 comboBox.removeAllItems();
258 Set<String> cacheItems = appCache.getAllCachedItemsFor(cacheKey);
259 List<String> reversedCacheItems = new ArrayList<>();
260 reversedCacheItems.addAll(cacheItems);
262 Collections.reverse(reversedCacheItems);
263 if (lastSearch.isEmpty())
265 comboBox.addItem("");
268 if (reversedCacheItems != null && !reversedCacheItems.isEmpty())
270 LinkedHashSet<String> foundCache = appCache
271 .getAllCachedItemsFor(cacheKey);
272 boolean prune = reversedCacheItems.size() > cacheLimit;
274 boolean limitExceeded = false;
275 for (String cacheItem : reversedCacheItems)
277 limitExceeded = (count++ > cacheLimit);
282 foundCache.remove(cacheItem);
286 comboBox.addItem(cacheItem);
291 comboBox.addItem(cacheItem);
294 appCache.putCache(cacheKey, foundCache);
296 setSelectedItem(lastSearch.isEmpty() ? "" : lastSearch);
302 * This method should be called to persist the in-memory cache when this
303 * components parent frame is closed / exited
305 public void persistCache()
307 if (appCache == null)
311 appCache.persistCache(cacheKey);
315 * Returns the trimmed text in the input field
319 public String getUserInput()
321 if (comboBox == null)
323 return textField.getText().trim();
325 Object item = comboBox.getEditor().getItem();
326 return item == null ? "" : item.toString().trim();
329 public JComponent getComponent()
331 return (comboBox == null ? textField : comboBox);
334 public void addActionListener(ActionListener actionListener)
336 if (comboBox == null)
338 textField.addActionListener(actionListener);
342 comboBox.addActionListener(actionListener);
346 public void addDocumentListener(DocumentListener listener)
348 textComponent.getDocument().addDocumentListener(listener);
351 public void addFocusListener(FocusListener focusListener)
353 textComponent.addFocusListener(focusListener);
356 public void addKeyListener(KeyListener kl)
358 textComponent.addKeyListener(kl);
361 public void addCaretListener(CaretListener caretListener)
363 textComponent.addCaretListener(caretListener);
366 public void setEditable(boolean b)
368 if (comboBox != null)
370 comboBox.setEditable(b);
374 public void setPrototypeDisplayValue(String string)
376 prototypeDisplayValue = string;
377 if (comboBox != null)
379 comboBox.setPrototypeDisplayValue(string);
383 public void setSelectedItem(String userInput)
385 if (comboBox != null)
387 comboBox.setSelectedItem(userInput);
391 public boolean isPopupVisible()
393 return (comboBox != null && comboBox.isPopupVisible());
396 public void addItem(String item)
398 if (comboBox != null)
400 comboBox.addItem(item);