1 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 31 public class DefaultTabItem extends WidgetTabItem { 32 33 public static String DIRTY_PREFIX = "*"; 35 private boolean busy = false; 36 37 private boolean bold = false; 38 39 private Font lastFont = null; 40 41 private String shortName = Util.ZERO_LENGTH_STRING; 42 43 private String 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 55 private static CTabItem getTab(CTabFolder parent, int index, int flags) { 56 return new CTabItem(parent, flags, index); 57 } 58 59 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 77 public boolean isShowing() { 78 return getItem().isShowing(); 79 } 80 81 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 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 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 128 public static String escapeAmpersands(String input) { 129 StringBuffer title = new StringBuffer (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); } 136 } 137 return title.toString(); 138 } 139 140 145 public void setBold(boolean bold) { 146 this.bold = bold; 147 super.setBold(bold); 148 updateFont(); 149 } 150 151 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 FontRegistry registry = PlatformUI.getWorkbench().getThemeManager() 167 .getCurrentTheme().getFontRegistry(); 168 169 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 computeShortName(PartInfo info) { 190 String text = info.name; 191 192 if (info.dirty) { 193 text = DIRTY_PREFIX + text; 194 } 195 196 return text; 197 } 198 199 private static String computeLongName(PartInfo info) { 200 String text = info.name; 201 202 String contentDescription = info.contentDescription; 203 204 if (contentDescription.equals("")) { 206 String 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("\\")) { titleTooltip = titleTooltip.substring(0, 215 titleTooltip.lastIndexOf("\\")).trim(); } 217 218 if (titleTooltip.endsWith("/")) { titleTooltip = titleTooltip.substring(0, 220 titleTooltip.lastIndexOf("/")).trim(); } 222 223 contentDescription = titleTooltip; 224 } 225 226 if (!contentDescription.equals("")) { text = MessageFormat.format( 228 WorkbenchMessages.EditorPart_AutoTitleFormat, new String [] { 229 text, contentDescription }); 230 } 231 232 if (info.dirty) { 233 text = DIRTY_PREFIX + text; 234 } 235 236 return text; 237 } 238 } 239 | Popular Tags |