001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.view;
018    
019    import java.util.ArrayList;
020    import java.util.List;
021    
022    import org.apache.camel.model.AggregateDefinition;
023    import org.apache.camel.model.BeanDefinition;
024    import org.apache.camel.model.ChoiceDefinition;
025    import org.apache.camel.model.FilterDefinition;
026    import org.apache.camel.model.FromDefinition;
027    import org.apache.camel.model.OtherwiseDefinition;
028    import org.apache.camel.model.ProcessorDefinition;
029    import org.apache.camel.model.RecipientListDefinition;
030    import org.apache.camel.model.ResequenceDefinition;
031    import org.apache.camel.model.RoutingSlipDefinition;
032    import org.apache.camel.model.SplitDefinition;
033    import org.apache.camel.model.ToDefinition;
034    import org.apache.camel.model.WhenDefinition;
035    
036    import static org.apache.camel.util.ObjectHelper.isEmpty;
037    import static org.apache.camel.util.ObjectHelper.isNotEmpty;
038    
039    /**
040     * Represents a node in the EIP diagram tree
041     *
042     * @version $Revision: 883288 $
043     */
044    public class NodeData {
045        public String id;
046        public String image;
047        public String label;
048        public String shape;
049        public String edgeLabel;
050        public String tooltop;
051        public String nodeType;
052        public boolean nodeWritten;
053        public String url;
054        public List<ProcessorDefinition> outputs;
055        public String association = "property";
056    
057        @SuppressWarnings("unchecked")
058        public NodeData(String id, Object node, String imagePrefix) {
059            this.id = id;
060    
061            if (node instanceof ProcessorDefinition) {
062                ProcessorDefinition<?> processorType = (ProcessorDefinition<?>)node;
063                this.edgeLabel = processorType.getLabel();
064            }
065            if (node instanceof FromDefinition) {
066                FromDefinition fromType = (FromDefinition)node;
067                this.tooltop = fromType.getLabel();
068                this.label = removeQueryString(this.tooltop);
069                this.url = "http://camel.apache.org/message-endpoint.html";
070            } else if (node instanceof ToDefinition) {
071                ToDefinition toType = (ToDefinition)node;
072                this.tooltop = toType.getLabel();
073                this.label = removeQueryString(this.tooltop);
074                this.edgeLabel = "";
075                this.url = "http://camel.apache.org/message-endpoint.html";
076            } else if (node instanceof FilterDefinition) {
077                this.image = imagePrefix + "MessageFilterIcon.png";
078                this.label = "Filter";
079                this.nodeType = "Message Filter";
080            } else if (node instanceof WhenDefinition) {
081                this.image = imagePrefix + "MessageFilterIcon.png";
082                this.nodeType = "When Filter";
083                this.label = "When";
084                this.url = "http://camel.apache.org/content-based-router.html";
085            } else if (node instanceof OtherwiseDefinition) {
086                this.nodeType = "Otherwise";
087                this.edgeLabel = "";
088                this.url = "http://camel.apache.org/content-based-router.html";
089                this.tooltop = "Otherwise";
090            } else if (node instanceof ChoiceDefinition) {
091                this.image = imagePrefix + "ContentBasedRouterIcon.png";
092                this.nodeType = "Content Based Router";
093                this.label = "Choice";
094                this.edgeLabel = "";
095    
096                ChoiceDefinition choice = (ChoiceDefinition)node;
097                List<ProcessorDefinition> outputs = new ArrayList<ProcessorDefinition>(choice.getWhenClauses());
098                if (choice.getOtherwise() != null) {
099                    outputs.add(choice.getOtherwise());
100                }
101                this.outputs = outputs;
102            } else if (node instanceof RecipientListDefinition) {
103                this.image = imagePrefix + "RecipientListIcon.png";
104                this.nodeType = "Recipient List";
105            } else if (node instanceof RoutingSlipDefinition) {
106                this.image = imagePrefix + "RoutingTableIcon.png";
107                this.nodeType = "Routing Slip";
108                this.url = "http://camel.apache.org/routing-slip.html";
109                this.tooltop = ((RoutingSlipDefinition) node).getHeaderName();
110            } else if (node instanceof SplitDefinition) {
111                this.image = imagePrefix + "SplitterIcon.png";
112                this.nodeType = "Splitter";
113            } else if (node instanceof AggregateDefinition) {
114                this.image = imagePrefix + "AggregatorIcon.png";
115                this.nodeType = "Aggregator";
116            } else if (node instanceof ResequenceDefinition) {
117                this.image = imagePrefix + "ResequencerIcon.png";
118                this.nodeType = "Resequencer";
119            } else if (node instanceof BeanDefinition) {
120                BeanDefinition beanRef = (BeanDefinition) node;
121    
122                // TODO
123                //this.image = imagePrefix + "Bean.png";
124                this.nodeType = "Bean Ref";
125                this.label = beanRef.getLabel() + " Bean"; 
126                this.shape = "box";
127            }
128    
129            // lets auto-default as many values as we can
130            if (isEmpty(this.nodeType) && node != null) {
131                // TODO we could add this to the model?
132                String name = node.getClass().getName();
133                int idx = name.lastIndexOf('.');
134                if (idx > 0) {
135                    name = name.substring(idx + 1);
136                }
137                if (name.endsWith("Type")) {
138                    name = name.substring(0, name.length() - 4);
139                }
140                this.nodeType = insertSpacesBetweenCamelCase(name);
141            }
142            if (this.label == null) {
143                if (isEmpty(this.image)) {
144                    this.label = this.nodeType;
145                    this.shape = "box";
146                } else if (isNotEmpty(this.edgeLabel)) {
147                    this.label = "";
148                } else {
149                    this.label = node.toString();
150                }
151            }
152            if (isEmpty(this.tooltop)) {
153                if (isNotEmpty(this.nodeType)) {
154                    String description = isNotEmpty(this.edgeLabel) ? this.edgeLabel : this.label;
155                    this.tooltop = this.nodeType + ": " + description;
156                } else {
157                    this.tooltop = this.label;
158                }
159            }
160            if (isEmpty(this.url) && isNotEmpty(this.nodeType)) {
161                this.url = "http://camel.apache.org/" + this.nodeType.toLowerCase().replace(' ', '-') + ".html";
162            }
163            if (node instanceof ProcessorDefinition && this.outputs == null) {
164                ProcessorDefinition processorType = (ProcessorDefinition)node;
165                this.outputs = processorType.getOutputs();
166            }
167        }
168    
169        protected String removeQueryString(String text) {
170            int idx = text.indexOf('?');
171            if (idx <= 0) {
172                return text;
173            } else {
174                return text.substring(0, idx);
175            }
176        }
177    
178        /**
179         * Lets insert a space before each upper case letter after a lowercase
180         */
181        public static String insertSpacesBetweenCamelCase(String name) {
182            boolean lastCharacterLowerCase = false;
183            StringBuffer buffer = new StringBuffer();
184            int i = 0;
185            for (int size = name.length(); i < size; i++) {
186                char ch = name.charAt(i);
187                if (Character.isUpperCase(ch)) {
188                    if (lastCharacterLowerCase) {
189                        buffer.append(' ');
190                    }
191                    lastCharacterLowerCase = false;
192                } else {
193                    lastCharacterLowerCase = true;
194                }
195                buffer.append(ch);
196            }
197            return buffer.toString();
198        }
199    }