001: /*
002: * Copyright Javelin Software, All rights reserved.
003: */
004:
005: package com.javelin.swinglets;
006:
007: /**
008: * A SMenu is a (popup) menu for adding SMenuItem's to.
009: *
010: * @author Robin Sharp
011: */
012:
013: import java.awt.event.*;
014: import java.io.*;
015: import java.util.*;
016:
017: import com.javelin.swinglets.event.*;
018:
019: public class SMenuItem extends SAbstractButton {
020: /**
021: * Construct a SMenuItem.
022: */
023: public SMenuItem() {
024: }
025:
026: /**
027: * Construct a SMenuItem.
028: */
029: public SMenuItem(String text) {
030: super (text);
031: }
032:
033: /**
034: * Construct a SMenuItem.
035: */
036: public SMenuItem(String text, SLink link) {
037: super (text);
038:
039: this .link = link;
040: }
041:
042: /**
043: * Construct a SMenuItem.
044: */
045: public SMenuItem(SIcon icon) {
046: super ((String) null, icon);
047: }
048:
049: /**
050: * Construct a SMenuItem.
051: */
052: public SMenuItem(String text, SIcon icon) {
053: super (text, icon);
054: }
055:
056: /**
057: * Returns the name of the L&F class that renders this component.
058: */
059: public Class getUIClass() {
060: return SMenuItem.class;
061: }
062:
063: /**
064: * Get the top most component in the containment hierarchy.
065: * This will be a SContainer.
066: */
067: public SContainer getTopLevelAncestor() {
068: if (getMenuBar() != null) {
069: return getMenuBar().getTopLevelAncestor();
070: }
071:
072: return getMenuBar();
073: }
074:
075: /**
076: * Get the link for the text
077: */
078: public SLink getLink() {
079: return link;
080: }
081:
082: /**
083: * Get the link for the text
084: */
085: public SMenuItem setLink(SLink link) {
086: this .link = link;
087: return this ;
088: }
089:
090: /**
091: * Get the SMenuBar for this Menu, or null.
092: */
093: public SMenuBar getMenuBar() {
094: if (getParentMenu() != null) {
095: return getParentMenu().getMenuBar();
096: }
097:
098: return (SMenuBar) getParent();
099: }
100:
101: /**
102: * Get the parent Menu component.
103: * If this is null, try getParent() to get the SMenuBar.
104: */
105: public SMenu getParentMenu() {
106: return parentMenu;
107: }
108:
109: /**
110: * Set the parent component.
111: */
112: public void setParentMenu(SMenu parentMenu) {
113: this .parentMenu = parentMenu;
114: }
115:
116: /**
117: * Get the top most menu in the containment hierarchy.
118: * This will be a SMenuItem (or SMenu).
119: */
120: public SMenuItem getTopLevelMenu() {
121: if (getParentMenu() != null) {
122: return getParentMenu().getTopLevelMenu();
123: }
124:
125: return this ;
126: }
127:
128: /**
129: * Get the index of this menu in its parents menu, or -1
130: */
131: public int getIndex() {
132: if (getParentMenu() != null) {
133: return getParentMenu().getComponentIndex(this );
134: }
135:
136: return -1;
137: }
138:
139: // COMPONENT //////////////////////////////////////////////////////////////
140:
141: /**
142: * Gets the font of this component.
143: * <P>
144: * If the font is null return the theme.getMenuTextFont()
145: */
146: public SFont getFont() {
147: if (font == null && parent != null && parent.getFont() != null) {
148: return parent.getFont();
149: }
150:
151: if (font == null && parentMenu != null
152: && parentMenu.getFont() != null) {
153: return parentMenu.getFont();
154: }
155:
156: return font;
157: }
158:
159: /**
160: * Get the URL for this component. So that its action event will be called back.
161: * SOURCE_CONTAINER + SOURCE_COMPONENT.
162: */
163: public String getComponentUrl()
164: /*
165: out.print( "&" );
166: out.print( SConstants.SOURCE_COMPONENT );
167: out.print( "=" );
168: out.print( parentMenu != null ? parentMenu.getName() : getMenuBar().getName() );
169: */
170: {
171: if (componentUrl == null) {
172: StringBuffer buf = new StringBuffer();
173:
174: //Get the absolute Path
175: if (getSwingletManager().getUrl() != null) {
176: buf.append(getSwingletManager().getUrl());
177: buf.append("/");
178: }
179:
180: buf.append("?");
181:
182: buf.append(SConstants.SOURCE_CONTAINER);
183: buf.append("=");
184:
185: SContainer container = getTopLevelAncestor();
186:
187: if (container == null) {
188: buf.append(getName());
189: } else {
190: buf.append(container.getName());
191: }
192:
193: buf.append("&");
194: buf.append(SConstants.SOURCE_COMPONENT);
195: buf.append("=");
196: buf.append(parentMenu != null ? parentMenu.getName()
197: : getMenuBar().getName());
198:
199: componentUrl = buf.toString();
200: }
201:
202: return componentUrl;
203: }
204:
205: // EVENT /////////////////////////////////////////////////////////////////
206:
207: /**
208: * Process an ActionEvent. The default behaviour is to consume the event.
209: */
210: public void processActionEvent(ActionEvent event) {
211: //System.out.println( "MenuItem" + event );
212: super .processActionEvent(event);
213: }
214:
215: /**
216: * Paint this component as a String in the header.
217: */
218: public void paintHeader(Object out) {
219: getUI().updateHeader(out, this );
220: }
221:
222: /**
223: * Paint this component as a String.
224: */
225: public void paint(Object out) {
226: getUI().update(out, this );
227: }
228:
229: // PRIVATE //////////////////////////////////////////////////////////
230:
231: protected SMenu parentMenu;
232:
233: protected SLink link;
234:
235: }
|