RichTextFX's undo is not working properly - java

Whenever, I type something in CodeArea and press CTRL + Z and then begin typing again, the cursor gets reset to the start of the text.
I looked into this issue https://github.com/FXMisc/RichTextFX/issues/761 and seems like this bug is fixed. However, I was able to replicate the bug in latest version (0.9.1).
Following code will replicate the behavior:
public class GuiTest extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
StyleClassedTextArea codeArea = new StyleClassedTextArea();
codeArea.replaceText("Text");
VirtualizedScrollPane<StyleClassedTextArea> scrollPane = new VirtualizedScrollPane<>(codeArea);
final Scene scene = new Scene(scrollPane, 600, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
}
Am I missing something? If the bug exists any idea how to solve it?

After a brief debugging I found that RichTextFx is doing some weird behavior when selecting the new range (it is from this range you start typing the text, cursor position does not matter). When selecting the new range, the current range is set to 0,0 and then only moved to the cursor position. However, after performing the undo, the new range is selected to 0,0 and never to the current cursor position. It is because of that when we press undo, though the cursor is highlighted in another position, the text starts getting typed from the 0,0.
I was able to somewhat solve this using reflection. You need to extend the StyleClassedTextArea, override the undo() method, perform undo, and then set the position where you need your range in. You could set the range to the current cursor position but the cursor is not always in the correct position especially when the cursor needs to move to another style in another paragraph. I got the correct position range inside the ChangeQueue in the undoManager.
You need to make sure that you are casting properly so, do some debugging before casting.
#Override
public void undo() {
if (isUndoAvailable()) {
super.undo();
UndoManager undoManager = getUndoManager();
try {
Field queueField = UndoManagerImpl.class.getDeclaredField("queue");
queueField.setAccessible(true);
UnlimitedChangeQueue queue = (UnlimitedChangeQueue) queueField
.get(undoManager);
int newRange = ((PlainTextChange) ((List) queue.peekNext()).get(0))
.getRemovalEnd();
selectRange(newRange, newRange);
} catch (NoSuchFieldException | IllegalAccessException e) {
// Handle exception
}
}
}
#Override
public void redo() {
if (isRedoAvailable()) {
super.redo();
UndoManager undoManager = getUndoManager();
try {
Field queueField = UndoManagerImpl.class.getDeclaredField("queue");
queueField.setAccessible(true);
UnlimitedChangeQueue queue = (UnlimitedChangeQueue) queueField
.get(undoManager);
int newRange = ((PlainTextChange) ((List) queue.peekPrev()).get(0))
.getInsertionEnd();
selectRange(newRange, newRange);
} catch (NoSuchFieldException | IllegalAccessException e) {
// Handle
}
}
}

Related

GeoMap isn't centring correctly

I have an RCP view with a GeoMap widget inside it. When the view is created, the map is centred on a position.
The problem I'm having is that when GeoMap calls getSize() it is wrong. It is returning (0,0).
I've found one method of fixing this but it required changing code in GeoMap even though I believe the problem is to do with the code in the view. If I change the setCenterPosition method for GeoMap to use the parents client area it works.
View
#PostConstruct
public void createPartControl(final Composite parent) {
Display.getDefault().asyncExec(() -> setInitialPosition());
}
private void setInitialPosition() {
widget.setCenterPosition(new PointD(longitude, latitude));
widget.redraw();
}
GeoMap
public void setCenterPosition(final Point p) {
// `parent.getClientArea()` instead of `getSize()`
final Rectangle size = getParent().getClientArea();
setMapPosition(p.x - (size.width / 2), p.y - (size.height / 2));
}
The problem with this is that I've had to change code in GeoMap to fix a problem that is likely to do with getting the size of the view. The view hasn't had the size set by the time I call to centre the map.
I've also tried adding a resize listener to the view. This wouldn't always produce the same results when getting the size of the GeoMap widget.
parent.addListener(SWT.Resize, new Listener() {
#Override
public void handleEvent(final org.eclipse.swt.widgets.Event e) {
if (firstResize) {
setInitialPosition();
firstResize = false;
}
}
});
Here mentions that a resize listener should be used, and getClientArea. The problem is that GeoMap doesn't use getClientArea but I don't know if it should be.
Is this a problem with how I'm calling setCenterPosition or a bug in GeoMap?
Update
I've also tried adding a resize listener to the widget.
widget.addListener(SWT.Resize, new Listener() {
#Override
public void handleEvent(final org.eclipse.swt.widgets.Event e) {
if (firstResize) {
setInitialPosition();
firstResize = false;
}
}
});
For some reason the size the widget gets is too small. It is getting a size of (615, 279) when it should be (768, 388).
By adding a button to call setCenterPosition again it gets the correct size of (768, 388).
I can fix this if I add it inside an async block but this seems more like a workaround than an actual fix.
widget.addListener(SWT.Resize, new Listener() {
#Override
public void handleEvent(final org.eclipse.swt.widgets.Event e) {
if (firstResize) {
Display.getDefault().asyncExec(() -> {
setInitialPosition();
firstResize = false;
});
}
}
});

JavaFX scroll started and ended

I have a very costly action to do on a mouse scroll on a pane. I currently use
pane.setOnScroll({myMethod()}).
The problem is that if you scroll a lot it computes everything many times. So what I want is to do my actions only when the scroll is finished. I hoped to use setOnScrollStarted, save the starting value and setOnScrollFinished to do my actions.
But I don't know why these two methods are never called. As a test I used
pane.setOnScroll({System.out.println("proof of action"});
and it was clearly never called.
Any idea on how to call my method only at the end of the scroll?
Thanks in advance, A
From the javadoc of ScrollEvent (emphasis mine):
When the scrolling is produced by a touch gesture (such as dragging a
finger over a touch screen), it is surrounded by the SCROLL_STARTED
and SCROLL_FINISHED events. Changing number of involved touch points
during the scrolling is considered a new gesture, so the pair of
SCROLL_FINISHED and SCROLL_STARTED notifications is delivered each
time the touchCount changes. When the scrolling is caused by a mouse
wheel rotation, only a one-time SCROLL event is delivered, without the
started/finished surroundings.
A possible workaround:
Increment a counter variable every time a scroll is detected. In the listener start a new thread that waits 1 second and performs the action that you want only if the counter equals to 1 (the last scrolling) then decrements the counter.
I created a Gist, but I copy here the code:
public class ScrollablePane extends Pane {
private Integer scrollCounter = 0;
private final ObjectProperty<EventHandler<? super ScrollEvent>> onScrollEnded = new SimpleObjectProperty<>();
public final ObjectProperty<EventHandler<? super ScrollEvent>> onScrollEndedProperty() {
return onScrollEnded;
}
public ScrollablePane() {
this.setOnScroll(e -> {
scrollCounter++;
Thread th = new Thread(() -> {
try {
Thread.sleep(1000);
if (scrollCounter == 1)
onScrollEnded.get().handle(e);
scrollCounter--;
} catch (Exception e1) {
e1.printStackTrace();
}
});
th.setDaemon(true);
th.start();
});
}
public void setOnScrollEnded(EventHandler<? super ScrollEvent> handler) {
onScrollEnded.setValue(handler);
}
}
To use it:
public class MyApplication extends Application {
#Override
public void start(Stage primaryStage) {
try {
BorderPane root = new BorderPane();
Scene scene = new Scene(root, 400, 400);
ScrollablePane pane = new ScrollablePane();
pane.setOnScrollEnded(e -> System.out.println("Scroll just has been ended"));
root.setCenter(pane);
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}

Trying to get one mouse listener action to stop before another one begins

Not sure how well I will explain this; I'm quite new to programming...
So I'm trying to make a desktop application that draws musical notes of a given type on some sheet music when the user selects the button corresponding to that type of note. Currently, if the user selects the "Whole Note" button, the user can then start clicking on the screen and the note will be drawn where the click occurred. It will also make a "ding" sound and write some info about that note to a text file.
That's all well and good, but unfortunately when the user selects a new button, say the "Quarter Note" button, for each mouse click there will be two notes drawn (one whole, one quarter), two dings, and two packets of info written to the file. I have no idea how to make this work! Currently, I'm trying to use threads, such that each button creates a new thread and the thread currently in use is interrupted when a new button is pressed, but that doesn't resolve the issue.
Initially, an empty linked list of threads ("NoteThreads") is constructed. Also, a private class known as SheetMusicPane (given the variable name "smp") is constructed in order to draw the sheet music. The buttons are added in the main constructor (public CompositionStudio), whereas the method containing the mouse listener (see what follows) is contained in the SheetMusicPane private class. Not sure whether that is part of the problem.
I have a button action listener:
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!threads.isEmpty()) {
NoteThread oldThread = threads.remove();
oldThread.interrupt();
}
NoteThread newThread = new NoteThread(e.getActionCommand());
threads.add(newThread);
newThread.run();
}
});
that produces a thread:
private class NoteThread extends Thread {
private String note;
public NoteThread(String note) {
this.note = note;
}
public void run() {
smp.getShape(smp.getGraphics(), note);
}
}
that when, on running, calls this method with graphics and a mouse listener:
public void getShape(final Graphics g, final String note) {
this.addMouseListener(new MouseListener() {
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
Point p = MouseInfo.getPointerInfo().getLocation();
addShape(g, p.x, p.y, note);
int pitch = 12;
piano.playNote(pitch);
advance(1.0, piano);
try { addToFile(pitch, note);}
catch(FileNotFoundException fnfe) {}
catch(IOException ioe) {}
}
});
}
The above method is responsible for drawing the note ("addShape()"), making the ding sound, and writing to the file.
Thanks in advance for any help you can give!
what you're trying to do does not require multithreading. This is the approach that I'd take:
set up a set of toggle buttons or radio buttons to select the note to paint. this way, only one note will be selected at a time. add action listeners to those that store in an adequately scoped variable what note is selected, or infer that every time a note should be drawn. this way, you don't even add action listeners to the buttons. in any case, don't spawn new threads.
in your mouse listener, find out what note to draw, and do that - only one note.
if you can, stay away from multithreading, especially as a beginner. also, I think you confuse adding and running listeners here. each call to getShape() adds a new listener, meaning they accumulate over time, which might be the cause of your problems.
PS: welcome to stackoverflow! your question contained the important information and I could infer that you tried solving the problem yourself. It's pleasant to answer such questions!
One solution would be to simply fetch all the listeners (which should be 1) and remove them before adding the new listener:
public void getShape(final Graphics g, final String note) {
MouseListener[] listeners = this.getMouseListeners();
for (MouseListener ml : listeners) {
this.removeMouseListener(ml);
}
this.addMouseListener(new MouseListener()...);
}
An alternative, since you have a finite number of buttons, would be to create a finite set of listeners, eg:
private MouseListener wholeNote = new MouseListener()...;
private MouseListener quarterNote = new MouseListener()...;
Create a reference to the "current" listener (private MouseListener current;), and have a means of deciding which listener to use whenever getShape is called (a series of if conditions on the note String would work, although I would prefer some refactoring to use an enum personally). Then you could do something along the lines of:
private MouseListener wholeNote = new MouseListener()...;
private MouseListener quarterNote = new MouseListener()...;
private MouseListener current;
...
public void getShape(final Graphics g, final String note) {
if (current != null) {
this.removeMouseListener(current);
}
if (...) { // note is Whole Note
current = wholeNote;
} else if (...) { // note is Quarter Note
current = quarterNote;
} // etc.
this.addMouseListener(current);
}
Another alternative would be to change your listener so that you only ever need the one, but clicking a button changes a variable which the listener has access to. For example:
// In the listener
public void mouseClicked(MouseEvent e) {
Point p = MouseInfo.getPointerInfo().getLocation();
addShape(g, p.x, p.y, currentNote);
int pitch = 12;
piano.playNote(pitch);
advance(1.0, piano);
try { addToFile(pitch, currentNote);}
catch(FileNotFoundException fnfe) {}
catch(IOException ioe) {}
}
// In the same class
protected String currentNote;
...
public void getShape(final Graphics g, final String note) {
currentNote = note;
}

My internal frames don't earn focus after opening... (Java)

I have a menu with items that open internal frames, but every time I need to click twice in the frame. One time to give focus to the Int.frame and the second time to actually do something (give focus to a textfield).
So, here is my question: It's possible to automatic give focus to the Int.Frame?
Code of my main screen:
public final class principal extends javax.swing.JFrame {
viewCity city = new viewCity();
public principal() {
initComponents();
myListeners();
setLocationRelativeTo(null);
}
public void myListeners() {
menuCity.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
menuCityClicked(e);
}
});
}
public void central(JInternalFrame window1) {
int lDesk = panelPrincipal.getWidth();
int aDesk = panelPrincipal.getHeight();
int lIFrame = window1.getWidth();
int aIFrame = window1.getHeight();
window1.setLocation(lDesk / 2 - lIFrame / 2, aDesk / 2 - aIFrame / 2);
}
private void menuCityClicked(MouseEvent e) {
if (!city.isVisible()) {
panelPrincipal.add(city);
central(city);
city.requestFocus(); // Nothing
city.requestFocusInWindow(); // Nothing
city.setVisible(true);
city.requestFocus(); // Nothing
city.requestFocusInWindow(); // Nothing
}
}}
No matter what, the menu will always keep the focus. For example, click in your browser's menu, and you will keep the focus, by moving the cursor you will open other menus without need to click.
By putting the properties "selection model" to null works, but give me nullpointerexception.
Ok, the problem is with the jMenu, but with jMenuItem Works fine, so... I'm using

Setting caret position in JTextArea

I have a JTextArea. I have a function that selects some amount of text when some combination is called. It's done properly. The thing is, I want to move caret to the selection beginning when some text is selected and VK_LEFT is pressed. KeyListener is implemented properly, I tested it in other way. The thing is, that when I write following code:
#Override public void keyPressed( KeyEvent e) {
if(e.getKeyChar()==KeyEvent.VK_LEFT)
if(mainarea.getSelectedText()!=null)
mainarea.setCaretPosition(mainarea.getSelectionStart());
}
and add an instance of this listener to mainarea, select some text (using my function) and press left arrow key, the caret position is set to the end of selection... And I wont it to be in the beginning... What's the matter? :S
Here's a code snippet
Action moveToSelectionStart = new AbstractAction("moveCaret") {
#Override
public void actionPerformed(ActionEvent e) {
int selectionStart = textComponent.getSelectionStart();
int selectionEnd = textComponent.getSelectionEnd();
if (selectionStart != selectionEnd) {
textComponent.setCaretPosition(selectionEnd);
textComponent.moveCaretPosition(selectionStart);
}
}
public boolean isEnabled() {
return textComponent.getSelectedText() != null;
}
};
Object actionMapKey = "caret-to-start";
textComponent.getInputMap().put(KeyStroke.getKeyStroke("LEFT"), actionMapKey);
textComponent.getActionMap().put(actionMapKey, moveToSelectionStart);
Note: it's not recommended to re-define typically installed keybindings like f.i. any of the arrow keys, users might get really annoyed ;-) Better look for some that's not already bound.

Categories

Resources