1 11 package org.eclipse.ui.texteditor; 12 13 import org.eclipse.swt.SWT; 14 import org.eclipse.swt.graphics.Color; 15 import org.eclipse.swt.graphics.Image; 16 import org.eclipse.swt.graphics.Rectangle; 17 import org.eclipse.swt.layout.GridData; 18 import org.eclipse.swt.layout.GridLayout; 19 import org.eclipse.swt.widgets.Composite; 20 import org.eclipse.swt.widgets.Label; 21 import org.eclipse.swt.widgets.Text; 22 23 import org.eclipse.jface.dialogs.Dialog; 24 import org.eclipse.jface.dialogs.IDialogConstants; 25 import org.eclipse.jface.dialogs.IMessageProvider; 26 import org.eclipse.jface.resource.JFaceColors; 27 import org.eclipse.jface.resource.JFaceResources; 28 29 30 40 class MessageRegion { 41 42 private Text messageText; 43 44 private Label messageImageLabel; 45 46 private Composite messageComposite; 47 48 private String lastMessageText = ""; 50 private int lastMessageType; 51 52 55 public MessageRegion() { 56 } 58 59 65 public void createContents(Composite parent) { 66 messageComposite = new Composite(parent, SWT.NONE); 67 GridLayout messageLayout = new GridLayout(); 68 messageLayout.numColumns = 2; 69 messageLayout.marginWidth = 0; 70 messageLayout.marginHeight = 0; 71 messageLayout.makeColumnsEqualWidth = false; 72 messageComposite.setLayout(messageLayout); 73 messageImageLabel = new Label(messageComposite, SWT.NONE); 74 75 GridData imageData = new GridData(GridData.VERTICAL_ALIGN_CENTER); 76 Image sizingImage = JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_ERROR); 77 Rectangle imageBounds; 78 if(sizingImage == null) 79 imageBounds = new Rectangle(0,0,IDialogConstants.VERTICAL_MARGIN * 2,IDialogConstants.VERTICAL_MARGIN * 2); 80 else 81 imageBounds = sizingImage.getBounds(); 82 imageData.heightHint = imageBounds.height + IDialogConstants.VERTICAL_SPACING; 83 imageData.widthHint = imageBounds.width + IDialogConstants.HORIZONTAL_SPACING; 84 messageImageLabel.setLayoutData(imageData); 85 86 messageText = new Text(messageComposite, SWT.NONE); 87 messageText.setEditable(false); 88 messageText.setBackground(parent.getDisplay().getSystemColor( 89 SWT.COLOR_WIDGET_BACKGROUND)); 90 91 GridData textData = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL 92 | GridData.VERTICAL_ALIGN_CENTER); 93 messageText.setLayoutData(textData); 94 hideRegion(); 95 96 } 97 98 105 public void setMessageLayoutData(Object layoutData) { 106 messageComposite.setLayoutData(layoutData); 107 } 108 109 120 public void updateText(String newMessage, int newType) { 121 Image newImage = null; 122 boolean showingError = false; 123 switch (newType) { 124 case IMessageProvider.NONE: 125 hideRegion(); 126 return; 127 case IMessageProvider.INFORMATION: 128 newImage = JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_INFO); 129 break; 130 case IMessageProvider.WARNING: 131 newImage = JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_WARNING); 132 break; 133 case IMessageProvider.ERROR: 134 newImage = JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_ERROR); 135 showingError = true; 136 break; 137 } 138 139 if(newMessage == null){ hideRegion(); 141 return; 142 } 143 showRegion(); 144 if (newMessage.equals(messageText.getText()) 146 && newImage == messageImageLabel.getImage()) 147 return; 148 messageImageLabel.setImage(newImage); 149 messageText.setText(newMessage); 150 if (showingError) 151 setMessageColors(JFaceColors.getErrorBackground(messageComposite.getDisplay())); 152 else { 153 lastMessageText = newMessage; 154 setMessageColors(JFaceColors.getBannerBackground(messageComposite.getDisplay())); 155 } 156 157 } 158 159 162 private void showRegion() { 163 messageComposite.setVisible(true); 164 } 165 166 169 private void hideRegion() { 170 messageComposite.setVisible(false); 171 lastMessageText = null; 172 lastMessageType = IMessageProvider.NONE; 173 } 174 175 181 private void setMessageColors(Color color) { 182 messageText.setBackground(color); 183 messageComposite.setBackground(color); 184 messageImageLabel.setBackground(color); 185 } 186 187 192 public void clearErrorMessage() { 193 updateText(lastMessageText, lastMessageType); 194 } 195 } 196 | Popular Tags |