1 11 12 package org.eclipse.jface.preference; 13 14 import org.eclipse.jface.dialogs.ControlAnimator; 15 import org.eclipse.jface.dialogs.Dialog; 16 import org.eclipse.jface.dialogs.DialogMessageArea; 17 import org.eclipse.jface.dialogs.IMessageProvider; 18 import org.eclipse.jface.dialogs.ImageAndMessageArea; 19 import org.eclipse.jface.resource.JFaceResources; 20 import org.eclipse.jface.util.Policy; 21 import org.eclipse.swt.SWT; 22 import org.eclipse.swt.custom.CLabel; 23 import org.eclipse.swt.graphics.Image; 24 import org.eclipse.swt.layout.FormAttachment; 25 import org.eclipse.swt.layout.FormData; 26 import org.eclipse.swt.widgets.Composite; 27 28 35 class PreferenceMessageArea extends DialogMessageArea { 36 37 private String lastMessageText; 38 39 private int lastMessageType; 40 41 private CLabel titleLabel; 42 43 private ImageAndMessageArea messageArea; 44 45 private PreferenceDialog preferenceDialog; 46 47 private static int titleLabelOffset = -1; 48 49 private ControlAnimator animator; 50 51 52 53 60 public PreferenceMessageArea(PreferenceDialog preferenceDialog) { 61 this.preferenceDialog = preferenceDialog; 62 } 63 64 67 public void createContents(Composite parent) { 68 69 titleLabel = new CLabel(parent, SWT.NONE); 71 titleLabel.setFont(JFaceResources.getBannerFont()); 72 73 messageArea = new ImageAndMessageArea(preferenceDialog.formTitleComposite, SWT.WRAP); 74 messageArea.setFont(JFaceResources.getDialogFont()); 75 messageArea.setVisible(false); 76 77 animator = Policy.getAnimatorFactory().createAnimator(messageArea); 78 } 79 80 83 public void setMessageLayoutData(Object layoutData) { 84 ((FormData) layoutData).top = null; 85 ((FormData) layoutData).right = new FormAttachment(100,-getTitleLabelOffset()); 86 messageArea.setLayoutData(layoutData); 87 } 88 89 90 99 private int getTitleLabelOffset() { 100 if (titleLabelOffset == -1) { 101 int offsetCalc = preferenceDialog.formTitleComposite.getBounds().width - 102 titleLabel.getBounds().width; 103 if (offsetCalc > 0) 104 return titleLabelOffset = offsetCalc; 105 106 return 0; 107 } 108 return titleLabelOffset; 109 } 110 111 114 public void updateText(String newMessage, int newType) { 115 Image newImage = null; 116 switch (newType) { 117 case IMessageProvider.NONE: 118 if (newMessage == null) { 119 restoreTitle(); 120 } else { 121 showTitle(newMessage, null); 122 } 123 return; 124 case IMessageProvider.INFORMATION: 125 newImage = JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_INFO); 126 break; 127 case IMessageProvider.WARNING: 128 newImage = JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_WARNING); 129 break; 130 case IMessageProvider.ERROR: 131 newImage = JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_ERROR); 132 133 break; 134 } 135 136 messageArea.moveAbove(null); 137 messageArea.setImage(newImage); 138 139 messageArea.setText(newMessage); 140 messageArea.setToolTipText(newMessage); 141 lastMessageText = newMessage; 142 143 int bottom = messageArea.getParent().getBounds().height; 144 if (!messageArea.isVisible() && messageArea.getBounds().y != bottom) { 145 messageArea.setBounds(messageArea.getBounds().x, 146 bottom, 147 titleLabel.getBounds().width, 148 messageArea.computeSize(SWT.DEFAULT,SWT.DEFAULT).y); 149 } 150 animator.setVisible(true); 151 setMessageLayoutData(messageArea.getLayoutData()); 152 } 153 154 157 public void restoreTitle() { 158 titleLabel.setVisible(true); 159 animator.setVisible(false); 160 lastMessageText = null; 161 lastMessageType = IMessageProvider.NONE; 162 } 163 164 167 public void clearErrorMessage() { 168 if (lastMessageText == null) { 169 restoreTitle(); 170 } else { 171 updateText(lastMessageText, lastMessageType); 172 } 173 } 174 175 178 public void setTitleLayoutData(Object layoutData) { 179 titleLabel.setLayoutData(layoutData); 180 } 181 182 185 public void showTitle(String titleMessage, Image titleImage) { 186 titleLabel.setImage(titleImage); 187 titleLabel.setText(titleMessage); 188 restoreTitle(); 189 return; 190 } 191 } 192 | Popular Tags |