PageRenderTime 196ms CodeModel.GetById 185ms app.highlight 8ms RepoModel.GetById 1ms 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
  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
 22package xml.gui;
 23
 24import org.gjt.sp.jedit.jEdit;
 25import org.gjt.sp.jedit.GUIUtilities;
 26
 27import javax.swing.AbstractAction;
 28import javax.swing.JButton;
 29import javax.swing.Action;
 30import javax.swing.Icon;
 31import javax.swing.ImageIcon;
 32import javax.swing.JMenuItem;
 33import javax.swing.JPopupMenu;
 34import javax.swing.JRadioButton;
 35
 36import java.awt.Dimension;
 37import java.net.URL;
 38
 39/**
 40 * Convenience abstract GUI action.
 41 * copied from the XSLT Plugin : xslt/XsltAction.java
 42 *
 43 * @author Robert McKinnon - robmckinnon@users.sourceforge.net
 44 */
 45public abstract class UsefulAction extends AbstractAction {
 46	private String actionType;
 47
 48  public UsefulAction(String actionType) {
 49    this.actionType = actionType;
 50    
 51    String actionName = jEdit.getProperty(actionType + ".name");
 52    String shortcut = jEdit.getProperty(actionType + ".shortcut");
 53    String shortDescription = jEdit.getProperty(actionType + ".short-desc");
 54    String iconName = jEdit.getProperty(actionType + ".small-icon");
 55
 56    shortDescription = (shortcut != null) ? shortDescription + " - " + shortcut : shortDescription;
 57
 58
 59    putValue(Action.ACTION_COMMAND_KEY, actionType);
 60    putValue(Action.NAME, actionName);
 61    putValue(Action.SHORT_DESCRIPTION, shortDescription);
 62    if(iconName != null){
 63    	Icon icon;
 64    	if(iconName.startsWith("/")){
 65    		URL u = getClass().getResource(iconName);
 66    		icon = new ImageIcon(u);
 67    	}else{
 68    		icon = GUIUtilities.loadIcon(iconName);
 69    	}
 70    	putValue(Action.SMALL_ICON, icon);
 71    }
 72  }
 73
 74
 75  public JButton getButton() {
 76    JButton button = new JButton(this);
 77    button.setText("");
 78    button.setName(actionType);
 79    Dimension dimension = getButtonDimension();
 80
 81    button.setMinimumSize(dimension);
 82    button.setPreferredSize(dimension);
 83    button.setMaximumSize(dimension);
 84
 85    return button;
 86  }
 87  
 88  public JRadioButton getRadioButton(String text) {
 89	JRadioButton button = new JRadioButton(this);
 90	button.setName(text);
 91	button.setText(jEdit.getProperty(text));
 92
 93	return button;
 94  }
 95
 96
 97  protected Dimension getButtonDimension() {
 98    Dimension dimension = new Dimension(30, 30);
 99    return dimension;
100  }
101
102
103  public JMenuItem getMenuItem() {
104    JMenuItem item = new JMenuItem(this);
105    item.setIcon(null);
106    return item;
107  }
108
109
110  public static JPopupMenu initMenu(Object[] actions) {
111    JPopupMenu menu = new JPopupMenu();
112
113    for(int i = 0; i < actions.length; i++) {
114      Object action = actions[i];
115      if(action == null) {
116        menu.addSeparator();
117      } else {
118        menu.add(((UsefulAction)action).getMenuItem());
119      }
120    }
121
122    return menu;
123  }
124}