PageRenderTime 52ms CodeModel.GetById 20ms app.highlight 11ms RepoModel.GetById 19ms app.codeStats 0ms

/bundles/plugins-trunk/CommonControls/common/gui/VFSPathFileList.java

#
Java | 157 lines | 113 code | 14 blank | 30 comment | 17 complexity | e8a334b7341a3ec1d344927e19ef491c MD5 | raw file
  1/*
  2 * jEdit - Programmer's Text Editor
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright Š 2011 Matthieu Casanova
  7 *
  8 * This program is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU General Public License
 10 * as published by the Free Software Foundation; either version 2
 11 * of the License, or any later version.
 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
 22package common.gui;
 23
 24import java.awt.event.ActionEvent;
 25import java.awt.event.ActionListener;
 26import java.io.File;
 27import java.util.ArrayList;
 28import java.util.Collection;
 29import java.util.StringTokenizer;
 30import javax.swing.BorderFactory;
 31import javax.swing.Box;
 32import javax.swing.BoxLayout;
 33import javax.swing.DefaultListModel;
 34import javax.swing.JButton;
 35import javax.swing.JList;
 36import javax.swing.JPanel;
 37import javax.swing.JScrollPane;
 38
 39import org.gjt.sp.jedit.GUIUtilities;
 40import org.gjt.sp.jedit.TextUtilities;
 41import org.gjt.sp.jedit.browser.VFSBrowser;
 42import org.gjt.sp.jedit.jEdit;
 43
 44/**
 45 * A component that contains a JList with a list of path,
 46 * and two buttons add and remove to add paths.
 47 * This can be used in option panes.
 48 * The property given in the constructor contains the list of paths
 49 * separated by the File.pathSeparator
 50 * Calling save() will save the paths to that property.
 51 *
 52 * @author Matthieu Casanova
 53 */
 54public class VFSPathFileList extends JPanel
 55{
 56	private JList searchList;
 57
 58	private DefaultListModel model;
 59
 60	private JButton add;
 61	private JButton remove;
 62	private String pathProperty;
 63	private JScrollPane scroll;
 64
 65	public VFSPathFileList()
 66	{
 67		setBorder(BorderFactory.createEtchedBorder());
 68		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
 69		model = new DefaultListModel();
 70		searchList = new JList(model);
 71		searchList.setPrototypeCellValue("a");
 72		add = new JButton(jEdit.getProperty("common.add"));
 73		remove = new JButton(jEdit.getProperty("common.remove"));
 74		ActionListener actionListener = new MyActionListener();
 75		add.addActionListener(actionListener);
 76		remove.addActionListener(actionListener);
 77		Box box = Box.createHorizontalBox();
 78
 79		box.add(add);
 80		box.add(Box.createHorizontalStrut(6));
 81		box.add(remove);
 82		box.add(Box.createHorizontalGlue());
 83		scroll = new JScrollPane(searchList);
 84		add(scroll);
 85		add(box);
 86	}
 87
 88	public VFSPathFileList(String pathProperty)
 89	{
 90		this();
 91		this.pathProperty = pathProperty;
 92		scroll.setBorder(BorderFactory.createTitledBorder(jEdit.getProperty(pathProperty + ".label")));
 93		String property = jEdit.getProperty(pathProperty, "");
 94		StringTokenizer tokenizer = new StringTokenizer(property, File.pathSeparator);
 95		while (tokenizer.hasMoreTokens())
 96		{
 97			String s = tokenizer.nextToken();
 98			addPath(s);
 99		}
100	}
101
102	public void save()
103	{
104		if (pathProperty != null)
105		{
106			jEdit.setProperty(pathProperty, getPaths());
107		}
108	}
109
110	public String getPaths()
111	{
112		int size = model.size();
113		Collection<String> paths = new ArrayList<String>();
114		for (int i = 0; i < size; i++)
115		{
116			String path = (String) model.getElementAt(i);
117			paths.add(path);
118		}
119		return TextUtilities.join(paths, File.pathSeparator);
120	}
121
122	private void addPath(String path)
123	{
124		if (path != null && !path.isEmpty() && !model.contains(path))
125			model.addElement(path);
126	}
127
128	private class MyActionListener implements ActionListener
129	{
130		private String lastBrowserDialog;
131
132		@Override
133		public void actionPerformed(ActionEvent e)
134		{
135			if (e.getSource() == add)
136			{
137				String[] strings = GUIUtilities
138					.showVFSFileDialog(jEdit.getActiveView(), lastBrowserDialog,
139							   VFSBrowser.CHOOSE_DIRECTORY_DIALOG, true);
140				if (strings != null)
141				{
142					for (String string : strings)
143					{
144						lastBrowserDialog = string;
145						addPath(string);
146					}
147				}
148			}
149			if (e.getSource() == remove)
150			{
151				int selectedIndex = searchList.getSelectedIndex();
152				if (selectedIndex != -1)
153					model.remove(selectedIndex);
154			}
155		}
156	}
157}