PageRenderTime 54ms CodeModel.GetById 34ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/org/gjt/sp/jedit/pluginmgr/PluginManager.java

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