1 11 package org.eclipse.pde.internal.ui.editor.feature; 12 13 import java.util.StringTokenizer ; 14 import java.util.Vector ; 15 16 import org.eclipse.jface.dialogs.Dialog; 17 import org.eclipse.jface.dialogs.IDialogConstants; 18 import org.eclipse.jface.dialogs.TrayDialog; 19 import org.eclipse.jface.viewers.CheckboxTableViewer; 20 import org.eclipse.jface.viewers.ITableLabelProvider; 21 import org.eclipse.jface.viewers.LabelProvider; 22 import org.eclipse.pde.internal.ui.IHelpContextIds; 23 import org.eclipse.pde.internal.ui.PDEPlugin; 24 import org.eclipse.pde.internal.ui.PDEUIMessages; 25 import org.eclipse.pde.internal.ui.elements.DefaultTableProvider; 26 import org.eclipse.pde.internal.ui.parts.WizardCheckboxTablePart; 27 import org.eclipse.swt.SWT; 28 import org.eclipse.swt.graphics.Image; 29 import org.eclipse.swt.layout.GridData; 30 import org.eclipse.swt.layout.GridLayout; 31 import org.eclipse.swt.widgets.Composite; 32 import org.eclipse.swt.widgets.Control; 33 import org.eclipse.swt.widgets.Shell; 34 import org.eclipse.ui.PlatformUI; 35 36 public class PortabilityChoicesDialog extends TrayDialog { 37 private String value; 38 private Choice[] choices; 39 private CheckboxTableViewer choiceViewer; 40 private WizardCheckboxTablePart checkboxTablePart; 41 42 class ContentProvider extends DefaultTableProvider { 43 public Object [] getElements(Object parent) { 44 return choices; 45 } 46 } 47 48 class ChoiceLabelProvider 49 extends LabelProvider 50 implements ITableLabelProvider { 51 public String getColumnText(Object obj, int index) { 52 return ((Choice) obj).getLabel(); 53 } 54 55 public Image getColumnImage(Object obj, int index) { 56 return null; 57 } 58 } 59 60 public PortabilityChoicesDialog( 61 Shell shell, 62 Choice[] choices, 63 String value) { 64 super(shell); 65 this.value = value; 66 this.choices = choices; 67 68 checkboxTablePart = new WizardCheckboxTablePart(PDEUIMessages.FeatureEditor_PortabilityChoicesDialog_choices); 69 } 70 71 protected void createButtonsForButtonBar(Composite parent) { 72 createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true); 74 createButton( 75 parent, 76 IDialogConstants.CANCEL_ID, 77 IDialogConstants.CANCEL_LABEL, 78 false); 79 } 80 81 protected Control createDialogArea(Composite parent) { 82 Composite container = new Composite(parent, SWT.NULL); 83 GridLayout layout = new GridLayout(); 84 layout.numColumns = 2; 85 layout.marginWidth = layout.marginHeight = 9; 86 container.setLayout(layout); 87 GridData gd = new GridData(GridData.FILL_BOTH); 88 container.setLayoutData(gd); 89 90 checkboxTablePart.createControl(container); 91 choiceViewer = checkboxTablePart.getTableViewer(); 92 choiceViewer.setContentProvider(new ContentProvider()); 93 choiceViewer.setLabelProvider(new ChoiceLabelProvider()); 94 95 gd = (GridData)checkboxTablePart.getControl().getLayoutData(); 96 gd.widthHint = 300; 97 gd.heightHint = 350; 98 99 Dialog.applyDialogFont(parent); 100 initialize(); 101 PlatformUI.getWorkbench().getHelpSystem().setHelp(container, IHelpContextIds.FEATURE_PORTABILITY_WIZARD); 102 return container; 103 } 104 public String getValue() { 105 return value; 106 } 107 108 protected void initialize() { 109 choiceViewer.setInput(PDEPlugin.getDefault()); 110 111 if (value != null) { 112 Vector selected = new Vector (); 113 StringTokenizer stok = new StringTokenizer (value, ","); while (stok.hasMoreElements()) { 115 String tok = stok.nextToken(); 116 Choice choice = findChoice(tok); 117 if (choice != null) 118 selected.add(choice); 119 } 120 checkboxTablePart.setSelection(selected.toArray()); 121 } 122 else 123 checkboxTablePart.selectAll(false); 124 } 125 126 private Choice findChoice(String value) { 127 for (int i = 0; i < choices.length; i++) { 128 Choice choice = choices[i]; 129 if (choice.getValue().equalsIgnoreCase(value)) 130 return choice; 131 } 132 return null; 133 } 134 135 protected void okPressed() { 136 value = computeNewValue(); 137 super.okPressed(); 138 } 139 140 private String computeNewValue() { 141 Object [] checked = checkboxTablePart.getSelection(); 142 if (checked.length == 0) 143 return ""; StringBuffer buf = new StringBuffer (); 145 for (int i = 0; i < checked.length; i++) { 146 Choice choice = (Choice) checked[i]; 147 if (i > 0) 148 buf.append(","); buf.append(choice.getValue()); 150 } 151 return buf.toString(); 152 } 153 } 154 | Popular Tags |