PageRenderTime 88ms CodeModel.GetById 46ms RepoModel.GetById 0ms app.codeStats 0ms

/bundles/plugins-trunk/CommonControls/common/gui/HistoryComboBox.java

#
Java | 105 lines | 66 code | 11 blank | 28 comment | 11 complexity | a8898014f87808391862a8ccb6e27c84 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. package common.gui;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import java.util.*;
  5. import org.gjt.sp.jedit.*;
  6. import org.gjt.sp.util.*;
  7. /**
  8. * A Combo Box that remembers a given number of previous entries, even between jEdit runs.
  9. *
  10. * @author mace
  11. * @version $Revision: 1347 $ modified $Date: 2004-02-09 03:49:01 +0100 (Mon, 09 Feb 2004) $ by $Author: bemace $
  12. */
  13. public class HistoryComboBox extends JComboBox {
  14. private Stack entries;
  15. private int entryCount;
  16. private DefaultComboBoxModel model;
  17. private String propertyName;
  18. /**
  19. * Build a combo box which will remember the given the given number
  20. * of previous entries, and store them in the given property.
  21. *
  22. * @param entryCount Description of the Parameter
  23. * @param propName Description of the Parameter
  24. */
  25. public HistoryComboBox(int entryCount, String propName) {
  26. propertyName = propName;
  27. setEditable(true);
  28. model = new DefaultComboBoxModel();
  29. setModel(model);
  30. this.entryCount = entryCount;
  31. entries = new Stack();
  32. if (propertyName != null)
  33. loadFromProperty(propertyName);
  34. }
  35. /**
  36. * Creates a combo box that remembers the given number of entries
  37. * but doesn't not remember them between sessions.
  38. */
  39. public HistoryComboBox(int entryCount) {
  40. this(entryCount, null);
  41. }
  42. protected void addEntry() {
  43. model.addElement(getSelectedItem().toString());
  44. if (model.getSize() > entryCount) {
  45. model.removeElementAt(0);
  46. }
  47. setSelectedItem("");
  48. repaint();
  49. }
  50. public void processKeyEvent(KeyEvent e) {
  51. super.processKeyEvent(e);
  52. if (e.getKeyCode() == KeyEvent.VK_ENTER) {
  53. fireActionEvent();
  54. }
  55. }
  56. public void actionPerformed(ActionEvent e) {
  57. super.actionPerformed(e);
  58. addEntry();
  59. if (propertyName != null)
  60. storeToProperty(propertyName);
  61. }
  62. /**
  63. * Stores this box's elements in the given property.
  64. *
  65. * @param name Description of the Parameter
  66. */
  67. protected void storeToProperty(String name) {
  68. int i = 1;
  69. while (i < getItemCount()) {
  70. //Log.log(Log.DEBUG,this,"Setting '"+name+"."+i+"' to '"+getItemAt(i)+"'");
  71. jEdit.setProperty(name + "." + i, getItemAt(i).toString());
  72. i++;
  73. }
  74. jEdit.unsetProperty(name + "." + i);
  75. }
  76. /**
  77. * Loads elements into this box from the given property.
  78. *
  79. * @param name Description of the Parameter
  80. */
  81. protected void loadFromProperty(String name) {
  82. removeAllItems();
  83. int i = 1;
  84. String query = jEdit.getProperty(name + "." + i);
  85. while (query != null) {
  86. if (!query.equals("")) {
  87. addItem(query);
  88. }
  89. query = jEdit.getProperty(name + "." + i);
  90. i++;
  91. }
  92. setSelectedItem("");
  93. }
  94. }