1 11 package org.eclipse.pde.internal.ui.parts; 12 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory; 13 import org.eclipse.swt.SWT; 14 import org.eclipse.swt.events.FocusAdapter; 15 import org.eclipse.swt.events.FocusEvent; 16 import org.eclipse.swt.events.KeyAdapter; 17 import org.eclipse.swt.events.KeyEvent; 18 import org.eclipse.swt.events.ModifyEvent; 19 import org.eclipse.swt.events.ModifyListener; 20 import org.eclipse.swt.events.SelectionAdapter; 21 import org.eclipse.swt.events.SelectionEvent; 22 import org.eclipse.swt.layout.GridData; 23 import org.eclipse.swt.layout.GridLayout; 24 import org.eclipse.swt.widgets.Button; 25 import org.eclipse.swt.widgets.Composite; 26 import org.eclipse.swt.widgets.Control; 27 import org.eclipse.swt.widgets.Layout; 28 import org.eclipse.swt.widgets.Text; 29 import org.eclipse.ui.forms.IFormColors; 30 import org.eclipse.ui.forms.widgets.FormToolkit; 31 import org.eclipse.ui.forms.widgets.Hyperlink; 32 import org.eclipse.ui.forms.widgets.TableWrapData; 33 import org.eclipse.ui.forms.widgets.TableWrapLayout; 34 41 public class FormEntry { 42 private Control fLabel; 43 private Text fText; 44 private Button fBrowse; 45 private String fValue=""; private boolean fDirty; 47 boolean fIgnoreModify = false; 48 private IFormEntryListener fListener; 49 50 public static final int F_DEFAULT_TEXT_WIDTH_HINT = 100; 51 52 56 public FormEntry(Composite parent, FormToolkit toolkit, String labelText, int style) { 57 createControl(parent, toolkit, labelText, style, null, false, 0, 0); 58 } 59 68 public FormEntry(Composite parent, FormToolkit toolkit, String labelText, 69 String browseText, boolean linkLabel) { 70 this(parent, toolkit, labelText, browseText, linkLabel, 0); 71 } 72 73 public FormEntry(Composite parent, FormToolkit toolkit, String labelText, 74 String browseText, boolean linkLabel, int indent) { 75 createControl(parent, toolkit, labelText, SWT.SINGLE, browseText, linkLabel, indent, 0); 76 } 77 78 public FormEntry(Composite parent, FormToolkit toolkit, String labelText, 79 int indent, int tcolspan) { 80 createControl(parent, toolkit, labelText, SWT.SINGLE, null, false, indent, tcolspan); 81 } 82 83 93 private void createControl(Composite parent, FormToolkit toolkit, 94 String labelText, int style, String browseText, boolean linkLabel, int indent, int tcolspan) { 95 if (linkLabel) { 96 Hyperlink link = toolkit.createHyperlink(parent, labelText, 97 SWT.NULL); 98 fLabel = link; 99 } else { 100 if (labelText != null) { 101 fLabel = toolkit.createLabel(parent, labelText); 102 fLabel.setForeground(toolkit.getColors().getColor(IFormColors.TITLE)); 103 } 104 } 105 fText = toolkit.createText(parent, "", style); addListeners(); 107 if (browseText != null) { 108 fBrowse = toolkit.createButton(parent, browseText, SWT.PUSH); 109 fBrowse.addSelectionListener(new SelectionAdapter() { 110 public void widgetSelected(SelectionEvent e) { 111 if (fListener != null) 112 fListener.browseButtonSelected(FormEntry.this); 113 } 114 }); 115 } 116 fillIntoGrid(parent, indent, tcolspan); 117 setTextWidthHint(F_DEFAULT_TEXT_WIDTH_HINT); 120 } 121 public void setEditable(boolean editable) { 122 fText.setEditable(editable); 123 if (fLabel instanceof Hyperlink) 124 ((Hyperlink)fLabel).setUnderlined(editable); 125 126 if (fBrowse!=null) 127 fBrowse.setEnabled(editable); 128 } 129 private void fillIntoGrid(Composite parent, int indent, int tcolspan) { 130 Layout layout = parent.getLayout(); 131 int tspan; 132 if (layout instanceof GridLayout) { 133 int span = ((GridLayout) layout).numColumns; 134 if (tcolspan > 0) 135 tspan = tcolspan; 136 else 137 tspan = fBrowse != null ? span - 2 : span - 1; 138 GridData gd; 139 if (fLabel != null) { 140 gd = new GridData(GridData.VERTICAL_ALIGN_CENTER); 141 gd.horizontalIndent = indent; 142 fLabel.setLayoutData(gd); 143 } 144 gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL); 145 gd.horizontalSpan = tspan; 146 if (fLabel != null) { 147 gd.horizontalIndent = FormLayoutFactory.CONTROL_HORIZONTAL_INDENT; 148 } 149 gd.grabExcessHorizontalSpace = (tspan == 1); 150 gd.widthHint = 10; 151 fText.setLayoutData(gd); 152 if (fBrowse != null) { 153 gd = new GridData(GridData.VERTICAL_ALIGN_CENTER); 154 fBrowse.setLayoutData(gd); 155 } 156 } else if (layout instanceof TableWrapLayout) { 157 int span = ((TableWrapLayout) layout).numColumns; 158 if (tcolspan > 0) 159 tspan = tcolspan; 160 else 161 tspan = fBrowse != null ? span - 2 : span - 1; 162 TableWrapData td; 163 if (fLabel != null) { 164 td = new TableWrapData(); 165 td.valign = TableWrapData.MIDDLE; 166 td.indent = indent; 167 fLabel.setLayoutData(td); 168 } 169 td = new TableWrapData(TableWrapData.FILL); 170 td.colspan = tspan; 171 if (fLabel != null) { 172 td.indent = FormLayoutFactory.CONTROL_HORIZONTAL_INDENT; 173 } 174 td.grabHorizontal = (tspan == 1); 175 td.valign = TableWrapData.MIDDLE; 176 fText.setLayoutData(td); 177 if (fBrowse != null) { 178 td = new TableWrapData(TableWrapData.FILL); 179 td.valign = TableWrapData.MIDDLE; 180 fBrowse.setLayoutData(td); 181 } 182 } 183 } 184 189 public void setFormEntryListener(IFormEntryListener listener) { 190 if (fLabel != null && fLabel instanceof Hyperlink) { 191 if (this.fListener!=null) 192 ((Hyperlink)fLabel).removeHyperlinkListener(this.fListener); 193 if (listener!=null) 194 ((Hyperlink)fLabel).addHyperlinkListener(listener); 195 } 196 this.fListener = listener; 197 } 198 private void addListeners() { 199 fText.addKeyListener(new KeyAdapter() { 200 public void keyReleased(KeyEvent e) { 201 keyReleaseOccured(e); 202 } 203 }); 204 fText.addModifyListener(new ModifyListener() { 205 public void modifyText(ModifyEvent e) { 206 editOccured(e); 207 } 208 }); 209 fText.addFocusListener(new FocusAdapter() { 210 public void focusGained(FocusEvent e) { 211 if (fListener != null) 212 fListener.focusGained(FormEntry.this); 213 } 214 public void focusLost(FocusEvent e) { 215 if (fDirty) 216 commit(); 217 } 218 }); 219 } 220 225 public void commit() { 226 if (fDirty) { 227 fValue = fText.getText(); 228 if (fListener != null) 232 fListener.textValueChanged(this); 233 } 234 fDirty = false; 235 } 236 public void cancelEdit() { 237 fDirty = false; 238 } 239 private void editOccured(ModifyEvent e) { 240 if (fIgnoreModify) 241 return; 242 fDirty = true; 243 if (fListener != null) 244 fListener.textDirty(this); 245 } 246 251 public Text getText() { 252 return fText; 253 } 254 255 public Control getLabel() { 256 return fLabel; 257 } 258 259 263 public Button getButton() { 264 return fBrowse; 265 } 266 272 public String getValue() { 273 return fValue.trim(); 274 } 275 280 public boolean isDirty() { 281 return fDirty; 282 } 283 private void keyReleaseOccured(KeyEvent e) { 284 if (e.character == '\r') { 285 if (fDirty) 287 commit(); 288 } else if (e.character == '\u001b') { if (!fValue.equals(fText.getText())) 290 fText.setText(fValue != null ? fValue : ""); fDirty = false; 292 } 293 if (fListener != null) 294 fListener.selectionChanged(FormEntry.this); 295 } 296 301 public void setValue(String value) { 302 if (fText != null) 303 fText.setText(value != null ? value : ""); this.fValue = (value != null) ? value : ""; } 306 313 public void setValue(String value, boolean blockNotification) { 314 fIgnoreModify = blockNotification; 315 setValue(value); 316 fIgnoreModify = false; 317 } 318 319 public void setVisible(boolean visible) { 320 if (fLabel != null) 321 fLabel.setVisible(visible); 322 if (fText != null) 323 fText.setVisible(visible); 324 if (fBrowse != null) 325 fBrowse.setVisible(visible); 326 } 327 328 334 public void setTextWidthHint(int width) { 335 Object data = getText().getLayoutData(); 336 if (data == null) { 337 return; 338 } else if (data instanceof GridData) { 339 ((GridData)data).widthHint = width; 340 } else if (data instanceof TableWrapData) { 341 ((TableWrapData)data).maxWidth = width; 342 } 343 } 344 } 345 | Popular Tags |