KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 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
12 package org.eclipse.ui.internal.presentations;
13
14 import java.text.Collator JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Iterator JavaDoc;
17
18 import org.eclipse.jface.viewers.ContentViewer;
19 import org.eclipse.jface.viewers.IBaseLabelProvider;
20 import org.eclipse.jface.viewers.IFontProvider;
21 import org.eclipse.jface.viewers.ILabelProvider;
22 import org.eclipse.jface.viewers.IStructuredContentProvider;
23 import org.eclipse.jface.viewers.IStructuredSelection;
24 import org.eclipse.jface.viewers.LabelProvider;
25 import org.eclipse.jface.viewers.TableViewer;
26 import org.eclipse.jface.viewers.Viewer;
27 import org.eclipse.jface.viewers.ViewerSorter;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.custom.CTabFolder;
30 import org.eclipse.swt.custom.CTabItem;
31 import org.eclipse.swt.graphics.Font;
32 import org.eclipse.swt.graphics.FontData;
33 import org.eclipse.swt.graphics.Image;
34 import org.eclipse.swt.layout.GridData;
35 import org.eclipse.swt.widgets.Composite;
36 import org.eclipse.swt.widgets.Shell;
37 import org.eclipse.swt.widgets.Table;
38 import org.eclipse.ui.presentations.IPresentablePart;
39
40 public class BasicStackList extends AbstractTableInformationControl {
41
42     private class BasicStackListContentProvider implements IStructuredContentProvider {
43
44         private DefaultPartPresentation basicStackPresentation;
45
46         public BasicStackListContentProvider() {
47             //no-op
48
}
49
50         public void dispose() {
51             //no-op
52
}
53
54         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
55             if (basicStackPresentation == null) { return new CTabItem[0]; }
56             final PaneFolder tabFolder = basicStackPresentation.getTabFolder();
57
58             /* TODO
59             ArrayList items = new ArrayList(Arrays.asList(tabFolder.getItems()));
60
61             for (Iterator iterator = items.iterator(); iterator.hasNext();) {
62                 CTabItem tabItem = (CTabItem) iterator.next();
63
64                 if (tabItem.isShowing()) iterator.remove();
65             }
66             
67             return items.toArray();
68             */

69             
70             return tabFolder.getItems();
71         }
72
73         public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
74             basicStackPresentation = (DefaultPartPresentation) newInput;
75         }
76     }
77     
78     private class BasicStackListLabelProvider extends LabelProvider implements IFontProvider {
79
80         private Font boldFont = null;
81
82         public BasicStackListLabelProvider() {
83             //no-op
84
}
85
86         public String JavaDoc getText(Object JavaDoc element) {
87             CTabItem tabItem = (CTabItem) element;
88             DefaultPartPresentation basicStackPresentation = (DefaultPartPresentation) getTableViewer()
89             .getInput();
90             IPresentablePart presentablePart = basicStackPresentation.getPartForTab(tabItem);
91             // TODO: the 'true' parameter should be removed
92
return basicStackPresentation.getLabelText(presentablePart, true);
93         }
94
95         public Image getImage(Object JavaDoc element) {
96             CTabItem tabItem = (CTabItem) element;
97             DefaultPartPresentation basicStackPresentation = (DefaultPartPresentation) getTableViewer()
98             .getInput();
99             IPresentablePart presentablePart = basicStackPresentation.getPartForTab(tabItem);
100             return basicStackPresentation.getLabelImage(presentablePart);
101         }
102         
103         public Font getFont(Object JavaDoc element) {
104             CTabItem tabItem = (CTabItem) element;
105             // check to determine if the user selected the single tab mode
106
CTabFolder tabFolder = tabItem.getParent();
107             boolean singleTab = tabFolder.getSingle();
108             
109             // if in single tab mode, do not use the bold font for non-visible tabs
110
// if in multiple tab mode, use the bold for non-visible tabs only
111
if (tabItem.isShowing() || singleTab)
112                 return null;
113             
114             if (boldFont == null) {
115                 Font originalFont = tabItem.getFont();
116                 FontData fontData[] = originalFont.getFontData();
117                 // Adding the bold attribute
118
for (int i = 0; i < fontData.length; i++)
119                     fontData[i].setStyle(fontData[i].getStyle()|SWT.BOLD);
120                 boldFont = new Font(tabItem.getDisplay(), fontData);
121             }
122             return boldFont;
123         }
124         
125         public void dispose() {
126             super.dispose();
127             if (boldFont != null)
128                 boldFont.dispose();
129         }
130     }
131     
132     private class BasicStackListViewerSorter extends ViewerSorter {
133         
134         public BasicStackListViewerSorter(){
135             //no-op
136
}
137         
138         public BasicStackListViewerSorter(Collator JavaDoc collator) {
139             super(collator);
140         }
141         
142         /* (non-Javadoc)
143          * @see org.eclipse.jface.viewers.ViewerSorter#compare(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
144          */

145         public int compare(Viewer viewer, Object JavaDoc e1, Object JavaDoc e2) {
146             int cat1 = category(e1);
147             int cat2 = category(e2);
148
149             if (cat1 != cat2)
150                 return cat1 - cat2;
151
152             // cat1 == cat2
153

154             String JavaDoc name1;
155             String JavaDoc name2;
156
157             if (viewer == null || !(viewer instanceof ContentViewer)) {
158                 name1 = e1.toString();
159                 name2 = e2.toString();
160             } else {
161                 IBaseLabelProvider prov = ((ContentViewer) viewer).getLabelProvider();
162                 if (prov instanceof ILabelProvider) {
163                     ILabelProvider lprov = (ILabelProvider) prov;
164                     name1 = lprov.getText(e1);
165                     name2 = lprov.getText(e2);
166                     // ILabelProvider's implementation in BasicStackList calls
167
// DefaultEditorPresentation.getLabelText which returns the name of dirty
168
// files begining with dirty prefix, sorting should not take dirty prefix in consideration
169
String JavaDoc prefix = DefaultEditorPresentation.DIRTY_PREFIX;
170                     if (name1.startsWith(prefix))
171                         name1 = name1.substring(prefix.length());
172                     if (name2.startsWith(prefix))
173                         name2 = name2.substring(prefix.length());
174                 } else {
175                     name1 = e1.toString();
176                     name2 = e2.toString();
177                 }
178             }
179             if (name1 == null)
180                 name1 = "";//$NON-NLS-1$
181
if (name2 == null)
182                 name2 = "";//$NON-NLS-1$
183
return collator.compare(name1, name2);
184         }
185         /* (non-Javadoc)
186          * @see org.eclipse.jface.viewers.ViewerSorter#category(java.lang.Object)
187          */

188         public int category(Object JavaDoc element) {
189             
190             CTabItem tabItem = (CTabItem) element;
191             if (tabItem.isShowing())
192                 return 1; // visible
193
return 0; // not visible
194
}
195     }
196     
197     public BasicStackList(Shell parent, int shellStyle, int treeStyle) {
198         super(parent, shellStyle, treeStyle);
199     }
200
201     protected TableViewer createTableViewer(Composite parent, int style) {
202         Table table = new Table(parent, SWT.SINGLE | (style & ~SWT.MULTI));
203         table.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
204         TableViewer tableViewer = new TableViewer(table) {
205             /* (non-Javadoc)
206              * @see org.eclipse.jface.viewers.TableViewer#internalRefresh(java.lang.Object)
207              */

208             protected void internalRefresh(Object JavaDoc element) {
209                 boolean usingMotif = "motif".equals(SWT.getPlatform());
210                 try {
211                     // This avoids a "graphic is disposed" error on Motif by not letting
212
// it redraw while we remove entries. Some items in this table are
213
// being removed and may have icons which may have already been
214
// disposed elsewhere.
215
if (usingMotif) getTable().setRedraw(false);
216                     super.internalRefresh(element);
217                 } finally {
218                     if (usingMotif) getTable().setRedraw(true);
219                 }
220             }
221         };
222         tableViewer.addFilter(new NamePatternFilter());
223         tableViewer.setContentProvider(new BasicStackListContentProvider());
224         tableViewer.setSorter(new BasicStackListViewerSorter());
225         tableViewer.setLabelProvider(new BasicStackListLabelProvider());
226         return tableViewer;
227     }
228
229     public void setInput(Object JavaDoc information) {
230         DefaultPartPresentation basicStackPresentation = (DefaultPartPresentation) information;
231         inputChanged(basicStackPresentation, basicStackPresentation.getTabFolder()
232                 .getSelection());
233     }
234
235     protected void gotoSelectedElement() {
236         Object JavaDoc selectedElement = getSelectedElement();
237
238         DefaultPartPresentation basicStackPresentation = null;
239         if (selectedElement != null) {
240             basicStackPresentation = (DefaultPartPresentation) getTableViewer()
241                     .getInput();
242         }
243         //close the shell
244
dispose();
245
246         //open the part, if one was selected
247
if (basicStackPresentation != null)
248             basicStackPresentation.setSelection((CTabItem) selectedElement);
249     }
250     
251     protected boolean deleteSelectedElements() {
252         
253         IStructuredSelection structuredSelection = getSelectedElements();
254         
255         if (structuredSelection != null) {
256             DefaultPartPresentation basicStackPresentation = (DefaultPartPresentation) getTableViewer()
257             .getInput();
258             
259             ArrayList JavaDoc list = new ArrayList JavaDoc(structuredSelection.size());
260             
261             for (Iterator JavaDoc iterator = structuredSelection.iterator(); iterator.hasNext();) {
262                 IPresentablePart presentablePart = basicStackPresentation.getPartForTab((CTabItem) iterator.next());
263                 list.add(presentablePart);
264             }
265             
266             basicStackPresentation.close((IPresentablePart[]) list.toArray(new IPresentablePart[list.size()]));
267         }
268         
269         if (((DefaultPartPresentation)getTableViewer().getInput()).isDisposed()) {
270             fComposite.dispose();
271             return true;
272         }
273         return false;
274         
275     }
276 }
277
Popular Tags