001: package com.xoetrope.carousel.services.dialog;
002:
003: import java.util.Hashtable;
004:
005: import java.awt.Color;
006: import java.awt.Component;
007: import java.awt.Font;
008: import javax.swing.ImageIcon;
009: import javax.swing.JTree;
010: import javax.swing.tree.DefaultMutableTreeNode;
011: import javax.swing.tree.DefaultTreeCellRenderer;
012:
013: import net.xoetrope.editor.XEditorDefaults;
014: import net.xoetrope.optional.data.sql.DatabaseTableModel;
015: import net.xoetrope.optional.service.XServiceModelNode;
016: import com.xoetrope.carousel.services.StaticDataEditor;
017: import com.xoetrope.carousel.services.ModelManager; // import com.xoetrope.carousel.services.ProjectRouteManager;
018: import com.xoetrope.carousel.services.RouteManager;
019: import net.xoetrope.xml.XmlElement;
020: import net.xoetrope.xui.XProject;
021: import net.xoetrope.xui.XProjectManager;
022: import net.xoetrope.xui.data.XModel;
023:
024: /**
025: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
026: * the GNU Public License (GPL), please see license.txt for more details. If
027: * you make commercial use of this software you must purchase a commercial
028: * license from Xoetrope.</p>
029: * <p> $Revision: 1.7 $</p>
030: */
031: public class XLibTreeCellRenderer extends DefaultTreeCellRenderer {
032: private XProject currentProject;
033: private int amendMode;
034: private XModel editModel;
035:
036: public XLibTreeCellRenderer(int mode, XModel model) {
037: editModel = model;
038: amendMode = mode;
039: init();
040: }
041:
042: public XLibTreeCellRenderer(int mode) {
043: amendMode = mode;
044: init();
045: }
046:
047: /** Font used if the string to be displayed isn't a font. */
048: static protected Font defaultFont;
049: static protected ImageIcon dataItemIcon, dataListIcon, dataSetIcon,
050: datasetsIcon;
051: static protected ImageIcon databaseIcon, connectionIcon,
052: resultsetIcon, serviceIcon;
053:
054: /** Color to use for the background when selected. */
055: static protected final Color SelectedBackgroundColor = Color.yellow; //new Color(0, 0, 128);
056:
057: /** Whether or not the item that was last configured is selected. */
058: protected boolean selected;
059:
060: public void init() {
061: currentProject = XProjectManager.getCurrentProject();
062: if (dataItemIcon == null) {
063: try {
064: defaultFont = XEditorDefaults.defaultFont;
065: } catch (Exception e) {
066: }
067: try {
068: dataItemIcon = new ImageIcon(currentProject
069: .getImage("dataitemicon.gif"));
070: dataListIcon = new ImageIcon(currentProject
071: .getImage("datalisticon.gif"));
072: dataSetIcon = new ImageIcon(currentProject
073: .getImage("seticon.gif"));
074: datasetsIcon = new ImageIcon(currentProject
075: .getImage("datasetsicon.gif"));
076: databaseIcon = new ImageIcon(currentProject
077: .getImage("databaseicon.gif"));
078: connectionIcon = new ImageIcon(currentProject
079: .getImage("connectionicon.gif"));
080: resultsetIcon = new ImageIcon(currentProject
081: .getImage("resultseticon.gif"));
082: serviceIcon = new ImageIcon(currentProject
083: .getImage("serviceicon.gif"));
084: } catch (Exception e) {
085: System.out.println("Couldn't load images: " + e);
086: }
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: Font font;
100: String stringValue = tree.convertValueToText(value, isSelected,
101: expanded, leaf, row, hasFocus);
102:
103: /* Set the text. */
104: setText(stringValue);
105: /* Tooltips used by the tree. */
106: setToolTipText(stringValue);
107:
108: DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
109: if (node == null)
110: return null;
111: if (amendMode == StaticDataEditor.AMEND_STATIC)
112: setStaticIcon(node);
113: else if (amendMode == StaticDataEditor.AMEND_DATABASE)
114: setDatabaseIcon(node);
115: else if (amendMode == StaticDataEditor.AMEND_SELECT)
116: setSelectIcon(node);
117:
118: Object userObject = ((DefaultMutableTreeNode) value)
119: .getUserObject();
120: if (hasFocus)
121: setForeground(Color.black);
122: setFont(defaultFont);
123:
124: /* Update the selected flag for the next paint. */
125: selected = isSelected;
126:
127: return this ;
128: }
129:
130: private void setStaticIcon(DefaultMutableTreeNode nod) {
131: if (editModel != null) {
132: String dataName = getTreePath(nod, false);
133:
134: if (dataName.compareTo("") == 0)
135: setIcon(datasetsIcon);
136: else {
137: XModel model = (XModel) editModel.get(dataName);
138: if (model.getTagName() == null)
139: setIcon(datasetsIcon);
140: else if (model.getTagName().compareTo("dataset") == 0)
141: setIcon(dataSetIcon);
142: else if (model.getTagName().compareTo("data") == 0)
143: setIcon(dataItemIcon);
144: else if (model.getTagName().compareTo("list") == 0)
145: setIcon(dataListIcon);
146: }
147: }
148: }
149:
150: private void setSelectIcon(DefaultMutableTreeNode nod) {
151: String dataName = getTreePath(nod, false);
152:
153: if (dataName.compareTo("") == 0)
154: setIcon(datasetsIcon);
155: else {
156: XModel model = (XModel) currentProject.getModel().get(
157: dataName);
158: if (model.getTagName() == null) {
159: if (model instanceof DatabaseTableModel)
160: setIcon(resultsetIcon);
161: else if (model.get() instanceof XServiceModelNode)
162: setIcon(serviceIcon);
163: else
164: setIcon(datasetsIcon);
165: } else if (model.getTagName().compareTo("dataset") == 0)
166: setIcon(dataSetIcon);
167: else if (model.getTagName().compareTo("data") == 0)
168: setIcon(dataItemIcon);
169: else if (model.getTagName().compareTo("list") == 0)
170: setIcon(dataListIcon);
171: else
172: System.out.println(model);
173: }
174: }
175:
176: public void setDatabaseIcon(DefaultMutableTreeNode node) {
177: if (node.isRoot())
178: setIcon(databaseIcon);
179: else {
180: Hashtable table = getModelManager().getDatabaseNodes();
181: XmlElement element = (XmlElement) table.get(node
182: .getLastLeaf().toString());
183: if (element.getName().compareTo("Connection") == 0)
184: setIcon(connectionIcon);
185: else if (element.getName().compareTo("ResultSet") == 0)
186: setIcon(resultsetIcon);
187: }
188: }
189:
190: private ModelManager getModelManager() {
191: // RouteManager routeMgr = ( RouteManager ) currentProject.getObject( ProjectRouteManager.ROUTE_MGR_LOOKUP );
192: // return routeMgr.getModelMgr();
193: return null;
194: }
195:
196: private String getTreePath(DefaultMutableTreeNode nod,
197: boolean startAtRoot) {
198: String modelName = null;
199: Object path[] = nod.getPath();
200:
201: modelName = "";
202: int start = startAtRoot ? 0 : 1;
203: for (int i = start; i < path.length; i++) {
204: String pathSection = path[i].toString();
205: if (pathSection.indexOf(" ~ ") > 0)
206: pathSection = pathSection.substring(0, pathSection
207: .indexOf(" ~ "));
208:
209: modelName += "/" + pathSection;
210: }
211:
212: if (modelName == null)
213: return "";
214: else if (modelName.trim().compareTo("") != 0)
215: return modelName.substring(1);
216: else
217: return "";
218: }
219:
220: /**
221: * paint is subclassed to draw the background correctly. JLabel
222: * currently does not allow backgrounds other than white, and it
223: * will also fill behind the icon. Something that isn't desirable.
224: */
225: /* public void paint( Graphics g )
226: {
227: Color bColor;
228: Icon currentI = getIcon();
229: if ( selected )
230: bColor = SelectedBackgroundColor;
231: else if ( getParent() != null ) {
232: // Pick background color up from parent (which will come from
233: // the JTree we're contained in).
234: bColor = getParent().getBackground();
235: }
236: else
237: bColor = getBackground();
238: g.setColor( bColor );
239: if ( currentI != null && getText() != null ) {
240: int offset = ( currentI.getIconWidth() + getIconTextGap() );
241: if ( getComponentOrientation().isLeftToRight() ) {
242: g.fillRect( offset, 0, getWidth() - 1 - offset,
243: getHeight() - 1 );
244: }
245: else {
246: g.fillRect( 0, 0, getWidth() - 1 - offset, getHeight() - 1 );
247: }
248: }
249: else
250: g.fillRect( 0, 0, getWidth() - 1, getHeight() - 1 );
251: super.paint( g );
252: }*/
253: }
|