/** * Helper method */ private static void writeInitialValuesElement( @NotNull final Element itemElement, @NotNull final HashMap<String, StringDescriptor> name2value ) { LOG.assertTrue(ELEMENT_ITEM.equals(itemElement.getName())); if (name2value.size() == 0) { // do not append 'initial-values' subtag return; } final Element initialValuesElement = new Element(ELEMENT_INITIAL_VALUES); itemElement.addContent(initialValuesElement); for (final Map.Entry<String, StringDescriptor> entry : name2value.entrySet()) { final Element propertyElement = new Element(ELEMENT_PROPERTY); initialValuesElement.addContent(propertyElement); propertyElement.setAttribute(ATTRIBUTE_NAME, entry.getKey()); propertyElement.setAttribute(ATTRIBUTE_VALUE, entry.getValue().getValue()/*descriptor is always trivial*/); } }
public ComponentItem( @NotNull Project project, @NotNull final String className, @Nullable final String iconPath, @Nullable final String toolTipText, @NotNull final GridConstraints defaultConstraints, @NotNull final HashMap<String, StringDescriptor> propertyName2initialValue, final boolean removable, final boolean autoCreateBinding, final boolean canAttachLabel ){ myAutoCreateBinding = autoCreateBinding; myCanAttachLabel = canAttachLabel; myProject = project; setClassName(className); setIconPath(iconPath); myToolTipText = toolTipText; myDefaultConstraints = defaultConstraints; myPropertyName2initialValue = propertyName2initialValue; myRemovable = removable; }
/** Creates deep copy of the object. You can edit any properties of the returned object. */ public ComponentItem clone(){ final ComponentItem result = new ComponentItem( myProject, myClassName, myIconPath, myToolTipText, (GridConstraints)myDefaultConstraints.clone(), (HashMap<String, StringDescriptor>)myPropertyName2initialValue.clone(), myRemovable, myAutoCreateBinding, myCanAttachLabel ); result.setIsContainer(myIsContainer); return result; }
private static void checkUpdateBindingFromText(final RadComponent component, final StringDescriptor value, final SupportCode.TextWithMnemonic textWithMnemonic) { if (component.isLoadingProperties()) { return; } // only generate binding from text if default locale is active (IDEADEV-9427) if (value.getValue() == null) { RadRootContainer root = (RadRootContainer) FormEditingUtil.getRoot(component); Locale locale = root.getStringDescriptorLocale(); if (locale != null && locale.getDisplayName().length() > 0) { return; } } BindingProperty.checkCreateBindingFromText(component, textWithMnemonic.myText); if (component.getDelegee() instanceof JLabel) { for(IProperty prop: component.getModifiedProperties()) { if (prop.getName().equals(SwingProperties.LABEL_FOR) && prop instanceof IntroComponentProperty) { ((IntroComponentProperty) prop).updateLabelForBinding(component); } } } }
/** * Applies specified bundle to the myTfWithBrowseButton */ private void setValue(final StringDescriptor descriptor){ myValue = descriptor; final JTextField textField = myTfWithButton.getTextField(); if(descriptor != null){ final String value = descriptor.getValue(); if(value != null){ // plain value textField.setEditable(true); textField.setText(value); textField.selectAll(); myTextFieldModified = false; } else{ // bundled value textField.setEditable(false); textField.setBackground(UIUtil.getTextFieldBackground()); textField.setText("[" + descriptor.getKey() + " / " + descriptor.getDottedBundleName() + "]"); } } else{ textField.setEditable(true); textField.setText(null); } }
public StringDescriptor getValue(){ if(myValue == null || (myValue.getValue() != null && myTextFieldModified)) { // editor is for "trivial" StringDescriptor final String value = myTfWithButton.getText(); if (myValue == null && value.length() == 0) { return null; } else{ final StringDescriptor stringDescriptor = StringDescriptor.create(value); if (myValue != null && myValue.isNoI18n()) { stringDescriptor.setNoI18n(true); } return stringDescriptor; } } else{ // editor is for "bundled" StringDescriptor return myValue; } }
@Override protected void doOKAction() { if (myForm.myRbResourceBundle.isSelected()) { final StringDescriptor descriptor = getDescriptor(); if (descriptor != null && descriptor.getKey().length() > 0) { final String value = myForm.myTfRbValue.getText(); final PropertiesFile propFile = getPropertiesFile(descriptor); if (propFile != null && propFile.findPropertyByKey(descriptor.getKey()) == null) { saveCreatedProperty(propFile, descriptor.getKey(), value, myEditor.getPsiFile()); } else { final String newKeyName = saveModifiedPropertyValue(myEditor.getModule(), descriptor, myLocale, value, myEditor.getPsiFile()); if (newKeyName != null) { myForm.myTfKey.setText(newKeyName); } } } } super.doOKAction(); }
/** * @return edited descriptor. If initial descriptor was <code>null</code> * and user didn't change anything then this method returns <code>null</code>. */ @Nullable StringDescriptor getDescriptor(){ if(myForm.myRbString.isSelected()){ // plain value final String value = myForm.myTfValue.getText(); if(myValue == null && value.length() == 0){ return null; } else{ final StringDescriptor stringDescriptor = StringDescriptor.create(value); stringDescriptor.setNoI18n(myForm.myNoI18nCheckbox.isSelected()); return stringDescriptor; } } else{ // bundled value final String bundleName = myForm.myTfBundleName.getText(); final String key = myForm.myTfKey.getText(); return new StringDescriptor(bundleName, key); } }
@Nullable public static String getText(@NotNull final Module module, final IComponent component) { IProperty textProperty = findProperty(component, SwingProperties.TEXT); if (textProperty != null) { Object propValue = textProperty.getPropertyValue(component); String value = null; if (propValue instanceof StringDescriptor) { StringDescriptor descriptor = (StringDescriptor) propValue; if (component instanceof RadComponent) { value = StringDescriptorManager.getInstance(module).resolve((RadComponent) component, descriptor); } else { value = StringDescriptorManager.getInstance(module).resolve(descriptor, null); } } else if (propValue instanceof String) { value = (String) propValue; } if (value != null) { return value; } } return null; }
public static void updateStringPropertyValue(GuiEditor editor, RadComponent component, IntroStringProperty prop, StringDescriptor descriptor, String result) { if (descriptor.getBundleName() == null) { prop.setValueEx(component, StringDescriptor.create(result)); } else { final String newKeyName = StringEditorDialog.saveModifiedPropertyValue(editor.getModule(), descriptor, editor.getStringDescriptorLocale(), result, editor.getPsiFile()); if (newKeyName != null) { prop.setValueEx(component, new StringDescriptor(descriptor.getBundleName(), newKeyName)); } } editor.refreshAndSave(false); }
@Nullable public String resolve(@Nullable StringDescriptor descriptor, @Nullable Locale locale) { if (descriptor == null) { return null; } if (descriptor.getValue() != null) { return descriptor.getValue(); } IProperty prop = resolveToProperty(descriptor, locale); if (prop != null) { final String value = prop.getUnescapedValue(); if (value != null) { return value; } } // We have to return surrogate string in case if propFile name is invalid or bundle doesn't have specified key return "[" + descriptor.getKey() + " / " + descriptor.getBundleName() + "]"; }
public IProperty resolveToProperty(@NotNull StringDescriptor descriptor, @Nullable Locale locale) { String propFileName = descriptor.getDottedBundleName(); Pair<Locale, String> cacheKey = Pair.create(locale, propFileName); PropertiesFile propertiesFile; synchronized (myPropertiesFileCache) { propertiesFile = myPropertiesFileCache.get(cacheKey); } if (propertiesFile == null || !propertiesFile.getContainingFile().isValid()) { propertiesFile = PropertiesUtilBase.getPropertiesFile(propFileName, myModule, locale); synchronized (myPropertiesFileCache) { myPropertiesFileCache.put(cacheKey, propertiesFile); } } if (propertiesFile != null) { final IProperty propertyByKey = propertiesFile.findPropertyByKey(descriptor.getKey()); if (propertyByKey != null) { return propertyByKey; } } return null; }
/** Helper method */ private static void writeInitialValuesElement( @NotNull final Element itemElement, @NotNull final HashMap<String, StringDescriptor> name2value ){ LOG.assertTrue(ELEMENT_ITEM.equals(itemElement.getName())); if(name2value.size() == 0){ // do not append 'initial-values' subtag return; } final Element initialValuesElement = new Element(ELEMENT_INITIAL_VALUES); itemElement.addContent(initialValuesElement); for (final Map.Entry<String, StringDescriptor> entry : name2value.entrySet()) { final Element propertyElement = new Element(ELEMENT_PROPERTY); initialValuesElement.addContent(propertyElement); propertyElement.setAttribute(ATTRIBUTE_NAME, entry.getKey()); propertyElement.setAttribute(ATTRIBUTE_VALUE, entry.getValue().getValue()/*descriptor is always trivial*/); } }
public IProperty resolveToProperty(@NotNull StringDescriptor descriptor, @Nullable Locale locale) { String propFileName = descriptor.getDottedBundleName(); Pair<Locale, String> cacheKey = new Pair<Locale, String>(locale, propFileName); SoftReference<PropertiesFile> propertiesFileRef; synchronized (myPropertiesFileCache) { propertiesFileRef = myPropertiesFileCache.get(cacheKey); } PropertiesFile propertiesFile = (propertiesFileRef == null) ? null : propertiesFileRef.get(); if (propertiesFile == null || !propertiesFile.getContainingFile().isValid()) { propertiesFile = PropertiesUtil.getPropertiesFile(propFileName, myModule, locale); synchronized (myPropertiesFileCache) { myPropertiesFileCache.put(cacheKey, new SoftReference<PropertiesFile>(propertiesFile)); } } if (propertiesFile != null) { final IProperty propertyByKey = propertiesFile.findPropertyByKey(descriptor.getKey()); if (propertyByKey != null) { return propertyByKey; } } return null; }
public static Set<String> collectUsedBundleNames(final IRootContainer rootContainer) { final Set<String> bundleNames = new HashSet<String>(); iterateStringDescriptors(rootContainer, new StringDescriptorVisitor<IComponent>() { public boolean visit(final IComponent component, final StringDescriptor descriptor) { if(descriptor.getBundleName() != null && !bundleNames.contains(descriptor.getBundleName())) { bundleNames.add(descriptor.getBundleName()); } return true; } }); return bundleNames; }
public void generatePushValue(final GeneratorAdapter generator, final Object value) { StringDescriptor descriptor = (StringDescriptor) value; if (descriptor == null) { generator.push((String)null); } else if (descriptor.getValue() !=null) { generator.push(descriptor.getValue()); } else { generator.push(descriptor.getBundleName()); generator.invokeStatic(myResourceBundleType, myGetBundleMethod); generator.push(descriptor.getKey()); generator.invokeVirtual(myResourceBundleType, myGetStringMethod); } }
public static ComponentItem createAnyComponentItem(final Project project) { ComponentItem result = new ComponentItem(project, "", null, null, new GridConstraints(), new HashMap<String, StringDescriptor>(), false, false, false); result.myAnyComponent = true; return result; }
public void writeStringDescriptor(final StringDescriptor descriptor, final String valueAttr, final String bundleAttr, final String keyAttr) { if(descriptor.getValue() != null){ // direct value addAttribute(valueAttr, descriptor.getValue()); if (descriptor.isNoI18n()) { addAttribute(UIFormXmlConstants.ATTRIBUTE_NOI18N, Boolean.TRUE.toString()); } } else{ // via resource bundle addAttribute(bundleAttr, descriptor.getBundleName()); addAttribute(keyAttr, descriptor.getKey()); } }