KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > forms > widgets > FormHeading


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.ui.internal.forms.widgets;
12
13 import java.util.Hashtable JavaDoc;
14
15 import org.eclipse.core.runtime.Assert;
16 import org.eclipse.core.runtime.ListenerList;
17 import org.eclipse.jface.action.IMenuManager;
18 import org.eclipse.jface.action.IToolBarManager;
19 import org.eclipse.jface.action.ToolBarManager;
20 import org.eclipse.jface.dialogs.Dialog;
21 import org.eclipse.jface.dialogs.IMessageProvider;
22 import org.eclipse.jface.resource.JFaceResources;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.custom.CLabel;
25 import org.eclipse.swt.dnd.DragSourceListener;
26 import org.eclipse.swt.dnd.DropTargetListener;
27 import org.eclipse.swt.dnd.Transfer;
28 import org.eclipse.swt.events.DisposeEvent;
29 import org.eclipse.swt.events.DisposeListener;
30 import org.eclipse.swt.events.MouseEvent;
31 import org.eclipse.swt.events.MouseMoveListener;
32 import org.eclipse.swt.events.MouseTrackListener;
33 import org.eclipse.swt.graphics.Color;
34 import org.eclipse.swt.graphics.Font;
35 import org.eclipse.swt.graphics.FontMetrics;
36 import org.eclipse.swt.graphics.GC;
37 import org.eclipse.swt.graphics.Image;
38 import org.eclipse.swt.graphics.Point;
39 import org.eclipse.swt.graphics.Rectangle;
40 import org.eclipse.swt.widgets.Canvas;
41 import org.eclipse.swt.widgets.Composite;
42 import org.eclipse.swt.widgets.Control;
43 import org.eclipse.swt.widgets.Event;
44 import org.eclipse.swt.widgets.Layout;
45 import org.eclipse.swt.widgets.Listener;
46 import org.eclipse.swt.widgets.ToolBar;
47 import org.eclipse.ui.forms.IFormColors;
48 import org.eclipse.ui.forms.IMessage;
49 import org.eclipse.ui.forms.events.IHyperlinkListener;
50 import org.eclipse.ui.forms.widgets.Hyperlink;
51 import org.eclipse.ui.forms.widgets.ILayoutExtension;
52 import org.eclipse.ui.forms.widgets.SizeCache;
53 import org.eclipse.ui.internal.forms.IMessageToolTipManager;
54 import org.eclipse.ui.internal.forms.MessageManager;
55
56 /**
57  * Form header moved out of the form class.
58  */

59 public class FormHeading extends Canvas {
60     private static final int TITLE_HMARGIN = 1;
61     private static final int SPACING = 5;
62     private static final int VSPACING = 5;
63     private static final int HMARGIN = 6;
64     private static final int VMARGIN = 1;
65     private static final int CLIENT_MARGIN = 1;
66
67     private static final int SEPARATOR = 1 << 1;
68     private static final int BOTTOM_TOOLBAR = 1 << 2;
69     private static final int BACKGROUND_IMAGE_TILED = 1 << 3;
70     private static final int SEPARATOR_HEIGHT = 2;
71     private static final int MESSAGE_AREA_LIMIT = 50;
72     static IMessage[] NULL_MESSAGE_ARRAY = new IMessage[] {};
73
74     public static final String JavaDoc COLOR_BASE_BG = "baseBg"; //$NON-NLS-1$
75

76     private Image backgroundImage;
77
78     private Image gradientImage;
79
80     Hashtable JavaDoc colors = new Hashtable JavaDoc();
81
82     private int flags;
83
84     private GradientInfo gradientInfo;
85
86     private ToolBarManager toolBarManager;
87
88     private SizeCache toolbarCache = new SizeCache();
89
90     private SizeCache clientCache = new SizeCache();
91
92     private SizeCache messageCache = new SizeCache();
93
94     private TitleRegion titleRegion;
95
96     private MessageRegion messageRegion;
97
98     private IMessageToolTipManager messageToolTipManager = new DefaultMessageToolTipManager();
99
100     private Control headClient;
101
102     private class DefaultMessageToolTipManager implements
103             IMessageToolTipManager {
104         public void createToolTip(Control control, boolean imageLabel) {
105         }
106
107         public void update() {
108             String JavaDoc details = getMessageType() == 0 ? null : MessageManager
109                     .createDetails(getChildrenMessages());
110             if (messageRegion != null)
111                 messageRegion.updateToolTip(details);
112             if (getMessageType() > 0
113                     && (details == null || details.length() == 0))
114                 details = getMessage();
115             titleRegion.updateToolTip(details);
116         }
117     }
118
119     private class GradientInfo {
120         Color[] gradientColors;
121
122         int[] percents;
123
124         boolean vertical;
125     }
126
127     private class FormHeadingLayout extends Layout implements ILayoutExtension {
128         public int computeMinimumWidth(Composite composite, boolean flushCache) {
129             return computeSize(composite, 5, SWT.DEFAULT, flushCache).x;
130         }
131
132         public int computeMaximumWidth(Composite composite, boolean flushCache) {
133             return computeSize(composite, SWT.DEFAULT, SWT.DEFAULT, flushCache).x;
134         }
135
136         public Point computeSize(Composite composite, int wHint, int hHint,
137                 boolean flushCache) {
138             return layout(composite, false, 0, 0, wHint, hHint, flushCache);
139         }
140
141         protected void layout(Composite composite, boolean flushCache) {
142             Rectangle rect = composite.getClientArea();
143             layout(composite, true, rect.x, rect.y, rect.width, rect.height,
144                     flushCache);
145         }
146
147         private Point layout(Composite composite, boolean move, int x, int y,
148                 int width, int height, boolean flushCache) {
149             Point tsize = null;
150             Point msize = null;
151             Point tbsize = null;
152             Point clsize = null;
153
154             if (flushCache) {
155                 clientCache.flush();
156                 messageCache.flush();
157                 toolbarCache.flush();
158             }
159             if (hasToolBar()) {
160                 ToolBar tb = toolBarManager.getControl();
161                 toolbarCache.setControl(tb);
162                 tbsize = toolbarCache.computeSize(SWT.DEFAULT, SWT.DEFAULT);
163             }
164             if (headClient != null) {
165                 clientCache.setControl(headClient);
166                 int cwhint = width;
167                 if (cwhint != SWT.DEFAULT) {
168                     cwhint -= HMARGIN * 2;
169                     if (tbsize != null && getToolBarAlignment() == SWT.BOTTOM)
170                         cwhint -= tbsize.x + SPACING;
171                 }
172                 clsize = clientCache.computeSize(cwhint, SWT.DEFAULT);
173             }
174             int totalFlexWidth = width;
175             int flexWidth = totalFlexWidth;
176             if (totalFlexWidth != SWT.DEFAULT) {
177                 totalFlexWidth -= TITLE_HMARGIN * 2;
178                 // complete right margin
179
if (hasToolBar() && getToolBarAlignment() == SWT.TOP
180                         || hasMessageRegion())
181                     totalFlexWidth -= SPACING;
182                 // subtract tool bar
183
if (hasToolBar() && getToolBarAlignment() == SWT.TOP)
184                     totalFlexWidth -= tbsize.x + SPACING;
185                 flexWidth = totalFlexWidth;
186                 if (hasMessageRegion()) {
187                     // remove message region spacing and divide by 2
188
flexWidth -= SPACING;
189                     // flexWidth /= 2;
190
}
191             }
192             /*
193              * // compute text and message sizes tsize =
194              * titleRegion.computeSize(flexWidth, SWT.DEFAULT); if (flexWidth !=
195              * SWT.DEFAULT && tsize.x < flexWidth) flexWidth += flexWidth -
196              * tsize.x;
197              *
198              * if (hasMessageRegion()) {
199              * messageCache.setControl(messageRegion.getMessageControl()); msize =
200              * messageCache.computeSize(flexWidth, SWT.DEFAULT); int maxWidth =
201              * messageCache.computeSize(SWT.DEFAULT, SWT.DEFAULT).x; if
202              * (maxWidth < msize.x) { msize.x = maxWidth; // recompute title
203              * with the reclaimed width int tflexWidth = totalFlexWidth -
204              * SPACING - msize.x; tsize = titleRegion.computeSize(tflexWidth,
205              * SWT.DEFAULT); } }
206              */

207             if (!hasMessageRegion()) {
208                 tsize = titleRegion.computeSize(flexWidth, SWT.DEFAULT);
209             } else {
210                 // Total flexible area in the first row is flexWidth.
211
// Try natural widths of title and
212
Point tsizeNatural = titleRegion.computeSize(SWT.DEFAULT,
213                         SWT.DEFAULT);
214                 messageCache.setControl(messageRegion.getMessageControl());
215                 Point msizeNatural = messageCache.computeSize(SWT.DEFAULT,
216                         SWT.DEFAULT);
217                 // try to fit all
218
tsize = tsizeNatural;
219                 msize = msizeNatural;
220                 if (flexWidth != SWT.DEFAULT) {
221                     int needed = tsizeNatural.x + msizeNatural.x;
222                     if (needed > flexWidth) {
223                         // too big - try to limit the message
224
int mwidth = flexWidth - tsizeNatural.x;
225                         if (mwidth >= MESSAGE_AREA_LIMIT) {
226                             msize.x = mwidth;
227                         } else {
228                             // message is squeezed to the limit
229
int flex = flexWidth - MESSAGE_AREA_LIMIT;
230                             tsize = titleRegion.computeSize(flex, SWT.DEFAULT);
231                             msize.x = MESSAGE_AREA_LIMIT;
232                         }
233                     }
234                 }
235             }
236
237             Point size = new Point(width, height);
238             if (!move) {
239                 // compute sizes
240
int width1 = 2 * TITLE_HMARGIN;
241                 width1 += tsize.x;
242                 if (msize != null)
243                     width1 += SPACING + msize.x;
244                 if (tbsize != null && getToolBarAlignment() == SWT.TOP)
245                     width1 += SPACING + tbsize.x;
246                 if (msize != null
247                         || (tbsize != null && getToolBarAlignment() == SWT.TOP))
248                     width1 += SPACING;
249                 size.x = width1;
250                 if (clsize != null) {
251                     int width2 = clsize.x;
252                     if (tbsize != null && getToolBarAlignment() == SWT.BOTTOM)
253                         width2 += SPACING + tbsize.x;
254                     width2 += 2 * HMARGIN;
255                     size.x = Math.max(width1, width2);
256                 }
257                 // height, first row
258
size.y = tsize.y;
259                 if (msize != null)
260                     size.y = Math.max(msize.y, size.y);
261                 if (tbsize != null && getToolBarAlignment() == SWT.TOP)
262                     size.y = Math.max(tbsize.y, size.y);
263                 if (size.y > 0)
264                     size.y += VMARGIN * 2;
265                 // add second row
266
int height2 = 0;
267                 if (tbsize != null && getToolBarAlignment() == SWT.BOTTOM)
268                     height2 = tbsize.y;
269                 if (clsize != null)
270                     height2 = Math.max(height2, clsize.y);
271                 if (height2 > 0)
272                     size.y += VSPACING + height2 + CLIENT_MARGIN;
273                 // add separator
274
if (size.y > 0 && isSeparatorVisible())
275                     size.y += SEPARATOR_HEIGHT;
276             } else {
277                 // position controls
278
int xloc = x;
279                 int yloc = y + VMARGIN;
280                 int row1Height = tsize.y;
281                 if (hasMessageRegion())
282                     row1Height = Math.max(row1Height, msize.y);
283                 if (hasToolBar() && getToolBarAlignment() == SWT.TOP)
284                     row1Height = Math.max(row1Height, tbsize.y);
285                 titleRegion.setBounds(xloc,
286                 // yloc + row1Height / 2 - tsize.y / 2,
287
yloc, tsize.x, tsize.y);
288                 xloc += tsize.x;
289
290                 if (hasMessageRegion()) {
291                     xloc += SPACING;
292                     int messageOffset = 0;
293                     if (tsize.y > 0) {
294                         // space between title area and title text
295
int titleLeadingSpace = (tsize.y - titleRegion.getFontHeight()) / 2;
296                         // space between message control and message text
297
int messageLeadingSpace = (msize.y - messageRegion.getFontHeight()) / 2;
298                         // how much to offset the message so baselines align
299
messageOffset = (titleLeadingSpace + titleRegion.getFontBaselineHeight())
300                             - (messageLeadingSpace + messageRegion.getFontBaselineHeight());
301                     }
302
303                     messageRegion
304                             .getMessageControl()
305                             .setBounds(
306                                     xloc,
307                                     tsize.y > 0 ? (yloc + messageOffset)
308                                             : (yloc + row1Height / 2 - msize.y / 2),
309                                     msize.x, msize.y);
310                     xloc += msize.x;
311                 }
312                 if (toolBarManager != null)
313                     toolBarManager.getControl().setVisible(
314                             !toolBarManager.isEmpty());
315                 if (tbsize != null && getToolBarAlignment() == SWT.TOP) {
316                     ToolBar tbar = toolBarManager.getControl();
317                     tbar.setBounds(x + width - 1 - tbsize.x - HMARGIN, yloc
318                             + row1Height - 1 - tbsize.y, tbsize.x, tbsize.y);
319                 }
320                 // second row
321
xloc = HMARGIN;
322                 yloc += row1Height + VSPACING;
323                 int tw = 0;
324
325                 if (tbsize != null && getToolBarAlignment() == SWT.BOTTOM) {
326                     ToolBar tbar = toolBarManager.getControl();
327                     tbar.setBounds(x + width - 1 - tbsize.x - HMARGIN, yloc,
328                             tbsize.x, tbsize.y);
329                     tw = tbsize.x + SPACING;
330                 }
331                 if (headClient != null) {
332                     int carea = width - HMARGIN * 2 - tw;
333                     headClient.setBounds(xloc, yloc, carea, clsize.y);
334                 }
335             }
336             return size;
337         }
338     }
339
340     private boolean hasToolBar() {
341         return toolBarManager != null && !toolBarManager.isEmpty();
342     }
343
344     private boolean hasMessageRegion() {
345         return messageRegion != null && !messageRegion.isEmpty();
346     }
347
348     private class MessageRegion {
349         private String JavaDoc message;
350         private int messageType;
351         private CLabel messageLabel;
352         private IMessage[] messages;
353         private Hyperlink messageHyperlink;
354         private ListenerList listeners;
355         private Color fg;
356         private int fontHeight = -1;
357         private int fontBaselineHeight = -1;
358
359         public MessageRegion() {
360         }
361
362         public boolean isDisposed() {
363             Control c = getMessageControl();
364             return c != null && c.isDisposed();
365         }
366
367         public boolean isEmpty() {
368             Control c = getMessageControl();
369             if (c == null)
370                 return true;
371             return !c.getVisible();
372         }
373
374         public int getFontHeight() {
375             if (fontHeight == -1) {
376                 Control c = getMessageControl();
377                 if (c == null)
378                     return 0;
379                 GC gc = new GC(c.getDisplay());
380                 gc.setFont(c.getFont());
381                 fontHeight = gc.getFontMetrics().getHeight();
382                 gc.dispose();
383             }
384             return fontHeight;
385         }
386
387         public int getFontBaselineHeight() {
388             if (fontBaselineHeight == -1) {
389                 Control c = getMessageControl();
390                 if (c == null)
391                     return 0;
392                 GC gc = new GC(c.getDisplay());
393                 gc.setFont(c.getFont());
394                 FontMetrics fm = gc.getFontMetrics();
395                 fontBaselineHeight = fm.getHeight() - fm.getDescent();
396                 gc.dispose();
397             }
398             return fontBaselineHeight;
399         }
400
401         public void showMessage(String JavaDoc newMessage, int newType,
402                 IMessage[] messages) {
403             Control oldControl = getMessageControl();
404             int oldType = messageType;
405             this.message = newMessage;
406             this.messageType = newType;
407             this.messages = messages;
408             if (newMessage == null) {
409                 // clearing of the message
410
if (oldControl != null && oldControl.getVisible())
411                     oldControl.setVisible(false);
412                 return;
413             }
414             ensureControlExists();
415             if (needHyperlink()) {
416                 messageHyperlink.setText(newMessage);
417                 messageHyperlink.setHref(messages);
418             } else {
419                 messageLabel.setText(newMessage);
420             }
421             if (oldType != newType)
422                 updateForeground();
423         }
424
425         public void updateToolTip(String JavaDoc toolTip) {
426             Control control = getMessageControl();
427             if (control != null)
428                 control.setToolTipText(toolTip);
429         }
430
431         public String JavaDoc getMessage() {
432             return message;
433         }
434
435         public int getMessageType() {
436             return messageType;
437         }
438
439         public IMessage[] getChildrenMessages() {
440             return messages;
441         }
442
443         public String JavaDoc getDetailedMessage() {
444             Control c = getMessageControl();
445             if (c != null)
446                 return c.getToolTipText();
447             return null;
448         }
449
450         public Control getMessageControl() {
451             if (needHyperlink() && messageHyperlink != null)
452                 return messageHyperlink;
453             return messageLabel;
454         }
455
456         public Image getMessageImage() {
457             switch (messageType) {
458             case IMessageProvider.INFORMATION:
459                 return JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_INFO);
460             case IMessageProvider.WARNING:
461                 return JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_WARNING);
462             case IMessageProvider.ERROR:
463                 return JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_ERROR);
464             default:
465                 return null;
466             }
467         }
468
469         public void addMessageHyperlinkListener(IHyperlinkListener listener) {
470             if (listeners == null)
471                 listeners = new ListenerList();
472             listeners.add(listener);
473             ensureControlExists();
474             if (messageHyperlink != null)
475                 messageHyperlink.addHyperlinkListener(listener);
476             if (listeners.size() == 1)
477                 updateForeground();
478         }
479
480         private void removeMessageHyperlinkListener(IHyperlinkListener listener) {
481             if (listeners != null) {
482                 listeners.remove(listener);
483                 if (messageHyperlink != null)
484                     messageHyperlink.removeHyperlinkListener(listener);
485                 if (listeners.isEmpty())
486                     listeners = null;
487                 ensureControlExists();
488                 if (listeners == null && !isDisposed())
489                     updateForeground();
490             }
491         }
492
493         private void ensureControlExists() {
494             if (needHyperlink()) {
495                 if (messageLabel != null)
496                     messageLabel.setVisible(false);
497                 if (messageHyperlink == null) {
498                     messageHyperlink = new Hyperlink(FormHeading.this, SWT.NULL);
499                     messageHyperlink.setUnderlined(true);
500                     messageHyperlink.setText(message);
501                     messageHyperlink.setHref(messages);
502                     Object JavaDoc[] llist = listeners.getListeners();
503                     for (int i = 0; i < llist.length; i++)
504                         messageHyperlink
505                                 .addHyperlinkListener((IHyperlinkListener) llist[i]);
506                     if (messageToolTipManager != null)
507                         messageToolTipManager.createToolTip(messageHyperlink, false);
508                 } else if (!messageHyperlink.getVisible()) {
509                     messageHyperlink.setText(message);
510                     messageHyperlink.setHref(messages);
511                     messageHyperlink.setVisible(true);
512                 }
513             } else {
514                 // need a label
515
if (messageHyperlink != null)
516                     messageHyperlink.setVisible(false);
517                 if (messageLabel == null) {
518                     messageLabel = new CLabel(FormHeading.this, SWT.NULL);
519                     messageLabel.setText(message);
520                     if (messageToolTipManager != null)
521                         messageToolTipManager.createToolTip(messageLabel, false);
522                 } else if (!messageLabel.getVisible()) {
523                     messageLabel.setText(message);
524                     messageLabel.setVisible(true);
525                 }
526             }
527             layout(true);
528         }
529
530         private boolean needHyperlink() {
531             return messageType > 0 && listeners != null;
532         }
533
534         public void setBackground(Color bg) {
535             if (messageHyperlink != null)
536                 messageHyperlink.setBackground(bg);
537             if (messageLabel != null)
538                 messageLabel.setBackground(bg);
539         }
540
541         public void setForeground(Color fg) {
542             this.fg = fg;
543         }
544
545         private void updateForeground() {
546             Color theFg;
547
548             switch (messageType) {
549             case IMessageProvider.ERROR:
550                 theFg = getDisplay().getSystemColor(SWT.COLOR_RED);
551                 break;
552             case IMessageProvider.WARNING:
553                 theFg = getDisplay().getSystemColor(SWT.COLOR_DARK_YELLOW);
554                 break;
555             default:
556                 theFg = fg;
557             }
558             getMessageControl().setForeground(theFg);
559         }
560     }
561
562     /**
563      * Creates the form content control as a child of the provided parent.
564      *
565      * @param parent
566      * the parent widget
567      */

568     public FormHeading(Composite parent, int style) {
569         super(parent, style);
570         setBackgroundMode(SWT.INHERIT_DEFAULT);
571         addListener(SWT.Paint, new Listener() {
572             public void handleEvent(Event e) {
573                 onPaint(e.gc);
574             }
575         });
576         addListener(SWT.Dispose, new Listener() {
577             public void handleEvent(Event e) {
578                 if (gradientImage != null) {
579                     gradientImage.dispose();
580                     gradientImage = null;
581                 }
582             }
583         });
584         addListener(SWT.Resize, new Listener() {
585             public void handleEvent(Event e) {
586                 if (gradientInfo != null
587                         || (backgroundImage != null && !isBackgroundImageTiled()))
588                     updateGradientImage();
589             }
590         });
591         addMouseMoveListener(new MouseMoveListener() {
592             public void mouseMove(MouseEvent e) {
593                 updateTitleRegionHoverState(e);
594             }
595         });
596         addMouseTrackListener(new MouseTrackListener() {
597             public void mouseEnter(MouseEvent e) {
598                 updateTitleRegionHoverState(e);
599             }
600
601             public void mouseExit(MouseEvent e) {
602                 titleRegion.setHoverState(TitleRegion.STATE_NORMAL);
603             }
604
605             public void mouseHover(MouseEvent e) {
606             }
607         });
608         super.setLayout(new FormHeadingLayout());
609         titleRegion = new TitleRegion(this);
610     }
611
612     /**
613      * Fully delegates the size computation to the internal layout manager.
614      */

615     public final Point computeSize(int wHint, int hHint, boolean changed) {
616         return ((FormHeadingLayout) getLayout()).computeSize(this, wHint,
617                 hHint, changed);
618     }
619
620     /**
621      * Prevents from changing the custom control layout.
622      */

623     public final void setLayout(Layout layout) {
624     }
625
626     /**
627      * Returns the title text that will be rendered at the top of the form.
628      *
629      * @return the title text
630      */

631     public String JavaDoc getText() {
632         return titleRegion.getText();
633     }
634
635     /**
636      * Returns the title image that will be rendered to the left of the title.
637      *
638      * @return the title image
639      * @since 3.2
640      */

641     public Image getImage() {
642         return titleRegion.getImage();
643     }
644
645     /**
646      * Sets the background color of the header.
647      */

648     public void setBackground(Color bg) {
649         super.setBackground(bg);
650         internalSetBackground(bg);
651     }
652
653     private void internalSetBackground(Color bg) {
654         titleRegion.setBackground(bg);
655         if (messageRegion != null)
656             messageRegion.setBackground(bg);
657         if (toolBarManager != null)
658             toolBarManager.getControl().setBackground(bg);
659         putColor(COLOR_BASE_BG, bg);
660     }
661
662     /**
663      * Sets the foreground color of the header.
664      */

665     public void setForeground(Color fg) {
666         super.setForeground(fg);
667         titleRegion.setForeground(fg);
668         if (messageRegion != null)
669             messageRegion.setForeground(fg);
670     }
671
672     /**
673      * Sets the text to be rendered at the top of the form above the body as a
674      * title.
675      *
676      * @param text
677      * the title text
678      */

679     public void setText(String JavaDoc text) {
680         titleRegion.setText(text);
681     }
682
683     public void setFont(Font font) {
684         super.setFont(font);
685         titleRegion.setFont(font);
686     }
687
688     /**
689      * Sets the image to be rendered to the left of the title.
690      *
691      * @param image
692      * the title image or <code>null</code> to show no image.
693      * @since 3.2
694      */

695     public void setImage(Image image) {
696         titleRegion.setImage(image);
697         if (messageRegion != null)
698             titleRegion.updateImage(messageRegion.getMessageImage(), true);
699         else
700             titleRegion.updateImage(null, true);
701     }
702
703     public void setTextBackground(Color[] gradientColors, int[] percents,
704             boolean vertical) {
705         if (gradientColors != null) {
706             gradientInfo = new GradientInfo();
707             gradientInfo.gradientColors = gradientColors;
708             gradientInfo.percents = percents;
709             gradientInfo.vertical = vertical;
710             setBackground(null);
711             updateGradientImage();
712         } else {
713             // reset
714
gradientInfo = null;
715             if (gradientImage != null) {
716                 gradientImage.dispose();
717                 gradientImage = null;
718                 setBackgroundImage(null);
719             }
720         }
721     }
722
723     public void setHeadingBackgroundImage(Image image) {
724         this.backgroundImage = image;
725         if (image != null)
726             setBackground(null);
727         if (isBackgroundImageTiled()) {
728             setBackgroundImage(image);
729         } else
730             updateGradientImage();
731     }
732
733     public Image getHeadingBackgroundImage() {
734         return backgroundImage;
735     }
736
737     public void setBackgroundImageTiled(boolean tiled) {
738         if (tiled)
739             flags |= BACKGROUND_IMAGE_TILED;
740         else
741             flags &= ~BACKGROUND_IMAGE_TILED;
742         setHeadingBackgroundImage(this.backgroundImage);
743     }
744
745     public boolean isBackgroundImageTiled() {
746         return (flags & BACKGROUND_IMAGE_TILED) != 0;
747     }
748
749     public void setBackgroundImage(Image image) {
750         super.setBackgroundImage(image);
751         if (image != null) {
752             internalSetBackground(null);
753         }
754     }
755
756     /**
757      * Returns the tool bar manager that is used to manage tool items in the
758      * form's title area.
759      *
760      * @return form tool bar manager
761      */

762     public IToolBarManager getToolBarManager() {
763         if (toolBarManager == null) {
764             toolBarManager = new ToolBarManager(SWT.FLAT);
765             ToolBar toolbar = toolBarManager.createControl(this);
766             toolbar.setBackground(getBackground());
767             toolbar.setForeground(getForeground());
768             toolbar.setCursor(FormsResources.getHandCursor());
769             addDisposeListener(new DisposeListener() {
770                 public void widgetDisposed(DisposeEvent e) {
771                     if (toolBarManager != null) {
772                         toolBarManager.dispose();
773                         toolBarManager = null;
774                     }
775                 }
776             });
777         }
778         return toolBarManager;
779     }
780
781     /**
782      * Returns the menu manager that is used to manage tool items in the form's
783      * title area.
784      *
785      * @return form drop-down menu manager
786      * @since 3.3
787      */

788     public IMenuManager getMenuManager() {
789         return titleRegion.getMenuManager();
790     }
791
792     /**
793      * Updates the local tool bar manager if used. Does nothing if local tool
794      * bar manager has not been created yet.
795      */

796     public void updateToolBar() {
797         if (toolBarManager != null)
798             toolBarManager.update(false);
799     }
800
801     private void onPaint(GC gc) {
802         if (!isSeparatorVisible() && getBackgroundImage() == null)
803             return;
804         Rectangle carea = getClientArea();
805         Image buffer = new Image(getDisplay(), carea.width, carea.height);
806         buffer.setBackground(getBackground());
807         GC igc = new GC(buffer);
808         igc.setBackground(getBackground());
809         igc.fillRectangle(0, 0, carea.width, carea.height);
810         if (getBackgroundImage() != null) {
811             if (gradientInfo != null)
812                 drawBackground(igc, carea.x, carea.y, carea.width, carea.height);
813             else {
814                 Image bgImage = getBackgroundImage();
815                 Rectangle ibounds = bgImage.getBounds();
816                 drawBackground(igc, carea.x, carea.y, ibounds.width,
817                         ibounds.height);
818             }
819         }
820
821         if (isSeparatorVisible()) {
822             // bg separator
823
if (hasColor(IFormColors.H_BOTTOM_KEYLINE1))
824                 igc.setForeground(getColor(IFormColors.H_BOTTOM_KEYLINE1));
825             else
826                 igc.setForeground(getBackground());
827             igc.drawLine(carea.x, carea.height - 2, carea.x + carea.width - 1,
828                     carea.height - 2);
829             if (hasColor(IFormColors.H_BOTTOM_KEYLINE2))
830                 igc.setForeground(getColor(IFormColors.H_BOTTOM_KEYLINE2));
831             else
832                 igc.setForeground(getForeground());
833             igc.drawLine(carea.x, carea.height - 1, carea.x + carea.width - 1,
834                     carea.height - 1);
835         }
836         igc.dispose();
837         gc.drawImage(buffer, carea.x, carea.y);
838         buffer.dispose();
839     }
840
841     private void updateTitleRegionHoverState(MouseEvent e) {
842         Rectangle titleRect = titleRegion.getBounds();
843         titleRect.width += titleRect.x + 15;
844         titleRect.height += titleRect.y + 15;
845         titleRect.x = 0;
846         titleRect.y = 0;
847         if (titleRect.contains(e.x, e.y))
848             titleRegion.setHoverState(TitleRegion.STATE_HOVER_LIGHT);
849         else
850             titleRegion.setHoverState(TitleRegion.STATE_NORMAL);
851     }
852
853     private void updateGradientImage() {
854         Rectangle rect = getBounds();
855         if (gradientImage != null) {
856             gradientImage.dispose();
857             gradientImage = null;
858         }
859         if (gradientInfo != null) {
860             boolean vertical = gradientInfo.vertical;
861             int width = vertical ? 1 : rect.width;
862             int height = vertical ? rect.height : 1;
863             gradientImage = new Image(getDisplay(), Math.max(width, 1), Math
864                     .max(height, 1));
865             GC gc = new GC(gradientImage);
866             drawTextGradient(gc, width, height);
867             gc.dispose();
868         } else if (backgroundImage != null && !isBackgroundImageTiled()) {
869             gradientImage = new Image(getDisplay(), Math.max(rect.width, 1),
870                     Math.max(rect.height, 1));
871             gradientImage.setBackground(getBackground());
872             GC gc = new GC(gradientImage);
873             gc.drawImage(backgroundImage, 0, 0);
874             gc.dispose();
875         }
876         setBackgroundImage(gradientImage);
877     }
878
879     private void drawTextGradient(GC gc, int width, int height) {
880         final Color oldBackground = gc.getBackground();
881         if (gradientInfo.gradientColors.length == 1) {
882             if (gradientInfo.gradientColors[0] != null)
883                 gc.setBackground(gradientInfo.gradientColors[0]);
884             gc.fillRectangle(0, 0, width, height);
885         } else {
886             final Color oldForeground = gc.getForeground();
887             Color lastColor = gradientInfo.gradientColors[0];
888             if (lastColor == null)
889                 lastColor = oldBackground;
890             int pos = 0;
891             for (int i = 0; i < gradientInfo.percents.length; ++i) {
892                 gc.setForeground(lastColor);
893                 lastColor = gradientInfo.gradientColors[i + 1];
894                 if (lastColor == null)
895                     lastColor = oldBackground;
896                 gc.setBackground(lastColor);
897                 if (gradientInfo.vertical) {
898                     final int gradientHeight = (gradientInfo.percents[i]
899                             * height / 100)
900                             - pos;
901                     gc.fillGradientRectangle(0, pos, width, gradientHeight,
902                             true);
903                     pos += gradientHeight;
904                 } else {
905                     final int gradientWidth = (gradientInfo.percents[i] * width / 100)
906                             - pos;
907                     gc.fillGradientRectangle(pos, 0, gradientWidth, height,
908                             false);
909                     pos += gradientWidth;
910                 }
911             }
912             if (gradientInfo.vertical && pos < height) {
913                 gc.setBackground(getColor(COLOR_BASE_BG));
914                 gc.fillRectangle(0, pos, width, height - pos);
915             }
916             if (!gradientInfo.vertical && pos < width) {
917                 gc.setBackground(getColor(COLOR_BASE_BG));
918                 gc.fillRectangle(pos, 0, width - pos, height);
919             }
920             gc.setForeground(oldForeground);
921         }
922     }
923
924     public boolean isSeparatorVisible() {
925         return (flags & SEPARATOR) != 0;
926     }
927
928     public void setSeparatorVisible(boolean addSeparator) {
929         if (addSeparator)
930             flags |= SEPARATOR;
931         else
932             flags &= ~SEPARATOR;
933     }
934
935     public void setToolBarAlignment(int alignment) {
936         if (alignment == SWT.BOTTOM)
937             flags |= BOTTOM_TOOLBAR;
938         else
939             flags &= ~BOTTOM_TOOLBAR;
940     }
941
942     public int getToolBarAlignment() {
943         return (flags & BOTTOM_TOOLBAR) != 0 ? SWT.BOTTOM : SWT.TOP;
944     }
945
946     public void addMessageHyperlinkListener(IHyperlinkListener listener) {
947         ensureMessageRegionExists();
948         messageRegion.addMessageHyperlinkListener(listener);
949     }
950
951     public void removeMessageHyperlinkListener(IHyperlinkListener listener) {
952         if (messageRegion != null)
953             messageRegion.removeMessageHyperlinkListener(listener);
954     }
955
956     public String JavaDoc getMessage() {
957         return messageRegion != null ? messageRegion.getMessage() : null;
958     }
959
960     public int getMessageType() {
961         return messageRegion != null ? messageRegion.getMessageType() : 0;
962     }
963
964     public IMessage[] getChildrenMessages() {
965         return messageRegion != null ? messageRegion.getChildrenMessages()
966                 : NULL_MESSAGE_ARRAY;
967     }
968
969     private void ensureMessageRegionExists() {
970         // ensure message region exists
971
if (messageRegion == null)
972             messageRegion = new MessageRegion();
973     }
974
975     public void showMessage(String JavaDoc newMessage, int type, IMessage[] messages) {
976         if (messageRegion == null) {
977             // check the trivial case
978
if (newMessage == null)
979                 return;
980         } else if (messageRegion.isDisposed())
981             return;
982         ensureMessageRegionExists();
983         messageRegion.showMessage(newMessage, type, messages);
984         titleRegion.updateImage(messageRegion.getMessageImage(), false);
985         if (messageToolTipManager != null)
986             messageToolTipManager.update();
987         layout();
988         redraw();
989     }
990
991     /**
992      * Tests if the form is in the 'busy' state.
993      *
994      * @return <code>true</code> if busy, <code>false</code> otherwise.
995      */

996
997     public boolean isBusy() {
998         return titleRegion.isBusy();
999     }
1000
1001    /**
1002     * Sets the form's busy state. Busy form will display 'busy' animation in
1003     * the area of the title image.
1004     *
1005     * @param busy
1006     * the form's busy state
1007     */

1008
1009    public void setBusy(boolean busy) {
1010        if (titleRegion.setBusy(busy))
1011            layout();
1012    }
1013
1014    public Control getHeadClient() {
1015        return headClient;
1016    }
1017
1018    public void setHeadClient(Control headClient) {
1019        if (headClient != null)
1020            Assert.isTrue(headClient.getParent() == this);
1021        this.headClient = headClient;
1022        layout();
1023    }
1024
1025    public void putColor(String JavaDoc key, Color color) {
1026        if (color == null)
1027            colors.remove(key);
1028        else
1029            colors.put(key, color);
1030    }
1031
1032    public Color getColor(String JavaDoc key) {
1033        return (Color) colors.get(key);
1034    }
1035
1036    public boolean hasColor(String JavaDoc key) {
1037        return colors.containsKey(key);
1038    }
1039
1040    public void addDragSupport(int operations, Transfer[] transferTypes,
1041            DragSourceListener listener) {
1042        titleRegion.addDragSupport(operations, transferTypes, listener);
1043    }
1044
1045    public void addDropSupport(int operations, Transfer[] transferTypes,
1046            DropTargetListener listener) {
1047        titleRegion.addDropSupport(operations, transferTypes, listener);
1048    }
1049
1050    public IMessageToolTipManager getMessageToolTipManager() {
1051        return messageToolTipManager;
1052    }
1053
1054    public void setMessageToolTipManager(
1055            IMessageToolTipManager messageToolTipManager) {
1056        this.messageToolTipManager = messageToolTipManager;
1057    }
1058}
1059
Popular Tags