KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > preferences > FileTypeTable


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.team.internal.ui.preferences;
13
14 import java.util.Collection JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.jface.viewers.*;
18 import org.eclipse.osgi.util.TextProcessor;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.custom.TableEditor;
21 import org.eclipse.swt.graphics.Image;
22 import org.eclipse.swt.widgets.*;
23 import org.eclipse.team.core.Team;
24 import org.eclipse.team.internal.ui.*;
25
26
27 public class FileTypeTable implements ICellModifier, IStructuredContentProvider, ITableLabelProvider {
28     
29     private final static int COMBO_INDEX_BINARY= 0;
30     private final static int COMBO_INDEX_TEXT= 1;
31     private final static String JavaDoc [] MODES_TEXT= { TeamUIMessages.FileTypeTable_0, TeamUIMessages.FileTypeTable_1 }; //
32

33     private final static int COMBO_INDEX_SAVE= 0;
34     private final static int COMBO_INDEX_DONT_SAVE= 1;
35     private static final String JavaDoc [] SAVE_TEXT= { TeamUIMessages.FileTypeTable_2, TeamUIMessages.FileTypeTable_3 }; //
36

37     private static final class FileTypeComparator extends ViewerComparator {
38         
39         public FileTypeComparator() {
40         }
41         
42         private int getCategory(Object JavaDoc element) {
43             if (element instanceof Extension)
44                 return 0;
45             if (element instanceof Name) {
46                 return 1;
47             }
48             return 2;
49         }
50
51         public int compare(Viewer viewer,Object JavaDoc e1,Object JavaDoc e2) {
52             final int compare= getCategory(e1) - getCategory(e2);
53             if (compare != 0)
54                 return compare;
55             return super.compare(viewer, ((Item)e1).name, ((Item)e2).name);
56         }
57     }
58
59     public abstract static class Item implements Comparable JavaDoc {
60         public final String JavaDoc name;
61         public boolean save;
62         public int mode;
63         public boolean contributed;
64         
65         public Item(String JavaDoc name, boolean contributed) { this.name= name; this.contributed = contributed; save= true; mode= Team.BINARY; }
66         
67         /* (non-Javadoc)
68          * @see java.lang.Comparable#compareTo(java.lang.Object)
69          */

70         public int compareTo(Object JavaDoc o) {
71             return name.compareTo(((Item)o).name);
72         }
73     }
74     
75     public static class Extension extends Item {
76         public Extension(String JavaDoc name, boolean contributed) { super(name, contributed); }
77     }
78
79     public static class Name extends Item {
80         public Name(String JavaDoc name, boolean contributed) { super(name, contributed); }
81     }
82     
83     private final static int COLUMN_PADDING = 5;
84     
85     protected static final String JavaDoc ITEM = "item"; //$NON-NLS-1$
86
protected static final String JavaDoc PROPERTY_MODE= "mode"; //$NON-NLS-1$
87
protected static final String JavaDoc PROPERTY_SAVE= "save"; //$NON-NLS-1$
88

89     private final TableViewer fTableViewer;
90     private final List JavaDoc fItems;
91     private final boolean fShowSaveColumn;
92     
93     public FileTypeTable(Composite composite, List JavaDoc items, boolean showSaveColumn) {
94         
95         fShowSaveColumn= showSaveColumn;
96         fItems= items;
97         
98
99         /**
100          * Create a table.
101          */

102         final Table table = new Table(composite, SWT.V_SCROLL | SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION);
103         table.setLayoutData(SWTUtils.createHVFillGridData());
104         table.setLinesVisible(true);
105         table.setHeaderVisible(true);
106         
107         final PixelConverter converter= SWTUtils.createDialogPixelConverter(composite);
108         
109         /**
110          * The 'Extension' column
111          */

112         final TableColumn fileColumn = new TableColumn(table, SWT.NONE, 0);
113         fileColumn.setWidth(converter.convertWidthInCharsToPixels(TeamUIMessages.FileTypeTable_4.length() + COLUMN_PADDING));
114         fileColumn.setText(TeamUIMessages.FileTypeTable_4);
115         
116         /**
117          * The 'Mode' column
118          */

119         final TableColumn modeColumn = new TableColumn(table, SWT.NONE, 1);
120         int length;
121         try {
122             length = TeamUIMessages.FileTypeTable_5.length();
123             length = Math.max(length, TeamUIMessages.FileTypeTable_0.length());
124             length = Math.max(length, TeamUIMessages.FileTypeTable_1.length());
125         } catch (RuntimeException JavaDoc e) {
126             // There may be an unbound message so just pick a reasonable length
127
length = 15;
128         }
129         modeColumn.setWidth(converter.convertWidthInCharsToPixels(length + COLUMN_PADDING));
130         modeColumn.setText(TeamUIMessages.FileTypeTable_5);
131
132         /**
133          * The 'Save' column
134          */

135         if (fShowSaveColumn) {
136             final TableColumn saveColumn = new TableColumn(table, SWT.NONE, 2);
137             saveColumn.setWidth(converter.convertWidthInCharsToPixels(TeamUIMessages.FileTypeTable_6.length() + COLUMN_PADDING));
138             saveColumn.setText(TeamUIMessages.FileTypeTable_6);
139         }
140         
141         /**
142          * Create a viewer for the table.
143          */

144         fTableViewer = new TableViewer(table);
145         fTableViewer.setContentProvider(this);
146         fTableViewer.setLabelProvider(this);
147         fTableViewer.setComparator(new FileTypeComparator());
148
149         /**
150          * Add a cell editor in the Keyword Substitution Mode column
151          */

152         new TableEditor(table);
153         
154         final CellEditor modeEditor = new ComboBoxCellEditor(table, MODES_TEXT, SWT.READ_ONLY);
155         final CellEditor saveEditor= new ComboBoxCellEditor(table, SAVE_TEXT, SWT.READ_ONLY);
156         
157         if (fShowSaveColumn) {
158             fTableViewer.setCellEditors(new CellEditor[] { null, modeEditor, saveEditor });
159             fTableViewer.setColumnProperties(new String JavaDoc [] { ITEM, PROPERTY_MODE, PROPERTY_SAVE });
160         } else {
161             fTableViewer.setCellEditors(new CellEditor [] { null, modeEditor });
162             fTableViewer.setColumnProperties(new String JavaDoc [] { ITEM, PROPERTY_MODE });
163         }
164             
165         fTableViewer.setCellModifier(this);
166
167         fTableViewer.setInput(fItems);
168     }
169     
170
171     public Object JavaDoc getValue(Object JavaDoc element, String JavaDoc property) {
172         
173         final Item item= (Item)element;
174         
175         if (PROPERTY_MODE.equals(property)) {
176             if (item.mode == Team.BINARY)
177                 return new Integer JavaDoc(COMBO_INDEX_BINARY);
178             if (item.mode == Team.TEXT)
179                 return new Integer JavaDoc(COMBO_INDEX_TEXT);
180         }
181         
182         if (fShowSaveColumn && PROPERTY_SAVE.equals(property)) {
183             return new Integer JavaDoc(item.save ? COMBO_INDEX_SAVE : COMBO_INDEX_DONT_SAVE);
184         }
185         return null;
186     }
187
188     public boolean canModify(Object JavaDoc element, String JavaDoc property) {
189         return PROPERTY_MODE.equals(property) || (fShowSaveColumn && PROPERTY_SAVE.equals(property));
190     }
191
192     public void modify(Object JavaDoc element, String JavaDoc property, Object JavaDoc value) {
193         
194         final IStructuredSelection selection = (IStructuredSelection)fTableViewer.getSelection();
195         final Item item= (Item)selection.getFirstElement();
196         if (item == null)
197             return;
198
199         final int comboIndex = ((Integer JavaDoc)value).intValue();
200         
201         if (PROPERTY_MODE.equals(property)) {
202             if (comboIndex == COMBO_INDEX_BINARY)
203                 item.mode= Team.BINARY;
204             if (comboIndex == COMBO_INDEX_TEXT)
205                 item.mode= Team.TEXT;
206         }
207             
208         if (fShowSaveColumn && PROPERTY_SAVE.equals(property)) {
209             item.save= COMBO_INDEX_SAVE == comboIndex;
210         }
211         fTableViewer.refresh(item);
212     }
213
214     public Image getColumnImage(Object JavaDoc element, int columnIndex) {
215         return null;
216     }
217
218     public String JavaDoc getColumnText(Object JavaDoc element, int columnIndex) {
219
220         final Item item= (Item) element;
221         
222         if (columnIndex == 0) {
223             String JavaDoc label = (item instanceof Extension ? "*." : "") + item.name; //$NON-NLS-1$ //$NON-NLS-2$
224
label = TextProcessor.process(label, ".*"); //$NON-NLS-1$
225
return label;
226         }
227         
228         if (columnIndex == 1) {
229             if (item.mode == Team.BINARY) {
230                 return MODES_TEXT[COMBO_INDEX_BINARY];
231             } else if (item.mode == Team.TEXT) {
232                 return MODES_TEXT[COMBO_INDEX_TEXT];
233             }
234         }
235         
236         if (columnIndex == 2) {
237             if (fShowSaveColumn) return SAVE_TEXT[item.save ? COMBO_INDEX_SAVE : COMBO_INDEX_DONT_SAVE];
238         }
239         
240         return null;
241     }
242
243     public void addListener(ILabelProviderListener listener) {}
244
245     public void dispose() {}
246
247     public boolean isLabelProperty(Object JavaDoc element, String JavaDoc property) {
248         return false;
249     }
250
251     public void removeListener(ILabelProviderListener listener) {}
252
253     public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
254         return ((Collection JavaDoc)inputElement).toArray();
255     }
256
257     public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {}
258     
259     public IStructuredSelection getSelection() {
260         return (IStructuredSelection)fTableViewer.getSelection();
261     }
262     
263     public void setInput(List JavaDoc items) {
264         fItems.clear();
265         fItems.addAll(items);
266         fTableViewer.refresh();
267     }
268     
269     public TableViewer getViewer() {
270         return fTableViewer;
271     }
272 }
273
Popular Tags