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: 795369 $
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        private final String imagePrefix;
057    
058        @SuppressWarnings("unchecked")
059        public NodeData(String id, Object node, String imagePrefix) {
060            this.id = id;
061            this.imagePrefix = imagePrefix;
062    
063            if (node instanceof ProcessorDefinition) {
064                ProcessorDefinition processorType = (ProcessorDefinition)node;
065                this.edgeLabel = processorType.getLabel();
066            }
067            if (node instanceof FromDefinition) {
068                FromDefinition fromType = (FromDefinition)node;
069                this.tooltop = fromType.getLabel();
070                this.label = removeQueryString(this.tooltop);
071                this.url = "http://camel.apache.org/message-endpoint.html";
072            } else if (node instanceof ToDefinition) {
073                ToDefinition toType = (ToDefinition)node;
074                this.tooltop = toType.getLabel();
075                this.label = removeQueryString(this.tooltop);
076                this.edgeLabel = "";
077                this.url = "http://camel.apache.org/message-endpoint.html";
078            } else if (node instanceof FilterDefinition) {
079                this.image = imagePrefix + "MessageFilterIcon.png";
080                this.label = "Filter";
081                this.nodeType = "Message Filter";
082            } else if (node instanceof WhenDefinition) {
083                this.image = imagePrefix + "MessageFilterIcon.png";
084                this.nodeType = "When Filter";
085                this.label = "When";
086                this.url = "http://camel.apache.org/content-based-router.html";
087            } else if (node instanceof OtherwiseDefinition) {
088                this.nodeType = "Otherwise";
089                this.edgeLabel = "";
090                this.url = "http://camel.apache.org/content-based-router.html";
091                this.tooltop = "Otherwise";
092            } else if (node instanceof ChoiceDefinition) {
093                this.image = imagePrefix + "ContentBasedRouterIcon.png";
094                this.nodeType = "Content Based Router";
095                this.label = "Choice";
096                this.edgeLabel = "";
097    
098                ChoiceDefinition choice = (ChoiceDefinition)node;
099                List<ProcessorDefinition> outputs = new ArrayList<ProcessorDefinition>(choice.getWhenClauses());
100                if (choice.getOtherwise() != null) {
101                    outputs.add(choice.getOtherwise());
102                }
103                this.outputs = outputs;
104            } else if (node instanceof RecipientListDefinition) {
105                this.image = imagePrefix + "RecipientListIcon.png";
106                this.nodeType = "Recipient List";
107            } else if (node instanceof RoutingSlipDefinition) {
108                this.image = imagePrefix + "RoutingTableIcon.png";
109                this.nodeType = "Routing Slip";
110                this.url = "http://camel.apache.org/routing-slip.html";
111                this.tooltop = ((RoutingSlipDefinition) node).getHeaderName();
112            } else if (node instanceof SplitDefinition) {
113                this.image = imagePrefix + "SplitterIcon.png";
114                this.nodeType = "Splitter";
115            } else if (node instanceof AggregateDefinition) {
116                this.image = imagePrefix + "AggregatorIcon.png";
117                this.nodeType = "Aggregator";
118            } else if (node instanceof ResequenceDefinition) {
119                this.image = imagePrefix + "ResequencerIcon.png";
120                this.nodeType = "Resequencer";
121            } else if (node instanceof BeanDefinition) {
122                BeanDefinition beanRef = (BeanDefinition) node;
123    
124                // TODO
125                //this.image = imagePrefix + "Bean.png";
126                this.nodeType = "Bean Ref";
127                this.label = beanRef.getLabel() + " Bean"; 
128                this.shape = "box";
129            }
130    
131            // lets auto-default as many values as we can
132            if (isEmpty(this.nodeType) && node != null) {
133                // TODO we could add this to the model?
134                String name = node.getClass().getName();
135                int idx = name.lastIndexOf('.');
136                if (idx > 0) {
137                    name = name.substring(idx + 1);
138                }
139                if (name.endsWith("Type")) {
140                    name = name.substring(0, name.length() - 4);
141                }
142                this.nodeType = insertSpacesBetweenCamelCase(name);
143            }
144            if (this.label == null) {
145                if (isEmpty(this.image)) {
146                    this.label = this.nodeType;
147                    this.shape = "box";
148                } else if (isNotEmpty(this.edgeLabel)) {
149                    this.label = "";
150                } else {
151                    this.label = node.toString();
152                }
153            }
154            if (isEmpty(this.tooltop)) {
155                if (isNotEmpty(this.nodeType)) {
156                    String description = isNotEmpty(this.edgeLabel) ? this.edgeLabel : this.label;
157                    this.tooltop = this.nodeType + ": " + description;
158                } else {
159                    this.tooltop = this.label;
160                }
161            }
162            if (isEmpty(this.url) && isNotEmpty(this.nodeType)) {
163                this.url = "http://camel.apache.org/" + this.nodeType.toLowerCase().replace(' ', '-') + ".html";
164            }
165            if (node instanceof ProcessorDefinition && this.outputs == null) {
166                ProcessorDefinition processorType = (ProcessorDefinition)node;
167                this.outputs = processorType.getOutputs();
168            }
169        }
170    
171        protected String removeQueryString(String text) {
172            int idx = text.indexOf('?');
173            if (idx <= 0) {
174                return text;
175            } else {
176                return text.substring(0, idx);
177            }
178        }
179    
180        /**
181         * Lets insert a space before each upper case letter after a lowercase
182         */
183        public static String insertSpacesBetweenCamelCase(String name) {
184            boolean lastCharacterLowerCase = false;
185            StringBuffer buffer = new StringBuffer();
186            int i = 0;
187            for (int size = name.length(); i < size; i++) {
188                char ch = name.charAt(i);
189                if (Character.isUpperCase(ch)) {
190                    if (lastCharacterLowerCase) {
191                        buffer.append(' ');
192                    }
193                    lastCharacterLowerCase = false;
194                } else {
195                    lastCharacterLowerCase = true;
196                }
197                buffer.append(ch);
198            }
199            return buffer.toString();
200        }
201    }