private void updateComboBoxContent() { ignoreUiUpdates = true; Object selected = this.getSelectedItem(); this.removeAllItems(); this.addItem(NONE); this.addItem(ONE_PROFILE_FOR_EVERYTHING); MappingManager.getInstance().generateDefaultMappings().forEach(this::addItem); List<MappingSet> all = MappingManager.getInstance().getAll(); if (all.size() > 0) { this.addItem(PREDEFINED_MAPPINGS); all.forEach(this::addItem); } if (VisicutModel.getInstance().getSelectedPart() != null) { Iterable<String> props = VisicutModel.getInstance() .getSelectedPart() .getGraphicObjects() .getInterestingAttributes(); if (props.iterator().hasNext()) { this.addItem(BY_PROPERTY); int count = 0; for (String att : props) { if (++count > 4) { break; } this.addItem(new MapByPropertyEntry(att)); } } } this.addItem(CUSTOM); this.setSelectedItem(selected); ignoreUiUpdates = false; }
private void propertyChanged(PropertyChangeEvent pce) { if (pce.getSource().equals(VisicutModel.getInstance())) { if (VisicutModel.PROP_SELECTEDPART.equals(pce.getPropertyName())) { updateComboBoxContent(); updateUi(); } else if (VisicutModel.PROP_PLF_PART_UPDATED.equals(pce.getPropertyName()) && pce.getNewValue().equals(VisicutModel.getInstance().getSelectedPart())) { updateUi(); } } else if (pce.getSource().equals(MappingManager.getInstance()) || pce.getSource().equals(ProfileManager.getInstance())) { updateComboBoxContent(); } }
/** Creates new form MappingPanel */ public PredefinedMappingBox() { ListCellRenderer cbRenderer = new DefaultListCellRenderer() { @Override public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { String mapBy = bundle.getString("MAP_BY"); Component result = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); if (result instanceof JLabel) { if (value instanceof MappingSet) { ((JLabel) result).setText((isPopupVisible() ? " " : "") + value.toString()); } else if (value instanceof MapByPropertyEntry) { ((JLabel) result) .setText( " " + mapBy.replace( "$property", GraphicSet.translateAttVal(((MapByPropertyEntry) value).property))); } } return result; } }; this.setRenderer(cbRenderer); PropertyChangeListener pl = pce -> propertyChanged(pce); MappingManager.getInstance().addPropertyChangeListener(pl); ProfileManager.getInstance().addPropertyChangeListener(pl); VisicutModel.getInstance().addPropertyChangeListener(pl); this.addActionListener(ae -> comboBoxActionPerformed(ae)); updateComboBoxContent(); updateUi(); }
/** * Updates the UI to represent the current this.mapping value WITHOUT generating propery change * events. */ private void updateUi() { if (VisicutModel.getInstance().getSelectedPart() != null) { ignoreUiUpdates = true; MappingSet ms = VisicutModel.getInstance().getSelectedPart().getMapping(); // guess the selected entry from the MappingSet // we have no information about the name, so we need to see which entry matches if ((lastSelectedPlfPart == VisicutModel.getInstance().getSelectedPart()) && (this.getSelectedItem() == CUSTOM || this.getSelectedItem() == BY_PROPERTY)) { // special case: // the selected PlfPart ("object") is still the same. // CUSTOM/BY_PROPERTY was selected and the mapping was edited // even if the mapping is now equal to a saved one, don't switch back to the entry of the // saved mapping! // otherwise the CustomMappingPanel would be hidden // change nothing, selectedItem is still CUSTOM } else { // default case: // show NONE if empty mapping // show saved mapping if one is equal to the current mapping // show "CUSTOM" otherwise if (ms == null || ms.isEmpty()) { Object selected = this.getSelectedItem(); // NONE, By_Property and Custom can represent a null mapping, so leave // them alone if they are selected. Otherwise select NONE by default if (!NONE.equals(selected) && !BY_PROPERTY.equals(selected) && !CUSTOM.equals(selected)) { this.setSelectedItem(NONE); } } else { this.setSelectedItem( PropertyMappingPanel.getPropertyMappingProperty(ms) != null ? BY_PROPERTY : CUSTOM); this.setSelectedItem( ms); // only changes the selection if the mapping exists in the comboBox } } ignoreUiUpdates = false; } lastSelectedPlfPart = VisicutModel.getInstance().getSelectedPart(); }
private void comboBoxActionPerformed(java.awt.event.ActionEvent evt) { if (!ignoreUiUpdates && VisicutModel.getInstance().getSelectedPart() != null) { Object selected = this.getSelectedItem(); if (selected == null || NONE.equals(selected)) { VisicutModel.getInstance().getSelectedPart().setMapping(null); VisicutModel.getInstance().firePartUpdated(VisicutModel.getInstance().getSelectedPart()); } else if (selected instanceof MappingSet) { VisicutModel.getInstance().getSelectedPart().setMapping((MappingSet) selected); VisicutModel.getInstance().firePartUpdated(VisicutModel.getInstance().getSelectedPart()); } else if (selected instanceof MapByPropertyEntry) { // do nothing. MappingPanel will handle this } else // some string selected, which should not be selectable => revert state { updateUi(); } } }