KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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
12 package org.eclipse.ui.internal.presentations;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16
17 import org.eclipse.jface.viewers.ContentViewer;
18 import org.eclipse.jface.viewers.IBaseLabelProvider;
19 import org.eclipse.jface.viewers.IFontProvider;
20 import org.eclipse.jface.viewers.ILabelProvider;
21 import org.eclipse.jface.viewers.IStructuredContentProvider;
22 import org.eclipse.jface.viewers.IStructuredSelection;
23 import org.eclipse.jface.viewers.LabelProvider;
24 import org.eclipse.jface.viewers.TableViewer;
25 import org.eclipse.jface.viewers.Viewer;
26 import org.eclipse.jface.viewers.ViewerComparator;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.graphics.Font;
29 import org.eclipse.swt.graphics.FontData;
30 import org.eclipse.swt.graphics.Image;
31 import org.eclipse.swt.layout.GridData;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.Control;
34 import org.eclipse.swt.widgets.Shell;
35 import org.eclipse.swt.widgets.Table;
36 import org.eclipse.ui.internal.presentations.defaultpresentation.DefaultTabItem;
37 import org.eclipse.ui.internal.presentations.util.AbstractTabItem;
38 import org.eclipse.ui.internal.presentations.util.PresentablePartFolder;
39 import org.eclipse.ui.presentations.IPresentablePart;
40 import org.eclipse.ui.presentations.IStackPresentationSite;
41
42 public class BasicPartList extends AbstractTableInformationControl {
43
44     private boolean hiddenTabsBold = true;
45     private PresentablePartFolder folder;
46     private IStackPresentationSite site;
47     
48     private class BasicStackListContentProvider implements
49             IStructuredContentProvider {
50         
51         /**
52          * Constructor for stack list content provider.
53          *
54          */

55         public BasicStackListContentProvider() {
56             //no-op
57
}
58
59         public void dispose() {
60             //no-op
61
}
62
63         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
64             if (folder == null) {
65                 return new IPresentablePart[0];
66             }
67             
68             return folder.getPartList();
69         }
70
71         public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
72             folder = (PresentablePartFolder) newInput;
73         }
74     }
75
76     private class BasicStackListLabelProvider extends LabelProvider implements
77             IFontProvider {
78
79         private Font boldFont = null;
80
81         /**
82          * Constructor for stack list label provider.
83          *
84          */

85         public BasicStackListLabelProvider() {
86             //no-op
87
}
88
89         public String JavaDoc getText(Object JavaDoc element) {
90             IPresentablePart presentablePart = (IPresentablePart) element;
91             if (presentablePart.isDirty()) {
92                 return DefaultTabItem.DIRTY_PREFIX + presentablePart.getName();
93             }
94             
95             return presentablePart.getName();
96         }
97
98         public Image getImage(Object JavaDoc element) {
99             IPresentablePart presentablePart = (IPresentablePart) element;
100             return presentablePart.getTitleImage();
101         }
102
103         public Font getFont(Object JavaDoc element) {
104             IPresentablePart presentablePart = (IPresentablePart)element;
105
106             AbstractTabItem item = folder.getTab(presentablePart);
107             // if in single tab mode, do not use the bold font for non-visible tabs
108
// if in multiple tab mode, use the bold for non-visible tabs only
109
if ((item!=null && item.isShowing()) || !hiddenTabsBold) {
110                 return folder.getTabFolder().getControl().getFont();
111             }
112
113             if (boldFont == null) {
114                 Control control = folder.getTabFolder().getControl();
115                 Font originalFont = control.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                 }
121                 boldFont = new Font(control.getDisplay(), fontData);
122             }
123             return boldFont;
124         }
125
126         public void dispose() {
127             super.dispose();
128             if (boldFont != null) {
129                 boldFont.dispose();
130             }
131         }
132     }
133
134     private class BasicStackListViewerComparator extends ViewerComparator {
135         /**
136          * Constructor for stack list viewer's comparator.
137          *
138          */

139         public BasicStackListViewerComparator() {
140             //no-op
141
}
142
143         /* (non-Javadoc)
144          * @see org.eclipse.jface.viewers.ViewerComparator#compare(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
145          */

146         public int compare(Viewer viewer, Object JavaDoc e1, Object JavaDoc e2) {
147             int cat1 = category(e1);
148             int cat2 = category(e2);
149
150             if (cat1 != cat2) {
151                 return cat1 - cat2;
152             }
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)
162                         .getLabelProvider();
163                 if (prov instanceof ILabelProvider) {
164                     ILabelProvider lprov = (ILabelProvider) prov;
165                     name1 = lprov.getText(e1);
166                     name2 = lprov.getText(e2);
167                     // ILabelProvider's implementation in BasicStackList calls
168
// DefaultEditorPresentation.getLabelText which returns the name of dirty
169
// files begining with dirty prefix, sorting should not take dirty prefix in consideration
170
String JavaDoc prefix = DefaultTabItem.DIRTY_PREFIX;
171                     if (name1.startsWith(prefix)) {
172                         name1 = name1.substring(prefix.length());
173                     }
174                     if (name2.startsWith(prefix)) {
175                         name2 = name2.substring(prefix.length());
176                     }
177                 } else {
178                     name1 = e1.toString();
179                     name2 = e2.toString();
180                 }
181             }
182             if (name1 == null) {
183                 name1 = "";//$NON-NLS-1$
184
}
185             if (name2 == null) {
186                 name2 = "";//$NON-NLS-1$
187
}
188             return getComparator().compare(name1, name2);
189         }
190
191         /* (non-Javadoc)
192          * @see org.eclipse.jface.viewers.ViewerComparator#category(java.lang.Object)
193          */

194         public int category(Object JavaDoc element) {
195
196             IPresentablePart part = (IPresentablePart)element;
197             AbstractTabItem tabItem = folder.getTab(part);
198             
199             if (tabItem!=null && tabItem.isShowing()) {
200                 return 1; // visible
201
}
202             return 0; // not visible
203
}
204     }
205
206     /**
207      * Constructor for BasicPartList.
208      *
209      * @param parent the parent shell
210      * @param shellStyle the SWT style bits used to create the shell
211      * @param treeStyle the SWT style bits used to create the tree
212      * @param site
213      * @param folder
214      */

215     public BasicPartList(Shell parent, int shellStyle, int treeStyle,
216             IStackPresentationSite site, PresentablePartFolder folder) {
217         super(parent, shellStyle, treeStyle);
218         
219         this.site = site;
220         this.folder = folder;
221     }
222
223     protected TableViewer createTableViewer(Composite parent, int style) {
224         Table table = new Table(parent, SWT.SINGLE | (style & ~SWT.MULTI));
225         table.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
226         TableViewer tableViewer = new TableViewer(table) {
227             /* (non-Javadoc)
228              * @see org.eclipse.jface.viewers.TableViewer#internalRefresh(java.lang.Object)
229              */

230             protected void internalRefresh(Object JavaDoc element) {
231                 boolean usingMotif = "motif".equals(SWT.getPlatform()); //$NON-NLS-1$
232
try {
233                     // This avoids a "graphic is disposed" error on Motif by not letting
234
// it redraw while we remove entries. Some items in this table are
235
// being removed and may have icons which may have already been
236
// disposed elsewhere.
237
if (usingMotif) {
238                         getTable().setRedraw(false);
239                     }
240                     super.internalRefresh(element);
241                 } finally {
242                     if (usingMotif) {
243                         getTable().setRedraw(true);
244                     }
245                 }
246             }
247         };
248         tableViewer.addFilter(new NamePatternFilter());
249         tableViewer.setContentProvider(new BasicStackListContentProvider());
250         tableViewer.setComparator(new BasicStackListViewerComparator());
251         tableViewer.setLabelProvider(new BasicStackListLabelProvider());
252         return tableViewer;
253     }
254
255     public void setInput(Object JavaDoc information) {
256         PresentablePartFolder newFolder = (PresentablePartFolder) information;
257         inputChanged(newFolder, newFolder.getCurrent());
258     }
259
260     protected void gotoSelectedElement() {
261         Object JavaDoc selectedElement = getSelectedElement();
262
263         //close the shell
264
dispose();
265
266         if (selectedElement instanceof IPresentablePart) {
267             site.selectPart((IPresentablePart)selectedElement);
268         }
269     }
270
271     protected boolean deleteSelectedElements() {
272
273         IStructuredSelection structuredSelection = getSelectedElements();
274
275         if (structuredSelection != null) {
276
277             ArrayList JavaDoc list = new ArrayList JavaDoc(structuredSelection.size());
278
279             for (Iterator JavaDoc iterator = structuredSelection.iterator(); iterator
280                     .hasNext();) {
281                 IPresentablePart presentablePart = (IPresentablePart) iterator.next();
282                 list.add(presentablePart);
283             }
284
285             site.close((IPresentablePart[]) list
286                     .toArray(new IPresentablePart[list.size()]));
287         }
288
289         if (folder.isDisposed()) {
290             fComposite.dispose();
291             return true;
292         }
293         return false;
294
295     }
296 }
297
Popular Tags