Example usage for java.awt Component setEnabled

List of usage examples for java.awt Component setEnabled

Introduction

In this page you can find the example usage for java.awt Component setEnabled.

Prototype

public void setEnabled(boolean b) 

Source Link

Document

Enables or disables this component, depending on the value of the parameter b .

Usage

From source file:com.evanbelcher.DrillBook.display.DBMenuBar.java

/**
 * Disables most menu items/*w w w . j a  v a  2s .  c  om*/
 */
public void disableMost() {
    //      for (Component component : getComponents()) {
    //         if (component instanceof JMenu && ((JMenu) component).getText().equals("Settings")) {
    //            for (Component menuComponent : ((JMenu) component).getMenuComponents()) {
    //               if (!(menuComponent instanceof JMenuItem) || (!((JMenuItem) menuComponent).getText().equals("Toggle Gridlines") && !((JMenuItem) menuComponent).getText().equals("Toggle Dot Names")))
    //                  menuComponent.setEnabled(false);
    //            }
    //         } else {
    //            component.setEnabled(false);
    //         }
    //      }
    for (Component component : getComponents()) {
        if (component instanceof JMenu) {
            boolean enabled = false;
            for (Component menuComponent : ((JMenu) component).getMenuComponents()) {
                JMenuItem menuItem = (JMenuItem) menuComponent;
                switch (menuItem.getText()) {
                case "Quit":
                case "Toggle Gridlines":
                case "Toggle Dot Names":
                case "Help":
                case "About":
                    enabled = true;
                    break;
                default:
                    menuItem.setEnabled(false);
                }
            }
            if (!enabled)
                component.setEnabled(false);
        } else
            component.setEnabled(false);
    }
}

From source file:edu.ku.brc.af.ui.forms.validation.FormValidator.java

/**
 * Evaluate all the enable/disable rules and set the control and label
 *//* www  . java  2s.  co  m*/
public boolean processFormRules() {
    boolean formIsOK = true;

    boolean debug = false;
    if (debug) {
        log.debug(name + " ****** processFormRules  ");
        Map<?, ?> map = jc.getVars();
        Object[] keys = map.keySet().toArray();
        for (Object key : keys) {
            log.debug(name + " ### [" + key + "][" + map.get(key).getClass().toString() + "]");
        }
    }

    //log.debug("processFormRules ["+name+"]------------------------------------------------- Number of Rules: "+formRules.size());
    for (FormValidationRuleIFace rule : formRules) {
        try {
            // Now evaluate the expression, getting the result
            boolean result = rule.evaluate(jc);
            //log.debug("Result ["+result+"] for ID["+rule.getId()+"]  Rule["+((RuleExpression)rule).expression.getExpression()+"]");
            if (rule.getScope() == FormValidationRuleIFace.Scope.Field) {
                Component comp = getComp(rule.getId());
                if (comp != null) {
                    //log.debug("    comp.setEnabled("+result+") "+comp.getClass().toString());
                    comp.setEnabled(result);
                }

                JLabel lbl = labels.get(rule.getId());
                if (lbl != null) {
                    lbl.setEnabled(result);
                }

            } else if (rule.getScope() == FormValidationRuleIFace.Scope.Form) {
                formIsOK &= result;
            }

        } catch (Exception ex) {
            edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
            edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(FormValidator.class, ex);
            log.error(name + " " + ex.toString());
            formIsOK = false;
            //ex.printStackTrace();
        }
    }
    return formIsOK;
}

From source file:edu.ku.brc.specify.datamodel.busrules.BaseTreeBusRules.java

@SuppressWarnings("unchecked")
@Override/*from   w  ww .j av a2  s .  c  o  m*/
public void afterFillForm(final Object dataObj) {
    // This is a little weak and cheesey, but it gets the job done.
    // Because both the Tree and Definition want/need to share Business Rules.
    String viewName = formViewObj.getView().getName();
    if (StringUtils.contains(viewName, "TreeDef")) {
        if (formViewObj.getAltView().getMode() != CreationMode.EDIT) {
            // when we're not in edit mode, we don't need to setup any listeners since the user can't change anything
            //log.debug("form is not in edit mode: no special listeners will be attached");
            return;
        }

        if (!StringUtils.contains(viewName, "TreeDefItem")) {
            return;
        }

        final I nodeInForm = (I) formViewObj.getDataObj();
        //disable FullName -related fields if TreeDefItem is used by nodes in the tree
        //NOTE: Can remove the edit restriction. Tree rebuilds now update fullname fields. Need to add tree rebuild after fullname def edits.
        if (nodeInForm != null && nodeInForm.getTreeDef() != null) {
            //               boolean canNOTEditFullNameFlds = nodeInForm.hasTreeEntries();
            //               if (canNOTEditFullNameFlds)
            //               {
            //                  ValTextField ftCtrl = (ValTextField )formViewObj.getControlByName("textAfter");
            //                  if (ftCtrl != null)
            //                  {
            //                     ftCtrl.setEnabled(false);
            //                  }
            //                  ftCtrl = (ValTextField )formViewObj.getControlByName("textBefore");
            //                  if (ftCtrl != null)
            //                  {
            //                     ftCtrl.setEnabled(false);
            //                  }
            //                  ftCtrl = (ValTextField )formViewObj.getControlByName("fullNameSeparator");
            //                  if (ftCtrl != null)
            //                  {
            //                     ftCtrl.setEnabled(false);
            //                  }
            //                  ValCheckBox ftBox = (ValCheckBox )formViewObj.getControlByName("isInFullName");
            //                  if (ftBox != null)
            //                  {
            //                     ftBox.setEnabled(false);
            //                  }
            //               }

            if (!viewName.endsWith("TreeDefItem")) {
                return;
            }

            //disabling editing of name and rank for standard levels.
            List<TreeDefItemStandardEntry> stds = nodeInForm.getTreeDef().getStandardLevels();
            TreeDefItemStandardEntry stdLevel = null;
            for (TreeDefItemStandardEntry std : stds) {
                //if (std.getTitle().equals(nodeInForm.getName()) && std.getRank() == nodeInForm.getRankId())
                if (std.getRank() == nodeInForm.getRankId()) {
                    stdLevel = std;
                    break;
                }
            }
            if (stdLevel != null) {
                ValTextField nameCtrl = (ValTextField) formViewObj.getControlByName("name");
                Component rankCtrl = formViewObj.getControlByName("rankId");
                if (nameCtrl != null) {
                    nameCtrl.setEnabled(false);
                }
                if (rankCtrl != null) {
                    rankCtrl.setEnabled(false);
                }
                if (nodeInForm.getTreeDef().isRequiredLevel(stdLevel.getRank())) {
                    Component enforcedCtrl = formViewObj.getControlByName("isEnforced");
                    if (enforcedCtrl != null) {
                        enforcedCtrl.setEnabled(false);
                    }
                }
            }
        }
        return;
    }

    final T nodeInForm = (T) formViewObj.getDataObj();

    if (formViewObj.getAltView().getMode() != CreationMode.EDIT) {
        if (nodeInForm != null) {
            //XXX this MAY be necessary due to a bug with TextFieldFromPickListTable??
            // TextFieldFromPickListTable.setValue() does nothing because of a null adapter member.
            Component comp = formViewObj.getControlByName("definitionItem");
            if (comp instanceof JTextField) {
                ((JTextField) comp).setText(nodeInForm.getDefinitionItem().getName());
            }
        }
    } else {
        processedRules = false;
        GetSetValueIFace parentField = (GetSetValueIFace) formViewObj.getControlByName("parent");
        Component comp = formViewObj.getControlByName("definitionItem");
        if (comp instanceof ValComboBox) {
            final ValComboBox rankComboBox = (ValComboBox) comp;

            if (parentField instanceof ValComboBoxFromQuery) {
                final ValComboBoxFromQuery parentCBX = (ValComboBoxFromQuery) parentField;
                if (parentCBX != null && rankComboBox != null && nodeInForm != null) {
                    parentCBX.registerQueryBuilder(new TreeableSearchQueryBuilder(nodeInForm, rankComboBox,
                            TreeableSearchQueryBuilder.PARENT));
                }
            }

            if (nodeInForm != null && nodeInForm.getDefinitionItem() != null) {
                // log.debug("node in form already has a set rank: forcing a call to
                // adjustRankComboBoxModel()");
                UIValidator.setIgnoreAllValidation(this, true);
                adjustRankComboBoxModel(parentField, rankComboBox, nodeInForm);
                UIValidator.setIgnoreAllValidation(this, false);
            }

            // TODO: the form system MUST require the accepted parent widget to be present if
            // the
            // isAccepted checkbox is present
            final JCheckBox acceptedCheckBox = (JCheckBox) formViewObj.getControlByName("isAccepted");
            final ValComboBoxFromQuery acceptedParentWidget = (ValComboBoxFromQuery) formViewObj
                    .getControlByName("acceptedParent");
            if (canAccessSynonymy(nodeInForm)) {
                if (acceptedCheckBox != null && acceptedParentWidget != null) {
                    if (acceptedCheckBox.isSelected() && nodeInForm != null
                            && nodeInForm.getDefinition() != null) {
                        // disable if necessary
                        boolean canSynonymize = nodeInForm.getDefinition().getSynonymizedLevel() <= nodeInForm
                                .getRankId() && nodeInForm.getDescendantCount() == 0;
                        acceptedCheckBox.setEnabled(canSynonymize);
                    }
                    acceptedParentWidget
                            .setEnabled(!acceptedCheckBox.isSelected() && acceptedCheckBox.isEnabled());
                    if (acceptedCheckBox.isSelected()) {
                        acceptedParentWidget.setValue(null, null);
                    }

                    if (nodeInForm != null && acceptedParentWidget != null && rankComboBox != null) {
                        acceptedParentWidget.registerQueryBuilder(new TreeableSearchQueryBuilder(nodeInForm,
                                rankComboBox, TreeableSearchQueryBuilder.ACCEPTED_PARENT));
                    }
                }
            } else {
                if (acceptedCheckBox != null) {
                    acceptedCheckBox.setEnabled(false);
                }
                if (acceptedParentWidget != null) {
                    acceptedParentWidget.setEnabled(false);
                }
            }

            if (parentField instanceof ValComboBoxFromQuery) {
                parentChanged(formViewObj, (ValComboBoxFromQuery) parentField, rankComboBox, acceptedCheckBox,
                        acceptedParentWidget);
            }
        }
    }
}

From source file:edu.uara.gui.tableeditor.ChartGenerationFrame.java

/**
 * turn tab panel on or off//  www.jav a  2  s  .  c  o m
 * @param enabled
 */
private void toggleTabPanel(boolean enabled) {
    for (Component comp : tab_chartProperties.getComponents()) {
        if (comp instanceof JPanel) {
            JPanel p = (JPanel) comp;
            for (Component childComp : p.getComponents())
                childComp.setEnabled(enabled);
        }
    }
}

From source file:edu.ku.brc.specify.tasks.SystemSetupTask.java

/**
 * // w  w  w  .  j a va2s  .co  m
 */
private void showAddObjPrefs() {
    Collection collection = AppContextMgr.getInstance().getClassObject(Collection.class);
    Integer colId = collection.getId();

    boolean isPrepTypeOK = false;
    String defVal = CollectionObjectBusRules.getDefValForPrepTypeHaveOnForm();
    if (StringUtils.isNotEmpty(defVal)) {
        isPrepTypeOK = CollectionObjectBusRules.getPrepTypeIdFromDefVal(defVal) != null;
    }

    AppPreferences remote = AppPreferences.getRemote();
    String CO_CREATE_PREP = "CO_CREATE_PREP";

    String[] keys = { "CO_CREATE_COA", CO_CREATE_PREP, "CO_CREATE_DET", };
    Properties props = new Properties();
    int i = 0;
    for (String key : keys) {
        String fullKey = key + "_" + colId;
        if (i == 1 && !isPrepTypeOK) {
            remote.putBoolean(fullKey, false);
        }
        props.put(key, remote.getBoolean(fullKey, false));
        i++;
    }

    FormPane pane = new FormPane("AddObjPrefs", this, "SystemSetup", "AddObjPrefs", "edit", props,
            MultiView.NO_OPTIONS | MultiView.DONT_USE_EMBEDDED_SEP, null); // not new data object
    CustomDialog dlg = new CustomDialog((Frame) getTopWindow(), getResourceString("SYSSTP_AOBTN"), true, pane);
    pane.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6));
    dlg.createUI();

    if (!isPrepTypeOK) {
        Component comp = pane.getMultiView().getCurrentViewAsFormViewObj().getControlByName(CO_CREATE_PREP);
        if (comp != null) {
            comp.setEnabled(false);
            UIRegistry.showLocalizedError("SYSSTP_AOPREP_ERR");
        }
    }

    pane.getMultiView().setData(props);
    UIHelper.centerAndShow(dlg);
    if (!dlg.isCancelled()) {
        pane.getMultiView().getCurrentViewAsFormViewObj().getDataFromUI();
        for (String key : keys) {
            Object val = props.get(key);
            if (val != null) {
                remote.putBoolean(key + "_" + colId, (Boolean) val);
            }
        }
    }
}

From source file:edu.uara.gui.tableeditor.ChartGenerationFrame.java

private void switchTabPanel(Component c) {
    this.tab_chartProperties.setSelectedComponent(c);
    Component[] compCollection = tab_chartProperties.getComponents();
    for (Component comp : compCollection) {
        if (comp.equals(c)) {
            if (comp instanceof JPanel) {
                JPanel p = (JPanel) comp;
                for (Component childComp : p.getComponents())
                    childComp.setEnabled(true);
            }/* w ww.j  a  v a 2 s . c  o m*/
        } else {
            if (comp instanceof JPanel) {
                JPanel p = (JPanel) comp;
                for (Component childComp : p.getComponents())
                    childComp.setEnabled(false);
            }
        }
    }
}

From source file:edu.uara.gui.tableeditor.ChartGenerationFrame.java

/**
 * turn on and off other options when multi-piechart is selected
 * @param enabled/* w  w  w  .  ja  v  a 2  s .c o m*/
 */
private void toggleMultiplePieChartOptions(boolean enabled) {
    for (Component comp : this.tabPanel_piechart.getComponents()) {
        if (!comp.equals(this.cbo_pieDataSelect) && !comp.equals(this.opt_multiPieCharts)
                && !comp.equals(this.lbl_datasetBy) && !comp.equals(this.lbl_pieLabelFormat)
                && !comp.equals(this.cbo_pieSectionLabel)) {
            comp.setEnabled(enabled);
        }
    }
}

From source file:edu.ku.brc.specify.datamodel.busrules.DeterminationBusRules.java

@Override
public void afterFillForm(final Object dataObj) {
    isBlockingChange = false;/* ww w  .java 2 s  .c  om*/
    determination = null;

    if (formViewObj != null && formViewObj.getDataObj() instanceof Determination) {
        determination = (Determination) formViewObj.getDataObj();

        // if determination exists and is new (no key) then set current true if CO has no other dets
        Component currentComp = formViewObj.getControlByName("isCurrent");
        if (determination != null && currentComp != null) {
            if (isNewObject) {
                // It should never be null, but, currently, it does happen.
                // Also, now with Batch ReIdentify is will always be NULL
                if (determination.getCollectionObject() != null) {
                    if (currentComp instanceof ValCheckBox) {
                        if (formViewObj.isCreatingNewObject()) {
                            // Do this instead of setSelected because
                            // this activates the DataChangeListener
                            isBlockingChange = true;
                            ((ValCheckBox) currentComp).doClick();
                            isBlockingChange = false;

                            // Well, if it is already checked then we just checked it to the 'off' state,
                            // so we need to re-check it so it is in the "checked state"
                            // Note: As stated in the comment above the 'doClick' the easiest way to activate
                            // all the change listeners is by simulating a mouse click.
                            // Also keep in mind that the change listener is listening for ActionEvents for the
                            // checkbox instead of ChangeEvents (ChangeEvents cause to many problems).
                            if (!((ValCheckBox) currentComp).isSelected()) {
                                ((ValCheckBox) currentComp).doClick();
                            }

                            Set<Determination> detSet = determination.getCollectionObject().getDeterminations();
                            for (Determination d : detSet) {
                                if (d != determination) {
                                    d.setIsCurrent(false);
                                }
                            }
                        }

                    } else {
                        log.error("IsCurrent not set to true because form control is of unexpected type: "
                                + currentComp.getClass().getName());
                    }
                }
            } else {
                ((ValCheckBox) currentComp).setValue(determination.getIsCurrent(), null);
            }
        }

        Component activeTax = formViewObj.getControlByName("preferredTaxon");
        if (activeTax != null) {
            JTextField activeTaxTF = (JTextField) activeTax;
            activeTaxTF.setFocusable(false);
            if (determination != null && determination.getPreferredTaxon() != null) {
                activeTaxTF.setText(determination.getPreferredTaxon().getFullName());
            } else {
                activeTaxTF.setText("");
            }
        }

        if (formViewObj.getAltView().getMode() != CreationMode.EDIT) {
            // when we're not in edit mode, we don't need to setup any listeners since the user can't change anything
            //log.debug("form is not in edit mode: no special listeners will be attached");
            return;
        }

        Component nameUsageComp = formViewObj.getControlByName("nameUsage");
        if (nameUsageComp instanceof ValComboBox) {
            // XXX this is probably not necessary anymore...
            if (!checkedBlankUsageItem) {
                boolean fnd = false;

                if (nameUsageComp instanceof ValComboBox) {
                    ValComboBox cbx = (ValComboBox) nameUsageComp;
                    if (cbx.getComboBox().getModel() instanceof PickListDBAdapterIFace) {
                        PickListDBAdapterIFace items = (PickListDBAdapterIFace) cbx.getComboBox().getModel();
                        for (PickListItemIFace item : items.getPickList().getItems()) {
                            if (StringUtils.isBlank(item.getValue())) {
                                fnd = true;
                                break;
                            }
                        }
                        if (!fnd) {
                            boolean readOnly = items.getPickList().getReadOnly();
                            if (readOnly) {
                                items.getPickList().setReadOnly(false);
                            }
                            items.addItem("", null);
                            if (readOnly) {
                                items.getPickList().setReadOnly(true);
                            }
                        }
                    }
                }
                checkedBlankUsageItem = true;
            }
            nameUsageComp.setEnabled(true);
        }

        final Component altNameComp = formViewObj.getControlByName("alternateName");
        if (altNameComp != null && determination != null) {
            altNameComp.setEnabled(determination.getTaxon() == null);
        }

        if (currentComp != null && chkbxCL == null) {
            chkbxCL = new ChangeListener() {
                @Override
                public void stateChanged(ChangeEvent e) {
                    adjustIsCurrentCheckbox();
                }
            };

            isCurrentCheckbox = (ValCheckBox) currentComp;
            isCurrentCheckbox.addChangeListener(chkbxCL);
        }
    }
    isNewObject = false;
}

From source file:edu.ku.brc.specify.tasks.subpane.qb.QueryFieldPanel.java

protected PickListCriteriaCombo createPickList(final Component saveBtn) {
    PickListCriteriaCombo result = new PickListCriteriaCombo(pickList);
    if (!ownerQuery.isPromptMode()) {
        result.addActionListener(new ActionListener() {

            /* (non-Javadoc)
             * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
             *///  www  .j a  va 2s .co  m
            @Override
            public void actionPerformed(ActionEvent e) {
                if (e.getID() == 1001/*ComboBoxChanged*/) {
                    if (saveBtn != null) {
                        saveBtn.setEnabled(true);
                    }
                }
            }

        });
    }
    return result;
}

From source file:edu.ku.brc.specify.datamodel.busrules.DeterminationBusRules.java

/**
 * @param taxonComboBox /* ww w . j a  v a  2  s .  c om*/
 * 
 * Sets text for preferredTaxon control to the selected taxon or it's accepted parent.
 * 
 * If the selected taxon is not accepted, then a dialog pops up to confirm the choice
 * with an option to use the accepted parent instead.
 * 
 * Disables/Enables AlternateName field based on whether taxon is non-null/null.
 */
protected void taxonChanged(final ValComboBoxFromQuery taxonComboBox, final Component altTaxName) {
    Object objInForm = formViewObj.getDataObj();
    if (objInForm == null) {
        return;
    }

    Taxon formNode = ((Determination) objInForm).getTaxon();

    Taxon taxon = null;
    if (taxonComboBox.getValue() instanceof String) {
        // the data is still in the VIEW mode for some reason
        taxonComboBox.getValue();
        taxon = formNode.getParent();
    } else {
        taxon = (Taxon) taxonComboBox.getValue();
    }

    String activeTaxName = null;

    // set the tree def for the object being edited by using the parent node's tree def
    if (taxon != null) {
        if (!taxon.getIsAccepted()
                && AppPreferences.getRemote().getBoolean("Determination.PromptToReplaceSynonym", false)) {
            PanelBuilder pb = new PanelBuilder(
                    new FormLayout("5dlu, f:p:g, 5dlu", "7dlu, c:p, 5dlu, c:p, 10dlu"));
            String msg1 = String.format(UIRegistry.getResourceString("DeterminationBusRule.SynChoiceMsg1"),
                    taxon.getFullName(), taxon.getAcceptedParent().getFullName());
            String msg2 = String.format(UIRegistry.getResourceString("DeterminationBusRule.SynChoiceMsg2"),
                    taxon.getFullName(), taxon.getAcceptedParent().getFullName());
            CellConstraints cc = new CellConstraints();
            pb.add(UIHelper.createLabel(msg1), cc.xy(2, 2));
            pb.add(UIHelper.createLabel(msg2), cc.xy(2, 4));
            String formTitle = UIRegistry.getResourceString("DeterminationBusRules.SYNONYM_INFORMATION");
            CustomDialog cd = new CustomDialog((Frame) UIRegistry.getTopWindow(), formTitle, true,
                    CustomDialog.OKCANCELHELP, pb.getPanel());
            cd.setModal(true);
            cd.setOkLabel(UIRegistry.getResourceString("DeterminationBusRules.Change"));
            cd.setCancelLabel(UIRegistry.getResourceString("DeterminationBusRules.Keep"));
            UIHelper.centerAndShow(cd);
            if (cd.getBtnPressed() == CustomDialog.OK_BTN) {
                taxon = taxon.getAcceptedParent();
                taxonComboBox.setValue(taxon, taxon.getFullName());
            }
        }
        if (taxon.getIsAccepted()) {
            activeTaxName = taxon.getFullName();
        } else {
            activeTaxName = taxon.getAcceptedParent().getFullName();
        }
    }

    Component activeTax = formViewObj.getControlByName("preferredTaxon");
    if (activeTax != null) {
        ((JTextField) activeTax).setText(activeTaxName);
    }

    if (altTaxName != null) {
        altTaxName.setEnabled(taxon == null);
    }
}