001    // This file is part of AceWiki.
002    // Copyright 2008-2012, AceWiki developers.
003    // 
004    // AceWiki is free software: you can redistribute it and/or modify it under the terms of the GNU
005    // Lesser General Public License as published by the Free Software Foundation, either version 3 of
006    // the License, or (at your option) any later version.
007    // 
008    // AceWiki is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009    // even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
010    // Lesser General Public License for more details.
011    // 
012    // You should have received a copy of the GNU Lesser General Public License along with AceWiki. If
013    // not, see http://www.gnu.org/licenses/.
014    
015    package ch.uzh.ifi.attempto.acewiki.gui;
016    
017    import java.io.IOException;
018    import java.io.OutputStream;
019    import java.util.List;
020    
021    import javax.swing.DefaultListSelectionModel;
022    
023    import nextapp.echo.app.Alignment;
024    import nextapp.echo.app.Column;
025    import nextapp.echo.app.Extent;
026    import nextapp.echo.app.Font;
027    import nextapp.echo.app.Grid;
028    import nextapp.echo.app.Insets;
029    import nextapp.echo.app.ListBox;
030    import nextapp.echo.app.Row;
031    import nextapp.echo.app.WindowPane;
032    import nextapp.echo.app.event.ActionEvent;
033    import nextapp.echo.app.event.ActionListener;
034    import nextapp.echo.app.event.WindowPaneEvent;
035    import nextapp.echo.app.event.WindowPaneListener;
036    import nextapp.echo.app.layout.GridLayoutData;
037    import nextapp.echo.filetransfer.app.AbstractDownloadProvider;
038    import nextapp.echo.filetransfer.app.DownloadCommand;
039    import ch.uzh.ifi.attempto.acewiki.Wiki;
040    import ch.uzh.ifi.attempto.acewiki.core.Ontology;
041    import ch.uzh.ifi.attempto.acewiki.core.OntologyExporter;
042    import ch.uzh.ifi.attempto.echocomp.GeneralButton;
043    import ch.uzh.ifi.attempto.echocomp.Label;
044    import ch.uzh.ifi.attempto.echocomp.Style;
045    import ch.uzh.ifi.attempto.echocomp.VSpace;
046    
047    /**
048     * This is a window that allows the user to choose from different kinds of file types for
049     * exporting the current knowledge base.
050     * 
051     * @author Tobias Kuhn
052     */
053    public class ExportWindow extends WindowPane implements ActionListener {
054    
055            private static final long serialVersionUID = -8594954833738936914L;
056            
057            private Wiki wiki;
058            
059            private List<OntologyExporter> exporters;
060            private ListBox listBox;
061    
062            /**
063             * Creates a new export window.
064             * 
065             * @param wiki The wiki instance.
066             */
067            public ExportWindow(Wiki wiki) {
068                    this.wiki = wiki;
069                    
070                    setTitle("Export");
071                    setTitleFont(new Font(Style.fontTypeface, Font.ITALIC, new Extent(13)));
072                    setModal(true);
073                    setWidth(new Extent(420));
074                    setHeight(new Extent(200));
075                    setResizable(false);
076                    setMovable(true);
077                    setTitleBackground(Style.windowTitleBackground);
078                    setStyleName("Default");
079    
080                    addWindowPaneListener(new WindowPaneListener() {
081    
082                            private static final long serialVersionUID = -3897741327122083261L;
083    
084                            public void windowPaneClosing(WindowPaneEvent e) {
085                                    actionPerformed(new ActionEvent(ExportWindow.this, "Close"));
086                            }
087    
088                    });
089    
090                    Grid grid = new Grid(1);
091                    grid.setInsets(new Insets(10, 10, 10, 0));
092                    grid.setColumnWidth(0, new Extent(400));
093                    grid.setRowHeight(0, new Extent(110));
094    
095                    Column messageColumn = new Column();
096                    GridLayoutData layout1 = new GridLayoutData();
097                    layout1.setAlignment(new Alignment(Alignment.LEFT, Alignment.TOP));
098                    messageColumn.setLayoutData(layout1);
099    
100                    Label label = new Label("Choose the type of export from the list:");
101                    label.setFont(new Font(Style.fontTypeface, Font.ITALIC, new Extent(13)));
102                    messageColumn.add(label);
103                    messageColumn.add(new VSpace());
104                    
105                    exporters = wiki.getOntologyExportManager().getExporters();
106                    String[] options = new String[exporters.size()];
107                    for (int i = 0 ; i < exporters.size() ; i++) {
108                            OntologyExporter e = exporters.get(i);
109                            options[i] = e.getName() + " (" + e.getFileSuffix() + ")";
110                    }
111                    
112                    listBox = new ListBox(options);
113                    listBox.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
114                    listBox.setFont(new Font(Style.fontTypeface, Font.ITALIC, new Extent(11)));
115                    listBox.setBackground(Style.lightBackground);
116                    listBox.setHeight(new Extent(70));
117                    listBox.setWidth(new Extent(375));
118                    listBox.setSelectedIndex(0);
119                    messageColumn.add(listBox);
120                    
121                    grid.add(messageColumn);
122    
123                    Row buttonBar = new Row();
124                    buttonBar.setCellSpacing(new Extent(10));
125                    buttonBar.setInsets(new Insets(0, 0, 0, 10));
126                    buttonBar.add(new GeneralButton("Export", 80, this));
127                    buttonBar.add(new GeneralButton("Cancel", 80, this));
128                    GridLayoutData layout2 = new GridLayoutData();
129                    layout2.setAlignment(new Alignment(Alignment.CENTER, Alignment.BOTTOM));
130                    buttonBar.setLayoutData(layout2);
131                    grid.add(buttonBar);
132    
133                    add(grid);
134            }
135    
136            public void actionPerformed(ActionEvent e) {
137                    setVisible(false);
138                    
139                    if (e.getActionCommand().equals("Export")) {
140                            
141                            final Ontology ontology = wiki.getOntology();
142                            final OntologyExporter exporter = exporters.get(listBox.getSelectedIndices()[0]);
143                            
144                            AbstractDownloadProvider provider = new AbstractDownloadProvider() {
145                                    
146                                    private static final long serialVersionUID = 3491081007747916029L;
147                                    
148                                    public String getContentType() {
149                                            return exporter.getContentType();
150                                    }
151    
152                                    public String getFileName() {
153                                            return ontology.getName() + exporter.getFileSuffix();
154                                    }
155    
156                                    public void writeFile(OutputStream out) throws IOException {
157                                            exporter.export(out);
158                                    }
159                                    
160                            };
161                            
162                            wiki.getApplication().enqueueCommand(new DownloadCommand(provider));
163                    }
164            }
165    
166    }