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

📄 abbrevsoptionpane.java

📁 用java 编写的源码开放的文本编辑器。有很多有用的特性
💻 JAVA
字号:
/* * AbbrevsOptionPane.java - Abbrevs options panel * :tabSize=8:indentSize=8:noTabs=false: * :folding=explicit:collapseFolds=1: * * Copyright (C) 1999, 2000, 2001, 2002 Slava Pestov * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. */package org.gjt.sp.jedit.options;//{{{ Importsimport javax.swing.border.EmptyBorder;import javax.swing.event.*;import javax.swing.table.*;import javax.swing.*;import java.awt.event.*;import java.awt.*;import java.util.*;import org.gjt.sp.jedit.gui.*;import org.gjt.sp.jedit.*;//}}}//{{{ AbbrevsOptionPane class/** * Abbrev editor. * @author Slava Pestov * @version $Id: AbbrevsOptionPane.java,v 1.9 2002/12/15 00:23:53 spestov Exp $ */public class AbbrevsOptionPane extends AbstractOptionPane{	//{{{ AbbrevsOptionPane constructor	public AbbrevsOptionPane()	{		super("abbrevs");	} //}}}	//{{{ _init() method	protected void _init()	{		setLayout(new BorderLayout());		JPanel panel = new JPanel(new BorderLayout(6,6));		expandOnInput = new JCheckBox(jEdit.getProperty("options.abbrevs"			+ ".expandOnInput"),Abbrevs.getExpandOnInput());		panel.add(expandOnInput,BorderLayout.NORTH);		JPanel panel2 = new JPanel();		panel2.setLayout(new BoxLayout(panel2,BoxLayout.X_AXIS));		panel2.setBorder(new EmptyBorder(0,0,6,0));		panel2.add(Box.createGlue());		JLabel label = new JLabel(jEdit.getProperty("options.abbrevs.set"));		label.setBorder(new EmptyBorder(0,0,0,12));		panel2.add(label);		Hashtable _modeAbbrevs = Abbrevs.getModeAbbrevs();		modeAbbrevs = new Hashtable();		Mode[] modes = jEdit.getModes();		String[] sets = new String[modes.length + 1];		sets[0] = "global";		for(int i = 0; i < modes.length; i++)		{			String name = modes[i].getName();			sets[i+1] = name;			modeAbbrevs.put(name,new AbbrevsModel((Hashtable)_modeAbbrevs.get(name)));		}		setsComboBox = new JComboBox(sets);		ActionHandler actionHandler = new ActionHandler();		setsComboBox.addActionListener(actionHandler);		panel2.add(setsComboBox);		panel2.add(Box.createGlue());		panel.add(panel2,BorderLayout.SOUTH);		add(BorderLayout.NORTH,panel);		globalAbbrevs = new AbbrevsModel(Abbrevs.getGlobalAbbrevs());		abbrevsTable = new JTable(globalAbbrevs);		abbrevsTable.getColumnModel().getColumn(1).setCellRenderer(			new Renderer());		abbrevsTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);		abbrevsTable.getTableHeader().setReorderingAllowed(false);		abbrevsTable.getTableHeader().addMouseListener(new HeaderMouseHandler());		abbrevsTable.getSelectionModel().addListSelectionListener(			new SelectionHandler());		abbrevsTable.addMouseListener(new TableMouseHandler());		Dimension d = abbrevsTable.getPreferredSize();		d.height = Math.min(d.height,200);		JScrollPane scroller = new JScrollPane(abbrevsTable);		scroller.setPreferredSize(d);		add(BorderLayout.CENTER,scroller);		JPanel buttons = new JPanel();		buttons.setLayout(new BoxLayout(buttons,BoxLayout.X_AXIS));		buttons.setBorder(new EmptyBorder(6,0,0,0));		add = new RolloverButton(GUIUtilities.loadIcon("Plus.png"));		add.setToolTipText(jEdit.getProperty("options.abbrevs.add"));		add.addActionListener(actionHandler);		buttons.add(add);		remove = new RolloverButton(GUIUtilities.loadIcon("Minus.png"));		remove.setToolTipText(jEdit.getProperty("options.abbrevs.remove"));		remove.addActionListener(actionHandler);		buttons.add(remove);		edit = new RolloverButton(GUIUtilities.loadIcon("ButtonProperties.png"));		edit.setToolTipText(jEdit.getProperty("options.abbrevs.edit"));		edit.addActionListener(actionHandler);		buttons.add(edit);		buttons.add(Box.createGlue());		add(BorderLayout.SOUTH,buttons);		updateEnabled();	} //}}}	//{{{ _save() method	protected void _save()	{		if(abbrevsTable.getCellEditor() != null)			abbrevsTable.getCellEditor().stopCellEditing();		Abbrevs.setExpandOnInput(expandOnInput.isSelected());		Abbrevs.setGlobalAbbrevs(globalAbbrevs.toHashtable());		Hashtable modeHash = new Hashtable();		Enumeration keys = modeAbbrevs.keys();		Enumeration values = modeAbbrevs.elements();		while(keys.hasMoreElements())		{			modeHash.put(keys.nextElement(),((AbbrevsModel)values.nextElement())				.toHashtable());		}		Abbrevs.setModeAbbrevs(modeHash);	} //}}}	//{{{ Private members	//{{{ Instance variables	private JComboBox setsComboBox;	private JCheckBox expandOnInput;	private JTable abbrevsTable;	private AbbrevsModel globalAbbrevs;	private Hashtable modeAbbrevs;	private JButton add;	private JButton edit;	private JButton remove;	//}}}	//{{{ updateEnabled() method	private void updateEnabled()	{		int selectedRow = abbrevsTable.getSelectedRow();		edit.setEnabled(selectedRow != -1);		remove.setEnabled(selectedRow != -1);	} //}}}	//{{{ edit() method	private void edit()	{		AbbrevsModel abbrevsModel = (AbbrevsModel)abbrevsTable.getModel();		int row = abbrevsTable.getSelectedRow();		String abbrev = (String)abbrevsModel.getValueAt(row,0);		String expansion = (String)abbrevsModel.getValueAt(row,1);		EditAbbrevDialog dialog = new EditAbbrevDialog(			GUIUtilities.getParentDialog(AbbrevsOptionPane.this),			abbrev,expansion);		abbrev = dialog.getAbbrev();		expansion = dialog.getExpansion();		if(abbrev != null && expansion != null)		{			abbrevsModel.setValueAt(abbrev,row,0);			abbrevsModel.setValueAt(expansion,row,1);		}	} //}}}	//}}}	//{{{ HeaderMouseHandler class	class HeaderMouseHandler extends MouseAdapter	{		public void mouseClicked(MouseEvent evt)		{			switch(abbrevsTable.getTableHeader().columnAtPoint(evt.getPoint()))			{			case 0:				((AbbrevsModel)abbrevsTable.getModel()).sort(0);				break;			case 1:				((AbbrevsModel)abbrevsTable.getModel()).sort(1);				break;			}		}	} //}}}	//{{{ TableMouseHandler class	class TableMouseHandler extends MouseAdapter	{		public void mouseClicked(MouseEvent evt)		{			if(evt.getClickCount() == 2)				edit();		}	} //}}}	//{{{ SelectionHandler class	class SelectionHandler implements ListSelectionListener	{		public void valueChanged(ListSelectionEvent evt)		{			updateEnabled();		}	} //}}}	//{{{ ActionHandler class	class ActionHandler implements ActionListener	{		public void actionPerformed(ActionEvent evt)		{			AbbrevsModel abbrevsModel = (AbbrevsModel)abbrevsTable.getModel();			Object source = evt.getSource();			if(source == setsComboBox)			{				String selected = (String)setsComboBox.getSelectedItem();				if(selected.equals("global"))				{					abbrevsTable.setModel(globalAbbrevs);				}				else				{					abbrevsTable.setModel((AbbrevsModel)						modeAbbrevs.get(selected));				}				updateEnabled();			}			else if(source == add)			{				EditAbbrevDialog dialog = new EditAbbrevDialog(					GUIUtilities.getParentDialog(AbbrevsOptionPane.this),					null,null);				String abbrev = dialog.getAbbrev();				String expansion = dialog.getExpansion();				if(abbrev != null && abbrev.length() != 0					&& expansion != null					&& expansion.length() != 0)				{					abbrevsModel.add(abbrev,expansion);					int index = abbrevsModel.getRowCount() - 1;					abbrevsTable.getSelectionModel()						.setSelectionInterval(index,index);					Rectangle rect = abbrevsTable.getCellRect(						index,0,true);					abbrevsTable.scrollRectToVisible(rect);					updateEnabled();				}			}			else if(source == edit)			{				edit();			}			else if(source == remove)			{				int selectedRow = abbrevsTable.getSelectedRow();				abbrevsModel.remove(selectedRow);				updateEnabled();			}		}	} //}}}	//{{{ Renderer class	static class Renderer extends DefaultTableCellRenderer	{		public Component getTableCellRendererComponent(			JTable table,			Object value,			boolean isSelected,			boolean cellHasFocus,			int row,			int col)		{			String valueStr = value.toString();			// workaround for Swing's annoying processing of			// labels starting with <html>, which often breaks			if(valueStr.toLowerCase().startsWith("<html>"))				valueStr = " " + valueStr;			return super.getTableCellRendererComponent(table,valueStr,				isSelected,cellHasFocus,row,col);		}	} //}}}} //}}}//{{{ AbbrevsModel classclass AbbrevsModel extends AbstractTableModel{	Vector abbrevs;	//{{{ AbbrevsModel constructor	AbbrevsModel()	{		abbrevs = new Vector();	} //}}}	//{{{ AbbrevsModel constructor	AbbrevsModel(Hashtable abbrevHash)	{		this();		if(abbrevHash != null)		{			Enumeration abbrevEnum = abbrevHash.keys();			Enumeration expandEnum = abbrevHash.elements();			while(abbrevEnum.hasMoreElements())			{				abbrevs.addElement(new Abbrev((String)abbrevEnum.nextElement(),					(String)expandEnum.nextElement()));			}			sort(0);		}	} //}}}	//{{{ sort() method	void sort(int col)	{		MiscUtilities.quicksort(abbrevs,new AbbrevCompare(col));		fireTableDataChanged();	} //}}}	//{{{ add() method	void add(String abbrev, String expansion)	{		abbrevs.addElement(new Abbrev(abbrev,expansion));		fireTableStructureChanged();	} //}}}	//{{{ remove() method	void remove(int index)	{		abbrevs.removeElementAt(index);		fireTableStructureChanged();	} //}}}	//{{{ toHashtable() method	public Hashtable toHashtable()	{		Hashtable hash = new Hashtable();		for(int i = 0; i < abbrevs.size(); i++)		{			Abbrev abbrev = (Abbrev)abbrevs.elementAt(i);			if(abbrev.abbrev.length() > 0				&& abbrev.expand.length() > 0)			{				hash.put(abbrev.abbrev,abbrev.expand);			}		}		return hash;	} //}}}	//{{{ getColumnCount() method	public int getColumnCount()	{		return 2;	} //}}}	//{{{ getRowCount() method	public int getRowCount()	{		return abbrevs.size();	} //}}}	//{{{ getValueAt() method	public Object getValueAt(int row, int col)	{		Abbrev abbrev = (Abbrev)abbrevs.elementAt(row);		switch(col)		{		case 0:			return abbrev.abbrev;		case 1:			return abbrev.expand;		default:			return null;		}	} //}}}	//{{{ isCellEditable() method	public boolean isCellEditable(int row, int col)	{		return false;	} //}}}	//{{{ setValueAt() method	public void setValueAt(Object value, int row, int col)	{		if(value == null)			value = "";		Abbrev abbrev = (Abbrev)abbrevs.elementAt(row);		if(col == 0)			abbrev.abbrev = (String)value;		else			abbrev.expand = (String)value;		fireTableRowsUpdated(row,row);	} //}}}	//{{{ getColumnName() method	public String getColumnName(int index)	{		switch(index)		{		case 0:			return jEdit.getProperty("options.abbrevs.abbrev");		case 1:			return jEdit.getProperty("options.abbrevs.expand");		default:			return null;		}	} //}}}	//{{{ AbbrevCompare class	class AbbrevCompare implements MiscUtilities.Compare	{		int col;		AbbrevCompare(int col)		{			this.col = col;		}		public int compare(Object obj1, Object obj2)		{			Abbrev a1 = (Abbrev)obj1;			Abbrev a2 = (Abbrev)obj2;			if(col == 0)			{				String abbrev1 = a1.abbrev.toLowerCase();				String abbrev2 = a2.abbrev.toLowerCase();				return MiscUtilities.compareStrings(					abbrev1,abbrev2,true);			}			else			{				String expand1 = a1.expand.toLowerCase();				String expand2 = a2.expand.toLowerCase();				return MiscUtilities.compareStrings(					expand1,expand2,true);			}		}	} //}}}} //}}}//{{{ Abbrev classclass Abbrev{	Abbrev() {}	Abbrev(String abbrev, String expand)	{		this.abbrev = abbrev;		this.expand = expand;	}	String abbrev;	String expand;} //}}}

⌨️ 快捷键说明

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