1 11 12 package org.eclipse.ui.internal.presentations; 13 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 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 55 public BasicStackListContentProvider() { 56 } 58 59 public void dispose() { 60 } 62 63 public Object [] getElements(Object 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 oldInput, Object 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 85 public BasicStackListLabelProvider() { 86 } 88 89 public String getText(Object 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 element) { 99 IPresentablePart presentablePart = (IPresentablePart) element; 100 return presentablePart.getTitleImage(); 101 } 102 103 public Font getFont(Object element) { 104 IPresentablePart presentablePart = (IPresentablePart)element; 105 106 AbstractTabItem item = folder.getTab(presentablePart); 107 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 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 139 public BasicStackListViewerComparator() { 140 } 142 143 146 public int compare(Viewer viewer, Object e1, Object e2) { 147 int cat1 = category(e1); 148 int cat2 = category(e2); 149 150 if (cat1 != cat2) { 151 return cat1 - cat2; 152 } 153 154 String name1; 155 String 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 String 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 = ""; } 185 if (name2 == null) { 186 name2 = ""; } 188 return getComparator().compare(name1, name2); 189 } 190 191 194 public int category(Object element) { 195 196 IPresentablePart part = (IPresentablePart)element; 197 AbstractTabItem tabItem = folder.getTab(part); 198 199 if (tabItem!=null && tabItem.isShowing()) { 200 return 1; } 202 return 0; } 204 } 205 206 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 230 protected void internalRefresh(Object element) { 231 boolean usingMotif = "motif".equals(SWT.getPlatform()); try { 233 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 information) { 256 PresentablePartFolder newFolder = (PresentablePartFolder) information; 257 inputChanged(newFolder, newFolder.getCurrent()); 258 } 259 260 protected void gotoSelectedElement() { 261 Object selectedElement = getSelectedElement(); 262 263 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 list = new ArrayList (structuredSelection.size()); 278 279 for (Iterator 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 |