baseundomanager.java
来自「一个eclipse插件源代码。用于web开发」· Java 代码 · 共 355 行
JAVA
355 行
/*
* $Header: /home/cvs/WEBPUMP2.0/WebPumpIDE_Src/WebPumpIDE/src/com/webpump/ui/base/gui/BaseUndoManager.java,v 1.1.1.1 2004/07/01 09:07:40 wang_j Exp $
* $Revision: 1.1.1.1 $
* $Date: 2004/07/01 09:07:40 $
*
* ====================================================================
*
* The NanJing HopeRun(IT-FOREST) Software License, Version 2.0.0
*
* Copyright 2003-2004 by NanJing HopeRun(IT-FOREST) Information System Co., Ltd, CHINA and
* IT Forest Corporation
* All rights reserved.
*
* This software is the confidential and proprietary information of
* HopeRun(IT-FOREST) Information System Co., Ltd, CHINA and IT Forest Corporation.
* You shall not disclose such Confidential Information and shall use it only in
* accordance with the terms of the license agreement you entered into with
* HopeRun(IT-FOREST) Information System Co., Ltd, CHINA and IT Forest Corporation.
*/
package com.webpump.ui.base.gui;
import java.util.List;
import java.util.Vector;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.action.IAction;
import org.eclipse.pde.core.*;
import org.eclipse.pde.internal.core.plugin.*;
import org.eclipse.pde.internal.ui.PDEPlugin;
import org.eclipse.pde.internal.ui.editor.*;
import com.webpump.ui.base.data.BaseDataObject;
import com.webpump.ui.base.data.BaseModel;
import com.webpump.ui.base.data.BaseObject;
import com.webpump.ui.perspective.WebpumpIDEPlugin;
/**
* @version 1.0
* @author
*/
public class BaseUndoManager implements IModelUndoManager, IModelChangedListener {
BaseModel model;
BaseEditor m_objEditor;
private static final String KEY_NO_UNDO = "UpdateManager.noUndo";
private static final String KEY_NO_REDO = "UpdateManager.noRedo";
private static final String KEY_UNDO = "UpdateManager.undo";
private static final String KEY_REDO = "UpdateManager.redo";
private static final String KEY_OP_ADD = "UpdateManager.op.add";
private static final String KEY_OP_REMOVE = "UpdateManager.op.remove";
private static final String KEY_OP_CHANGE = "UpdateManager.op.change";
private boolean ignoreChanges;
protected List operations;
private int undoLevelLimit = 10;
private int cursor = -1;
private IAction undoAction;
private IAction redoAction;
public BaseUndoManager(PDEMultiPageEditor editor) {
this.m_objEditor = (BaseEditor) editor;
}
/*
* @see IModelUndoManager#disconnect(IModelChangeProvider)
*/
public void disconnect(IModelChangeProvider provider) {
provider.removeModelChangedListener(this);
}
private void initialize() {
operations = new Vector();
cursor = -1;
updateActions();
}
/*
* @see IModelUndoManager#isUndoable()
*/
public boolean isUndoable() {
return cursor >= 0;
}
/*
* @see IModelUndoManager#isRedoable()
*/
public boolean isRedoable() {
return (cursor + 1) < operations.size();
}
/*
* @see IModelUndoManager#undo()
*/
public void undo() {
IModelChangedEvent op = getCurrentOperation();
if (op == null)
return;
ignoreChanges = true;
openRelatedPage(op);
execute(op, true);
cursor--;
updateActions();
ignoreChanges = false;
}
/*
* @see IModelUndoManager#redo()
*/
public void redo() {
cursor++;
IModelChangedEvent op = getCurrentOperation();
if (op == null)
return;
ignoreChanges = true;
openRelatedPage(op);
execute(op, false);
ignoreChanges = false;
updateActions();
}
private void openRelatedPage(IModelChangedEvent op) {
Object obj = op.getChangedObjects()[0];
String pageId = getPageId(obj);
if (pageId != null) {
IPDEEditorPage cpage = m_objEditor.getCurrentPage();
IPDEEditorPage newPage = m_objEditor.getPage(pageId);
if (cpage != newPage)
m_objEditor.showPage(newPage);
}
}
/*
* @see IModelChangedListener#modelChanged(IModelChangedEvent)
*/
public void modelChanged(IModelChangedEvent event) {
if (event.getChangeType() == IModelChangedEvent.CHANGE) {
if(event.getChangedProperty() == "position") {
return;
}
BaseObject obj = (BaseObject) event.getChangedObjects()[0];
//Ignore events from objects that are not yet in the model.
if (!(obj instanceof BaseObject) && obj.isInTheModel() == false)
return;
}
if (ignoreChanges)
return;
if (event.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
initialize();
return;
}
addOperation(event);
}
private IModelChangedEvent getCurrentOperation() {
if (cursor == -1 || cursor == operations.size())
return null;
return (IModelChangedEvent) operations.get(cursor);
}
private IModelChangedEvent getNextOperation() {
int peekCursor = cursor + 1;
if (peekCursor >= operations.size())
return null;
return (IModelChangedEvent) operations.get(peekCursor);
}
private void addOperation(IModelChangedEvent operation) {
operations.add(operation);
int size = operations.size();
if (size > undoLevelLimit) {
int extra = size - undoLevelLimit;
// trim
for (int i = 0; i < extra; i++) {
operations.remove(i);
}
}
cursor = operations.size() - 1;
updateActions();
}
public void setActions(IAction undoAction, IAction redoAction) {
this.undoAction = undoAction;
this.redoAction = redoAction;
updateActions();
}
private void updateActions() {
if (undoAction != null && redoAction != null) {
undoAction.setEnabled(isUndoable());
undoAction.setText(getUndoText());
redoAction.setEnabled(isRedoable());
redoAction.setText(getRedoText());
}
}
private String getUndoText() {
IModelChangedEvent op = getCurrentOperation();
if (op == null) {
return PDEPlugin.getResourceString(KEY_NO_UNDO);
} else {
String opText = getOperationText(op);
return PDEPlugin.getFormattedMessage(KEY_UNDO, opText);
}
}
private String getRedoText() {
IModelChangedEvent op = getNextOperation();
if (op == null) {
return PDEPlugin.getResourceString(KEY_NO_REDO);
} else {
String opText = getOperationText(op);
return PDEPlugin.getFormattedMessage(KEY_REDO, opText);
}
}
private String getOperationText(IModelChangedEvent op) {
String opText = "";
switch (op.getChangeType()) {
case IModelChangedEvent.INSERT :
opText = PDEPlugin.getResourceString(KEY_OP_ADD);
break;
case IModelChangedEvent.REMOVE :
opText = PDEPlugin.getResourceString(KEY_OP_REMOVE);
break;
case IModelChangedEvent.CHANGE :
opText = PDEPlugin.getResourceString(KEY_OP_CHANGE);
break;
}
return opText;
}
public void setUndoLevelLimit(int limit) {
this.undoLevelLimit = limit;
}
public void setIgnoreChanges(boolean ignore) {
this.ignoreChanges = ignore;
}
public void connect(IModelChangeProvider provider) {
model = (BaseModel) provider;
provider.addModelChangedListener(this);
if (operations == null)
initialize();
}
protected String getPageId(Object obj) {
/*if (obj instanceof IPluginBase)
return ManifestEditor.OVERVIEW_PAGE;
if (obj instanceof IPluginImport)
return ManifestEditor.DEPENDENCIES_PAGE;
if (obj instanceof IPluginLibrary)
return ManifestEditor.RUNTIME_PAGE;
if (obj instanceof IPluginExtension ||
obj instanceof IPluginElement ||
obj instanceof IPluginAttribute)
return ManifestEditor.EXTENSIONS_PAGE;
if (obj instanceof IPluginExtensionPoint)
return ManifestEditor.EXTENSION_POINT_PAGE;*/
return m_objEditor.getHomePage().getLabel();
// return null;
}
protected void execute(IModelChangedEvent event, boolean undo) {
Object[] elements = event.getChangedObjects();
int type = event.getChangeType();
String propertyName = event.getChangedProperty();
switch (type) {
case IModelChangedEvent.INSERT :
if (undo)
executeRemove(elements);
else
executeAdd(elements);
break;
case IModelChangedEvent.REMOVE :
if (undo) {
executeAdd(elements);
operations.remove(operations.size() - 1);
}
else
executeRemove(elements);
break;
case IModelChangedEvent.CHANGE :
if (event instanceof AttributeChangedEvent) {
executeAttributeChange((AttributeChangedEvent) event, undo);
} else {
if (undo)
executeChange(
elements[0],
propertyName,
event.getNewValue(),
event.getOldValue());
else
executeChange(
elements[0],
propertyName,
event.getOldValue(),
event.getNewValue());
}
}
}
private void executeAdd(Object[] elements) {
for (int i = 0; i < elements.length; i++) {
Object element = elements[i];
if (element instanceof BaseObject) {
BaseDataObject objParent = (BaseDataObject) ((BaseObject) element).getParent();
objParent.add(element);
}
}
}
private void executeRemove(Object[] elements) {
if (elements instanceof BaseObject[]) {
BaseDataObject objParent = (BaseDataObject) ((BaseObject) elements[0]).getParent();
objParent.remove(elements);
}
}
protected void executeAttributeChange(AttributeChangedEvent e, boolean undo) {
PluginElement element = (PluginElement) e.getChangedObjects()[0];
PluginAttribute att = (PluginAttribute) e.getChagedAttribute();
Object oldValue = e.getOldValue();
Object newValue = e.getNewValue();
try {
if (undo)
element.setAttribute(att.getName(), oldValue.toString());
else
element.setAttribute(att.getName(), newValue.toString());
} catch (CoreException ex) {
PDEPlugin.logException(ex);
}
}
protected void executeChange(
Object element,
String propertyName,
Object oldValue,
Object newValue) {
if (element instanceof BaseObject) {
BaseObject pobj = (BaseObject) element;
try {
pobj.restoreProperty(propertyName, oldValue, newValue);
} catch (CoreException e) {
WebpumpIDEPlugin.log(e);
}
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?