Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[platform-swt-dev] Decoreated Field

Hello, im trying to create the typical decorator to an input text that
uses Eclipse 3.2 (typical bulb). I have found an example, but is for use
with RCP (inside Eclipse), and i want to use in a standalone
application. The snippet i have created is:

package com.dg.dg59.specialist;

import org.eclipse.core.runtime.Status;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.StatusLineManager;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.fieldassist.DecoratedField;
import org.eclipse.jface.fieldassist.FieldDecoration;
import org.eclipse.jface.fieldassist.TextControlCreator;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.dg.dg59.specialist.images.Dummy;

/**
 * 
 * @author asoto
 * @author rob
 * @invariant
 */
public class DecoratorSandbox extends ApplicationWindow {

	private boolean showContentProposalCue = true, showSecondaryPopup =
true,
	showRequiredFieldColor = true, showRequiredFieldDecoration = true,
	showErrorDecoration = true, showErrorMessage = true,
showDecorationsOnFocus = true;
	
	private FieldDecoration requiredFieldDecoration, cueDecoration,
	errorDecoration;

	
	private StyledText styledText;
	/**
	 * Create the application window
	 */
	public DecoratorSandbox() {
		super(null);
		createActions();
		addToolBar(SWT.FLAT | SWT.WRAP);
		addMenuBar();
		addStatusLine();
	}

	/**
	 * Create contents of the application window
	 * @param parent
	 */
	@Override
	protected Control createContents(Composite parent) {
		
		Label label;
		Composite container = new Composite(parent, SWT.NONE);
		
		GridLayout layout = new GridLayout();
		
		container.setLayout(layout);
		container.setLayoutData(new GridData(GridData.FILL_BOTH));
		

		label = new Label(container, SWT.LEFT);
		label.setLayoutData(new GridData());
		label.setText("User name:");

		// Create a field representing a user name
		final DecoratedField field = new DecoratedField(container, SWT.BORDER,
				new TextControlCreator());
		createFieldDecorations(field);

		Text text = (Text) field.getControl();
		text.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent event) {
				handleModify(field);
			}
		});

		

		// Extra field added to ensure everything lines up
		label = new Label(container, SWT.LEFT);
		label.setText("Password:");
		DecoratedField anotherField = new DecoratedField(container,
SWT.BORDER,
				new TextControlCreator());
		((Text) anotherField.getControl()).setText("******");

		container.addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent event) {
				handleDialogDispose();
			}
		});

		final Button button = new Button(container, SWT.NONE);
		button.setLayoutData(new GridData());
		
		Image image = new Image(getShell().getDisplay(),
"content_proposal_cue.gif");
		button.setImage(image);

		//
		return container;
	}

	private void createFieldDecorations(DecoratedField field) {
		if (showRequiredFieldDecoration) {
			field.addFieldDecoration(getRequiredFieldDecoration(), SWT.TOP
					| SWT.RIGHT, showDecorationsOnFocus);
		}

		if (showContentProposalCue) {
			field.addFieldDecoration(getCueDecoration(), SWT.BOTTOM | SWT.LEFT,
					showDecorationsOnFocus);
		}

		if (showErrorDecoration) {
			field.addFieldDecoration(getErrorDecoration(), SWT.TOP | SWT.LEFT,
					showDecorationsOnFocus);
		}

	}
	
	private void handleDialogDispose() {
		Image image;
		if (cueDecoration != null && (image = cueDecoration.getImage()) !=
null) {
			image.dispose();
			cueDecoration = null;
		}
		if (requiredFieldDecoration != null
				&& (image = requiredFieldDecoration.getImage()) != null) {
			image.dispose();
			requiredFieldDecoration = null;
		}
		if (errorDecoration != null
				&& (image = errorDecoration.getImage()) != null) {
			image.dispose();
			errorDecoration = null;
		}
	}

	private FieldDecoration getCueDecoration() {
		if (cueDecoration == null) {
			ImageDescriptor descriptor = ImageDescriptor.createFromFile(
					Dummy.class, "images/content_proposal_cue.gif"); //$NON-NLS-1$
			cueDecoration = new FieldDecoration(descriptor
					.createImage(getShell().getDisplay()),
					"Content assist available for this field, press "
							);
		}
		return cueDecoration;
	}

	private FieldDecoration getErrorDecoration() {
		if (errorDecoration == null) {
//			Image image = new Image(Display.getCurrent(), )
//			ImageDescriptor.createFromImage()
			ImageDescriptor descriptor = ImageDescriptor.createFromFile(
					Dummy.class, "images/content_proposal_cue.gif"); //$NON-NLS-1$
			errorDecoration = new FieldDecoration(descriptor
					.createImage(getShell().getDisplay()),
					"Invalid data in field");
		}
		return errorDecoration;
	}

	private FieldDecoration getRequiredFieldDecoration() {
		if (requiredFieldDecoration == null) {
			
			Image image = new Image(getShell().getDisplay(),
"content_proposal_cue.gif");
			getShell().setImage(image);
			ImageDescriptor descriptor = ImageDescriptor.createFromImage(image);
			
//			ImageDescriptor descriptor = ImageDescriptor.createFromFile(
//					Dummy.class, "images/content_proposal_cue.gif"); //$NON-NLS-1$
			requiredFieldDecoration = new FieldDecoration(descriptor
					.createImage(getShell().getDisplay()),
					"Field must not be empty");
		}
		return requiredFieldDecoration;
	}

	private void handleModify(DecoratedField field) {
		String contents = ((Text) field.getControl()).getText();
//		if (!isValidUser(contents)) {
//			showError(field);
//		} else {
//			hideError(field);
//		}

		boolean isEmpty = contents.length() == 0;

		if (showRequiredFieldColor) {
			if (isEmpty) {
				field.getControl().setBackground(
						field.getControl().getDisplay().getSystemColor(
								SWT.COLOR_YELLOW));
			} else {
				//field.getControl().setBackground(defaultTextColor);
			}
		} else if (showRequiredFieldDecoration) {
			if (isEmpty)
				field.showDecoration(getRequiredFieldDecoration());
			else
				field.hideDecoration(getRequiredFieldDecoration());
		}
	}

//	private boolean isValidUser(String username) {
//		for (int i = 0; i < validUsers.length; i++) {
//			if (validUsers[i].equals(username)) {
//				return true;
//			}
//		}
//		return false;
//	}

	private void showError(DecoratedField field) {
		if (showErrorMessage) {
//			updateStatus(new Status(Status.ERROR,
//					"org.eclipse.examples.contentassist", 0,
//					getErrorDecoration().getDescription(), null));
		}
		if (showErrorDecoration)
			field.showDecoration(getErrorDecoration());
	}

	private void hideError(DecoratedField field) {
		if (showErrorMessage) {
//			this.updateStatus(Status.OK_STATUS);
		}
		if (showErrorDecoration)
			field.hideDecoration(getErrorDecoration());
	}
	
	/**
	 * Create the actions
	 */
	private void createActions() {
		// Create the actions
	}

	/**
	 * Create the menu manager
	 * @return the menu manager
	 */
	@Override
	protected MenuManager createMenuManager() {
		MenuManager menuManager = new MenuManager("menu");
		return menuManager;
	}

	/**
	 * Create the toolbar manager
	 * @return the toolbar manager
	 */
	@Override
	protected ToolBarManager createToolBarManager(int style) {
		ToolBarManager toolBarManager = new ToolBarManager(style);
		return toolBarManager;
	}

	/**
	 * Create the status line manager
	 * @return the status line manager
	 */
	@Override
	protected StatusLineManager createStatusLineManager() {
		StatusLineManager statusLineManager = new StatusLineManager();
		statusLineManager.setMessage(null, "");
		return statusLineManager;
	}

	/**
	 * Launch the application
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			DecoratorSandbox window = new DecoratorSandbox();
			window.setBlockOnOpen(true);
			window.open();
			Display.getCurrent().dispose();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Configure the shell
	 * @param newShell
	 */
	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("New Application");
	}

	/**
	 * Return the initial size of the window
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(500, 375);
	}

}


It is a copy paste of the example TaskAssistant Example. 

Thank you very much.


La información contenida en el presente e-mail es confidencial y está reservada para el uso exclusivo de su destinatario. Se prohíbe estrictamente la distribución, copia o utilización de esta información sin el previo permiso de su destinatario. Si usted no fuera el destinatario, por favor notifíquelo inmediatamente al remitente y elimine el presente mensaje de su sistema informático.

Information contained in this e-mail is confidential and is intended for the use of the addressee only. Any dissemination, distribution, copying or use of this communication without prior permission of the addressee is strictly prohibited. If you are not the intended addressee, please notify the sender immediately by reply and then delete this message from your computer system.


Back to the top