added file extension php5
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / ui / dialog / BookmarkSelectionDialog.java
1 package com.quantum.ui.dialog;
2
3 import java.beans.PropertyChangeListener;
4 import java.beans.PropertyChangeSupport;
5
6 import com.quantum.Messages;
7 import com.quantum.model.Bookmark;
8 import com.quantum.model.BookmarkCollection;
9
10 import org.eclipse.jface.dialogs.Dialog;
11 import org.eclipse.swt.SWT;
12 import org.eclipse.swt.layout.GridData;
13 import org.eclipse.swt.layout.GridLayout;
14 import org.eclipse.swt.widgets.Composite;
15 import org.eclipse.swt.widgets.Control;
16 import org.eclipse.swt.widgets.Event;
17 import org.eclipse.swt.widgets.Label;
18 import org.eclipse.swt.widgets.List;
19 import org.eclipse.swt.widgets.Listener;
20 import org.eclipse.swt.widgets.Shell;
21
22 /**
23  * @author BC
24  */
25 public class BookmarkSelectionDialog extends Dialog {
26     
27     private List list;
28     private String selection = null;
29     private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
30
31     /**
32      * @param parentShell
33      */
34     public BookmarkSelectionDialog(Shell parentShell) {
35         super(parentShell);
36         int style = getShellStyle() | SWT.TITLE;
37         setShellStyle(style);
38     }
39     
40     protected void configureShell(Shell shell) {
41         super.configureShell(shell);
42         shell.setText(Messages.getString("BookmarkSelectionDialog.title"));
43     }
44         
45     /**
46      * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
47      */
48     protected Control createDialogArea(Composite parent) {
49
50         Composite composite = new Composite(parent, 0);
51         GridLayout layout = new GridLayout();
52         composite.setLayout(layout);
53         layout.numColumns = 1;
54         layout.verticalSpacing = 1;
55
56         Label label = new Label(composite, SWT.NULL);
57         label.setText(Messages.getString("BookmarkSelectionDialog.text"));
58         
59         this.list = new List(composite, SWT.SINGLE | SWT.BORDER | SWT.V_SCROLL);
60         Bookmark[] bookmarks = BookmarkCollection.getInstance().getBookmarks();
61         
62         for (int i = 0, length = (bookmarks == null) ? 0 : bookmarks.length; i < length; i++) {
63             this.list.add(bookmarks[i].getName());
64             if (i == 0) {
65                 this.list.select(i);
66                 setSelection(bookmarks[i].getName());
67             }
68         }
69
70         final List list = this.list;
71         list.addListener(SWT.Selection, new Listener() {
72                 public void handleEvent (Event event) {
73                     String[] selections = list.getSelection();
74                     if (selections != null && selections.length > 0) {
75                         BookmarkSelectionDialog.this.setSelection(selections[0]);
76                     }
77                 }        
78             });
79
80         GridData full = new GridData();
81         full.horizontalAlignment = GridData.FILL;
82         full.verticalAlignment = GridData.FILL;
83         full.heightHint = convertHeightInCharsToPixels(3);
84         this.list.setLayoutData(full);
85
86         return composite;
87     }
88     
89     /**
90      * @param listener
91      */
92     public synchronized void addPropertyChangeListener(PropertyChangeListener listener) {
93         this.propertyChangeSupport.addPropertyChangeListener(listener);
94     }
95
96     /**
97      * @param listener
98      */
99     public synchronized void removePropertyChangeListener(PropertyChangeListener listener) {
100         this.propertyChangeSupport.removePropertyChangeListener(listener);
101     }
102     
103     private void setSelection(String selection) {
104         String original = this.selection;
105         if (this.selection == null || !this.selection.equals(selection)) {
106             this.selection = selection;
107             this.propertyChangeSupport.firePropertyChange("selection", original, this.selection);
108         }
109     }
110 }