public void generateKey(NLSSubstitution[] substitutions) { if (fState != EXTERNALIZED || ((fState == EXTERNALIZED) && hasStateChanged())) { int min= Integer.MAX_VALUE; int max= Integer.MIN_VALUE; for (int i= 0; i < substitutions.length; i++) { NLSSubstitution substitution= substitutions[i]; if (substitution == this || substitution.fState != EXTERNALIZED) continue; try { int value= Integer.parseInt(substitution.internalGetKeyWithoutPrefix()); min= Math.min(min, value); max= Math.max(max, value); } catch (NumberFormatException ex) { } } fCachedPrefixPlusKey= null; if (min == Integer.MAX_VALUE) fKey= createKey(0); else if (min > 0) fKey= createKey(min-1); else fKey= createKey(max + 1); } }
/** * @see ICellModifier#modify(Object, String, Object) */ public void modify(Object element, String property, Object value) { if (element instanceof TableItem) { Object data= ((TableItem) element).getData(); if (data instanceof NLSSubstitution) { NLSSubstitution substitution= (NLSSubstitution) data; if (PROPERTIES[KEY_PROP].equals(property)) { String string = (String)value; string = windEscapeChars(string); substitution.setKey(string); } if (PROPERTIES[VAL_PROP].equals(property)) { String string = (String)value; string = windEscapeChars(string); substitution.setValue(string); } if (PROPERTIES[STATE_PROP].equals(property)) { substitution.setState(((Integer) value).intValue()); if ((substitution.getState() == NLSSubstitution.EXTERNALIZED) && substitution.hasStateChanged()) { substitution.generateKey(fSubstitutions); } } } validateKeys(false); fTableViewer.update(data, null); } } }
static boolean doReplace(NLSSubstitution substitution, Map newKeyToSubstMap, Map oldKeyToSubstMap) { if (substitution.getState() != NLSSubstitution.EXTERNALIZED || substitution.hasStateChanged() || substitution.getInitialValue() == null) { return false; // was not in property file before } if (oldKeyToSubstMap.get(substitution.getInitialKey()) != substitution) { return false; // not the owner of this key } if (substitution.isKeyRename() || substitution.isValueRename()) { if (newKeyToSubstMap.get(substitution.getKey()) == substitution) { // only rename if we're not a duplicate. duplicates will be removed return true; } } return false; }
private boolean hasNewOrMissingSubstitutions() { for (int i= 0; i < fSubstitutions.length; i++) { NLSSubstitution curr= fSubstitutions[i]; if (curr.getInitialState() == NLSSubstitution.INTERNALIZED) { return true; } if (curr.getInitialState() == NLSSubstitution.EXTERNALIZED && curr.getInitialValue() == null) { return true; } } return false; }
private void createDefaultExternalization(NLSSubstitution[] substitutions, String defaultPrefix) { for (int i= 0; i < substitutions.length; i++) { NLSSubstitution substitution= substitutions[i]; if (substitution.getState() == NLSSubstitution.INTERNALIZED) { substitution.setState(NLSSubstitution.EXTERNALIZED); substitution.generateKey(substitutions); } } }
public static void updateSubtitutions(NLSSubstitution[] substitutions, Properties props, String accessorClassName) { for (int i= 0; i < substitutions.length; i++) { NLSSubstitution substitution= substitutions[i]; if ((substitution.getState() == NLSSubstitution.EXTERNALIZED) && !substitution.hasStateChanged()) { substitution.setInitialValue(props.getProperty(substitution.getKey())); substitution.setUpdatedAccessor(accessorClassName); } } }
private void addNLS(NLSSubstitution sub, TextChange change, String accessorName) { if (sub.getState() == NLSSubstitution.INTERNALIZED) return; NLSElement element= sub.getNLSElement(); addAccessor(sub, change, accessorName); if (!fIsEclipseNLS || sub.getState() == NLSSubstitution.IGNORED) { // Add $NON-NLS-n tag String arg= sub.getState() == NLSSubstitution.EXTERNALIZED ? sub.getKey() : sub.getValueNonEmpty(); String name= Messages.format(NLSMessages.NLSSourceModifier_add_tag, arg); TextChangeCompatibility.addTextEdit(change, name, createAddTagChange(element)); } }
int newState= substitution.getState(); if (substitution.hasStateChanged()) { if (newState == NLSSubstitution.EXTERNALIZED) { if (substitution.getInitialState() == NLSSubstitution.INTERNALIZED) { sourceModification.addNLS(substitution, change, accessorClassName); } else if (substitution.getInitialState() == NLSSubstitution.IGNORED) { sourceModification.addAccessor(substitution, change, accessorClassName); if (substitution.getInitialState() == NLSSubstitution.IGNORED) { sourceModification.deleteTag(substitution, change); if (substitution.isValueRename()) { sourceModification.replaceValue(substitution, change); } else if (substitution.getInitialState() == NLSSubstitution.EXTERNALIZED) { sourceModification.deleteAccessor(substitution, change, cu); if (!isEclipseNLS) if (substitution.getInitialState() == NLSSubstitution.INTERNALIZED) { sourceModification.addNLS(substitution, change, accessorClassName); if (substitution.isValueRename()) { sourceModification.replaceValue(substitution, change); if (substitution.getInitialState() == NLSSubstitution.EXTERNALIZED) { sourceModification.deleteAccessor(substitution, change, cu); if (substitution.isKeyRename()) { sourceModification.replaceKey(substitution, change); if (substitution.isAccessorRename()) {
static boolean doRemove(NLSSubstitution substitution, Map<String, NLSSubstitution> newKeyToSubstMap, Map<String, NLSSubstitution> oldKeyToSubstMap) { if (substitution.getInitialState() != NLSSubstitution.EXTERNALIZED || substitution.getInitialKey() == null) { return false; // was not in property file before } if (oldKeyToSubstMap.get(substitution.getInitialKey()) != substitution) { return false; // not the owner of this key } if (substitution.hasStateChanged()) { return true; // was externalized, but not anymore } else { if (substitution.hasPropertyFileChange() && newKeyToSubstMap.get(substitution.getKey()) != substitution) { return true; // has been changed to an already existing } } return false; }
private void setSelectedTasks(int state) { Assert.isTrue(state == NLSSubstitution.EXTERNALIZED || state == NLSSubstitution.IGNORED || state == NLSSubstitution.INTERNALIZED); List selected= getSelectedTableEntries(); String[] props= new String[]{PROPERTIES[STATE_PROP]}; for (Iterator iter= selected.iterator(); iter.hasNext();) { NLSSubstitution substitution= (NLSSubstitution) iter.next(); substitution.setState(state); if ((substitution.getState() == NLSSubstitution.EXTERNALIZED) && substitution.hasStateChanged()) { substitution.generateKey(fSubstitutions); } } fTableViewer.update(selected.toArray(), props); fTableViewer.getControl().setFocus(); updateButtonStates((IStructuredSelection) fTableViewer.getSelection()); }
private static void addReplaceEdits(TextChange textChange, NLSSubstitution[] substitutions, Map<String, NLSSubstitution> newKeyToSubstMap, Map<String, NLSSubstitution> oldKeyToSubstMap, PropertyFileDocumentModel model) { for (int i= 0; i < substitutions.length; i++) { NLSSubstitution substitution= substitutions[i]; if (doReplace(substitution, newKeyToSubstMap, oldKeyToSubstMap)) { KeyValuePair initialPair= new KeyValuePair(substitution.getInitialKey(), substitution.getInitialValue()); String key= PropertyFileDocumentModel.escape(substitution.getKey(), false); String value= PropertyFileDocumentModel.escape(substitution.getValue(), true) + model.getLineDelimiter(); KeyValuePair newPair= new KeyValuePair(key, value); TextEdit edit= model.replace(initialPair, newPair); if (edit != null) { TextChangeCompatibility.addTextEdit(textChange, Messages.format(NLSMessages.NLSPropertyFileModifier_replace_entry, BasicElementLabels.getJavaElementName(substitution.getKey())), edit); } } } }
private static void addReplaceEdits(TextChange textChange, NLSSubstitution[] substitutions, Map newKeyToSubstMap, Map oldKeyToSubstMap, PropertyFileDocumentModel model) { for (int i= 0; i < substitutions.length; i++) { NLSSubstitution substitution= substitutions[i]; if (doReplace(substitution, newKeyToSubstMap, oldKeyToSubstMap)) { KeyValuePair initialPair= new KeyValuePair(substitution.getInitialKey(), substitution.getInitialValue()); KeyValuePair newPair= new KeyValuePair(substitution.getKey(), substitution.getValueNonEmpty()); TextEdit edit= model.replace(initialPair, newPair); if (edit != null) { TextChangeCompatibility.addTextEdit(textChange, Messages.format(NLSMessages.NLSPropertyFileModifier_replace_entry, substitution.getKey()), edit); } } } }
private void replaceKey(NLSSubstitution substitution, TextChange change) { Region region= substitution.getNLSElement().getPosition(); String[] args= {substitution.getInitialKey(), substitution.getKey()}; ReplaceEdit replaceEdit; if (fIsEclipseNLS) replaceEdit= new ReplaceEdit(region.getOffset(), region.getLength(), substitution.getKey()); else replaceEdit= new ReplaceEdit(region.getOffset(), region.getLength(), '\"' + unwindEscapeChars(substitution.getKey()) + '\"'); // TextChangeCompatibility.addTextEdit(change, Messages.format(NLSMessages.NLSSourceModifier_replace_key, args), replaceEdit); }
private void replaceValue(NLSSubstitution substitution, TextChange change) { Region region= substitution.getNLSElement().getPosition(); String[] args= {substitution.getInitialValue(), substitution.getValueNonEmpty()}; TextChangeCompatibility.addTextEdit(change, Messages.format(NLSMessages.NLSSourceModifier_replace_value, args), new ReplaceEdit(region.getOffset(), region.getLength(), '\"' + unwindEscapeChars(substitution.getValueNonEmpty()) + '\"')); // }
private RefactoringStatus checkKeys() { RefactoringStatus result= new RefactoringStatus(); NLSSubstitution[] subs= fSubstitutions; for (int i= 0; i < subs.length; i++) { NLSSubstitution substitution= subs[i]; if ((substitution.getState() == NLSSubstitution.EXTERNALIZED) && substitution.hasStateChanged()) { result.merge(checkKey(substitution.getKey())); } } return result; }
/** * Maps the old keys to a substitutions. If a substitution is not in the map then it is a duplicate. * * @param substitutions the substitutions to add to the map * @return the map containing the substitutions */ static HashMap<String, NLSSubstitution> getOldKeyToSubstitutionMap(NLSSubstitution[] substitutions) { HashMap<String, NLSSubstitution> keyToSubstMap= new HashMap<>(substitutions.length); // find all duplicates for (int i= 0; i < substitutions.length; i++) { NLSSubstitution curr= substitutions[i]; if (curr.getInitialState() == NLSSubstitution.EXTERNALIZED) { String key= curr.getInitialKey(); if (key != null) { NLSSubstitution fav= keyToSubstMap.get(key); if (fav == null || (fav.hasStateChanged() && !curr.hasStateChanged())) { keyToSubstMap.put(key, curr); // store if first or if stored will not be externalized anymore } } } } return keyToSubstMap; }
public NLSInputDialog(Shell parent, NLSSubstitution substitution) { super(parent); setTitle(NLSUIMessages.ExternalizeWizardPage_NLSInputDialog_Title); fSubstitution= substitution; fMessageField= new DialogField(); if (substitution.getState() == NLSSubstitution.EXTERNALIZED) { fMessageField.setLabelText(NLSUIMessages.ExternalizeWizardPage_NLSInputDialog_ext_Label); } else { fMessageField.setLabelText(NLSUIMessages.ExternalizeWizardPage_NLSInputDialog_Label); } fKeyField= new StringDialogField(); fKeyField.setLabelText(NLSUIMessages.ExternalizeWizardPage_NLSInputDialog_Enter_key); fKeyField.setDialogFieldListener(this); fValueField= new StringDialogField(); fValueField.setLabelText(NLSUIMessages.ExternalizeWizardPage_NLSInputDialog_Enter_value); fValueField.setDialogFieldListener(this); if (substitution.getState() == NLSSubstitution.EXTERNALIZED) { fKeyField.setText(substitution.getKeyWithoutPrefix()); } else { fKeyField.setText(""); //$NON-NLS-1$ } fValueField.setText(substitution.getValueNonEmpty()); }
/** * @see ICellModifier#getValue(Object, String) */ public Object getValue(Object element, String property) { if (element instanceof NLSSubstitution) { NLSSubstitution substitution= (NLSSubstitution) element; String res= null; if (PROPERTIES[KEY_PROP].equals(property)) { res= substitution.getKeyWithoutPrefix(); } else if (PROPERTIES[VAL_PROP].equals(property)) { res= substitution.getValue(); } else if (PROPERTIES[STATE_PROP].equals(property)) { return new Integer(substitution.getState()); } if (res != null) { return unwindEscapeChars(res); } return ""; //$NON-NLS-1$ } return ""; //$NON-NLS-1$ }
private Image getNLSImage(NLSSubstitution sub) { if ((sub.getValue() == null) && (sub.getKey() != null)) { // Missing keys JavaElementImageDescriptor imageDescriptor= new JavaElementImageDescriptor(getNLSImageDescriptor(sub.getState()), JavaElementImageDescriptor.WARNING, JavaElementImageProvider.SMALL_SIZE); return JavaPlugin.getImageDescriptorRegistry().get(imageDescriptor); } else if (sub.isConflicting(fSubstitutions) || !isKeyValid(sub, null)) { JavaElementImageDescriptor imageDescriptor= new JavaElementImageDescriptor(getNLSImageDescriptor(sub.getState()), JavaElementImageDescriptor.ERROR, JavaElementImageProvider.SMALL_SIZE); return JavaPlugin.getImageDescriptorRegistry().get(imageDescriptor); } else { return getNLSImage(sub.getState()); } }
private void removeKey(NLSSubstitution sub, TextChange change) { ASTNode node= findField(fRoot, sub.getInitialKey()); if (node == null) return; String name= Messages.format(NLSMessages.AccessorClassModifier_remove_entry, BasicElementLabels.getJavaElementName(sub.getKey())); TextEditGroup editGroup= new TextEditGroup(name); fListRewrite.remove(node, editGroup); change.addTextEditGroup(editGroup); fFields.remove(node); }