root/trunk/plugins/examples/org/pathvisio/example/ExImporter.java

Revision 2979, 3.2 KB (checked in by martijn, 8 months ago)

For the sake of uniformity, removed all trailing whitespace in Java code

Line 
1// PathVisio,
2// a tool for data visualization and analysis using Biological Pathways
3// Copyright 2006-2009 BiGCaT Bioinformatics
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16//
17package org.pathvisio.example;
18
19import java.io.BufferedReader;
20import java.io.File;
21import java.io.FileReader;
22import java.io.IOException;
23
24import org.pathvisio.gui.swing.PvDesktop;
25import org.pathvisio.model.ConverterException;
26import org.pathvisio.model.ObjectType;
27import org.pathvisio.model.Pathway;
28import org.pathvisio.model.PathwayElement;
29import org.pathvisio.model.PathwayImporter;
30import org.pathvisio.plugin.Plugin;
31
32/**
33 * Example of how to create and register a Pathway importer.
34 * <p>
35 * Here we convert a text file to a pathway, by making a separate
36 * label out of each line.
37 */
38public class ExImporter implements Plugin
39{
40        private PvDesktop desktop;
41
42        public void init(PvDesktop desktop)
43        {
44                this.desktop = desktop;
45
46                // instantiate TextLinesImporter, our own importer, and register it
47                desktop.getSwingEngine().getEngine().addPathwayImporter(new TextLinesImporter());
48        }
49
50        public void done() {}
51
52        private static class TextLinesImporter implements PathwayImporter
53        {
54                /**
55                 * Called after the user selected a file and picked this importer from the
56                 * File types dropdown.
57                 * <p>
58                 * Our job is to create a new Pathway object, fill its contents
59                 * and return it.
60                 */
61                public Pathway doImport(File file)
62                                throws ConverterException
63                {
64                        Pathway pathway = new Pathway();
65                        try
66                        {
67                                // some constants: this is the size of each label
68                                double xpos = 300;
69                                double ypos = 300;
70                                double height = 300;
71                                double width = 1500;
72
73                                // open file for reading
74                                BufferedReader reader = null;
75                                reader = new BufferedReader(new FileReader (file));
76                                String line;
77                                // go over each line
78                                while ((line = reader.readLine()) != null)
79                                {
80                                        line = line.trim();
81                                        // skip empty lines
82                                        if (line.equals ("")) { continue; }
83
84                                        // construct a new label object, set the properties
85                                        PathwayElement elt = PathwayElement.createPathwayElement(ObjectType.LABEL);
86                                        elt.setMWidth(width);
87                                        elt.setMHeight(height);
88                                        elt.setMTop(ypos);
89                                        elt.setMLeft(xpos);
90                                        elt.setTextLabel(line);
91                                        // add the object to the pathway
92                                        pathway.add(elt);
93                                        ypos += height; // move to next line
94                                }
95                                // set the pathway title
96                                pathway.getMappInfo().setMapInfoName(file.getName());
97                                reader.close();
98                        }
99                        catch (IOException ex)
100                        {
101                                throw new ConverterException (ex);
102                        }
103                        return pathway;
104                }
105
106                private static final String[] EXTENSIONS = new String[] { "txt" };
107
108                public String[] getExtensions()
109                {
110                        return EXTENSIONS;
111                }
112
113                public String getName()
114                {
115                        return "Lines from text file";
116                }
117        }
118}
Note: See TracBrowser for help on using the browser.