PageRenderTime 34ms CodeModel.GetById 12ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre3/org/gjt/sp/jedit/pluginmgr/PluginManager.java

#
Java | 431 lines | 345 code | 66 blank | 20 comment | 67 complexity | 39c21dd647c54fee07449707cf3c8920 MD5 | raw file
  1/*
  2 * PluginManager.java - Plugin manager window
  3 * Copyright (C) 2000, 2001 Slava Pestov
  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
 20package org.gjt.sp.jedit.pluginmgr;
 21
 22import javax.swing.border.*;
 23import javax.swing.event.*;
 24import javax.swing.tree.*;
 25import javax.swing.*;
 26import java.awt.event.*;
 27import java.awt.*;
 28import java.io.File;
 29import java.util.*;
 30import org.gjt.sp.jedit.gui.*;
 31import org.gjt.sp.jedit.*;
 32import org.gjt.sp.util.Log;
 33
 34public class PluginManager extends JDialog
 35{
 36	public PluginManager(Frame frame)
 37	{
 38		super(frame,jEdit.getProperty("plugin-manager.title"),true);
 39
 40		JPanel content = new JPanel(new BorderLayout());
 41		content.setBorder(new EmptyBorder(12,12,12,12));
 42		setContentPane(content);
 43
 44		JLabel caption = new JLabel(jEdit.getProperty(
 45			"plugin-manager.caption"));
 46		caption.setBorder(new EmptyBorder(0,0,6,0));
 47		content.add(BorderLayout.NORTH,caption);
 48
 49		tree = new JTree();
 50		tree.setCellRenderer(new Renderer());
 51		tree.setRootVisible(false);
 52		tree.setVisibleRowCount(16);
 53		tree.addTreeSelectionListener(new TreeHandler());
 54
 55		JPanel panel = new JPanel(new BorderLayout());
 56
 57		panel.add(BorderLayout.CENTER,new JScrollPane(tree));
 58
 59		JPanel panel2 = new JPanel(new BorderLayout());
 60		panel2.setBorder(new EmptyBorder(6,0,0,0));
 61		JPanel labelBox = new JPanel(new GridLayout(3,1,0,3));
 62		labelBox.setBorder(new EmptyBorder(0,0,0,12));
 63		labelBox.add(new JLabel(jEdit.getProperty("plugin-manager"
 64			+ ".info.name"),SwingConstants.RIGHT));
 65		labelBox.add(new JLabel(jEdit.getProperty("plugin-manager"
 66			+ ".info.author"),SwingConstants.RIGHT));
 67		labelBox.add(new JLabel(jEdit.getProperty("plugin-manager"
 68			+ ".info.version"),SwingConstants.RIGHT));
 69		panel2.add(BorderLayout.WEST,labelBox);
 70
 71		JPanel valueBox = new JPanel(new GridLayout(3,1,0,3));
 72		valueBox.add(name = new JLabel());
 73		valueBox.add(author = new JLabel());
 74		valueBox.add(version = new JLabel());
 75		panel2.add(BorderLayout.CENTER,valueBox);
 76
 77		panel.add(BorderLayout.SOUTH,panel2);
 78		content.add(BorderLayout.CENTER,panel);
 79
 80		JPanel buttons = new JPanel();
 81		buttons.setLayout(new BoxLayout(buttons,BoxLayout.X_AXIS));
 82		buttons.setBorder(new EmptyBorder(12,0,0,0));
 83
 84		buttons.add(Box.createGlue());
 85		remove = new JButton(jEdit.getProperty("plugin-manager"
 86			+ ".remove"));
 87		remove.addActionListener(new ActionHandler());
 88		buttons.add(remove);
 89		buttons.add(Box.createHorizontalStrut(6));
 90		update = new JButton(jEdit.getProperty("plugin-manager"
 91			+ ".update"));
 92		update.addActionListener(new ActionHandler());
 93		buttons.add(update);
 94		buttons.add(Box.createHorizontalStrut(6));
 95		install = new JButton(jEdit.getProperty("plugin-manager"
 96			+ ".install"));
 97		install.addActionListener(new ActionHandler());
 98		buttons.add(install);
 99		buttons.add(Box.createHorizontalStrut(6));
100		close = new JButton(jEdit.getProperty("common.close"));
101		close.addActionListener(new ActionHandler());
102		buttons.add(close);
103		buttons.add(Box.createGlue());
104
105		content.add(BorderLayout.SOUTH,buttons);
106
107		updateTree();
108
109		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
110
111		pack();
112
113		setLocationRelativeTo(frame);
114
115		show();
116	}
117
118	// private members
119	private JTree tree;
120	private JLabel name;
121	private JLabel author;
122	private JLabel version;
123	private JButton remove;
124	private JButton update;
125	private JButton install;
126	private JButton close;
127
128	private void updateTree()
129	{
130		DefaultMutableTreeNode treeRoot = new DefaultMutableTreeNode();
131		DefaultTreeModel treeModel = new DefaultTreeModel(treeRoot);
132
133		DefaultMutableTreeNode loadedTree = new DefaultMutableTreeNode(
134			jEdit.getProperty("plugin-manager.loaded"),true);
135		DefaultMutableTreeNode notLoadedTree = new DefaultMutableTreeNode(
136			jEdit.getProperty("plugin-manager.not-loaded"),true);
137		DefaultMutableTreeNode newTree = new DefaultMutableTreeNode(
138			jEdit.getProperty("plugin-manager.new"),true);
139
140		EditPlugin[] plugins = jEdit.getPlugins();
141		for(int i = 0; i < plugins.length; i++)
142		{
143			EditPlugin plugin = plugins[i];
144			String path = plugin.getJAR().getPath();
145			if(!new File(path).exists())
146			{
147				// plugin was deleted
148				continue;
149			}
150
151			if(plugin instanceof EditPlugin.Broken)
152			{
153				Entry entry = new Entry(path,plugin.getClassName(),true);
154				notLoadedTree.add(new DefaultMutableTreeNode(entry));
155			}
156			else
157			{
158				Entry entry = new Entry(path,plugin.getClassName(),false);
159				loadedTree.add(new DefaultMutableTreeNode(entry));
160			}
161		}
162
163		if(notLoadedTree.getChildCount() != 0)
164			treeRoot.add(notLoadedTree);
165
166		if(loadedTree.getChildCount() != 0)
167			treeRoot.add(loadedTree);
168
169		String[] newPlugins = jEdit.getNotLoadedPluginJARs();
170		for(int i = 0; i < newPlugins.length; i++)
171		{
172			Entry entry = new Entry(newPlugins[i],null,false);
173			newTree.add(new DefaultMutableTreeNode(entry));
174		}
175
176		if(newTree.getChildCount() != 0)
177			treeRoot.add(newTree);
178
179		tree.setModel(treeModel);
180		for(int i = 0; i < tree.getRowCount(); i++)
181			tree.expandRow(i);
182
183		remove.setEnabled(false);
184
185		name.setText(null);
186		author.setText(null);
187		version.setText(null);
188	}
189
190	class Entry
191	{
192		String clazz;
193		String name, version, author;
194		Vector jars;
195		boolean broken;
196
197		Entry(String path, String clazz, boolean broken)
198		{
199			Entry.this.clazz = clazz;
200			Entry.this.broken = broken;
201
202			jars = new Vector();
203			jars.addElement(path);
204
205			if(clazz == null)
206				Entry.this.name = path;
207			else
208			{
209				Entry.this.name = jEdit.getProperty("plugin." + clazz + ".name");
210				if(name == null)
211					name = clazz;
212
213				Entry.this.version = jEdit.getProperty("plugin." + clazz
214					+ ".version");
215
216				Entry.this.author = jEdit.getProperty("plugin." + clazz
217					+ ".author");
218
219				String jarsProp = jEdit.getProperty("plugin." + clazz
220					+ ".jars");
221
222				if(jarsProp != null)
223				{
224					String directory = MiscUtilities.getParentOfPath(path);
225
226					StringTokenizer st = new StringTokenizer(jarsProp);
227					while(st.hasMoreElements())
228					{
229						jars.addElement(MiscUtilities.constructPath(
230							directory,st.nextToken()));
231					}
232				}
233			}
234		}
235
236		public String toString()
237		{
238			return Entry.this.name;
239		}
240	}
241
242	class ActionHandler implements ActionListener
243	{
244		public void actionPerformed(ActionEvent evt)
245		{
246			Object source = evt.getSource();
247			if(source == close)
248				dispose();
249			else if(source == remove)
250			{
251				TreePath[] selected = tree.getSelectionModel()
252					.getSelectionPaths();
253
254				StringBuffer buf = new StringBuffer();
255				Roster roster = new Roster();
256				for(int i = 0; i < selected.length; i++)
257				{
258					Object last = ((DefaultMutableTreeNode)
259						selected[i].getLastPathComponent())
260						.getUserObject();
261					if(last instanceof Entry)
262					{
263						Entry entry = (Entry)last;
264						for(int j = 0; j < entry.jars.size(); j++)
265						{
266							String jar = (String)entry.jars.elementAt(j);
267							if(buf.length() != 0)
268								buf.append('\n');
269							buf.append(jar);
270							roster.addOperation(new Roster.Remove(jar));
271						}
272					}
273				}
274
275				String[] args = { buf.toString() };
276				if(GUIUtilities.confirm(PluginManager.this,
277					"plugin-manager.remove-confirm",args,
278					JOptionPane.YES_NO_OPTION,
279					JOptionPane.QUESTION_MESSAGE)
280					== JOptionPane.YES_OPTION)
281				{
282					new PluginManagerProgress(PluginManager.this,
283						jEdit.getProperty("plugin-manager.progress"
284						+ ".removing-task"),roster);
285					updateTree();
286				}
287			}
288			else if(source == update)
289			{
290				PluginList list = new PluginListDownloadProgress(PluginManager.this)
291					.getPluginList();
292				if(list == null)
293					return;
294
295				if(jEdit.getSettingsDirectory() == null)
296				{
297					GUIUtilities.error(PluginManager.this,
298						"no-settings",null);
299					return;
300				}
301
302				Vector plugins = new Vector();
303				for(int i = 0; i < list.plugins.size(); i++)
304				{
305					PluginList.Plugin plugin = (PluginList.Plugin)list
306						.plugins.elementAt(i);
307					PluginList.Branch branch = plugin.getCompatibleBranch();
308
309					if(branch != null
310						&& plugin.installedVersion != null
311						&& MiscUtilities.compareStrings(branch.version,
312						plugin.installedVersion,false) > 0)
313						plugins.addElement(plugin);
314				}
315
316				if(plugins.size() == 0)
317				{
318					GUIUtilities.message(PluginManager.this,
319						"plugin-manager.up-to-date",null);
320					return;
321				}
322
323				Roster roster = new Roster();
324				new InstallPluginsDialog(PluginManager.this,plugins,
325					InstallPluginsDialog.UPDATE)
326					.installPlugins(roster);
327
328				if(roster.isEmpty())
329					return;
330
331				new PluginManagerProgress(PluginManager.this,
332					jEdit.getProperty("plugin-manager.progress"
333					+ ".updating-task"),roster);
334
335				updateTree();
336			}
337			else if(source == install)
338			{
339				PluginList list = new PluginListDownloadProgress(PluginManager.this)
340					.getPluginList();
341				if(list == null)
342					return;
343
344				if(jEdit.getSettingsDirectory() == null
345					&& jEdit.getJEditHome() == null)
346				{
347					GUIUtilities.error(PluginManager.this,"no-settings",null);
348					return;
349				}
350
351				Vector plugins = new Vector();
352				for(int i = 0; i < list.plugins.size(); i++)
353				{
354					PluginList.Plugin plugin = (PluginList.Plugin)list
355						.plugins.elementAt(i);
356					if(plugin.installed == null
357						&& plugin.canBeInstalled())
358						plugins.addElement(plugin);
359				}
360
361				Roster roster = new Roster();
362				new InstallPluginsDialog(PluginManager.this,plugins,
363					InstallPluginsDialog.INSTALL)
364					.installPlugins(roster);
365
366				if(roster.isEmpty())
367					return;
368
369				new PluginManagerProgress(PluginManager.this,
370					jEdit.getProperty("plugin-manager.progress"
371					+ ".installing-task"),roster);
372
373				updateTree();
374			}
375		}
376	}
377
378	class TreeHandler implements TreeSelectionListener
379	{
380		public void valueChanged(TreeSelectionEvent evt)
381		{
382			TreePath selection = evt.getPath();
383			DefaultMutableTreeNode node;
384			if(selection == null)
385			{
386				node = null;
387			}
388			else
389			{
390				node = (DefaultMutableTreeNode)
391					selection.getLastPathComponent();
392			}
393
394			name.setText(null);
395			author.setText(null);
396			version.setText(null);
397
398			if(node != null && node.isLeaf()
399				&& node.getUserObject() instanceof Entry)
400			{
401				remove.setEnabled(true);
402
403				Entry entry = (Entry)node.getUserObject();
404
405				if(entry.clazz != null)
406				{
407					name.setText(entry.name);
408					author.setText(entry.author);
409					version.setText(entry.version);
410				}
411			}
412			else
413				remove.setEnabled(false);
414		}
415	}
416
417	class Renderer extends DefaultTreeCellRenderer
418	{
419		public Component getTreeCellRendererComponent(JTree tree,
420			Object value, boolean selected, boolean expanded,
421			boolean leaf, int row, boolean hasFocus)
422		{
423			super.getTreeCellRendererComponent(tree,value,
424				selected,expanded,leaf,row,hasFocus);
425
426			setIcon(null);
427
428			return this;
429		}
430	}
431}