001: package com.quantum.flatfiles.wizard;
002:
003: import java.util.Arrays;
004:
005: import com.quantum.ImageStore;
006: import com.quantum.QuantumPlugin;
007: import com.quantum.flatfiles.MessageUtil;
008: import com.quantum.model.Bookmark;
009: import com.quantum.model.BookmarkCollection;
010: import com.quantum.ui.dialog.ConnectionUtil;
011: import com.quantum.util.DisplayableComparator;
012: import com.quantum.view.widget.SimpleLabelProvider;
013: import com.quantum.wizards.PropertyChangeWizardPage;
014:
015: import org.eclipse.jface.viewers.ISelectionChangedListener;
016: import org.eclipse.jface.viewers.IStructuredContentProvider;
017: import org.eclipse.jface.viewers.IStructuredSelection;
018: import org.eclipse.jface.viewers.SelectionChangedEvent;
019: import org.eclipse.jface.viewers.TableViewer;
020: import org.eclipse.jface.viewers.Viewer;
021: import org.eclipse.swt.SWT;
022: import org.eclipse.swt.events.SelectionAdapter;
023: import org.eclipse.swt.events.SelectionEvent;
024: import org.eclipse.swt.layout.GridData;
025: import org.eclipse.swt.layout.GridLayout;
026: import org.eclipse.swt.widgets.Button;
027: import org.eclipse.swt.widgets.Composite;
028: import org.eclipse.swt.widgets.Label;
029:
030: /**
031: * @author BC Holmes
032: */
033: public class SelectBookmarkWizardPage extends PropertyChangeWizardPage {
034:
035: private Bookmark bookmark;
036: private ConnectionUtil connectionUtil = new ConnectionUtil();
037: private Button connectButton;
038:
039: public class ContentProviderImpl implements
040: IStructuredContentProvider {
041:
042: public Object[] getElements(Object inputElement) {
043: if (inputElement instanceof BookmarkCollection) {
044: Bookmark[] bookmarks = ((BookmarkCollection) inputElement)
045: .getBookmarks();
046: Arrays.sort(bookmarks, new DisplayableComparator());
047: return bookmarks;
048: } else {
049: return null;
050: }
051: }
052:
053: public void dispose() {
054: }
055:
056: public void inputChanged(Viewer viewer, Object oldInput,
057: Object newInput) {
058: }
059: }
060:
061: /**
062: * @param pageName
063: */
064: public SelectBookmarkWizardPage(String pageName) {
065: super (pageName);
066:
067: setTitle(MessageUtil.getString(getClass(), "title"));
068: setDescription(MessageUtil.getString(getClass(), "description"));
069: }
070:
071: public void createControl(Composite parent) {
072:
073: Composite composite = new Composite(parent, SWT.NONE);
074: composite.setLayout(new GridLayout());
075: composite.setLayoutData(new GridData(GridData.FILL_BOTH));
076:
077: Label label = new Label(composite, SWT.NONE);
078: label.setText(MessageUtil.getString(getClass(), "bookmark"));
079:
080: TableViewer viewer = new TableViewer(composite);
081: viewer.setLabelProvider(new SimpleLabelProvider(ImageStore
082: .getImage(ImageStore.BOOKMARK, QuantumPlugin
083: .getDefault())));
084: viewer.setContentProvider(new ContentProviderImpl());
085: viewer.setInput(BookmarkCollection.getInstance());
086: viewer.getControl().setLayoutData(
087: new GridData(GridData.FILL_BOTH));
088:
089: this .connectButton = new Button(composite, SWT.PUSH);
090: this .connectButton.setText(MessageUtil.getString(getClass(),
091: "connect"));
092: this .connectButton.setEnabled(false);
093: this .connectButton.setLayoutData(new GridData(
094: GridData.HORIZONTAL_ALIGN_END));
095:
096: viewer
097: .addSelectionChangedListener(new ISelectionChangedListener() {
098: public void selectionChanged(
099: SelectionChangedEvent event) {
100: IStructuredSelection selection = (IStructuredSelection) event
101: .getSelection();
102: setBookmark(selection.isEmpty() ? null
103: : (Bookmark) selection
104: .getFirstElement());
105: updateState();
106: }
107: });
108:
109: connectButton.addSelectionListener(new SelectionAdapter() {
110: public void widgetSelected(SelectionEvent event) {
111: connectToBookmarkIfNecessary();
112: updateState();
113: }
114: });
115:
116: setControl(composite);
117: updateState();
118: }
119:
120: protected void updateState() {
121: this .connectButton.setEnabled(this .bookmark != null
122: && !this .bookmark.isConnected());
123:
124: boolean pageComplete = (this .bookmark != null && this .bookmark
125: .isConnected());
126: if (this .bookmark != null) {
127: setErrorMessage(this .bookmark.isConnected() ? null
128: : MessageUtil.getString(getClass(), "notConnected"));
129: }
130: setPageComplete(pageComplete);
131: }
132:
133: protected boolean connectToBookmarkIfNecessary() {
134: if (!this .bookmark.isConnected()) {
135: this .connectionUtil
136: .getConnection(this .bookmark, getShell());
137: }
138: return this .bookmark.isConnected();
139: }
140:
141: public Bookmark getBookmark() {
142: return this .bookmark;
143: }
144:
145: public void setBookmark(Bookmark bookmark) {
146: Bookmark original = this .bookmark;
147: this .bookmark = bookmark;
148: firePropertyChange("bookmark", original, bookmark);
149: }
150: }
|