PageRenderTime 36ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/org/gjt/sp/jedit/menu/EnhancedMenu.java

#
Java | 188 lines | 120 code | 30 blank | 38 comment | 28 complexity | 30a1a43d1d6134bc45319fa38cab2007 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. /*
  2. * EnhancedMenu.java - jEdit menu
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2001, 2003 Slava Pestov
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. package org.gjt.sp.jedit.menu;
  23. //{{{ Imports
  24. import javax.swing.event.*;
  25. import javax.swing.*;
  26. import java.util.StringTokenizer;
  27. import org.gjt.sp.jedit.msg.*;
  28. import org.gjt.sp.jedit.*;
  29. //}}}
  30. public class EnhancedMenu extends JMenu implements MenuListener
  31. {
  32. //{{{ EnhancedMenu constructor
  33. public EnhancedMenu(String name)
  34. {
  35. this(name,jEdit.getProperty(name.concat(".label")),
  36. jEdit.getActionContext());
  37. } //}}}
  38. //{{{ EnhancedMenu constructor
  39. public EnhancedMenu(String name, String label)
  40. {
  41. this(name,label,jEdit.getActionContext());
  42. } //}}}
  43. //{{{ EnhancedMenu constructor
  44. public EnhancedMenu(String name, String label, ActionContext context)
  45. {
  46. this.context = context;
  47. if(label == null)
  48. label = name;
  49. char mnemonic;
  50. int index = label.indexOf('$');
  51. if(index != -1 && label.length() - index > 1)
  52. {
  53. mnemonic = Character.toLowerCase(label.charAt(index + 1));
  54. label = label.substring(0,index).concat(label.substring(++index));
  55. }
  56. else
  57. mnemonic = '\0';
  58. setText(label);
  59. if(!OperatingSystem.isMacOS())
  60. setMnemonic(mnemonic);
  61. String menuItems = jEdit.getProperty(name);
  62. if(menuItems != null)
  63. {
  64. StringTokenizer st = new StringTokenizer(menuItems);
  65. while(st.hasMoreTokens())
  66. {
  67. String menuItemName = st.nextToken();
  68. if(menuItemName.equals("-"))
  69. addSeparator();
  70. else
  71. add(GUIUtilities.loadMenuItem(context,menuItemName,true));
  72. }
  73. }
  74. initialComponentCount = getMenuComponentCount();
  75. providerCode = jEdit.getProperty(name + ".code");
  76. ebStub = new EditBusStub(name);
  77. ebStub.menuOutOfDate = true;
  78. addMenuListener(this);
  79. if(providerCode != null)
  80. EditBus.addToBus(ebStub);
  81. } //}}}
  82. //{{{ menuSelected() method
  83. public void menuSelected(MenuEvent evt)
  84. {
  85. init();
  86. } //}}}
  87. public void menuDeselected(MenuEvent e) {}
  88. public void menuCanceled(MenuEvent e) {}
  89. //{{{ init() method
  90. public void init()
  91. {
  92. if(providerCode == null)
  93. return;
  94. if(provider == null)
  95. {
  96. Object obj = BeanShell.eval(null,
  97. BeanShell.getNameSpace(),
  98. providerCode);
  99. provider = (DynamicMenuProvider)obj;
  100. }
  101. if(provider == null)
  102. {
  103. // error
  104. providerCode = null;
  105. return;
  106. }
  107. if(ebStub.menuOutOfDate || provider.updateEveryTime())
  108. {
  109. ebStub.menuOutOfDate = false;
  110. while(getMenuComponentCount() != initialComponentCount)
  111. remove(getMenuComponentCount() - 1);
  112. if(provider != null)
  113. provider.update(this);
  114. }
  115. } //}}}
  116. //{{{ Protected members
  117. protected int initialComponentCount;
  118. protected ActionContext context;
  119. protected String providerCode;
  120. protected DynamicMenuProvider provider;
  121. protected EditBusStub ebStub;
  122. //{{{ finalize() method
  123. protected void finalize() throws Exception
  124. {
  125. if(ebStub != null)
  126. EditBus.removeFromBus(ebStub);
  127. } //}}}
  128. //}}}
  129. //{{{ EditBusStub class
  130. /* EnhancedMenu has a reference to EditBusStub, but not the other
  131. * way around. So when the EnhancedMenu is being garbage collected
  132. * its finalize() method removes the EditBusStub from the edit bus. */
  133. static class EditBusStub implements EBComponent
  134. {
  135. String name;
  136. boolean menuOutOfDate;
  137. EditBusStub(String name)
  138. {
  139. this.name = name;
  140. menuOutOfDate = true;
  141. }
  142. public void handleMessage(EBMessage msg)
  143. {
  144. if(msg instanceof DynamicMenuChanged
  145. && name.equals(((DynamicMenuChanged)msg)
  146. .getMenuName()))
  147. {
  148. menuOutOfDate = true;
  149. }
  150. else if(msg instanceof PropertiesChanged)
  151. {
  152. // while this might be questionable, some
  153. // menus depend on properties
  154. menuOutOfDate = true;
  155. }
  156. }
  157. } //}}}
  158. }