From c8b114195d87c2a26576f1e1b49fa4158432411f Mon Sep 17 00:00:00 2001 From: Bjorn Pijnacker Date: Tue, 23 Feb 2021 13:20:29 +0100 Subject: [PATCH 1/6] added a lolcat mode --- .../erme/EntityRelationMappingEditor.java | 43 ++--- .../control/actions/ExportToImageAction.java | 4 + .../erme/control/actions/LolcatAction.java | 27 ++++ .../andrewlalis/erme/view/EditorMenuBar.java | 1 + .../view/view_models/AttributeViewModel.java | 152 +++++++++++------- 5 files changed, 146 insertions(+), 81 deletions(-) create mode 100644 src/main/java/nl/andrewlalis/erme/control/actions/LolcatAction.java diff --git a/src/main/java/nl/andrewlalis/erme/EntityRelationMappingEditor.java b/src/main/java/nl/andrewlalis/erme/EntityRelationMappingEditor.java index 6099536..e50e30b 100644 --- a/src/main/java/nl/andrewlalis/erme/EntityRelationMappingEditor.java +++ b/src/main/java/nl/andrewlalis/erme/EntityRelationMappingEditor.java @@ -7,25 +7,30 @@ import nl.andrewlalis.erme.view.EditorFrame; import java.nio.charset.StandardCharsets; public class EntityRelationMappingEditor { - public static final String VERSION = "1.3.1"; + public static final String VERSION = "1.3.1"; + private static EditorFrame frame; - public static void main(String[] args) { - if (!FlatLightLaf.install()) { - System.err.println("Could not install FlatLight Look and Feel."); - } - final boolean includeAdminActions = shouldIncludeAdminActions(args); - if (includeAdminActions) { - System.out.println("Admin actions have been enabled."); - } - final EditorFrame frame = new EditorFrame(includeAdminActions); - frame.setVisible(true); - } + public static EditorFrame getFrame() { + return frame; + } - private static boolean shouldIncludeAdminActions(String[] args) { - if (args.length < 1) { - return false; - } - byte[] pw = args[0].getBytes(StandardCharsets.UTF_8); - return Hash.matches(pw, "admin_hash.txt"); - } + public static void main(String[] args) { + if (!FlatLightLaf.install()) { + System.err.println("Could not install FlatLight Look and Feel."); + } + final boolean includeAdminActions = shouldIncludeAdminActions(args); + if (includeAdminActions) { + System.out.println("Admin actions have been enabled."); + } + frame = new EditorFrame(includeAdminActions); + frame.setVisible(true); + } + + private static boolean shouldIncludeAdminActions(String[] args) { + if (args.length < 1) { + return false; + } + byte[] pw = args[0].getBytes(StandardCharsets.UTF_8); + return Hash.matches(pw, "admin_hash.txt"); + } } diff --git a/src/main/java/nl/andrewlalis/erme/control/actions/ExportToImageAction.java b/src/main/java/nl/andrewlalis/erme/control/actions/ExportToImageAction.java index 96c6fba..7ddeb19 100644 --- a/src/main/java/nl/andrewlalis/erme/control/actions/ExportToImageAction.java +++ b/src/main/java/nl/andrewlalis/erme/control/actions/ExportToImageAction.java @@ -4,6 +4,7 @@ import lombok.Setter; import nl.andrewlalis.erme.model.MappingModel; import nl.andrewlalis.erme.model.Relation; import nl.andrewlalis.erme.view.DiagramPanel; +import nl.andrewlalis.erme.view.view_models.AttributeViewModel; import nl.andrewlalis.erme.view.view_models.MappingModelViewModel; import javax.imageio.ImageIO; @@ -113,10 +114,13 @@ public class ExportToImageAction extends AbstractAction { DiagramPanel.prepareGraphics(g2d); // Render the model. + boolean lolcat = AttributeViewModel.getLolcatMode(); // save previous lolcat mode + AttributeViewModel.setLolcatMode(false); List selectedRelations = this.model.getSelectedRelations(); this.model.getSelectedRelations().forEach(r -> r.setSelected(false)); new MappingModelViewModel(this.model).draw(g2d); this.model.getRelations().forEach(r -> r.setSelected(selectedRelations.contains(r))); + AttributeViewModel.setLolcatMode(lolcat); // revert previous lolcat mode // Revert back to the normal image space, and render a watermark. g2d.setTransform(originalTransform); diff --git a/src/main/java/nl/andrewlalis/erme/control/actions/LolcatAction.java b/src/main/java/nl/andrewlalis/erme/control/actions/LolcatAction.java new file mode 100644 index 0000000..7fc7d94 --- /dev/null +++ b/src/main/java/nl/andrewlalis/erme/control/actions/LolcatAction.java @@ -0,0 +1,27 @@ +package nl.andrewlalis.erme.control.actions; + +import nl.andrewlalis.erme.EntityRelationMappingEditor; +import nl.andrewlalis.erme.view.view_models.AttributeViewModel; + +import javax.swing.*; +import java.awt.event.ActionEvent; + +public class LolcatAction extends AbstractAction { + private static LolcatAction instance; + public static LolcatAction getInstance() { + if (instance == null) { + instance = new LolcatAction(); + } + return instance; + } + + public LolcatAction() { + super("Toggle Lolcat Mode"); + } + + @Override + public void actionPerformed(ActionEvent actionEvent) { + AttributeViewModel.setLolcatMode(((AbstractButton)actionEvent.getSource()).getModel().isSelected()); + EntityRelationMappingEditor.getFrame().getContentPane().repaint(); + } +} diff --git a/src/main/java/nl/andrewlalis/erme/view/EditorMenuBar.java b/src/main/java/nl/andrewlalis/erme/view/EditorMenuBar.java index f2a5770..8710c3d 100644 --- a/src/main/java/nl/andrewlalis/erme/view/EditorMenuBar.java +++ b/src/main/java/nl/andrewlalis/erme/view/EditorMenuBar.java @@ -39,6 +39,7 @@ public class EditorMenuBar extends JMenuBar { menu.add(RemoveRelationAction.getInstance()); menu.add(AddAttributeAction.getInstance()); menu.add(RemoveAttributeAction.getInstance()); + menu.add(new JCheckBoxMenuItem(LolcatAction.getInstance())); menu.addSeparator(); menu.add(UndoAction.getInstance()); menu.add(RedoAction.getInstance()); diff --git a/src/main/java/nl/andrewlalis/erme/view/view_models/AttributeViewModel.java b/src/main/java/nl/andrewlalis/erme/view/view_models/AttributeViewModel.java index 6612554..e82d6c6 100644 --- a/src/main/java/nl/andrewlalis/erme/view/view_models/AttributeViewModel.java +++ b/src/main/java/nl/andrewlalis/erme/view/view_models/AttributeViewModel.java @@ -1,8 +1,10 @@ package nl.andrewlalis.erme.view.view_models; +import nl.andrewlalis.erme.EntityRelationMappingEditor; import nl.andrewlalis.erme.model.Attribute; import nl.andrewlalis.erme.model.AttributeType; import nl.andrewlalis.erme.model.ForeignKeyAttribute; +import nl.andrewlalis.erme.view.DiagramPanel; import java.awt.*; import java.awt.font.TextAttribute; @@ -13,72 +15,98 @@ import java.text.AttributedString; * View model for rendering a single attribute of a relation. */ public class AttributeViewModel implements ViewModel { - public static final int PADDING_X = 5; - public static final int PADDING_Y = 5; - public static final Color BACKGROUND_COLOR = Color.LIGHT_GRAY; - public static final Color FONT_COLOR = Color.BLACK; - public static final float FK_FONT_SIZE = 11.0f; + public static final int PADDING_X = 5; + public static final int PADDING_Y = 5; + public static final Color BACKGROUND_COLOR = Color.LIGHT_GRAY; + public static final Color FONT_COLOR = Color.BLACK; + public static final float FK_FONT_SIZE = 11.0f; + private static final float LOLCAT_SAT = 0.75f; + private static final float LOLCAT_BRIGHT = 1f; + private static boolean lolcatMode; + private final Attribute attribute; - private final Attribute attribute; + public AttributeViewModel(Attribute attribute) { + this.attribute = attribute; + } - public AttributeViewModel(Attribute attribute) { - this.attribute = attribute; - } + public static boolean getLolcatMode() { + return lolcatMode; + } - @Override - public void draw(Graphics2D g) { - AttributedString as = this.getAttributedString(g); - Rectangle r = this.getBounds(g, as); - g.setColor(BACKGROUND_COLOR); - g.fillRect(r.x, r.y, r.width, r.height); - g.setColor(FONT_COLOR); - g.drawRect(r.x, r.y, r.width, r.height); - g.drawString(as.getIterator(), r.x + PADDING_X, r.y + (r.height - PADDING_Y)); - if (this.attribute instanceof ForeignKeyAttribute) { - ForeignKeyAttribute fkAttribute = (ForeignKeyAttribute) this.attribute; - Font originalFont = g.getFont(); - g.setFont(g.getFont().deriveFont(Font.ITALIC, FK_FONT_SIZE)); - g.drawString(fkAttribute.getFullReferenceName(), r.x + PADDING_X, r.y - PADDING_Y); - g.setFont(originalFont); - } - } + public static void setLolcatMode(boolean lolcatMode) { + AttributeViewModel.lolcatMode = lolcatMode; + } - @Override - public Rectangle getBounds(Graphics2D g) { - return this.getBounds(g, this.getAttributedString(g)); - } + @Override + public void draw(Graphics2D g) { + AttributedString as = this.getAttributedString(g); + Rectangle r = this.getBounds(g, as); + g.setColor(this.getBackgroundColor(r.x + r.width / 2, r.y + r.height / 2, g)); + g.fillRect(r.x, r.y, r.width, r.height); + g.setColor(FONT_COLOR); + g.drawRect(r.x, r.y, r.width, r.height); + g.drawString(as.getIterator(), r.x + PADDING_X, r.y + (r.height - PADDING_Y)); + if (this.attribute instanceof ForeignKeyAttribute) { + ForeignKeyAttribute fkAttribute = (ForeignKeyAttribute) this.attribute; + Font originalFont = g.getFont(); + g.setFont(g.getFont().deriveFont(Font.ITALIC, FK_FONT_SIZE)); + g.drawString(fkAttribute.getFullReferenceName(), r.x + PADDING_X, r.y - PADDING_Y); + g.setFont(originalFont); + } + } - private Rectangle getBounds(Graphics2D g, AttributedString as) { - final RelationViewModel relationViewModel = (RelationViewModel) this.attribute.getRelation().getViewModel(); - int x = this.attribute.getRelation().getPosition().x + RelationViewModel.PADDING_X; - int y = this.attribute.getRelation().getPosition().y + relationViewModel.getNameBounds(g).height + RelationViewModel.ATTRIBUTE_SEPARATION; - int i = 0; - while (!this.attribute.getRelation().getAttributes().get(i).equals(this.attribute)) { - x += this.attribute.getRelation().getAttributes().get(i).getViewModel().getBounds(g).width; - i++; - } - Rectangle2D nameRect = g.getFontMetrics().getStringBounds(as.getIterator(), 0, this.attribute.getName().length(), g); - int width = (int) nameRect.getWidth() + (2 * PADDING_X); - int height = (int) nameRect.getHeight() + (2 * PADDING_Y); - if (this.attribute instanceof ForeignKeyAttribute) { - ForeignKeyAttribute fkAttribute = (ForeignKeyAttribute) this.attribute; - Font originalFont = g.getFont(); - g.setFont(g.getFont().deriveFont(Font.ITALIC, FK_FONT_SIZE)); - Rectangle referenceNameBounds = g.getFontMetrics().getStringBounds(fkAttribute.getFullReferenceName(), g).getBounds(); - g.setFont(originalFont); - width = Math.max(width, referenceNameBounds.width + (2 * PADDING_X)); - } - return new Rectangle(x, y, width, height); - } + private Color getBackgroundColor(int x, int y, Graphics2D g) { + if (!lolcatMode) return BACKGROUND_COLOR; + Dimension viewportSize = ((DiagramPanel)EntityRelationMappingEditor.getFrame().getContentPane()).getModel().getRelationBounds().getSize(); - private AttributedString getAttributedString(Graphics2D g) { - AttributedString as = new AttributedString(this.attribute.getName()); - as.addAttribute(TextAttribute.FONT, g.getFont()); - if (this.attribute.getType().equals(AttributeType.ID_KEY)) { - as.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON); - } else if (this.attribute.getType().equals(AttributeType.PARTIAL_ID_KEY)) { - as.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_LOW_DASHED); - } - return as; - } + double dx = viewportSize.width; + double dy = viewportSize.height; + double mag = Math.sqrt(dx * dx + dy * dy); + dx /= mag; + dy /= mag; + + double lambda = (dx * x + dy * y); + double diag_val = Math.sqrt(Math.pow(dx * lambda, 2) + Math.pow(dy * lambda, 2)) / mag; + + return Color.getHSBColor((float) diag_val, LOLCAT_SAT, LOLCAT_BRIGHT); + } + + @Override + public Rectangle getBounds(Graphics2D g) { + return this.getBounds(g, this.getAttributedString(g)); + } + + private Rectangle getBounds(Graphics2D g, AttributedString as) { + final RelationViewModel relationViewModel = (RelationViewModel) this.attribute.getRelation().getViewModel(); + int x = this.attribute.getRelation().getPosition().x + RelationViewModel.PADDING_X; + int y = this.attribute.getRelation().getPosition().y + relationViewModel.getNameBounds(g).height + RelationViewModel.ATTRIBUTE_SEPARATION; + int i = 0; + while (!this.attribute.getRelation().getAttributes().get(i).equals(this.attribute)) { + x += this.attribute.getRelation().getAttributes().get(i).getViewModel().getBounds(g).width; + i++; + } + Rectangle2D nameRect = g.getFontMetrics().getStringBounds(as.getIterator(), 0, this.attribute.getName().length(), g); + int width = (int) nameRect.getWidth() + (2 * PADDING_X); + int height = (int) nameRect.getHeight() + (2 * PADDING_Y); + if (this.attribute instanceof ForeignKeyAttribute) { + ForeignKeyAttribute fkAttribute = (ForeignKeyAttribute) this.attribute; + Font originalFont = g.getFont(); + g.setFont(g.getFont().deriveFont(Font.ITALIC, FK_FONT_SIZE)); + Rectangle referenceNameBounds = g.getFontMetrics().getStringBounds(fkAttribute.getFullReferenceName(), g).getBounds(); + g.setFont(originalFont); + width = Math.max(width, referenceNameBounds.width + (2 * PADDING_X)); + } + return new Rectangle(x, y, width, height); + } + + private AttributedString getAttributedString(Graphics2D g) { + AttributedString as = new AttributedString(this.attribute.getName()); + as.addAttribute(TextAttribute.FONT, g.getFont()); + if (this.attribute.getType().equals(AttributeType.ID_KEY)) { + as.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON); + } else if (this.attribute.getType().equals(AttributeType.PARTIAL_ID_KEY)) { + as.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_LOW_DASHED); + } + return as; + } } From 16c5d7b2d0d4ae3546d0287da28e820d3f8cd502 Mon Sep 17 00:00:00 2001 From: Bjorn Pijnacker Date: Tue, 23 Feb 2021 16:19:05 +0100 Subject: [PATCH 2/6] changed spaces to tabs I have honestly no idea why it wasn't autodetecting the use of tabs --- .../erme/EntityRelationMappingEditor.java | 46 ++--- .../erme/control/actions/LolcatAction.java | 30 ++-- .../view/view_models/AttributeViewModel.java | 166 +++++++++--------- 3 files changed, 121 insertions(+), 121 deletions(-) diff --git a/src/main/java/nl/andrewlalis/erme/EntityRelationMappingEditor.java b/src/main/java/nl/andrewlalis/erme/EntityRelationMappingEditor.java index e50e30b..48a8243 100644 --- a/src/main/java/nl/andrewlalis/erme/EntityRelationMappingEditor.java +++ b/src/main/java/nl/andrewlalis/erme/EntityRelationMappingEditor.java @@ -7,30 +7,30 @@ import nl.andrewlalis.erme.view.EditorFrame; import java.nio.charset.StandardCharsets; public class EntityRelationMappingEditor { - public static final String VERSION = "1.3.1"; - private static EditorFrame frame; + public static final String VERSION = "1.3.1"; + private static EditorFrame frame; - public static EditorFrame getFrame() { - return frame; - } + public static EditorFrame getFrame() { + return frame; + } - public static void main(String[] args) { - if (!FlatLightLaf.install()) { - System.err.println("Could not install FlatLight Look and Feel."); - } - final boolean includeAdminActions = shouldIncludeAdminActions(args); - if (includeAdminActions) { - System.out.println("Admin actions have been enabled."); - } - frame = new EditorFrame(includeAdminActions); - frame.setVisible(true); - } + public static void main(String[] args) { + if (!FlatLightLaf.install()) { + System.err.println("Could not install FlatLight Look and Feel."); + } + final boolean includeAdminActions = shouldIncludeAdminActions(args); + if (includeAdminActions) { + System.out.println("Admin actions have been enabled."); + } + frame = new EditorFrame(includeAdminActions); + frame.setVisible(true); + } - private static boolean shouldIncludeAdminActions(String[] args) { - if (args.length < 1) { - return false; - } - byte[] pw = args[0].getBytes(StandardCharsets.UTF_8); - return Hash.matches(pw, "admin_hash.txt"); - } + private static boolean shouldIncludeAdminActions(String[] args) { + if (args.length < 1) { + return false; + } + byte[] pw = args[0].getBytes(StandardCharsets.UTF_8); + return Hash.matches(pw, "admin_hash.txt"); + } } diff --git a/src/main/java/nl/andrewlalis/erme/control/actions/LolcatAction.java b/src/main/java/nl/andrewlalis/erme/control/actions/LolcatAction.java index 7fc7d94..d3a9925 100644 --- a/src/main/java/nl/andrewlalis/erme/control/actions/LolcatAction.java +++ b/src/main/java/nl/andrewlalis/erme/control/actions/LolcatAction.java @@ -7,21 +7,21 @@ import javax.swing.*; import java.awt.event.ActionEvent; public class LolcatAction extends AbstractAction { - private static LolcatAction instance; - public static LolcatAction getInstance() { - if (instance == null) { - instance = new LolcatAction(); - } - return instance; - } + private static LolcatAction instance; + public static LolcatAction getInstance() { + if (instance == null) { + instance = new LolcatAction(); + } + return instance; + } - public LolcatAction() { - super("Toggle Lolcat Mode"); - } + public LolcatAction() { + super("Toggle Lolcat Mode"); + } - @Override - public void actionPerformed(ActionEvent actionEvent) { - AttributeViewModel.setLolcatMode(((AbstractButton)actionEvent.getSource()).getModel().isSelected()); - EntityRelationMappingEditor.getFrame().getContentPane().repaint(); - } + @Override + public void actionPerformed(ActionEvent actionEvent) { + AttributeViewModel.setLolcatMode(((AbstractButton)actionEvent.getSource()).getModel().isSelected()); + EntityRelationMappingEditor.getFrame().getContentPane().repaint(); + } } diff --git a/src/main/java/nl/andrewlalis/erme/view/view_models/AttributeViewModel.java b/src/main/java/nl/andrewlalis/erme/view/view_models/AttributeViewModel.java index e82d6c6..595b866 100644 --- a/src/main/java/nl/andrewlalis/erme/view/view_models/AttributeViewModel.java +++ b/src/main/java/nl/andrewlalis/erme/view/view_models/AttributeViewModel.java @@ -15,98 +15,98 @@ import java.text.AttributedString; * View model for rendering a single attribute of a relation. */ public class AttributeViewModel implements ViewModel { - public static final int PADDING_X = 5; - public static final int PADDING_Y = 5; - public static final Color BACKGROUND_COLOR = Color.LIGHT_GRAY; - public static final Color FONT_COLOR = Color.BLACK; - public static final float FK_FONT_SIZE = 11.0f; - private static final float LOLCAT_SAT = 0.75f; - private static final float LOLCAT_BRIGHT = 1f; - private static boolean lolcatMode; - private final Attribute attribute; + public static final int PADDING_X = 5; + public static final int PADDING_Y = 5; + public static final Color BACKGROUND_COLOR = Color.LIGHT_GRAY; + public static final Color FONT_COLOR = Color.BLACK; + public static final float FK_FONT_SIZE = 11.0f; + private static final float LOLCAT_SAT = 0.75f; + private static final float LOLCAT_BRIGHT = 1f; + private static boolean lolcatMode; + private final Attribute attribute; - public AttributeViewModel(Attribute attribute) { - this.attribute = attribute; - } + public AttributeViewModel(Attribute attribute) { + this.attribute = attribute; + } - public static boolean getLolcatMode() { - return lolcatMode; - } + public static boolean getLolcatMode() { + return lolcatMode; + } - public static void setLolcatMode(boolean lolcatMode) { - AttributeViewModel.lolcatMode = lolcatMode; - } + public static void setLolcatMode(boolean lolcatMode) { + AttributeViewModel.lolcatMode = lolcatMode; + } - @Override - public void draw(Graphics2D g) { - AttributedString as = this.getAttributedString(g); - Rectangle r = this.getBounds(g, as); - g.setColor(this.getBackgroundColor(r.x + r.width / 2, r.y + r.height / 2, g)); - g.fillRect(r.x, r.y, r.width, r.height); - g.setColor(FONT_COLOR); - g.drawRect(r.x, r.y, r.width, r.height); - g.drawString(as.getIterator(), r.x + PADDING_X, r.y + (r.height - PADDING_Y)); - if (this.attribute instanceof ForeignKeyAttribute) { - ForeignKeyAttribute fkAttribute = (ForeignKeyAttribute) this.attribute; - Font originalFont = g.getFont(); - g.setFont(g.getFont().deriveFont(Font.ITALIC, FK_FONT_SIZE)); - g.drawString(fkAttribute.getFullReferenceName(), r.x + PADDING_X, r.y - PADDING_Y); - g.setFont(originalFont); - } - } + @Override + public void draw(Graphics2D g) { + AttributedString as = this.getAttributedString(g); + Rectangle r = this.getBounds(g, as); + g.setColor(this.getBackgroundColor(r.x + r.width / 2, r.y + r.height / 2, g)); + g.fillRect(r.x, r.y, r.width, r.height); + g.setColor(FONT_COLOR); + g.drawRect(r.x, r.y, r.width, r.height); + g.drawString(as.getIterator(), r.x + PADDING_X, r.y + (r.height - PADDING_Y)); + if (this.attribute instanceof ForeignKeyAttribute) { + ForeignKeyAttribute fkAttribute = (ForeignKeyAttribute) this.attribute; + Font originalFont = g.getFont(); + g.setFont(g.getFont().deriveFont(Font.ITALIC, FK_FONT_SIZE)); + g.drawString(fkAttribute.getFullReferenceName(), r.x + PADDING_X, r.y - PADDING_Y); + g.setFont(originalFont); + } + } - private Color getBackgroundColor(int x, int y, Graphics2D g) { - if (!lolcatMode) return BACKGROUND_COLOR; - Dimension viewportSize = ((DiagramPanel)EntityRelationMappingEditor.getFrame().getContentPane()).getModel().getRelationBounds().getSize(); + private Color getBackgroundColor(int x, int y, Graphics2D g) { + if (!lolcatMode) return BACKGROUND_COLOR; + Dimension viewportSize = ((DiagramPanel)EntityRelationMappingEditor.getFrame().getContentPane()).getModel().getRelationBounds().getSize(); - double dx = viewportSize.width; - double dy = viewportSize.height; - double mag = Math.sqrt(dx * dx + dy * dy); - dx /= mag; - dy /= mag; + double dx = viewportSize.width; + double dy = viewportSize.height; + double mag = Math.sqrt(dx * dx + dy * dy); + dx /= mag; + dy /= mag; - double lambda = (dx * x + dy * y); - double diag_val = Math.sqrt(Math.pow(dx * lambda, 2) + Math.pow(dy * lambda, 2)) / mag; + double lambda = (dx * x + dy * y); + double diag_val = Math.sqrt(Math.pow(dx * lambda, 2) + Math.pow(dy * lambda, 2)) / mag; - return Color.getHSBColor((float) diag_val, LOLCAT_SAT, LOLCAT_BRIGHT); - } + return Color.getHSBColor((float) diag_val, LOLCAT_SAT, LOLCAT_BRIGHT); + } - @Override - public Rectangle getBounds(Graphics2D g) { - return this.getBounds(g, this.getAttributedString(g)); - } + @Override + public Rectangle getBounds(Graphics2D g) { + return this.getBounds(g, this.getAttributedString(g)); + } - private Rectangle getBounds(Graphics2D g, AttributedString as) { - final RelationViewModel relationViewModel = (RelationViewModel) this.attribute.getRelation().getViewModel(); - int x = this.attribute.getRelation().getPosition().x + RelationViewModel.PADDING_X; - int y = this.attribute.getRelation().getPosition().y + relationViewModel.getNameBounds(g).height + RelationViewModel.ATTRIBUTE_SEPARATION; - int i = 0; - while (!this.attribute.getRelation().getAttributes().get(i).equals(this.attribute)) { - x += this.attribute.getRelation().getAttributes().get(i).getViewModel().getBounds(g).width; - i++; - } - Rectangle2D nameRect = g.getFontMetrics().getStringBounds(as.getIterator(), 0, this.attribute.getName().length(), g); - int width = (int) nameRect.getWidth() + (2 * PADDING_X); - int height = (int) nameRect.getHeight() + (2 * PADDING_Y); - if (this.attribute instanceof ForeignKeyAttribute) { - ForeignKeyAttribute fkAttribute = (ForeignKeyAttribute) this.attribute; - Font originalFont = g.getFont(); - g.setFont(g.getFont().deriveFont(Font.ITALIC, FK_FONT_SIZE)); - Rectangle referenceNameBounds = g.getFontMetrics().getStringBounds(fkAttribute.getFullReferenceName(), g).getBounds(); - g.setFont(originalFont); - width = Math.max(width, referenceNameBounds.width + (2 * PADDING_X)); - } - return new Rectangle(x, y, width, height); - } + private Rectangle getBounds(Graphics2D g, AttributedString as) { + final RelationViewModel relationViewModel = (RelationViewModel) this.attribute.getRelation().getViewModel(); + int x = this.attribute.getRelation().getPosition().x + RelationViewModel.PADDING_X; + int y = this.attribute.getRelation().getPosition().y + relationViewModel.getNameBounds(g).height + RelationViewModel.ATTRIBUTE_SEPARATION; + int i = 0; + while (!this.attribute.getRelation().getAttributes().get(i).equals(this.attribute)) { + x += this.attribute.getRelation().getAttributes().get(i).getViewModel().getBounds(g).width; + i++; + } + Rectangle2D nameRect = g.getFontMetrics().getStringBounds(as.getIterator(), 0, this.attribute.getName().length(), g); + int width = (int) nameRect.getWidth() + (2 * PADDING_X); + int height = (int) nameRect.getHeight() + (2 * PADDING_Y); + if (this.attribute instanceof ForeignKeyAttribute) { + ForeignKeyAttribute fkAttribute = (ForeignKeyAttribute) this.attribute; + Font originalFont = g.getFont(); + g.setFont(g.getFont().deriveFont(Font.ITALIC, FK_FONT_SIZE)); + Rectangle referenceNameBounds = g.getFontMetrics().getStringBounds(fkAttribute.getFullReferenceName(), g).getBounds(); + g.setFont(originalFont); + width = Math.max(width, referenceNameBounds.width + (2 * PADDING_X)); + } + return new Rectangle(x, y, width, height); + } - private AttributedString getAttributedString(Graphics2D g) { - AttributedString as = new AttributedString(this.attribute.getName()); - as.addAttribute(TextAttribute.FONT, g.getFont()); - if (this.attribute.getType().equals(AttributeType.ID_KEY)) { - as.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON); - } else if (this.attribute.getType().equals(AttributeType.PARTIAL_ID_KEY)) { - as.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_LOW_DASHED); - } - return as; - } + private AttributedString getAttributedString(Graphics2D g) { + AttributedString as = new AttributedString(this.attribute.getName()); + as.addAttribute(TextAttribute.FONT, g.getFont()); + if (this.attribute.getType().equals(AttributeType.ID_KEY)) { + as.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON); + } else if (this.attribute.getType().equals(AttributeType.PARTIAL_ID_KEY)) { + as.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_LOW_DASHED); + } + return as; + } } From 21ac8308966904ab040733e73b1d41b63e7028f6 Mon Sep 17 00:00:00 2001 From: Bjorn Pijnacker Date: Tue, 23 Feb 2021 18:19:01 +0100 Subject: [PATCH 3/6] changed the way the lolcat boolean is represented; changed the way LolcatAction accesses DiagramPanel; changed the way AttributeViewModel gets the window bounds --- .../erme/control/actions/ExportToImageAction.java | 6 +++--- .../erme/control/actions/LolcatAction.java | 15 +++++++++++++-- .../nl/andrewlalis/erme/view/DiagramPanel.java | 1 + .../erme/view/view_models/AttributeViewModel.java | 15 ++++----------- 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/main/java/nl/andrewlalis/erme/control/actions/ExportToImageAction.java b/src/main/java/nl/andrewlalis/erme/control/actions/ExportToImageAction.java index 7ddeb19..a040a3d 100644 --- a/src/main/java/nl/andrewlalis/erme/control/actions/ExportToImageAction.java +++ b/src/main/java/nl/andrewlalis/erme/control/actions/ExportToImageAction.java @@ -114,13 +114,13 @@ public class ExportToImageAction extends AbstractAction { DiagramPanel.prepareGraphics(g2d); // Render the model. - boolean lolcat = AttributeViewModel.getLolcatMode(); // save previous lolcat mode - AttributeViewModel.setLolcatMode(false); + boolean lolcat = LolcatAction.getInstance().isLolcatEnabled(); // save previous lolcat mode + LolcatAction.getInstance().setLolcatEnabled(false); List selectedRelations = this.model.getSelectedRelations(); this.model.getSelectedRelations().forEach(r -> r.setSelected(false)); new MappingModelViewModel(this.model).draw(g2d); this.model.getRelations().forEach(r -> r.setSelected(selectedRelations.contains(r))); - AttributeViewModel.setLolcatMode(lolcat); // revert previous lolcat mode + LolcatAction.getInstance().setLolcatEnabled(lolcat); // revert previous lolcat mode // Revert back to the normal image space, and render a watermark. g2d.setTransform(originalTransform); diff --git a/src/main/java/nl/andrewlalis/erme/control/actions/LolcatAction.java b/src/main/java/nl/andrewlalis/erme/control/actions/LolcatAction.java index d3a9925..b193c96 100644 --- a/src/main/java/nl/andrewlalis/erme/control/actions/LolcatAction.java +++ b/src/main/java/nl/andrewlalis/erme/control/actions/LolcatAction.java @@ -1,6 +1,9 @@ package nl.andrewlalis.erme.control.actions; +import lombok.Getter; +import lombok.Setter; import nl.andrewlalis.erme.EntityRelationMappingEditor; +import nl.andrewlalis.erme.view.DiagramPanel; import nl.andrewlalis.erme.view.view_models.AttributeViewModel; import javax.swing.*; @@ -8,6 +11,11 @@ import java.awt.event.ActionEvent; public class LolcatAction extends AbstractAction { private static LolcatAction instance; + + @Getter + @Setter + private boolean lolcatEnabled = false; + public static LolcatAction getInstance() { if (instance == null) { instance = new LolcatAction(); @@ -15,13 +23,16 @@ public class LolcatAction extends AbstractAction { return instance; } + @Setter + private DiagramPanel diagramPanel; + public LolcatAction() { super("Toggle Lolcat Mode"); } @Override public void actionPerformed(ActionEvent actionEvent) { - AttributeViewModel.setLolcatMode(((AbstractButton)actionEvent.getSource()).getModel().isSelected()); - EntityRelationMappingEditor.getFrame().getContentPane().repaint(); + lolcatEnabled = ((AbstractButton)actionEvent.getSource()).getModel().isSelected(); + diagramPanel.repaint(); } } diff --git a/src/main/java/nl/andrewlalis/erme/view/DiagramPanel.java b/src/main/java/nl/andrewlalis/erme/view/DiagramPanel.java index 8210b1e..54bc65e 100644 --- a/src/main/java/nl/andrewlalis/erme/view/DiagramPanel.java +++ b/src/main/java/nl/andrewlalis/erme/view/DiagramPanel.java @@ -128,6 +128,7 @@ public class DiagramPanel extends JPanel implements ModelChangeListener { AddAttributeAction.getInstance().setModel(this.model); RemoveAttributeAction.getInstance().setModel(this.model); LoadSampleModelAction.getInstance().setDiagramPanel(this); + LolcatAction.getInstance().setDiagramPanel(this); } public static void prepareGraphics(Graphics2D g) { diff --git a/src/main/java/nl/andrewlalis/erme/view/view_models/AttributeViewModel.java b/src/main/java/nl/andrewlalis/erme/view/view_models/AttributeViewModel.java index 595b866..6b32638 100644 --- a/src/main/java/nl/andrewlalis/erme/view/view_models/AttributeViewModel.java +++ b/src/main/java/nl/andrewlalis/erme/view/view_models/AttributeViewModel.java @@ -1,6 +1,7 @@ package nl.andrewlalis.erme.view.view_models; import nl.andrewlalis.erme.EntityRelationMappingEditor; +import nl.andrewlalis.erme.control.actions.LolcatAction; import nl.andrewlalis.erme.model.Attribute; import nl.andrewlalis.erme.model.AttributeType; import nl.andrewlalis.erme.model.ForeignKeyAttribute; @@ -22,21 +23,12 @@ public class AttributeViewModel implements ViewModel { public static final float FK_FONT_SIZE = 11.0f; private static final float LOLCAT_SAT = 0.75f; private static final float LOLCAT_BRIGHT = 1f; - private static boolean lolcatMode; private final Attribute attribute; public AttributeViewModel(Attribute attribute) { this.attribute = attribute; } - public static boolean getLolcatMode() { - return lolcatMode; - } - - public static void setLolcatMode(boolean lolcatMode) { - AttributeViewModel.lolcatMode = lolcatMode; - } - @Override public void draw(Graphics2D g) { AttributedString as = this.getAttributedString(g); @@ -56,8 +48,8 @@ public class AttributeViewModel implements ViewModel { } private Color getBackgroundColor(int x, int y, Graphics2D g) { - if (!lolcatMode) return BACKGROUND_COLOR; - Dimension viewportSize = ((DiagramPanel)EntityRelationMappingEditor.getFrame().getContentPane()).getModel().getRelationBounds().getSize(); + if (!LolcatAction.getInstance().isLolcatEnabled()) return BACKGROUND_COLOR; + Dimension viewportSize = g.getClipBounds().getSize(); double dx = viewportSize.width; double dy = viewportSize.height; @@ -67,6 +59,7 @@ public class AttributeViewModel implements ViewModel { double lambda = (dx * x + dy * y); double diag_val = Math.sqrt(Math.pow(dx * lambda, 2) + Math.pow(dy * lambda, 2)) / mag; + System.out.println(diag_val); return Color.getHSBColor((float) diag_val, LOLCAT_SAT, LOLCAT_BRIGHT); } From c84d9b138acedbc8f09d4d5b17621585d832bc9d Mon Sep 17 00:00:00 2001 From: Bjorn Pijnacker Date: Tue, 23 Feb 2021 18:49:54 +0100 Subject: [PATCH 4/6] changed the way color location is calculated --- .../view/view_models/AttributeViewModel.java | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/main/java/nl/andrewlalis/erme/view/view_models/AttributeViewModel.java b/src/main/java/nl/andrewlalis/erme/view/view_models/AttributeViewModel.java index 6b32638..48617ed 100644 --- a/src/main/java/nl/andrewlalis/erme/view/view_models/AttributeViewModel.java +++ b/src/main/java/nl/andrewlalis/erme/view/view_models/AttributeViewModel.java @@ -49,19 +49,25 @@ public class AttributeViewModel implements ViewModel { private Color getBackgroundColor(int x, int y, Graphics2D g) { if (!LolcatAction.getInstance().isLolcatEnabled()) return BACKGROUND_COLOR; + Point offset = g.getClipBounds().getLocation(); + g.translate(offset.x, offset.y); Dimension viewportSize = g.getClipBounds().getSize(); + g.drawRect(0, 0, viewportSize.width, viewportSize.height); + g.fillRect(-5, -5, 10, 10); - double dx = viewportSize.width; - double dy = viewportSize.height; - double mag = Math.sqrt(dx * dx + dy * dy); - dx /= mag; - dy /= mag; + double diagonal_slope = (double) viewportSize.width / (double) viewportSize.height; + double perp_slope = -1f / diagonal_slope; - double lambda = (dx * x + dy * y); - double diag_val = Math.sqrt(Math.pow(dx * lambda, 2) + Math.pow(dy * lambda, 2)) / mag; - System.out.println(diag_val); + double perp_offset = y - perp_slope * x; - return Color.getHSBColor((float) diag_val, LOLCAT_SAT, LOLCAT_BRIGHT); + double x_intersect = perp_offset / (diagonal_slope - perp_slope); + double y_intersect = diagonal_slope * (perp_offset / (diagonal_slope - perp_slope)); + + double total_dist = Math.sqrt(viewportSize.height * viewportSize.height + viewportSize.width * viewportSize.width); + double dist_frac = Math.sqrt(x_intersect * x_intersect + y_intersect * y_intersect) / total_dist; + + g.translate(-offset.x, -offset.y); + return Color.getHSBColor((float) dist_frac, LOLCAT_SAT, LOLCAT_BRIGHT); } @Override From b84ca5deafd282ad40057e679c71d626efdfb466 Mon Sep 17 00:00:00 2001 From: Bjorn Pijnacker Date: Tue, 23 Feb 2021 18:51:46 +0100 Subject: [PATCH 5/6] removed static Frame in main class --- .../nl/andrewlalis/erme/EntityRelationMappingEditor.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/main/java/nl/andrewlalis/erme/EntityRelationMappingEditor.java b/src/main/java/nl/andrewlalis/erme/EntityRelationMappingEditor.java index 48a8243..b9cafa6 100644 --- a/src/main/java/nl/andrewlalis/erme/EntityRelationMappingEditor.java +++ b/src/main/java/nl/andrewlalis/erme/EntityRelationMappingEditor.java @@ -8,11 +8,6 @@ import java.nio.charset.StandardCharsets; public class EntityRelationMappingEditor { public static final String VERSION = "1.3.1"; - private static EditorFrame frame; - - public static EditorFrame getFrame() { - return frame; - } public static void main(String[] args) { if (!FlatLightLaf.install()) { @@ -22,7 +17,7 @@ public class EntityRelationMappingEditor { if (includeAdminActions) { System.out.println("Admin actions have been enabled."); } - frame = new EditorFrame(includeAdminActions); + EditorFrame frame = new EditorFrame(includeAdminActions); frame.setVisible(true); } From fb4ae1824232e1c85f92b9ddee223e526964d682 Mon Sep 17 00:00:00 2001 From: Bjorn Pijnacker Date: Tue, 23 Feb 2021 18:53:59 +0100 Subject: [PATCH 6/6] added description to lolcat toggle item --- .../java/nl/andrewlalis/erme/control/actions/LolcatAction.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/nl/andrewlalis/erme/control/actions/LolcatAction.java b/src/main/java/nl/andrewlalis/erme/control/actions/LolcatAction.java index b193c96..f2ffc74 100644 --- a/src/main/java/nl/andrewlalis/erme/control/actions/LolcatAction.java +++ b/src/main/java/nl/andrewlalis/erme/control/actions/LolcatAction.java @@ -28,6 +28,7 @@ public class LolcatAction extends AbstractAction { public LolcatAction() { super("Toggle Lolcat Mode"); + this.putValue(SHORT_DESCRIPTION, "Does some wacky color stuff."); } @Override