PageRenderTime 54ms CodeModel.GetById 38ms app.highlight 7ms RepoModel.GetById 7ms app.codeStats 1ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/gui/ListModelEditor.java

#
Java | 108 lines | 100 code | 8 blank | 0 comment | 17 complexity | 5e3bab127aa9bb64d0621a4d3a2351a6 MD5 | raw file
  1package org.gjt.sp.jedit.gui;
  2
  3import java.awt.event.KeyAdapter;
  4import java.awt.event.KeyEvent;
  5
  6import javax.swing.DefaultListModel;
  7import javax.swing.JOptionPane;
  8import javax.swing.JTable;
  9import javax.swing.ListSelectionModel;
 10import javax.swing.table.DefaultTableModel;
 11
 12import org.gjt.sp.jedit.jEdit;
 13
 14public class ListModelEditor
 15{
 16
 17	public void open(DefaultListModel listModel)
 18	{
 19		final DefaultTableModel tableModel = createTableModel(listModel);
 20		final JTable table = new JTable(tableModel);
 21		table
 22		    .setToolTipText("Move: PgUp/PgDown; Edit: Double-Click or Insert/Delete");
 23
 24		table.addKeyListener(new KeyAdapter()
 25		{
 26			public void keyPressed(KeyEvent e)
 27			{
 28				int[] selRows = table.getSelectedRows();
 29				if (selRows.length == 0)
 30				{
 31					return;
 32				}
 33				int firstSelectedRow = selRows[0];
 34				int key = e.getKeyCode();
 35				ListSelectionModel selectionModel = table.getSelectionModel();
 36				switch (key)
 37				{
 38					case KeyEvent.VK_DELETE:
 39						for (int i = selRows.length - 1; i >= 0; i--)
 40						{
 41							tableModel.removeRow(selRows[i]);
 42						}
 43						if (firstSelectedRow >= 0
 44						    && firstSelectedRow < tableModel.getRowCount())
 45						{
 46							selectionModel.addSelectionInterval(firstSelectedRow,
 47							    firstSelectedRow);
 48						}
 49						e.consume();// avoid beep
 50						break;
 51					case KeyEvent.VK_INSERT:
 52						tableModel.insertRow(firstSelectedRow + 1, new String[] { "" });
 53						e.consume(); // Dont edit cell
 54						break;
 55					case KeyEvent.VK_PAGE_UP:
 56					case KeyEvent.VK_PAGE_DOWN:
 57						boolean isUp = key == KeyEvent.VK_PAGE_UP;
 58						int direction = isUp ? -1 : 1;
 59						int min = selectionModel.getMinSelectionIndex() + direction;
 60						int max = selectionModel.getMaxSelectionIndex() + direction;
 61						if (min < 0 || max >= tableModel.getRowCount())
 62						{
 63							return; // avoid ArrayIndexOutOfBoundsException
 64						}
 65						for (int i = 0; i < selRows.length; i++)
 66						{
 67							int row = selRows[isUp ? i : (selRows.length - 1 - i)];
 68							int to = row + direction;
 69							selectionModel.removeSelectionInterval(row, row);
 70							selectionModel.addSelectionInterval(to, to);
 71							tableModel.moveRow(row, row, to);
 72						}
 73						break;
 74				}
 75			}
 76		});
 77		int result = JOptionPane.showConfirmDialog(null, table, "Change "
 78		    + jEdit.getProperty("history.caption"), JOptionPane.OK_CANCEL_OPTION);
 79		if (result == JOptionPane.OK_OPTION)
 80		{
 81			updatelistModel(listModel, tableModel);
 82		}
 83	}
 84
 85	private DefaultTableModel createTableModel(DefaultListModel listModel)
 86	{
 87		Object[][] tableRowData = new String[listModel.size()][1];
 88		for (int i = 0; i < listModel.size(); i++)
 89		{
 90			tableRowData[i][0] = listModel.get(i);
 91		}
 92		return new DefaultTableModel(tableRowData, new String[] { "" });
 93	}
 94
 95	private void updatelistModel(DefaultListModel listModel,
 96	    DefaultTableModel tableModel)
 97	{
 98		listModel.removeAllElements();
 99		for (int i = 0; i < tableModel.getRowCount(); i++)
100		{
101			Object cellText = tableModel.getValueAt(i, 0);
102			if (cellText != null && !cellText.equals(""))
103			{
104				listModel.addElement(cellText);
105			}
106		}
107	}
108}