diff options
author | Dani Megert | 2008-03-14 10:10:52 +0000 |
---|---|---|
committer | Dani Megert | 2008-03-14 10:10:52 +0000 |
commit | 94005a35a2d591bce7d2dd75cfcf25de066d4210 (patch) | |
tree | 70baaf258b2e5c7820e33590b0c084a9de590df2 /org.eclipse.jface.text/src/org/eclipse | |
parent | e7b50643ffcf7cb7426373e8b3c23fade87a8a7b (diff) | |
download | eclipse.platform.text-94005a35a2d591bce7d2dd75cfcf25de066d4210.tar.gz eclipse.platform.text-94005a35a2d591bce7d2dd75cfcf25de066d4210.tar.xz eclipse.platform.text-94005a35a2d591bce7d2dd75cfcf25de066d4210.zip |
Committed Martin's patch to fix bug 221690: [content assist] code assist with colored labels
Diffstat (limited to 'org.eclipse.jface.text/src/org/eclipse')
4 files changed, 239 insertions, 2 deletions
diff --git a/org.eclipse.jface.text/src/org/eclipse/jface/internal/text/TableOwnerDrawSupport.java b/org.eclipse.jface.text/src/org/eclipse/jface/internal/text/TableOwnerDrawSupport.java new file mode 100644 index 00000000000..81b739e1469 --- /dev/null +++ b/org.eclipse.jface.text/src/org/eclipse/jface/internal/text/TableOwnerDrawSupport.java @@ -0,0 +1,148 @@ +/******************************************************************************* + * Copyright (c) 2008 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.jface.internal.text; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.StyleRange; +import org.eclipse.swt.events.DisposeEvent; +import org.eclipse.swt.events.DisposeListener; +import org.eclipse.swt.graphics.GC; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.Rectangle; +import org.eclipse.swt.graphics.TextLayout; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Event; +import org.eclipse.swt.widgets.Listener; +import org.eclipse.swt.widgets.Table; +import org.eclipse.swt.widgets.TableItem; + +import org.eclipse.jface.window.Window; + + +/** + * Adds owner draw support for tables. + * + * @since 3.4 + */ +public class TableOwnerDrawSupport implements Listener, DisposeListener { + + private static final String STYLED_RANGES_KEY= "styled_ranges"; //$NON-NLS-1$ + + private TextLayout fLayout; + + public static void install(Table table) { + TableOwnerDrawSupport listener= new TableOwnerDrawSupport(table.getDisplay()); + table.addDisposeListener(listener); + table.addListener(SWT.MeasureItem, listener); + table.addListener(SWT.EraseItem, listener); + table.addListener(SWT.PaintItem, listener); + } + + /** + * Stores the styled ranges in the given table item. + * + * @param item table item + * @param ranges the styled ranges or <code>null</code> to remove them + * @since 3.4 + */ + public static void storeStyleRanges(TableItem item, StyleRange[] ranges) { + item.setData(STYLED_RANGES_KEY, ranges); + } + + /** + * Returns the styled ranges which are stored in the given table item. + * + * @param item table item + * @return the styled ranges + * @since 3.4 + */ + private static StyleRange[] getStyledRanges(TableItem item) { + return (StyleRange[])item.getData(STYLED_RANGES_KEY); + } + + private TableOwnerDrawSupport(Display display) { + fLayout= new TextLayout(display); + fLayout.setOrientation(Window.getDefaultOrientation()); + } + + /* + * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event) + */ + public void handleEvent(Event event) { + switch (event.type) { + case SWT.MeasureItem: + break; + case SWT.EraseItem: + event.detail &= ~SWT.FOREGROUND; + break; + case SWT.PaintItem: + performPaint(event); + break; + } + } + + /** + * Performs the paint operation. + * + * @param event the event + */ + private void performPaint(Event event) { + TableItem item= (TableItem) event.item; + GC gc= event.gc; + + Image image = item.getImage(0); + if (image != null) { + Rectangle imageBounds = item.getImageBounds(0); + Rectangle bounds = image.getBounds(); + int x = imageBounds.x + Math.max(0, (imageBounds.width - bounds.width) / 2); + int y = imageBounds.y + Math.max(0, (imageBounds.height - bounds.height) / 2); + gc.drawImage(image, x, y); + } + + fLayout.setText(""); //$NON-NLS-1$ + fLayout.setText(item.getText(0)); + StyleRange[] ranges= getStyledRanges(item); + if (ranges != null) { + boolean isSelected= (event.detail & SWT.SELECTED) != 0; + for (int i= 0; i < ranges.length; i++) { + StyleRange curr= ranges[i]; + if (isSelected) { + curr= (StyleRange) curr.clone(); + curr.foreground= null; + curr.background= null; + } + fLayout.setStyle(curr, curr.start, curr.start + curr.length - 1); + } + } + + Rectangle textBounds = item.getTextBounds(0); + if (textBounds != null) { + Rectangle layoutBounds = fLayout.getBounds(); + int x = textBounds.x; + int y = textBounds.y + Math.max(0, (textBounds.height - layoutBounds.height) / 2); + fLayout.draw(gc, x, y); + } + + + if ((event.detail & SWT.FOCUSED) != 0) { + Rectangle focusBounds = item.getBounds(); + gc.drawFocus(focusBounds.x, focusBounds.y, focusBounds.width, focusBounds.height); + } + } + + /* + * @see org.eclipse.swt.events.DisposeListener#widgetDisposed(org.eclipse.swt.events.DisposeEvent) + */ + public void widgetDisposed(DisposeEvent e) { + fLayout.dispose(); + } +} + diff --git a/org.eclipse.jface.text/src/org/eclipse/jface/internal/text/link/contentassist/CompletionProposalPopup2.java b/org.eclipse.jface.text/src/org/eclipse/jface/internal/text/link/contentassist/CompletionProposalPopup2.java index 6cad3222ad8..a2a13579e03 100644 --- a/org.eclipse.jface.text/src/org/eclipse/jface/internal/text/link/contentassist/CompletionProposalPopup2.java +++ b/org.eclipse.jface.text/src/org/eclipse/jface/internal/text/link/contentassist/CompletionProposalPopup2.java @@ -17,6 +17,7 @@ import java.util.ArrayList; import java.util.List; import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.StyleRange; import org.eclipse.swt.custom.StyledText; import org.eclipse.swt.events.ControlEvent; import org.eclipse.swt.events.ControlListener; @@ -36,7 +37,9 @@ import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.TableItem; +import org.eclipse.jface.internal.text.TableOwnerDrawSupport; import org.eclipse.jface.resource.JFaceResources; +import org.eclipse.jface.viewers.StyledStringBuilder; import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.DocumentEvent; @@ -52,6 +55,7 @@ import org.eclipse.jface.text.TextUtilities; import org.eclipse.jface.text.contentassist.ICompletionProposal; import org.eclipse.jface.text.contentassist.ICompletionProposalExtension; import org.eclipse.jface.text.contentassist.ICompletionProposalExtension2; +import org.eclipse.jface.text.contentassist.ICompletionProposalExtension6; import org.eclipse.jface.text.contentassist.IContextInformation; @@ -101,6 +105,13 @@ class CompletionProposalPopup2 implements IContentAssistListener2 { private String fLineDelimiter; /** The most recently selected proposal. */ private ICompletionProposal fLastProposal; + /** + * Tells whether owner draw support is enabled. + * @since 3.4 + */ + private boolean fIsOwnerDrawSupportEnabled; + + private final IEditingSupport fFocusEditingSupport= new IEditingSupport() { public boolean isOriginator(DocumentEvent event, IRegion focus) { @@ -254,6 +265,10 @@ class CompletionProposalPopup2 implements IContentAssistListener2 { // fProposalShell= new Shell(control.getShell(), SWT.ON_TOP | SWT.RESIZE ); fProposalTable= new Table(fProposalShell, SWT.H_SCROLL | SWT.V_SCROLL); // fProposalTable= new Table(fProposalShell, SWT.H_SCROLL | SWT.V_SCROLL); + + + if (fIsOwnerDrawSupportEnabled) + TableOwnerDrawSupport.install(fProposalTable); fProposalTable.setLocation(0, 0); if (fAdditionalInfoController != null) @@ -531,7 +546,20 @@ class CompletionProposalPopup2 implements IContentAssistListener2 { item= new TableItem(fProposalTable, SWT.NULL); if (p.getImage() != null) item.setImage(p.getImage()); - item.setText(p.getDisplayString()); + + String displayString; + StyleRange[] styleRanges= null; + if (fIsOwnerDrawSupportEnabled && p instanceof ICompletionProposalExtension6) { + StyledStringBuilder stringBuilder= ((ICompletionProposalExtension6)p).getStyledDisplayString(); + displayString= stringBuilder.toString(); + styleRanges= stringBuilder.toStyleRanges(); + } else + displayString= p.getDisplayString(); + + item.setText(displayString); + if (fIsOwnerDrawSupportEnabled) + TableOwnerDrawSupport.storeStyleRanges(item, styleRanges); + item.setData(p); if (validate && validateProposal(document, p, endOffset, null)) { diff --git a/org.eclipse.jface.text/src/org/eclipse/jface/text/contentassist/CompletionProposalPopup.java b/org.eclipse.jface.text/src/org/eclipse/jface/text/contentassist/CompletionProposalPopup.java index 8e3e7955417..c96b33386e9 100644 --- a/org.eclipse.jface.text/src/org/eclipse/jface/text/contentassist/CompletionProposalPopup.java +++ b/org.eclipse.jface.text/src/org/eclipse/jface/text/contentassist/CompletionProposalPopup.java @@ -16,6 +16,7 @@ import java.util.List; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.BusyIndicator; +import org.eclipse.swt.custom.StyleRange; import org.eclipse.swt.events.ControlEvent; import org.eclipse.swt.events.ControlListener; import org.eclipse.swt.events.DisposeEvent; @@ -57,8 +58,10 @@ import org.eclipse.core.runtime.Assert; import org.eclipse.jface.bindings.keys.KeySequence; import org.eclipse.jface.bindings.keys.SWTKeySupport; import org.eclipse.jface.contentassist.IContentAssistSubjectControl; +import org.eclipse.jface.internal.text.TableOwnerDrawSupport; import org.eclipse.jface.resource.JFaceResources; import org.eclipse.jface.util.Geometry; +import org.eclipse.jface.viewers.StyledStringBuilder; import org.eclipse.jface.text.AbstractInformationControlManager; import org.eclipse.jface.text.BadLocationException; @@ -411,7 +414,13 @@ class CompletionProposalPopup implements IContentAssistListener { * @since 3.2 */ private String fEmptyMessage= null; + /** + * Tells whether owner draw support is enabled. + * @since 3.4 + */ + private boolean fIsOwnerDrawEnabled= true; + /** * Creates a new completion proposal popup for the given elements. * @@ -569,6 +578,9 @@ class CompletionProposalPopup implements IContentAssistListener { } else { fProposalTable= new Table(fProposalShell, SWT.H_SCROLL | SWT.V_SCROLL); } + + if (fIsOwnerDrawEnabled) + TableOwnerDrawSupport.install(fProposalTable); fProposalTable.setLocation(0, 0); if (fAdditionalInfoController != null) @@ -772,7 +784,19 @@ class CompletionProposalPopup implements IContentAssistListener { if (0 <= index && index < fFilteredProposals.length) { ICompletionProposal current= fFilteredProposals[index]; - item.setText(current.getDisplayString()); + String displayString; + StyleRange[] styleRanges= null; + if (fIsOwnerDrawEnabled && current instanceof ICompletionProposalExtension6) { + StyledStringBuilder stringBuilder= ((ICompletionProposalExtension6)current).getStyledDisplayString(); + displayString= stringBuilder.toString(); + styleRanges= stringBuilder.toStyleRanges(); + } else + displayString= current.getDisplayString(); + + item.setText(displayString); + if (fIsOwnerDrawEnabled) + TableOwnerDrawSupport.storeStyleRanges(item, styleRanges); + item.setImage(current.getImage()); item.setData(current); } else { diff --git a/org.eclipse.jface.text/src/org/eclipse/jface/text/contentassist/ICompletionProposalExtension6.java b/org.eclipse.jface.text/src/org/eclipse/jface/text/contentassist/ICompletionProposalExtension6.java new file mode 100644 index 00000000000..accad72035c --- /dev/null +++ b/org.eclipse.jface.text/src/org/eclipse/jface/text/contentassist/ICompletionProposalExtension6.java @@ -0,0 +1,37 @@ +/******************************************************************************* + * Copyright (c) 2008 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.jface.text.contentassist; + +import org.eclipse.jface.viewers.StyledStringBuilder; + + +/** + * Extends {@link org.eclipse.jface.text.contentassist.ICompletionProposal} with the following + * function: + * <ul> + * <li>Allow styled ranges in the display string.</li> + * </ul> + * + * @since 3.4 + */ +public interface ICompletionProposalExtension6 { + /** + * Returns the string to be displayed in the list of completion proposals. + * + * <p> + * If this interface implemented, {@link #getStyledDisplayString} will be used + * instead of {@link ICompletionProposal#getDisplayString()}. + * </p> + * + * @return the string to be displayed + */ + StyledStringBuilder getStyledDisplayString(); +} |