KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > PDEFormPage


1 /*******************************************************************************
2  * Copyright (c) 2003, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.editor;
12 import java.io.PrintWriter JavaDoc;
13 import java.io.StringWriter JavaDoc;
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 JavaDoc id, String JavaDoc title) {
67         super(editor, id, title);
68         fLastFocusControl = null;
69     }
70
71     public PDEFormPage(FormEditor editor, String JavaDoc id, String JavaDoc 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         //FormColors colors = toolkit.getColors();
103
//form.getForm().setSeparatorColor(colors.getColor(FormColors.TB_BORDER));
104
if (fNewStyleHeader) {
105             //createNewStyleHeader(form, colors);
106
toolkit.decorateFormHeading(form.getForm());
107         }
108         final String JavaDoc href = getHelpResource();
109         if (href != null) {
110             IToolBarManager manager = form.getToolBarManager();
111             Action helpAction = new Action("help") { //$NON-NLS-1$
112
public void run() {
113                     BusyIndicator.showWhile(form.getDisplay(), new Runnable JavaDoc() {
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         //check to see if our form parts are contributing actions
125
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 JavaDoc 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 JavaDoc 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 JavaDoc data = parent.getData("part"); //$NON-NLS-1$
204
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 JavaDoc 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(""); //$NON-NLS-1$
239
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     /* (non-Javadoc)
254      * @see org.eclipse.ui.forms.editor.FormPage#createPartControl(org.eclipse.swt.widgets.Composite)
255      */

256     public void createPartControl(Composite parent) {
257         super.createPartControl(parent);
258         // Dynamically add focus listeners to all the forms children in order
259
// to track the last focus control
260
IManagedForm managedForm = getManagedForm();
261         if (managedForm != null) {
262             addLastFocusListeners(managedForm.getForm());
263         }
264     }
265     
266     /**
267      * Programatically and recursively add focus listeners to the specified
268      * composite and its children that track the last control to have focus
269      * before a page change or the editor lost focus
270      *
271      * @param composite
272      */

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             // Add a focus listener if the control is any one of the below types
278
// Note that the controls listed below represent all the controls
279
// currently in use by all form pages in PDE. In the future,
280
// more controls will have to be added.
281
// Could not add super class categories of controls because it
282
// would include things like tool bars that we don't want to track
283
// focus for.
284
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                 // Recursively add focus listeners to this composites children
302
addLastFocusListeners((Composite)control);
303             }
304         }
305     }
306     
307     /**
308      * Add a focus listener to the specified control that tracks the last
309      * control to have focus on this page.
310      * When focus is gained by this control, it registers itself as the last
311      * control to have focus. The last control to have focus is stored in order
312      * to be restored after a page change or editor loses focus.
313      *
314      * @param control
315      */

316     private void addLastFocusListener(final Control control) {
317         control.addFocusListener(new FocusListener() {
318             public void focusGained(FocusEvent e) {
319                 // NO-OP
320
}
321             public void focusLost(FocusEvent e) {
322                 fLastFocusControl = control;
323             }
324         });
325     }
326     
327     /**
328      * Set the focus on the last control to have focus before a page change
329      * or the editor lost focus.
330      */

331     public void updateFormSelection() {
332         if ((fLastFocusControl != null) &&
333                 (fLastFocusControl.isDisposed() == false)) {
334             // Set focus on the control
335
fLastFocusControl.setFocus();
336             // If the control is a Text widget, select its contents
337
if (fLastFocusControl instanceof Text) {
338                 Text text = (Text)fLastFocusControl;
339                 text.setSelection(0, text.getText().length());
340             }
341         } else {
342             // No focus control set
343
// Fallback on managed form selection mechanism by setting the
344
// focus on this page itself.
345
// The managed form will set focus on the first managed part.
346
// Most likely this will turn out to be a section.
347
// In order for this to work properly, we must override the
348
// sections setFocus() method and set focus on a child control
349
// (preferrably first) that can practically take focus.
350
setFocus();
351         }
352     }
353     
354     /**
355      * @return
356      */

357     public Control getLastFocusControl() {
358         return fLastFocusControl;
359     }
360     
361     /**
362      * @param control
363      */

364     public void setLastFocusControl(Control control) {
365         fLastFocusControl = control;
366     }
367     
368     /**
369      * @param managedForm
370      * @param errorTitle
371      * @param errorMessage
372      */

373     protected void createFormErrorContent(IManagedForm managedForm,
374             String JavaDoc errorTitle, String JavaDoc errorMessage) {
375         createFormErrorContent(managedForm, errorTitle, errorMessage, null);
376     }
377     
378     /**
379      * @param managedForm
380      * @param errorTitle
381      * @param errorMessage
382      * @param e
383      */

384     protected void createFormErrorContent(IManagedForm managedForm,
385             String JavaDoc errorTitle, String JavaDoc errorMessage, Exception JavaDoc e) {
386         
387         ScrolledForm form = managedForm.getForm();
388         FormToolkit toolkit = managedForm.getToolkit();
389         //FormColors colors = toolkit.getColors();
390
//form.getForm().setSeparatorColor(colors.getColor(FormColors.TB_BORDER));
391
if (fNewStyleHeader) {
392             //createNewStyleHeader(form, colors);
393
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         // Set the title and image of the form
404
form.setText(errorTitle);
405         form.setImage(JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_ERROR));
406         
407         int sectionStyle = Section.DESCRIPTION | ExpandableComposite.TITLE_BAR;
408         // Create the message section
409
Section messageSection = createUISection(parent, PDEUIMessages.PDEFormPage_titleMessage,
410                 errorMessage, sectionStyle);
411         Composite messageClient = createUISectionContainer(messageSection, 1);
412         // Bind the widgets
413
toolkit.paintBordersFor(messageClient);
414         messageSection.setClient(messageClient);
415         // Ensure the exception was defined
416
if (e == null) {
417             return;
418         }
419         // Create the details section
420
Section detailsSection = createUISection(parent, PDEUIMessages.PDEFormPage_titleDetails,
421                 e.getMessage(), sectionStyle);
422         Composite detailsClient = createUISectionContainer(detailsSection, 1);
423         // Create text widget holding the exception trace
424
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         // Bind the widgets
431
toolkit.paintBordersFor(detailsClient);
432         detailsSection.setClient(detailsClient);
433         // Note: The veritical scrollbar fails to appear when text widget is
434
// not entirely shown
435
}
436
437     /**
438      * @param parent
439      * @param text
440      * @param description
441      * @param style
442      * @return
443      */

444     public Section createUISection(Composite parent, String JavaDoc text,
445             String JavaDoc 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     /**
458      * @param parent
459      * @param columns
460      * @return
461      */

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     /**
469      * @param throwable
470      * @return
471      */

472     public String JavaDoc getStackTrace(Throwable JavaDoc throwable) {
473         StringWriter JavaDoc swriter = new StringWriter JavaDoc();
474         PrintWriter JavaDoc pwriter = new PrintWriter JavaDoc(swriter);
475         throwable.printStackTrace(pwriter);
476         pwriter.flush();
477         pwriter.close();
478         return swriter.toString();
479     }
480     
481     /**
482      * Used to align the section client / decriptions of two section headers
483      * horizontally adjacent to each other. The misalignment is caused by one
484      * section header containing toolbar icons and the other not.
485      *
486      * @param masterSection
487      * @param detailsSection
488      */

489     public void alignSectionHeaders(Section masterSection,
490             Section detailsSection) {
491         detailsSection.descriptionVerticalSpacing +=
492             masterSection.getTextClientHeightDifference();
493     }
494     
495     /**
496      * @param form
497      * @param colors
498      */

499     /*
500     private void createNewStyleHeader(final ScrolledForm form, FormColors colors) {
501         colors.initializeSectionToolBarColors();
502         Color gbg = colors.getColor(IFormColors.TB_BG);
503         Color bg = colors.getBackground();
504         form.getForm().setTextBackground(new Color[]{bg, gbg}, new int [] {100}, true);
505         form.getForm().setSeparatorVisible(true);
506     }
507     */

508 }
509
Popular Tags