source: trunk/autoquest-ui-core/src/main/java/de/ugoe/cs/autoquest/commands/usage/CMDshowTrie.java @ 927

Last change on this file since 927 was 927, checked in by sherbold, 12 years ago
  • added copyright under the Apache License, Version 2.0
  • Property svn:mime-type set to text/plain
File size: 3.6 KB
Line 
1//   Copyright 2012 Georg-August-Universität Göttingen, Germany
2//
3//   Licensed under the Apache License, Version 2.0 (the "License");
4//   you may not use this file except in compliance with the License.
5//   You may obtain a copy of the License at
6//
7//       http://www.apache.org/licenses/LICENSE-2.0
8//
9//   Unless required by applicable law or agreed to in writing, software
10//   distributed under the License is distributed on an "AS IS" BASIS,
11//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//   See the License for the specific language governing permissions and
13//   limitations under the License.
14
15package de.ugoe.cs.autoquest.commands.usage;
16
17import java.awt.Dimension;
18import java.awt.Rectangle;
19import java.awt.Shape;
20import java.util.List;
21
22import javax.swing.JFrame;
23
24import org.apache.commons.collections15.Transformer;
25
26import de.ugoe.cs.autoquest.CommandHelpers;
27import de.ugoe.cs.autoquest.usageprofiles.Trie;
28import de.ugoe.cs.autoquest.usageprofiles.TrieBasedModel;
29import de.ugoe.cs.autoquest.usageprofiles.Trie.Edge;
30import de.ugoe.cs.autoquest.usageprofiles.Trie.TrieVertex;
31import de.ugoe.cs.util.console.Command;
32import de.ugoe.cs.util.console.GlobalDataContainer;
33import edu.uci.ics.jung.algorithms.layout.Layout;
34import edu.uci.ics.jung.algorithms.layout.TreeLayout;
35import edu.uci.ics.jung.graph.Tree;
36import edu.uci.ics.jung.visualization.BasicVisualizationServer;
37import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
38import edu.uci.ics.jung.visualization.renderers.Renderer.VertexLabel.Position;
39
40/**
41 * <p>
42 * Command that visualizes the {@link Trie} of a {@link TrieBasedModel}.
43 * </p>
44 *
45 * @author Steffen Herbold
46 * @version 1.0
47 */
48public class CMDshowTrie implements Command {
49
50        /*
51         * (non-Javadoc)
52         *
53         * @see de.ugoe.cs.util.console.Command#help()
54         */
55        @Override
56        public String help() {
57                return "showTrie <modelname>";
58        }
59
60        /*
61         * (non-Javadoc)
62         *
63         * @see de.ugoe.cs.util.console.Command#run(java.util.List)
64         */
65        @Override
66        public void run(List<Object> parameters) {
67                String modelname;
68                try {
69                        modelname = (String) parameters.get(0);
70                } catch (Exception e) {
71                        throw new IllegalArgumentException();
72                }
73
74                Object dataObject = GlobalDataContainer.getInstance()
75                                .getData(modelname);
76                if (dataObject == null) {
77                        CommandHelpers.objectNotFoundMessage(modelname);
78                        return;
79                }
80                if (!(dataObject instanceof TrieBasedModel)) {
81                        CommandHelpers.objectNotType(modelname, "TrieBasedModel");
82                }
83                TrieBasedModel model = (TrieBasedModel) dataObject;
84                Tree<TrieVertex, Edge> graph = model.getTrieGraph();
85                Layout<TrieVertex, Edge> layout = new TreeLayout<TrieVertex, Edge>(
86                                graph, 60);
87                // The BasicVisualizationServer<V,E> is parameterized by the edge
88                // types
89                BasicVisualizationServer<TrieVertex, Edge> vv = new BasicVisualizationServer<TrieVertex, Edge>(
90                                layout);
91                vv.setPreferredSize(new Dimension(1100, 850)); // Sets the viewing
92                                                                                                                // area size
93
94                final Rectangle rect = new Rectangle(40, 20);
95
96                Transformer<TrieVertex, Shape> vertexShapeTransformer = new Transformer<TrieVertex, Shape>() {
97                        public Shape transform(TrieVertex s) {
98                                return rect;
99                        }
100                };
101                vv.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);
102                vv.getRenderContext().setVertexShapeTransformer(vertexShapeTransformer);
103                vv.getRenderContext().setVertexLabelTransformer(
104                                new ToStringLabeller<TrieVertex>());
105
106                JFrame frame = new JFrame("Trie");
107                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
108                frame.getContentPane().add(vv);
109                frame.pack();
110                frame.setVisible(true);
111        }
112
113}
Note: See TracBrowser for help on using the repository browser.