Java Swing - PUmlViewerMulti
package jlib5.app; import jlib5.swing.Dock; import jlib5.swing.Drop; import jlib5.swing.ImageView; import com.formdev.flatlaf.FlatLightLaf; import java.awt.BorderLayout; import java.awt.Component; import java.awt.Frame; import java.awt.event.ActionEvent; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.List; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JToolBar; import javax.swing.SwingUtilities; import jlib5.java.FileApi; import jlib5.java.PUmlApi; import jlib5.swing.Swing; import jlib5.swing.TextView; import net.infonode.docking.View; public final class PUmlViewerMulti { Dock dock; javax.swing.JFrame frame; ImageView imageView; TextView textView; List<BufferedImage> images; int imageIndex = 0; JLabel statusText; public static void main(String[] args) throws Exception { FlatLightLaf.setup(); SwingUtilities.invokeLater(new Runnable() { public void run() { new PUmlViewerMulti(); } }); } PUmlViewerMulti() { createViews(); showFrame(); } final void createViews() { boolean useToolbar = true; textView = new TextView(); imageView = new ImageView(); JPanel vbox = Swing.getVbox(); if( useToolbar ) { JToolBar toolBar = new JToolBar(); toolBar.add(Swing.getButton("<",null, this::prevImage ) ); toolBar.add(Swing.getButton(">",null, this::nextImage ) ); toolBar.add(Swing.getButton("(0)",null,() -> { imageView.updateImage("original"); } ) ); toolBar.add(Swing.getButton("(+)",null,() -> { imageView.updateImage("increase"); } ) ); toolBar.add(Swing.getButton("(-)",null,() -> { imageView.updateImage("decrease"); } ) ); toolBar.add(Swing.getButton("(F)",null,() -> { imageView.updateImage("fit"); } ) ); toolBar.add(Swing.getButton("(W)",null,() -> { imageView.updateImage("width"); } ) ); toolBar.add(Swing.getButton("(H)",null,() -> { imageView.updateImage("height"); } ) ); toolBar.setAlignmentX(Component.LEFT_ALIGNMENT); vbox.add(toolBar); } else { JPanel hbox = Swing.getHbox(Swing.getButton("<",null, this::prevImage ), Swing.getButton(">",null, this::nextImage ), Swing.getButton("(0)",null,() -> { imageView.updateImage("original"); } ), Swing.getButton("(+)",null,() -> { imageView.updateImage("increase"); } ), Swing.getButton("(-)",null,() -> { imageView.updateImage("decrease"); } ), Swing.getButton("(F)",null,() -> { imageView.updateImage("fit"); } ), Swing.getButton("(W)",null,() -> { imageView.updateImage("width"); } ), Swing.getButton("(H)",null,() -> { imageView.updateImage("height"); } )); vbox.add(hbox); } vbox.add( imageView.getLayout() ); textView.getCtrl().setTransferHandler(new Drop(this::fileDropHandler) ); dock = new Dock(); View txtView = dock.addToViewMap("Script", null, textView.getLayout()); View imgView = dock.addToViewMap("Image", null, vbox ); dock.createRootWindow(); dock.getRootWindow().setWindow(dock.getHSplit(0.4f, txtView, imgView)); } void showFrame() { frame = new JFrame("PlantUml Viewer"); frame.getContentPane().add(createToolBar(), BorderLayout.NORTH); frame.getContentPane().add(dock.getRootWindow(), BorderLayout.CENTER); frame.getContentPane().add(statusText = new JLabel("Ready"), BorderLayout.SOUTH); frame.setJMenuBar(createMenuBar()); frame.setSize(900, 700); frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } //--------------------------------------------------------------------------- public File fileOpenDialog(Frame frame) { JFileChooser fileChooser = new JFileChooser(); fileChooser.setCurrentDirectory(new File(System.getProperty("user.home"))); int result = fileChooser.showOpenDialog(frame); if (result == JFileChooser.APPROVE_OPTION) { return fileChooser.getSelectedFile(); } return null; } JToolBar createToolBar() { JToolBar toolBar = new JToolBar(); toolBar.add(Swing.getButton("Render",null,this::render) ); return toolBar; } JMenuBar createMenuBar() { JMenuBar menu = new JMenuBar(); menu.add(createFileMenu()); return menu; } JMenu createFileMenu() { JMenu menu = new JMenu("File"); JMenuItem item = new JMenuItem("Exit"); menu.add(item).addActionListener((ActionEvent e) -> { System.exit(0); }); return menu; } //--------------------------------------------------------------------------- boolean fileDropHandler(Object...obj) { java.util.List<File> files = (java.util.List<File>) obj[0]; try { textView.getCtrl().setText(FileApi.fileToString(files.get(0).getAbsolutePath())); } catch (IOException ignore) { } return true; } void fileOpen() { imageView.loadImage(fileOpenDialog(frame)); } void render() { try { images = PUmlApi.makeImage( textView.getCtrl().getText() ); if( images != null ) { imageIndex = 0; imageView.setImage(images.get(imageIndex)); statusText.setText( "" + (imageIndex+1) + " / " + images.size()); } } catch (IOException ignore) { } catch (InterruptedException ignore) { } } void prevImage() { if( images != null && imageIndex > 0 ) { imageIndex -= 1; imageView.setImage(images.get(imageIndex)); statusText.setText( "" + (imageIndex+1) + " / " + images.size()); } } void nextImage() { if( images != null && imageIndex < images.size() - 1 ) { imageIndex += 1; imageView.setImage(images.get(imageIndex)); statusText.setText( "" + (imageIndex+1) + " / " + images.size()); } } }
댓글 없음:
댓글 쓰기