001: package org.enhydra.jawe.components;
002:
003: import java.awt.Component;
004: import java.util.HashSet;
005: import java.util.Iterator;
006: import java.util.List;
007: import java.util.Set;
008:
009: import javax.swing.Icon;
010: import javax.swing.ImageIcon;
011: import javax.swing.JTree;
012: import javax.swing.tree.DefaultMutableTreeNode;
013: import javax.swing.tree.DefaultTreeCellRenderer;
014:
015: import org.enhydra.jawe.CombinedIcon;
016: import org.enhydra.jawe.JaWEComponent;
017: import org.enhydra.jawe.JaWEManager;
018: import org.enhydra.jawe.ResourceManager;
019: import org.enhydra.jawe.Utils;
020: import org.enhydra.jawe.base.controller.JaWEType;
021: import org.enhydra.jawe.base.xpdlvalidator.ValidationError;
022: import org.enhydra.shark.xpdl.XMLElement;
023: import org.enhydra.shark.xpdl.XMLUtil;
024: import org.enhydra.shark.xpdl.XMLValidationError;
025: import org.enhydra.shark.xpdl.elements.Activity;
026:
027: /**
028: * Used to paint Package hierarchy tree.
029: *
030: * @author Sasa Bojanic
031: * @author Miroslav Popov
032: */
033: public class XPDLTreeCellRenderer extends DefaultTreeCellRenderer {
034:
035: protected JaWEComponent owner;
036: protected Icon defaultImage;
037: protected Icon warning;
038: protected Icon error;
039:
040: protected Set errors = new HashSet();
041: protected Set warnings = new HashSet();
042:
043: public XPDLTreeCellRenderer(JaWEComponent comp) {
044: super ();
045: this .owner = comp;
046: try {
047: defaultImage = new ImageIcon(ResourceManager.class
048: .getClassLoader().getResource(
049: "org/enhydra/jawe/images/default.gif"));
050: warning = new ImageIcon(
051: ResourceManager.class
052: .getClassLoader()
053: .getResource(
054: "org/enhydra/jawe/images/warning_icon_small.gif"));
055: error = new ImageIcon(
056: ResourceManager.class
057: .getClassLoader()
058: .getResource(
059: "org/enhydra/jawe/images/error_icon_small.gif"));
060: } catch (Exception e) {
061: }
062: }
063:
064: public Component getTreeCellRendererComponent(JTree tree,
065: Object value, boolean sel, boolean expanded, boolean leaf,
066: int row, boolean pHasFocus) {
067:
068: super .getTreeCellRendererComponent(tree, value, sel, expanded,
069: leaf, row, pHasFocus);
070:
071: DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
072: Object userObj = node.getUserObject();
073:
074: boolean err = false;
075: boolean war = false;
076:
077: if (warnings.contains(userObj))
078: war = true;
079: if (errors.contains(userObj))
080: err = true;
081:
082: Icon image = null;
083:
084: JaWEType jtype = null;
085: if (userObj instanceof XMLElement)
086: jtype = JaWEManager.getInstance().getJaWEController()
087: .getTypeResolver()
088: .getJaWEType((XMLElement) userObj);
089:
090: if (jtype != null) {
091: if (err) {
092: // image = jtype.getErrorIcon();
093: image = new CombinedIcon(getIcon((XMLElement) userObj,
094: jtype), error);
095: } else if (war) {
096: image = new CombinedIcon(getIcon((XMLElement) userObj,
097: jtype), warning);
098: // image = jtype.getWarningIcon();
099: } else
100: image = getIcon((XMLElement) userObj, jtype);
101: }
102: if (image == null) {
103: if (err) {
104: // image = error;
105: image = new CombinedIcon(defaultImage, error);
106: } else if (war) {
107: // image = warning;
108: image = new CombinedIcon(defaultImage, warning);
109: } else {
110: image = defaultImage;
111: }
112: }
113:
114: setIcon(image);
115:
116: return this ;
117: }
118:
119: public void setValidationErrors(List verrors) {
120: Set serr = new HashSet();
121: Set swarn = new HashSet();
122: if (verrors != null) {
123: for (int i = 0; i < verrors.size(); i++) {
124: ValidationError verr = (ValidationError) verrors.get(i);
125: if (verr.getType()
126: .equals(XMLValidationError.TYPE_ERROR)) {
127: serr.add(verr.getElement());
128: } else {
129: swarn.add(verr.getElement());
130: }
131: }
132: }
133: this .errors = new HashSet(serr);
134: this .warnings = new HashSet(swarn);
135:
136: Iterator it = serr.iterator();
137: while (it.hasNext()) {
138: XMLElement el = (XMLElement) it.next();
139: this .errors.addAll(XMLUtil.getAllParents(el));
140: }
141: it = swarn.iterator();
142: while (it.hasNext()) {
143: XMLElement el = (XMLElement) it.next();
144: this .warnings.addAll(XMLUtil.getAllParents(el));
145: }
146: }
147:
148: public void setErrors(Set errs) {
149: this .errors = errs;
150: }
151:
152: public Set getErrors() {
153: return errors;
154: }
155:
156: public void setWarnings(Set wrns) {
157: this .warnings = wrns;
158: }
159:
160: public Set getWarnings() {
161: return warnings;
162: }
163:
164: public ImageIcon getIcon(XMLElement el, JaWEType jtype) {
165: if (el instanceof Activity) {
166: String icon = ((Activity) el).getIcon();
167:
168: ImageIcon ii = null;
169: if (!icon.equals("")) {
170: ii = (ImageIcon) Utils.getOriginalActivityIconsMap()
171: .get(icon);
172: }
173:
174: if (ii == null) {
175: ii = jtype.getIcon();
176: }
177:
178: return ii;
179:
180: }
181: return jtype.getIcon();
182: }
183:
184: }
|