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.impl;
042:
043: import java.net.URL;
044: import java.util.HashMap;
045: import java.util.Map;
046: import java.util.MissingResourceException;
047: import java.util.ResourceBundle;
048: import net.java.hulp.i18n.Logger;
049: import javax.swing.Icon;
050: import javax.swing.ImageIcon;
051: import org.netbeans.modules.etl.logger.Localizer;
052: import org.netbeans.modules.etl.logger.LogUtil;
053: import org.w3c.dom.Element;
054:
055: /**
056: *
057: */
058: public class CommonNodeX {
059:
060: public static final String ATTR_NAME = "name";
061: public static final String TAG_ATTRIBUTE = "attr";
062: private static final String ATTR_STRING_VALUE = "stringvalue";
063: private static final String ATTR_BOOLEAN_VALUE = "boolvalue";
064: private static final String ATTR_INT_VALUE = "intvalue";
065: private static final String ATTR_URL_VALUE = "urlvalue";
066: private static final String KEY_RESOURCE_BUNDLE = "localizingBundle";
067: private static final String KEY_TOOLBARCATEGORY = "ToolbarCategory";
068: private static final String KEY_TOOLTIP = "ToolTip";
069: private static final String KEY_DISPLAY_NAME = "DisplayName";
070: private static final String KEY_ICON_URL = "iconURL";
071: private ResourceBundle resBundle = null;
072: protected Map attributes = new HashMap();
073: protected String name = "unknown";
074: private static final String LOG_CATEGORY = CommonNodeX.class
075: .getName();
076: private static transient final Logger mLogger = LogUtil
077: .getLogger(CommonNodeX.class.getName());
078: private static transient final Localizer mLoc = Localizer.get();
079:
080: public CommonNodeX(Element elem) {
081: this .name = elem.getAttribute(ATTR_NAME);
082: }
083:
084: private void setResourceBundle() {
085: if (resBundle == null) {
086: String resBundleName = (String) this .attributes
087: .get(KEY_RESOURCE_BUNDLE);
088: if (resBundleName != null) {
089: synchronized (this ) {
090: resBundle = ResourceBundle.getBundle(resBundleName);
091: }
092: }
093: }
094: }
095:
096: protected String getLocalizedValue(String key) {
097: if (key == null) {
098: return key;
099: }
100:
101: if (resBundle == null) {
102: setResourceBundle();
103: }
104:
105: if (resBundle == null) {
106: return key;
107: }
108:
109: try {
110: key = resBundle.getString(key);
111: } catch (MissingResourceException ex) {
112: // @TODO Log this exception
113: }
114:
115: return key;
116: }
117:
118: protected Object getAttributeValue(Element attrElement) {
119: Object ret = null;
120: if (attrElement.getAttributeNode(ATTR_STRING_VALUE) != null) {
121: ret = attrElement.getAttribute(ATTR_STRING_VALUE);
122: } else if (attrElement.getAttributeNode(ATTR_BOOLEAN_VALUE) != null) {
123: ret = Boolean.valueOf(attrElement
124: .getAttribute(ATTR_BOOLEAN_VALUE));
125: } else if (attrElement.getAttributeNode(ATTR_INT_VALUE) != null) {
126: ret = Integer.valueOf(attrElement
127: .getAttribute(ATTR_INT_VALUE));
128: } else if (attrElement.getAttributeNode(ATTR_URL_VALUE) != null) {
129: try {
130: ret = this .getClass().getResource(
131: attrElement.getAttribute(ATTR_URL_VALUE));
132: } catch (Exception ex) {
133: mLogger.errorNoloc(mLoc.t("PRSR107: Exception{0}",
134: LOG_CATEGORY), ex);
135: }
136: }
137: return ret;
138: }
139:
140: /**
141: * Returns Operator or Category name. No I18N
142: * @return name
143: */
144: public String getName() {
145: return this .name;
146: }
147:
148: public String getDisplayName() {
149: return this .getLocalizedValue((String) this .attributes
150: .get(KEY_DISPLAY_NAME));
151: }
152:
153: /**
154: * Gets tool tip for the operator
155: * @return tool tip
156: */
157: public String getToolTip() {
158: return getLocalizedValue((String) this .attributes
159: .get(KEY_TOOLTIP));
160: }
161:
162: /**
163: * @see org.netbeans.modules.sql.framework.model.IOperatorXmlInfoCategory#getToolbarType()
164: */
165: public int getToolbarType() {
166: Integer toolbarType = (Integer) this .attributes
167: .get(KEY_TOOLBARCATEGORY);
168: int ret = org.netbeans.modules.sql.framework.ui.graph.IOperatorXmlInfoModel.CATEGORY_ALL;
169: if (toolbarType != null) {
170: ret = toolbarType.intValue();
171: }
172: return ret;
173: }
174:
175: /**
176: * Gets the icon for this operator
177: *
178: * @return Icon for this category
179: */
180: public Icon getIcon() {
181: return new ImageIcon((URL) this .attributes.get(KEY_ICON_URL));
182: }
183:
184: public String toString() {
185: return this.getDisplayName();
186: }
187: }
|