001: package com.xoetrope.carousel.visualizer;
002:
003: import java.awt.Color;
004: import java.awt.Component;
005: import java.awt.Font;
006: import javax.swing.ImageIcon;
007: import javax.swing.JTree;
008: import javax.swing.tree.DefaultMutableTreeNode;
009: import javax.swing.tree.DefaultTreeCellRenderer;
010: import javax.swing.JLabel;
011:
012: import net.xoetrope.xui.data.XModel;
013: import net.xoetrope.xui.XProjectManager;
014: import net.xoetrope.xui.XProject;
015:
016: /**
017: * A tree cell renderer for the model visualizer
018: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
019: * the GNU Public License (GPL), please see license.txt for more details. If
020: * you make commercial use of this software you must purchase a commercial
021: * license from Xoetrope.</p>
022: * <p> $Revision: 1.9 $</p>
023: */
024: public class ModelVisualiserTreeCellRenderer extends
025: DefaultTreeCellRenderer {
026: private int amendMode;
027: private XModel editModel;
028:
029: public static final int AMEND_STATIC = 1;
030: public static final int AMEND_DATABASE = 2;
031: public static final int AMEND_SELECT = 3;
032:
033: /** Font used if the string to be displayed isn't a font. */
034: protected Font defaultFont;
035: static protected ImageIcon dataItemIcon, dataListIcon, dataSetIcon,
036: datasetsIcon;
037: static protected ImageIcon databaseIcon, connectionIcon,
038: resultsetIcon, serviceIcon;
039:
040: /**
041: * The owner project and the context in which this object operates.
042: */
043: protected XProject currentProject = XProjectManager
044: .getCurrentProject();
045:
046: /** Color to use for the background when selected. */
047: // static protected final Color SelectedBackgroundColor = Color.yellow; //new Color(0, 0, 128);
048: /** Whether or not the item that was last configured is selected. */
049: protected boolean selected;
050:
051: public ModelVisualiserTreeCellRenderer(int mode, XModel model) {
052: this (mode);
053: editModel = model;
054:
055: defaultFont = new Font("arial", 12, 0); //XEditorDefaults.defaultFont;
056: }
057:
058: public ModelVisualiserTreeCellRenderer(int mode) {
059: setBackgroundSelectionColor(Color.white);
060: setIcons();
061: amendMode = mode;
062: }
063:
064: private void setIcons() {
065: try {
066: if (dataItemIcon != null) {
067: String iconPath = "com/xoetrope/carousel/resources/icons/";
068: dataItemIcon = new ImageIcon(currentProject
069: .getImage(iconPath + "dataitemicon.gif"));
070: dataListIcon = new ImageIcon(currentProject
071: .getImage(iconPath + "datalisticon.gif"));
072: dataSetIcon = new ImageIcon(currentProject
073: .getImage(iconPath + "seticon.gif"));
074: datasetsIcon = new ImageIcon(currentProject
075: .getImage(iconPath + "datasetsicon.gif"));
076: databaseIcon = new ImageIcon(currentProject
077: .getImage(iconPath + "databaseicon.gif"));
078: connectionIcon = new ImageIcon(currentProject
079: .getImage(iconPath + "connectionicon.gif"));
080: resultsetIcon = new ImageIcon(currentProject
081: .getImage(iconPath + "resultseticon.gif"));
082: serviceIcon = new ImageIcon(currentProject
083: .getImage(iconPath + "serviceicon.gif"));
084: }
085: } catch (Exception e) {
086: System.out.println("Couldn't load images: " + e);
087: }
088: }
089:
090: /**
091: * This is messaged from JTree whenever it needs to get the size
092: * of the component or it wants to draw it.
093: * This attempts to set the font based on value, which will be
094: * a TreeNode.
095: */
096: public Component getTreeCellRendererComponent(JTree tree,
097: Object value, boolean isSelected, boolean expanded,
098: boolean leaf, int row, boolean hasFocus) {
099: JLabel lbl = (JLabel) super .getTreeCellRendererComponent(tree,
100: value, selected, expanded, leaf, row, hasFocus);
101:
102: lbl.setBackground(new Color(230, 230, 230));
103: String stringValue = tree.convertValueToText(value, isSelected,
104: expanded, leaf, row, hasFocus);
105:
106: /* Set the text. */
107: setText(stringValue);
108:
109: /* Tooltips used by the tree. */
110: setToolTipText(stringValue);
111:
112: DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
113: if (node == null)
114: return null;
115: if (amendMode == AMEND_STATIC)
116: setStaticIcon(node);
117: else if (amendMode == AMEND_DATABASE)
118: setDatabaseIcon(node);
119: else if (amendMode == AMEND_SELECT)
120: setSelectIcon(node);
121:
122: Object userObject = ((DefaultMutableTreeNode) value)
123: .getUserObject();
124: //setForeground( hasFocus ? XEditorDefaults.darkBlue : Color.black );
125: lbl.setOpaque(true);
126: if (isSelected) {
127: lbl.setForeground(Color.white);
128: lbl.setBackground(new Color(49, 106, 197));
129: } else {
130: lbl.setForeground(Color.black);
131: lbl.setBackground(Color.white);
132: }
133: lbl.setFont(defaultFont);
134:
135: /* Update the selected flag for the next paint. */
136: selected = isSelected;
137:
138: return lbl;
139: }
140:
141: private void setStaticIcon(DefaultMutableTreeNode nod) {
142: if (editModel != null) {
143: String dataName = getTreePath(nod, false);
144:
145: if (dataName.compareTo("") == 0)
146: setIcon(datasetsIcon);
147: else {
148: XModel model = (XModel) editModel.get(dataName);
149: String tag = model.getTagName();
150: if (tag == null)
151: setIcon(datasetsIcon);
152: else if (tag.compareTo("dataset") == 0)
153: setIcon(dataSetIcon);
154: else if (tag.compareTo("data") == 0)
155: setIcon(dataItemIcon);
156: else if (tag.compareTo("list") == 0)
157: setIcon(dataListIcon);
158: }
159: }
160: }
161:
162: private void setSelectIcon(DefaultMutableTreeNode node) {
163: XModel model = null;
164: if (!(node instanceof ModelVisualiserTree.XDefaultMutableTreeNode))
165: return;
166: model = ((ModelVisualiserTree.XDefaultMutableTreeNode) node)
167: .getModel();
168:
169: ImageIcon icon = XModelVisualizerFactory.getIcon(model);
170: if (icon != null)
171: setIcon(icon);
172: }
173:
174: /*
175: private void setSelectIcon( DefaultMutableTreeNode node )
176: {
177: ImageIcon icon = null;
178: String path = getTreePath( node, false );
179: XModel model = (XModel)currentProject.getModel().get( path );
180:
181: if ( XModelVisualizerFactory.isTransientProperty( model ))
182: icon = XModelVisualizerFactory.getTransientPropertyIcon();
183:
184: if ( icon != null )
185: setIcon( icon );
186: }
187: */
188:
189: /*
190: private void setSelectIcon( DefaultMutableTreeNode nod )
191: {
192: String dataName = getTreePath( nod, false );
193: ImageIcon icon = XModelVisualizerFactory.getIcon( dataName );
194: if ( icon != null )
195: setIcon( icon );
196: }
197: */
198:
199: /*
200: private void setSelectIcon( DefaultMutableTreeNode nod )
201: {
202: String dataName = getTreePath( nod, false );
203:
204: if ( dataName.compareTo( "" ) == 0 )
205: setIcon( datasetsIcon );
206: else {
207: XModel model = ( XModel )currentProject.getModel().get( dataName );
208: String tag = ( model != null ? model.getTagName() : null );
209: if ( tag == null ) {
210: if ( model instanceof DatabaseTableModel )
211: setIcon( resultsetIcon );
212: else if ( ( model != null ) && ( model.get()instanceof XServiceModelNode ) )
213: setIcon( serviceIcon );
214: else
215: setIcon( datasetsIcon );
216: }
217: else if ( tag.compareTo( "dataset" ) == 0 )
218: setIcon( dataSetIcon );
219: else if ( tag.compareTo( "data" ) == 0 )
220: setIcon( dataItemIcon );
221: else if ( tag.compareTo( "list" ) == 0 )
222: setIcon( dataListIcon );
223: else
224: System.out.println( "model: " + model );
225: }
226: }
227: */
228:
229: public void setDatabaseIcon(DefaultMutableTreeNode node) {
230: if (node.isRoot())
231: setIcon(databaseIcon);
232: else {
233: // Hashtable table = XLibXModelManager.getInstance().getDatabaseNodes();
234: // XmlElement element = ( XmlElement )table.get( node.getLastLeaf().toString() );
235: // if ( element.getName().compareTo( "Connection" ) == 0 )
236: // setIcon( connectionIcon );
237: // else if ( element.getName().compareTo( "ResultSet" ) == 0 )
238: // setIcon( resultsetIcon );
239: }
240: }
241:
242: private String getTreePath(DefaultMutableTreeNode nod,
243: boolean startAtRoot) {
244: String modelName = null;
245: Object path[] = nod.getPath();
246:
247: modelName = "";
248: int start = startAtRoot ? 0 : 1;
249: for (int i = start; i < path.length; i++) {
250: String pathSection = path[i].toString();
251: if (pathSection.indexOf(": ") > 0)
252: pathSection = pathSection.substring(0, pathSection
253: .indexOf(": "));
254:
255: modelName += "/" + pathSection;
256: }
257:
258: if (modelName == null)
259: return "";
260: else if (modelName.trim().compareTo("") != 0)
261: return modelName.substring(1);
262: else
263: return "";
264: }
265:
266: }
|