⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xplugininfotable.java

📁 XBrowser是一个完全免费并且开源的Web浏览器
💻 JAVA
字号:
/****************************************************************
*              XBrowser  -  eXtended web Browser                *
*                                                               *
*           Copyright (c) 2000-2001  Armond Avanes              *
*     Refer to ReadMe & License files for more information      *
*                                                               *
*                                                               *
*                      By: Armond Avanes                        *
*       Armond555@yahoo.com     &    Armond333@yahoo.com        *
*                http://xbrowser.sourceforge.net/               *
*****************************************************************/
package xbrowser.widgets;

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.table.*;
import java.util.*;
import java.text.*;
import java.beans.*;
import java.awt.*;
import java.awt.event.*;

import xbrowser.*;
import xbrowser.util.*;
import xbrowser.screen.*;
import xbrowser.plugin.*;
import xbrowser.plugin.event.*;

public class XPluginInfoTable extends JTable
{
	public XPluginInfoTable()
	{
		customizeTable();
		registerListeners();

		startPluginAction.setEnabled(false);
		stopPluginAction.setEnabled(false);
		uninstallPluginAction.setEnabled(false);
		pluginInfoAction.setEnabled(false);

	    pluginInfoPopup.add( XRepository.getComponentBuilder().buildMenuItem(startPluginAction) );
	    pluginInfoPopup.add( XRepository.getComponentBuilder().buildMenuItem(stopPluginAction) );
	    pluginInfoPopup.add( XRepository.getComponentBuilder().buildMenuItem(uninstallPluginAction) );
	    pluginInfoPopup.addSeparator();
	    pluginInfoPopup.add( XRepository.getComponentBuilder().buildMenuItem(pluginInfoAction) );
		add(pluginInfoPopup);

		XRepository.getPluginManager().addPluginListener(model);
	}

	private void customizeTable()
	{
	XPluginInfoCellRenderer cell_renderer = new XPluginInfoCellRenderer();
	XPluginInfoHeaderRenderer header_renderer = new XPluginInfoHeaderRenderer();
	TableColumn col;

		setModel(model);

		for( int i=0; i<getColumnCount(); i++ )
		{
			col = getColumnModel().getColumn(i);
			col.setPreferredWidth(columnWidths[i]);
			col.setCellRenderer(cell_renderer);
			col.setHeaderRenderer(header_renderer);
		}

		setRowHeight(20);
	}

	// JTable is not update its header properly!! maybe a bug!!
	public void updateUI()
	{
		super.updateUI();
		getTableHeader().updateUI();
	}

	private void updateActions()
	{
	int sel_rows[] = getSelectedRows();
	boolean has_started_plugin = false;
	boolean has_not_started_plugin = false;
	int status;

		for( int i=0; i<sel_rows.length; i++ )
		{
			status = model.getPluginInfo(sel_rows[i]).getStatus();

			if( status==XProjectConstants.PLUGIN_STARTED )
			{
				has_started_plugin = true;
				break;
			}
			else if( status==XProjectConstants.PLUGIN_INITIATED || status==XProjectConstants.PLUGIN_STOPPED )
			{
				has_not_started_plugin = true;
				break;
			}
		}

		startPluginAction.setEnabled( has_not_started_plugin );
		stopPluginAction.setEnabled( has_started_plugin );
		uninstallPluginAction.setEnabled( sel_rows.length>0 );
		pluginInfoAction.setEnabled( sel_rows.length>0 );
	}

	private void displayPluginInfo(XPluginObject plugin)
	{
		XRepository.getScreenManager().getPluginPropertiesLayout(plugin).setVisible(true);
	}

	private void registerListeners()
	{
		addMouseListener(new MouseAdapter() {
			public void mouseReleased(MouseEvent e)
			{
				showPopup(e);
			}

			public void mousePressed(MouseEvent e)
			{
				showPopup(e);
			}

			public void mouseClicked(MouseEvent e)
			{
				if( e.getClickCount()==2 )
				{
				int index = rowAtPoint( e.getPoint() );

					if( index!=-1 )
						displayPluginInfo( model.getPluginInfo(index) );
				}

				showPopup(e);
			}

			private void showPopup(MouseEvent e)
			{
				if( e.isPopupTrigger() )
				{
				int index = rowAtPoint( e.getPoint() );

					if( !isRowSelected(index) )
						getSelectionModel().setSelectionInterval(index,index);

					pluginInfoPopup.show(XPluginInfoTable.this, e.getX(), e.getY());
				}
			}
		});

		getSelectionModel().addListSelectionListener( new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e)
			{
				updateActions();
			}
		});
/*
		model.addTableModelListener( new TableModelListener() {
			public void tableChanged(TableModelEvent e)
			{
				updateActions();
			}
		});*/
	}

	private class XPluginInfoCellRenderer extends DefaultTableCellRenderer//JLabel implements TableCellRenderer
	{
		public XPluginInfoCellRenderer()
		{
			setHorizontalAlignment( JLabel.CENTER );
		}

		public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
		{
			super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

		String label = "";

			if( value instanceof String )
				label = (String)value;

			setText(label);

			if( label.equals("") )
				setToolTipText(null);
			else
				setToolTipText(label);

			/*if( isSelected )
			{
			   setOpaque(true);
			   setBackground(SystemColor.textHighlight);
			   setForeground(SystemColor.textHighlightText);
			}
			else
			{
				setOpaque(false);
				setBackground(SystemColor.text);
				setForeground(SystemColor.textText);
			}*/

			return this;
		}
	}

	private class XPluginInfoHeaderRenderer extends JLabel implements TableCellRenderer
	{
		public XPluginInfoHeaderRenderer()
		{
			setHorizontalAlignment( JLabel.CENTER );
			setHorizontalTextPosition( JLabel.LEFT );
		}

		public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
		{
			if( value!=null )
				setText( (String)value );
			else
				setText("");

    		if( getColumnModel().getColumn(column).getModelIndex()==sortedIndex )
				setIcon( sortAsc ? ascSortedIcon : descSortedIcon );
			else
				setIcon(null);

			if( isSelected )
			{
				super.setForeground(table.getSelectionForeground());
				super.setBackground(table.getSelectionBackground());
			}
			else
			{
				super.setForeground(table.getForeground());
				super.setBackground(table.getBackground());
			}

			setFont(table.getFont());

			if( hasFocus )
			{
				setBorder( UIManager.getBorder("Table.focusCellHighlightBorder") );

				if( table.isCellEditable(row, column) )
				{
					super.setForeground( UIManager.getColor("Table.focusCellForeground") );
					super.setBackground( UIManager.getColor("Table.focusCellBackground") );
				}
			}
			else
				setBorder(noFocusBorder);

		Color back = getBackground();
		boolean color_match = (back != null) && ( back.equals(table.getBackground()) ) && table.isOpaque();

			setOpaque(!color_match);

			return this;
		}

	// Attributes:
		private ImageIcon ascSortedIcon = XRepository.getComponentBuilder().buildImageIcon(XPluginInfoTable.this, "image.AscendingSorted");
		private ImageIcon descSortedIcon = XRepository.getComponentBuilder().buildImageIcon(XPluginInfoTable.this, "image.DescendingSorted");

	    private Border noFocusBorder = BorderFactory.createRaisedBevelBorder();
	}

	private class XPluginInfoTableModel extends AbstractTableModel implements XPluginListener, PropertyChangeListener
	{
		public XPluginInfoTableModel()
		{
			getTableHeader().addMouseListener( new MouseAdapter() {
				public void mouseClicked(MouseEvent e)
				{
				TableColumnModel colModel = getColumnModel();
				int columnModelIndex = colModel.getColumnIndexAtX(e.getX());
				int modelIndex = colModel.getColumn(columnModelIndex).getModelIndex();

					if( modelIndex<0 )
						return;

					if( sortedIndex==modelIndex )
						sortAsc = !sortAsc;
					else
						sortedIndex = modelIndex;

					getTableHeader().repaint();
					sort();
				}
			});

			columnNames = new String[] {
				XRepository.getResourceManager().getProperty(XPluginInfoTable.this, "Column.Status"),
				XRepository.getResourceManager().getProperty(XPluginInfoTable.this, "Column.Name"),
				XRepository.getResourceManager().getProperty(XPluginInfoTable.this, "Column.Version"),
				XRepository.getResourceManager().getProperty(XPluginInfoTable.this, "Column.Dock"),
				XRepository.getResourceManager().getProperty(XPluginInfoTable.this, "Column.AutoStart"),
				XRepository.getResourceManager().getProperty(XPluginInfoTable.this, "Column.ReleaseDate"),
				XRepository.getResourceManager().getProperty(XPluginInfoTable.this, "Column.AuthorName"),
			};
		}

		private void sort()
		{
			Collections.sort(data, new XPluginComparator(sortedIndex, sortAsc));
			tableChanged( new TableModelEvent(XPluginInfoTableModel.this) );
			repaint();
		}

		public Class getColumnClass(int c)
		{
			return getValueAt(0, c).getClass();
		}

		public int getColumnCount()
		{
			return columnNames.length;
		}

		public String getColumnName(int col)
		{
	    	return columnNames[col];
		}

		public int getRowCount()
		{
			return data.size();
		}

		public Object getValueAt(int row, int col)
		{
		XPluginObject plugin = (XPluginObject)data.get(row);

			switch( col )
			{
				case 0:
					return plugin.getStatusString();
				case 1:
					return plugin.getName();
				case 2:
					return plugin.getVersion();
				case 3:
					return plugin.getDockString();
				case 4:
					return plugin.getAutoStartString();
				case 5:
					return plugin.getReleaseDate();
				case 6:
					return plugin.getAuthorName();
			}

			return null;
		}

		public void propertyChange(PropertyChangeEvent evt)
		{
		int index = data.indexOf(evt.getSource());

			if( sortedIndex<0 )
				fireTableRowsUpdated(index, index);
			else
				sort();
		}

		public void pluginAdded(XPluginObject plugin)
		{
			data.add(0, plugin);

			if( sortedIndex<0 )
				fireTableRowsInserted(0, 0);
			else
				sort();

			plugin.addPropertyChangeListener(this);
		}

		public void pluginRemoved(XPluginObject plugin)
		{
		int index = data.indexOf(plugin);

			if( index!=-1 )
			{
				data.remove(index);
				fireTableRowsDeleted(index, index);
				plugin.removePropertyChangeListener(this);

				updateActions();
			}
		}

		public XPluginObject getPluginInfo(int row_index)
		{
			return (XPluginObject) data.get(row_index);
		}

	// Attributes:
		private String[] columnNames;
		private LinkedList data = new LinkedList();
	}

    private class UninstallPluginAction extends XDefaultAction
    {
        public UninstallPluginAction()
        {
            super(XPluginInfoTable.this, "UninstallPlugin" ,null);
        }

        public void actionPerformed(ActionEvent e)
        {
		int sel_rows[] = getSelectedRows();
		LinkedList list = new LinkedList();

			for( int i=0; i<sel_rows.length; i++ )
				list.add( model.getPluginInfo(sel_rows[i]) );

		Iterator it = list.iterator();

			while( it.hasNext() )
				XRepository.getPluginManager().removePlugin( (XPluginObject)it.next() );
        }
    }

    private class StartPluginAction extends XDefaultAction
    {
        public StartPluginAction()
        {
            super(XPluginInfoTable.this, "StartPlugin" ,null);
        }

        public void actionPerformed(ActionEvent e)
        {
		int sel_rows[] = getSelectedRows();

			for( int i=0; i<sel_rows.length; i++ )
				model.getPluginInfo(sel_rows[i]).start();

			updateActions();
        }
    }

    private class StopPluginAction extends XDefaultAction
    {
        public StopPluginAction()
        {
            super(XPluginInfoTable.this, "StopPlugin" ,null);
        }

        public void actionPerformed(ActionEvent e)
        {
		int sel_rows[] = getSelectedRows();

			for( int i=0; i<sel_rows.length; i++ )
				model.getPluginInfo(sel_rows[i]).stop();

			updateActions();
        }
    }

    private class PluginInfoAction extends XDefaultAction
    {
        public PluginInfoAction()
        {
            super(XPluginInfoTable.this, "PluginInfo" ,null);
        }

        public void actionPerformed(ActionEvent e)
        {
			displayPluginInfo( model.getPluginInfo(getSelectedRow()) );
        }
    }

// Attributes:
	private XPluginInfoTableModel model = new XPluginInfoTableModel();
	private JPopupMenu pluginInfoPopup = new XPopupMenu();
	private final int[]	columnWidths = { 70, 80, 50, 60, 70, 100, 120 };

	private int sortedIndex = -1;
	private boolean sortAsc = true;

	public final XAction startPluginAction = new StartPluginAction();
	public final XAction stopPluginAction = new StopPluginAction();
	public final XAction uninstallPluginAction = new UninstallPluginAction();
	public final XAction pluginInfoAction = new PluginInfoAction();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -