001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.presentations;
011:
012: import java.util.ArrayList;
013: import java.util.Iterator;
014:
015: import org.eclipse.jface.viewers.ContentViewer;
016: import org.eclipse.jface.viewers.IBaseLabelProvider;
017: import org.eclipse.jface.viewers.IFontProvider;
018: import org.eclipse.jface.viewers.ILabelProvider;
019: import org.eclipse.jface.viewers.IStructuredContentProvider;
020: import org.eclipse.jface.viewers.IStructuredSelection;
021: import org.eclipse.jface.viewers.LabelProvider;
022: import org.eclipse.jface.viewers.TableViewer;
023: import org.eclipse.jface.viewers.Viewer;
024: import org.eclipse.jface.viewers.ViewerComparator;
025: import org.eclipse.swt.SWT;
026: import org.eclipse.swt.graphics.Font;
027: import org.eclipse.swt.graphics.FontData;
028: import org.eclipse.swt.graphics.Image;
029: import org.eclipse.swt.layout.GridData;
030: import org.eclipse.swt.widgets.Composite;
031: import org.eclipse.swt.widgets.Control;
032: import org.eclipse.swt.widgets.Shell;
033: import org.eclipse.swt.widgets.Table;
034: import org.eclipse.ui.internal.presentations.defaultpresentation.DefaultTabItem;
035: import org.eclipse.ui.internal.presentations.util.AbstractTabItem;
036: import org.eclipse.ui.internal.presentations.util.PresentablePartFolder;
037: import org.eclipse.ui.presentations.IPresentablePart;
038: import org.eclipse.ui.presentations.IStackPresentationSite;
039:
040: public class BasicPartList extends AbstractTableInformationControl {
041:
042: private boolean hiddenTabsBold = true;
043: private PresentablePartFolder folder;
044: private IStackPresentationSite site;
045:
046: private class BasicStackListContentProvider implements
047: IStructuredContentProvider {
048:
049: /**
050: * Constructor for stack list content provider.
051: *
052: */
053: public BasicStackListContentProvider() {
054: //no-op
055: }
056:
057: public void dispose() {
058: //no-op
059: }
060:
061: public Object[] getElements(Object inputElement) {
062: if (folder == null) {
063: return new IPresentablePart[0];
064: }
065:
066: return folder.getPartList();
067: }
068:
069: public void inputChanged(Viewer viewer, Object oldInput,
070: Object newInput) {
071: folder = (PresentablePartFolder) newInput;
072: }
073: }
074:
075: private class BasicStackListLabelProvider extends LabelProvider
076: implements IFontProvider {
077:
078: private Font boldFont = null;
079:
080: /**
081: * Constructor for stack list label provider.
082: *
083: */
084: public BasicStackListLabelProvider() {
085: //no-op
086: }
087:
088: public String getText(Object element) {
089: IPresentablePart presentablePart = (IPresentablePart) element;
090: if (presentablePart.isDirty()) {
091: return DefaultTabItem.DIRTY_PREFIX
092: + presentablePart.getName();
093: }
094:
095: return presentablePart.getName();
096: }
097:
098: public Image getImage(Object element) {
099: 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 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()
120: | SWT.BOLD);
121: }
122: boldFont = new Font(control.getDisplay(), fontData);
123: }
124: return boldFont;
125: }
126:
127: public void dispose() {
128: super .dispose();
129: if (boldFont != null) {
130: boldFont.dispose();
131: }
132: }
133: }
134:
135: private class BasicStackListViewerComparator extends
136: ViewerComparator {
137: /**
138: * Constructor for stack list viewer's comparator.
139: *
140: */
141: public BasicStackListViewerComparator() {
142: //no-op
143: }
144:
145: /* (non-Javadoc)
146: * @see org.eclipse.jface.viewers.ViewerComparator#compare(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
147: */
148: public int compare(Viewer viewer, Object e1, Object e2) {
149: int cat1 = category(e1);
150: int cat2 = category(e2);
151:
152: if (cat1 != cat2) {
153: return cat1 - cat2;
154: }
155:
156: String name1;
157: String name2;
158:
159: if (viewer == null || !(viewer instanceof ContentViewer)) {
160: name1 = e1.toString();
161: name2 = e2.toString();
162: } else {
163: IBaseLabelProvider prov = ((ContentViewer) viewer)
164: .getLabelProvider();
165: if (prov instanceof ILabelProvider) {
166: ILabelProvider lprov = (ILabelProvider) prov;
167: name1 = lprov.getText(e1);
168: name2 = lprov.getText(e2);
169: // ILabelProvider's implementation in BasicStackList calls
170: // DefaultEditorPresentation.getLabelText which returns the name of dirty
171: // files begining with dirty prefix, sorting should not take dirty prefix in consideration
172: String prefix = DefaultTabItem.DIRTY_PREFIX;
173: if (name1.startsWith(prefix)) {
174: name1 = name1.substring(prefix.length());
175: }
176: if (name2.startsWith(prefix)) {
177: name2 = name2.substring(prefix.length());
178: }
179: } else {
180: name1 = e1.toString();
181: name2 = e2.toString();
182: }
183: }
184: if (name1 == null) {
185: name1 = "";//$NON-NLS-1$
186: }
187: if (name2 == null) {
188: name2 = "";//$NON-NLS-1$
189: }
190: return getComparator().compare(name1, name2);
191: }
192:
193: /* (non-Javadoc)
194: * @see org.eclipse.jface.viewers.ViewerComparator#category(java.lang.Object)
195: */
196: public int category(Object element) {
197:
198: IPresentablePart part = (IPresentablePart) element;
199: AbstractTabItem tabItem = folder.getTab(part);
200:
201: if (tabItem != null && tabItem.isShowing()) {
202: return 1; // visible
203: }
204: return 0; // not visible
205: }
206: }
207:
208: /**
209: * Constructor for BasicPartList.
210: *
211: * @param parent the parent shell
212: * @param shellStyle the SWT style bits used to create the shell
213: * @param treeStyle the SWT style bits used to create the tree
214: * @param site
215: * @param folder
216: */
217: public BasicPartList(Shell parent, int shellStyle, int treeStyle,
218: IStackPresentationSite site, PresentablePartFolder folder) {
219: super (parent, shellStyle, treeStyle);
220:
221: this .site = site;
222: this .folder = folder;
223: }
224:
225: protected TableViewer createTableViewer(Composite parent, int style) {
226: Table table = new Table(parent, SWT.SINGLE
227: | (style & ~SWT.MULTI));
228: table.setLayoutData(new GridData(
229: GridData.VERTICAL_ALIGN_BEGINNING));
230: TableViewer tableViewer = new TableViewer(table) {
231: /* (non-Javadoc)
232: * @see org.eclipse.jface.viewers.TableViewer#internalRefresh(java.lang.Object)
233: */
234: protected void internalRefresh(Object element) {
235: boolean usingMotif = "motif".equals(SWT.getPlatform()); //$NON-NLS-1$
236: try {
237: // This avoids a "graphic is disposed" error on Motif by not letting
238: // it redraw while we remove entries. Some items in this table are
239: // being removed and may have icons which may have already been
240: // disposed elsewhere.
241: if (usingMotif) {
242: getTable().setRedraw(false);
243: }
244: super .internalRefresh(element);
245: } finally {
246: if (usingMotif) {
247: getTable().setRedraw(true);
248: }
249: }
250: }
251: };
252: tableViewer.addFilter(new NamePatternFilter());
253: tableViewer
254: .setContentProvider(new BasicStackListContentProvider());
255: tableViewer.setComparator(new BasicStackListViewerComparator());
256: tableViewer.setLabelProvider(new BasicStackListLabelProvider());
257: return tableViewer;
258: }
259:
260: public void setInput(Object information) {
261: PresentablePartFolder newFolder = (PresentablePartFolder) information;
262: inputChanged(newFolder, newFolder.getCurrent());
263: }
264:
265: protected void gotoSelectedElement() {
266: Object selectedElement = getSelectedElement();
267:
268: //close the shell
269: dispose();
270:
271: if (selectedElement instanceof IPresentablePart) {
272: site.selectPart((IPresentablePart) selectedElement);
273: }
274: }
275:
276: protected boolean deleteSelectedElements() {
277:
278: IStructuredSelection structuredSelection = getSelectedElements();
279:
280: if (structuredSelection != null) {
281:
282: ArrayList list = new ArrayList(structuredSelection.size());
283:
284: for (Iterator iterator = structuredSelection.iterator(); iterator
285: .hasNext();) {
286: IPresentablePart presentablePart = (IPresentablePart) iterator
287: .next();
288: list.add(presentablePart);
289: }
290:
291: site.close((IPresentablePart[]) list
292: .toArray(new IPresentablePart[list.size()]));
293: }
294:
295: if (folder.isDisposed()) {
296: fComposite.dispose();
297: return true;
298: }
299: return false;
300:
301: }
302: }
|