package com.quantum.ui.dialog; import java.beans.PropertyChangeListener; import java.beans.PropertyChangeSupport; import com.quantum.Messages; import com.quantum.model.Bookmark; import com.quantum.model.BookmarkCollection; import org.eclipse.jface.dialogs.Dialog; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.List; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Shell; /** * @author BC */ public class BookmarkSelectionDialog extends Dialog { private List list; private String selection = null; private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this); /** * @param parentShell */ public BookmarkSelectionDialog(Shell parentShell) { super(parentShell); int style = getShellStyle() | SWT.TITLE; setShellStyle(style); } protected void configureShell(Shell shell) { super.configureShell(shell); shell.setText(Messages.getString("BookmarkSelectionDialog.title")); } /** * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite) */ protected Control createDialogArea(Composite parent) { Composite composite = new Composite(parent, 0); GridLayout layout = new GridLayout(); composite.setLayout(layout); layout.numColumns = 1; layout.verticalSpacing = 1; Label label = new Label(composite, SWT.NULL); label.setText(Messages.getString("BookmarkSelectionDialog.text")); this.list = new List(composite, SWT.SINGLE | SWT.BORDER | SWT.V_SCROLL); Bookmark[] bookmarks = BookmarkCollection.getInstance().getBookmarks(); for (int i = 0, length = (bookmarks == null) ? 0 : bookmarks.length; i < length; i++) { this.list.add(bookmarks[i].getName()); if (i == 0) { this.list.select(i); setSelection(bookmarks[i].getName()); } } final List list = this.list; list.addListener(SWT.Selection, new Listener() { public void handleEvent (Event event) { String[] selections = list.getSelection(); if (selections != null && selections.length > 0) { BookmarkSelectionDialog.this.setSelection(selections[0]); } } }); GridData full = new GridData(); full.horizontalAlignment = GridData.FILL; full.verticalAlignment = GridData.FILL; full.heightHint = convertHeightInCharsToPixels(3); this.list.setLayoutData(full); return composite; } /** * @param listener */ public synchronized void addPropertyChangeListener(PropertyChangeListener listener) { this.propertyChangeSupport.addPropertyChangeListener(listener); } /** * @param listener */ public synchronized void removePropertyChangeListener(PropertyChangeListener listener) { this.propertyChangeSupport.removePropertyChangeListener(listener); } private void setSelection(String selection) { String original = this.selection; if (this.selection == null || !this.selection.equals(selection)) { this.selection = selection; this.propertyChangeSupport.firePropertyChange("selection", original, this.selection); } } }