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