KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > presentations > DefaultEditorPresentation


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

11 package org.eclipse.ui.internal.presentations;
12
13 import java.text.MessageFormat JavaDoc;
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 /**
41  * Controls the appearance of views stacked into the workbench.
42  *
43  * @since 3.0
44  */

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 JavaDoc DIRTY_PREFIX = "*"; //$NON-NLS-1$
52

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     /**
103      * For editors we'll have to replace the contents instead of simply adding
104      * them to the presentation.
105      *
106      * @see org.eclipse.ui.presentations.StackPresentation#restoreState(org.eclipse.ui.presentations.IPresentationSerializer, org.eclipse.ui.IMemento)
107      */

108     public void restoreState(IPresentationSerializer serializer,
109             IMemento savedState) {
110         IMemento[] parts = savedState.getChildren(IWorkbenchConstants.TAG_PART);
111
112         IPresentablePart currentPart = getCurrentPart();
113
114         // the insertion index for the current part
115
int insert = 0;
116         for (int idx = 0; idx < parts.length; idx++) {
117             String JavaDoc id = parts[idx].getString(IWorkbenchConstants.TAG_ID);
118
119             // if the part is not around, then it doesn't get added
120
IPresentablePart part = id == null ? null : serializer.getPart(id);
121             if (part == null)
122                 continue;
123
124             int partIndex = indexOf(part);
125
126             // otherwise if the part is in the right place then do nothing
127
if (partIndex == insert) {
128                 ++insert;
129                 continue;
130             }
131             
132             // otherwise remove the part and add it in the right place
133
removePart(part);
134             addPart(part, partIndex < insert ? insert - 1 : insert++);
135
136             // reselect the part if it was previously the current
137
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         // do not support close box on unselected tabs.
156
tabFolder.setUnselectedCloseVisible(true);
157         // do not support icons in unselected tabs.
158
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     /* (non-Javadoc)
167      * @see org.eclipse.ui.internal.presentations.DefaultPartPresentation#widgetDisposed()
168      */

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 JavaDoc toolTipText = part.getTitleToolTip();
179         if (!toolTipText.equals(Util.ZERO_LENGTH_STRING)) {
180             tabItem.setToolTipText(toolTipText);
181         }
182     }
183
184     /* (non-Javadoc)
185      * @see org.eclipse.ui.presentations.StackPresentation#setActive(int)
186      */

187     public void setActive(int newState) {
188         super.setActive(newState);
189        
190         updateGradient();
191
192     }
193     /**
194      * Set the tab folder tab style to a tradional style tab
195      *
196      * @param traditionalTab
197      * <code>true</code> if traditional style tabs should be used
198      * <code>false</code> otherwise.
199      */

200     protected void setTabStyle(boolean traditionalTab) {
201         // set the tab style to non-simple
202
getTabFolder().setSimpleTab(traditionalTab);
203     }
204     
205     /*
206      * (non-Javadoc)
207      *
208      * @see org.eclipse.ui.internal.presentations.DefaultPartPresentation#getCurrentTitle()
209      */

210     protected String JavaDoc getCurrentTitle() {
211         return ""; //$NON-NLS-1$
212
}
213     
214     /* (non-Javadoc)
215      * @see org.eclipse.ui.internal.presentations.DefaultPartPresentation#updateGradient()
216      */

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             //only layout on font changes.
248
resizeNeeded = true;
249         }
250         
251         //call super to ensure that the toolbar is updated properly.
252
super.updateGradient();
253         
254         if (resizeNeeded) {
255             getTabFolder().setTabHeight(computeTabHeight());
256             //ensure proper control sizes for new fonts
257
setControlSize();
258         }
259     }
260     /* (non-Javadoc)
261      * @see org.eclipse.ui.internal.presentations.DefaultPartPresentation#getPaneName()
262      */

263     protected String JavaDoc getPaneName() {
264         return WorkbenchMessages.getString("EditorPane.moveEditor"); //$NON-NLS-1$
265
}
266     /* (non-Javadoc)
267      * @see org.eclipse.ui.internal.presentations.DefaultPartPresentation#getLabelText(org.eclipse.ui.presentations.IPresentablePart, boolean)
268      */

269     String JavaDoc getLabelText(IPresentablePart presentablePart, boolean includePath) {
270         String JavaDoc title = super.getLabelText(presentablePart, includePath);
271         String JavaDoc text = title;
272
273         if (includePath) {
274             String JavaDoc contentDescription = presentablePart.getTitleStatus();
275             
276             if (contentDescription.equals("")) { //$NON-NLS-1$
277

278                 String JavaDoc 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("\\")) //$NON-NLS-1$
285
titleTooltip = titleTooltip.substring(0,
286                                 titleTooltip.lastIndexOf("\\")).trim(); //$NON-NLS-1$
287

288                 if (titleTooltip.endsWith("/")) //$NON-NLS-1$
289
titleTooltip = titleTooltip.substring(0,
290                                 titleTooltip.lastIndexOf("/")).trim(); //$NON-NLS-1$
291

292                 contentDescription = titleTooltip;
293             }
294             
295             if (!contentDescription.equals("")) { //$NON-NLS-1$
296
text = MessageFormat.format(WorkbenchMessages.getString("EditorPart.AutoTitleFormat"), new String JavaDoc[] {text, contentDescription}); //$NON-NLS-1$
297
}
298         }
299
300         if (presentablePart.isDirty()) {
301                 text = DIRTY_PREFIX + text; //$NON-NLS-1$
302
}
303
304         return text;
305     }
306 }
Popular Tags