下面列出了怎么用javax.swing.event.UndoableEditListener的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Register the tab to the Petri net
* @param net Petri net
* @param tab tab which houses the graphical petri net components
* @param historyObserver listener for stepback/forward events in animation
* @param undoListener listener for undo/redo events
* @param zoomListener listener for zoom events
*/
//TODO: THIS IS RATHER UGLY, too many params but better than what was here before
public void registerTab(PetriNet net, PetriNetTab tab, Observer historyObserver, UndoableEditListener undoListener,
PropertyChangeListener zoomListener) {
AnimationHistoryImpl animationHistory = new AnimationHistoryImpl();
animationHistory.addObserver(historyObserver);
GUIAnimator animator = new GUIAnimator(new PetriNetAnimator(net), animationHistory, this);
CopyPasteManager copyPasteManager = new CopyPasteManager(undoListener, tab, net, this);
ZoomController zoomController = new ZoomController(100);
tab.addZoomListener(zoomController);
PetriNetController petriNetController =
new PetriNetController(net, undoListener, animator, copyPasteManager, zoomController, tab);
netControllers.put(tab, petriNetController);
tab.updatePreferredSize();
PropertyChangeListener changeListener =
new PetriNetChangeListener(applicationModel, tab, petriNetController);
net.addPropertyChangeListener(changeListener);
setActiveTab(tab);
initialiseNet(net, changeListener);
}
public static void installTextComponent(final JTextComponent component) {
//Make sure this component does not already have a UndoManager
Document document = component.getDocument();
boolean found = false;
if (document instanceof AbstractDocument) {
AbstractDocument abstractDocument = (AbstractDocument) document;
for (UndoableEditListener editListener : abstractDocument.getUndoableEditListeners()) {
if (editListener.getClass().equals(CompoundUndoManager.class)) {
CompoundUndoManager undoManager = (CompoundUndoManager) editListener;
undoManager.reset();
return;
}
}
}
if (!found) {
new TextManager(component);
}
}
/**
* Creates actions for editing the petri net
*
* @param controller PIPE application controller
*/
public ComponentEditorManager(PipeApplicationController controller) {
copyAction = new CopyAction(controller);
pasteAction = new PasteAction(controller);
cutAction = new CutAction(controller);
deleteAction = new DeleteAction(controller);
undoAction = new UndoAction(controller);
redoAction = new RedoAction(controller, undoAction);
undoAction.registerRedoAction(redoAction);
undoAction.setEnabled(false);
redoAction.setEnabled(false);
UndoableEditListener listener = new SimpleUndoListener(redoAction, undoAction, controller);
deleteAction.addUndoableEditListener(listener);
copyAction.addUndoableEditListener(listener);
cutAction.addUndoableEditListener(listener);
pasteAction.addUndoableEditListener(listener);
storeEnabledStatus();
}
/**
* Constructor
* @param undoListener undo listener
* @param applicationModel PIPE application model
* @param applicationController PIPE application controller
* @param applicationView PIPE application view
*/
public TokenActionManager(UndoableEditListener undoListener, PipeApplicationModel applicationModel,
PipeApplicationController applicationController, PipeApplicationView applicationView) {
tokenAction = new AddTokenAction(applicationModel);
deleteTokenAction = new DeleteTokenAction(applicationModel);
specifyTokenClasses = new SpecifyTokenAction(applicationController, applicationView);
tokenAction.addUndoableEditListener(undoListener);
deleteTokenAction.addUndoableEditListener(undoListener);
specifyTokenClasses.addUndoableEditListener(undoListener);
}
void sendUndoableEdit(Document d, UndoableEdit ue) {
if(d instanceof AbstractDocument) {
UndoableEditListener[] uels = ((AbstractDocument)d).getUndoableEditListeners();
UndoableEditEvent ev = new UndoableEditEvent(d, ue);
for(UndoableEditListener uel : uels) {
uel.undoableEditHappened(ev);
}
}
}
@Override
public void addUndoableEditListener(UndoableEditListener listener) {
super.addUndoableEditListener(listener);
if (LOG.isLoggable(Level.FINE)) {
UndoableEditListener[] listeners = getUndoableEditListeners();
if (listeners.length > 1) {
// Having two UE listeners may be dangerous - for example
// having two undo managers attached at once will lead to strange errors
// since only one of the UMs will work normally while processing
// in the other one will be (typically silently) failing.
LOG.log(Level.INFO, "Two or more UndoableEditListeners attached", new Exception()); // NOI18N
}
}
}
public void testRecursiveUndoableEdits() throws Exception {
final BaseDocument doc = new BaseDocument(false, "text/plain");
class UEL implements UndoableEditListener, Runnable {
boolean undo;
@Override
public void undoableEditHappened(UndoableEditEvent e) {
//doc.runAtomic(this);
doc.render(this);
undo = e.getEdit().canUndo();
}
@Override
public void run() {
}
}
UEL uel = new UEL();
doc.addUndoableEditListener(uel);
class Atom implements Runnable {
@Override
public void run() {
try {
doc.insertString(0, "Ahoj", null);
} catch (BadLocationException ex) {
throw new IllegalStateException(ex);
}
}
}
doc.runAtomicAsUser(new Atom());
assertTrue("Can undo now", uel.undo);
}
static void sendUndoableEdit(Document d, UndoableEdit ue) {
if(d instanceof AbstractDocument) {
UndoableEditListener[] uels = ((AbstractDocument)d).getUndoableEditListeners();
UndoableEditEvent ev = new UndoableEditEvent(d, ue);
for(UndoableEditListener uel : uels) {
uel.undoableEditHappened(ev);
}
}
}
@Override
public synchronized void addUndoableRefactorListener(UndoableEditListener uel) {
//
savedUndoableEditListeners = ues.getUndoableEditListeners();
if (savedUndoableEditListeners != null) {
for (UndoableEditListener saved : savedUndoableEditListeners) {
if (saved instanceof UndoManager) {
((UndoManager)saved).discardAllEdits();
}
}
}
ues = new ModelUndoableEditSupport();
ues.addUndoableEditListener(uel);
}
private static void sendUndoableEdit(Document d, UndoableEdit ue) {
if(d instanceof AbstractDocument) {
UndoableEditListener[] uels = ((AbstractDocument)d).getUndoableEditListeners();
UndoableEditEvent ev = new UndoableEditEvent(d, ue);
for(UndoableEditListener uel : uels) {
uel.undoableEditHappened(ev);
}
}
}
private static void sendUndoableEdit(Document d, UndoableEdit ue) {
if(d instanceof AbstractDocument) {
UndoableEditListener[] uels = ((AbstractDocument)d).getUndoableEditListeners();
UndoableEditEvent ev = new UndoableEditEvent(d, ue);
for(UndoableEditListener uel : uels) {
uel.undoableEditHappened(ev);
}
}
}
public ComponentCreatorManager(UndoableEditListener undoListener, PipeApplicationModel applicationModel,
PipeApplicationController applicationController) {
placeAction = new PlaceAction(applicationModel);
transAction = new ImmediateTransitionAction(applicationModel);
timedtransAction = new TimedTransitionAction(applicationModel);
annotationAction = new AnnotationAction(applicationModel);
inhibarcAction =
new ArcAction("Inhibitor Arc", "Add an inhibitor arc (alt-h)", KeyEvent.VK_H, InputEvent.ALT_DOWN_MASK,
new InhibitorSourceVisitor(), new InhibitorCreator(), applicationModel,
applicationController, new InhibitorArcHead());
arcAction = new ArcAction("Arc", "Add an arc (alt-a)", KeyEvent.VK_A, InputEvent.ALT_DOWN_MASK,
new NormalArcSourceVisitor(), new NormalCreator(applicationController), applicationModel,
applicationController,
new NormalHead());
rateParameterAction = new SpecifyRateParameterAction(applicationController);
placeAction.addUndoableEditListener(undoListener);
transAction.addUndoableEditListener(undoListener);
timedtransAction.addUndoableEditListener(undoListener);
arcAction.addUndoableEditListener(undoListener);
inhibarcAction.addUndoableEditListener(undoListener);
annotationAction.addUndoableEditListener(undoListener);
rateParameterAction.addUndoableEditListener(undoListener);
}
public SyntaxDocument(Lexer lexer) {
super();
putProperty(PlainDocument.tabSizeAttribute, 4);
this.lexer = lexer;
// Listen for undo and redo events
addUndoableEditListener(new UndoableEditListener() {
@Override
public void undoableEditHappened(UndoableEditEvent evt) {
if (evt.getEdit().isSignificant()) {
undo.addEdit(evt.getEdit());
}
}
});
}
/**
* Constructor
*
* @param listener undoable event listener, used to register undo events to
* @param petriNetTab current Petri net tab
* @param net underlying Petri net displayed on the Petri net tab
* @param applicationController main application controller
*/
public CopyPasteManager(UndoableEditListener listener, PetriNetTab petriNetTab, PetriNet net,
PipeApplicationController applicationController) {
this.petriNetTab = petriNetTab;
petriNet = net;
this.applicationController = applicationController;
addMouseListener(this);
addMouseMotionListener(this);
addKeyListener(this);
this.listener = listener;
}
@Override
public void addUndoableEditListener(UndoableEditListener listener) {
this.document.addUndoableEditListener(listener);
}
@Override
public void removeUndoableEditListener(UndoableEditListener listener) {
this.document.removeUndoableEditListener(listener);
}
@Override
public void addUndoableEditListener(UndoableEditListener listener) {
this.document.addUndoableEditListener(listener);
}
@Override
public void removeUndoableEditListener(UndoableEditListener listener) {
this.document.removeUndoableEditListener(listener);
}
@Override
public void addUndoableEditListener(UndoableEditListener listener) {
this.document.addUndoableEditListener(listener);
}
@Override
public void removeUndoableEditListener(UndoableEditListener undoableEditListener) {
}
@Override
public void addUndoableEditListener(UndoableEditListener listener) {
this.document.addUndoableEditListener(listener);
}
@Override
public void removeUndoableEditListener(UndoableEditListener listener) {
this.document.removeUndoableEditListener(listener);
}
public InstantRefactoringPerformer(final JTextComponent target, int caretOffset, InstantRefactoringUI ui) {
releaseAll();
this.target = target;
this.ui = ui;
doc = target.getDocument();
MutablePositionRegion mainRegion = null;
List<MutablePositionRegion> regions = new ArrayList<>(ui.getRegions().size());
for (MutablePositionRegion current : ui.getRegions()) {
// TODO: type parameter name is represented as ident -> ignore surrounding <> in rename
if (isIn(current, caretOffset)) {
mainRegion = current;
} else {
regions.add(current);
}
}
if (mainRegion == null) {
throw new IllegalArgumentException("No highlight contains the caret.");
}
regions.add(0, mainRegion);
region = new SyncDocumentRegion(doc, regions);
if (doc instanceof BaseDocument) {
BaseDocument bdoc = ((BaseDocument) doc);
bdoc.addPostModificationDocumentListener(this);
UndoableWrapper wrapper = MimeLookup.getLookup("text/x-java").lookup(UndoableWrapper.class);
if(wrapper != null) {
wrapper.setActive(true, this);
}
UndoableEdit undo = new CancelInstantRenameUndoableEdit(this);
for (UndoableEditListener l : bdoc.getUndoableEditListeners()) {
l.undoableEditHappened(new UndoableEditEvent(doc, undo));
}
}
target.addKeyListener(this);
target.putClientProperty(InstantRefactoringPerformer.class, this);
target.putClientProperty("NetBeansEditor.navigateBoundaries", mainRegion); // NOI18N
requestRepaint();
target.select(mainRegion.getStartOffset(), mainRegion.getEndOffset());
span = region.getFirstRegionLength();
compl = new CompletionLayout(this);
compl.setEditorComponent(target);
final KeyStroke OKKS = ui.getKeyStroke();
target.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(OKKS, OKActionKey);
Action OKAction = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent ae) {
if(registry.contains(InstantRefactoringPerformer.this)) {
doFullRefactoring();
target.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).remove(OKKS);
target.getActionMap().remove(OKActionKey);
} else {
target.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).remove(OKKS);
target.getActionMap().remove(OKActionKey);
}
}
};
target.getActionMap().put(OKActionKey, OKAction);
final KeyStroke keyStroke = ui.getKeyStroke();
compl.showCompletion(ui.getOptions(), caretOffset, Bundle.INFO_PressAgain(getKeyStrokeAsText(keyStroke)));
registry.add(this);
}
/** Creates a new instance of InstantRenamePerformer */
private InstantRenamePerformer(JTextComponent target, Set<Token> highlights, int caretOffset) throws BadLocationException {
this.target = target;
doc = target.getDocument();
MutablePositionRegion mainRegion = null;
List<MutablePositionRegion> regions = new ArrayList<MutablePositionRegion>();
for (Token h : highlights) {
// type parameter name is represented as ident -> ignore surrounding <> in rename
int delta = h.id() == JavadocTokenId.IDENT && h.text().charAt(0) == '<' && h.text().charAt(h.length() - 1) == '>' ? 1 : 0;
Position start = NbDocument.createPosition(doc, h.offset(null) + delta, Bias.Backward);
Position end = NbDocument.createPosition(doc, h.offset(null) + h.length() - delta, Bias.Forward);
MutablePositionRegion current = new MutablePositionRegion(start, end);
if (isIn(current, caretOffset)) {
mainRegion = current;
} else {
regions.add(current);
}
}
if (mainRegion == null) {
throw new IllegalArgumentException("No highlight contains the caret.");
}
regions.add(0, mainRegion);
region = new SyncDocumentRegion(doc, regions);
if (doc instanceof BaseDocument) {
BaseDocument bdoc = ((BaseDocument) doc);
bdoc.setPostModificationDocumentListener(this);
UndoableEdit undo = new CancelInstantRenameUndoableEdit(this);
for (UndoableEditListener l : bdoc.getUndoableEditListeners()) {
l.undoableEditHappened(new UndoableEditEvent(doc, undo));
}
}
target.addKeyListener(this);
target.putClientProperty(InstantRenamePerformer.class, this);
target.putClientProperty("NetBeansEditor.navigateBoundaries", mainRegion); // NOI18N
requestRepaint();
target.select(mainRegion.getStartOffset(), mainRegion.getEndOffset());
span = region.getFirstRegionLength();
registry.add(this);
sendUndoableEdit(doc, CloneableEditorSupport.BEGIN_COMMIT_GROUP);
}
public void setUndoListener(UndoableEditListener listener) {
undoListener = listener;
}
/**
* Adds a listener to this action that is interested in undoable actions
* @param l listener
*/
public void addUndoableEditListener(UndoableEditListener l) {
//TODO: Should ideally throw an exception if listener != null
listener = l;
}
public void addUndoableEditListener(UndoableEditListener listener) {
//no-op
}
public void removeUndoableEditListener(UndoableEditListener listener) {
//no-op
}
protected DummyController(Place component, UndoableEditListener listener) {
super(component, listener);
}
@Override
public void removeUndoableEditListener(UndoableEditListener uel) {
ues.removeUndoableEditListener(uel);
}