001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.uml.ui.products.ad.application.action;
043:
044: import java.awt.event.ActionEvent;
045:
046: import javax.swing.AbstractAction;
047: import javax.swing.AbstractButton;
048: import javax.swing.Action;
049: import javax.swing.Icon;
050: import javax.swing.JCheckBoxMenuItem;
051: import javax.swing.JComponent;
052: import javax.swing.JMenu;
053: import javax.swing.JMenuItem;
054:
055: //import org.netbeans.modules.uml.core.addinframework.plugins.IExtension;
056: import org.netbeans.modules.uml.ui.products.ad.application.IMenuManager;
057:
058: /**
059: *
060: * @author Trey Spiva
061: */
062: public class BaseAction extends AbstractAction {
063: private boolean m_IsChecked = false;
064: private int m_Style = 0;
065: private Icon m_HoverIcon = null;
066: private Icon m_DisableIcon = null;
067: private String m_Id = "";
068: private String m_Label = "";
069: private IMenuManager m_MenuManager = null;
070:
071: /**
072: * Action style constant (value <code>0</code>) indicating action style
073: * is not specified yet. By default, the action will assume a push button
074: * style. If <code>setChecked</code> is called, then the style will change
075: * to a check box, or if <code>setMenuCreator</code> is called, then the
076: * style will change to a drop down menu.
077: */
078: public static final int AS_UNSPECIFIED = 0x00;
079:
080: /**
081: * Action style constant (value <code>1</code>) indicating action is
082: * a simple push button.
083: */
084: public static final int AS_PUSH_BUTTON = 0x01;
085:
086: /**
087: * Action style constant (value <code>2</code>) indicating action is
088: * a check box (or a toggle button).
089: */
090: public static final int AS_CHECK_BOX = 0x02;
091:
092: /**
093: * Action style constant (value <code>4</code>) indicating action is
094: * a drop down menu.
095: */
096: public static final int AS_DROP_DOWN_MENU = 0x04;
097:
098: /**
099: * Action style constant (value <code>8</code>) indicating action is
100: * a radio button.
101: *
102: * @since 2.1
103: */
104: public static final int AS_RADIO_BUTTON = 0x08;
105:
106: public static final int AS_CUSTOM_COMPONENT = 0x16;
107:
108: /**
109: * Property name of an action's text (value <code>"text"</code>).
110: */
111: public static final String TEXT = "text"; //$NON-NLS-1$
112:
113: /**
114: * Property name of an action's enabled state
115: * (value <code>"enabled"</code>).
116: */
117: public static final String ENABLED = "enabled"; //$NON-NLS-1$
118:
119: /**
120: * Property name of an action's image (value <code>"image"</code>).
121: */
122: public static final String IMAGE = "image"; //$NON-NLS-1$
123:
124: /**
125: * Property name of an action's tooltip text (value <code>"toolTipText"</code>).
126: */
127: public static final String TOOL_TIP_TEXT = "toolTipText"; //$NON-NLS-1$
128:
129: /**
130: * Property name of an action's description (value <code>"description"</code>).
131: * Typically the description is shown as a (longer) help text in the status line.
132: */
133: public static final String DESCRIPTION = "description"; //$NON-NLS-1$
134:
135: public static final String MB_ADDITIONS = "additions";
136:
137: /**
138: * Property name of an action's checked status (value
139: * <code>"checked"</code>). Applicable when the style is
140: * <code>AS_CHECK_BOX</code> or <code>AS_RADIO_BUTTON</code>.
141: */
142: public static final String CHECKED = "checked"; //$NON-NLS-1$
143:
144: /* (non-Javadoc)
145: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
146: */
147: public void actionPerformed(ActionEvent e) {
148:
149: }
150:
151: /**
152: * @return
153: */
154: public String getText() {
155: return (String) getValue(Action.NAME);
156: }
157:
158: /**
159: * @param label
160: */
161: public void setText(String label) {
162: StringBuffer name = new StringBuffer();
163: for (int index = 0; index < label.length(); index++) {
164: char curChar = label.charAt(index);
165: if ((curChar == '&') && ((index + 1) < label.length())) {
166: index++;
167: curChar = label.charAt(index);
168: putValue(Action.MNEMONIC_KEY, new Integer(curChar));
169: }
170:
171: name.append(curChar);
172: }
173:
174: putValue(Action.NAME, name.toString());
175: }
176:
177: /**
178: * @param id
179: */
180: public void setId(String id) {
181: m_Id = id;
182: }
183:
184: /**
185: *
186: */
187: public String getId() {
188: return m_Id;
189: }
190:
191: public void setLabel(String label) {
192: m_Label = label;
193: }
194:
195: /**
196: *
197: */
198: public String getLabel() {
199: return m_Label;
200: }
201:
202: /**
203: * @return
204: */
205: public String getToolTipText() {
206: return (String) getValue(Action.SHORT_DESCRIPTION);
207: }
208:
209: /**
210: * @param tooltip
211: */
212: public void setToolTipText(String tooltip) {
213: putValue(Action.SHORT_DESCRIPTION, tooltip);
214: }
215:
216: /**
217: * @param description
218: */
219: public void setDescription(String description) {
220: putValue(Action.LONG_DESCRIPTION, description);
221: }
222:
223: /**
224: * @param b
225: */
226: public void setChecked(boolean b) {
227: m_IsChecked = b;
228:
229: }
230:
231: /**
232: * @return
233: */
234: public boolean isChecked() {
235: return m_IsChecked;
236: }
237:
238: /**
239: * @param i
240: */
241: public void setAccelerator(int i) {
242: // TODO Auto-generated method stub
243:
244: }
245:
246: /**
247: *
248: */
249: public int getAccelerator() {
250: return 0;
251: // TODO Auto-generated method stub
252:
253: }
254:
255: /**
256: * @param text
257: * @return
258: */
259: public static String removeAcceleratorText(String text) {
260: // TODO Auto-generated method stub
261: return null;
262: }
263:
264: /**
265: * @param acceleratorText
266: * @return
267: */
268: public static int convertAccelerator(String acceleratorText) {
269: // TODO Auto-generated method stub
270: return 0;
271: }
272:
273: /**
274: * Sets the actions icon. The extension is used to retrieve the specified
275: * icon from the filesystem.
276: *
277: * @param extension The extension that defined the action.
278: * @param icon The path to the icon.
279: */
280: // public void setSmallIcon(IExtension extension, String icon)
281: // {
282: // Icon smallIcon = retrieveIcon(extension, icon);
283: // if (smallIcon != null)
284: // {
285: // putValue(Action.SMALL_ICON, smallIcon);
286: // }
287: // }
288: /**
289: * Sets the actions icon.
290: *
291: * @param extension The extension that defined the action.
292: * @param icon The path to the icon.
293: */
294: public void setSmallIcon(Icon icon) {
295: if (icon != null) {
296: putValue(Action.SMALL_ICON, icon);
297: }
298: }
299:
300: /**
301: * Gets the actions small icon.
302: *
303: * @param hoverIcon
304: */
305: public Icon getSmallImage() {
306: Icon retVal = null;
307:
308: Object obj = getValue(Action.SMALL_ICON);
309: if (obj instanceof Icon) {
310: retVal = (Icon) obj;
311:
312: }
313: return retVal;
314: }
315:
316: /**
317: * Sets the icon to use when the mouse has hovered over a toolbar button.
318: * The extension is used to retrieve the specified icon from the filesystem.
319: *
320: * @param extension The extension that defined the action.
321: * @param icon The path to the icon.
322: */
323: // public void setHoverImage(IExtension extension, String icon)
324: // {
325: // setHoverImage(retrieveIcon(extension, icon));
326: // }
327: /**
328: * Sets the icon to use when the mouse has hovered over a toolbar button.
329: *
330: * @param extension The extension that defined the action.
331: * @param icon The path to the icon.
332: */
333: public void setHoverImage(Icon icon) {
334: m_HoverIcon = icon;
335:
336: }
337:
338: /**
339: * Gets the icon to use when the mouse has hovered over a toolbar button.
340: *
341: * @return The hover icon or <code>null</code> if a hover icon has not been
342: * defined.
343: */
344: public Icon getHoverImage() {
345: return m_HoverIcon;
346: }
347:
348: /**
349: * Sets the icon to display when the action has been disabled.
350: *
351: * @param extension The extension that defined the action.
352: * @param icon The path to the icon.
353: */
354: // public void setDisabledImage(IExtension extension, String disabledIcon)
355: // {
356: // setDisabledImage(retrieveIcon(extension, disabledIcon));
357: // }
358: /**
359: * Sets the icon to display when the action has been disabled.
360: * The extension is used to retrieve the specified icon from the filesystem.
361: *
362: * @param extension The extension that defined the action.
363: * @param icon The path to the icon.
364: */
365: public void setDisabledImage(Icon icon) {
366: m_DisableIcon = icon;
367: }
368:
369: /**
370: * Gets the icon to use when the action has been disabled.
371: *
372: * @return The disable icon or <code>null</code> if a hover icon has not been
373: * defined.
374: */
375: public Icon getDisableImage() {
376: return m_HoverIcon;
377: }
378:
379: /**
380: * @return
381: */
382: public int getStyle() {
383: return m_Style;
384: }
385:
386: public void setStyle(int s) {
387: m_Style = s;
388:
389: if ((s == BaseAction.AS_CHECK_BOX) && (isChecked())) {
390: setChecked(true);
391: }
392: }
393:
394: // **************************************************
395: // Helper Methods
396: //**************************************************
397:
398: /**
399: * Uses the extension to retrieve the specified icon from the plugins.
400: *
401: * @param extension The extension that represents the location of the icon.
402: * @param icon The relative path to the icon. The icon is relative to the
403: * extension.
404: * @return The icon if it is found, <code>null</code> if the icon is not
405: * found.
406: */
407: // protected Icon retrieveIcon(IExtension extension, String icon)
408: // {
409: // Icon retVal = null;
410: //
411: // URL installURL = extension.getDeclaringPluginDescriptor().getInstallURL();
412: // URL iconURL;
413: // try
414: // {
415: // iconURL = new URL(installURL.getProtocol(), installURL.getHost(), installURL.getFile() + "/" + icon);
416: // retVal = new ImageIcon(iconURL);
417: // }
418: // catch (MalformedURLException e)
419: // {
420: // // TODO Figure out what to do about exceptions
421: // e.printStackTrace();
422: // }
423: //
424: // return retVal;
425: // }
426: /**
427: * This method is for BaseACtions which want to return custom components like combobox etc.
428: */
429: public JComponent getActionComponent() {
430: JComponent retVal = null;
431:
432: int style = getStyle();
433:
434: switch (style) {
435: case AS_CHECK_BOX:
436: JCheckBoxMenuItem item = new JCheckBoxMenuItem(this );
437: item.setState(isChecked());
438: retVal = item;
439: break;
440: case AS_DROP_DOWN_MENU:
441: retVal = new JMenu();
442: break;
443: case AS_RADIO_BUTTON:
444: retVal = new JCheckBoxMenuItem(this );
445: break;
446: case AS_CUSTOM_COMPONENT:
447: retVal = getCustomComponent();
448: break;
449: default:
450: retVal = new JMenuItem();
451: ((JMenuItem) retVal).setAction(this );
452: break;
453: }
454:
455: if (retVal instanceof AbstractButton) {
456: // System.out.println("Got a button: " + getText());
457:
458: AbstractButton btn = (AbstractButton) retVal;
459:
460: Integer keyCode = (Integer) getValue(Action.MNEMONIC_KEY);
461: if (keyCode != null) {
462: btn.setMnemonic(keyCode);
463: }
464: btn.setText(getText());
465: btn.getAccessibleContext().setAccessibleName(getText());
466: }
467:
468: return retVal;
469: }
470:
471: public void setMenuManager(IMenuManager mgr) {
472: m_MenuManager = mgr;
473: }
474:
475: public IMenuManager getMenuManager() {
476: return m_MenuManager;
477: }
478:
479: public JComponent getCustomComponent() {
480: return null;
481: }
482: }
|