1 11 package org.eclipse.pde.internal.ui.editor; 12 import java.io.PrintWriter ; 13 import java.io.StringWriter ; 14 15 import org.eclipse.core.runtime.IAdaptable; 16 import org.eclipse.jface.action.Action; 17 import org.eclipse.jface.action.IAction; 18 import org.eclipse.jface.action.IMenuManager; 19 import org.eclipse.jface.action.IToolBarManager; 20 import org.eclipse.jface.dialogs.Dialog; 21 import org.eclipse.jface.resource.JFaceResources; 22 import org.eclipse.pde.core.IBaseModel; 23 import org.eclipse.pde.internal.ui.PDEPluginImages; 24 import org.eclipse.pde.internal.ui.PDEUIMessages; 25 import org.eclipse.swt.SWT; 26 import org.eclipse.swt.custom.BusyIndicator; 27 import org.eclipse.swt.custom.CCombo; 28 import org.eclipse.swt.custom.CTabFolder; 29 import org.eclipse.swt.dnd.Clipboard; 30 import org.eclipse.swt.events.FocusEvent; 31 import org.eclipse.swt.events.FocusListener; 32 import org.eclipse.swt.layout.GridData; 33 import org.eclipse.swt.layout.GridLayout; 34 import org.eclipse.swt.widgets.Button; 35 import org.eclipse.swt.widgets.Combo; 36 import org.eclipse.swt.widgets.Composite; 37 import org.eclipse.swt.widgets.Control; 38 import org.eclipse.swt.widgets.Display; 39 import org.eclipse.swt.widgets.Link; 40 import org.eclipse.swt.widgets.List; 41 import org.eclipse.swt.widgets.Menu; 42 import org.eclipse.swt.widgets.Spinner; 43 import org.eclipse.swt.widgets.TabFolder; 44 import org.eclipse.swt.widgets.Table; 45 import org.eclipse.swt.widgets.Text; 46 import org.eclipse.swt.widgets.Tree; 47 import org.eclipse.ui.PlatformUI; 48 import org.eclipse.ui.actions.ActionFactory; 49 import org.eclipse.ui.dialogs.FilteredTree; 50 import org.eclipse.ui.forms.AbstractFormPart; 51 import org.eclipse.ui.forms.IFormPart; 52 import org.eclipse.ui.forms.IManagedForm; 53 import org.eclipse.ui.forms.editor.FormEditor; 54 import org.eclipse.ui.forms.editor.FormPage; 55 import org.eclipse.ui.forms.widgets.ExpandableComposite; 56 import org.eclipse.ui.forms.widgets.FormToolkit; 57 import org.eclipse.ui.forms.widgets.Hyperlink; 58 import org.eclipse.ui.forms.widgets.ScrolledForm; 59 import org.eclipse.ui.forms.widgets.Section; 60 61 public abstract class PDEFormPage extends FormPage { 62 63 private boolean fNewStyleHeader=true; 64 private Control fLastFocusControl; 65 66 public PDEFormPage(FormEditor editor, String id, String title) { 67 super(editor, id, title); 68 fLastFocusControl = null; 69 } 70 71 public PDEFormPage(FormEditor editor, String id, String title, boolean newStyleHeader) { 72 this(editor, id, title); 73 fNewStyleHeader = newStyleHeader; 74 } 75 76 public void dispose() { 77 Control c = getPartControl(); 78 if (c!=null && !c.isDisposed()) { 79 Menu menu = c.getMenu(); 80 if (menu!=null) 81 resetMenu(menu, c); 82 } 83 super.dispose(); 84 } 85 private void resetMenu(Menu menu, Control c) { 86 if (c instanceof Composite) { 87 Composite comp = (Composite)c; 88 Control [] children = comp.getChildren(); 89 for (int i=0; i<children.length; i++) { 90 resetMenu(menu, children[i]); 91 } 92 } 93 Menu cmenu = c.getMenu(); 94 if (cmenu!=null && cmenu.equals(menu)) { 95 c.setMenu(null); 96 } 97 } 98 99 protected void createFormContent(IManagedForm managedForm) { 100 final ScrolledForm form = managedForm.getForm(); 101 FormToolkit toolkit = managedForm.getToolkit(); 102 if (fNewStyleHeader) { 105 toolkit.decorateFormHeading(form.getForm()); 107 } 108 final String href = getHelpResource(); 109 if (href != null) { 110 IToolBarManager manager = form.getToolBarManager(); 111 Action helpAction = new Action("help") { public void run() { 113 BusyIndicator.showWhile(form.getDisplay(), new Runnable () { 114 public void run() { 115 PlatformUI.getWorkbench().getHelpSystem().displayHelpResource(href); 116 } 117 }); 118 } 119 }; 120 helpAction.setToolTipText(PDEUIMessages.PDEFormPage_help); 121 helpAction.setImageDescriptor(PDEPluginImages.DESC_HELP); 122 manager.add(helpAction); 123 } 124 IFormPart[] parts = managedForm.getParts(); 126 for(int i = 0; i < parts.length; i++) { 127 if(parts[i] instanceof IAdaptable) { 128 IAdaptable adapter = (IAdaptable) parts[i]; 129 IAction[] actions = 130 (IAction[]) adapter.getAdapter(IAction[].class); 131 if(actions != null) { 132 for(int j = 0; j < actions.length; j++) { 133 form.getToolBarManager().add(actions[j]); 134 } 135 } 136 } 137 } 138 form.updateToolBar(); 139 } 140 141 public PDEFormEditor getPDEEditor() { 142 return (PDEFormEditor) getEditor(); 143 } 144 protected String getHelpResource() { 145 return null; 146 } 147 public IBaseModel getModel() { 148 return getPDEEditor().getAggregateModel(); 149 } 150 public void contextMenuAboutToShow(IMenuManager menu) { 151 } 152 153 protected Control getFocusControl() { 154 IManagedForm form = getManagedForm(); 155 if (form == null) 156 return null; 157 Control control = form.getForm(); 158 if (control == null || control.isDisposed()) 159 return null; 160 Display display = control.getDisplay(); 161 Control focusControl = display.getFocusControl(); 162 if (focusControl == null || focusControl.isDisposed()) 163 return null; 164 return focusControl; 165 } 166 public boolean performGlobalAction(String actionId) { 167 Control focusControl = getFocusControl(); 168 if (focusControl == null) 169 return false; 170 171 if (canPerformDirectly(actionId, focusControl)) 172 return true; 173 AbstractFormPart focusPart = getFocusSection(); 174 if (focusPart!=null) { 175 if (focusPart instanceof PDESection) 176 return ((PDESection)focusPart).doGlobalAction(actionId); 177 if (focusPart instanceof PDEDetails) 178 return ((PDEDetails)focusPart).doGlobalAction(actionId); 179 } 180 return false; 181 } 182 183 public boolean canPaste(Clipboard clipboard) { 184 AbstractFormPart focusPart = getFocusSection(); 185 if (focusPart != null) { 186 if (focusPart instanceof PDESection) { 187 return ((PDESection)focusPart).canPaste(clipboard); 188 } 189 if (focusPart instanceof PDEDetails) { 190 return ((PDEDetails)focusPart).canPaste(clipboard); 191 } 192 } 193 return false; 194 } 195 196 private AbstractFormPart getFocusSection() { 197 Control focusControl = getFocusControl(); 198 if (focusControl == null) 199 return null; 200 Composite parent = focusControl.getParent(); 201 AbstractFormPart targetPart = null; 202 while (parent != null) { 203 Object data = parent.getData("part"); if (data != null && data instanceof AbstractFormPart) { 205 targetPart = (AbstractFormPart) data; 206 break; 207 } 208 parent = parent.getParent(); 209 } 210 return targetPart; 211 } 212 213 protected boolean canPerformDirectly(String id, Control control) { 214 if (control instanceof Text) { 215 Text text = (Text) control; 216 if (id.equals(ActionFactory.CUT.getId())) { 217 text.cut(); 218 return true; 219 } 220 if (id.equals(ActionFactory.COPY.getId())) { 221 text.copy(); 222 return true; 223 } 224 if (id.equals(ActionFactory.PASTE.getId())) { 225 text.paste(); 226 return true; 227 } 228 if (id.equals(ActionFactory.SELECT_ALL.getId())) { 229 text.selectAll(); 230 return true; 231 } 232 if (id.equals(ActionFactory.DELETE.getId())) { 233 int count = text.getSelectionCount(); 234 if (count == 0) { 235 int caretPos = text.getCaretPosition(); 236 text.setSelection(caretPos, caretPos + 1); 237 } 238 text.insert(""); return true; 240 } 241 } 242 return false; 243 } 244 public void cancelEdit() { 245 IFormPart [] parts = getManagedForm().getParts(); 246 for (int i=0; i<parts.length; i++) { 247 IFormPart part = parts[i]; 248 if (part instanceof IContextPart) 249 ((IContextPart)part).cancelEdit(); 250 } 251 } 252 253 256 public void createPartControl(Composite parent) { 257 super.createPartControl(parent); 258 IManagedForm managedForm = getManagedForm(); 261 if (managedForm != null) { 262 addLastFocusListeners(managedForm.getForm()); 263 } 264 } 265 266 273 public void addLastFocusListeners(Composite composite) { 274 Control[] controls = composite.getChildren(); 275 for (int i = 0; i < controls.length; i++) { 276 Control control = controls[i]; 277 if ((control instanceof Text) || 285 (control instanceof Button) || 286 (control instanceof Combo) || 287 (control instanceof CCombo) || 288 (control instanceof Tree) || 289 (control instanceof Table) || 290 (control instanceof Spinner) || 291 (control instanceof Link) || 292 (control instanceof List) || 293 (control instanceof TabFolder) || 294 (control instanceof CTabFolder) || 295 (control instanceof Hyperlink) || 296 (control instanceof FilteredTree) 297 ) { 298 addLastFocusListener(control); 299 } 300 if (control instanceof Composite) { 301 addLastFocusListeners((Composite)control); 303 } 304 } 305 } 306 307 316 private void addLastFocusListener(final Control control) { 317 control.addFocusListener(new FocusListener() { 318 public void focusGained(FocusEvent e) { 319 } 321 public void focusLost(FocusEvent e) { 322 fLastFocusControl = control; 323 } 324 }); 325 } 326 327 331 public void updateFormSelection() { 332 if ((fLastFocusControl != null) && 333 (fLastFocusControl.isDisposed() == false)) { 334 fLastFocusControl.setFocus(); 336 if (fLastFocusControl instanceof Text) { 338 Text text = (Text)fLastFocusControl; 339 text.setSelection(0, text.getText().length()); 340 } 341 } else { 342 setFocus(); 351 } 352 } 353 354 357 public Control getLastFocusControl() { 358 return fLastFocusControl; 359 } 360 361 364 public void setLastFocusControl(Control control) { 365 fLastFocusControl = control; 366 } 367 368 373 protected void createFormErrorContent(IManagedForm managedForm, 374 String errorTitle, String errorMessage) { 375 createFormErrorContent(managedForm, errorTitle, errorMessage, null); 376 } 377 378 384 protected void createFormErrorContent(IManagedForm managedForm, 385 String errorTitle, String errorMessage, Exception e) { 386 387 ScrolledForm form = managedForm.getForm(); 388 FormToolkit toolkit = managedForm.getToolkit(); 389 if (fNewStyleHeader) { 392 toolkit.decorateFormHeading(form.getForm()); 394 } 395 396 Composite parent = form.getBody(); 397 GridLayout layout = new GridLayout(); 398 GridData data2 = new GridData(GridData.FILL_BOTH); 399 layout.marginWidth = 7; 400 layout.marginHeight = 7; 401 parent.setLayout(layout); 402 parent.setLayoutData(data2); 403 form.setText(errorTitle); 405 form.setImage(JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_ERROR)); 406 407 int sectionStyle = Section.DESCRIPTION | ExpandableComposite.TITLE_BAR; 408 Section messageSection = createUISection(parent, PDEUIMessages.PDEFormPage_titleMessage, 410 errorMessage, sectionStyle); 411 Composite messageClient = createUISectionContainer(messageSection, 1); 412 toolkit.paintBordersFor(messageClient); 414 messageSection.setClient(messageClient); 415 if (e == null) { 417 return; 418 } 419 Section detailsSection = createUISection(parent, PDEUIMessages.PDEFormPage_titleDetails, 421 e.getMessage(), sectionStyle); 422 Composite detailsClient = createUISectionContainer(detailsSection, 1); 423 int style = SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL | SWT.READ_ONLY; 425 Text text = toolkit.createText(detailsClient, getStackTrace(e), style); 426 GridData data = new GridData(GridData.FILL_HORIZONTAL); 427 data.heightHint = 160; 428 data.widthHint = 200; 429 text.setLayoutData(data); 430 toolkit.paintBordersFor(detailsClient); 432 detailsSection.setClient(detailsClient); 433 } 436 437 444 public Section createUISection(Composite parent, String text, 445 String description, int style) { 446 Section section = 447 getManagedForm().getToolkit().createSection(parent, style); 448 section.clientVerticalSpacing = FormLayoutFactory.SECTION_HEADER_VERTICAL_SPACING; 449 section.setLayout(FormLayoutFactory.createClearGridLayout(false, 1)); 450 section.setText(text); 451 section.setDescription(description); 452 GridData data = new GridData(GridData.FILL_HORIZONTAL); 453 section.setLayoutData(data); 454 return section; 455 } 456 457 462 public Composite createUISectionContainer(Composite parent, int columns) { 463 Composite container = getManagedForm().getToolkit().createComposite(parent); 464 container.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, columns)); 465 return container; 466 } 467 468 472 public String getStackTrace(Throwable throwable) { 473 StringWriter swriter = new StringWriter (); 474 PrintWriter pwriter = new PrintWriter (swriter); 475 throwable.printStackTrace(pwriter); 476 pwriter.flush(); 477 pwriter.close(); 478 return swriter.toString(); 479 } 480 481 489 public void alignSectionHeaders(Section masterSection, 490 Section detailsSection) { 491 detailsSection.descriptionVerticalSpacing += 492 masterSection.getTextClientHeightDifference(); 493 } 494 495 499 508 } 509 | Popular Tags |