Search in sources :

Example 1 with ModifiableSizeInfo

use of CCDD.CcddConstants.ModifiableSizeInfo in project CCDD by nasa.

the class CcddPreferencesDialog method initialize.

/**
 ********************************************************************************************
 * Create the preferences dialog
 ********************************************************************************************
 */
private void initialize() {
    maxScrollPaneHeight = 0;
    // Create an empty border for use with the dialog components
    emptyBorder = BorderFactory.createEmptyBorder();
    // Create a tabbed pane
    tabbedPane = new DnDTabbedPane(SwingConstants.TOP);
    tabbedPane.setFont(ModifiableFontInfo.LABEL_BOLD.getFont());
    // Add the tabs to the tabbed pane
    addLafTab();
    addFontTab();
    addColorTab();
    addSizeTab();
    addSpacingTab();
    addPathTab();
    addOtherTab();
    // Create a panel for the preference dialog buttons
    JPanel buttonPnl = new JPanel();
    // Update button
    final JButton btnUpdateAll = CcddButtonPanelHandler.createButton("Update", STORE_ICON, KeyEvent.VK_U, "Update the program preference values");
    // Add a listener for the Update button
    btnUpdateAll.addActionListener(new ActionListener() {

        /**
         ************************************************************************************
         * Update the program preference values
         ************************************************************************************
         */
        @Override
        public void actionPerformed(ActionEvent ae) {
            int index = 0;
            // Base the action on the name of the tab selected
            switch(tabbedPane.getTitleAt(tabbedPane.getSelectedIndex())) {
                case LAF:
                case FONT:
                case COLOR:
                    // preference value
                    break;
                case SIZE:
                    // size
                    for (ModifiableSizeInfo modSize : ModifiableSizeInfo.values()) {
                        // Get the current value from the size text field
                        int currentValue = Integer.valueOf(sizeFld[index].getText());
                        // Check if the size has changed
                        if (modSize.getSize() != currentValue) {
                            // Update the size to the new value
                            modSize.setSize(currentValue, ccddMain.getProgPrefs());
                        }
                        index++;
                    }
                    break;
                case SPACING:
                    // modifiable spacing
                    for (ModifiableSpacingInfo modSpacing : ModifiableSpacingInfo.values()) {
                        // Get the current value from the spacing text field
                        int currentValue = Integer.valueOf(spacingFld[index].getText());
                        // Check if the spacing has changed
                        if (modSpacing.getSpacing() != currentValue) {
                            // Update the spacing to the new value
                            modSpacing.setSpacing(currentValue, ccddMain.getProgPrefs());
                        }
                        index++;
                    }
                    break;
                case PATH:
                    // Update the program path preference. Step through each modifiable path
                    for (ModifiablePathInfo modPath : ModifiablePathInfo.values()) {
                        // Get the current path from the path text field
                        String currentPath = pathFld[index].getText().trim();
                        // Check if the path has changed
                        if (!modPath.getPath().equals(currentPath)) {
                            // Store the path in the program preferences backing store
                            CcddFileIOHandler.storePath(ccddMain, currentPath, false, modPath);
                        }
                        index++;
                    }
                    break;
                case OTHER:
                    // setting
                    for (ModifiableOtherSettingInfo modOther : ModifiableOtherSettingInfo.values()) {
                        // Get the current setting value from the other setting text field
                        String currentValue = otherFld[index].getText().trim();
                        // Check if the setting value has changed
                        if (!modOther.getValue().equals(currentValue)) {
                            // Update the other setting to the new value
                            modOther.setValue(currentValue, ccddMain.getProgPrefs());
                        }
                        index++;
                    }
                    break;
            }
        }
    });
    // Close button
    JButton btnCloseDlg = CcddButtonPanelHandler.createButton("Close", CLOSE_ICON, KeyEvent.VK_C, "Close the program preferences dialog");
    // Add a listener for the Close button
    btnCloseDlg.addActionListener(new ActionListener() {

        /**
         ************************************************************************************
         * Close the program preferences dialog
         ************************************************************************************
         */
        @Override
        public void actionPerformed(ActionEvent ae) {
            // Close the dialog
            closeDialog();
        }
    });
    // Listen for tab selection changes
    tabbedPane.addChangeListener(new ChangeListener() {

        /**
         ************************************************************************************
         * Handle a tab selection change
         ************************************************************************************
         */
        @Override
        public void stateChanged(ChangeEvent ce) {
            // Get the index of the selected tab
            int tabIndex = tabbedPane.getSelectedIndex();
            // Check if a tab is selected
            if (tabIndex != -1) {
                // Base the action of the name of the tab selected
                switch(tabbedPane.getTitleAt(tabIndex)) {
                    case LAF:
                    case FONT:
                    case COLOR:
                        // Hide the update button
                        btnUpdateAll.setVisible(false);
                        break;
                    case SIZE:
                    case SPACING:
                    case PATH:
                    case OTHER:
                        // Show the update button
                        btnUpdateAll.setVisible(true);
                        break;
                }
            }
        }
    });
    // Add the buttons to the dialog panel
    buttonPnl.add(btnUpdateAll);
    buttonPnl.add(btnCloseDlg);
    // Toggle the tab selection so that the first tab is selected and the Update button
    // visibility is set accordingly
    tabbedPane.setSelectedIndex(-1);
    tabbedPane.setSelectedIndex(0);
    // Set the initial size of the preferences dialog based on the individual panes' contents
    tabbedPane.setPreferredSize(new Dimension(tabbedPane.getPreferredSize().width + LAF_SCROLL_BAR_WIDTH, maxScrollPaneHeight));
    // Display the Preferences dialog
    showOptionsDialog(ccddMain.getMainFrame(), tabbedPane, buttonPnl, btnCloseDlg, "Preferences", true);
}
Also used : JPanel(javax.swing.JPanel) ModifiableOtherSettingInfo(CCDD.CcddConstants.ModifiableOtherSettingInfo) ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton) ModifiablePathInfo(CCDD.CcddConstants.ModifiablePathInfo) Dimension(java.awt.Dimension) DnDTabbedPane(CCDD.CcddClassesComponent.DnDTabbedPane) ActionListener(java.awt.event.ActionListener) PropertyChangeEvent(java.beans.PropertyChangeEvent) ChangeEvent(javax.swing.event.ChangeEvent) ModifiableSizeInfo(CCDD.CcddConstants.ModifiableSizeInfo) ModifiableSpacingInfo(CCDD.CcddConstants.ModifiableSpacingInfo) ChangeListener(javax.swing.event.ChangeListener) PropertyChangeListener(java.beans.PropertyChangeListener)

Example 2 with ModifiableSizeInfo

use of CCDD.CcddConstants.ModifiableSizeInfo in project CCDD by nasa.

the class CcddPreferencesDialog method addSizeTab.

/**
 ********************************************************************************************
 * Add the size update tab to the tabbed pane
 ********************************************************************************************
 */
private void addSizeTab() {
    // Set the initial layout manager characteristics
    GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(ModifiableSpacingInfo.LABEL_VERTICAL_SPACING.getSpacing(), ModifiableSpacingInfo.LABEL_HORIZONTAL_SPACING.getSpacing() / 2, ModifiableSpacingInfo.LABEL_VERTICAL_SPACING.getSpacing(), ModifiableSpacingInfo.LABEL_HORIZONTAL_SPACING.getSpacing() / 2), 0, 0);
    // Create a border for the input fields
    Border border = BorderFactory.createCompoundBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED, Color.LIGHT_GRAY, Color.GRAY), BorderFactory.createEmptyBorder(ModifiableSpacingInfo.INPUT_FIELD_PADDING.getSpacing(), ModifiableSpacingInfo.INPUT_FIELD_PADDING.getSpacing(), ModifiableSpacingInfo.INPUT_FIELD_PADDING.getSpacing(), ModifiableSpacingInfo.INPUT_FIELD_PADDING.getSpacing()));
    // Create storage for the description and input field representing each modifiable size
    JLabel[] sizeLbl = new JLabel[ModifiableSizeInfo.values().length];
    JButton[] sizeBtn = new JButton[ModifiableSizeInfo.values().length];
    sizeFld = new JTextField[ModifiableSizeInfo.values().length];
    // Create a panel to contain the size components
    JPanel innerSizePnl = new JPanel(new GridBagLayout());
    innerSizePnl.setBorder(emptyBorder);
    // Use an outer panel so that the components can be forced to the top of the tab area
    JPanel sizePnl = new JPanel(new BorderLayout());
    sizePnl.setBorder(emptyBorder);
    sizePnl.add(innerSizePnl, BorderLayout.PAGE_START);
    // Create a scroll pane in which to display the size labels and fields
    JScrollPane sizeScrollPane = new JScrollPane(sizePnl);
    sizeScrollPane.setBorder(emptyBorder);
    sizeScrollPane.setViewportBorder(emptyBorder);
    // Add the size update tab to the tabbed pane
    tabbedPane.addTab(SIZE, null, sizeScrollPane, "Change program maximum width and length values");
    // Create a listener for the default size buttons
    ActionListener defaultListener = new ActionListener() {

        /**
         ************************************************************************************
         * Update the size to the default value
         ************************************************************************************
         */
        @Override
        public void actionPerformed(ActionEvent ae) {
            // Get the index of the size field array, which is stored as the button's name
            int index = Integer.valueOf(((JButton) ae.getSource()).getName());
            // Set the size to its default value
            sizeFld[index].setText(String.valueOf(ModifiableSizeInfo.values()[index].getDefault()));
        }
    };
    int index = 0;
    // Step through each modifiable size
    for (final ModifiableSizeInfo modSize : ModifiableSizeInfo.values()) {
        // Create the size label and input field
        sizeLbl[index] = new JLabel(modSize.getName() + " (" + modSize.getMinimum() + ", " + modSize.getMaximum() + ")");
        sizeLbl[index].setFont(ModifiableFontInfo.LABEL_BOLD.getFont());
        sizeLbl[index].setToolTipText(CcddUtilities.wrapText(modSize.getDescription(), ModifiableSizeInfo.MAX_TOOL_TIP_LENGTH.getSize()));
        innerSizePnl.add(sizeLbl[index], gbc);
        sizeFld[index] = new JTextField(String.valueOf(modSize.getSize()), 3);
        sizeFld[index].setFont(ModifiableFontInfo.INPUT_TEXT.getFont());
        sizeFld[index].setForeground(ModifiableColorInfo.INPUT_TEXT.getColor());
        sizeFld[index].setBackground(ModifiableColorInfo.INPUT_BACK.getColor());
        sizeFld[index].setBorder(border);
        sizeFld[index].setName(modSize.getPreferenceKey());
        sizeFld[index].setToolTipText(CcddUtilities.wrapText(modSize.getDescription(), ModifiableSizeInfo.MAX_TOOL_TIP_LENGTH.getSize()));
        // Create an input verifier to keep the size value within its specified limits
        sizeFld[index].setInputVerifier(new InputVerifier() {

            // Storage for the last valid value entered; used to restore the size value if an
            // invalid value is entered
            String lastValid = String.valueOf(modSize.getSize());

            /**
             ********************************************************************************
             * Verify the contents of a the size field
             ********************************************************************************
             */
            @Override
            public boolean verify(JComponent input) {
                boolean isValid = true;
                JTextField sizeFld = (JTextField) input;
                try {
                    // Get the reference to the modifiable size information using its program
                    // preferences key, which is stored as the field's name
                    ModifiableSizeInfo modSize = ModifiableSizeInfo.getModifiableSizeInfo(input.getName());
                    // Remove any leading or trailing white space characters
                    String size = sizeFld.getText().trim();
                    // Check if the size field is empty
                    if (size.isEmpty()) {
                        throw new CCDDException(modSize.getName() + "<b>' cannot be blank");
                    }
                    // Check if the size value isn't a positive integer
                    if (!size.matches(InputDataType.INT_POSITIVE.getInputMatch())) {
                        throw new CCDDException(modSize.getName() + "<b>' must be a positive integer value");
                    }
                    // Convert the text to an integer
                    int currentValue = Integer.valueOf(size);
                    // Check if the size value is outside of its specified limits
                    if (currentValue < modSize.getMinimum() || currentValue > modSize.getMaximum()) {
                        throw new CCDDException(modSize.getName() + "<b>' is outside allowable limits");
                    }
                    // Update the size field to the new (valid) value
                    sizeFld.setText(size);
                    // Store the new value as the last valid value
                    lastValid = sizeFld.getText();
                } catch (CCDDException ce) {
                    // Inform the user that the input value is invalid
                    new CcddDialogHandler().showMessageDialog(CcddPreferencesDialog.this, "<html><b>The value for '</b>" + ce.getMessage(), "Missing/Invalid Input", JOptionPane.WARNING_MESSAGE, DialogOption.OK_OPTION);
                    // Restore the size field to the last valid value
                    sizeFld.setText(lastValid);
                    // Set the flag to indicate the size value is invalid
                    isValid = false;
                    // Toggle the controls enable status so that the buttons are redrawn
                    // correctly
                    CcddPreferencesDialog.this.setControlsEnabled(false);
                    CcddPreferencesDialog.this.setControlsEnabled(true);
                }
                return isValid;
            }
        });
        // Add the size field to the size panel
        gbc.gridx++;
        innerSizePnl.add(sizeFld[index], gbc);
        // Create a button for setting the size to its default value and add it to the size
        // panel
        sizeBtn[index] = new JButton("Default (" + modSize.getDefault() + ")");
        sizeBtn[index].setFont(ModifiableFontInfo.DIALOG_BUTTON.getFont());
        sizeBtn[index].setName(String.valueOf(index));
        sizeBtn[index].addActionListener(defaultListener);
        gbc.gridx++;
        innerSizePnl.add(sizeBtn[index], gbc);
        gbc.weightx = 1.0;
        gbc.gridx++;
        innerSizePnl.add(new JLabel(""), gbc);
        gbc.weightx = 0.0;
        gbc.gridx = 0;
        gbc.gridy++;
        index++;
    }
    // Set the scroll bar scroll increment
    sizeScrollPane.getVerticalScrollBar().setUnitIncrement(sizeFld[0].getPreferredSize().height / 2 + ModifiableSpacingInfo.LABEL_VERTICAL_SPACING.getSpacing());
    // Calculate the maximum required height of the panel containing the size labels and fields
    // (= # of rows * row height)
    maxScrollPaneHeight = Math.max(maxScrollPaneHeight, 10 * sizeScrollPane.getPreferredSize().height / sizeFld.length);
}
Also used : JScrollPane(javax.swing.JScrollPane) JPanel(javax.swing.JPanel) GridBagConstraints(java.awt.GridBagConstraints) Insets(java.awt.Insets) CCDDException(CCDD.CcddClassesDataTable.CCDDException) GridBagLayout(java.awt.GridBagLayout) ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton) JComponent(javax.swing.JComponent) JLabel(javax.swing.JLabel) JTextField(javax.swing.JTextField) InputVerifier(javax.swing.InputVerifier) BorderLayout(java.awt.BorderLayout) ActionListener(java.awt.event.ActionListener) ModifiableSizeInfo(CCDD.CcddConstants.ModifiableSizeInfo) Border(javax.swing.border.Border) BevelBorder(javax.swing.border.BevelBorder)

Aggregations

ModifiableSizeInfo (CCDD.CcddConstants.ModifiableSizeInfo)2 ActionEvent (java.awt.event.ActionEvent)2 ActionListener (java.awt.event.ActionListener)2 JButton (javax.swing.JButton)2 JPanel (javax.swing.JPanel)2 DnDTabbedPane (CCDD.CcddClassesComponent.DnDTabbedPane)1 CCDDException (CCDD.CcddClassesDataTable.CCDDException)1 ModifiableOtherSettingInfo (CCDD.CcddConstants.ModifiableOtherSettingInfo)1 ModifiablePathInfo (CCDD.CcddConstants.ModifiablePathInfo)1 ModifiableSpacingInfo (CCDD.CcddConstants.ModifiableSpacingInfo)1 BorderLayout (java.awt.BorderLayout)1 Dimension (java.awt.Dimension)1 GridBagConstraints (java.awt.GridBagConstraints)1 GridBagLayout (java.awt.GridBagLayout)1 Insets (java.awt.Insets)1 PropertyChangeEvent (java.beans.PropertyChangeEvent)1 PropertyChangeListener (java.beans.PropertyChangeListener)1 InputVerifier (javax.swing.InputVerifier)1 JComponent (javax.swing.JComponent)1 JLabel (javax.swing.JLabel)1