PageRenderTime 52ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/bundles/plugins-trunk/XML/xml/gui/UsefulAction.java

#
Java | 124 lines | 74 code | 24 blank | 26 comment | 8 complexity | 12ea7cf9dcbaee421de49cf84a1797c3 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. * UsefulAction.java - Convenience abstract GUI action
  3. *
  4. * Copyright (C) 2003 Robert McKinnon
  5. * Copyright (C) 2010 Eric Le Lay
  6. *
  7. * This program 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 2
  10. * of the License, or any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. */
  21. package xml.gui;
  22. import org.gjt.sp.jedit.jEdit;
  23. import org.gjt.sp.jedit.GUIUtilities;
  24. import javax.swing.AbstractAction;
  25. import javax.swing.JButton;
  26. import javax.swing.Action;
  27. import javax.swing.Icon;
  28. import javax.swing.ImageIcon;
  29. import javax.swing.JMenuItem;
  30. import javax.swing.JPopupMenu;
  31. import javax.swing.JRadioButton;
  32. import java.awt.Dimension;
  33. import java.net.URL;
  34. /**
  35. * Convenience abstract GUI action.
  36. * copied from the XSLT Plugin : xslt/XsltAction.java
  37. *
  38. * @author Robert McKinnon - robmckinnon@users.sourceforge.net
  39. */
  40. public abstract class UsefulAction extends AbstractAction {
  41. private String actionType;
  42. public UsefulAction(String actionType) {
  43. this.actionType = actionType;
  44. String actionName = jEdit.getProperty(actionType + ".name");
  45. String shortcut = jEdit.getProperty(actionType + ".shortcut");
  46. String shortDescription = jEdit.getProperty(actionType + ".short-desc");
  47. String iconName = jEdit.getProperty(actionType + ".small-icon");
  48. shortDescription = (shortcut != null) ? shortDescription + " - " + shortcut : shortDescription;
  49. putValue(Action.ACTION_COMMAND_KEY, actionType);
  50. putValue(Action.NAME, actionName);
  51. putValue(Action.SHORT_DESCRIPTION, shortDescription);
  52. if(iconName != null){
  53. Icon icon;
  54. if(iconName.startsWith("/")){
  55. URL u = getClass().getResource(iconName);
  56. icon = new ImageIcon(u);
  57. }else{
  58. icon = GUIUtilities.loadIcon(iconName);
  59. }
  60. putValue(Action.SMALL_ICON, icon);
  61. }
  62. }
  63. public JButton getButton() {
  64. JButton button = new JButton(this);
  65. button.setText("");
  66. button.setName(actionType);
  67. Dimension dimension = getButtonDimension();
  68. button.setMinimumSize(dimension);
  69. button.setPreferredSize(dimension);
  70. button.setMaximumSize(dimension);
  71. return button;
  72. }
  73. public JRadioButton getRadioButton(String text) {
  74. JRadioButton button = new JRadioButton(this);
  75. button.setName(text);
  76. button.setText(jEdit.getProperty(text));
  77. return button;
  78. }
  79. protected Dimension getButtonDimension() {
  80. Dimension dimension = new Dimension(30, 30);
  81. return dimension;
  82. }
  83. public JMenuItem getMenuItem() {
  84. JMenuItem item = new JMenuItem(this);
  85. item.setIcon(null);
  86. return item;
  87. }
  88. public static JPopupMenu initMenu(Object[] actions) {
  89. JPopupMenu menu = new JPopupMenu();
  90. for(int i = 0; i < actions.length; i++) {
  91. Object action = actions[i];
  92. if(action == null) {
  93. menu.addSeparator();
  94. } else {
  95. menu.add(((UsefulAction)action).getMenuItem());
  96. }
  97. }
  98. return menu;
  99. }
  100. }