1 11 package org.eclipse.ui.internal.presentations; 12 13 import java.text.MessageFormat ; 14 15 import org.eclipse.jface.preference.IPreferenceStore; 16 import org.eclipse.jface.resource.ColorRegistry; 17 import org.eclipse.jface.util.IPropertyChangeListener; 18 import org.eclipse.jface.util.PropertyChangeEvent; 19 import org.eclipse.swt.SWT; 20 import org.eclipse.swt.custom.CTabItem; 21 import org.eclipse.swt.graphics.Color; 22 import org.eclipse.swt.graphics.Font; 23 import org.eclipse.swt.widgets.Composite; 24 import org.eclipse.ui.IMemento; 25 import org.eclipse.ui.IWorkbenchPreferenceConstants; 26 import org.eclipse.ui.PlatformUI; 27 import org.eclipse.ui.internal.IPreferenceConstants; 28 import org.eclipse.ui.internal.IWorkbenchConstants; 29 import org.eclipse.ui.internal.IWorkbenchThemeConstants; 30 import org.eclipse.ui.internal.WorkbenchMessages; 31 import org.eclipse.ui.internal.WorkbenchPlugin; 32 import org.eclipse.ui.internal.util.PrefUtil; 33 import org.eclipse.ui.internal.util.Util; 34 import org.eclipse.ui.presentations.IPresentablePart; 35 import org.eclipse.ui.presentations.IPresentationSerializer; 36 import org.eclipse.ui.presentations.IStackPresentationSite; 37 import org.eclipse.ui.presentations.StackPresentation; 38 import org.eclipse.ui.themes.ITheme; 39 40 45 public class DefaultEditorPresentation extends DefaultPartPresentation { 46 47 private IPreferenceStore preferenceStore = WorkbenchPlugin.getDefault() 48 .getPreferenceStore(); 49 private IPreferenceStore apiPreferenceStore = PrefUtil.getAPIPreferenceStore(); 50 51 public static String DIRTY_PREFIX = "*"; 53 private final IPropertyChangeListener propertyChangeListener = new IPropertyChangeListener() { 54 55 public void propertyChange(PropertyChangeEvent propertyChangeEvent) { 56 57 if(isDisposed()) 58 return; 59 60 if (IPreferenceConstants.EDITOR_TAB_POSITION 61 .equals(propertyChangeEvent.getProperty()) 62 && !isDisposed()) { 63 int tabLocation = preferenceStore 64 .getInt(IPreferenceConstants.EDITOR_TAB_POSITION); 65 getTabFolder().setTabPosition(tabLocation); 66 layout(false); 67 } else if (IWorkbenchPreferenceConstants.SHOW_TRADITIONAL_STYLE_TABS 68 .equals(propertyChangeEvent.getProperty()) 69 && !isDisposed()) { 70 boolean traditionalTab = apiPreferenceStore 71 .getBoolean(IWorkbenchPreferenceConstants.SHOW_TRADITIONAL_STYLE_TABS); 72 setTabStyle(traditionalTab); 73 } 74 75 boolean multiChanged = IPreferenceConstants.SHOW_MULTIPLE_EDITOR_TABS 76 .equals(propertyChangeEvent.getProperty()); 77 boolean styleChanged = IWorkbenchPreferenceConstants.SHOW_TRADITIONAL_STYLE_TABS 78 .equals(propertyChangeEvent.getProperty()); 79 PaneFolder tabFolder = getTabFolder(); 80 81 if ((multiChanged || styleChanged) && tabFolder != null) { 82 if (multiChanged) { 83 boolean multi = preferenceStore 84 .getBoolean(IPreferenceConstants.SHOW_MULTIPLE_EDITOR_TABS); 85 tabFolder.setSingleTab(!multi); 86 } else { 87 boolean simple = apiPreferenceStore 88 .getBoolean(IWorkbenchPreferenceConstants.SHOW_TRADITIONAL_STYLE_TABS); 89 tabFolder.setSimpleTab(simple); 90 } 91 92 CTabItem[] tabItems = tabFolder.getItems(); 93 94 for (int i = 0; i < tabItems.length; i++) { 95 CTabItem tabItem = tabItems[i]; 96 initTab(tabItem, getPartForTab(tabItem)); 97 } 98 } 99 } 100 }; 101 102 108 public void restoreState(IPresentationSerializer serializer, 109 IMemento savedState) { 110 IMemento[] parts = savedState.getChildren(IWorkbenchConstants.TAG_PART); 111 112 IPresentablePart currentPart = getCurrentPart(); 113 114 int insert = 0; 116 for (int idx = 0; idx < parts.length; idx++) { 117 String id = parts[idx].getString(IWorkbenchConstants.TAG_ID); 118 119 IPresentablePart part = id == null ? null : serializer.getPart(id); 121 if (part == null) 122 continue; 123 124 int partIndex = indexOf(part); 125 126 if (partIndex == insert) { 128 ++insert; 129 continue; 130 } 131 132 removePart(part); 134 addPart(part, partIndex < insert ? insert - 1 : insert++); 135 136 if (part == currentPart) 138 selectPart(part); 139 } 140 } 141 142 public DefaultEditorPresentation(Composite parent, IStackPresentationSite newSite) { 143 super(new PaneFolder(parent, SWT.BORDER), newSite); 144 final PaneFolder tabFolder = getTabFolder(); 145 146 preferenceStore.addPropertyChangeListener(propertyChangeListener); 147 apiPreferenceStore.addPropertyChangeListener(propertyChangeListener); 148 int tabLocation = preferenceStore 149 .getInt(IPreferenceConstants.EDITOR_TAB_POSITION); 150 tabFolder.setTabPosition(tabLocation); 151 tabFolder.setSingleTab(!preferenceStore 152 .getBoolean(IPreferenceConstants.SHOW_MULTIPLE_EDITOR_TABS)); 153 setTabStyle(apiPreferenceStore 154 .getBoolean(IWorkbenchPreferenceConstants.SHOW_TRADITIONAL_STYLE_TABS)); 155 tabFolder.setUnselectedCloseVisible(true); 157 tabFolder.setUnselectedImageVisible(true); 159 160 getSystemMenuManager().add(new UpdatingActionContributionItem(new SystemMenuCloseOthers(this))); 161 getSystemMenuManager().add(new UpdatingActionContributionItem(new SystemMenuCloseAll(this))); 162 163 init(); 164 } 165 166 169 protected void widgetDisposed() { 170 preferenceStore.removePropertyChangeListener(propertyChangeListener); 171 apiPreferenceStore.removePropertyChangeListener(propertyChangeListener); 172 super.widgetDisposed(); 173 } 174 175 protected void initTab(CTabItem tabItem, IPresentablePart part) { 176 tabItem.setText(getLabelText(part, (getTabFolder().getControl().getStyle() & SWT.MULTI) == 0)); 177 tabItem.setImage(getLabelImage(part)); 178 String toolTipText = part.getTitleToolTip(); 179 if (!toolTipText.equals(Util.ZERO_LENGTH_STRING)) { 180 tabItem.setToolTipText(toolTipText); 181 } 182 } 183 184 187 public void setActive(int newState) { 188 super.setActive(newState); 189 190 updateGradient(); 191 192 } 193 200 protected void setTabStyle(boolean traditionalTab) { 201 getTabFolder().setSimpleTab(traditionalTab); 203 } 204 205 210 protected String getCurrentTitle() { 211 return ""; } 213 214 217 protected void updateGradient() { 218 if (isDisposed()) 219 return; 220 221 ITheme theme = PlatformUI.getWorkbench().getThemeManager().getCurrentTheme(); 222 ColorRegistry colorRegistry = theme.getColorRegistry(); 223 224 225 if (getActive() == StackPresentation.AS_ACTIVE_FOCUS) { 226 setActiveTabColors(); 227 } 228 else if (getActive() == StackPresentation.AS_ACTIVE_NOFOCUS) { 229 drawGradient( 230 colorRegistry.get(IWorkbenchThemeConstants.INACTIVE_TAB_TEXT_COLOR), 231 new Color [] { 232 colorRegistry.get(IWorkbenchThemeConstants.INACTIVE_TAB_BG_START) 233 }, 234 new int [0], 235 true); 236 } 237 else { 238 setInactiveTabColors(); 239 } 240 241 boolean resizeNeeded = false; 242 Font tabFont = theme.getFontRegistry().get(IWorkbenchThemeConstants.TAB_TEXT_FONT); 243 Font oldTabFont = getTabFolder().getControl().getFont(); 244 if (!oldTabFont.equals(tabFont)) { 245 getTabFolder().getControl().setFont(tabFont); 246 247 resizeNeeded = true; 249 } 250 251 super.updateGradient(); 253 254 if (resizeNeeded) { 255 getTabFolder().setTabHeight(computeTabHeight()); 256 setControlSize(); 258 } 259 } 260 263 protected String getPaneName() { 264 return WorkbenchMessages.getString("EditorPane.moveEditor"); } 266 269 String getLabelText(IPresentablePart presentablePart, boolean includePath) { 270 String title = super.getLabelText(presentablePart, includePath); 271 String text = title; 272 273 if (includePath) { 274 String contentDescription = presentablePart.getTitleStatus(); 275 276 if (contentDescription.equals("")) { 278 String titleTooltip = presentablePart.getTitleToolTip().trim(); 279 280 if (titleTooltip.endsWith(title)) 281 titleTooltip = titleTooltip.substring(0, 282 titleTooltip.lastIndexOf(title)).trim(); 283 284 if (titleTooltip.endsWith("\\")) titleTooltip = titleTooltip.substring(0, 286 titleTooltip.lastIndexOf("\\")).trim(); 288 if (titleTooltip.endsWith("/")) titleTooltip = titleTooltip.substring(0, 290 titleTooltip.lastIndexOf("/")).trim(); 292 contentDescription = titleTooltip; 293 } 294 295 if (!contentDescription.equals("")) { text = MessageFormat.format(WorkbenchMessages.getString("EditorPart.AutoTitleFormat"), new String [] {text, contentDescription}); } 298 } 299 300 if (presentablePart.isDirty()) { 301 text = DIRTY_PREFIX + text; } 303 304 return text; 305 } 306 } | Popular Tags |