1 11 package org.eclipse.pde.internal.ui.parts; 12 import org.eclipse.swt.SWT; 13 import org.eclipse.swt.events.SelectionEvent; 14 import org.eclipse.swt.events.SelectionListener; 15 import org.eclipse.swt.layout.GridData; 16 import org.eclipse.swt.layout.GridLayout; 17 import org.eclipse.swt.widgets.Button; 18 import org.eclipse.swt.widgets.Composite; 19 import org.eclipse.ui.forms.widgets.FormToolkit; 20 21 22 public abstract class SharedPartWithButtons extends SharedPart { 23 private String [] fButtonLabels; 24 private Button[] fButtons; 25 protected Composite fButtonContainer; 26 private class SelectionHandler implements SelectionListener { 27 public void widgetSelected(SelectionEvent e) { 28 buttonSelected(e); 29 } 30 public void widgetDefaultSelected(SelectionEvent e) { 31 buttonSelected(e); 32 } 33 private void buttonSelected(SelectionEvent e) { 34 Integer index = (Integer ) e.widget.getData(); 35 SharedPartWithButtons.this.buttonSelected((Button) e.widget, index 36 .intValue()); 37 } 38 } 39 public SharedPartWithButtons(String [] buttonLabels) { 40 fButtonLabels = buttonLabels; 41 } 42 public void setButtonEnabled(int index, boolean enabled) { 43 if (fButtons != null && index >= 0 && fButtons.length > index) { 44 fButtons[index].setEnabled(enabled); 45 } 46 } 47 protected abstract void createMainControl(Composite parent, int style, 48 int span, FormToolkit toolkit); 49 protected abstract void buttonSelected(Button button, int index); 50 53 public void createControl(Composite parent, int style, int span, 54 FormToolkit toolkit) { 55 createMainLabel(parent, span, toolkit); 56 createMainControl(parent, style, span - 1, toolkit); 57 createButtons(parent, toolkit); 58 } 59 protected void createButtons(Composite parent, FormToolkit toolkit) { 60 if (fButtonLabels != null && fButtonLabels.length > 0) { 61 fButtonContainer = createComposite(parent, toolkit); 62 GridData gd = new GridData(GridData.FILL_VERTICAL); 63 fButtonContainer.setLayoutData(gd); 64 fButtonContainer.setLayout(createButtonsLayout()); 65 fButtons = new Button[fButtonLabels.length]; 66 SelectionHandler listener = new SelectionHandler(); 67 for (int i = 0; i < fButtonLabels.length; i++) { 68 String label = fButtonLabels[i]; 69 if (label != null) { 70 Button button = createButton(fButtonContainer, label, i, 71 toolkit); 72 button.addSelectionListener(listener); 73 fButtons[i] = button; 74 } else { 75 createEmptySpace(fButtonContainer, 1, toolkit); 76 } 77 } 78 } 79 } 80 protected GridLayout createButtonsLayout() { 81 GridLayout layout = new GridLayout(); 82 layout.marginWidth = layout.marginHeight = 0; 83 return layout; 84 } 85 protected Button createButton(Composite parent, String label, int index, 86 FormToolkit toolkit) { 87 Button button; 88 if (toolkit != null) 89 button = toolkit.createButton(parent, label, SWT.PUSH); 90 else { 91 button = new Button(parent, SWT.PUSH); 92 button.setText(label); 93 } 94 GridData gd = new GridData(GridData.FILL_HORIZONTAL 95 | GridData.VERTICAL_ALIGN_BEGINNING); 96 button.setLayoutData(gd); 97 button.setData(new Integer (index)); 98 return button; 99 } 100 protected void updateEnabledState() { 101 for (int i = 0; i < fButtons.length; i++) { 102 fButtons[i].setEnabled(isEnabled()); 103 } 104 } 105 protected void createMainLabel(Composite parent, int span, 106 FormToolkit toolkit) { 107 } 108 109 113 public Button getButton(int index) { 114 if ((fButtons == null) || 116 (index < 0) || 117 (index >= fButtons.length)) { 118 return null; 119 } 120 return fButtons[index]; 122 } 123 } | Popular Tags |