trying to streamline methods that update appearance/colors/etc

This commit is contained in:
Ben Fry
2021-07-06 21:07:09 -04:00
parent e5dc86e9c1
commit 73fd1ac6e6
6 changed files with 79 additions and 42 deletions

View File

@@ -183,6 +183,15 @@ public class JEditTextArea extends JComponent
}
public void updateAppearance() {
// This default version will update the fonts and not much else.
// It's expected to always be overridden by the PdeTextArea version,
// but it's here if a Mode author *really* must avoid PdeTextArea.
painter.updateAppearance();
repaint();
}
/**
* Override this to provide your own painter for this {@link JEditTextArea}.
* @return a newly constructed {@link TextAreaPainter}.

View File

@@ -77,9 +77,17 @@ public class PdeTextArea extends JEditTextArea {
}
/*
public void setMode(Mode mode) {
((PdeTextAreaPainter) painter).setMode(mode);
}
*/
@Override
public void updateAppearance() {
((PdeTextAreaPainter) painter).updateAppearance(editor.getMode());
repaint();
}
/**

View File

@@ -4,7 +4,7 @@
PdeTextAreaDefaults - grabs font/color settings for the editor
Part of the Processing project - http://processing.org
Copyright (c) 2012-14 The Processing Foundation
Copyright (c) 2012-21 The Processing Foundation
Copyright (c) 2004-12 Ben Fry and Casey Reas
Copyright (c) 2001-03 Massachusetts Institute of Technology
@@ -49,11 +49,15 @@ public class PdeTextAreaDefaults extends TextAreaDefaults {
// http://code.google.com/p/processing/issues/detail?id=1275
rows = 5;
styles = new SyntaxStyle[Token.ID_COUNT];
updateAppearance(mode);
}
protected void updateAppearance(Mode mode) {
fgcolor = mode.getColor("editor.fgcolor");
bgcolor = mode.getColor("editor.bgcolor");
styles = new SyntaxStyle[Token.ID_COUNT];
styles[Token.COMMENT1] = mode.getStyle("comment1");
styles[Token.COMMENT2] = mode.getStyle("comment2");

View File

@@ -82,11 +82,12 @@ public class PdeTextAreaPainter extends TextAreaPainter {
/**
* Loads theme for TextAreaPainter. This is handled here because in the olden
* days, Modes had different visual design. Now, these are just pulling the
* defaults from the standard theme, though there may be minor additions or
* overrides added in a Mode's own theme.txt file.
* days, Modes had different visual design from one another. Now, these are
* just pulling the defaults from the standard theme, though there may be
* minor additions or overrides added in a Mode's own theme.txt file.
*/
public void setMode(Mode mode) {
//public void setMode(Mode mode) {
protected void updateAppearance(Mode mode) {
errorUnderlineColor = mode.getColor("editor.error.underline.color");
warningUnderlineColor = mode.getColor("editor.warning.underline.color");
@@ -97,6 +98,14 @@ public class PdeTextAreaPainter extends TextAreaPainter {
gutterTextColor.getBlue(),
96);
gutterLineHighlightColor = mode.getColor("editor.gutter.linehighlight.color");
// pull in changes for syntax style, as well as foreground and background color
if (defaults instanceof PdeTextAreaDefaults) {
((PdeTextAreaDefaults) defaults).updateAppearance(mode);
}
// needs to happen after PdeTextAreaDefaults.updateAppearance(mode)
super.updateAppearance();
}

View File

@@ -77,7 +77,7 @@ public class TextAreaPainter extends JComponent implements TabExpander {
}
public void updateAppearance() {
protected void updateAppearance() {
setForeground(defaults.fgcolor);
setBackground(defaults.bgcolor);

View File

@@ -96,11 +96,6 @@ public abstract class Editor extends JFrame implements RunnerListener {
" " +
" ";
/**
* true if this file has not yet been given a name by the user
*/
// private boolean untitled;
private PageFormat pageFormat;
private PrinterJob printerJob;
@@ -139,8 +134,8 @@ public abstract class Editor extends JFrame implements RunnerListener {
// maintain caret position during undo operations
private Stack<Integer> caretUndoStack = new Stack<>();
private Stack<Integer> caretRedoStack = new Stack<>();
// used internally for every edit. Groups hotkey-event text manipulations and
// groups multi-character inputs into a single undos.
// Used internally for every edit. Groups hot key event text manipulations
// and multi-character inputs into a single undo objects.
private CompoundEdit compoundEdit;
// timer to decide when to group characters into an undo
private final Timer timer;
@@ -152,8 +147,6 @@ public abstract class Editor extends JFrame implements RunnerListener {
JMenu toolsMenu;
JMenu modePopup;
//Image backgroundGradient;
protected List<Problem> problems = Collections.emptyList();
@@ -173,9 +166,9 @@ public abstract class Editor extends JFrame implements RunnerListener {
// add listener to handle window close box hit event
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
base.handleClose(Editor.this, false);
base.handleClose(Editor.this, false);
}
});
});
// don't close the window when clicked, the app will take care
// of that via the handleQuitInternal() methods
// http://dev.processing.org/bugs/show_bug.cgi?id=440
@@ -184,24 +177,24 @@ public abstract class Editor extends JFrame implements RunnerListener {
// When bringing a window to front, let the Base know
addWindowListener(new WindowAdapter() {
public void windowActivated(WindowEvent e) {
base.handleActivated(Editor.this);
fileMenu.insert(Recent.getMenu(), 2);
Toolkit.setMenuMnemsInside(fileMenu);
public void windowActivated(WindowEvent e) {
base.handleActivated(Editor.this);
fileMenu.insert(Recent.getMenu(), 2);
Toolkit.setMenuMnemsInside(fileMenu);
mode.insertImportMenu(sketchMenu);
Toolkit.setMenuMnemsInside(sketchMenu);
mode.insertToolbarRecentMenu();
}
mode.insertImportMenu(sketchMenu);
Toolkit.setMenuMnemsInside(sketchMenu);
mode.insertToolbarRecentMenu();
}
public void windowDeactivated(WindowEvent e) {
// TODO call handleActivated(null)? or do we run the risk of the
// deactivate call for old window being called after the activate?
fileMenu.remove(Recent.getMenu());
mode.removeImportMenu(sketchMenu);
mode.removeToolbarRecentMenu();
}
});
public void windowDeactivated(WindowEvent e) {
// TODO call handleActivated(null)? or do we run the risk of the
// deactivate call for old window being called after the activate?
fileMenu.remove(Recent.getMenu());
mode.removeImportMenu(sketchMenu);
mode.removeToolbarRecentMenu();
}
});
timer = new Timer();
@@ -250,10 +243,11 @@ public abstract class Editor extends JFrame implements RunnerListener {
upper.add(editorPanel);
// set colors and fonts for the painter object
PdeTextArea pta = getPdeTextArea();
if (pta != null) {
pta.setMode(mode);
}
// PdeTextArea pta = getPdeTextArea();
// if (pta != null) {
// pta.setMode(mode);
// }
updateAppearance();
splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, upper, footer);
@@ -351,6 +345,19 @@ public abstract class Editor extends JFrame implements RunnerListener {
}
public void updateAppearance() {
/*
PdeTextArea pta = getPdeTextArea();
// will be null if a subclass has overridden createTextArea()
// to return something besides a PdeTextArea
if (pta != null) {
pta.updateAppearance();
}
*/
textarea.updateAppearance();
}
protected JEditTextArea createTextArea() {
return new JEditTextArea(new PdeTextAreaDefaults(mode),
new PdeInputHandler(this));
@@ -574,9 +581,9 @@ public abstract class Editor extends JFrame implements RunnerListener {
*/
public void applyPreferences() {
// Update fonts and other items controllable from the prefs
textarea.getPainter().updateAppearance();
textarea.repaint();
// textarea.getPainter().updateAppearance();
// textarea.repaint();
textarea.updateAppearance();
console.updateAppearance();
}