1 11 package org.eclipse.ui.internal.dialogs; 12 13 import com.ibm.icu.text.Collator; 14 import java.util.Arrays ; 15 import java.util.Collections ; 16 import java.util.Comparator ; 17 18 import org.eclipse.jface.preference.IPreferencePage; 19 import org.eclipse.jface.preference.PreferencePage; 20 import org.eclipse.jface.viewers.CheckStateChangedEvent; 21 import org.eclipse.jface.viewers.CheckboxTableViewer; 22 import org.eclipse.jface.viewers.ICheckStateListener; 23 import org.eclipse.jface.viewers.ISelectionChangedListener; 24 import org.eclipse.jface.viewers.IStructuredContentProvider; 25 import org.eclipse.jface.viewers.IStructuredSelection; 26 import org.eclipse.jface.viewers.LabelProvider; 27 import org.eclipse.jface.viewers.SelectionChangedEvent; 28 import org.eclipse.jface.viewers.StructuredSelection; 29 import org.eclipse.jface.viewers.Viewer; 30 import org.eclipse.swt.SWT; 31 import org.eclipse.swt.graphics.Font; 32 import org.eclipse.swt.layout.GridData; 33 import org.eclipse.swt.layout.GridLayout; 34 import org.eclipse.swt.widgets.Composite; 35 import org.eclipse.swt.widgets.Control; 36 import org.eclipse.swt.widgets.Label; 37 import org.eclipse.swt.widgets.Text; 38 import org.eclipse.ui.IWorkbench; 39 import org.eclipse.ui.IWorkbenchPreferencePage; 40 import org.eclipse.ui.PlatformUI; 41 import org.eclipse.ui.internal.IWorkbenchHelpContextIds; 42 import org.eclipse.ui.internal.WorkbenchMessages; 43 import org.eclipse.ui.internal.WorkbenchPlugin; 44 import org.eclipse.ui.internal.decorators.DecoratorDefinition; 45 import org.eclipse.ui.internal.decorators.DecoratorManager; 46 47 51 public class DecoratorsPreferencePage extends PreferencePage implements 52 IWorkbenchPreferencePage { 53 54 private Text descriptionText; 55 56 private CheckboxTableViewer checkboxViewer; 57 58 61 protected Control createContents(Composite parent) { 62 63 Font font = parent.getFont(); 64 65 PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, 66 IWorkbenchHelpContextIds.DECORATORS_PREFERENCE_PAGE); 67 68 Composite mainComposite = new Composite(parent, SWT.NONE); 69 mainComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 70 mainComposite.setFont(font); 71 72 GridLayout layout = new GridLayout(); 73 layout.marginWidth = 0; 74 layout.marginHeight = 0; 75 layout.verticalSpacing = 10; 76 mainComposite.setLayout(layout); 77 78 Label topLabel = new Label(mainComposite, SWT.NONE); 79 topLabel.setText(WorkbenchMessages.DecoratorsPreferencePage_explanation); 80 topLabel.setFont(font); 81 82 createDecoratorsArea(mainComposite); 83 createDescriptionArea(mainComposite); 84 populateDecorators(); 85 86 return mainComposite; 87 } 88 89 92 private void createDecoratorsArea(Composite mainComposite) { 93 94 Font mainFont = mainComposite.getFont(); 95 Composite decoratorsComposite = new Composite(mainComposite, SWT.NONE); 96 decoratorsComposite.setLayoutData(new GridData(GridData.FILL_BOTH)); 97 GridLayout decoratorsLayout = new GridLayout(); 98 decoratorsLayout.marginWidth = 0; 99 decoratorsLayout.marginHeight = 0; 100 decoratorsComposite.setLayout(decoratorsLayout); 101 decoratorsComposite.setFont(mainFont); 102 103 Label decoratorsLabel = new Label(decoratorsComposite, SWT.NONE); 104 decoratorsLabel.setText(WorkbenchMessages.DecoratorsPreferencePage_decoratorsLabel); 105 decoratorsLabel.setFont(mainFont); 106 107 checkboxViewer = CheckboxTableViewer.newCheckList(decoratorsComposite, 109 SWT.SINGLE | SWT.TOP | SWT.BORDER); 110 checkboxViewer.getTable().setLayoutData( 111 new GridData(GridData.FILL_BOTH)); 112 checkboxViewer.getTable().setFont(decoratorsComposite.getFont()); 113 checkboxViewer.setLabelProvider(new LabelProvider() { 114 public String getText(Object element) { 115 return ((DecoratorDefinition) element).getName(); 116 } 117 }); 118 checkboxViewer.getTable().setFont(mainFont); 119 120 checkboxViewer.setContentProvider(new IStructuredContentProvider() { 121 private final Comparator comparer = new Comparator () { 122 private Collator collator = Collator.getInstance(); 123 124 public int compare(Object arg0, Object arg1) { 125 String s1 = ((DecoratorDefinition) arg0).getName(); 126 String s2 = ((DecoratorDefinition) arg1).getName(); 127 return collator.compare(s1, s2); 128 } 129 }; 130 131 public void dispose() { 132 } 134 135 public void inputChanged(Viewer viewer, Object oldInput, 136 Object newInput) { 137 } 138 139 public Object [] getElements(Object inputElement) { 140 Object [] elements = (Object []) inputElement; 142 Object [] results = new Object [elements.length]; 143 System.arraycopy(elements, 0, results, 0, elements.length); 144 Collections.sort(Arrays.asList(results), comparer); 145 return results; 146 } 147 148 }); 149 150 checkboxViewer 151 .addSelectionChangedListener(new ISelectionChangedListener() { 152 public void selectionChanged(SelectionChangedEvent event) { 153 if (event.getSelection() instanceof IStructuredSelection) { 154 IStructuredSelection sel = (IStructuredSelection) event 155 .getSelection(); 156 DecoratorDefinition definition = (DecoratorDefinition) sel 157 .getFirstElement(); 158 if (definition == null) { 159 clearDescription(); 160 } else { 161 showDescription(definition); 162 } 163 } 164 } 165 }); 166 167 checkboxViewer.addCheckStateListener(new ICheckStateListener() { 168 public void checkStateChanged(CheckStateChangedEvent event) { 169 checkboxViewer.setSelection(new StructuredSelection(event 170 .getElement())); 171 } 172 }); 173 } 174 175 178 private void createDescriptionArea(Composite mainComposite) { 179 180 Font mainFont = mainComposite.getFont(); 181 Composite textComposite = new Composite(mainComposite, SWT.NONE); 182 textComposite.setLayoutData(new GridData(GridData.FILL_BOTH)); 183 GridLayout textLayout = new GridLayout(); 184 textLayout.marginWidth = 0; 185 textLayout.marginHeight = 0; 186 textComposite.setLayout(textLayout); 187 textComposite.setFont(mainFont); 188 189 Label descriptionLabel = new Label(textComposite, SWT.NONE); 190 descriptionLabel.setText(WorkbenchMessages.DecoratorsPreferencePage_description); 191 descriptionLabel.setFont(mainFont); 192 193 descriptionText = new Text(textComposite, SWT.MULTI | SWT.WRAP 194 | SWT.READ_ONLY | SWT.BORDER | SWT.H_SCROLL); 195 descriptionText.setLayoutData(new GridData(GridData.FILL_BOTH)); 196 descriptionText.setFont(mainFont); 197 } 198 199 202 private void populateDecorators() { 203 DecoratorDefinition[] definitions = getAllDefinitions(); 204 checkboxViewer.setInput(definitions); 205 for (int i = 0; i < definitions.length; i++) { 206 checkboxViewer.setChecked(definitions[i], definitions[i] 207 .isEnabled()); 208 } 209 } 210 211 214 private void showDescription(DecoratorDefinition definition) { 215 if (descriptionText == null || descriptionText.isDisposed()) { 216 return; 217 } 218 String text = definition.getDescription(); 219 if (text == null || text.length() == 0) { 220 descriptionText.setText(WorkbenchMessages.PreferencePage_noDescription); 221 } else { 222 descriptionText.setText(text); 223 } 224 } 225 226 229 private void clearDescription() { 230 if (descriptionText == null || descriptionText.isDisposed()) { 231 return; 232 } 233 descriptionText.setText(""); } 235 236 239 protected void performDefaults() { 240 super.performDefaults(); 241 DecoratorManager manager = WorkbenchPlugin.getDefault() 242 .getDecoratorManager(); 243 DecoratorDefinition[] definitions = manager 244 .getAllDecoratorDefinitions(); 245 for (int i = 0; i < definitions.length; i++) { 246 checkboxViewer.setChecked(definitions[i], definitions[i] 247 .getDefaultValue()); 248 } 249 } 250 251 254 public boolean performOk() { 255 if (super.performOk()) { 256 DecoratorManager manager = getDecoratorManager(); 257 manager.clearCaches(); 259 DecoratorDefinition[] definitions = manager 260 .getAllDecoratorDefinitions(); 261 for (int i = 0; i < definitions.length; i++) { 262 boolean checked = checkboxViewer.getChecked(definitions[i]); 263 definitions[i].setEnabled(checked); 264 265 } 266 manager.clearCaches(); 269 manager.updateForEnablementChange(); 270 return true; 271 } 272 return false; 273 } 274 275 278 public void init(IWorkbench workbench) { 279 } 280 281 284 private DecoratorDefinition[] getAllDefinitions() { 285 return getDecoratorManager().getAllDecoratorDefinitions(); 286 } 287 288 293 private DecoratorManager getDecoratorManager() { 294 return WorkbenchPlugin.getDefault().getDecoratorManager(); 295 } 296 297 } 298 | Popular Tags |