1 11 12 package org.eclipse.ui.internal.presentations; 13 14 import java.text.Collator ; 15 import java.util.ArrayList ; 16 import java.util.Iterator ; 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 } 49 50 public void dispose() { 51 } 53 54 public Object [] getElements(Object inputElement) { 55 if (basicStackPresentation == null) { return new CTabItem[0]; } 56 final PaneFolder tabFolder = basicStackPresentation.getTabFolder(); 57 58 69 70 return tabFolder.getItems(); 71 } 72 73 public void inputChanged(Viewer viewer, Object oldInput, Object 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 } 85 86 public String getText(Object element) { 87 CTabItem tabItem = (CTabItem) element; 88 DefaultPartPresentation basicStackPresentation = (DefaultPartPresentation) getTableViewer() 89 .getInput(); 90 IPresentablePart presentablePart = basicStackPresentation.getPartForTab(tabItem); 91 return basicStackPresentation.getLabelText(presentablePart, true); 93 } 94 95 public Image getImage(Object 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 element) { 104 CTabItem tabItem = (CTabItem) element; 105 CTabFolder tabFolder = tabItem.getParent(); 107 boolean singleTab = tabFolder.getSingle(); 108 109 if (tabItem.isShowing() || singleTab) 112 return null; 113 114 if (boldFont == null) { 115 Font originalFont = tabItem.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 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 } 137 138 public BasicStackListViewerSorter(Collator collator) { 139 super(collator); 140 } 141 142 145 public int compare(Viewer viewer, Object e1, Object e2) { 146 int cat1 = category(e1); 147 int cat2 = category(e2); 148 149 if (cat1 != cat2) 150 return cat1 - cat2; 151 152 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).getLabelProvider(); 162 if (prov instanceof ILabelProvider) { 163 ILabelProvider lprov = (ILabelProvider) prov; 164 name1 = lprov.getText(e1); 165 name2 = lprov.getText(e2); 166 String 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 = ""; if (name2 == null) 182 name2 = ""; return collator.compare(name1, name2); 184 } 185 188 public int category(Object element) { 189 190 CTabItem tabItem = (CTabItem) element; 191 if (tabItem.isShowing()) 192 return 1; return 0; } 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 208 protected void internalRefresh(Object element) { 209 boolean usingMotif = "motif".equals(SWT.getPlatform()); 210 try { 211 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 information) { 230 DefaultPartPresentation basicStackPresentation = (DefaultPartPresentation) information; 231 inputChanged(basicStackPresentation, basicStackPresentation.getTabFolder() 232 .getSelection()); 233 } 234 235 protected void gotoSelectedElement() { 236 Object selectedElement = getSelectedElement(); 237 238 DefaultPartPresentation basicStackPresentation = null; 239 if (selectedElement != null) { 240 basicStackPresentation = (DefaultPartPresentation) getTableViewer() 241 .getInput(); 242 } 243 dispose(); 245 246 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 list = new ArrayList (structuredSelection.size()); 260 261 for (Iterator 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 |