mirror of
https://github.com/maxkratz/lectureStudio.git
synced 2024-09-08 00:03:48 +00:00
presenter: fixed text-boxes disposal
This commit is contained in:
parent
8150a24e80
commit
595556be97
7 changed files with 61 additions and 77 deletions
|
@ -38,4 +38,6 @@ public interface PageObjectView<T extends Shape> extends View {
|
|||
|
||||
void setOnFocus(ConsumerAction<Boolean> action);
|
||||
|
||||
void dispose();
|
||||
|
||||
}
|
||||
|
|
|
@ -85,6 +85,11 @@ public class TeXBox extends PageObject<TeXShape> implements TeXBoxView {
|
|||
return getClass().getResource("/resources/css/tex-box.css").toExternalForm();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Skin<?> createDefaultSkin() {
|
||||
return new TeXBoxSkin(this);
|
||||
|
|
|
@ -102,6 +102,11 @@ public class TextBox extends PageObject<TextShape> implements TextBoxView {
|
|||
return Objects.requireNonNull(getClass().getResource("/resources/css/text-box.css")).toExternalForm();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Skin<?> createDefaultSkin() {
|
||||
return new TextBoxSkin(this);
|
||||
|
|
|
@ -243,6 +243,10 @@ public class SlideView extends JComponent implements org.lecturestudio.core.view
|
|||
}
|
||||
|
||||
public void removeAllPageObjectViews() {
|
||||
for (var objectView : getPageObjectViews()) {
|
||||
objectView.dispose();
|
||||
}
|
||||
|
||||
getPageObjectViews().clear();
|
||||
|
||||
surfaceView.removeAll();
|
||||
|
|
|
@ -112,7 +112,11 @@ public abstract class TextInputPageObject<T extends Shape> extends PageObject<T>
|
|||
updateContentSize();
|
||||
}
|
||||
|
||||
protected void dispose() {
|
||||
public void dispose() {
|
||||
|
||||
}
|
||||
|
||||
protected void close() {
|
||||
if (nonNull(getOnClose())) {
|
||||
getOnClose().execute();
|
||||
}
|
||||
|
@ -176,7 +180,7 @@ public abstract class TextInputPageObject<T extends Shape> extends PageObject<T>
|
|||
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
dispose();
|
||||
close();
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
@ -104,7 +104,7 @@ public class SwingTeXBoxView extends TextInputPageObject<TeXShape> implements Te
|
|||
}
|
||||
|
||||
@Override
|
||||
protected void dispose() {
|
||||
public void dispose() {
|
||||
if (nonNull(listener)) {
|
||||
getPageShape().textProperty().removeListener(listener);
|
||||
}
|
||||
|
|
|
@ -26,22 +26,17 @@ import java.awt.Rectangle;
|
|||
import java.awt.event.FocusAdapter;
|
||||
import java.awt.event.FocusEvent;
|
||||
import java.awt.font.FontRenderContext;
|
||||
import java.awt.font.LineBreakMeasurer;
|
||||
import java.awt.font.TextAttribute;
|
||||
import java.awt.font.TextLayout;
|
||||
import java.awt.geom.AffineTransform;
|
||||
import java.text.AttributedCharacterIterator;
|
||||
import java.text.AttributedString;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.JTextArea;
|
||||
import javax.swing.*;
|
||||
import javax.swing.event.DocumentEvent;
|
||||
import javax.swing.event.DocumentListener;
|
||||
import javax.swing.plaf.basic.BasicTextAreaUI;
|
||||
|
||||
import org.lecturestudio.core.beans.ChangeListener;
|
||||
import org.lecturestudio.core.geometry.Point2D;
|
||||
import org.lecturestudio.core.geometry.Rectangle2D;
|
||||
import org.lecturestudio.core.graphics.Color;
|
||||
|
@ -49,22 +44,22 @@ import org.lecturestudio.core.model.shape.TextShape;
|
|||
import org.lecturestudio.core.text.Font;
|
||||
import org.lecturestudio.core.text.TextAttributes;
|
||||
import org.lecturestudio.core.view.TextBoxView;
|
||||
import org.lecturestudio.swing.beans.Binding;
|
||||
import org.lecturestudio.swing.components.TextInputPageObject;
|
||||
import org.lecturestudio.swing.converter.ColorConverter;
|
||||
import org.lecturestudio.swing.converter.FontConverter;
|
||||
import org.lecturestudio.swing.util.SwingUtils;
|
||||
|
||||
public class SwingTextBoxView extends TextInputPageObject<TextShape> implements TextBoxView {
|
||||
|
||||
private static final java.awt.Color THEME_COLOR = new java.awt.Color(253, 224, 71, 125);
|
||||
|
||||
private Binding textBinding;
|
||||
|
||||
private JTextArea textArea;
|
||||
|
||||
private double fontSize;
|
||||
|
||||
private DocumentListener documentListener;
|
||||
|
||||
private ChangeListener<String> textChangeListener;
|
||||
|
||||
|
||||
public SwingTextBoxView() {
|
||||
super();
|
||||
|
@ -135,9 +130,12 @@ public class SwingTextBoxView extends TextInputPageObject<TextShape> implements
|
|||
}
|
||||
|
||||
@Override
|
||||
protected void dispose() {
|
||||
if (nonNull(textBinding)) {
|
||||
textBinding.unbind();
|
||||
public void dispose() {
|
||||
if (nonNull(textChangeListener)) {
|
||||
getPageShape().textProperty().removeListener(textChangeListener);
|
||||
}
|
||||
if (nonNull(documentListener)) {
|
||||
textArea.getDocument().removeDocumentListener(documentListener);
|
||||
}
|
||||
|
||||
super.dispose();
|
||||
|
@ -154,7 +152,32 @@ public class SwingTextBoxView extends TextInputPageObject<TextShape> implements
|
|||
textArea.setFont(FontConverter.INSTANCE.to(shape.getFont()));
|
||||
textArea.setText(shape.getText());
|
||||
|
||||
textBinding = SwingUtils.bindBidirectional(textArea, shape.textProperty());
|
||||
textChangeListener = (observable, oldValue, newValue) -> {
|
||||
if (!textArea.getText().equals(newValue)) {
|
||||
textArea.setText(newValue);
|
||||
}
|
||||
};
|
||||
|
||||
documentListener = new DocumentListener() {
|
||||
|
||||
@Override
|
||||
public void insertUpdate(DocumentEvent e) {
|
||||
changedUpdate(e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeUpdate(DocumentEvent e) {
|
||||
changedUpdate(e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void changedUpdate(DocumentEvent e) {
|
||||
shape.setText(textArea.getText());
|
||||
}
|
||||
};
|
||||
|
||||
shape.textProperty().addListener(textChangeListener);
|
||||
textArea.getDocument().addDocumentListener(documentListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -293,65 +316,6 @@ public class SwingTextBoxView extends TextInputPageObject<TextShape> implements
|
|||
shape.setDirtyBounds(dirtyBounds);
|
||||
}
|
||||
|
||||
private List<TextLayout> getTextLayouts() {
|
||||
List<TextLayout> layouts = new ArrayList<>();
|
||||
TextShape shape = getPageShape();
|
||||
String text = shape.getText();
|
||||
|
||||
if (text.isEmpty()) {
|
||||
return layouts;
|
||||
}
|
||||
|
||||
// Scale font.
|
||||
Font font = shape.getFont().clone();
|
||||
font.setSize(font.getSize() * getPageTransform().getScaleY());
|
||||
|
||||
java.awt.Font textFont = FontConverter.INSTANCE.to(font);
|
||||
|
||||
AttributedString styledText = new AttributedString(text);
|
||||
styledText.addAttribute(TextAttribute.FONT, textFont);
|
||||
styledText.addAttribute(TextAttribute.UNDERLINE, shape.isUnderline() ? TextAttribute.UNDERLINE_ON : -1);
|
||||
styledText.addAttribute(TextAttribute.STRIKETHROUGH, shape.isStrikethrough());
|
||||
styledText.addAttribute(TextAttribute.FOREGROUND, java.awt.Color.red);
|
||||
|
||||
FontRenderContext frc = new FontRenderContext(null, true, true);
|
||||
|
||||
AttributedCharacterIterator iterator = styledText.getIterator();
|
||||
int start = iterator.getBeginIndex();
|
||||
int end = iterator.getEndIndex();
|
||||
|
||||
LineBreakMeasurer measurer = new LineBreakMeasurer(iterator, frc);
|
||||
measurer.setPosition(start);
|
||||
|
||||
float wrapWidth = getWrappingWidth(text, frc, textFont);
|
||||
int limit;
|
||||
int newLineIndex;
|
||||
|
||||
while (measurer.getPosition() < end) {
|
||||
limit = measurer.nextOffset(wrapWidth);
|
||||
|
||||
newLineIndex = text.indexOf('\n', measurer.getPosition());
|
||||
if (newLineIndex != -1) {
|
||||
limit = newLineIndex;
|
||||
}
|
||||
|
||||
layouts.add(measurer.nextLayout(wrapWidth, limit + 1, false));
|
||||
}
|
||||
|
||||
return layouts;
|
||||
}
|
||||
|
||||
private float getWrappingWidth(String text, FontRenderContext frc, java.awt.Font textFont) {
|
||||
String[] lines = text.split("\\n");
|
||||
double wrapWidth = 0;
|
||||
|
||||
for (String line : lines) {
|
||||
wrapWidth = Math.max(wrapWidth, textFont.getStringBounds(line, frc).getWidth());
|
||||
}
|
||||
|
||||
return (float) wrapWidth;
|
||||
}
|
||||
|
||||
public static Number toAwtFontUnderline(boolean underline) {
|
||||
if (underline) {
|
||||
return TextAttribute.UNDERLINE_ON;
|
||||
|
|
Loading…
Reference in a new issue