Example usage for javax.swing JMenu setModel

List of usage examples for javax.swing JMenu setModel

Introduction

In this page you can find the example usage for javax.swing JMenu setModel.

Prototype

public void setModel(ButtonModel newModel) 

Source Link

Document

Sets the data model for the "menu button" -- the label that the user clicks to open or close the menu.

Usage

From source file:org.squidy.designer.Designer.java

/**
 * Initializes menu bar.// ww  w  .j a  v a2 s  .c om
 */
private void initMenuBar() {

    JMenuBar menuBar = new JMenuBar();

    JMenu workspace = new JMenu("Workspace");
    workspace.add(new AbstractAction("Open from...") {

        /*
         * (non-Javadoc)
         * 
         * @see
         * java.awt.event.ActionListener#actionPerformed(java.awt.event.
         * ActionEvent)
         */
        public void actionPerformed(ActionEvent e) {
            JFileChooser fileChooser = new JFileChooser();
            fileChooser.setFileHidingEnabled(true);
            fileChooser.setFileFilter(new FileFilter() {

                /*
                 * (non-Javadoc)
                 * 
                 * @see
                 * javax.swing.filechooser.FileFilter#accept(java.io.File)
                 */
                @Override
                public boolean accept(File f) {
                    return f.isDirectory() || f.getName().endsWith(".sdy");
                }

                /*
                 * (non-Javadoc)
                 * 
                 * @see javax.swing.filechooser.FileFilter#getDescription()
                 */
                @Override
                public String getDescription() {
                    return "Squidy Workspace";
                }
            });
            int option = fileChooser.showOpenDialog(Designer.this);

            if (option == JFileChooser.APPROVE_OPTION) {

                // Stop replacing workspace if currently running.
                if (data != null) {
                    data.getWorkspace().stop();
                }

                File workspaceFile = fileChooser.getSelectedFile();

                if (storage instanceof LocalJAXBStorage) {
                    ((LocalJAXBStorage) storage).setWorkspaceFile(workspaceFile);
                    load();
                } else {
                    try {
                        data = ModelViewHandler.getModelViewHandler().load(new FileInputStream(workspaceFile));

                        WorkspaceShape workspace = data.getWorkspaceShape();
                        workspace.setModel(data);
                        workspace.setStorageHandler(Designer.this);
                        workspace.initialize();
                        LayoutConstraint lc = workspace.getLayoutConstraint();
                        workspace.setScale(lc.getScale());
                        workspace.setOffset(lc.getX(), lc.getY());

                        getCanvas().getLayer().addChild(workspace);

                        zoomToZoomedShape(data);
                    } catch (SquidyException e1) {
                        e1.printStackTrace();
                    } catch (FileNotFoundException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        }
    });

    workspace.add(new AbstractAction("Export as...") {

        /*
         * (non-Javadoc)
         * 
         * @see
         * java.awt.event.ActionListener#actionPerformed(java.awt.event.
         * ActionEvent)
         */
        public void actionPerformed(ActionEvent e) {

            JFileChooser fileChooser = new JFileChooser();
            fileChooser.setFileHidingEnabled(true);
            fileChooser.setFileFilter(new FileFilter() {

                /*
                 * (non-Javadoc)
                 * 
                 * @see
                 * javax.swing.filechooser.FileFilter#accept(java.io.File)
                 */
                @Override
                public boolean accept(File f) {
                    return f.isDirectory() || f.getName().endsWith(".sdy");
                }

                /*
                 * (non-Javadoc)
                 * 
                 * @see javax.swing.filechooser.FileFilter#getDescription()
                 */
                @Override
                public String getDescription() {
                    return "Squidy Workspace";
                }
            });

            if (storage instanceof LocalJAXBStorage) {
                fileChooser.setSelectedFile(((LocalJAXBStorage) storage).getWorkspaceFile());
            }

            int option = fileChooser.showSaveDialog(Designer.this);

            if (option == JFileChooser.APPROVE_OPTION) {

                File workspaceFile = fileChooser.getSelectedFile();

                try {
                    ModelViewHandler.getModelViewHandler().save(new FileOutputStream(workspaceFile), data);
                } catch (FileNotFoundException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        }
    });

    JMenu options = new JMenu("Options");
    rendering.addItemListener(new ItemListener() {

        public void itemStateChanged(ItemEvent e) {
            VisualShape.setRenderPrimitiveRect(rendering.isSelected());
            data.setRenderPrimitiveRect(rendering.isSelected());
            storage.store(data);
            repaint();
        }
    });
    options.add(rendering);

    JMenu storage = new JMenu("Storage");

    ButtonGroup group = new ButtonGroup();

    final JRadioButtonMenuItem storageLocalJAXB = new JRadioButtonMenuItem("Local JAXB");
    storageLocalJAXB.addItemListener(new ItemListener() {

        public void itemStateChanged(ItemEvent e) {
            if (storageLocalJAXB.isSelected()) {
                setStorageMode(StorageMode.FILE);
            }
        }
    });
    storage.add(storageLocalJAXB);
    group.add(storageLocalJAXB);

    final JRadioButtonMenuItem storageBaseX = new JRadioButtonMenuItem("BaseX");
    storageBaseX.addItemListener(new ItemListener() {

        public void itemStateChanged(ItemEvent e) {
            if (storageBaseX.isSelected()) {
                setStorageMode(StorageMode.BASEX);
            }
        }
    });
    storageBaseX.setSelected(storageType.equals(BaseXStorage.class));

    storage.add(storageBaseX);
    group.add(storageBaseX);
    options.add(storage);

    menuBar.add(workspace);
    menuBar.add(options);

    setJMenuBar(menuBar);
}