1 56 57 package org.objectstyle.cayenne.modeler.util; 58 59 import java.awt.event.ActionEvent ; 60 61 import javax.swing.AbstractAction ; 62 import javax.swing.Action ; 63 import javax.swing.Icon ; 64 import javax.swing.JButton ; 65 import javax.swing.JMenuItem ; 66 import javax.swing.KeyStroke ; 67 68 import org.objectstyle.cayenne.modeler.Application; 69 import org.objectstyle.cayenne.modeler.ProjectController; 70 import org.objectstyle.cayenne.modeler.dialog.ErrorDebugDialog; 71 import org.objectstyle.cayenne.project.Project; 72 import org.objectstyle.cayenne.project.ProjectPath; 73 74 80 public abstract class CayenneAction extends AbstractAction { 81 82 protected boolean alwaysOn; 83 protected Application application; 84 85 88 public CayenneAction(String name, Application application) { 89 super(name); 90 super.putValue(Action.DEFAULT, name); 91 92 this.application = application; 93 94 Icon icon = createIcon(); 95 if (icon != null) { 96 super.putValue(Action.SMALL_ICON, icon); 97 } 98 99 KeyStroke accelerator = getAcceleratorKey(); 100 if (accelerator != null) { 101 super.putValue(Action.ACCELERATOR_KEY, accelerator); 102 } 103 104 setEnabled(false); 105 } 106 107 public Application getApplication() { 108 return application; 109 } 110 111 protected Project getCurrentProject() { 112 return application 113 .getFrameController() 114 .getProjectController() 115 .getProject(); 116 } 117 118 122 public void setName(String newName) { 123 super.putValue(Action.NAME, newName); 124 } 125 126 130 public KeyStroke getAcceleratorKey() { 131 return null; 132 } 133 134 139 public String getIconName() { 140 return null; 141 } 142 143 148 public Icon createIcon() { 149 String name = getIconName(); 150 return (name != null) ? ModelerUtil.buildIcon(name) : null; 151 } 152 153 156 public String getKey() { 157 return (String ) super.getValue(Action.DEFAULT); 158 } 159 160 164 public abstract void performAction(ActionEvent e); 165 166 171 public boolean enableForPath(ProjectPath obj) { 172 return false; 173 } 174 175 178 public ProjectController getProjectController() { 179 return application.getFrameController().getProjectController(); 180 } 181 182 186 public void actionPerformed(ActionEvent e) { 187 try { 188 performAction(e); 189 } 190 catch (Throwable th) { 191 ErrorDebugDialog.guiException(th); 192 } 193 } 194 195 198 public JMenuItem buildMenu() { 199 return new JMenuItem (this); 200 } 201 202 205 public JButton buildButton() { 206 return new CayenneToolbarButton(this); 207 } 208 209 214 public boolean isAlwaysOn() { 215 return alwaysOn; 216 } 217 218 223 public void setAlwaysOn(boolean alwaysOn) { 224 this.alwaysOn = alwaysOn; 225 226 if (alwaysOn) { 227 super.setEnabled(true); 228 } 229 } 230 231 234 public void setEnabled(boolean b) { 235 if (!isAlwaysOn()) { 236 super.setEnabled(b); 237 } 238 } 239 240 243 final class CayenneToolbarButton extends JButton { 244 245 protected boolean showingText; 246 247 250 public CayenneToolbarButton(Action a) { 251 super(); 252 setAction(a); 253 } 254 255 258 public boolean isShowingText() { 259 return showingText; 260 } 261 262 265 public void setShowingText(boolean showingText) { 266 this.showingText = showingText; 267 } 268 269 272 public String getText() { 273 return (showingText) ? super.getText() : null; 274 } 275 276 279 public void setText(String text) { 280 if (showingText) { 281 super.setText(text); 282 } 283 else { 284 super.setToolTipText(text); 285 } 286 } 287 } 288 } | Popular Tags |