Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7793f8e9e192ea31c2a15edbc1353c8acb281c9d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.team.internal.ui.preferences;

import java.util.Collection;
import java.util.List;

import org.eclipse.jface.viewers.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.*;
import org.eclipse.team.core.Team;
import org.eclipse.team.internal.ui.SWTUtils;


public class FileTypeTable implements ICellModifier, IStructuredContentProvider, ITableLabelProvider {
    
    private final static int COMBO_INDEX_BINARY= 0;
    private final static int COMBO_INDEX_TEXT= 1;
    private final static String [] MODES_TEXT= { "Binary", "ASCII Text" };
    
    private final static int COMBO_INDEX_SAVE= 0;
    private final static int COMBO_INDEX_DONT_SAVE= 1;
    private static final String [] SAVE_TEXT= { "Yes", "No" };

    public interface PixelConverter	{
        
        int convertWidthInCharsToPixels(int chars);
        
    }
    
    public abstract static class Item {
        public final String name;
        public boolean save;
        public int mode;
        
        public Item(String name) { this.name= name; save= true; mode= Team.BINARY; }
    }
    
    public static class Extension extends Item {
        public Extension(String name) { super(name); }

    }

    public static class Name extends Item {
        public Name(String name) { super(name); }
    }
    
    private final static int SMALL_COLUMN= 15;
    private final static int LARGE_COLUMN= 30;
    
    protected static final String ITEM = "item";
    protected static final String PROPERTY_MODE= "mode";
    protected static final String PROPERTY_SAVE= "save";

    private final TableViewer fTableViewer;
    private final List fItems;
    private final boolean fShowSaveColumn;
    
	public FileTypeTable(Composite composite, PixelConverter converter, List items, boolean showSaveColumn) {
	    
	    fShowSaveColumn= showSaveColumn;
	    
	    fItems= items;

		/**
		 * Create a table.
		 */
		final Table table = new Table(composite, SWT.V_SCROLL | SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION);
		table.setLayoutData(SWTUtils.createHVFillGridData());
		table.setLinesVisible(false);
		table.setHeaderVisible(true);
		
		/**
		 * Create a viewer for the table.
		 */
		fTableViewer = new TableViewer(table);
		fTableViewer.setContentProvider(this);
		fTableViewer.setLabelProvider(this);
		fTableViewer.setSorter(new ViewerSorter() {
		    public int category(Object element) {
		        if (element instanceof Extension)
		            return 0;
		        if (element instanceof Name) {
		            return 1;
		        }
		        return 2;
		    }
		    
		});

		/**
		 * The 'Extension' column
		 */
		final TableColumn fileColumn = new TableColumn(table, SWT.NONE, 0);
		fileColumn.setWidth(converter.convertWidthInCharsToPixels(LARGE_COLUMN));
		fileColumn.setText("Name/Extension");
		
		/**
		 * The 'Mode' column
		 */
		final TableColumn modeColumn = new TableColumn(table, SWT.NONE, 1);
		modeColumn.setWidth(converter.convertWidthInCharsToPixels(SMALL_COLUMN));
		modeColumn.setText("Content");

		/**
		 * The 'Save' column
		 */
		if (fShowSaveColumn) {
		    final TableColumn saveColumn = new TableColumn(table, SWT.NONE, 2);
		    saveColumn.setWidth(converter.convertWidthInCharsToPixels(LARGE_COLUMN));
		    saveColumn.setText("Remember decision?");
		}
		
		/**
		 * Add a cell editor in the Keyword Substitution Mode column
		 */
		new TableEditor(table);
		
		final CellEditor modeEditor = new ComboBoxCellEditor(table, MODES_TEXT, SWT.READ_ONLY);
		final CellEditor saveEditor= new ComboBoxCellEditor(table, SAVE_TEXT, SWT.READ_ONLY);
		
		if (fShowSaveColumn) {
		    fTableViewer.setCellEditors(new CellEditor[] { null, modeEditor, saveEditor });
		    fTableViewer.setColumnProperties(new String [] { ITEM, PROPERTY_MODE, PROPERTY_SAVE });
		} else { 
		    fTableViewer.setCellEditors(new CellEditor [] { null, modeEditor });
		    fTableViewer.setColumnProperties(new String [] { ITEM, PROPERTY_MODE });
		}
		    
		fTableViewer.setCellModifier(this);

		fTableViewer.setInput(fItems);
	}
	

    public Object getValue(Object element, String property) {
        
        final Item item= (Item)element;
        
        if (PROPERTY_MODE.equals(property)) {
            if (item.mode == Team.BINARY)
                return new Integer(COMBO_INDEX_BINARY);
            if (item.mode == Team.TEXT) 
                return new Integer(COMBO_INDEX_TEXT);
        }
        
        if (fShowSaveColumn && PROPERTY_SAVE.equals(property)) {
            return new Integer(item.save ? COMBO_INDEX_SAVE : COMBO_INDEX_DONT_SAVE);
        }
        return null;
    }

    public boolean canModify(Object element, String property) {
    	return PROPERTY_MODE.equals(property) || (fShowSaveColumn && PROPERTY_SAVE.equals(property));
    }

    public void modify(Object element, String property, Object value) {
        
        final IStructuredSelection selection = (IStructuredSelection)fTableViewer.getSelection();
        final Item item= (Item)selection.getFirstElement();

        final int comboIndex = ((Integer)value).intValue();
        
        if (PROPERTY_MODE.equals(property)) {
    	    if (comboIndex == COMBO_INDEX_BINARY)
    	        item.mode= Team.BINARY;
    	    if (comboIndex == COMBO_INDEX_TEXT)
    	        item.mode= Team.TEXT;
        }
    	    
        if (fShowSaveColumn && PROPERTY_SAVE.equals(property)) {
    	    item.save= COMBO_INDEX_SAVE == comboIndex;
        }
        fTableViewer.refresh(item);
    }

    public Image getColumnImage(Object element, int columnIndex) {
    	return null;
    }

    public String getColumnText(Object element, int columnIndex) {

        final Item item= (Item) element;
        
        if (columnIndex == 0) { 
            return (item instanceof Extension ? "*." : "") + item.name;
        }
        
        if (columnIndex == 1) {
            if (item.mode == Team.BINARY) {
                return MODES_TEXT[COMBO_INDEX_BINARY];
            } else if (item.mode == Team.TEXT) {
                return MODES_TEXT[COMBO_INDEX_TEXT];
            }
        }
        
        if (columnIndex == 2) {
            if (fShowSaveColumn) return SAVE_TEXT[item.save ? COMBO_INDEX_SAVE : COMBO_INDEX_DONT_SAVE];
        }
        
    	return null;
    }

    public void addListener(ILabelProviderListener listener) {}

    public void dispose() {}

    public boolean isLabelProperty(Object element, String property) {
        return false;
    }

    public void removeListener(ILabelProviderListener listener) {}

    public Object[] getElements(Object inputElement) {	
    	return ((Collection)inputElement).toArray();
    }

    public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}
    
    public IStructuredSelection getSelection() {
        return (IStructuredSelection)fTableViewer.getSelection();
    }
    
    public void setInput(List items) {
        fItems.clear();
        fItems.addAll(items);
        fTableViewer.refresh();
    }
    
    public TableViewer getViewer() {
        return fTableViewer;
    }
}

Back to the top