KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > presentations > defaultpresentation > DefaultTabItem


1 /*******************************************************************************
2  * Copyright (c) 2004, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.presentations.defaultpresentation;
12
13 import org.eclipse.jface.resource.FontRegistry;
14 import org.eclipse.jface.util.Geometry;
15 import org.eclipse.swt.custom.CTabFolder;
16 import org.eclipse.swt.custom.CTabItem;
17 import org.eclipse.swt.graphics.Font;
18 import org.eclipse.swt.graphics.Rectangle;
19 import org.eclipse.ui.PlatformUI;
20 import org.eclipse.ui.internal.IWorkbenchThemeConstants;
21 import org.eclipse.ui.internal.WorkbenchMessages;
22 import org.eclipse.ui.internal.presentations.util.PartInfo;
23 import org.eclipse.ui.internal.presentations.util.WidgetTabItem;
24 import org.eclipse.ui.internal.util.Util;
25
26 import com.ibm.icu.text.MessageFormat;
27
28 /**
29  * @since 3.1
30  */

31 public class DefaultTabItem extends WidgetTabItem {
32
33     public static String JavaDoc DIRTY_PREFIX = "*"; //$NON-NLS-1$
34

35     private boolean busy = false;
36
37     private boolean bold = false;
38
39     private Font lastFont = null;
40
41     private String JavaDoc shortName = Util.ZERO_LENGTH_STRING;
42
43     private String JavaDoc longName = Util.ZERO_LENGTH_STRING;
44
45     public DefaultTabItem(CTabFolder parent, int index, int flags) {
46         super(getTab(parent, index, flags));
47         updateFont();
48     }
49
50     /**
51      * Get a new tab for the receiver.
52      *
53      * @return CTabItem
54      */

55     private static CTabItem getTab(CTabFolder parent, int index, int flags) {
56         return new CTabItem(parent, flags, index);
57     }
58
59     /*
60      * (non-Javadoc)
61      *
62      * @see org.eclipse.ui.internal.presentations.util.AbstractTabItem#getBounds()
63      */

64     public Rectangle getBounds() {
65         return Geometry.toDisplay(getItem().getParent(), getItem().getBounds());
66     }
67
68     public CTabItem getItem() {
69         return (CTabItem) getWidget();
70     }
71
72     /*
73      * (non-Javadoc)
74      *
75      * @see org.eclipse.ui.internal.presentations.util.AbstractTabItem#isShowing()
76      */

77     public boolean isShowing() {
78         return getItem().isShowing();
79     }
80
81     /*
82      * (non-Javadoc)
83      *
84      * @see org.eclipse.ui.internal.presentations.util.AbstractTabItem#setInfo(org.eclipse.ui.internal.presentations.util.PartInfo)
85      */

86     public void setInfo(PartInfo info) {
87         CTabItem tabItem = getItem();
88
89         shortName = computeShortName(info);
90         longName = computeLongName(info);
91
92         updateTabText();
93
94         if (tabItem.getImage() != info.image) {
95             tabItem.setImage(info.image);
96         }
97
98         String JavaDoc toolTipText = info.toolTip;
99         if (toolTipText.equals(Util.ZERO_LENGTH_STRING)) {
100             toolTipText = null;
101         }
102
103         if (!Util.equals(toolTipText, tabItem.getToolTipText())) {
104             tabItem.setToolTipText(toolTipText);
105         }
106     }
107
108     public void updateTabText() {
109         CTabItem tabItem = getItem();
110
111         String JavaDoc newName = tabItem.getParent().getSingle() ? longName : shortName;
112
113         newName = escapeAmpersands(newName);
114
115         if (!Util.equals(newName, tabItem.getText())) {
116             tabItem.setText(newName);
117         }
118     }
119
120     /**
121      * Escapes all the ampersands in the given string such that they can be
122      * displayed verbatim in an SWT label rather than treated as accelerators.
123      *
124      * @since 3.1
125      *
126      * @return a string where all ampersands are escaped
127      */

128     public static String JavaDoc escapeAmpersands(String JavaDoc input) {
129         StringBuffer JavaDoc title = new StringBuffer JavaDoc(input.length());
130         for (int i = 0; i < input.length(); i++) {
131             char character = input.charAt(i);
132             title.append(character);
133             if (character == '&') {
134                 title.append(character); // escape ampersand
135
}
136         }
137         return title.toString();
138     }
139
140     /*
141      * (non-Javadoc)
142      *
143      * @see org.eclipse.ui.internal.presentations.util.AbstractTabItem#setBold(boolean)
144      */

145     public void setBold(boolean bold) {
146         this.bold = bold;
147         super.setBold(bold);
148         updateFont();
149     }
150
151     /*
152      * (non-Javadoc)
153      *
154      * @see org.eclipse.ui.internal.presentations.util.AbstractTabItem#setBusy(boolean)
155      */

156     public void setBusy(boolean busy) {
157         this.busy = busy;
158         super.setBusy(busy);
159         updateFont();
160     }
161
162     private void updateFont() {
163         CTabItem tabItem = getItem();
164
165         // Set the font if necessary
166
FontRegistry registry = PlatformUI.getWorkbench().getThemeManager()
167                 .getCurrentTheme().getFontRegistry();
168
169         // Determine the parent font. We will set the tab's font
170
Font targetFont = null;
171
172         if (busy) {
173             targetFont = registry
174                     .getItalic(IWorkbenchThemeConstants.TAB_TEXT_FONT);
175         } else {
176
177             if (bold) {
178                 targetFont = registry
179                         .getBold(IWorkbenchThemeConstants.TAB_TEXT_FONT);
180             }
181         }
182
183         if (lastFont != targetFont) {
184             tabItem.setFont(targetFont);
185             lastFont = targetFont;
186         }
187     }
188
189     private static String JavaDoc computeShortName(PartInfo info) {
190         String JavaDoc text = info.name;
191
192         if (info.dirty) {
193             text = DIRTY_PREFIX + text;
194         }
195
196         return text;
197     }
198
199     private static String JavaDoc computeLongName(PartInfo info) {
200         String JavaDoc text = info.name;
201
202         String JavaDoc contentDescription = info.contentDescription;
203
204         if (contentDescription.equals("")) { //$NON-NLS-1$
205

206             String JavaDoc titleTooltip = info.toolTip.trim();
207
208             if (titleTooltip.endsWith(info.name)) {
209                 titleTooltip = titleTooltip.substring(0,
210                         titleTooltip.lastIndexOf(info.name)).trim();
211             }
212
213             if (titleTooltip.endsWith("\\")) { //$NON-NLS-1$
214
titleTooltip = titleTooltip.substring(0,
215                         titleTooltip.lastIndexOf("\\")).trim(); //$NON-NLS-1$
216
}
217
218             if (titleTooltip.endsWith("/")) { //$NON-NLS-1$
219
titleTooltip = titleTooltip.substring(0,
220                         titleTooltip.lastIndexOf("/")).trim(); //$NON-NLS-1$
221
}
222
223             contentDescription = titleTooltip;
224         }
225
226         if (!contentDescription.equals("")) { //$NON-NLS-1$
227
text = MessageFormat.format(
228                     WorkbenchMessages.EditorPart_AutoTitleFormat, new String JavaDoc[] {
229                             text, contentDescription });
230         }
231
232         if (info.dirty) {
233             text = DIRTY_PREFIX + text;
234         }
235
236         return text;
237     }
238 }
239
Popular Tags