1 11 package org.eclipse.team.internal.ui.dialogs; 12 13 import org.eclipse.core.resources.mapping.ResourceMapping; 14 import org.eclipse.jface.viewers.*; 15 import org.eclipse.swt.SWT; 16 import org.eclipse.swt.events.SelectionAdapter; 17 import org.eclipse.swt.events.SelectionEvent; 18 import org.eclipse.swt.layout.GridData; 19 import org.eclipse.swt.layout.GridLayout; 20 import org.eclipse.swt.widgets.Button; 21 import org.eclipse.swt.widgets.Composite; 22 import org.eclipse.swt.widgets.Table; 23 import org.eclipse.team.internal.ui.TeamUIMessages; 24 import org.eclipse.team.internal.ui.mapping.ResourceMappingLabelProvider; 25 import org.eclipse.ui.model.AdaptableList; 26 import org.eclipse.ui.model.BaseWorkbenchContentProvider; 27 28 31 public class ResourceMappingSelectionArea extends DialogArea { 32 33 37 public static final String SELECTED_MAPPING = "SelectedMapping"; 39 44 public static final String CHECKED_MAPPINGS = "CheckedMappings"; 46 private ResourceMapping[] mappings; 47 private TableViewer viewer; 48 private ResourceMapping[] checkedMappings; 49 private ResourceMapping selectedMapping; 50 private String description; 51 private boolean supportsChecking; 52 private boolean supportsSelection; 53 54 public ResourceMappingSelectionArea(ResourceMapping[] mappings, boolean supportSelection, boolean supportChecking) { 55 this.mappings = mappings; 56 this.supportsChecking = supportChecking; 57 this.supportsSelection = supportSelection; 58 } 59 60 63 public void createArea(Composite parent) { 64 Composite composite = createComposite(parent, 1, true); 65 GridLayout layout = new GridLayout(1, false); 66 layout.marginHeight = 0; 67 layout.marginWidth = 0; 68 layout.verticalSpacing = 0; 69 layout.horizontalSpacing = 0; 70 composite.setLayout(layout); 71 72 if (description != null) 73 createWrappingLabel(composite, description, 1); 74 75 createViewer(composite); 76 GridData data = new GridData(GridData.FILL_BOTH); 77 data.heightHint = 100; 78 data.widthHint = 300; 79 viewer.getControl().setLayoutData(data); 80 viewer.setContentProvider(new BaseWorkbenchContentProvider()); 81 viewer.setLabelProvider(new ResourceMappingLabelProvider()); 82 viewer.setInput(new AdaptableList(mappings)); 83 if (isSupportsSelection()) { 84 viewer.addSelectionChangedListener(new ISelectionChangedListener() { 85 public void selectionChanged(SelectionChangedEvent event) { 86 ResourceMapping oldSelection = selectedMapping; 87 selectedMapping = internalGetSelectedMapping(); 88 if (oldSelection != selectedMapping) 89 firePropertyChangeChange(SELECTED_MAPPING, oldSelection, selectedMapping); 90 } 91 }); 92 } 93 if (isSupportsChecking()) 94 initializeCheckboxViewer(composite); 95 } 96 97 private void initializeCheckboxViewer(Composite composite) { 98 final CheckboxTableViewer checkboxViewer = getCheckboxTableViewer(); 99 checkboxViewer.addCheckStateListener(new ICheckStateListener() { 100 public void checkStateChanged(CheckStateChangedEvent event) { 101 ResourceMapping[] oldMappings = checkedMappings; 102 checkedMappings = internalGetCheckedMappings(); 103 if (oldMappings != checkedMappings) 104 firePropertyChangeChange(CHECKED_MAPPINGS, oldMappings, checkedMappings); 105 } 106 }); 107 checkboxViewer.setCheckedElements(mappings); 108 checkedMappings = mappings; 109 110 Composite buttons = createEmbeddedButtonComposite(composite); 111 112 Button selectAll = new Button(buttons, SWT.PUSH); 113 selectAll.setText(TeamUIMessages.ResourceMappingSelectionArea_0); 114 selectAll.setLayoutData(new GridData(GridData.FILL_BOTH)); 115 selectAll.addSelectionListener(new SelectionAdapter() { 116 public void widgetSelected(SelectionEvent e) { 117 checkboxViewer.setAllChecked(true); 118 } 119 }); 120 121 Button deselectAll = new Button(buttons, SWT.PUSH); 122 deselectAll.setText(TeamUIMessages.ResourceMappingSelectionArea_1); 123 deselectAll.setLayoutData(new GridData(GridData.FILL_BOTH)); 124 deselectAll.addSelectionListener(new SelectionAdapter() { 125 public void widgetSelected(SelectionEvent e) { 126 checkboxViewer.setAllChecked(false); 127 } 128 }); 129 } 130 131 private void createViewer(Composite composite) { 132 if (isSupportsChecking()) 133 viewer = CheckboxTableViewer.newCheckList(composite, getViewerStyle()); 134 else 135 viewer = new TableViewer(new Table(composite, getViewerStyle())); 136 } 137 138 private int getViewerStyle() { 139 int style = SWT.BORDER; 140 if (isSupportsSelection()) 141 style |= SWT.SINGLE; 142 return style; 143 } 144 145 ResourceMapping[] internalGetCheckedMappings() { 146 Object [] checked = getCheckboxTableViewer().getCheckedElements(); 147 ResourceMapping[] mappings = new ResourceMapping[checked.length]; 148 for (int i = 0; i < checked.length; i++) { 149 Object object = checked[i]; 150 mappings[i] = (ResourceMapping)object; 151 } 152 return mappings; 153 } 154 155 private Composite createEmbeddedButtonComposite(Composite composite) { 156 GridData data; 157 Composite buttons = new Composite(composite, SWT.NONE); 158 GridLayout layout = new GridLayout(); 159 layout.numColumns = 2; layout.makeColumnsEqualWidth = true; 161 layout.marginWidth = 0; 162 buttons.setLayout(layout); 163 data = new GridData(GridData.HORIZONTAL_ALIGN_END 164 | GridData.VERTICAL_ALIGN_CENTER); 165 buttons.setLayoutData(data); 166 return buttons; 167 } 168 169 ResourceMapping internalGetSelectedMapping() { 170 ISelection selection = viewer.getSelection(); 171 if (selection instanceof IStructuredSelection) { 172 IStructuredSelection ss = (IStructuredSelection) selection; 173 Object firstElement = ss.getFirstElement(); 174 if (firstElement instanceof ResourceMapping) 175 return (ResourceMapping)firstElement; 176 } 177 return null; 178 } 179 180 public void setDescription(String description) { 181 this.description = description; 182 } 183 public ResourceMapping[] getCheckedMappings() { 184 return checkedMappings; 185 } 186 public ResourceMapping getSelectedMapping() { 187 return selectedMapping; 188 } 189 190 private CheckboxTableViewer getCheckboxTableViewer() { 191 return (CheckboxTableViewer)viewer; 192 } 193 194 public boolean isSupportsChecking() { 195 return supportsChecking; 196 } 197 198 public boolean isSupportsSelection() { 199 return supportsSelection; 200 } 201 } 202 | Popular Tags |