📄 ktableactionhandler.java
字号:
/*
* Copyright (C) 2004 by Friederich Kupzog Elektronik & Software
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package de.kupzog.ktable;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Vector;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.HTMLTransfer;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.actions.ActionFactory;
/**
*
* @author Lorenz Maierhofer <lorenz.maierhofer@logicmindguide.com>
*/
public class KTableActionHandler {
static final char TAB = '\t';
static final String PlatformLineDelimiter = System.getProperty("line.separator");
public KTableCopyAction m_CopyAction;
public KTableCopyAllAction m_CopyAllAction;
public KTableCutAction m_CutAction;
public KTablePasteAction m_PasteAction;
public KTableSelectAllAction m_SelectAllAction;
protected KTable m_table;
protected MenuManager m_contextMenuManager;
/**
*
*/
public KTableActionHandler(KTable table) {
m_table = table;
createActions();
registerActionUpdater();
// add actions to context menu:
m_contextMenuManager = new MenuManager("#PopupMenu");
m_contextMenuManager.setRemoveAllWhenShown(true);
m_contextMenuManager.addMenuListener(new IMenuListener() {
public void menuAboutToShow(IMenuManager manager) {
fillContextMenu(manager);
}
});
Menu menu = m_contextMenuManager.createContextMenu(m_table);
m_table.setMenu(menu);
}
/**
* @return Returns the menu manager used to build the context menu
* of the table.<p>
* The purpose for this is normally the registering of context
* menus in the workbench.
* @see org.eclipse.ui.IWorkbenchPartSite#registerContextMenu(org.eclipse.jface.action.MenuManager, org.eclipse.jface.viewers.ISelectionProvider)
*/
public MenuManager getMenuManager() {
return m_contextMenuManager;
}
protected void createActions() {
m_CopyAction = new KTableCopyAction();
m_CopyAllAction = new KTableCopyAllAction();
m_PasteAction = new KTablePasteAction();
m_CutAction = new KTableCutAction();
m_SelectAllAction = new KTableSelectAllAction();
}
protected void fillContextMenu(IMenuManager menumanager) {
menumanager.add(m_CopyAction);
menumanager.add(m_CutAction);
menumanager.add(m_PasteAction);
menumanager.add(new Separator());
menumanager.add(m_CopyAllAction);
menumanager.add(m_SelectAllAction);
menumanager.add(new Separator());
// Other plug-ins can contribute their actions here
menumanager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
}
/**
* Registers the cut, copy, paste and select_all actions for global use at the IActionBar given.<p>
* Currently does not set up the UNDO and REDO actions because they will be implemented in another way.
* @param actionBar The IActionBars that allows global action registration. Normally
* you can get that with getViewerSite().getActionBars() or getEditorSite().getActionBars().
*/
public void registerGlobalActions(IActionBars actionBar) {
actionBar.setGlobalActionHandler(ActionFactory.CUT.getId(), this.m_CutAction);
actionBar.setGlobalActionHandler(ActionFactory.COPY.getId(), this.m_CopyAction);
actionBar.setGlobalActionHandler(ActionFactory.PASTE.getId(),this.m_PasteAction);
actionBar.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(), this.m_SelectAllAction);
actionBar.updateActionBars();
}
protected class KTableCopyAction extends Action {
protected KTableCopyAction() {
setId("KTableCopyActionHandler");//$NON-NLS-1$
setEnabled(false);
setText("Kopieren");
}
public void run() {
if (m_table != null && !m_table.isDisposed()) {
setClipboardContent(m_table.getCellSelection());
}
}
public void updateEnabledState() {
if (m_table != null && !m_table.isDisposed()) {
Point[] selection = m_table.getCellSelection();
setEnabled(selection!=null && selection.length>0);
} else setEnabled(false);
}
}
protected class KTableCopyAllAction extends Action {
protected KTableCopyAllAction() {
setId("KTableCopyAllActionHandler");//$NON-NLS-1$
setEnabled(false);
setText("Ganze Tabelle kopieren");
}
public void run() {
if (m_table != null && !m_table.isDisposed()) {
setClipboardContent(getAllTableCells());
}
}
public void updateEnabledState() {
if (m_table != null && !m_table.isDisposed()) {
setEnabled(true);
} else setEnabled(false);
}
private Point[] getAllTableCells() {
KTableModel model = m_table.getModel();
if (model==null) return new Point[]{};
Vector cells = new Vector(model.getColumnCount()*model.getRowCount());
for (int row=0; row<model.getRowCount(); row++) {
for (int col=0; col<model.getColumnCount(); col++) {
Point valid = model.belongsToCell(col, row);
if (valid.y==row && valid.x==col)
cells.add(valid);
}
}
return (Point[])cells.toArray(new Point[]{});
}
}
protected class KTableCutAction extends Action {
protected KTableCutAction() {
setId("KTableCutActionHandler");//$NON-NLS-1$
setEnabled(false);
setText("Ausschneiden");
}
public void run() {
if (m_table != null && !m_table.isDisposed()) {
Point[] selection = m_table.getCellSelection();
setClipboardContent(selection);
removeContentAt(selection);
}
}
public void updateEnabledState() {
if (m_table != null && !m_table.isDisposed()) {
Point[] selection = m_table.getCellSelection();
setEnabled(selection!=null && selection.length>0);
} else setEnabled(false);
}
protected void removeContentAt(Point[] selection) {
KTableModel model = m_table.getModel();
if (model==null) return;
boolean updateSeperateCells = selection.length>4?false:true;
try {
if (!updateSeperateCells)
m_table.setRedraw(false);
for (int i=0; i<selection.length; i++) {
model.setContentAt(selection[i].x, selection[i].y, "");
if (updateSeperateCells)
m_table.updateCell(selection[i].x, selection[i].y);
}
} finally {
if (!updateSeperateCells)
m_table.setRedraw(true);
}
}
}
protected class KTableSelectAllAction extends Action {
protected KTableSelectAllAction() {
setId("KTableSelectAllActionHandler");//$NON-NLS-1$
setEnabled(false);
setText("Alles Markieren");
}
public void run() {
if (m_table != null && !m_table.isDisposed()) {
KTableModel model = m_table.getModel();
if (model!=null)
selectAll(model);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -