# setMessage-Methode in Title-Area-Dialogen



## dzim (12. Nov 2008)

Hi folks,

ich bastel gerade mal wieder ein wenig herum und habe mir Testweise nen kleinen Hilfsdialog geschrieben, der für den Datei-Import und -Export genutzt werden soll.
Funktioniert alles super - bis auf eine Kleinigkeit: Der Message-Bereich in der TitleArea meines Dialogs wird nicht so aktualiisiert, wie ich es erwarte - obwohl augenscheinlich die Methode setMessage(String) aufgerufen wird. (ich hab bei meinen Programmen immer zum Testen ne Console eingebunden auf die ich syserr und out binde).
Aber der Text wird nur bei ErrorMessages erneuert.
Ich hab diesen Bereicht beriets mal für nen Wizard genutzt, da ging so was super - hier nicht...

any ideas?

Die betreffende Codestelle befinded sich ganz am Ende in der privaten Listener-Klasse...

Wenn einer ne Ahnung hat und was beisteuern kann: Schon mal Danke im Voraus!
D


```
package com.ipoque.p2p.tracker.rcp.pfs.ui.dialog;

import java.io.File;
import java.io.IOException;
import java.util.InvalidPropertiesFormatException;
import java.util.Map;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.ipoque.p2p.tracker.rcp.core.simpleLogger.Logger;
import com.ipoque.p2p.tracker.rcp.pfs.util.PFSManagerUtil;
import com.swtdesigner.SWTResourceManager;

public class OpenSaveDialog extends TitleAreaDialog {

	private static final Color BLACK = SWTResourceManager.getColor(0, 0, 0);
	private static final Color RED = SWTResourceManager.getColor(255, 0, 0);

	private static final String WINDOW_TITLE = "Open/Save Dialog";

	private static final String ERROR_OPEN_FILE_MSG = "You need to select an exiting file!";
	private static final String ERROR_OPEN_DIRECTORY_MSG = "You need to select an exiting directory!";
	private static final String ERROR_OPEN_COMMON_MSG = "Specified file does not exist!";

	private static final String OPEN_MSG = "Select or enter a file to open.";
	private static final String SAVE_MSG = "Select or enter a file to save.";

	private static final String STANDARD_TITLE_OPEN = "Open";
	private static final String STANDARD_TITLE_SAVE = "Save";

	private CLabel fileToHandleLabel;
	private Button browseButton;
	private Text fileToHandleText;

	private boolean isOpenDialog;
	private boolean isDirectory;

	private Map<String, String> fileExtensions;
	private File fileToHandle;

	private String titleAreaTitle;

	/**
	 * Create the dialog
	 * 
	 * @param parentShell
	 */
	public OpenSaveDialog(Shell parentShell, boolean isOpenDialog,
			String title, File fileToHandle, boolean isDirectory,
			Map<String, String> fileExtensions) {

		super(parentShell);

		this.isOpenDialog = isOpenDialog;

		if (title == null) {
			this.titleAreaTitle = isOpenDialog ? STANDARD_TITLE_OPEN
					: STANDARD_TITLE_SAVE;
		} else {
			this.titleAreaTitle = title;
		}

		this.fileToHandle = fileToHandle;
		this.isDirectory = isDirectory;
		this.fileExtensions = fileExtensions;

		System.err.println("isOpenDialog=" + isOpenDialog + " / isDirectory="
				+ isDirectory);
	}

	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);

		newShell.setText(WINDOW_TITLE); //$NON-NLS-1$
	}

	/**
	 * Create contents of the dialog
	 * 
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {

		Composite area = (Composite) super.createDialogArea(parent);
		Composite container = new Composite(area, SWT.NONE);
		container.setLayoutData(new GridData(GridData.FILL_BOTH));
		container.setLayout(new FormLayout());

		fileToHandleText = new Text(container, SWT.BORDER);
		final FormData fd_text = new FormData();
		fd_text.right = new FormAttachment(0, 345);
		fd_text.top = new FormAttachment(0, 5);
		fd_text.bottom = new FormAttachment(0, 30);
		fileToHandleText.setLayoutData(fd_text);

		fileToHandleLabel = new CLabel(container, SWT.NONE);
		fd_text.left = new FormAttachment(fileToHandleLabel, 5, SWT.RIGHT);
		final FormData fd_label = new FormData();
		fd_label.bottom = new FormAttachment(0, 25);
		fd_label.top = new FormAttachment(0, 10);
		fd_label.right = new FormAttachment(0, 65);
		fd_label.left = new FormAttachment(0, 5);
		fileToHandleLabel.setLayoutData(fd_label);
		fileToHandleLabel.setText("File");

		browseButton = new Button(container, SWT.NONE);
		final FormData fd_browseButton = new FormData();
		fd_browseButton.right = new FormAttachment(100, -5);
		fd_browseButton.bottom = new FormAttachment(fileToHandleText, 0,
				SWT.BOTTOM);
		fd_browseButton.top = new FormAttachment(fileToHandleText, 0, SWT.TOP);
		fd_browseButton.left = new FormAttachment(fileToHandleText, 5,
				SWT.RIGHT);
		browseButton.setLayoutData(fd_browseButton);
		browseButton.setText("...");

		initTitleArea();

		hookListeners();

		return area;
	}

	private void initTitleArea() {

		setTitle(titleAreaTitle);
		setMessage(isOpenDialog ? OPEN_MSG : SAVE_MSG);
	}

	private void hookListeners() {

		OpenSaveWidgetsListener listener = new OpenSaveWidgetsListener();

		fileToHandleText.addListener(SWT.Modify, listener);
		browseButton.addListener(SWT.Selection, listener);
	}

	/**
	 * Create contents of the button bar
	 * 
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
				true);
		createButton(parent, IDialogConstants.CANCEL_ID,
				IDialogConstants.CANCEL_LABEL, false);
	}

	@Override
	protected void okPressed() {

		System.err.println(isDirectory + " / " + isOpenDialog);

		if (isDirectory) {

			if (fileToHandle.isDirectory()) {

				setMessage(isOpenDialog ? OPEN_MSG : SAVE_MSG);
				fileToHandleLabel.setForeground(BLACK);

				super.okPressed();

			} else {

				setErrorMessage(ERROR_OPEN_DIRECTORY_MSG);
				fileToHandleLabel.setForeground(RED);

				return;
			}

		} else {

			if (fileToHandle.isFile()) {

				setMessage(isOpenDialog ? OPEN_MSG : SAVE_MSG);
				fileToHandleLabel.setForeground(BLACK);

				super.okPressed();

			} else {

				setErrorMessage(ERROR_OPEN_FILE_MSG);
				fileToHandleLabel.setForeground(RED);

				return;
			}
		}
	}

	/**
	 * Return the initial size of the dialog
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(395, 185);
	}

	public File getFileToHandle() {
		return fileToHandle;
	}

	private class OpenSaveWidgetsListener implements Listener {

		@Override
		public void handleEvent(Event event) {

			if (event.widget.equals(browseButton)) {

				String lastDir = null;

				try {
					lastDir = PFSManagerUtil.getPFSApplicationLastDir();
				} catch (InvalidPropertiesFormatException e) {
					System.err.println(e.getMessage());
					Logger.logError(e);
				}

				Shell parentShell = OpenSaveDialog.this.getShell();

				String selection = null;

				if (isDirectory) {

					DirectoryDialog directoryDialog;

					if (isOpenDialog) {

						directoryDialog = new DirectoryDialog(parentShell,
								SWT.OPEN);

						directoryDialog.setText(WINDOW_TITLE + ": "
								+ STANDARD_TITLE_OPEN);
						directoryDialog.setMessage(OPEN_MSG);

						if (fileToHandle != null && fileToHandle.isFile()) {
							directoryDialog.setFilterPath(fileToHandle
									.getParent());
						} else if (fileToHandle != null
								&& fileToHandle.isDirectory()) {
							directoryDialog.setFilterPath(fileToHandle
									.getAbsolutePath());
						} else {
							directoryDialog.setFilterPath(lastDir);
						}

					} else {

						directoryDialog = new DirectoryDialog(parentShell,
								SWT.SAVE);

						directoryDialog.setText(WINDOW_TITLE + ": "
								+ STANDARD_TITLE_SAVE);
						directoryDialog.setMessage(SAVE_MSG);

						if (fileToHandle != null && fileToHandle.isFile()) {
							directoryDialog.setFilterPath(fileToHandle
									.getParent());
						} else if (fileToHandle != null
								&& fileToHandle.isDirectory()) {
							directoryDialog.setFilterPath(fileToHandle
									.getAbsolutePath());
						} else {
							directoryDialog.setFilterPath(lastDir);
						}
					}

					selection = directoryDialog.open();

				} else {

					FileDialog fileDialog;

					String[] filterNames = new String[fileExtensions.size()];
					String[] filterExtensions = new String[fileExtensions
							.size()];

					int index = 0;
					for (String name : fileExtensions.keySet()) {

						filterNames[index] = name;
						filterExtensions[index] = fileExtensions.get(name);

						index++;
					}

					if (isOpenDialog) {

						fileDialog = new FileDialog(parentShell, SWT.OPEN);

						fileDialog.setText(WINDOW_TITLE + ": "
								+ STANDARD_TITLE_OPEN);

						if (fileToHandle != null && fileToHandle.isFile()) {
							fileDialog.setFilterPath(fileToHandle.getParent());
						} else if (fileToHandle != null
								&& fileToHandle.isDirectory()) {
							fileDialog.setFilterPath(fileToHandle
									.getAbsolutePath());
						} else {
							fileDialog.setFilterPath(lastDir);
						}

						fileDialog.setFilterExtensions(filterExtensions);
						fileDialog.setFilterNames(filterNames);

						if (fileToHandle != null && fileToHandle.isFile()) {
							fileDialog.setFileName(fileToHandle.getName());
						}

					} else {

						fileDialog = new FileDialog(parentShell, SWT.SAVE);

						fileDialog.setText(WINDOW_TITLE + ": "
								+ STANDARD_TITLE_SAVE);

						if (fileToHandle != null && fileToHandle.isFile()) {
							fileDialog.setFilterPath(fileToHandle.getParent());
						} else if (fileToHandle != null
								&& fileToHandle.isDirectory()) {
							fileDialog.setFilterPath(fileToHandle
									.getAbsolutePath());
						} else {
							fileDialog.setFilterPath(lastDir);
						}

						fileDialog.setFilterExtensions(filterExtensions);
						fileDialog.setFilterNames(filterNames);

						fileDialog.setOverwrite(true);
					}

					selection = fileDialog.open();
				}

				if (selection != null) {

					try {
						if (!PFSManagerUtil.setPFSApplicationLastDir(selection)) {

							String message = "Error on "
									+ selection
									+ ": Couldn't store the selection to PFS system property "
									+ PFSManagerUtil
											.getPFSApplicationSettingsFile(System
													.getProperty("os.name"));
							System.err.println(message);
							Logger.logWarning(message);
						}

						fileToHandle = new File(selection);

					} catch (InvalidPropertiesFormatException e) {
						System.err.println(e.getMessage());
						Logger.logError(e);
					} catch (IOException e) {
						System.err.println(e.getMessage());
						Logger.logError(e);
					}
				}

				if (fileToHandle != null) {
					fileToHandleText.setText(fileToHandle.getAbsolutePath());
				}

			} else if (event.widget.equals(fileToHandleText)) {

				File test = new File(fileToHandleText.getText());

				if (test.exists()) {

					if (isDirectory && test.isFile()) {

						setErrorMessage(ERROR_OPEN_DIRECTORY_MSG);
						fileToHandleLabel.setForeground(RED);

						return;

					} else if (!isDirectory && test.isDirectory()) {

						setErrorMessage(ERROR_OPEN_FILE_MSG);
						fileToHandleLabel.setForeground(RED);

						return;
					}

					setMessage((isOpenDialog ? OPEN_MSG : SAVE_MSG),
							IMessageProvider.NONE);
					fileToHandleLabel.setForeground(BLACK);

					fileToHandle = test;

				}

				if (!test.exists()) {

					setErrorMessage(ERROR_OPEN_COMMON_MSG);
					fileToHandleLabel.setForeground(RED);

					return;
				}

			} else {

				System.err.println("Button " + event.widget
						+ " to select not found");
				Logger.logError(new Exception("Button " + event.widget
						+ " to select not found"));
			}
		}
	}
}
```


----------



## Wildcard (12. Nov 2008)

vor setMessage musst du setError(null) aufrufen, die Error Message steht in der Priorität höher.


----------



## dzim (12. Nov 2008)

sehr geil!

Danke - jetzt wo du das sagst, glaube ich mich zu erinnern, dass das bei den Wizards auch schon so war - ich hätte mir vielleicht einfach mal die Zeit nehmen sollen, die Klassen davon mal erneut durchzulesen...

Vielen Dank!

Problem Nr. ein gefixt ;-)


----------

