001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: ******************************************************************************/package org.eclipse.ui.internal.quickaccess;
011:
012: import org.eclipse.jface.resource.DeviceResourceException;
013: import org.eclipse.jface.resource.ImageDescriptor;
014: import org.eclipse.jface.resource.ResourceManager;
015: import org.eclipse.swt.SWT;
016: import org.eclipse.swt.graphics.Color;
017: import org.eclipse.swt.graphics.Image;
018: import org.eclipse.swt.graphics.Rectangle;
019: import org.eclipse.swt.graphics.TextLayout;
020: import org.eclipse.swt.graphics.TextStyle;
021: import org.eclipse.swt.widgets.Event;
022: import org.eclipse.swt.widgets.Table;
023: import org.eclipse.swt.widgets.TableItem;
024: import org.eclipse.ui.internal.IWorkbenchGraphicConstants;
025: import org.eclipse.ui.internal.WorkbenchImages;
026: import org.eclipse.ui.internal.WorkbenchPlugin;
027:
028: class QuickAccessEntry {
029: boolean firstInCategory;
030: boolean lastInCategory;
031: QuickAccessElement element;
032: QuickAccessProvider provider;
033: int[][] elementMatchRegions;
034: int[][] providerMatchRegions;
035:
036: QuickAccessEntry(QuickAccessElement element,
037: QuickAccessProvider provider, int[][] elementMatchRegions,
038: int[][] providerMatchRegions) {
039: this .element = element;
040: this .provider = provider;
041: this .elementMatchRegions = elementMatchRegions;
042: this .providerMatchRegions = providerMatchRegions;
043: }
044:
045: Image getImage(QuickAccessElement element,
046: ResourceManager resourceManager) {
047: Image image = findOrCreateImage(element.getImageDescriptor(),
048: resourceManager);
049: if (image == null) {
050: image = WorkbenchImages
051: .getImage(IWorkbenchGraphicConstants.IMG_OBJ_ELEMENT);
052: }
053: return image;
054: }
055:
056: private Image findOrCreateImage(ImageDescriptor imageDescriptor,
057: ResourceManager resourceManager) {
058: if (imageDescriptor == null) {
059: return null;
060: }
061: Image image = (Image) resourceManager.find(imageDescriptor);
062: if (image == null) {
063: try {
064: image = resourceManager.createImage(imageDescriptor);
065: } catch (DeviceResourceException e) {
066: WorkbenchPlugin.log(e);
067: }
068: }
069: return image;
070: }
071:
072: /**
073: * @param event
074: * @param boldStyle
075: */
076: public void measure(Event event, TextLayout textLayout,
077: ResourceManager resourceManager, TextStyle boldStyle) {
078: Table table = ((TableItem) event.item).getParent();
079: textLayout.setFont(table.getFont());
080: switch (event.index) {
081: case 0:
082: if (firstInCategory || providerMatchRegions.length > 0) {
083: textLayout.setText(provider.getName());
084: for (int i = 0; i < providerMatchRegions.length; i++) {
085: int[] matchRegion = providerMatchRegions[i];
086: textLayout.setStyle(boldStyle, matchRegion[0],
087: matchRegion[1]);
088: }
089: }
090: break;
091: case 1:
092: Image image = getImage(element, resourceManager);
093: Rectangle imageRect = image.getBounds();
094: event.width += imageRect.width + 2;
095: event.height = Math.max(event.height, imageRect.height + 2);
096: textLayout.setText(element.getLabel());
097: for (int i = 0; i < elementMatchRegions.length; i++) {
098: int[] matchRegion = elementMatchRegions[i];
099: textLayout.setStyle(boldStyle, matchRegion[0],
100: matchRegion[1]);
101: }
102: break;
103: }
104: Rectangle rect = textLayout.getBounds();
105: event.width += rect.width + 2;
106: event.height = Math.max(event.height, rect.height + 2);
107: }
108:
109: /**
110: * @param event
111: * @param textLayout
112: * @param resourceManager
113: * @param boldStyle
114: */
115: public void paint(Event event, TextLayout textLayout,
116: ResourceManager resourceManager, TextStyle boldStyle,
117: Color grayColor) {
118: final Table table = ((TableItem) event.item).getParent();
119: textLayout.setFont(table.getFont());
120: switch (event.index) {
121: case 0:
122: if (firstInCategory || providerMatchRegions.length > 0) {
123: textLayout.setText(provider.getName());
124: for (int i = 0; i < providerMatchRegions.length; i++) {
125: int[] matchRegion = providerMatchRegions[i];
126: textLayout.setStyle(boldStyle, matchRegion[0],
127: matchRegion[1]);
128: }
129: if (providerMatchRegions.length > 0 && !firstInCategory) {
130: event.gc.setForeground(grayColor);
131: }
132: Rectangle availableBounds = ((TableItem) event.item)
133: .getTextBounds(event.index);
134: Rectangle requiredBounds = textLayout.getBounds();
135: textLayout
136: .draw(
137: event.gc,
138: availableBounds.x + 1,
139: availableBounds.y
140: + (availableBounds.height - requiredBounds.height)
141: / 2);
142: }
143: break;
144: case 1:
145: Image image = getImage(element, resourceManager);
146: event.gc.drawImage(image, event.x + 1, event.y + 1);
147: textLayout.setText(element.getLabel());
148: for (int i = 0; i < elementMatchRegions.length; i++) {
149: int[] matchRegion = elementMatchRegions[i];
150: textLayout.setStyle(boldStyle, matchRegion[0],
151: matchRegion[1]);
152: }
153: Rectangle availableBounds = ((TableItem) event.item)
154: .getTextBounds(event.index);
155: Rectangle requiredBounds = textLayout.getBounds();
156: textLayout.draw(event.gc, availableBounds.x + 1
157: + image.getBounds().width, availableBounds.y
158: + (availableBounds.height - requiredBounds.height)
159: / 2);
160: break;
161: }
162: if (lastInCategory) {
163: event.gc.setForeground(table.getDisplay().getSystemColor(
164: SWT.COLOR_GRAY));
165: Rectangle bounds = ((TableItem) event.item)
166: .getBounds(event.index);
167: event.gc.drawLine(Math.max(0, bounds.x - 1), bounds.y
168: + bounds.height - 1, bounds.x + bounds.width,
169: bounds.y + bounds.height - 1);
170: }
171: }
172:
173: /**
174: * @param event
175: */
176: public void erase(Event event) {
177: // We are only custom drawing the foreground.
178: event.detail &= ~SWT.FOREGROUND;
179: }
180: }
|