Search in sources :

Example 51 with SpringLayout

use of javax.swing.SpringLayout in project uncommons-maths by dwdyer.

the class SpringUtilities method getConstraintsForCell.

/**
 * Helper method for {@link #makeCompactGrid(Container, int, int, int, int, int, int)}.
 */
private static SpringLayout.Constraints getConstraintsForCell(int row, int col, Container parent, int cols) {
    SpringLayout layout = (SpringLayout) parent.getLayout();
    Component c = parent.getComponent(row * cols + col);
    return layout.getConstraints(c);
}
Also used : SpringLayout(javax.swing.SpringLayout) Component(java.awt.Component)

Example 52 with SpringLayout

use of javax.swing.SpringLayout in project uncommons-maths by dwdyer.

the class SpringUtilities method makeCompactGrid.

/**
 * Aligns the first {@code rows} * {@code cols} components of {@code parent}
 * in a grid.  Each component in a column is as wide as the maximum preferred
 * width of the components in that column; height is similarly determined for
 * each row.  The parent is made just big enough to fit them all.
 * @param parent The container to layout.
 * @param rows number of rows
 * @param columns number of columns
 * @param initialX x location to start the grid at
 * @param initialY y location to start the grid at
 * @param xPad x padding between cells
 * @param yPad y padding between cells
 */
public static void makeCompactGrid(Container parent, int rows, int columns, int initialX, int initialY, int xPad, int yPad) {
    if (!(parent.getLayout() instanceof SpringLayout)) {
        throw new IllegalArgumentException("The first argument to makeCompactGrid must use SpringLayout.");
    }
    SpringLayout layout = (SpringLayout) parent.getLayout();
    // Align all cells in each column and make them the same width.
    Spring x = Spring.constant(initialX);
    for (int c = 0; c < columns; c++) {
        Spring width = Spring.constant(0);
        for (int r = 0; r < rows; r++) {
            width = Spring.max(width, getConstraintsForCell(r, c, parent, columns).getWidth());
        }
        for (int r = 0; r < rows; r++) {
            SpringLayout.Constraints constraints = getConstraintsForCell(r, c, parent, columns);
            constraints.setX(x);
            constraints.setWidth(width);
        }
        x = Spring.sum(x, Spring.sum(width, Spring.constant(xPad)));
    }
    // Align all cells in each row and make them the same height.
    Spring y = Spring.constant(initialY);
    for (int r = 0; r < rows; r++) {
        Spring height = Spring.constant(0);
        for (int c = 0; c < columns; c++) {
            height = Spring.max(height, getConstraintsForCell(r, c, parent, columns).getHeight());
        }
        for (int c = 0; c < columns; c++) {
            SpringLayout.Constraints constraints = getConstraintsForCell(r, c, parent, columns);
            constraints.setY(y);
            constraints.setHeight(height);
        }
        y = Spring.sum(y, Spring.sum(height, Spring.constant(yPad)));
    }
    // Set the parent's size.
    SpringLayout.Constraints parentConstraints = layout.getConstraints(parent);
    parentConstraints.setConstraint(SpringLayout.SOUTH, y);
    parentConstraints.setConstraint(SpringLayout.EAST, x);
}
Also used : SpringLayout(javax.swing.SpringLayout) Spring(javax.swing.Spring)

Example 53 with SpringLayout

use of javax.swing.SpringLayout in project BIMserver by opensourceBIM.

the class SpringUtilities method makeGrid.

/**
 * Aligns the first <code>rows</code> * <code>cols</code>
 * components of <code>parent</code> in
 * a grid. Each component is as big as the maximum
 * preferred width and height of the components.
 * The parent is made just big enough to fit them all.
 *
 * @param rows number of rows
 * @param cols number of columns
 * @param initialX x location to start the grid at
 * @param initialY y location to start the grid at
 * @param xPad x padding between cells
 * @param yPad y padding between cells
 */
public static void makeGrid(Container parent, int rows, int cols, int initialX, int initialY, int xPad, int yPad) {
    SpringLayout layout;
    try {
        layout = (SpringLayout) parent.getLayout();
    } catch (ClassCastException exc) {
        System.err.println("The first argument to makeGrid must use SpringLayout.");
        return;
    }
    Spring xPadSpring = Spring.constant(xPad);
    Spring yPadSpring = Spring.constant(yPad);
    Spring initialXSpring = Spring.constant(initialX);
    Spring initialYSpring = Spring.constant(initialY);
    int max = rows * cols;
    // Calculate Springs that are the max of the width/height so that all
    // cells have the same size.
    Spring maxWidthSpring = layout.getConstraints(parent.getComponent(0)).getWidth();
    Spring maxHeightSpring = layout.getConstraints(parent.getComponent(0)).getWidth();
    for (int i = 1; i < max; i++) {
        SpringLayout.Constraints cons = layout.getConstraints(parent.getComponent(i));
        maxWidthSpring = Spring.max(maxWidthSpring, cons.getWidth());
        maxHeightSpring = Spring.max(maxHeightSpring, cons.getHeight());
    }
    // components to have the same size.
    for (int i = 0; i < max; i++) {
        SpringLayout.Constraints cons = layout.getConstraints(parent.getComponent(i));
        cons.setWidth(maxWidthSpring);
        cons.setHeight(maxHeightSpring);
    }
    // Then adjust the x/y constraints of all the cells so that they
    // are aligned in a grid.
    SpringLayout.Constraints lastCons = null;
    SpringLayout.Constraints lastRowCons = null;
    for (int i = 0; i < max; i++) {
        SpringLayout.Constraints cons = layout.getConstraints(parent.getComponent(i));
        if (i % cols == 0) {
            // start of new row
            lastRowCons = lastCons;
            cons.setX(initialXSpring);
        } else {
            // x position depends on previous component
            cons.setX(Spring.sum(lastCons.getConstraint(SpringLayout.EAST), xPadSpring));
        }
        if (i / cols == 0) {
            // first row
            cons.setY(initialYSpring);
        } else {
            // y position depends on previous row
            cons.setY(Spring.sum(lastRowCons.getConstraint(SpringLayout.SOUTH), yPadSpring));
        }
        lastCons = cons;
    }
    // Set the parent's size.
    SpringLayout.Constraints pCons = layout.getConstraints(parent);
    pCons.setConstraint(SpringLayout.SOUTH, Spring.sum(Spring.constant(yPad), lastCons.getConstraint(SpringLayout.SOUTH)));
    pCons.setConstraint(SpringLayout.EAST, Spring.sum(Spring.constant(xPad), lastCons.getConstraint(SpringLayout.EAST)));
}
Also used : SpringLayout(javax.swing.SpringLayout) Spring(javax.swing.Spring)

Example 54 with SpringLayout

use of javax.swing.SpringLayout in project BIMserver by opensourceBIM.

the class SpringUtilities method makeCompactGrid.

/**
 * Aligns the first <code>rows</code> * <code>cols</code>
 * components of <code>parent</code> in
 * a grid. Each component in a column is as wide as the maximum
 * preferred width of the components in that column;
 * height is similarly determined for each row.
 * The parent is made just big enough to fit them all.
 *
 * @param rows number of rows
 * @param cols number of columns
 * @param initialX x location to start the grid at
 * @param initialY y location to start the grid at
 * @param xPad x padding between cells
 * @param yPad y padding between cells
 */
public static void makeCompactGrid(Container parent, int rows, int cols, int initialX, int initialY, int xPad, int yPad) {
    SpringLayout layout;
    try {
        layout = (SpringLayout) parent.getLayout();
    } catch (ClassCastException exc) {
        System.err.println("The first argument to makeCompactGrid must use SpringLayout.");
        return;
    }
    // Align all cells in each column and make them the same width.
    Spring x = Spring.constant(initialX);
    for (int c = 0; c < cols; c++) {
        Spring width = Spring.constant(0);
        for (int r = 0; r < rows; r++) {
            width = Spring.max(width, getConstraintsForCell(r, c, parent, cols).getWidth());
        }
        for (int r = 0; r < rows; r++) {
            SpringLayout.Constraints constraints = getConstraintsForCell(r, c, parent, cols);
            constraints.setX(x);
            constraints.setWidth(width);
        }
        x = Spring.sum(x, Spring.sum(width, Spring.constant(xPad)));
    }
    // Align all cells in each row and make them the same height.
    Spring y = Spring.constant(initialY);
    for (int r = 0; r < rows; r++) {
        Spring height = Spring.constant(0);
        for (int c = 0; c < cols; c++) {
            height = Spring.max(height, getConstraintsForCell(r, c, parent, cols).getHeight());
        }
        for (int c = 0; c < cols; c++) {
            SpringLayout.Constraints constraints = getConstraintsForCell(r, c, parent, cols);
            constraints.setY(y);
            constraints.setHeight(height);
        }
        y = Spring.sum(y, Spring.sum(height, Spring.constant(yPad)));
    }
    // Set the parent's size.
    SpringLayout.Constraints pCons = layout.getConstraints(parent);
    pCons.setConstraint(SpringLayout.SOUTH, y);
    pCons.setConstraint(SpringLayout.EAST, x);
}
Also used : SpringLayout(javax.swing.SpringLayout) Spring(javax.swing.Spring)

Example 55 with SpringLayout

use of javax.swing.SpringLayout in project BIMserver by opensourceBIM.

the class Starter method start.

private void start() {
    final JTextArea logField = new JTextArea();
    final PrintStream systemOut = System.out;
    PrintStream out = new PrintStream(new OutputStream() {

        @Override
        public void write(byte[] bytes, int off, int len) throws IOException {
            String str = new String(bytes, off, len);
            systemOut.append(str);
            logField.append(str);
            logField.setCaretPosition(logField.getDocument().getLength());
        }

        @Override
        public void write(int b) throws IOException {
            String str = new String(new char[] { (char) b });
            systemOut.append(str);
            logField.append(str);
            logField.setCaretPosition(logField.getDocument().getLength());
        }
    });
    System.setOut(out);
    System.setErr(out);
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
        e.printStackTrace();
    }
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setTitle("BIMserver Starter");
    try {
        setIconImage(ImageIO.read(getClass().getResource("logo_small.png")));
    } catch (IOException e) {
        e.printStackTrace();
    }
    setSize(640, 500);
    getContentPane().setLayout(new BorderLayout());
    JPanel fields = new JPanel(new SpringLayout());
    JLabel jvmLabel = new JLabel("JVM");
    fields.add(jvmLabel);
    jvmField = new JTextField(jarSettings.getJvm());
    browserJvm = new JButton("Browse...");
    browserJvm.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            File currentFile = new File(jvmField.getText());
            JFileChooser chooser = new JFileChooser(currentFile.exists() ? currentFile : new File("."));
            chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            int showOpenDialog = chooser.showOpenDialog(Starter.this);
            if (showOpenDialog == JFileChooser.APPROVE_OPTION) {
                jvmField.setText(chooser.getSelectedFile().getAbsolutePath());
            }
        }
    });
    JPanel jvmPanel = new JPanel();
    jvmPanel.setLayout(new BorderLayout());
    jvmPanel.add(jvmField, BorderLayout.CENTER);
    jvmPanel.add(browserJvm, BorderLayout.EAST);
    fields.add(jvmPanel);
    JLabel homeDirLabel = new JLabel("Home directory");
    fields.add(homeDirLabel);
    homeDirField = new JTextField(jarSettings.getHomedir());
    browserHomeDir = new JButton("Browse...");
    browserHomeDir.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            File currentFile = new File(homeDirField.getText());
            JFileChooser chooser = new JFileChooser(currentFile.exists() ? currentFile : new File("."));
            chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            int showOpenDialog = chooser.showOpenDialog(Starter.this);
            if (showOpenDialog == JFileChooser.APPROVE_OPTION) {
                homeDirField.setText(chooser.getSelectedFile().getAbsolutePath());
            }
        }
    });
    JPanel homeDirPanel = new JPanel();
    homeDirPanel.setLayout(new BorderLayout());
    homeDirPanel.add(homeDirField, BorderLayout.CENTER);
    homeDirPanel.add(browserHomeDir, BorderLayout.EAST);
    fields.add(homeDirPanel);
    JLabel addressLabel = new JLabel("Address");
    fields.add(addressLabel);
    addressField = new JTextField(jarSettings.getAddress());
    fields.add(addressField);
    JLabel portLabel = new JLabel("Port");
    fields.add(portLabel);
    portField = new JTextField(jarSettings.getPort() + "");
    fields.add(portField);
    JLabel heapSizeLabel = new JLabel("Max Heap Size");
    fields.add(heapSizeLabel);
    heapSizeField = new JTextField(jarSettings.getHeapsize());
    fields.add(heapSizeField);
    JLabel stackSizeLabel = new JLabel("Stack Size");
    fields.add(stackSizeLabel);
    stackSizeField = new JTextField(jarSettings.getStacksize());
    fields.add(stackSizeField);
    JLabel forceIpv4Label = new JLabel("Force IPv4");
    fields.add(forceIpv4Label);
    forceIpv4Field = new JCheckBox();
    forceIpv4Field.setSelected(jarSettings.isForceipv4());
    fields.add(forceIpv4Field);
    JLabel useProxyLabel = new JLabel("");
    useProxy = new JCheckBox("Use proxy server");
    useProxy.setSelected(jarSettings.isUseProxy());
    fields.add(useProxyLabel);
    fields.add(useProxy);
    proxyHostLabel = new JLabel("Proxy Host");
    fields.add(proxyHostLabel);
    proxyHost = new JTextField(jarSettings.getProxyHost());
    fields.add(proxyHost);
    proxyPortLabel = new JLabel("Proxy Port");
    fields.add(proxyPortLabel);
    proxyPort = new JTextField("" + jarSettings.getProxyPort());
    fields.add(proxyPort);
    // rows, cols
    SpringUtilities.makeCompactGrid(// rows, cols
    fields, // rows, cols
    10, // rows, cols
    2, // initX, initY
    6, // initX, initY
    6, 6, // xPad, yPad
    6);
    JPanel buttons = new JPanel(new FlowLayout(FlowLayout.TRAILING));
    final JButton startStopButton = new JButton("Start");
    startStopButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (startStopButton.getText().equals("Start")) {
                new Thread(new Runnable() {

                    @Override
                    public void run() {
                        if (jvmField.getText().equalsIgnoreCase("default") || new File(jvmField.getText()).exists()) {
                            setComponentsEnabled(false);
                            File file = expand();
                            startStopButton.setText("Stop");
                            start(file, addressField.getText(), portField.getText(), heapSizeField.getText(), stackSizeField.getText(), jvmField.getText(), homeDirField.getText());
                        } else {
                            JOptionPane.showMessageDialog(Starter.this, "JVM field should contain a valid JVM directory, or 'default' for the default JVM");
                        }
                    }
                }, "BIMserver Starter Thread").start();
            } else if (startStopButton.getText().equals("Stop")) {
                if (exec != null) {
                    exec.destroy();
                    System.out.println("Server has been shut down");
                    exec = null;
                    startStopButton.setText("Start");
                    setComponentsEnabled(true);
                }
            }
        }
    });
    final DocumentListener documentChangeListener = new DocumentListener() {

        @Override
        public void removeUpdate(DocumentEvent e) {
            save();
        }

        @Override
        public void insertUpdate(DocumentEvent e) {
            save();
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            save();
        }
    };
    jvmField.getDocument().addDocumentListener(documentChangeListener);
    homeDirField.getDocument().addDocumentListener(documentChangeListener);
    addressField.getDocument().addDocumentListener(documentChangeListener);
    portField.getDocument().addDocumentListener(documentChangeListener);
    heapSizeField.getDocument().addDocumentListener(documentChangeListener);
    stackSizeField.getDocument().addDocumentListener(documentChangeListener);
    useProxy.addChangeListener(new ChangeListener() {

        @Override
        public void stateChanged(ChangeEvent e) {
            updateProxyVisibility();
            save();
        }
    });
    proxyHost.getDocument().addDocumentListener(documentChangeListener);
    proxyPort.getDocument().addDocumentListener(documentChangeListener);
    forceIpv4Field.addChangeListener(new ChangeListener() {

        @Override
        public void stateChanged(ChangeEvent e) {
            save();
        }
    });
    buttons.add(startStopButton);
    JButton launchWebBrowser = new JButton("Launch Webbrowser");
    launchWebBrowser.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                Desktop.getDesktop().browse(new URI("http://" + addressField.getText() + ":" + portField.getText()));
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    });
    buttons.add(launchWebBrowser);
    logField.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
    logField.setEditable(true);
    logField.addKeyListener(new KeyAdapter() {

        @Override
        public void keyPressed(KeyEvent e) {
            try {
                exec.getOutputStream().write(e.getKeyChar());
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    exec.getOutputStream().flush();
                }
            } catch (IOException e2) {
                e2.printStackTrace();
            }
        }
    });
    JScrollPane scrollPane = new JScrollPane(logField);
    getContentPane().add(fields, BorderLayout.NORTH);
    getContentPane().add(scrollPane, BorderLayout.CENTER);
    getContentPane().add(buttons, BorderLayout.SOUTH);
    updateProxyVisibility();
    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {

        @Override
        public void run() {
            if (exec != null) {
                exec.destroy();
            }
        }
    }, "Thutdown Hook"));
    setVisible(true);
}
Also used : JPanel(javax.swing.JPanel) DocumentListener(javax.swing.event.DocumentListener) JTextArea(javax.swing.JTextArea) FlowLayout(java.awt.FlowLayout) ActionEvent(java.awt.event.ActionEvent) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) KeyAdapter(java.awt.event.KeyAdapter) JButton(javax.swing.JButton) JTextField(javax.swing.JTextField) URI(java.net.URI) Font(java.awt.Font) KeyEvent(java.awt.event.KeyEvent) BorderLayout(java.awt.BorderLayout) ChangeListener(javax.swing.event.ChangeListener) JScrollPane(javax.swing.JScrollPane) PrintStream(java.io.PrintStream) JLabel(javax.swing.JLabel) IOException(java.io.IOException) DocumentEvent(javax.swing.event.DocumentEvent) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JCheckBox(javax.swing.JCheckBox) ActionListener(java.awt.event.ActionListener) JFileChooser(javax.swing.JFileChooser) ChangeEvent(javax.swing.event.ChangeEvent) SpringLayout(javax.swing.SpringLayout) JarFile(java.util.jar.JarFile) File(java.io.File)

Aggregations

SpringLayout (javax.swing.SpringLayout)57 JLabel (javax.swing.JLabel)21 JPanel (javax.swing.JPanel)21 Spring (javax.swing.Spring)21 BorderLayout (java.awt.BorderLayout)18 JTextField (javax.swing.JTextField)17 ActionEvent (java.awt.event.ActionEvent)15 ActionListener (java.awt.event.ActionListener)15 Component (java.awt.Component)12 JDialog (javax.swing.JDialog)12 Dimension (java.awt.Dimension)11 JButton (javax.swing.JButton)10 JScrollPane (javax.swing.JScrollPane)10 EmptyBorder (javax.swing.border.EmptyBorder)9 FlowLayout (java.awt.FlowLayout)8 JMenuItem (javax.swing.JMenuItem)7 Constraints (javax.swing.SpringLayout.Constraints)6 ItemEvent (java.awt.event.ItemEvent)5 Callable (java.util.concurrent.Callable)5 JMenu (javax.swing.JMenu)5