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: package org.netbeans.modules.sql.framework.model;
042:
043: import java.util.Collection;
044: import java.util.HashMap;
045: import java.util.Iterator;
046: import java.util.Map;
047:
048: import org.w3c.dom.Element;
049: import org.w3c.dom.Node;
050: import org.w3c.dom.NodeList;
051: import net.java.hulp.i18n.Logger;
052: import com.sun.sql.framework.exception.BaseException;
053: import com.sun.sql.framework.utils.Attribute;
054: import org.netbeans.modules.etl.logger.Localizer;
055: import org.netbeans.modules.etl.logger.LogUtil;
056:
057: /**
058: * Stores UI attributes.
059: *
060: * @author Ritesh Adval
061: * @version $Revision$
062: */
063: public class GUIInfo implements Cloneable {
064:
065: private static transient final Logger mLogger = LogUtil
066: .getLogger(GUIInfo.class.getName());
067: private static transient final Localizer mLoc = Localizer.get();
068: /** Attribute key: expanded state of UI element */
069: public static final String ATTR_EXPANDED = "expanded";
070: /** Attribute key: expanded state of UI element */
071: public static final String ATTR_EXPANDED_HEIGHT = "expandedHeight";
072: /** Attribute key: expanded state of UI element */
073: public static final String ATTR_EXPANDED_WIDTH = "expandedWidth";
074: /** Attribute key: height of UI element */
075: public static final String ATTR_HEIGHT = "height";
076: public static final String ATTR_VISIBLE = "visible";
077: /** Attribute key: width of UI element */
078: public static final String ATTR_WIDTH = "width";
079: /** Attribute key: x-coordinate */
080: public static final String ATTR_X = "x";
081: /** Attribute key: y-coordinate */
082: public static final String ATTR_Y = "y";
083: /** XML element tag */
084: public static String TAG_GUIINFO = "guiInfo";
085:
086: /* Log4J category string */
087: static String logCategory = GUIInfo.class.getName();
088:
089: /* Log4J category string */
090: private static final String LOG_CATEGORY = GUIInfo.class.getName();
091:
092: /* Map of attributes */
093: private HashMap attrMap = new HashMap();
094:
095: /** Creates a new default instance of GUIInfo */
096: public GUIInfo() {
097: }
098:
099: /**
100: * Creates a new instance of GUIInfo with content derived from the given DOM element.
101: *
102: * @param element DOM element containing content info
103: * @throws BaseException if error occurs while parsing element
104: */
105: public GUIInfo(Element element) throws BaseException {
106: parseXML(element);
107: }
108:
109: /**
110: * New instance
111: *
112: * @param src - src
113: */
114: public GUIInfo(GUIInfo src) {
115:
116: if (src == null) {
117: throw new IllegalArgumentException(
118: " can not create GUIInfo using copy constuctor, src is null.");
119: }
120:
121: copyFrom(src);
122: }
123:
124: /**
125: * Clone
126: *
127: * @return cloned object
128: */
129: public Object clone() {
130: return new GUIInfo(this );
131: }
132:
133: /**
134: * Indicates whether given object is functionally identical to this instance.
135: *
136: * @param o Object to test for equality
137: * @return true if functionally identical, false otherwise
138: */
139: public boolean equals(Object o) {
140: boolean response = false;
141:
142: if (o == null) {
143: return false;
144: } else if (o == this ) {
145: return true;
146: }
147:
148: if (o instanceof GUIInfo) {
149: GUIInfo target = (GUIInfo) o;
150: response = (getX() == target.getX());
151: response &= (getY() == target.getY());
152: response &= (getWidth() == target.getWidth());
153: response &= (getHeight() == target.getHeight());
154: }
155:
156: return response;
157: }
158:
159: /**
160: * Gets an attribute based on its name
161: *
162: * @param attrName attribute Name
163: * @return Attribute instance associated with attrName, or null if none exists
164: */
165: public Attribute getAttribute(String attrName) {
166: return (Attribute) attrMap.get(attrName);
167: }
168:
169: /**
170: * @see SQLObject#getAttributeNames
171: */
172: public Collection getAttributeNames() {
173: return attrMap.keySet();
174: }
175:
176: /**
177: * @see SQLObject#getAttributeObject
178: */
179: public Object getAttributeValue(String attrName) {
180: Attribute attr = getAttribute(attrName);
181: return (attr != null) ? attr.getAttributeValue() : null;
182: }
183:
184: /**
185: * Gets height attribute.
186: *
187: * @return height
188: */
189: public int getHeight() {
190: Attribute attr = getAttribute(ATTR_HEIGHT);
191: if (attr != null && attr.getAttributeValue() != null) {
192: Integer val = (Integer) attr.getAttributeValue();
193: return val.intValue();
194: }
195:
196: return -1;
197: }
198:
199: /**
200: * Gets width attribute.
201: *
202: * @return width
203: */
204: public int getWidth() {
205: Attribute attr = getAttribute(ATTR_WIDTH);
206: if (attr != null && attr.getAttributeValue() != null) {
207: Integer val = (Integer) attr.getAttributeValue();
208: return val.intValue();
209: }
210:
211: return -1;
212: }
213:
214: /**
215: * Gets x-coordinate attribute.
216: *
217: * @return x-coordinate
218: */
219: public int getX() {
220: Attribute attr = getAttribute(ATTR_X);
221: if (attr != null && attr.getAttributeValue() != null) {
222: Integer val = (Integer) attr.getAttributeValue();
223: return val.intValue();
224: }
225:
226: return -1;
227: }
228:
229: /**
230: * Gets y-coordinate attribute.
231: *
232: * @return y-coordinate
233: */
234: public int getY() {
235: Attribute attr = getAttribute(ATTR_Y);
236: if (attr != null && attr.getAttributeValue() != null) {
237: Integer val = (Integer) attr.getAttributeValue();
238: return val.intValue();
239: }
240:
241: return -1;
242: }
243:
244: /**
245: * Overrides hashCode to reflect internal content.
246: *
247: * @return computed hashcode for this instance.
248: */
249: public int hashCode() {
250: return getX() + getY() + getWidth() + getHeight();
251: }
252:
253: /**
254: * Gets expanded state of the gui element
255: *
256: * @return expanded state
257: */
258: public boolean isExpanded() {
259: Attribute attr = getAttribute(ATTR_EXPANDED);
260: if (attr != null && attr.getAttributeValue() != null) {
261: Boolean val = (Boolean) attr.getAttributeValue();
262: return val.booleanValue();
263: }
264:
265: return true;
266: }
267:
268: public boolean isVisible() {
269: Attribute attr = getAttribute(ATTR_VISIBLE);
270: if (attr != null && attr.getAttributeValue() != null) {
271: Boolean val = (Boolean) attr.getAttributeValue();
272: return val.booleanValue();
273: }
274:
275: return true;
276: }
277:
278: /**
279: * Parses the XML content, if any, using the given Element as a source for
280: * reconstituting the member variables and collections of this instance.
281: *
282: * @param xmlElement DOM element containing XML marshalled version of a GUIInfo
283: * instance
284: * @exception BaseException thrown while parsing XML
285: */
286: public void parseXML(Element xmlElement) throws BaseException {
287: if (xmlElement == null) {
288: return;
289: }
290:
291: NodeList nodes;
292: nodes = xmlElement.getChildNodes();
293: Element element;
294:
295: try {
296: for (int i = 0; i < nodes.getLength(); i++) {
297: if (nodes.item(i).getNodeType() == Node.ELEMENT_NODE) {
298: element = (Element) nodes.item(i);
299: Attribute attr = new Attribute();
300: attr.parseXMLString(element);
301: attrMap.put(attr.getAttributeName(), attr);
302: }
303: }
304: } catch (Exception e) {
305: throw new BaseException(e);
306: }
307: }
308:
309: /**
310: * Sets an attribute name-value pair. The name of the Attribute should be one of the
311: * String constants defined in this class.
312: *
313: * @param attrName attribute Name
314: * @param val value of the attribute
315: */
316: public void setAttribute(String attrName, Object val) {
317: Attribute attr = getAttribute(attrName);
318: if (attr != null) {
319: attr.setAttributeValue(val);
320: } else {
321: attr = new Attribute(attrName, val);
322: attrMap.put(attrName, attr);
323: }
324: }
325:
326: /**
327: * set the expanded state of
328: *
329: * @param expand expanded state of the gui element
330: */
331: public void setExpanded(boolean expand) {
332: setAttribute(ATTR_EXPANDED, new Boolean(expand));
333: }
334:
335: /**
336: * Sets height attribute.
337: *
338: * @param height height of UI object
339: */
340: public void setHeight(int height) {
341: setAttribute(ATTR_HEIGHT, new Integer(height));
342: }
343:
344: public void setVisible(boolean visible) {
345: setAttribute(ATTR_VISIBLE, new Boolean(visible));
346: }
347:
348: /**
349: * Sets width attribute.
350: *
351: * @param width width of UI object
352: */
353: public void setWidth(int width) {
354: setAttribute(ATTR_WIDTH, new Integer(width));
355: }
356:
357: /**
358: * Sets x-coordinate attribute.
359: *
360: * @param x x-coordinate of UI object
361: */
362: public void setX(int x) {
363: setAttribute(ATTR_X, new Integer(x));
364: }
365:
366: /**
367: * Sets y-coordinate attribute.
368: *
369: * @param y y-coordinate of UI object
370: */
371: public void setY(int y) {
372: setAttribute(ATTR_Y, new Integer(y));
373: }
374:
375: /**
376: * Gets XML representation of this object's set of Attributes, appending the given
377: * String to the beginning of each line in the XML document.
378: *
379: * @param prefix Prefix string to be appended to each line of the generated XML
380: * document
381: * @return XML document representing contents of this object
382: */
383: public String toXMLString(String prefix) {
384: StringBuilder xml = new StringBuilder(prefix + "<"
385: + TAG_GUIINFO + ">\n");
386: Iterator it = attrMap.entrySet().iterator();
387:
388: while (it.hasNext()) {
389: Map.Entry entry = (Map.Entry) it.next();
390: Attribute attr = (Attribute) entry.getValue();
391: if (attr != null) {
392: xml.append(attr.toXMLString(prefix + "\t"));
393: }
394: }
395:
396: xml.append(prefix).append("</" + TAG_GUIINFO + ">\n");
397:
398: return xml.toString();
399: }
400:
401: private void copyFrom(GUIInfo source) {
402: // clone attributes
403: Collection attrNames = source.getAttributeNames();
404: Iterator it = attrNames.iterator();
405:
406: while (it.hasNext()) {
407: String name = (String) it.next();
408: Attribute attr = source.getAttribute(name);
409: if (attr != null) {
410: try {
411: Attribute copiedAttr = (Attribute) attr.clone();
412: this .attrMap.put(name, copiedAttr);
413: } catch (CloneNotSupportedException ex) {
414: mLogger
415: .errorNoloc(
416: mLoc
417: .t(
418: "PRSR134: Failed to copy source guinfo attributes{0}",
419: LOG_CATEGORY), ex);
420: }
421: }
422: }
423: }
424: }
|