Skip to content

Commit

Permalink
Reduce memory consumption by the logging panel (disable undo events f…
Browse files Browse the repository at this point in the history
…or it)

There are two fixes:
1) undoManager.setLimit(0) -> setLimit(1) since (0) means "unlimited undo"
See bobbylight/RSyntaxTextArea#99

2) By default, JMeter adds undoManager to ALL text fields via Swing updateUI method,
so we need to explicitly uninstall it for the case when undo is not needed
  • Loading branch information
vlsi committed Nov 4, 2021
1 parent 3c23246 commit 062190a
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.gui.JFactory;
import org.apache.jorphan.gui.JMeterUIDefaults;
import org.apache.jorphan.gui.ui.TextComponentUI;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
import org.fife.ui.rsyntaxtextarea.Theme;
Expand Down Expand Up @@ -240,6 +241,7 @@ public JSyntaxTextArea(int rows, int cols, boolean disableUndo) {
}
}
if(disableUndo) {
TextComponentUI.uninstallUndo(this);
// We need to do this to force recreation of undoManager which
// will use the disableUndo otherwise it would always be false
// See BUG 57440
Expand Down Expand Up @@ -276,7 +278,7 @@ public void setLanguage(String language) {
protected RUndoManager createUndoManager() {
RUndoManager undoManager = super.createUndoManager();
if(disableUndo) {
undoManager.setLimit(0);
undoManager.setLimit(1);
} else {
undoManager.setLimit(MAX_UNDOS);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.jorphan.gui.ui;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.text.Document;
import javax.swing.undo.UndoManager;

class AddUndoableEditListenerPropertyChangeListener implements PropertyChangeListener {
private final UndoManager manager;

public AddUndoableEditListenerPropertyChangeListener(UndoManager manager) {
this.manager = manager;
}

public final UndoManager getUndoManager() {
return manager;
}

@Override
public void propertyChange(PropertyChangeEvent evt) {
manager.discardAllEdits();
if (evt.getOldValue() != null) {
((Document) evt.getOldValue()).removeUndoableEditListener(manager);
}
if (evt.getNewValue() != null) {
((Document) evt.getNewValue()).addUndoableEditListener(manager);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.jorphan.gui.ui;

import java.util.concurrent.atomic.AtomicInteger;

import javax.swing.event.UndoableEditEvent;
import javax.swing.undo.UndoManager;

class DefaultUndoManager extends UndoManager {
private final AtomicInteger undoEpoch;
private int ourUndoEpoch;

DefaultUndoManager(AtomicInteger undoEpoch) {
this.undoEpoch = undoEpoch;
this.ourUndoEpoch = undoEpoch.get();
}

@Override
public synchronized void discardAllEdits() {
super.discardAllEdits();
ourUndoEpoch = undoEpoch.get();
}

@Override
public void undoableEditHappened(UndoableEditEvent e) {
int epoch = undoEpoch.get();
if (ourUndoEpoch != epoch) {
discardAllEdits();
}
super.undoableEditHappened(e);
}

@Override
public synchronized boolean canUndo() {
return ourUndoEpoch == undoEpoch.get() && super.canUndo();
}

@Override
public synchronized boolean canRedo() {
return ourUndoEpoch == undoEpoch.get() && super.canRedo();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;

import javax.swing.AbstractAction;
import javax.swing.KeyStroke;
import javax.swing.event.UndoableEditEvent;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
import javax.swing.undo.UndoManager;
Expand Down Expand Up @@ -70,44 +72,10 @@ public void installUndo(JTextComponent component) {
// JComponent#name is updated. However, we don't want user to be able to "undo" that
// So when tree selection is changed, we increase undoEpoch. That enables
// UndoManagers to treat that as "end of undo history"
UndoManager manager = new UndoManager() {
private int ourUndoEpoch = undoEpoch.get();

@Override
public synchronized void discardAllEdits() {
super.discardAllEdits();
ourUndoEpoch = undoEpoch.get();
}

@Override
public void undoableEditHappened(UndoableEditEvent e) {
int epoch = undoEpoch.get();
if (ourUndoEpoch != epoch) {
discardAllEdits();
}
super.undoableEditHappened(e);
}

@Override
public synchronized boolean canUndo() {
return ourUndoEpoch == undoEpoch.get() && super.canUndo();
}

@Override
public synchronized boolean canRedo() {
return ourUndoEpoch == undoEpoch.get() && super.canRedo();
}
};
UndoManager manager = new DefaultUndoManager(undoEpoch);
manager.setLimit(200);
component.addPropertyChangeListener("document", evt -> {
manager.discardAllEdits();
if (evt.getOldValue() != null) {
((Document) evt.getOldValue()).removeUndoableEditListener(manager);
}
if (evt.getNewValue() != null) {
((Document) evt.getNewValue()).addUndoableEditListener(manager);
}
});
component.addPropertyChangeListener("document",
new AddUndoableEditListenerPropertyChangeListener(manager));
component.getActionMap().put("undo", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
Expand All @@ -130,4 +98,32 @@ public void actionPerformed(ActionEvent e) {
KeyStroke shiftCommandZ = KeyStroke.getKeyStroke(KeyEvent.VK_Z, COMMAND_KEY | InputEvent.SHIFT_DOWN_MASK);
component.getInputMap().put(shiftCommandZ, "redo");
}

/**
* Removes the default undo manager.
* By default, JMeter installs undo manager to all text fields via {@code Swing -> createUI},
* however, undo is not always needed (e.g. log panel), so here's an API to remove it.
* @param component JTextField or JTextArea
*/
@API(since = "5.5", status = API.Status.INTERNAL)
public static void uninstallUndo(JTextComponent component) {
List<PropertyChangeListener> listenersToRemove = new ArrayList<>();
for (PropertyChangeListener listener : component.getPropertyChangeListeners("document")) {
if (listener instanceof AddUndoableEditListenerPropertyChangeListener) {
AddUndoableEditListenerPropertyChangeListener v =
(AddUndoableEditListenerPropertyChangeListener) listener;
listenersToRemove.add(v);

UndoManager undoManager = v.getUndoManager();
undoManager.discardAllEdits();
Document document = component.getDocument();
if (document != null) {
document.removeUndoableEditListener(undoManager);
}
}
}
for (PropertyChangeListener listener : listenersToRemove) {
component.removePropertyChangeListener("document", listener);
}
}
}
1 change: 1 addition & 0 deletions xdocs/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ Summary
<ul>
<li><bug>61805</bug><pr>663</pr>Add simple HTTP request template. Contributed by Ori Marko (orimarko at gmail.com)</li>
<li><bug>65611</bug><pr>673</pr>Add support for IPv6 addresses when specifying a remote worker node. Based on a patch by Peter Wong (peter.wong at csexperts.com)</li>
<li>Reduce memory consumption by the logging panel (disable undo events for it)</li>
</ul>

<ch_section>Non-functional changes</ch_section>
Expand Down

0 comments on commit 062190a

Please sign in to comment.