{"id":1733,"date":"2016-12-22T14:04:05","date_gmt":"2016-12-22T14:04:05","guid":{"rendered":"http:\/\/mindfusion.eu\/blog\/?p=1733"},"modified":"2021-01-21T14:06:17","modified_gmt":"2021-01-21T14:06:17","slug":"a-class-diagram-tool-in-java-with-the-flowchart-library-i","status":"publish","type":"post","link":"https:\/\/mindfusion.dev\/blog\/a-class-diagram-tool-in-java-with-the-flowchart-library-i\/","title":{"rendered":"A Class Diagram Tool in Java with the Flowchart Library &#8211; I"},"content":{"rendered":"<p>This blog post is a step-by-step guide on how to create a tool that parses *.jar files and builds the class hierarchy. The visualization of the diagram is performed by <a href=\"http:\/\/mindfusion.dev\/java-diagram.html\">MindFusion Java Swing Diagram library.<\/a><\/p>\n<p>Here is an image of the final application:<\/p>\n<div id=\"attachment_1738\" style=\"width: 843px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2016\/12\/java-class-diagram.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1738\" class=\"size-full wp-image-1738\" src=\"http:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2016\/12\/java-class-diagram.png\" alt=\"Class Library Tool in Java\" width=\"833\" height=\"562\" srcset=\"https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2016\/12\/java-class-diagram.png 833w, https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2016\/12\/java-class-diagram-300x202.png 300w, https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2016\/12\/java-class-diagram-768x518.png 768w, https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2016\/12\/java-class-diagram-445x300.png 445w\" sizes=\"auto, (max-width: 833px) 100vw, 833px\" \/><\/a><p id=\"caption-attachment-1738\" class=\"wp-caption-text\">Class Library Tool in Java<\/p><\/div>\n<p>In part one we will take a look at the controls that build the user interface for the application.<\/p>\n<p><strong>I. UI Controls<\/strong><\/p>\n<p>We will use three controls from the diagram library:<\/p>\n<pre>    private Diagram diagram;\n    private DiagramView diagramView;\n    private ZoomControl zoomer;\n<\/pre>\n<p>One <a href=\"https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/javax\/swing\/JScrollPane.html\">JScrollPane<\/a>:<\/p>\n<pre>private JScrollPane _scrollPane;<\/pre>\n<p>and a <a href=\"https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/javax\/swing\/JPanel.html\">JPanel<\/a> for the legend and a <a href=\"https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/javax\/swing\/JMenuBar.html\">JMenuBar<\/a>.<\/p>\n<pre>    JPanel controlsPanel = new JPanel();\n    JMenuBar menuBar;\n<\/pre>\n<p>Those controls build the user interface. At the top is the menu bar with menus for handling the *.jar files. In the center is a scrollable area that contains the diagram. Right to it is a zoom control. At the bottom is the panel with the legend \u2013 images and text that explain the colors and symbols on the class diagram.<\/p>\n<p><strong>II. The Diagram Controls<\/strong><\/p>\n<p>The three diagram controls are the <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/jdiagram\/index.htm?T_com_mindfusion_diagramming_Diagram_Members.htm\">Diagram<\/a>, the <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/jdiagram\/index.htm?T_com_mindfusion_diagramming_DocumentView_Members.htm\">DiagramView<\/a> and the <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/jdiagram\/index.htm?T_com_mindfusion_diagramming_ZoomControl.htm\">ZoomControl<\/a>. The diagram needs a diagramView to render itself onto. The diagramView users a scrollPane to provide scroll functionality for the flowchart. The zoomControl is a typical <a href=\"https:\/\/en.wikipedia.org\/wiki\/Swing_(Java)\">Java Swing<\/a> control, we set a few customization options on it to make it pass the layout of our application.<\/p>\n<pre>       \/\/diagram initialization\n        diagram = new Diagram();\n        diagram.setAutoResize(AutoResize.RightAndDown);\n<\/pre>\n<p>We set auto resize for the diagram and assign it to the diagramView:<\/p>\n<pre>        \/\/initialize a diagramView that will render the diagram.\n        diagramView = new DiagramView(diagram);\n        diagramView.setVisible(true);\n<\/pre>\n<p>The scrollPane is initialized with the diagramView and scrolls automatically when the view is bigger than the available size:<\/p>\n<pre>         \/\/use a scroll pane to host large diagrams\n        _scrollPane = new JScrollPane(diagramView);\n        _scrollPane.setVisible(true);\n        _scrollPane.setAutoscrolls(true);\n<\/pre>\n<p>The zoomControl is also attached to the diagramView. It\u2019s important that we set its Dimension, the width will be used by the Java layout manager to calculate the available space for it on the application.<\/p>\n<pre>        \/\/provide a zoomer for the diagram\n        zoomer = new ZoomControl();\n        zoomer.setView(diagramView);\n        zoomer.setPreferredSize(new Dimension(70, 50));\n        zoomer.setVisible(true); \n<\/pre>\n<p>The arrangement of the controls in the <a href=\"https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/javax\/swing\/JFrame.html\">JFrame<\/a> is done with the <a href=\"https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/awt\/BorderLayout.html\">BorderLayout<\/a>. It\u2019s important that we set the layout before we start adding the controls:<\/p>\n<pre>     getContentPane().setLayout(new BorderLayout());\n     this.add(zoomer, BorderLayout.EAST);\n     this.add(createLegendPanel(), BorderLayout.SOUTH);\n<\/pre>\n<p>The zoomer is to the right, the legend panel is at the bottom. The last control that we add is the scrollPane with the diagram, we align it to the center, which means that all the available space would be allocated to her.<\/p>\n<pre>     this.add(_scrollPane, BorderLayout.CENTER);\n<\/pre>\n<p>Finally, we create the menu bar.<\/p>\n<pre>     this.setJMenuBar(createMenuBar());\n<\/pre>\n<p><strong>III. The Legend Panel<\/strong><\/p>\n<p>The legend panel is a JPanel with BoxLayout of type \u201cLINE_AXIS\u201d.<\/p>\n<pre>        JPanel controlsPanel = new JPanel();\n        controlsPanel.setLayout(new BoxLayout(controlsPanel, BoxLayout.LINE_AXIS));\n        controlsPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));\n        controlsPanel.add(Box.createHorizontalGlue());\n<\/pre>\n<p>The legend items are <a href=\"https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/javax\/swing\/ImageIcon.html\">ImageIcon<\/a>-s &#8211; for each symbol in the diagram there\u2019s an icon. We have also created images for the colors of the diagram. Each icon is rendered with explanation label.<\/p>\n<p>Generally, each item on the LegendPanel is initialized like this:<\/p>\n<pre>        \/\/create labels for each item on the legend\n        JLabel label = new JLabel(\"Constructor\", constructorIcon, JLabel.CENTER);\n        label.setAlignmentX(JComponent.CENTER_ALIGNMENT);\n        controlsPanel.add(label);\n        controlsPanel.add(Box.createRigidArea(new Dimension(10, 0)));\n        controlsPanel.add(Box.createHorizontalGlue());\n<\/pre>\n<p>The layout distributes evenly the available space between the items and we get an easy-to-read legend at the bottom of the application.<\/p>\n<p><strong>IV. The Menu<\/strong><\/p>\n<p>The menu at the top is implemented as a <a href=\"https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/javax\/swing\/JMenuBar.html\">JMenuBar<\/a>, which uses mnemonic keys and accelerators to grant access to the menu items with keyboard shortcuts. The only menu item and submenu items currently present are File -&gt; Open jar.<\/p>\n<pre>        \/\/Build the first menu.\n        menu = new JMenu(\"File\");\n        menu.setMnemonic(KeyEvent.VK_F);\n        menu.getAccessibleContext().setAccessibleDescription(\n                \"File operations\");\n        menuBar.add(menu);\n<\/pre>\n<p>The \u201cOpen jar\u201d command uses action listener, which brings up the Open File diagolg. This is a <a href=\"https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/javax\/swing\/JFileChooser.html\">JFileChooser<\/a>, which filters all files except *.jar files.<\/p>\n<pre>     \/\/the method that handles events\n    public void actionPerformed(ActionEvent e)\n    {\n        \/\/identify the command\n        if (\"open_jar\".equals(e.getActionCommand())) {\n\n            \/\/a list with MemberInfo objects that hold class info\n            ArrayList membersList = new ArrayList();\n            fileChooser = new JFileChooser();\n\n            \/\/set the default directory to this file's directory\n            fileChooser.setCurrentDirectory(currFile);\n\n            \/\/filter only *.jar files\n            FileFilter filter = new FileNameExtensionFilter(null, \"jar\");\n            fileChooser.setFileFilter(filter);\n            fileChooser.removeChoosableFileFilter(fileChooser.getAcceptAllFileFilter());\n            .......\n<\/pre>\n<p>If the method confirms that the user has selected a valid jar, the path to the file is provided to the method that reads and parses the jar, which will be topic for the second part of this tutorial.<\/p>\n<p>The whole sample is available for direct download from this link:<\/p>\n<p><a href=\"https:\/\/mindfusion.dev\/samples\/java\/diagram\/JarClassDiagram.zip\">Download the Class Diagram Tool in Java Application<\/a><\/p>\n<p>MindFusion support team welcomes your questions about the Java diagram library or any other of our programming tools at the <a href=\"http:\/\/mindfusion.dev\/Forum\/YaBB.pl\">discussion board<\/a> or per e-mail at <a title=\"E-mail\" href=\"mailto:support@mindfusion.dev?subject=MindFusion product inquiry&amp;body=Please, remember to add mindfusion.dev to your email whitelist. We usually reply to all contact inquiries within 3 to 4 hours. In case you have not received an answer in 24 hours, the email most likely has been filtered by an anti-spam software running at your mail server. Thank you for the understanding!\">support@mindfusion.dev<\/a><\/p>\n<p><em>About Diagramming for Java Swing:<\/em> MindFusion.Diagramming for Java Swing provides your Java application with all necessary functionality to create and customize a diagram. The library is very easy to integrate and program. There are numerous utility methods, rich event set, more than 100 predefined shapes. The tool supports a variety of ways to render or export the diagram, advanced node types like TreeView nodes, hierarchical nodes, tables, container nodes and many more. There are 15 automatic layouts, various input \/ output options and fully customizable appearance. A detailed list with JDiagram\u2019s features is uploaded <a href=\"http:\/\/www.mindfusion.dev\/features-jdiagram.html\">here.<\/a> You can check <a href=\"http:\/\/mindfusion.dev\/demos\/jdiagram\/start.htm\">the online demo<\/a> to see some of the functionality implemented.<\/p>\n<p>Diagramming for Java Swing is royalty free, there are no distribution fees. Licenses depend on the count of developers using the tool \u2013 check <a href=\"http:\/\/www.mindfusion.dev\/buy-jdiagram.html\">here<\/a> the prices.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This blog post is a step-by-step guide on how to create a tool that parses *.jar files and builds the class hierarchy. The visualization of the diagram is performed by MindFusion Java Swing Diagram library. Here is an image of &hellip; <a href=\"https:\/\/mindfusion.dev\/blog\/a-class-diagram-tool-in-java-with-the-flowchart-library-i\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[95,74],"tags":[448,447,433],"class_list":["post-1733","post","type-post","status-publish","format-standard","hentry","category-diagramming-2","category-sample-code","tag-java-class-diagram","tag-java-class-library","tag-java-diagram-library"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3RlKs-rX","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/1733","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/comments?post=1733"}],"version-history":[{"count":9,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/1733\/revisions"}],"predecessor-version":[{"id":2612,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/1733\/revisions\/2612"}],"wp:attachment":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/media?parent=1733"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/categories?post=1733"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/tags?post=1733"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}