/plugins/LaTeXTools/trunk/uk/co/antroy/latextools/macros/CompilationMacros.java

# · Java · 415 lines · 284 code · 102 blank · 29 comment · 34 complexity · 4597b20bbb58425b4429653aa96fa202 MD5 · raw file

  1. /*:folding=indent:
  2. * CompilationMacros.java - Macros to interact with the console.
  3. * Copyright (C) 2003 Anthony Roy
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. package uk.co.antroy.latextools.macros;
  20. import console.Console;
  21. import console.Shell;
  22. import gnu.regexp.RE;
  23. import gnu.regexp.REException;
  24. import java.awt.Component;
  25. import java.awt.Dimension;
  26. import java.awt.Frame;
  27. import java.awt.GridLayout;
  28. import java.awt.Point;
  29. import java.awt.event.ActionEvent;
  30. import java.awt.event.ActionListener;
  31. import java.awt.event.WindowEvent;
  32. import java.awt.event.WindowListener;
  33. import java.io.File;
  34. import java.io.FilenameFilter;
  35. import java.util.HashSet;
  36. import java.util.Iterator;
  37. import java.util.Set;
  38. import javax.swing.JButton;
  39. import javax.swing.JCheckBox;
  40. import javax.swing.JDialog;
  41. import javax.swing.JLabel;
  42. import javax.swing.JOptionPane;
  43. import javax.swing.JPanel;
  44. import org.gjt.sp.jedit.Buffer;
  45. import org.gjt.sp.jedit.Macros;
  46. import org.gjt.sp.jedit.View;
  47. import org.gjt.sp.jedit.gui.*;
  48. import org.gjt.sp.jedit.jEdit;
  49. import org.gjt.sp.util.*;
  50. public class CompilationMacros {
  51. //~ Methods ...............................................................
  52. public static void bibtex(View view, Buffer buffer) {
  53. historyCommand(view, buffer, false, "latex.bibtex", null, "");
  54. }
  55. public static void compile(View view, Buffer buffer, boolean prompt) {
  56. historyCommand(view, buffer, prompt, "latex.compile",
  57. "latextools.compile.history",
  58. "Enter Compilation Command");
  59. }
  60. public static void deleteWorkingFiles(View view, Buffer buffer) {
  61. String tex = ProjectMacros.getMainTeXPath(buffer);
  62. if (!(tex.substring(tex.length() - 3, tex.length()).equals("tex"))) {
  63. Macros.error(view, tex + " is not a TeX file.");
  64. return;
  65. }
  66. String[] extensions = {
  67. ".log", ".bak", ".aux", ".bbl", ".blg", ".toc", ".pdf", ".xyc",
  68. ".out", ".tex~", ".bak"
  69. };
  70. WorkingClassDialog dialog = new WorkingClassDialog(view, extensions);
  71. dialog.setVisible(true);
  72. Set toRemove = dialog.getToRemove();
  73. if (toRemove == null) {
  74. return;
  75. }
  76. File dir = (new File(tex)).getParentFile();
  77. File[] toDelete = dir.listFiles(new ExtensionFilter(toRemove));
  78. StringBuffer sb = new StringBuffer(
  79. "<html><h3>About to delete the following files:</h3>");
  80. for (int i = 0; i < toDelete.length; i++) {
  81. sb.append(toDelete[i]);
  82. sb.append("<br>");
  83. }
  84. sb.append("<br><b>Erase Now?");
  85. int del = Macros.confirm(view, sb.toString(),
  86. JOptionPane.YES_NO_OPTION);
  87. if (del == JOptionPane.YES_OPTION) {
  88. for (int i = 0; i < toDelete.length; i++) {
  89. toDelete[i].delete();
  90. }
  91. }
  92. }
  93. public static void viewOutput(View view, Buffer buffer, boolean prompt) {
  94. historyCommand(view, buffer, prompt, "latex.viewoutput",
  95. "latextools.viewoutput.history", "Enter Viewer Command");
  96. }
  97. private static Point getCenter(Component parent, Component dialog) {
  98. Dimension pd = parent.getSize();
  99. Dimension cd = dialog.getSize();
  100. Point pp = parent.getLocation();
  101. Point cp = new Point(pp);
  102. int x = (int)((pd.getWidth() - cd.getWidth()) / 2);
  103. int y = (int)((pd.getHeight() - cd.getHeight()) / 2);
  104. cp.translate(x, y);
  105. return cp;
  106. }
  107. private static void historyCommand(View view, Buffer buffer,
  108. boolean prompt, String commandProps,
  109. String historyProps, String dialogTitle) {
  110. String tex = ProjectMacros.getMainTeXPath(buffer);
  111. if (!(tex.substring(tex.length() - 3, tex.length()).equals("tex"))) {
  112. Macros.error(view, tex + " is not a TeX file.");
  113. return;
  114. }
  115. tex = tex.substring(0, tex.length() - 4);
  116. String command;
  117. String ext;
  118. if (prompt) {
  119. CommandHistoryDialog hd = new CommandHistoryDialog(view,
  120. historyProps,
  121. dialogTitle);
  122. hd.setVisible(true);
  123. command = hd.getCommand();
  124. ext = hd.getExtension();
  125. if (command == null) {
  126. Macros.message(view, "Aborting...");
  127. return;
  128. }
  129. } else {
  130. command = jEdit.getProperty(commandProps + ".command");
  131. ext = jEdit.getProperty(commandProps + ".ext");
  132. }
  133. jEdit.saveAllBuffers(view, false);
  134. String texRoot = new File(tex).getParent().toString();
  135. StringBuffer str = new StringBuffer(command);
  136. if (commandProps.equals("latex.compile") &&
  137. jEdit.getBooleanProperty("latex.compile.parse-errors")) {
  138. str.append(" ").append(jEdit.getProperty("latex.compile.c-errors"));
  139. }
  140. str.append(" '").append(tex).append(ext).append("'");
  141. command = str.toString();
  142. boolean detach = jEdit.getBooleanProperty(commandProps + ".detach");
  143. runCommand(view, texRoot, command, detach);
  144. }
  145. private static void runCommand(View view, String dir, String command, boolean detach) {
  146. DockableWindowManager manager = view.getDockableWindowManager();
  147. manager.showDockableWindow("console");
  148. Console console = (Console) manager.getDockable("console");
  149. Shell _shell = Shell.getShell("System");
  150. console.setShell(_shell);
  151. console.run(_shell, "%kill");
  152. console.run(_shell, "cd " + '"' + dir + '"');
  153. console.run(_shell, command);
  154. if (detach) {
  155. console.run(_shell, "%detach");
  156. }
  157. }
  158. //~ Inner classes .........................................................
  159. private static class CommandHistoryDialog
  160. extends JDialog
  161. implements ActionListener,
  162. WindowListener {
  163. //~ Instance/static variables .........................................
  164. private HistoryTextField htf;
  165. private HistoryTextField extHtf;
  166. private String command;
  167. private String extension;
  168. //~ Constructors ......................................................
  169. CommandHistoryDialog(Frame owner, String historyProps,
  170. String dialogTitle) {
  171. super(owner, dialogTitle, true);
  172. JPanel panel = new JPanel();
  173. htf = new HistoryTextField(historyProps, false, true);
  174. htf.setColumns(20);
  175. extHtf = new HistoryTextField(historyProps + ".ext", false, true);
  176. extHtf.setColumns(8);
  177. if (htf.getModel().getSize() > 0) {
  178. htf.setText(htf.getModel().getItem(0));
  179. }
  180. if (extHtf.getModel().getSize() > 0) {
  181. extHtf.setText(htf.getModel().getItem(0));
  182. }
  183. htf.addActionListener(this);
  184. panel.setLayout(new GridLayout(0, 1));
  185. panel.add((new JPanel()).add(htf));
  186. panel.add((new JPanel()).add(extHtf));
  187. JButton ok = new JButton("OK");
  188. JButton cancel = new JButton("Cancel");
  189. ok.addActionListener(this);
  190. cancel.addActionListener(this);
  191. JPanel buttonPanel = new JPanel();
  192. buttonPanel.add(ok);
  193. buttonPanel.add(cancel);
  194. panel.add(buttonPanel);
  195. setContentPane(panel);
  196. addWindowListener(this);
  197. pack();
  198. setLocation(getCenter(owner, this));
  199. }
  200. //~ Methods ...........................................................
  201. public String getCommand() {
  202. return command;
  203. }
  204. public String getExtension() {
  205. return extension;
  206. }
  207. public void actionPerformed(ActionEvent e) {
  208. if (e.getActionCommand() == "OK") {
  209. command = htf.getText();
  210. htf.addCurrentToHistory();
  211. extension = extHtf.getText();
  212. extHtf.addCurrentToHistory();
  213. setVisible(false);
  214. } else if (e.getActionCommand() == "Cancel") {
  215. command = null;
  216. extension = null;
  217. setVisible(false);
  218. }
  219. }
  220. public void windowActivated(WindowEvent e) {
  221. }
  222. public void windowClosed(WindowEvent e) {
  223. }
  224. public void windowClosing(WindowEvent e) {
  225. command = null;
  226. setVisible(false);
  227. }
  228. public void windowDeactivated(WindowEvent e) {
  229. }
  230. public void windowDeiconified(WindowEvent e) {
  231. }
  232. public void windowIconified(WindowEvent e) {
  233. }
  234. public void windowOpened(WindowEvent e) {
  235. }
  236. }
  237. private static class ExtensionFilter
  238. implements FilenameFilter {
  239. //~ Instance/static variables .........................................
  240. StringBuffer sb;
  241. RE regEx;
  242. //~ Constructors ......................................................
  243. ExtensionFilter(Set accept) {
  244. Iterator it = accept.iterator();
  245. sb = new StringBuffer("(\\w+?\\");
  246. sb.append(it.next()).append(")");
  247. for (; it.hasNext();) {
  248. sb.append("|(\\w+?\\");
  249. sb.append(it.next()).append(")");
  250. }
  251. sb.append("\\b");
  252. try {
  253. regEx = new RE(sb.toString());
  254. } catch (REException e) {
  255. e.printStackTrace();
  256. }
  257. }
  258. //~ Methods ...........................................................
  259. public boolean accept(File dir, String name) {
  260. return regEx.getMatch(name) != null;
  261. }
  262. }
  263. private static class WorkingClassDialog
  264. extends JDialog
  265. implements ActionListener {
  266. //~ Instance/static variables .........................................
  267. private String[] extensions;
  268. private Set toRemove;
  269. private JCheckBox[] boxes;
  270. //~ Constructors ......................................................
  271. WorkingClassDialog(Frame owner, String[] extensions) {
  272. super(owner, "Erase Working Files", true);
  273. this.extensions = extensions;
  274. toRemove = new HashSet();
  275. boxes = new JCheckBox[extensions.length];
  276. JPanel boxPanel = new JPanel(new GridLayout(0, 2));
  277. for (int i = 0; i < boxes.length; i++) {
  278. boxes[i] = new JCheckBox(extensions[i]);
  279. boxes[i].setSelected(true);
  280. boxPanel.add(boxes[i]);
  281. }
  282. JButton ok = new JButton("OK");
  283. JButton cancel = new JButton("Cancel");
  284. ok.addActionListener(this);
  285. cancel.addActionListener(this);
  286. if (boxes.length % 2 == 1) {
  287. boxPanel.add(new JLabel(""));
  288. }
  289. boxPanel.add(ok);
  290. boxPanel.add(cancel);
  291. setContentPane(boxPanel);
  292. pack();
  293. setLocation(getCenter(owner, this));
  294. }
  295. //~ Methods ...........................................................
  296. public Set getToRemove() {
  297. return toRemove;
  298. }
  299. public void actionPerformed(ActionEvent e) {
  300. if (e.getActionCommand() == "OK") {
  301. for (int i = 0; i < extensions.length; i++) {
  302. if (boxes[i].isSelected()) {
  303. toRemove.add(boxes[i].getText());
  304. }
  305. }
  306. setVisible(false);
  307. } else if (e.getActionCommand() == "Cancel") {
  308. toRemove = null;
  309. setVisible(false);
  310. }
  311. }
  312. }
  313. }