1 15 16 package org.eclipse.ui.internal.net; 17 18 import java.util.Arrays ; 19 import java.util.Iterator ; 20 import java.util.TreeSet ; 21 import org.eclipse.jface.dialogs.InputDialog; 22 import org.eclipse.jface.fieldassist.ControlDecoration; 23 import org.eclipse.jface.fieldassist.FieldDecorationRegistry; 24 import org.eclipse.jface.viewers.*; 25 import org.eclipse.jface.window.Window; 26 import org.eclipse.swt.SWT; 27 import org.eclipse.swt.events.SelectionAdapter; 28 import org.eclipse.swt.events.SelectionEvent; 29 import org.eclipse.swt.layout.GridData; 30 import org.eclipse.swt.layout.GridLayout; 31 import org.eclipse.swt.widgets.*; 32 33 37 public class NonProxyHostsComposite extends Composite { 38 private Table table_; 39 TableViewer tableViewer_; 40 private TreeSet tableValues_; 41 private Button add_; 42 private Button edit_; 43 private Button remove_; 44 45 public NonProxyHostsComposite(Composite parent, int style) { 46 super(parent, style); 47 createWidgets(); 48 } 49 50 public void enableComposite(boolean enabled) { 51 table_.setEnabled(enabled); 52 add_.setEnabled(enabled); 53 edit_.setEnabled(enabled); 54 remove_.setEnabled(enabled); 55 } 56 57 protected void createWidgets() { 58 GridLayout layout = new GridLayout(); 59 layout.horizontalSpacing = 6; 60 layout.verticalSpacing = 6; 61 layout.marginWidth = 0; 62 layout.marginHeight = 0; 63 layout.numColumns = 2; 64 setLayout(layout); 65 66 table_ = new Table(this, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL 67 | SWT.MULTI | SWT.FULL_SELECTION); 68 GridData data = new GridData(GridData.FILL_BOTH 69 | GridData.VERTICAL_ALIGN_FILL); 70 71 table_.setLayoutData(data); 72 table_.setHeaderVisible(false); 73 table_.setLinesVisible(true); 74 75 TableLayout tableLayout = new TableLayout(); 76 77 new TableColumn(table_, SWT.NONE); 78 ColumnWeightData colData = new ColumnWeightData(100, 60, false); 79 tableLayout.addColumnData(colData); 80 81 table_.setLayout(tableLayout); 82 83 tableViewer_ = new TableViewer(table_); 84 tableViewer_.setContentProvider(new NonProxyHostsContentProvider()); 85 tableViewer_.setLabelProvider(new NonProxyHostsLabelProvider()); 86 87 tableViewer_ 88 .addSelectionChangedListener(new ISelectionChangedListener() { 89 public void selectionChanged(SelectionChangedEvent event) { 90 enableButtons(); 91 } 92 }); 93 94 tableViewer_.addDoubleClickListener(new IDoubleClickListener() { 95 public void doubleClick(DoubleClickEvent event) { 96 editSelection(); 97 } 98 }); 99 100 Composite buttonComp = new Composite(this, SWT.NONE); 101 layout = new GridLayout(); 102 layout.horizontalSpacing = 0; 103 layout.verticalSpacing = 8; 104 layout.marginWidth = 0; 105 layout.marginHeight = 0; 106 layout.numColumns = 1; 107 buttonComp.setLayout(layout); 108 data = new GridData(GridData.HORIZONTAL_ALIGN_END 109 | GridData.VERTICAL_ALIGN_FILL); 110 buttonComp.setLayoutData(data); 111 112 add_ = createButton(buttonComp, NetUIMessages.BUTTON_PREFERENCE_ADD); 113 114 add_.addSelectionListener(new SelectionAdapter() { 115 public void widgetSelected(SelectionEvent e) { 116 addHost(); 117 } 118 }); 119 120 edit_ = createButton(buttonComp, NetUIMessages.BUTTON_PREFERENCE_EDIT); 121 122 edit_.addSelectionListener(new SelectionAdapter() { 123 public void widgetSelected(SelectionEvent e) { 124 editSelection(); 125 } 126 }); 127 edit_.setEnabled(false); 128 129 remove_ = createButton(buttonComp, 130 NetUIMessages.BUTTON_PREFERENCE_REMOVE); 131 132 remove_.addSelectionListener(new SelectionAdapter() { 133 public void widgetSelected(SelectionEvent e) { 134 removeFromList((IStructuredSelection) tableViewer_ 135 .getSelection()); 136 tableViewer_.refresh(); 137 } 138 }); 139 remove_.setEnabled(false); 140 } 141 142 private Button createButton(Composite comp, String label) { 143 Button button = new Button(comp, SWT.PUSH); 144 button.setText(label); 145 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL 146 | GridData.VERTICAL_ALIGN_BEGINNING); 147 button.setLayoutData(data); 148 return button; 149 } 150 151 154 public void setEnabled(boolean enabled) { 155 super.setEnabled(enabled); 156 enableButtons(); 157 } 158 159 public void setList(String [] hosts) { 160 tableValues_ = new TreeSet (Arrays.asList(hosts)); 161 162 tableViewer_.setInput(tableValues_); 163 tableViewer_.refresh(); 164 } 165 166 public String [] getList() { 167 return (String []) tableValues_.toArray(new String [0]); 168 } 169 170 String getStringList(Iterator iterator) { 171 StringBuffer buffer = new StringBuffer (); 172 173 if (iterator.hasNext()) { 174 buffer.append((String ) iterator.next()); 175 } 176 177 while (iterator.hasNext()) { 178 buffer.append(','); 179 buffer.append((String ) iterator.next()); 180 } 181 182 return buffer.toString(); 183 } 184 185 void removeFromList(IStructuredSelection selection) { 186 tableValues_.removeAll(selection.toList()); 187 } 188 189 void updateList(String value) { 190 String [] hosts = value.split("\\|| |,"); 194 tableValues_.addAll(Arrays.asList(hosts)); 195 tableValues_.remove(""); tableViewer_.refresh(); 197 } 198 199 void enableButtons() { 200 boolean enabled = getEnabled(); 201 202 if (enabled) { 203 boolean itemsSelected = !tableViewer_.getSelection().isEmpty(); 204 205 add_.setEnabled(true); 206 edit_.setEnabled(itemsSelected); 207 remove_.setEnabled(itemsSelected); 208 } else { 209 add_.setEnabled(false); 210 edit_.setEnabled(false); 211 remove_.setEnabled(false); 212 } 213 } 214 215 void editSelection() { 216 IStructuredSelection selection = (IStructuredSelection) tableViewer_.getSelection(); 217 String selectedHosts = getStringList(selection.iterator()); 218 String value = promptForHost(selectedHosts); 219 if (value != null) { 220 removeFromList(selection); 221 updateList(value); 222 } 223 } 224 225 void addHost() { 226 String value = promptForHost(null); 227 if (value != null) { 228 updateList(value); 229 } 230 } 231 232 private String promptForHost(String selectedHosts) { 233 InputDialog dialog = new InputDialog(getShell(), 234 NetUIMessages.TITLE_PREFERENCE_HOSTS_DIALOG, 235 NetUIMessages.LABEL_PREFERENCE_HOSTS_DIALOG, selectedHosts, 236 null) { 237 private ControlDecoration decorator; 238 protected Control createDialogArea(Composite parent) { 239 Control createDialogArea = super.createDialogArea(parent); 240 decorator = new ControlDecoration(getText(), SWT.TOP | SWT.LEFT); 241 decorator.setDescriptionText(NetUIMessages.NonProxyHostsComposite_0); 242 decorator.setImage(FieldDecorationRegistry.getDefault().getFieldDecoration( 243 FieldDecorationRegistry.DEC_INFORMATION).getImage()); 244 return createDialogArea; 245 } 246 public boolean close() { 247 decorator.dispose(); 248 return super.close(); 249 } 250 }; 251 int result = dialog.open(); 252 String value; 253 if (result != Window.CANCEL) { 254 value = dialog.getValue(); 255 } else { 256 value = null; 257 } 258 return value; 259 } 260 } 261 | Popular Tags |