Skip to content

Commit

Permalink
#409 simplify Swing-related code used by BrowserStartup
Browse files Browse the repository at this point in the history
* make fields non-nullable (when possible)
* make fields final (when possible)
* fix usages of deprecated Swing apis
  • Loading branch information
asolntsev committed Oct 14, 2024
1 parent b521bd9 commit 739fb9d
Show file tree
Hide file tree
Showing 19 changed files with 338 additions and 767 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@
* @author Patrick Wright
*/
public class FSScrollPane extends JScrollPane {
private static final long serialVersionUID = 2L;

/** Constant used for mapping a key binding to "scroll down 1 page" */
public static final String PAGE_DOWN = "page-down";

Expand All @@ -82,12 +80,6 @@ public class FSScrollPane extends JScrollPane {
/** Constant used for mapping a key binding to "scroll to top of document" */
public static final String PAGE_START = "page-start";


public FSScrollPane() {
this(null);
}


/** Instantiates a new FSScrollPane around the given Panel; see class documentation. */
public FSScrollPane(JPanel view) {
super(view, VERTICAL_SCROLLBAR_ALWAYS, HORIZONTAL_SCROLLBAR_ALWAYS);
Expand Down Expand Up @@ -119,11 +111,11 @@ private void setDefaultInputMap(JComponent view) {
view.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).
put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), LINE_UP);
view.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).
put(KeyStroke.getKeyStroke(KeyEvent.VK_END, KeyEvent.CTRL_MASK), PAGE_END);
put(KeyStroke.getKeyStroke(KeyEvent.VK_END, KeyEvent.CTRL_DOWN_MASK), PAGE_END);
view.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).
put(KeyStroke.getKeyStroke(KeyEvent.VK_END, 0), PAGE_END);
view.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).
put(KeyStroke.getKeyStroke(KeyEvent.VK_HOME, KeyEvent.CTRL_MASK), PAGE_START);
put(KeyStroke.getKeyStroke(KeyEvent.VK_HOME, KeyEvent.CTRL_DOWN_MASK), PAGE_START);
view.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).
put(KeyStroke.getKeyStroke(KeyEvent.VK_HOME, 0), PAGE_START);

Expand All @@ -133,8 +125,6 @@ private void setDefaultInputMap(JComponent view) {
private void setDefaultActionMap(JComponent view) {
view.getActionMap().put(PAGE_DOWN,
new AbstractAction() {
private static final long serialVersionUID = 1L;

@Override
public void actionPerformed(ActionEvent evt) {
JScrollBar sb = getVerticalScrollBar();
Expand All @@ -143,8 +133,6 @@ public void actionPerformed(ActionEvent evt) {
});
view.getActionMap().put(PAGE_END,
new AbstractAction() {
private static final long serialVersionUID = 1L;

@Override
public void actionPerformed(ActionEvent evt) {
JScrollBar sb = getVerticalScrollBar();
Expand All @@ -153,8 +141,6 @@ public void actionPerformed(ActionEvent evt) {
});
view.getActionMap().put(PAGE_UP,
new AbstractAction() {
private static final long serialVersionUID = 1L;

@Override
public void actionPerformed(ActionEvent evt) {
JScrollBar sb = getVerticalScrollBar();
Expand All @@ -163,8 +149,6 @@ public void actionPerformed(ActionEvent evt) {
});
view.getActionMap().put(PAGE_START,
new AbstractAction() {
private static final long serialVersionUID = 1L;

@Override
public void actionPerformed(ActionEvent evt) {
JScrollBar sb = getVerticalScrollBar();
Expand All @@ -173,8 +157,6 @@ public void actionPerformed(ActionEvent evt) {
});
view.getActionMap().put(LINE_DOWN,
new AbstractAction() {
private static final long serialVersionUID = 1L;

@Override
public void actionPerformed(ActionEvent evt) {
JScrollBar sb = getVerticalScrollBar();
Expand All @@ -183,8 +165,6 @@ public void actionPerformed(ActionEvent evt) {
});
view.getActionMap().put(LINE_UP,
new AbstractAction() {
private static final long serialVersionUID = 1L;

@Override
public void actionPerformed(ActionEvent evt) {
JScrollBar sb = getVerticalScrollBar();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
* ctx.setLogging(true); // turn on logging
* ctx.setValidating(true); // turn on doctype validation
* ctx.addFont(fnt,"Arial"); // redefine a font
* ctx.setDomImplementation("com.cooldom.DomImpl");
* ctx.setDomImplementation("com.cool.dom.DomImpl");
* </pre>
* <p>
* <p>XHTMLPanel comes with a pre-installed MouseListener which handles :hover events used for rollovers
Expand Down Expand Up @@ -131,15 +131,6 @@ private void resetListeners() {
}
}

/**
* Lays out the current document again, and re-renders.
*/
@Override
public void relayout() {
sharedContext.flushFonts();
super.relayout();
}

/**
* Loads and renders a Document given an uri.
* The uri is resolved by the UserAgentCallback
Expand Down Expand Up @@ -204,20 +195,6 @@ public void setDocument(File file) throws MalformedURLException {
);
}

/**
* Sets the {@link RenderingContext} attribute of the XHTMLPanel object. Generally
* you should not use this unless you have a heavily customized context to
* use. To modify just some rendering behavior, consider using
* {@link #getSharedContext()} to retrieve the current context, and using
* mutators to change its behavior.
*
* @param ctx A new RenderingContext to use for rendering.
*/
@Override
public void setSharedContext(SharedContext ctx) {
super.setSharedContext(ctx);
}

/**
* Sets the scaling factor used by {@link #incrementFontSize()} and
* {@link #decrementFontSize()}--both scale the font up or down by this
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@
* When applied to a Swing component, limits the total number of
* characters that can be entered.
*/
class SizeLimitedDocument extends PlainDocument
{
private static final long serialVersionUID = 1L;

class SizeLimitedDocument extends PlainDocument {
private final int _maximumLength;

public SizeLimitedDocument(int maximumLength) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
* just as you would expect from a browsable XHTML document. Drop your {@link org.xhtmlrenderer.simple.XHTMLPanel}
* in a {@link org.xhtmlrenderer.simple.FSScrollPane} and your users can move up or down by line or page, and jump to
* the start or end of the document, just as they are used to.</p>
* <p>{@link org.xhtmlrenderer.simple.XHTMLPrintable} supports the AWT {@link java.awt.print.Printable} interface,
* allowing you to print a document without rendering it onscreen first.</p>
*
* <h2>Related Documentation</h2>
* For overviews, tutorials, examples, guides, and tool documentation, please see:
* <ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package org.xhtmlrenderer.swing;

import com.google.errorprone.annotations.CheckReturnValue;
import org.jspecify.annotations.Nullable;
import org.w3c.dom.Document;
import org.xhtmlrenderer.css.style.CalculatedStyle.Edge;
Expand Down Expand Up @@ -73,7 +74,7 @@ protected BasicPanel() {
}

protected BasicPanel(UserAgentCallback uac) {
sharedContext = new SharedContext(uac);
super(new SharedContext(uac));
mouseTracker = new MouseTracker(this);
formSubmissionListener = query -> {
System.out.println("Form Submitted!");
Expand All @@ -87,7 +88,7 @@ protected BasicPanel(UserAgentCallback uac) {
JOptionPane.INFORMATION_MESSAGE
);
};
sharedContext.setFormSubmissionListener(formSubmissionListener);
getSharedContext().setFormSubmissionListener(formSubmissionListener);
init();
}

Expand Down Expand Up @@ -145,19 +146,11 @@ protected void doRender(RenderingContext c, Layer root) {
}
} catch (ThreadDeath t) {
throw t;
} catch (Throwable t) {
} catch (Error | RuntimeException t) {
if (hasDocumentListeners()) {
fireOnRenderException(t);
} else {
if (t instanceof Error) {
throw (Error)t;
}
if (t instanceof RuntimeException) {
throw (RuntimeException)t;
}

// "Shouldn't" happen
XRLog.exception(t.getMessage(), t);
throw t;
}
}
}
Expand Down Expand Up @@ -307,10 +300,6 @@ private void printTree(Box box, String tab) {
public void setLayout(LayoutManager l) {
}

public void setSharedContext(SharedContext ctx) {
sharedContext = ctx;
}

@Override
public void setSize(Dimension d) {
XRLog.layout(Level.FINEST, "set size called");
Expand Down Expand Up @@ -413,6 +402,7 @@ public void reloadDocument(Document doc) {
setDocument(this.doc, getSharedContext().getBaseURL(), getSharedContext().getNamespaceHandler());
}

@CheckReturnValue
public URL getURL() {
try {
return new URL(getSharedContext().getUac().getBaseURL());
Expand All @@ -421,6 +411,7 @@ public URL getURL() {
}
}

@CheckReturnValue
public Document getDocument() {
return doc;
}
Expand All @@ -437,7 +428,7 @@ public String getDocumentTitle() {
}

protected Document loadDocument(final String uri) {
XMLResource xmlResource = sharedContext.getUac().getXMLResource(uri);
XMLResource xmlResource = getSharedContext().getUac().getXMLResource(uri);
return xmlResource.getDocument();
}

Expand Down Expand Up @@ -548,6 +539,6 @@ public void submit(String url) {
}
public void setFormSubmissionListener(FormSubmissionListener fsl) {
formSubmissionListener =fsl;
sharedContext.setFormSubmissionListener(formSubmissionListener);
getSharedContext().setFormSubmissionListener(formSubmissionListener);
}
}
Loading

0 comments on commit 739fb9d

Please sign in to comment.