@Override protected void init(String text, Icon icon) { model = createButtonModel(); setModel(model); addActionListener(this); super.init(text, icon); }
@Override public void actionPerformed(ActionEvent e) { stateUpdated(getState()); }
@Override public void updateUI() { super.updateUI(); if (isMixed()) { adjustMixedIcon(); } else { restoreMixedIcon(); } }
final ImageIcon shapeIcon = SpectrumShapeProvider.getShapeIcon(spectrum.getSymbolIndex()); spectraPanel.add(collapseButton); final TristateCheckBox tristateCheckBox = new TristateCheckBox(); tristateCheckBox.setState(selectionAdmin.getState(index)); tristateCheckBox.addActionListener(new TristateCheckboxListener(index)); tristateCheckBoxes[index] = tristateCheckBox; spectraPanel.add(tristateCheckBox);
/** * This method is called when the selection state changes. * * @param state the new selection state. */ protected void stateUpdated(int state) { if (state == STATE_MIXED) { adjustMixedIcon(); Object cp = UIDefaultsLookup.get("TristateCheckBox.setMixed.clientProperty"); if (cp != null) { putClientProperty(((Object[]) cp)[0], ((Object[]) cp)[1]); // for Aqua L&F } String name = UIDefaultsLookup.getString("TristateCheckBox.setMixed.componentName"); if (name != null) { setName(name); // for Synthetica } } else { restoreMixedIcon(); Object cp = UIDefaultsLookup.get("TristateCheckBox.clearMixed.clientProperty"); if (cp != null) { putClientProperty(((Object[]) cp)[0], ((Object[]) cp)[1]); // for Aqua L&F } String name = UIDefaultsLookup.getString("TristateCheckBox.clearMixed.componentName"); if (name != null) { setName(name); // for Synthetica } } } }
private void validateCheckBox() { if (_checkBox == null || _checkBoxIcon != UIManager.getDefaults().getIcon("CheckBox.icon")) { _checkBox = new TristateCheckBox(); _checkBox.setOpaque(false); _checkBoxIcon = UIManager.getDefaults().getIcon("CheckBox.icon"); _uncheckImage = null; _checkedImage = null; _mixedImage = null; } }
@Override public void actionPerformed(ActionEvent e) { if (!selectionChangeLock) { selectionChangeLock = true; selectionAdmin.updateSpectrumSelectionState(index, tristateCheckBoxes[index].getState()); tristateCheckBoxes[index].setState(selectionAdmin.getState(index)); updateBandsTable(index); spectra[index].setSelected(selectionAdmin.isSpectrumSelected(index)); selectionChangeLock = false; } } }
@Override public void paintIcon(Component c, Graphics g, int x, int y) { validateCheckBox(); if (_uncheckImage == null) { GraphicsConfiguration graphicsConfiguration = ((Graphics2D) g).getDeviceConfiguration(); _uncheckImage = graphicsConfiguration.createCompatibleImage(getIconWidth(), getIconHeight(), Transparency.TRANSLUCENT); _checkBox.setState(TristateCheckBox.STATE_UNSELECTED); _checkBox.setBounds(0, 0, getIconWidth(), getIconHeight()); _checkBox.paint(_uncheckImage.getGraphics()); _checkedImage = graphicsConfiguration.createCompatibleImage(getIconWidth(), getIconHeight(), Transparency.TRANSLUCENT); _checkBox.setState(TristateCheckBox.STATE_SELECTED); _checkBox.setBounds(0, 0, getIconWidth(), getIconHeight()); _checkBox.paint(_checkedImage.getGraphics()); _mixedImage = graphicsConfiguration.createCompatibleImage(getIconWidth(), getIconHeight(), Transparency.TRANSLUCENT); _checkBox.setState(TristateCheckBox.STATE_MIXED); _checkBox.setBounds(0, 0, getIconWidth(), getIconHeight()); _checkBox.paint(_mixedImage.getGraphics()); } if (getState() == TristateCheckBox.STATE_SELECTED) { g.drawImage(_checkedImage, x, y, null); } else if (getState() == TristateCheckBox.STATE_UNSELECTED) { g.drawImage(_uncheckImage, x, y, null); } else if (getState() == TristateCheckBox.STATE_MIXED) { g.drawImage(_mixedImage, x, y, null); } }
myCheckBox = new TristateCheckBox(); myCheckBox.setText("123123"); add(myCheckBox); myCheckBox.setHalfSelected(true);
/** * Checks if the check box is in mixed selection state. * * @return true or false. */ public boolean isMixed() { return getState() == STATE_MIXED; }
public CheckBoxTreeCellRenderer(TreeCellRenderer renderer, TristateCheckBox checkBox) { _protoType = new TristateCheckBox(); if (checkBox == null) { _checkBox = createCheckBox(); } else { _checkBox = checkBox; } _emptyBox = (JComponent) Box.createHorizontalStrut(_protoType.getPreferredSize().width); setLayout(new BorderLayout(0, 0)); setOpaque(false); _actualTreeRenderer = renderer; }
@Override public int getIconWidth() { validateCheckBox(); return _checkBox.getPreferredSize().width; }
@Override public void paintIcon(Component c, Graphics g, int x, int y) { Icon icon = (Icon) _originalIcon.createValue(UIManager.getDefaults()); if (isSynthIcon(icon)) { int state = getComponentState((JComponent) c); if (c instanceof TristateCheckBox && ((TristateCheckBox) c).getModel() instanceof TristateButtonModel && ((TristateButtonModel) ((TristateCheckBox) c).getModel()).isMixed()) state &= ~SynthConstants.SELECTED; SynthContext context = new SynthContext((JComponent) c, Region.CHECK_BOX, SynthLookAndFeel.getStyle((JComponent) c, Region.CHECK_BOX), state); final int w = ((SynthIcon) icon).getIconWidth(context); final int h = ((SynthIcon) icon).getIconHeight(context); ((SynthIcon) icon).paintIcon(context, g, x, y, w, h); } else { if (c instanceof TristateCheckBox && ((TristateCheckBox) c).getModel() instanceof TristateButtonModel && ((TristateButtonModel) ((TristateCheckBox) c).getModel()).isMixed()) { ((TristateButtonModel) ((TristateCheckBox) c).getModel()).internalSetSelected(false); } icon.paintIcon(c, g, x, y); if (c instanceof TristateCheckBox && ((TristateCheckBox) c).getModel() instanceof TristateButtonModel && ((TristateButtonModel) ((TristateCheckBox) c).getModel()).isMixed()) { ((TristateButtonModel) ((TristateCheckBox) c).getModel()).internalSetSelected(true); } } g.setColor(UIManager.getColor("CheckBox.foreground")); if (c instanceof TristateCheckBox && ((TristateCheckBox) c).getModel() instanceof TristateButtonModel && ((TristateButtonModel) ((TristateCheckBox) c).getModel()).isMixed()) drawSquare(c, g, x, y); }
@Override public String getToolTipText(MouseEvent event) { if (_actualTreeRenderer instanceof JComponent) { Point p = event.getPoint(); p.translate(-_checkBox.getWidth(), 0); MouseEvent newEvent = new MouseEvent(((JComponent) _actualTreeRenderer), event.getID(), event.getWhen(), event.getModifiers(), p.x, p.y, event.getClickCount(), event.isPopupTrigger()); String tip = ((JComponent) _actualTreeRenderer).getToolTipText( newEvent); if (tip != null) { return tip; } } return super.getToolTipText(event); } }
@Override public int getIconHeight() { validateCheckBox(); return _checkBox.getPreferredSize().height; }
/** * Updates the check box state based on the selection in the selection model. By default, we check if the path is * selected. If yes, we mark the check box as TristateCheckBox.SELECTED. If not, we will check if the path is * partially selected, if yes, we set the check box as null or TristateCheckBox.DONT_CARE to indicate the path is * partially selected. Otherwise, we set it to TristateCheckBox.NOT_SELECTED. * * @param checkBox the TristateCheckBox for the particular tree path. * @param path the tree path. * @param selectionModel the CheckBoxTreeSelectionModel. */ protected void updateCheckBoxState(TristateCheckBox checkBox, TreePath path, CheckBoxTreeSelectionModel selectionModel) { if (selectionModel.isPathSelected(path, selectionModel.isDigIn())) checkBox.setState(TristateCheckBox.STATE_SELECTED); else checkBox.setState(selectionModel.isDigIn() && selectionModel.isPartiallySelected(path) ? TristateCheckBox.STATE_MIXED : TristateCheckBox.STATE_UNSELECTED); }
selectionAdmin.setBandSelected(index, bandRow, selected); selectionAdmin.updateSpectrumSelectionState(index, selectionAdmin.getState(index)); tristateCheckBoxes[index].setState(selectionAdmin.getState(index)); spectrum1.setSelected(selectionAdmin.isSpectrumSelected(index)); selectionChangeLock = false;