PageRenderTime 36ms CodeModel.GetById 12ms RepoModel.GetById 0ms 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
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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. */
  20. package org.jedit.ruby.ri;
  21. import org.gjt.sp.jedit.AbstractOptionPane;
  22. import org.gjt.sp.jedit.jEdit;
  23. import org.gjt.sp.jedit.GUIUtilities;
  24. import org.gjt.sp.jedit.gui.RolloverButton;
  25. import org.gjt.sp.jedit.browser.VFSBrowser;
  26. import org.jedit.ruby.utils.CommandUtils;
  27. import org.jedit.ruby.RubyPlugin;
  28. import javax.swing.*;
  29. import javax.swing.event.ListSelectionListener;
  30. import javax.swing.event.ListSelectionEvent;
  31. import java.awt.BorderLayout;
  32. import java.awt.event.ActionListener;
  33. import java.awt.event.ActionEvent;
  34. import java.io.File;
  35. import java.util.List;
  36. import java.util.ArrayList;
  37. /**
  38. * @author robmckinnon at users.sourceforge.net
  39. */
  40. public final class RDocSelectionPane extends AbstractOptionPane {
  41. private static final String RDOC_PATH_PREFIX = "rubyplugin.rdoc-path.";
  42. private JList pathList;
  43. private DefaultListModel pathListModel;
  44. private final List<String> deletedPaths;
  45. private JButton add;
  46. private JButton remove;
  47. public RDocSelectionPane() {
  48. super("rdoc");
  49. deletedPaths = new ArrayList<String>();
  50. }
  51. protected final void _init() {
  52. setLayout(new BorderLayout());
  53. pathListModel = new DefaultListModel();
  54. int i = 0;
  55. String element;
  56. while ((element = jEdit.getProperty(RDOC_PATH_PREFIX + i)) != null) {
  57. pathListModel.addElement(element);
  58. i++;
  59. }
  60. pathList = new JList(pathListModel);
  61. pathList.addListSelectionListener(new ListHandler());
  62. ActionHandler handler = new ActionHandler();
  63. add = initButton("Plus.png", "options.rdoc.add", handler);
  64. remove = initButton("Minus.png", "options.rdoc.remove", handler);
  65. add(BorderLayout.CENTER, new JScrollPane(pathList));
  66. add(BorderLayout.NORTH, initButtonPanel(add, remove));
  67. updateEnabled();
  68. }
  69. private static JPanel initButtonPanel(JButton add, JButton remove) {
  70. JLabel label = new JLabel(jEdit.getProperty("options.rdoc.caption"));
  71. label.setBorder(BorderFactory.createEmptyBorder(0, 0, 6, 0));
  72. JPanel panel = new JPanel();
  73. panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
  74. panel.setBorder(BorderFactory.createEmptyBorder(6, 0, 0, 0));
  75. panel.add(label);
  76. panel.add(Box.createHorizontalStrut(5));
  77. panel.add(add);
  78. panel.add(remove);
  79. panel.add(Box.createGlue());
  80. return panel;
  81. }
  82. private static JButton initButton(String iconName, String tooltipProperty, ActionHandler handler) {
  83. JButton button = new RolloverButton(GUIUtilities.loadIcon(iconName));
  84. button.setToolTipText(jEdit.getProperty(tooltipProperty));
  85. button.addActionListener(handler);
  86. return button;
  87. }
  88. protected final void _save() {
  89. int i;
  90. StringBuffer buffer = new StringBuffer("require '" + CommandUtils.getStoragePath("rdoc_to_java.rb") + "'\n");
  91. File resultPath = CommandUtils.getStoragePath("java-xml");
  92. buffer.append("result_dir = '").append(resultPath).append("'\n");
  93. buffer.append("template_dir = '").append(resultPath.getParent()).append("'\n");
  94. buffer.append("serializer = JavaXmlSerializer.new(template_dir, result_dir)\n");
  95. for (i = 0; i < pathListModel.getSize(); i++) {
  96. String path = (String)pathListModel.getElementAt(i);
  97. jEdit.setProperty(RDOC_PATH_PREFIX + i, path);
  98. File directory = new File(path);
  99. String baseDirectory = directory.getParent() + File.separatorChar;
  100. buffer.append("serializer.convert_dir('").append(baseDirectory).append("', '").append(directory.getName()).append("')\n");
  101. }
  102. try {
  103. File convertScript = CommandUtils.getCommandFile("convert_rdoc.rb", true, buffer.toString());
  104. String log;
  105. if(CommandUtils.isWindows()) {
  106. String text = "ruby.bat \"" + convertScript.getPath() + "\"";
  107. File commandFile = CommandUtils.getCommandFile("convert_rdoc.bat", false, text);
  108. log = CommandUtils.getOutput(commandFile.getPath(), false);
  109. } else {
  110. String command = "ruby " + convertScript.getPath();
  111. RubyPlugin.log("Running: " + command, getClass());
  112. log = CommandUtils.getOutput(command, false, -1);
  113. }
  114. RubyPlugin.log(log, getClass());
  115. } catch (Exception e) {
  116. e.printStackTrace();
  117. RubyPlugin.error(e, getClass());
  118. }
  119. jEdit.unsetProperty(RDOC_PATH_PREFIX + i);
  120. }
  121. private void updateEnabled() {
  122. boolean selected = (pathList.getSelectedValue() != null);
  123. remove.setEnabled(selected);
  124. }
  125. private final class ActionHandler implements ActionListener {
  126. public final void actionPerformed(ActionEvent event) {
  127. Object source = event.getSource();
  128. if (source == add) {
  129. handleAdd();
  130. } else if (source == remove) {
  131. String deleted = (String)pathListModel.remove(pathList.getSelectedIndex());
  132. deletedPaths.add(deleted);
  133. updateEnabled();
  134. }
  135. }
  136. private void handleAdd() {
  137. String userHome = System.getProperty("user.home");
  138. File rdocPath = new File(userHome, ".rdoc");
  139. if(!rdocPath.isDirectory()) {
  140. rdocPath = rdocPath.getParentFile();
  141. }
  142. String path = rdocPath.getPath() + File.separatorChar;
  143. String[] files = GUIUtilities.showVFSFileDialog(null, path, VFSBrowser.CHOOSE_DIRECTORY_DIALOG, true);
  144. if (files != null) {
  145. for (String file : files) {
  146. pathListModel.addElement(file);
  147. }
  148. }
  149. }
  150. }
  151. private final class ListHandler implements ListSelectionListener {
  152. public final void valueChanged(ListSelectionEvent evt) {
  153. updateEnabled();
  154. }
  155. }
  156. }