PageRenderTime 54ms CodeModel.GetById 20ms app.highlight 18ms RepoModel.GetById 13ms app.codeStats 0ms

/bundles/plugins-trunk/RubyPlugin/src/org/jedit/ruby/ri/RDocSelectionPane.java

#
Java | 178 lines | 134 code | 22 blank | 22 comment | 15 complexity | b4ae9f302672a380b277e5300cdde0d7 MD5 | raw file
  1/*
  2* RDocOptionsPane.java -
  3*
  4* Copyright 2005 Robert McKinnon
  5*
  6* This program is free software; you can redistribute it and/or
  7* modify it under the terms of the GNU General Public License
  8* as published by the Free Software Foundation; either version 2
  9* of the License, or any later version.
 10*
 11* This program is distributed in the hope that it will be useful,
 12* but WITHOUT ANY WARRANTY; without even the implied warranty of
 13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14* GNU General Public License for more details.
 15*
 16* You should have received a copy of the GNU General Public License
 17* along with this program; if not, write to the Free Software
 18* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 19*/
 20package org.jedit.ruby.ri;
 21
 22import org.gjt.sp.jedit.AbstractOptionPane;
 23import org.gjt.sp.jedit.jEdit;
 24import org.gjt.sp.jedit.GUIUtilities;
 25import org.gjt.sp.jedit.gui.RolloverButton;
 26import org.gjt.sp.jedit.browser.VFSBrowser;
 27import org.jedit.ruby.utils.CommandUtils;
 28import org.jedit.ruby.RubyPlugin;
 29
 30import javax.swing.*;
 31import javax.swing.event.ListSelectionListener;
 32import javax.swing.event.ListSelectionEvent;
 33import java.awt.BorderLayout;
 34import java.awt.event.ActionListener;
 35import java.awt.event.ActionEvent;
 36import java.io.File;
 37import java.util.List;
 38import java.util.ArrayList;
 39
 40/**
 41 * @author robmckinnon at users.sourceforge.net
 42 */
 43public final class RDocSelectionPane extends AbstractOptionPane {
 44
 45    private static final String RDOC_PATH_PREFIX = "rubyplugin.rdoc-path.";
 46    private JList pathList;
 47    private DefaultListModel pathListModel;
 48    private final List<String> deletedPaths;
 49    private JButton add;
 50    private JButton remove;
 51
 52    public RDocSelectionPane() {
 53        super("rdoc");
 54        deletedPaths = new ArrayList<String>();
 55    }
 56
 57    protected final void _init() {
 58        setLayout(new BorderLayout());
 59
 60        pathListModel = new DefaultListModel();
 61        int i = 0;
 62        String element;
 63        while ((element = jEdit.getProperty(RDOC_PATH_PREFIX + i)) != null) {
 64            pathListModel.addElement(element);
 65            i++;
 66        }
 67        pathList = new JList(pathListModel);
 68        pathList.addListSelectionListener(new ListHandler());
 69
 70        ActionHandler handler = new ActionHandler();
 71        add = initButton("Plus.png", "options.rdoc.add", handler);
 72        remove = initButton("Minus.png", "options.rdoc.remove", handler);
 73
 74        add(BorderLayout.CENTER, new JScrollPane(pathList));
 75        add(BorderLayout.NORTH, initButtonPanel(add, remove));
 76        updateEnabled();
 77    }
 78
 79    private static JPanel initButtonPanel(JButton add, JButton remove) {
 80        JLabel label = new JLabel(jEdit.getProperty("options.rdoc.caption"));
 81        label.setBorder(BorderFactory.createEmptyBorder(0, 0, 6, 0));
 82
 83        JPanel panel = new JPanel();
 84        panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
 85        panel.setBorder(BorderFactory.createEmptyBorder(6, 0, 0, 0));
 86        panel.add(label);
 87        panel.add(Box.createHorizontalStrut(5));
 88        panel.add(add);
 89        panel.add(remove);
 90        panel.add(Box.createGlue());
 91        return panel;
 92    }
 93
 94    private static JButton initButton(String iconName, String tooltipProperty, ActionHandler handler) {
 95        JButton button = new RolloverButton(GUIUtilities.loadIcon(iconName));
 96        button.setToolTipText(jEdit.getProperty(tooltipProperty));
 97        button.addActionListener(handler);
 98        return button;
 99    }
100
101    protected final void _save() {
102        int i;
103        StringBuffer buffer = new StringBuffer("require '" + CommandUtils.getStoragePath("rdoc_to_java.rb") + "'\n");
104        File resultPath = CommandUtils.getStoragePath("java-xml");
105        buffer.append("result_dir = '").append(resultPath).append("'\n");
106        buffer.append("template_dir = '").append(resultPath.getParent()).append("'\n");
107        buffer.append("serializer = JavaXmlSerializer.new(template_dir, result_dir)\n");
108
109        for (i = 0; i < pathListModel.getSize(); i++) {
110            String path = (String)pathListModel.getElementAt(i);
111            jEdit.setProperty(RDOC_PATH_PREFIX + i, path);
112            File directory = new File(path);
113            String baseDirectory = directory.getParent() + File.separatorChar;
114            buffer.append("serializer.convert_dir('").append(baseDirectory).append("', '").append(directory.getName()).append("')\n");
115        }
116
117        try {
118            File convertScript = CommandUtils.getCommandFile("convert_rdoc.rb", true, buffer.toString());
119            String log;
120            if(CommandUtils.isWindows()) {
121                String text = "ruby.bat \"" + convertScript.getPath() + "\"";
122                File commandFile = CommandUtils.getCommandFile("convert_rdoc.bat", false, text);
123                log = CommandUtils.getOutput(commandFile.getPath(), false);
124            } else {
125                String command = "ruby " + convertScript.getPath();
126                RubyPlugin.log("Running: " + command, getClass());
127                log = CommandUtils.getOutput(command, false, -1);
128            }
129
130            RubyPlugin.log(log, getClass());
131        } catch (Exception e) {
132            e.printStackTrace();
133            RubyPlugin.error(e, getClass());
134        }
135        jEdit.unsetProperty(RDOC_PATH_PREFIX + i);
136    }
137
138    private void updateEnabled() {
139        boolean selected = (pathList.getSelectedValue() != null);
140        remove.setEnabled(selected);
141    }
142
143    private final class ActionHandler implements ActionListener {
144        public final void actionPerformed(ActionEvent event) {
145            Object source = event.getSource();
146            if (source == add) {
147                handleAdd();
148            } else if (source == remove) {
149                String deleted = (String)pathListModel.remove(pathList.getSelectedIndex());
150                deletedPaths.add(deleted);
151                updateEnabled();
152            }
153        }
154
155        private void handleAdd() {
156            String userHome = System.getProperty("user.home");
157            File rdocPath = new File(userHome, ".rdoc");
158
159            if(!rdocPath.isDirectory()) {
160                rdocPath = rdocPath.getParentFile();
161            }
162
163            String path = rdocPath.getPath() + File.separatorChar;
164            String[] files = GUIUtilities.showVFSFileDialog(null, path, VFSBrowser.CHOOSE_DIRECTORY_DIALOG, true);
165            if (files != null) {
166                for (String file : files) {
167                    pathListModel.addElement(file);
168                }
169            }
170        }
171    }
172
173    private final class ListHandler implements ListSelectionListener {
174        public final void valueChanged(ListSelectionEvent evt) {
175            updateEnabled();
176        }
177    }
178}