001: /*******************************************************************************
002: * Copyright (c) 2004, 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.presentations.defaultpresentation;
011:
012: import org.eclipse.jface.resource.FontRegistry;
013: import org.eclipse.jface.util.Geometry;
014: import org.eclipse.swt.custom.CTabFolder;
015: import org.eclipse.swt.custom.CTabItem;
016: import org.eclipse.swt.graphics.Font;
017: import org.eclipse.swt.graphics.Rectangle;
018: import org.eclipse.ui.PlatformUI;
019: import org.eclipse.ui.internal.IWorkbenchThemeConstants;
020: import org.eclipse.ui.internal.WorkbenchMessages;
021: import org.eclipse.ui.internal.presentations.util.PartInfo;
022: import org.eclipse.ui.internal.presentations.util.WidgetTabItem;
023: import org.eclipse.ui.internal.util.Util;
024:
025: import com.ibm.icu.text.MessageFormat;
026:
027: /**
028: * @since 3.1
029: */
030: public class DefaultTabItem extends WidgetTabItem {
031:
032: public static String DIRTY_PREFIX = "*"; //$NON-NLS-1$
033:
034: private boolean busy = false;
035:
036: private boolean bold = false;
037:
038: private Font lastFont = null;
039:
040: private String shortName = Util.ZERO_LENGTH_STRING;
041:
042: private String longName = Util.ZERO_LENGTH_STRING;
043:
044: public DefaultTabItem(CTabFolder parent, int index, int flags) {
045: super (getTab(parent, index, flags));
046: updateFont();
047: }
048:
049: /**
050: * Get a new tab for the receiver.
051: *
052: * @return CTabItem
053: */
054: private static CTabItem getTab(CTabFolder parent, int index,
055: int flags) {
056: return new CTabItem(parent, flags, index);
057: }
058:
059: /*
060: * (non-Javadoc)
061: *
062: * @see org.eclipse.ui.internal.presentations.util.AbstractTabItem#getBounds()
063: */
064: public Rectangle getBounds() {
065: return Geometry.toDisplay(getItem().getParent(), getItem()
066: .getBounds());
067: }
068:
069: public CTabItem getItem() {
070: return (CTabItem) getWidget();
071: }
072:
073: /*
074: * (non-Javadoc)
075: *
076: * @see org.eclipse.ui.internal.presentations.util.AbstractTabItem#isShowing()
077: */
078: public boolean isShowing() {
079: return getItem().isShowing();
080: }
081:
082: /*
083: * (non-Javadoc)
084: *
085: * @see org.eclipse.ui.internal.presentations.util.AbstractTabItem#setInfo(org.eclipse.ui.internal.presentations.util.PartInfo)
086: */
087: public void setInfo(PartInfo info) {
088: CTabItem tabItem = getItem();
089:
090: shortName = computeShortName(info);
091: longName = computeLongName(info);
092:
093: updateTabText();
094:
095: if (tabItem.getImage() != info.image) {
096: tabItem.setImage(info.image);
097: }
098:
099: String toolTipText = info.toolTip;
100: if (toolTipText.equals(Util.ZERO_LENGTH_STRING)) {
101: toolTipText = null;
102: }
103:
104: if (!Util.equals(toolTipText, tabItem.getToolTipText())) {
105: tabItem.setToolTipText(toolTipText);
106: }
107: }
108:
109: public void updateTabText() {
110: CTabItem tabItem = getItem();
111:
112: String newName = tabItem.getParent().getSingle() ? longName
113: : shortName;
114:
115: newName = escapeAmpersands(newName);
116:
117: if (!Util.equals(newName, tabItem.getText())) {
118: tabItem.setText(newName);
119: }
120: }
121:
122: /**
123: * Escapes all the ampersands in the given string such that they can be
124: * displayed verbatim in an SWT label rather than treated as accelerators.
125: *
126: * @since 3.1
127: *
128: * @return a string where all ampersands are escaped
129: */
130: public static String escapeAmpersands(String input) {
131: StringBuffer title = new StringBuffer(input.length());
132: for (int i = 0; i < input.length(); i++) {
133: char character = input.charAt(i);
134: title.append(character);
135: if (character == '&') {
136: title.append(character); // escape ampersand
137: }
138: }
139: return title.toString();
140: }
141:
142: /*
143: * (non-Javadoc)
144: *
145: * @see org.eclipse.ui.internal.presentations.util.AbstractTabItem#setBold(boolean)
146: */
147: public void setBold(boolean bold) {
148: this .bold = bold;
149: super .setBold(bold);
150: updateFont();
151: }
152:
153: /*
154: * (non-Javadoc)
155: *
156: * @see org.eclipse.ui.internal.presentations.util.AbstractTabItem#setBusy(boolean)
157: */
158: public void setBusy(boolean busy) {
159: this .busy = busy;
160: super .setBusy(busy);
161: updateFont();
162: }
163:
164: private void updateFont() {
165: CTabItem tabItem = getItem();
166:
167: // Set the font if necessary
168: FontRegistry registry = PlatformUI.getWorkbench()
169: .getThemeManager().getCurrentTheme().getFontRegistry();
170:
171: // Determine the parent font. We will set the tab's font
172: Font targetFont = null;
173:
174: if (busy) {
175: targetFont = registry
176: .getItalic(IWorkbenchThemeConstants.TAB_TEXT_FONT);
177: } else {
178:
179: if (bold) {
180: targetFont = registry
181: .getBold(IWorkbenchThemeConstants.TAB_TEXT_FONT);
182: }
183: }
184:
185: if (lastFont != targetFont) {
186: tabItem.setFont(targetFont);
187: lastFont = targetFont;
188: }
189: }
190:
191: private static String computeShortName(PartInfo info) {
192: String text = info.name;
193:
194: if (info.dirty) {
195: text = DIRTY_PREFIX + text;
196: }
197:
198: return text;
199: }
200:
201: private static String computeLongName(PartInfo info) {
202: String text = info.name;
203:
204: String contentDescription = info.contentDescription;
205:
206: if (contentDescription.equals("")) { //$NON-NLS-1$
207:
208: String titleTooltip = info.toolTip.trim();
209:
210: if (titleTooltip.endsWith(info.name)) {
211: titleTooltip = titleTooltip.substring(0,
212: titleTooltip.lastIndexOf(info.name)).trim();
213: }
214:
215: if (titleTooltip.endsWith("\\")) { //$NON-NLS-1$
216: titleTooltip = titleTooltip.substring(0,
217: titleTooltip.lastIndexOf("\\")).trim(); //$NON-NLS-1$
218: }
219:
220: if (titleTooltip.endsWith("/")) { //$NON-NLS-1$
221: titleTooltip = titleTooltip.substring(0,
222: titleTooltip.lastIndexOf("/")).trim(); //$NON-NLS-1$
223: }
224:
225: contentDescription = titleTooltip;
226: }
227:
228: if (!contentDescription.equals("")) { //$NON-NLS-1$
229: text = MessageFormat.format(
230: WorkbenchMessages.EditorPart_AutoTitleFormat,
231: new String[] { text, contentDescription });
232: }
233:
234: if (info.dirty) {
235: text = DIRTY_PREFIX + text;
236: }
237:
238: return text;
239: }
240: }
|