[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.platform] SimpleContentProposalProvider with LabelProvider gives a wrong proposal text

Hello!
I making a content assist for a text field.

private void createContentAssist(Text field) {
		ControlDecoration dec = new ControlDecoration(field, SWT.CENTER
				| SWT.LEAD);
		dec.setImage(getContentAssistDecoration().getImage());
		dec.setDescriptionText(getContentAssistDecoration().getDescription()
				+ "(" + CONTENT_ASSIST_KEYSTROKE + ")");
		dec.setShowOnlyOnFocus(true);

		char[] autoActivationCharacters = new char[] { '#', '(' };
		KeyStroke keyStroke = null;
		try {
			keyStroke = KeyStroke.getInstance(CONTENT_ASSIST_KEYSTROKE);
		} catch (ParseException e) {
		}
		ContentProposalAdapter adapter = new ContentProposalAdapter(field,
				new TextContentAdapter(), new SimpleContentProposalProvider(
						toArrayOfStrings(getTreesList())), keyStroke,
				autoActivationCharacters);
		adapter.setLabelProvider(getLabelProvider());

	}

	private static final String CONTENT_ASSIST_KEYSTROKE = "Ctrl+Space";

	private FieldDecoration getContentAssistDecoration() {
		return FieldDecorationRegistry.getDefault().getFieldDecoration(
				FieldDecorationRegistry.DEC_CONTENT_PROPOSAL);
	}

	private String[] toArrayOfStrings(Object[] objects) {
		String[] result = new String[objects.length];
		for (int i = 0; i < objects.length; i++)
			result[i] = objects[i].toString();
		return result;
	}

and when launching it gives me icons in content assist (that's why i needed a LabelProvider) and wrong text, something like: org.eclipse.jface.fieldassist.SimpleContentProposalProvider$1@4ca6b6
org.eclipse.jface.fieldassist.SimpleContentProposalProvider$1@16b904d
org.eclipse.jface.fieldassist.SimpleContentProposalProvider$1@cec78d


getText in my LabelProvider uses toString().
this happens because of this code:

public class ContentProposalAdapter {
..
/*
		 * Get the string for the specified proposal. Always return a String of
		 * some kind.
		 */
		private String getString(IContentProposal proposal) {
			if (proposal == null) {
				return EMPTY;
			}
			if (labelProvider == null) {
				return proposal.getLabel() == null ? proposal.getContent()
						: proposal.getLabel();
			}
			return labelProvider.getText(proposal);
		}

proposal is defined in SimpleContentProposalProvider like that:

public class SimpleContentProposalProvider implements IContentProposalProvider {
..
/*
* Make an IContentProposal for showing the specified String.
*/
private IContentProposal makeContentProposal(final String proposal) {
return new IContentProposal() {
public String getContent() {
return proposal;
}


			public String getDescription() {
				return null;
			}

			public String getLabel() {
				return null;
			}

			public int getCursorPosition() {
				return proposal.length();
			}
		};
	}
}

so this proposal doesn't return content in toString().
i've bypassed this by changing a getText() in my LabelProvider to:

public String getText(Object element) {
		if (element instanceof IContentProposal) {
			IContentProposal proposal = ((IContentProposal) element);
			return proposal.getLabel() == null ? proposal.getContent()
					: proposal.getLabel();
		}

		return super.getText(element);
	}

but it is not quite right, isn't it?

and i suggest to add this code to SimpleContentProposalProvider:

	private IContentProposal makeContentProposal(final String proposal) {
		return new IContentProposal() {
		...
		public String toString() {
				return proposal;
			}
		};
	}


Thanks

Kurill