/bundles/plugins-trunk/BufferList/bufferlist/BufferListPopup.java

# · Java · 293 lines · 211 code · 28 blank · 54 comment · 58 complexity · 48fe3d700033f18987be75320c4a9c0f MD5 · raw file

  1. /*{{{ header
  2. * BufferListPopup.java - provides popup actions for BufferList
  3. * Copyright (c) 2000-2002 Dirk Moebius
  4. *
  5. * :tabSize=4:indentSize=4:noTabs=false:maxLineLen=0:folding=explicit:collapseFolds=1:
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. * }}}
  21. */
  22. package bufferlist;
  23. // {{{ imports
  24. import java.awt.Toolkit;
  25. import java.awt.datatransfer.StringSelection;
  26. import java.awt.event.ActionEvent;
  27. import java.awt.event.ActionListener;
  28. import java.util.List;
  29. import javax.swing.JMenuItem;
  30. import javax.swing.JPopupMenu;
  31. import javax.swing.JTree;
  32. import javax.swing.tree.TreePath;
  33. import org.gjt.sp.jedit.Buffer;
  34. import org.gjt.sp.jedit.GUIUtilities;
  35. import org.gjt.sp.jedit.MiscUtilities;
  36. import org.gjt.sp.jedit.View;
  37. import org.gjt.sp.jedit.jEdit;
  38. import org.gjt.sp.jedit.browser.VFSBrowser;
  39. import org.gjt.sp.jedit.search.DirectoryListSet;
  40. import org.gjt.sp.jedit.search.SearchAndReplace;
  41. import org.gjt.sp.jedit.search.SearchDialog;
  42. // }}}
  43. /**
  44. * A popup menu for BufferList.
  45. *
  46. * @author Dirk Moebius
  47. */
  48. public class BufferListPopup extends JPopupMenu
  49. {
  50. private static final long serialVersionUID = 1L;
  51. // {{{ instance variables
  52. /**
  53. * Current view.
  54. */
  55. private View view;
  56. /**
  57. * Current buffer tree.
  58. */
  59. private JTree tree;
  60. /**
  61. * Selected paths in the tree.
  62. */
  63. private TreePath[] sel;
  64. /**
  65. * Selected directory. <code>null</code> if none or more than one
  66. * directory selected.
  67. */
  68. private String dir;
  69. // }}}
  70. // {{{ +BufferListPopup(View, JTree, TreePath[]) : <init>
  71. public BufferListPopup(View view, JTree tree, TreePath[] sel, List<MenuEntries> extensions)
  72. {
  73. this.view = view;
  74. this.tree = tree;
  75. this.sel = sel;
  76. // check whether user selected at most one directory
  77. if (sel != null)
  78. {
  79. String lastDir = null;
  80. for (int i = 0; i < sel.length; ++i)
  81. {
  82. BufferListTreeNode node = (BufferListTreeNode) sel[i].getLastPathComponent();
  83. Object obj = node.getUserObject();
  84. if (obj instanceof String)
  85. {
  86. dir = (String) obj;
  87. }
  88. else if (obj instanceof Buffer)
  89. {
  90. dir = MiscUtilities.getParentOfPath(((Buffer) obj).getPath());
  91. }
  92. if (lastDir == null)
  93. {
  94. lastDir = dir;
  95. }
  96. else
  97. {
  98. if (!lastDir.equals(dir))
  99. {
  100. dir = null; // more than one dir in selection
  101. break;
  102. }
  103. }
  104. }
  105. }
  106. Buffer viewBuffer = view.getBuffer();
  107. Buffer selectedBuffer = null;
  108. String title = jEdit.getProperty("bufferlist.popup.title.allFiles");
  109. if (sel != null)
  110. {
  111. title = jEdit.getProperty("bufferlist.popup.title.multipleSelection");
  112. if (sel.length == 1)
  113. {
  114. // one entry selected
  115. BufferListTreeNode node = (BufferListTreeNode) sel[0].getLastPathComponent();
  116. Object obj = node.getUserObject();
  117. if (obj instanceof Buffer)
  118. {
  119. selectedBuffer = (Buffer) obj;
  120. title = selectedBuffer.getName();
  121. }
  122. }
  123. }
  124. add(title).setEnabled(false);
  125. addSeparator();
  126. if (selectedBuffer != null && selectedBuffer != viewBuffer)
  127. {
  128. add(createMenuItem("goto"));
  129. }
  130. if (selectedBuffer != null)
  131. {
  132. add(createMenuItem("open-in-new-view"));
  133. }
  134. if (sel != null)
  135. {
  136. add(createMenuItem("close"));
  137. add(createMenuItem("save"));
  138. }
  139. if (selectedBuffer != null)
  140. {
  141. add(createMenuItem("save-as"));
  142. }
  143. if (sel != null)
  144. {
  145. add(createMenuItem("reload"));
  146. }
  147. addSeparator();
  148. add(createMenuItem("toggle-display-mode"));
  149. add(createMenuItem("expand-all"));
  150. add(createMenuItem("collapse-all"));
  151. if (dir != null)
  152. {
  153. addSeparator();
  154. add(createMenuItem("browse"));
  155. add(createMenuItem("search"));
  156. }
  157. if (sel != null)
  158. {
  159. addSeparator();
  160. add(createMenuItem("copy-paths"));
  161. }
  162. // process any menu extensions
  163. for (MenuEntries me : extensions)
  164. {
  165. addSeparator();
  166. // casting here is safe as the entries have already been checked
  167. // when they were
  168. // added to the List in the BufferListPlugin class.
  169. me.addEntries(this, view, tree, sel);
  170. }
  171. } // }}}
  172. // {{{ -createMenuItem(String) : JMenuItem
  173. private JMenuItem createMenuItem(String name)
  174. {
  175. String label = jEdit.getProperty("bufferlist.popup." + name + ".label");
  176. JMenuItem mi = new JMenuItem(label);
  177. mi.setActionCommand(name);
  178. mi.addActionListener(new ActionHandler());
  179. return mi;
  180. } // }}}
  181. // {{{ -class ActionHandler
  182. private class ActionHandler implements ActionListener
  183. {
  184. // {{{ +actionPerformed(ActionEvent) : void
  185. public void actionPerformed(ActionEvent evt)
  186. {
  187. String actionCommand = evt.getActionCommand();
  188. if (actionCommand.equals("expand-all"))
  189. {
  190. TreeTools.expandAll(tree);
  191. }
  192. else if (actionCommand.equals("collapse-all"))
  193. {
  194. TreeTools.collapseAll(tree);
  195. }
  196. else if (actionCommand.equals("toggle-display-mode"))
  197. {
  198. ((BufferList)view.getDockableWindowManager().getDockable("bufferlist")).toggleDisplayMode();
  199. }
  200. else if (actionCommand.equals("browse"))
  201. {
  202. GUIUtilities.showVFSFileDialog(view, dir, VFSBrowser.BROWSER, true);
  203. }
  204. else if (actionCommand.equals("search"))
  205. {
  206. SearchAndReplace.setSearchFileSet(new DirectoryListSet(dir, "*[^~#]", true));
  207. SearchDialog.showSearchDialog(view, "", SearchDialog.DIRECTORY);
  208. }
  209. else if (sel != null)
  210. {
  211. if (actionCommand.equals("copy-paths"))
  212. {
  213. StringBuilder pathStrings = new StringBuilder();
  214. for (TreePath path : sel)
  215. {
  216. pathStrings.append(((BufferListTreeNode)path.getLastPathComponent()).getUserPath());
  217. pathStrings.append('\n');
  218. }
  219. pathStrings.setLength(pathStrings.length() - 1);
  220. Toolkit.getDefaultToolkit().getSystemClipboard().setContents(
  221. new StringSelection(pathStrings.toString()), null);
  222. }
  223. else
  224. {
  225. for (TreePath path : sel)
  226. {
  227. BufferListTreeNode node = (BufferListTreeNode) path.getLastPathComponent();
  228. if (node.isBuffer())
  229. {
  230. Buffer buffer = node.getBuffer();
  231. if (actionCommand.equals("goto"))
  232. {
  233. view.setBuffer(buffer);
  234. }
  235. else if (actionCommand.equals("open-in-new-view"))
  236. {
  237. jEdit.newView(view, buffer);
  238. }
  239. else if (actionCommand.equals("close"))
  240. {
  241. jEdit.closeBuffer(view, buffer);
  242. }
  243. else if (actionCommand.equals("save"))
  244. {
  245. buffer.save(view, null);
  246. }
  247. else if (actionCommand.equals("save-as"))
  248. {
  249. buffer.saveAs(view, true);
  250. }
  251. else if (actionCommand.equals("reload"))
  252. {
  253. buffer.reload(view);
  254. }
  255. }
  256. }
  257. }
  258. }
  259. // help out the garbage collector
  260. view = null;
  261. tree = null;
  262. sel = null;
  263. } // }}}
  264. } // }}}
  265. }