001: /* *************************************************************************
002:
003: Millstone(TM)
004: Open Sourced User Interface Library for
005: Internet Development with Java
006:
007: Millstone is a registered trademark of IT Mill Ltd
008: Copyright (C) 2000-2005 IT Mill Ltd
009:
010: *************************************************************************
011:
012: This library is free software; you can redistribute it and/or
013: modify it under the terms of the GNU Lesser General Public
014: license version 2.1 as published by the Free Software Foundation.
015:
016: This library is distributed in the hope that it will be useful,
017: but WITHOUT ANY WARRANTY; without even the implied warranty of
018: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: Lesser General Public License for more details.
020:
021: You should have received a copy of the GNU Lesser General Public
022: License along with this library; if not, write to the Free Software
023: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024:
025: *************************************************************************
026:
027: For more information, contact:
028:
029: IT Mill Ltd phone: +358 2 4802 7180
030: Ruukinkatu 2-4 fax: +358 2 4802 7181
031: 20540, Turku email: info@itmill.com
032: Finland company www: www.itmill.com
033:
034: Primary source for MillStone information and releases: www.millstone.org
035:
036: ********************************************************************** */
037:
038: package org.millstone.base.ui;
039:
040: import org.millstone.base.terminal.PaintTarget;
041: import org.millstone.base.terminal.PaintException;
042: import org.millstone.base.terminal.Resource;
043:
044: /** Link component.
045: * Link is used to create external or internal URL links.
046: *
047: * Internal links can be used to create action items, which
048: * change the state to application to one of the predefined states.
049: * For example, a link can be created for existing MenuTree items.
050: *
051: * @author IT Mill Ltd.
052: * @version 3.1.1
053: * @since 3.0
054: */
055: public class Link extends AbstractComponent {
056:
057: /* Target window border type constant: No window border */
058: public static final int TARGET_BORDER_NONE = Window.BORDER_NONE;
059: /* Target window border type constant: Minimal window border */
060: public static final int TARGET_BORDER_MINIMAL = Window.BORDER_MINIMAL;
061: /* Target window border type constant: Default window border */
062: public static final int TARGET_BORDER_DEFAULT = Window.BORDER_DEFAULT;
063:
064: private Resource resource = null;
065: private Window window = null;
066: private String targetName;
067: private int targetBorder = TARGET_BORDER_DEFAULT;
068: private int targetWidth = -1;
069: private int targetHeight = -1;
070:
071: /** Creates a new link. */
072: public Link() {
073:
074: }
075:
076: /** Creates a new link to a window. */
077: public Link(Window window) {
078:
079: // Set the link caption to match window caption
080: setCaption(window.getCaption());
081:
082: // Set the target
083: setTargetName(window.getName());
084:
085: setTargetName(window.getName());
086: setTargetWidth(window.getWidth());
087: setTargetHeight(window.getHeight());
088: setTargetBorder(window.getBorder());
089: }
090:
091: /** Creates a new instance of Link */
092: public Link(String caption, Resource resource) {
093: setCaption(caption);
094: this .resource = resource;
095: }
096:
097: /** Creates a new instance of Link that opens a new window.
098: *
099: *
100: * @param caption Link text.
101: * @param targetName The name of the target window where the link
102: * opens to. Empty name of null implies that
103: * the target is opened to the window containing the link.
104: * @param width Width of the target window.
105: * @param height Height of the target window.
106: * @param border Borget style of the target window.
107: *
108: */
109: public Link(String caption, Resource resource, String targetName,
110: int width, int height, int border) {
111: setCaption(caption);
112: this .resource = resource;
113: setTargetName(targetName);
114: setTargetWidth(width);
115: setTargetHeight(height);
116: setTargetBorder(border);
117: }
118:
119: /** Get component UIDL tag.
120: * @return Component UIDL tag as string.
121: */
122: public String getTag() {
123: return "link";
124: }
125:
126: /** Paint the content of this component.
127: * @param event PaintEvent.
128: * @throws PaintException The paint operation failed.
129: */
130: public void paintContent(PaintTarget target) throws PaintException {
131:
132: if (window != null)
133: target.addAttribute("src", window.getURL().toString());
134: else if (resource != null)
135: target.addAttribute("src", resource);
136: else
137: return;
138:
139: // Target window name
140: String name = getTargetName();
141: if (name != null && name.length() > 0)
142: target.addAttribute("name", name);
143:
144: // Target window size
145: if (getTargetWidth() >= 0)
146: target.addAttribute("width", getTargetWidth());
147: if (getTargetHeight() >= 0)
148: target.addAttribute("height", getTargetHeight());
149:
150: // Target window border
151: switch (getTargetBorder()) {
152: case TARGET_BORDER_MINIMAL:
153: target.addAttribute("border", "minimal");
154: break;
155: case TARGET_BORDER_NONE:
156: target.addAttribute("border", "none");
157: break;
158: }
159: }
160:
161: /** Returns the target window border.
162: * @return int
163: */
164: public int getTargetBorder() {
165: return targetBorder;
166: }
167:
168: /** Returns the target window height or -1 if not set.
169: * @return int
170: */
171: public int getTargetHeight() {
172: return targetHeight < 0 ? -1 : targetHeight;
173: }
174:
175: /** Returns the target window name. Empty name of null implies that
176: * the target is opened to the window containing the link.
177: * @return String
178: */
179: public String getTargetName() {
180: return targetName;
181: }
182:
183: /** Returns the target window width or -1 if not set.
184: * @return int
185: */
186: public int getTargetWidth() {
187: return targetWidth < 0 ? -1 : targetWidth;
188: }
189:
190: /** Sets the border of the target window.
191: * @param targetBorder The targetBorder to set
192: */
193: public void setTargetBorder(int targetBorder) {
194: if (targetBorder == TARGET_BORDER_DEFAULT
195: || targetBorder == TARGET_BORDER_MINIMAL
196: || targetBorder == TARGET_BORDER_NONE)
197: this .targetBorder = targetBorder;
198: }
199:
200: /** Sets the target window height.
201: * @param targetHeight The targetHeight to set
202: */
203: public void setTargetHeight(int targetHeight) {
204: this .targetHeight = targetHeight;
205: }
206:
207: /** Sets the target window name.
208: * @param targetName The targetName to set
209: */
210: public void setTargetName(String targetName) {
211: this .targetName = targetName;
212: }
213:
214: /** Sets the target window width.
215: * @param targetWidth The targetWidth to set
216: */
217: public void setTargetWidth(int targetWidth) {
218: this .targetWidth = targetWidth;
219: }
220:
221: /** Returns the resource this link opens.
222: * @return Resource
223: */
224: public Resource getResource() {
225: return resource;
226: }
227:
228: /** Returns the window this link opens.
229: * @return Window
230: */
231: public Window getWindow() {
232: return window;
233: }
234:
235: /** Sets the resource this link opens.
236: * @param resource The resource to set
237: */
238: public void setResource(Resource resource) {
239: this .resource = resource;
240: if (resource != null)
241: window = null;
242: }
243:
244: /** Sets the window this link opens.
245: * @param window The window to set
246: */
247: public void setWindow(Window window) {
248: this.window = window;
249: if (window != null)
250: this.resource = null;
251: }
252:
253: }
|