PageRenderTime 157ms CodeModel.GetById 100ms app.highlight 50ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/org/gjt/sp/jedit/pluginmgr/InstallPanel.java

#
Java | 699 lines | 553 code | 88 blank | 58 comment | 76 complexity | 816f78f6636ae61007b5adb459bbf1c0 MD5 | raw file
  1/*
  2 * InstallPanel.java - For installing plugins
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 2002 Kris Kopicki
  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.table.*;
 29import javax.swing.*;
 30import java.awt.event.*;
 31import java.awt.*;
 32import java.text.NumberFormat;
 33import java.util.*;
 34import org.gjt.sp.jedit.gui.*;
 35import org.gjt.sp.jedit.io.VFSManager;
 36import org.gjt.sp.jedit.*;
 37import org.gjt.sp.util.Log;
 38//}}}
 39
 40class InstallPanel extends JPanel
 41{
 42	//{{{ InstallPanel constructor
 43	InstallPanel(PluginManager window, boolean updates)
 44	{
 45		super(new BorderLayout(12,12));
 46
 47		this.window = window;
 48		this.updates = updates;
 49
 50		setBorder(new EmptyBorder(12,12,12,12));
 51
 52		final JSplitPane split = new JSplitPane(
 53			JSplitPane.VERTICAL_SPLIT,true);
 54
 55		/* Setup the table */
 56		table = new JTable(pluginModel = new PluginTableModel());
 57		table.setShowGrid(false);
 58		table.setIntercellSpacing(new Dimension(0,0));
 59		table.setRowHeight(table.getRowHeight() + 2);
 60		table.setPreferredScrollableViewportSize(new Dimension(500,200));
 61		table.setRequestFocusEnabled(false);
 62		table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
 63		table.setDefaultRenderer(Object.class, new TextRenderer(
 64			(DefaultTableCellRenderer)table.getDefaultRenderer(Object.class)));
 65
 66		TableColumn col1 = table.getColumnModel().getColumn(0);
 67		TableColumn col2 = table.getColumnModel().getColumn(1);
 68		TableColumn col3 = table.getColumnModel().getColumn(2);
 69		TableColumn col4 = table.getColumnModel().getColumn(3);
 70		TableColumn col5 = table.getColumnModel().getColumn(4);
 71
 72		col1.setPreferredWidth(30);
 73		col1.setMinWidth(30);
 74		col1.setMaxWidth(30);
 75		col1.setResizable(false);
 76
 77		col2.setPreferredWidth(180);
 78		col3.setPreferredWidth(130);
 79		col4.setPreferredWidth(70);
 80		col5.setPreferredWidth(70);
 81
 82		JTableHeader header = table.getTableHeader();
 83		header.setReorderingAllowed(false);
 84		header.addMouseListener(new HeaderMouseHandler());
 85
 86		JScrollPane scrollpane = new JScrollPane(table);
 87		scrollpane.getViewport().setBackground(table.getBackground());
 88		split.setTopComponent(scrollpane);
 89
 90		/* Create description */
 91		JScrollPane infoPane = new JScrollPane(
 92			infoBox = new PluginInfoBox());
 93		infoPane.setPreferredSize(new Dimension(500,100));
 94		split.setBottomComponent(infoPane);
 95
 96		SwingUtilities.invokeLater(new Runnable()
 97		{
 98			public void run()
 99			{
100				split.setDividerLocation(0.75);
101			}
102		});
103
104		add(BorderLayout.CENTER,split);
105
106		/* Create buttons */
107		Box buttons = new Box(BoxLayout.X_AXIS);
108
109		buttons.add(new InstallButton());
110		buttons.add(Box.createHorizontalStrut(12));
111		buttons.add(new SelectallButton());
112		buttons.add(Box.createGlue());
113		buttons.add(new SizeLabel());
114
115		add(BorderLayout.SOUTH,buttons);
116	} //}}}
117
118	//{{{ updateModel() method
119	public void updateModel()
120	{
121		pluginModel.clear();
122		infoBox.setText(jEdit.getProperty("plugin-manager.list-download"));
123
124		VFSManager.runInAWTThread(new Runnable()
125		{
126			public void run()
127			{
128				infoBox.setText(null);
129				pluginModel.update();
130			}
131		});
132	} //}}}
133
134	//{{{ Private members
135
136	//{{{ Variables
137	private JTable table;
138	private PluginTableModel pluginModel;
139	private PluginManager window;
140	private PluginInfoBox infoBox;
141
142	private boolean updates;
143	//}}}
144
145	//{{{ formatSize() method
146	private String formatSize(int size)
147	{
148		NumberFormat df = NumberFormat.getInstance();
149		df.setMaximumFractionDigits(1);
150		df.setMinimumFractionDigits(0);
151		String sizeText;
152		if (size < 1048576)
153			sizeText = size/1024 + "KB";
154		else
155			sizeText = df.format(size/1048576d) + "MB";
156		return sizeText;
157	} //}}}
158
159	//}}}
160
161	//{{{ Inner classes
162
163	//{{{ PluginTableModel class
164	class PluginTableModel extends AbstractTableModel
165	{
166		private ArrayList entries = new ArrayList();
167		private int sortType = EntryCompare.CATEGORY;
168
169		//{{{ getColumnClass() method
170		public Class getColumnClass(int columnIndex)
171		{
172			switch (columnIndex)
173			{
174				case 0: return Boolean.class;
175				case 1:
176				case 2:
177				case 3:
178				case 4: return Object.class;
179				default: throw new Error("Column out of range");
180			}
181		} //}}}
182
183		//{{{ getColumnCount() method
184		public int getColumnCount()
185		{
186			return 5;
187		} //}}}
188
189		//{{{ getColumnName() method
190		public String getColumnName(int column)
191		{
192			switch (column)
193			{
194				case 0: return " ";
195				case 1: return " "+jEdit.getProperty("install-plugins.info.name");
196				case 2: return " "+jEdit.getProperty("install-plugins.info.category");
197				case 3: return " "+jEdit.getProperty("install-plugins.info.version");
198				case 4: return " "+jEdit.getProperty("install-plugins.info.size");
199				default: throw new Error("Column out of range");
200			}
201		} //}}}
202
203		//{{{ getRowCount() method
204		public int getRowCount()
205		{
206			return entries.size();
207		} //}}}
208
209		//{{{ getValueAt() method
210		public Object getValueAt(int rowIndex,int columnIndex)
211		{
212			Object obj = entries.get(rowIndex);
213			if(obj instanceof String)
214			{
215				if(columnIndex == 1)
216					return obj;
217				else
218					return null;
219			}
220			else
221			{
222				Entry entry = (Entry)obj;
223
224				switch (columnIndex)
225				{
226					case 0:
227						return Boolean.valueOf(
228							entry.install);
229					case 1:
230						return entry.name;
231					case 2:
232						return entry.set;
233					case 3:
234						return entry.version;
235					case 4:
236						return formatSize(entry.size);
237					default:
238						throw new Error("Column out of range");
239				}
240			}
241		} //}}}
242
243		//{{{ isCellEditable() method
244		public boolean isCellEditable(int rowIndex, int columnIndex)
245		{
246			return (columnIndex == 0);
247		} //}}}
248
249		//{{{ setSelectAll() method
250		public void setSelectAll(boolean b)
251		{
252			if(isDownloadingList())
253				return;
254
255			int length = getRowCount();
256			for (int i = 0; i < length; i++)
257			{
258				if (b)
259					setValueAt(Boolean.TRUE,i,0);
260				else
261				{
262					Entry entry = (Entry)entries.get(i);
263					entry.parents = new LinkedList();
264					entry.install = false;
265				}
266			}
267			fireTableChanged(new TableModelEvent(this));
268		} //}}}
269
270		//{{{ setSortType() method
271		public void setSortType(int type)
272		{
273			sortType = type;
274			sort(type);
275		} //}}}
276
277		//{{{ setValueAt() method
278		public void setValueAt(Object aValue, int row, int column)
279		{
280			if (column != 0) return;
281
282			Object obj = entries.get(row);
283			if(obj instanceof String)
284				return;
285
286			Entry entry = (Entry)obj;
287			Vector deps = entry.plugin.getCompatibleBranch().deps;
288			boolean value = ((Boolean)aValue).booleanValue();
289			if (!value)
290			{
291				if (entry.parents.size() > 0)
292				{
293					String[] args = {
294						entry.name,
295						entry.getParentString()
296					};
297					int result = GUIUtilities.confirm(
298						window,"plugin-manager.dependency",
299						args,JOptionPane.OK_CANCEL_OPTION,
300						JOptionPane.WARNING_MESSAGE);
301					if (result != JOptionPane.OK_OPTION)
302						return;
303					Iterator parentsIter = entry.parents.iterator();
304					while(parentsIter.hasNext())
305					{
306						((Entry)parentsIter.next()).install = false;
307					}
308
309					fireTableRowsUpdated(0,getRowCount() - 1);
310				}
311			}
312
313			for (int i = 0; i < deps.size(); i++)
314			{
315				PluginList.Dependency dep = (PluginList.Dependency)deps.elementAt(i);
316				if (dep.what.equals("plugin"))
317				{
318					for (int j = 0; j < entries.size(); j++)
319					{
320						Entry temp = (Entry)entries.get(j);
321						if (temp.plugin == dep.plugin)
322						{
323							if (value)
324							{
325								temp.parents.add(entry);
326								setValueAt(Boolean.TRUE,j,0);
327							}
328							else
329								temp.parents.remove(entry);
330						}
331					}
332				}
333			}
334
335			entry.install = Boolean.TRUE.equals(aValue);
336			fireTableCellUpdated(row,column);
337		} //}}}
338
339		//{{{ sort() method
340		public void sort(int type)
341		{
342			if(isDownloadingList())
343				return;
344
345			Collections.sort(entries,new EntryCompare(type));
346			fireTableChanged(new TableModelEvent(this));
347		}
348		//}}}
349
350		//{{{ isDownloadingList() method
351		private boolean isDownloadingList()
352		{
353			return (entries.size() == 1 && entries.get(0) instanceof String);
354		} //}}}
355
356		//{{{ clear() method
357		public void clear()
358		{
359			entries = new ArrayList();
360			fireTableChanged(new TableModelEvent(this));
361		} //}}}
362
363		//{{{ update() method
364		public void update()
365		{
366			PluginList pluginList = window.getPluginList();
367
368			if (pluginList == null) return;
369
370			entries = new ArrayList();
371
372			for(int i = 0; i < pluginList.pluginSets.size(); i++)
373			{
374				PluginList.PluginSet set = (PluginList.PluginSet)
375					pluginList.pluginSets.get(i);
376				for(int j = 0; j < set.plugins.size(); j++)
377				{
378					PluginList.Plugin plugin = (PluginList.Plugin)
379						pluginList.pluginHash.get(set.plugins.get(j));
380					PluginList.Branch branch = plugin.getCompatibleBranch();
381					String installedVersion =
382						plugin.getInstalledVersion();
383					if (updates)
384					{
385						if(branch != null
386							&& branch.canSatisfyDependencies()
387							&& installedVersion != null
388							&& MiscUtilities.compareStrings(branch.version,
389							installedVersion,false) > 0)
390						{
391							entries.add(new Entry(plugin,set.name));
392						}
393					}
394					else
395					{
396						if(installedVersion == null && plugin.canBeInstalled())
397							entries.add(new Entry(plugin,set.name));
398					}
399				}
400			}
401
402			sort(sortType);
403
404			fireTableChanged(new TableModelEvent(this));
405		} //}}}
406	} //}}}
407
408	//{{{ Entry class
409	class Entry
410	{
411		String name, version, author, date, description, set;
412		int size;
413		boolean install;
414		PluginList.Plugin plugin;
415		LinkedList parents = new LinkedList();
416
417		Entry(PluginList.Plugin plugin, String set)
418		{
419			PluginList.Branch branch = plugin.getCompatibleBranch();
420			boolean downloadSource = jEdit.getBooleanProperty("plugin-manager.downloadSource");
421			int size = (downloadSource) ? branch.downloadSourceSize : branch.downloadSize;
422
423			this.name = plugin.name;
424			this.author = plugin.author;
425			this.version = branch.version;
426			this.size = size;
427			this.date = branch.date;
428			this.description = plugin.description;
429			this.set = set;
430			this.install = false;
431			this.plugin = plugin;
432		}
433
434		String getParentString()
435		{
436			StringBuffer buf = new StringBuffer();
437			Iterator iter = parents.iterator();
438			while(iter.hasNext())
439			{
440				buf.append(((Entry)iter.next()).name);
441				if(iter.hasNext())
442					buf.append('\n');
443			}
444			return buf.toString();
445		}
446	} //}}}
447
448	//{{{ PluginInfoBox class
449	class PluginInfoBox extends JTextArea implements ListSelectionListener
450	{
451		public PluginInfoBox()
452		{
453			setEditable(false);
454			setLineWrap(true);
455			setWrapStyleWord(true);
456			table.getSelectionModel().addListSelectionListener(this);
457		}
458
459
460		public void valueChanged(ListSelectionEvent e)
461		{
462			String text = "";
463			if (table.getSelectedRowCount() == 1)
464			{
465				Entry entry = (Entry)pluginModel.entries
466					.get(table.getSelectedRow());
467				text = jEdit.getProperty("install-plugins.info",
468					new String[] {entry.author,entry.date,entry.description});
469			}
470			setText(text);
471			setCaretPosition(0);
472		}
473	} //}}}
474
475	//{{{ SizeLabel class
476	class SizeLabel extends JLabel implements TableModelListener
477	{
478		private int size;
479
480		public SizeLabel()
481		{
482			size = 0;
483			setText(jEdit.getProperty("install-plugins.totalSize")+formatSize(size));
484			pluginModel.addTableModelListener(this);
485		}
486
487		public void tableChanged(TableModelEvent e)
488		{
489			if (e.getType() == TableModelEvent.UPDATE)
490			{
491				if(pluginModel.isDownloadingList())
492					return;
493
494				size = 0;
495				int length = pluginModel.getRowCount();
496				for (int i = 0; i < length; i++)
497				{
498					Entry entry = (Entry)pluginModel
499						.entries.get(i);
500					if (entry.install)
501						size += entry.size;
502				}
503				setText(jEdit.getProperty("install-plugins.totalSize")+formatSize(size));
504			}
505		}
506	} //}}}
507
508	//{{{ SelectallButton class
509	class SelectallButton extends JCheckBox implements ActionListener, TableModelListener
510	{
511		public SelectallButton()
512		{
513			super(jEdit.getProperty("install-plugins.select-all"));
514			addActionListener(this);
515			pluginModel.addTableModelListener(this);
516			setEnabled(false);
517		}
518
519		public void actionPerformed(ActionEvent evt)
520		{
521			pluginModel.setSelectAll(isSelected());
522		}
523
524		public void tableChanged(TableModelEvent e)
525		{
526			if(pluginModel.isDownloadingList())
527				return;
528
529			setEnabled(pluginModel.getRowCount() != 0);
530			if (e.getType() == TableModelEvent.UPDATE)
531			{
532				int length = pluginModel.getRowCount();
533				for (int i = 0; i < length; i++)
534					if (!((Boolean)pluginModel.getValueAt(i,0)).booleanValue())
535					{
536						setSelected(false);
537						return;
538					}
539				if (length > 0)
540					setSelected(true);
541			}
542		}
543	} //}}}
544
545	//{{{ InstallButton class
546	class InstallButton extends JButton implements ActionListener, TableModelListener
547	{
548		public InstallButton()
549		{
550			super(jEdit.getProperty("install-plugins.install"));
551			pluginModel.addTableModelListener(this);
552			addActionListener(this);
553			setEnabled(false);
554		}
555
556		public void actionPerformed(ActionEvent evt)
557		{
558			if(pluginModel.isDownloadingList())
559				return;
560
561			boolean downloadSource = jEdit.getBooleanProperty(
562				"plugin-manager.downloadSource");
563			boolean installUser = jEdit.getBooleanProperty(
564				"plugin-manager.installUser");
565			Roster roster = new Roster();
566			String installDirectory;
567			if(installUser)
568			{
569				installDirectory = MiscUtilities.constructPath(
570					jEdit.getSettingsDirectory(),"jars");
571			}
572			else
573			{
574				installDirectory = MiscUtilities.constructPath(
575					jEdit.getJEditHome(),"jars");
576			}
577
578			int length = pluginModel.getRowCount();
579			int instcount = 0;
580			for (int i = 0; i < length; i++)
581			{
582				Entry entry = (Entry)pluginModel.entries.get(i);
583				if (entry.install)
584				{
585					entry.plugin.install(roster,installDirectory,downloadSource);
586					if (updates)
587						entry.plugin.getCompatibleBranch().satisfyDependencies(
588						roster,installDirectory,downloadSource);
589					instcount++;
590				}
591			}
592
593			if(roster.isEmpty())
594				return;
595
596			boolean cancel = false;
597			if (updates && roster.getOperationCount() > instcount)
598				if (GUIUtilities.confirm(window,
599					"install-plugins.depend",
600					null,
601					JOptionPane.OK_CANCEL_OPTION,
602					JOptionPane.WARNING_MESSAGE) == JOptionPane.CANCEL_OPTION)
603					cancel = true;
604			
605			if (!cancel)
606			{
607				new PluginManagerProgress(window,roster);
608	
609				roster.performOperationsInAWTThread(window);
610				pluginModel.update();
611			}
612		}
613
614		public void tableChanged(TableModelEvent e)
615		{
616			if(pluginModel.isDownloadingList())
617				return;
618
619			if (e.getType() == TableModelEvent.UPDATE)
620			{
621				int length = pluginModel.getRowCount();
622				for (int i = 0; i < length; i++)
623					if (((Boolean)pluginModel.getValueAt(i,0)).booleanValue())
624					{
625						setEnabled(true);
626						return;
627					}
628				setEnabled(false);
629			}
630		}
631	} //}}}
632
633	//{{{ EntryCompare class
634	static class EntryCompare implements Comparator
635	{
636		public static final int NAME = 0;
637		public static final int CATEGORY = 1;
638
639		private int type;
640
641		public EntryCompare(int type)
642		{
643			this.type = type;
644		}
645
646		public int compare(Object o1, Object o2)
647		{
648			InstallPanel.Entry e1 = (InstallPanel.Entry)o1;
649			InstallPanel.Entry e2 = (InstallPanel.Entry)o2;
650
651			if (type == NAME)
652				return e1.name.compareToIgnoreCase(e2.name);
653			else
654			{
655				int result;
656				if ((result = e1.set.compareToIgnoreCase(e2.set)) == 0)
657					return e1.name.compareToIgnoreCase(e2.name);
658				return result;
659			}
660		}
661	} //}}}
662
663	//{{{ HeaderMouseHandler class
664	class HeaderMouseHandler extends MouseAdapter
665	{
666		public void mouseClicked(MouseEvent evt)
667		{
668			switch(table.getTableHeader().columnAtPoint(evt.getPoint()))
669			{
670				case 1:
671					pluginModel.sort(EntryCompare.NAME);
672					break;
673				case 2:
674					pluginModel.sort(EntryCompare.CATEGORY);
675					break;
676				default:
677			}
678		}
679	} //}}}
680
681	//{{{ TextRenderer
682	class TextRenderer extends DefaultTableCellRenderer
683	{
684		private DefaultTableCellRenderer tcr;
685
686		public TextRenderer(DefaultTableCellRenderer tcr)
687		{
688			this.tcr = tcr;
689		}
690
691		public Component getTableCellRendererComponent(JTable table, Object value,
692			boolean isSelected, boolean hasFocus, int row, int column)
693		{
694			return tcr.getTableCellRendererComponent(table,value,isSelected,false,row,column);
695		}
696	} //}}}
697
698	//}}}
699}