From 2343d0140d3d59e9de0c55947506c59a2d93439c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anian=20B=C3=BChler?= <anian.buehler@reutlingen-university.de> Date: Fri, 6 Aug 2021 14:57:09 +0200 Subject: [PATCH] added to refrerence to context menu --- .../codeblockutil/CHoverScrollPane.java | 73 ++++++++++++ .../mit/blocks/codeblockutil/CScrollPane.java | 2 - .../edu/mit/blocks/workspace/BlockCanvas.java | 6 +- .../edu/mit/blocks/workspace/ContextMenu.java | 111 +++++++++++++++++- 4 files changed, 184 insertions(+), 8 deletions(-) diff --git a/src/main/java/edu/mit/blocks/codeblockutil/CHoverScrollPane.java b/src/main/java/edu/mit/blocks/codeblockutil/CHoverScrollPane.java index 8de97cc..398ad33 100644 --- a/src/main/java/edu/mit/blocks/codeblockutil/CHoverScrollPane.java +++ b/src/main/java/edu/mit/blocks/codeblockutil/CHoverScrollPane.java @@ -25,6 +25,7 @@ import javax.swing.KeyStroke; import javax.swing.ScrollPaneConstants; import edu.mit.blocks.codeblockutil.CScrollPane.ScrollPolicy; +import edu.mit.blocks.workspace.Workspace; /** * The CHoverScrollPane is a swing-compatible widget that @@ -52,6 +53,12 @@ public class CHoverScrollPane extends CScrollPane implements KeyListener { private ScrollPolicy vpolicy; private ScrollPolicy hpolicy; private int thumbWidth; + private Workspace workspace = null; + + /**workspace zoom settings */ + private static final double WORKSPACE_MIN_ZOOM = 0.6; + private static final double WORKSPACE_MAX_ZOOM = 3.0; + private static final double WORKSPACE_ZOOM_STEPSIZE = 0.1; /** * Constructs a custom CHoverScrollPane with the view port set to "view", @@ -110,6 +117,51 @@ public class CHoverScrollPane extends CScrollPane implements KeyListener { ScrollPolicy.HORIZONTAL_BAR_ALWAYS, thumbWidth, thumbColor, trackColor); } + + //TODO TEST letsgoING + /** + * Constructs a custom CHoverScrollPane with the view port set to "view", + * with correponding vertical and horizontal bar policies (see + * javax.swing.JScrollPane for a description on the use of + * scroll bar policies). The thumb will have a girth equal to + * "thumbWidth" and an interior color of thumbColor. The background + * underneath the thumb will have a color equal to thumbBackground. + * + * @param view - the viewport + * @param verticalPolicy - the vertical scroll bar policy + * @param horizontalPolicy - the horizontal scroll bar policy + * @param thumbWidth - the width of the vertical scroll bar in pixels and + * the height of the horizotntal scroll bar in pixels + * @param thumbColor - the interior color of the thumb + * @param trackColor - the background color under the thumb + * @param workspace - current workspace to zoom with mousewheel + * + * @requires view != null + * @effects Creates a JScrollPane that displays the view component + * in a viewport whose view position can be controlled with + * a pair of scrollbars. + * -If the scrollbar policies are null, then it will use the default + * "ALWAYS" policy. That is, the scroll bars will always show. + * -If the thumbWidth is null or less than 0, then the scroll bars + * will not show. + * -If thumbColor is null, then thumbs will default on Color.black. + * -If trackColor is null, then the default grayed-out transparent color + * will be used as the background color. + */ + public CHoverScrollPane( + JComponent view, + ScrollPolicy verticalPolicy, + ScrollPolicy horizontalPolicy, + int thumbWidth, + Color thumbColor, + Color trackColor, Workspace workspace) { + + this(view, verticalPolicy, + horizontalPolicy, + thumbWidth, thumbColor, trackColor); + + this.workspace = workspace; + } /** * Constructs a custom CHoverScrollPane with the view port set to "view", @@ -277,6 +329,10 @@ public class CHoverScrollPane extends CScrollPane implements KeyListener { this.SCROLLINGUNIT = x; this.verticalbar.setScrollingUnit(x); } + + public void setWorkspace(Workspace workspace) { + this.workspace = workspace; + } /** * @overrides CScrollPane.mouseWheelMoved @@ -295,6 +351,23 @@ public class CHoverScrollPane extends CScrollPane implements KeyListener { scrollviewport.getHorizontalScrollBar().getModel().getValue() + e.getUnitsToScroll() * e.getScrollAmount() * SCROLLINGUNIT); horizontalbar.repaint(); + } + //TODO: TEST ZOOM + else if(e.isControlDown()) { + if(workspace != null) { + if(e.isControlDown()) { + if(e.getUnitsToScroll() > 0) { + if(workspace.getCurrentWorkspaceZoom() > WORKSPACE_MIN_ZOOM) { + workspace.setWorkspaceZoom(workspace.getCurrentWorkspaceZoom()-WORKSPACE_ZOOM_STEPSIZE); + } + } + else { + if(workspace.getCurrentWorkspaceZoom() < WORKSPACE_MAX_ZOOM) { + workspace.setWorkspaceZoom(workspace.getCurrentWorkspaceZoom()+WORKSPACE_ZOOM_STEPSIZE); + } + } + } + } } else { scrollviewport.getVerticalScrollBar().getModel().setValue( scrollviewport.getVerticalScrollBar().getModel().getValue() diff --git a/src/main/java/edu/mit/blocks/codeblockutil/CScrollPane.java b/src/main/java/edu/mit/blocks/codeblockutil/CScrollPane.java index a44fc2e..cf2213b 100644 --- a/src/main/java/edu/mit/blocks/codeblockutil/CScrollPane.java +++ b/src/main/java/edu/mit/blocks/codeblockutil/CScrollPane.java @@ -7,7 +7,6 @@ import java.awt.event.MouseWheelListener; import javax.swing.BoundedRangeModel; import javax.swing.JLayeredPane; - /** * The CScrollPane is a swing-compatible widget that * allows clients of this CScrollPane to control the @@ -69,7 +68,6 @@ public abstract class CScrollPane extends JLayeredPane implements MouseWheelList * @effects set this.scrollingunit to x */ abstract public void setScrollingUnit(int x); - /** * MouseWheelListener: Should move the viewport by same amount of wheel scroll */ diff --git a/src/main/java/edu/mit/blocks/workspace/BlockCanvas.java b/src/main/java/edu/mit/blocks/workspace/BlockCanvas.java index d63c83a..f4a72d3 100644 --- a/src/main/java/edu/mit/blocks/workspace/BlockCanvas.java +++ b/src/main/java/edu/mit/blocks/workspace/BlockCanvas.java @@ -50,7 +50,7 @@ public class BlockCanvas implements PageChangeListener, ISupportMemento { /** serial version ID */ @SuppressWarnings("unused") - private static final long serialVersionUID = 7458721329L; + private static final long serialVersionUID = 7458721329L; /** the collection of pages that this BlockCanvas stores */ private List<Page> pages = new ArrayList<Page>(); /** the collection of PageDivideres that this BlockCanvas stores */ @@ -63,6 +63,7 @@ public class BlockCanvas implements PageChangeListener, ISupportMemento { private final Workspace workspace; private boolean collapsible = false; + ////////////////////////////// //Constructor/Destructor // @@ -77,7 +78,7 @@ public class BlockCanvas implements PageChangeListener, ISupportMemento { this.scrollPane = new CHoverScrollPane(canvas, ScrollPolicy.VERTICAL_BAR_ALWAYS, ScrollPolicy.HORIZONTAL_BAR_ALWAYS, - 18, CGraphite.blue, null); + 18, CGraphite.blue, null, workspace); scrollPane.setScrollingUnit(5); canvas.setLayout(null); canvas.setBackground(Color.gray); @@ -566,6 +567,7 @@ public class BlockCanvas implements PageChangeListener, ISupportMemento { private static final long serialVersionUID = 438974092314L; private Point p; + public Canvas() { super(); this.p = null; diff --git a/src/main/java/edu/mit/blocks/workspace/ContextMenu.java b/src/main/java/edu/mit/blocks/workspace/ContextMenu.java index d5208fb..147bc01 100644 --- a/src/main/java/edu/mit/blocks/workspace/ContextMenu.java +++ b/src/main/java/edu/mit/blocks/workspace/ContextMenu.java @@ -1,11 +1,33 @@ package edu.mit.blocks.workspace; +import java.awt.BorderLayout; +import java.awt.Desktop; +import java.awt.Frame; import java.awt.MenuItem; import java.awt.PopupMenu; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ResourceBundle; +import javax.print.DocFlavor.URL; +import javax.swing.JEditorPane; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JOptionPane; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JTextArea; +import javax.swing.text.Document; +import javax.swing.text.EditorKit; +import javax.swing.text.html.HTMLEditorKit; + import edu.mit.blocks.renderable.RenderableBlock; /** @@ -31,10 +53,12 @@ public class ContextMenu extends PopupMenu implements ActionListener { private static boolean removeCommentMenuInit = false; private final static String CLONE_BLOCK = "CLONE"; //heqichen private final static String CLONE_BLOCKS = "CLONEALL"; //letsgoING - private static MenuItem cloneItem1 = null; //heqichen - private static MenuItem cloneItem2 = null; //heqichen - private static MenuItem cloneAllItem1 = null; //heqichen - private static MenuItem cloneAllItem2 = null; //heqichen + private final static String OPEN_REFERENCE = "OPEN_REFERENCE"; + private static MenuItem cloneItem1 = null; //letsgoING + private static MenuItem cloneItem2 = null; //letsgoING + private static MenuItem cloneAllItem1 = null; //letsgoING + private static MenuItem cloneAllItem2 = null; //letsgoING + private static MenuItem refrenceItem = null; //letsgoING //context menu for canvas plus //menu items for canvas context menu private static ContextMenu canvasMenu = new ContextMenu(); @@ -70,6 +94,11 @@ public class ContextMenu extends PopupMenu implements ActionListener { cloneAllItem1.addActionListener(rndBlockMenu); addCommentMenu.add(cloneAllItem1); + refrenceItem = new MenuItem(uiMessageBundle.getString("ardublock.ui.reference")); + refrenceItem.setActionCommand(OPEN_REFERENCE); + refrenceItem.addActionListener(rndBlockMenu); + addCommentMenu.add(refrenceItem); + addCommentMenuInit = true; } @@ -97,6 +126,11 @@ public class ContextMenu extends PopupMenu implements ActionListener { cloneAllItem2.setActionCommand(CLONE_BLOCKS); cloneAllItem2.addActionListener(rndBlockMenu); removeCommentMenu.add(cloneAllItem2); + + refrenceItem = new MenuItem(uiMessageBundle.getString("ardublock.ui.reference")); + refrenceItem.setActionCommand(OPEN_REFERENCE); + refrenceItem.addActionListener(rndBlockMenu); + removeCommentMenu.add(refrenceItem); removeCommentMenuInit = true; } @@ -148,6 +182,69 @@ public class ContextMenu extends PopupMenu implements ActionListener { } return null; } + + /** + * opens reference-file (html) for active block + * in standard browser + * @param blockGenusName = genus name of the active block + * added by letsgoING + */ + private void createReferenceWindow(String blockGenusName) { + //TODO: Test with reference files + String resourcePath = "/com/ardublock/reference/"+blockGenusName+".html"; + + System.out.println("TEST | Active Block: "+blockGenusName); + + //TODO: call 404.html when there is no reference-file for active block + try { + Desktop.getDesktop().browse(getClass().getResource(resourcePath).toURI()); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (URISyntaxException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NullPointerException e) { + e.printStackTrace(); + } + + /* + //get html path + String referenceText = "Block not found!"; + Path path = null; + try { + path = Paths.get(getClass().getResource(resourcePath).toURI()); + } catch (URISyntaxException e1 ) { + // TODO Auto-generated catch block + //e1.printStackTrace(); + } catch (NullPointerException ne) { + //ne.printStackTrace(); + } + + if(path != null) { + try { + referenceText = new String(Files.readAllBytes(path)); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + //OPTION JFRAME + Frame frame = new JFrame("Block Reference"); + JLabel label = new JLabel(referenceText); + frame.add(label); + frame.setLocationRelativeTo( null ); + frame.pack(); + frame.setVisible(true); + + //OPTION DIALOG + JOptionPane.showMessageDialog( + null, + referenceText, + "Block-Referenz - "+((RenderableBlock) activeComponent).getBlock().getGenusName(), JOptionPane.INFORMATION_MESSAGE, null); + */ + return; + } public void actionPerformed(ActionEvent a) { if (a.getActionCommand() == ARRANGE_ALL_BLOCKS) { @@ -177,5 +274,11 @@ public class ContextMenu extends PopupMenu implements ActionListener { ((RenderableBlock) activeComponent).cloneMe(false); } } + else if (a.getActionCommand() == OPEN_REFERENCE) { + //notify the renderableblock componenet that lauched the conetxt menu + if (activeComponent != null && activeComponent instanceof RenderableBlock) { + createReferenceWindow(((RenderableBlock) activeComponent).getBlock().getGenusName()); + } + } } } -- GitLab