Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 72424 Details for
Bug 188333
[Viewers] Label provider for multi-colored TableItems/TreeItems
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
Snippet for multi line labels
Snippet042MultiLineProvider.java (text/plain), 8.79 KB, created by
Michael Chervil
on 2007-06-25 18:08:22 EDT
(
hide
)
Description:
Snippet for multi line labels
Filename:
MIME Type:
Creator:
Michael Chervil
Created:
2007-06-25 18:08:22 EDT
Size:
8.79 KB
patch
obsolete
>/******************************************************************************* > * Copyright (c) 2007 Michael Krkoska 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: > * Michael Krkoska - initial API and implementation > *******************************************************************************/ >package org.eclipse.jface.snippets.viewers; > >import java.util.ArrayList; >import java.util.Iterator; >import java.util.List; >import java.util.regex.Matcher; >import java.util.regex.Pattern; > >import org.eclipse.jface.resource.JFaceResources; >import org.eclipse.jface.text.TextPresentation; >import org.eclipse.jface.viewers.ArrayContentProvider; >import org.eclipse.jface.viewers.ColumnPixelData; >import org.eclipse.jface.viewers.ITableColorProvider; >import org.eclipse.jface.viewers.ITableFontProvider; >import org.eclipse.jface.viewers.ITableLabelProvider; >import org.eclipse.jface.viewers.MultiLineLabelProvider; >import org.eclipse.jface.viewers.StyledLabelProvider; >import org.eclipse.jface.viewers.TableLayout; >import org.eclipse.jface.viewers.TableViewer; >import org.eclipse.swt.SWT; >import org.eclipse.swt.custom.StyleRange; >import org.eclipse.swt.graphics.Color; >import org.eclipse.swt.graphics.Font; >import org.eclipse.swt.graphics.FontData; >import org.eclipse.swt.graphics.Image; >import org.eclipse.swt.layout.GridData; >import org.eclipse.swt.layout.GridLayout; >import org.eclipse.swt.widgets.Composite; >import org.eclipse.swt.widgets.Display; >import org.eclipse.swt.widgets.Event; >import org.eclipse.swt.widgets.Label; >import org.eclipse.swt.widgets.Listener; >import org.eclipse.swt.widgets.Shell; >import org.eclipse.swt.widgets.TableColumn; >import org.eclipse.swt.widgets.Text; > >/** > * A TableViewer using a MultiLineLabelProvider to show some of the possibilities: > * <ul> > * <li>placing the image provided by the label provider anywhere in the label</li> > * <li>multi-line labels (currently only by placing line breaks in the label)</li> > * <li>multi font labels</li> > * <li>multi colored labels</li> > * </ul> > * > * @author Michael Krkoska <michael.krkoska@gmail.com> > */ >public class Snippet042MultiLineProvider { > > private static final String[] TEXT = new String[] { > "TextStyle defines a set of styles that can be\n" + > "applied to a range of text. The hashCode() method\n" + > "in this class uses the values of the public fields to", > "compute the hash value.\uFFFC When storing instances\n" + > "of the class in hashed collections do not modify\n" + > "these fields after the object has been inserted.", > "Application code does not need to explicitly\n" + > "release the resources managed and thus no\n" + > "dispose() method is provided.\uFFFC", }; > private static final Pattern METHODS = Pattern.compile("\\w+\\(\\)"); > private static final int SHELL_WIDTH = 640; > private static final Display DISPLAY = Display.getDefault(); > private static int IMAGE_SIZE = 16; > private static final Image IMAGE1 = new Image(DISPLAY, DISPLAY.getSystemImage(SWT.ICON_WARNING).getImageData().scaledTo(IMAGE_SIZE, IMAGE_SIZE)); > private static final Image IMAGE2 = new Image(DISPLAY, DISPLAY.getSystemImage(SWT.ICON_ERROR).getImageData().scaledTo(IMAGE_SIZE, IMAGE_SIZE)); > > > public static void main( String[] args ) { > > Shell shell = new Shell(DISPLAY, SWT.CLOSE); > shell.setSize(SHELL_WIDTH, 300); > shell.setLayout(new GridLayout(2, false)); > > Snippet042MultiLineProvider example = new Snippet042MultiLineProvider(); > example.createPartControl(shell); > > shell.open(); > > while ( !shell.isDisposed() ) { > if ( !DISPLAY.readAndDispatch() ) { > DISPLAY.sleep(); > } > } > DISPLAY.dispose(); > } > > private TableViewer viewer; > > private Text text; > > public Snippet042MultiLineProvider() { > int defaultSize = JFaceResources.getDefaultFontDescriptor().getFontData()[0].getHeight(); > JFaceResources.getFontRegistry().put("Courier", new FontData[] { new FontData("Courier New", defaultSize, SWT.NORMAL) }); > FontData[] fontData = JFaceResources.getFontRegistry().defaultFontDescriptor().getFontData(); > fontData[0].setHeight(fontData[0].getHeight() + 2); > JFaceResources.getFontRegistry().put("BiggerDefault", fontData); > } > > public void createPartControl( Composite parent ) { > Label label = new Label(parent, SWT.NONE); > label.setText("type something here:"); > > text = new Text(parent, SWT.NONE); > text.setText("hash"); > text.setSelection(0, 4); > text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); > text.addListener(SWT.Modify, new Listener() { > > public void handleEvent( Event event ) { > viewer.getControl().redraw(); > } > }); > > // SWT.FULL_SELECTION is needed on win32 as long as Bug 168807 exists > viewer = new TableViewer(parent, SWT.FULL_SELECTION); > > GridData data = new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.FILL_BOTH); > data.horizontalSpan = 2; > viewer.getControl().setLayoutData(data); > > viewer.setContentProvider(new ArrayContentProvider()); > createColumns(); > > MyMultiLineLabelProvider multiLineLabelProvider = new MyMultiLineLabelProvider(StyledLabelProvider.CENTER_ITEMS_VERTICALLY | StyledLabelProvider.CENTER_TEXT_VERTICALLY); > viewer.setLabelProvider(multiLineLabelProvider); > MultiLineLabelProvider.setUpOwnerDraw(viewer); > > viewer.setInput(TEXT); > } > > private void createColumns() { > TableLayout layout = new TableLayout(); > viewer.getTable().setLayout(layout); > viewer.getTable().setHeaderVisible(true); > viewer.getTable().setLinesVisible(true); > > int width = SHELL_WIDTH - 2 * 5 - viewer.getTable().getShell().computeTrim(0, 0, 0, 0).width; > int width1 = width / 2; > int width2 = width - width1; > > TableColumn tc = new TableColumn(viewer.getTable(), SWT.NONE); > tc.setText("col1"); > layout.addColumnData(new ColumnPixelData(width1)); > > tc = new TableColumn(viewer.getTable(), SWT.NONE); > tc.setText("col2"); > layout.addColumnData(new ColumnPixelData(width2)); > } > > private final class MyMultiLineLabelProvider extends MultiLineLabelProvider implements ITableLabelProvider, ITableColorProvider, ITableFontProvider { > > public MyMultiLineLabelProvider(int style) { > super(style); > } > > public StyleRange[] getStyleRanges( Object element, int columnIndex, String label ) { > String searchText = text.getText(); > TextPresentation tp = new TextPresentation(); > if ( searchText.length() > 0 ) { > Color yellow = text.getDisplay().getSystemColor(SWT.COLOR_YELLOW); > int index = -1; > while ( (index = label.indexOf(searchText, index + 1)) >= 0 ) { > StyleRange styleRange = new StyleRange(index, searchText.length(), null, yellow); > tp.mergeStyleRange(styleRange); > } > } > > Font courier = JFaceResources.getFont("Courier"); > Matcher matcher = METHODS.matcher(label); > while ( matcher.find() ) { > StyleRange styleRange = new StyleRange(matcher.start(), matcher.end() - matcher.start(), null, null); > styleRange.font = courier; > styleRange.fontStyle = SWT.BOLD; > tp.mergeStyleRange(styleRange); > } > > List ranges = new ArrayList(); > for ( Iterator iterator = tp.getAllStyleRangeIterator(); iterator.hasNext(); ) { > ranges.add(iterator.next()); > } > return (StyleRange[])ranges.toArray(new StyleRange[ranges.size()]); > } > > public Color getBackground( Object element, int columnIndex ) { > return null; > } > > public Image getColumnImage( Object element, int columnIndex ) { > return columnIndex == 0 ? IMAGE1 : IMAGE2; > } > > public String getColumnText( Object element, int columnIndex ) { > return element.toString(); > } > > public Font getFont( Object element, int columnIndex ) { > if ( element == TEXT[(columnIndex + 2) % 3] ) { > return JFaceResources.getFont("BiggerDefault"); > } > return null; > } > > public Color getForeground( Object element, int columnIndex ) { > if ( element == TEXT[columnIndex + 1] ) { > return DISPLAY.getSystemColor(SWT.COLOR_DARK_RED); > } > return null; > } > } > >}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 188333
:
72297
|
72298
|
72299
|
72300
|
72421
|
72422
|
72423
|
72424
|
72495
|
72496
|
72539
|
72542
|
72592
|
73167
|
73272
|
73353
|
73614
|
73626
|
73633
|
73818
|
80884
|
80912
|
82965
|
83672