1 19 20 package org.netbeans.modules.ant.freeform.customcommands; 21 22 import java.awt.Component ; 23 import java.awt.event.ActionEvent ; 24 import java.beans.PropertyChangeListener ; 25 import java.io.IOException ; 26 import java.io.Serializable ; 27 import java.net.URL ; 28 import java.util.ArrayList ; 29 import java.util.Arrays ; 30 import java.util.List ; 31 import javax.swing.Action ; 32 import javax.swing.ImageIcon ; 33 import javax.swing.JButton ; 34 import javax.swing.JMenuItem ; 35 import org.netbeans.spi.project.ui.support.FileSensitiveActions; 36 import org.openide.filesystems.FileObject; 37 import org.openide.filesystems.Repository; 38 import org.openide.loaders.DataFolder; 39 import org.openide.loaders.DataObject; 40 import org.openide.loaders.InstanceDataObject; 41 import org.openide.util.ContextAwareAction; 42 import org.openide.util.Lookup; 43 import org.openide.util.actions.Presenter; 44 45 49 public final class Command implements Serializable , Action , Presenter.Menu, Presenter.Toolbar, Presenter.Popup, ContextAwareAction { 50 51 private static final long serialVersionUID = 1L; 52 53 private final String command; 54 private final String namePattern; 55 private final URL icon; 56 private transient Action delegate; 57 58 Command(String command, String namePattern, URL icon) { 59 this.command = command; 60 this.namePattern = namePattern; 61 this.icon = icon; 62 } 63 64 private Action getDelegate() { 65 if (delegate == null) { 66 delegate = FileSensitiveActions.fileCommandAction(command, namePattern, icon != null ? new ImageIcon (icon) : null); 67 } 68 return delegate; 69 } 70 71 private static DataFolder folder(String path) { 72 FileObject f = Repository.getDefault().getDefaultFileSystem().findResource(path); 73 return f != null ? DataFolder.findFolder(f) : null; 74 } 75 76 public void create(DataFolder menuFolder, int position) throws IOException { 77 DataFolder ap = folder("Actions/Project"); if (ap == null) { 79 ap = folder("Actions"); assert ap != null; 81 } 82 DataObject instance = InstanceDataObject.create(ap, command, this, null, true); 83 String menuName = menuFolder.getName(); 84 if (menuName.endsWith("Project")) { menuName = menuName.substring(0, menuName.length() - "Project".length()); } 87 DataFolder oda = folder("OptionsDialog/Actions/" + menuName); if (oda == null) { 89 oda = folder("OptionsDialog/Actions/Build"); } 91 if (oda != null) { 92 instance.createShadow(oda); 93 } 94 DataObject nue = instance.createShadow(menuFolder); 95 List children = new ArrayList (Arrays.asList(menuFolder.getChildren())); 96 assert children.contains(nue); 97 children.remove(nue); 98 children.add(position, nue); 99 menuFolder.setOrder((DataObject[]) children.toArray(new DataObject[children.size()])); 100 } 101 102 public Object getValue(String key) { 103 return getDelegate().getValue(key); 104 } 105 106 public void putValue(String key, Object value) { 107 getDelegate().putValue(key, value); 108 } 109 110 public void setEnabled(boolean b) { 111 getDelegate().setEnabled(b); 112 } 113 114 public boolean isEnabled() { 115 return getDelegate().isEnabled(); 116 } 117 118 public void addPropertyChangeListener(PropertyChangeListener listener) { 119 getDelegate().addPropertyChangeListener(listener); 120 } 121 122 public void removePropertyChangeListener(PropertyChangeListener listener) { 123 getDelegate().removePropertyChangeListener(listener); 124 } 125 126 public void actionPerformed(ActionEvent e) { 127 getDelegate().actionPerformed(e); 128 } 129 130 public JMenuItem getMenuPresenter() { 131 if (getDelegate() instanceof Presenter.Menu) { 132 return ((Presenter.Menu) getDelegate()).getMenuPresenter(); 133 } else { 134 return new JMenuItem (this); 135 } 136 } 137 138 public Component getToolbarPresenter() { 139 if (getDelegate() instanceof Presenter.Toolbar) { 140 return ((Presenter.Toolbar) getDelegate()).getToolbarPresenter(); 141 } else { 142 return new JButton (this); 143 } 144 } 145 146 public JMenuItem getPopupPresenter() { 147 if (getDelegate() instanceof Presenter.Popup) { 148 return ((Presenter.Popup) getDelegate()).getPopupPresenter(); 149 } else { 150 return new JMenuItem (this); 151 } 152 } 153 154 public Action createContextAwareInstance(Lookup actionContext) { 155 if (getDelegate() instanceof ContextAwareAction) { 156 return ((ContextAwareAction) getDelegate()).createContextAwareInstance(actionContext); 157 } else { 158 return this; 159 } 160 } 161 162 } 163 | Popular Tags |