/* * Switch_Project.bsh - a BeanShell macro script to switch Projects * (this macro requires the ProjectViewer plugin to work!) * * Copyright (C) 2005 Jeroen Budts, http://dev.budts.be * This script contains code from Buffer_Switcher.bsh which is * Copyright (C) 2001-2004 Ollie Rutherfurd, oliver@rutherfurd.net * * :mode=beanshell:tabSize=4:indentSize=4:maxLineLen=0:noTabs=false: * :indentOnTab=true:indentOnEnter=true:folding=explicit:collapseFolds=1: * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with the jEdit program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * You can download the most recent version of this macro (and probably others) * from my DARCS repository: * $ darcs get http://dev.budts.be/darcs/jedit-macros */ import javax.swing.border.EmptyBorder; EditPlugin getProjectViewerPlugin() { jEdit.getPlugin("projectviewer.ProjectPlugin",true); } boolean foundProjectViewerPlugin() { return getProjectViewerPlugin() != null; } void warnProjectViewerPluginNotFound() { Macros.error(view, "ProjectViewer was not found!"); } Object[] getProjects() { import projectviewer.ProjectManager; import projectviewer.vpt.VPTProject; // Why does getProjects return an iterator and not some kind of collection? List l = new ArrayList(); pm = ProjectManager.getInstance(); Iterator it = pm.getProjects(); while (it.hasNext()) { l.add((VPTProject) it.next()); } return l.toArray(); } showSwitchDialog(Object[] projects) { dialog = new JDialog(view, "Switch project", true); content = new JPanel(new BorderLayout()); content.setBorder(new EmptyBorder(12,12,12,12)); dialog.setContentPane(content); projectList = new JList(projects); projectList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); content.add(new JScrollPane(projectList), BorderLayout.CENTER); buttonPanel = new JPanel(); buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS)); buttonPanel.setBorder(new EmptyBorder(12,50,0,50)); buttonPanel.add(Box.createGlue()); ok = new JButton("OK"); close = new JButton("Close"); ok.setPreferredSize(close.getPreferredSize()); dialog.getRootPane().setDefaultButton(ok); buttonPanel.add(ok); buttonPanel.add(Box.createHorizontalStrut(6)); buttonPanel.add(close); buttonPanel.add(Box.createGlue()); content.add(buttonPanel, BorderLayout.SOUTH); void switchBuffer() { _project = projectList.getSelectedValue(); if(_project == projectList) { view.getToolkit().beep(); } else { import projectviewer.ProjectViewer; p = ProjectViewer.getViewer(view); p.setProject(_project); } } void actionPerformed(evt) { if(evt.getSource() == ok) { switchBuffer(); } dialog.dispose(); dialog = null; } void keyPressed(evt) { if(evt.getKeyCode() == KeyEvent.VK_ESCAPE) { dialog.dispose(); } } void keyReleased(evt) { int selected = projectList.getSelectedIndex(); if(selected > -1) { bufferList.ensureIndexIsVisible(selected); } } void keyTyped(evt){} void mouseClicked(evt) { if(evt.getClickCount() > 1) { switchBuffer(); dialog.dispose(); } } void mouseEntered(evt){} void mouseExited(evt){} void mousePressed(evt){} void mouseReleased(evt){} dialog.addKeyListener(this); projectList.addKeyListener(this); projectList.addMouseListener(this); ok.addActionListener(this); close.addActionListener(this); dialog.pack(); import projectviewer.vpt.VPTNode; import projectviewer.ProjectViewer; projectList.setSelectedValue(VPTNode.findProjectFor(ProjectViewer.getActiveProject(view)), true); dialog.setLocationRelativeTo(view); dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); dialog.setVisible(true); } void switchProject() { if (!foundProjectViewerPlugin()) { warnProjectViewerPluginNotFound(); } else { showSwitchDialog(getProjects()); } } // start the macro switchProject();