Commit 12338c5c authored by Nico Mack's avatar Nico Mack

Implemented support for switchable stylesheets depending on selection

state
parent ba136533
......@@ -25,6 +25,7 @@ import lu.list.itis.dkd.tui.content.InformationReceiver;
import lu.list.itis.dkd.tui.utility.ColorPair;
import lu.list.itis.dkd.tui.utility.Point;
import lu.list.itis.dkd.tui.utility.ScreenCoordinates;
import lu.list.itis.dkd.tui.utility.SwitchablePair;
import lu.list.itis.dkd.tui.utility.SyncHtmlEditorKit;
import lu.list.itis.dkd.tui.widget.corona.builder.BaseHtmlBoxBuilder;
......@@ -46,6 +47,7 @@ import java.net.URL;
import java.util.List;
import javax.swing.JEditorPane;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.StyleSheet;
......@@ -69,13 +71,15 @@ public class HtmlBox extends SelectableCorona implements InformationReceiver<Str
protected int strokeWidth;
protected int insetBorder;
protected List<String> styleRules;
protected BufferedImage rendered;
protected SwitchablePair<List<String>> switchableStyleRules;
protected BufferedImage rendered;
private Stroke borderStroke;
private Rectangle2D shapeBounds;
private Point renderedCentre;
private JEditorPane editor;
private SwitchablePair<StyleSheet> styleSheets;
// ***************************************************************************
// * Constant(s) *
......@@ -111,6 +115,7 @@ public class HtmlBox extends SelectableCorona implements InformationReceiver<Str
borderStroke = (builder.strokeWidth > 0) ? new BasicStroke(builder.strokeWidth) : null;
insetBorder = builder.insetBorder;
styleRules = builder.styleRules;
switchableStyleRules = builder.switchableStyleRules;
shapeBounds = shape.getBounds();
this.buildEditorFromProperties();
}
......@@ -136,6 +141,7 @@ public class HtmlBox extends SelectableCorona implements InformationReceiver<Str
borderStroke = (original.strokeWidth > 0) ? new BasicStroke(original.strokeWidth) : null;
insetBorder = original.insetBorder;
styleRules = original.styleRules;
switchableStyleRules = original.switchableStyleRules;
shapeBounds = shape.getBounds();
this.buildEditorFromProperties();
}
......@@ -147,21 +153,29 @@ public class HtmlBox extends SelectableCorona implements InformationReceiver<Str
// ---------------------------------------------------------------------------
private void buildEditorFromProperties() {
SyncHtmlEditorKit htmlEditor = new SyncHtmlEditorKit();
if (styleRules != null) {
StyleSheet stylesheet = htmlEditor.getStyleSheet();
styleRules.forEach(rule -> stylesheet.addRule(rule));
SyncHtmlEditorKit editorKit = new SyncHtmlEditorKit();
if (switchableStyleRules != null) {
styleSheets = new SwitchablePair<>(new StyleSheet(), new StyleSheet());
applyStyles(styleSheets.getFirst(), switchableStyleRules.getFirst());
applyStyles(styleSheets.getSecond(), switchableStyleRules.getSecond());
// applyStyles(editorKits.getSecond().getStyleSheet(), switchableStyleRules.getSecond());
styleSheets.setSwitched(this.isSelected);
editorKit.getStyleSheet().addStyleSheet(styleSheets.getCurrent());
} else {
editorKit = new SyncHtmlEditorKit();
applyStyles(editorKit.getStyleSheet(), styleRules);
}
editor = new JEditorPane();
editor.setContentType("text/html"); //$NON-NLS-1$
editor.setEditorKit(htmlEditor);
editor.setEditorKit(editorKit);
editor.setEditable(false);
int width = (int) (shapeBounds.getWidth() - 2 * insetBorder);
int height = (int) (shapeBounds.getHeight() - 2 * insetBorder);
editor.setSize(new Dimension(width, height));
editor.setOpaque(false);
editor.setDoubleBuffered(false);
......@@ -174,10 +188,24 @@ public class HtmlBox extends SelectableCorona implements InformationReceiver<Str
// ---------------------------------------------------------------------------
private StyleSheet applyStyles(StyleSheet styleSheet, List<String> styles) {
styles.forEach(style -> styleSheet.addRule(style));
return styleSheet;
}
// ---------------------------------------------------------------------------
private void renderHtml(String html) {
try {
editor.read(new StringReader(html), baseUrl);
if (styleSheets != null) {
HTMLDocument document = (HTMLDocument) editor.getDocument();
StyleSheet styleSheet = document.getStyleSheet();
styleSheet.addStyleSheet(styleSheets.getCurrent());
}
this.htmlContent = html;
} catch (IOException exception) {
LOGGER.error("failed to update HTML content!", exception); //$NON-NLS-1$
}
......@@ -195,7 +223,6 @@ public class HtmlBox extends SelectableCorona implements InformationReceiver<Str
@Override
public void setSelected(boolean selectIt) {
super.setSelected(selectIt);
if (this.fillColour != null) {
this.fillColour.setSwitched(selectIt);
}
......@@ -203,6 +230,11 @@ public class HtmlBox extends SelectableCorona implements InformationReceiver<Str
this.strokeColour.setSwitched(selectIt);
}
if (styleSheets != null) {
styleSheets.setSwitched(selectIt);
if ((htmlContent != null) && !htmlContent.isEmpty())
this.setInformation(htmlContent);
}
}
// ---------------------------------------------------------------------------
......
......@@ -30,6 +30,7 @@ import lu.list.itis.dkd.tui.utility.ColorPair;
import lu.list.itis.dkd.tui.utility.Externalization;
import lu.list.itis.dkd.tui.utility.Point;
import lu.list.itis.dkd.tui.utility.StringUtils;
import lu.list.itis.dkd.tui.utility.SwitchablePair;
import lu.list.itis.dkd.tui.widget.corona.HtmlBox;
import com.google.common.base.Strings;
......@@ -68,6 +69,7 @@ public abstract class BaseHtmlBoxBuilder<B extends BaseHtmlBoxBuilder<B>> extend
public int strokeWidth;
public int insetBorder;
public List<String> styleRules;
public SwitchablePair<List<String>> switchableStyleRules;
// ***************************************************************************
// * Constants *
......@@ -168,9 +170,25 @@ public abstract class BaseHtmlBoxBuilder<B extends BaseHtmlBoxBuilder<B>> extend
strokeWidth = BootstrappingUtils.getContentAsInteger(rootElement, Externalization.STROKE_WIDTH_NODE, BootstrappingUtils.OPTIONAL, 1, context);
insetBorder = BootstrappingUtils.getContentAsInteger(rootElement, Externalization.INSET_BORDER_NODE, BootstrappingUtils.OPTIONAL, 0, context);
List<String> selectedRules = null;
List<String> deselectedRules = null;
Element styleSheetElement = rootElement.getChild(Externalization.STYLESHEET_NODE);
if (styleSheetElement != null) {
styleRules = this.buildStyleRules(styleSheetElement, context, callback);
Element selectedElement = styleSheetElement.getChild(Externalization.SELECTED_ELEMENT);
if (selectedElement != null) {
selectedRules = this.buildStyleRules(selectedElement, context, callback);
}
Element deselectedElement = styleSheetElement.getChild(Externalization.DESELECTED_ELEMENT);
if (deselectedElement != null) {
deselectedRules = this.buildStyleRules(deselectedElement, context, callback);
}
if ((selectedRules == null) && (deselectedRules == null)) {
styleRules = this.buildStyleRules(styleSheetElement, context, callback);
} else {
switchableStyleRules = new SwitchablePair<>(selectedRules, deselectedRules);
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment