001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package org.terracotta.dso.editors.chooser;
006:
007: import org.eclipse.core.resources.IProject;
008: import org.eclipse.jface.dialogs.IDialogConstants;
009: import org.eclipse.jface.dialogs.MessageDialog;
010: import org.eclipse.swt.SWT;
011: import org.eclipse.swt.events.KeyAdapter;
012: import org.eclipse.swt.events.KeyEvent;
013: import org.eclipse.swt.events.ModifyEvent;
014: import org.eclipse.swt.events.ModifyListener;
015: import org.eclipse.swt.events.SelectionAdapter;
016: import org.eclipse.swt.events.SelectionEvent;
017: import org.eclipse.swt.events.SelectionListener;
018: import org.eclipse.swt.layout.GridData;
019: import org.eclipse.swt.layout.GridLayout;
020: import org.eclipse.swt.widgets.Button;
021: import org.eclipse.swt.widgets.Composite;
022: import org.eclipse.swt.widgets.Control;
023: import org.eclipse.swt.widgets.List;
024: import org.eclipse.swt.widgets.Shell;
025: import org.eclipse.swt.widgets.Text;
026: import org.terracotta.ui.util.SWTUtil;
027:
028: import com.tc.util.event.EventMulticaster;
029: import com.tc.util.event.UpdateEvent;
030: import com.tc.util.event.UpdateEventListener;
031:
032: public class ExpressionChooser extends MessageDialog {
033:
034: private static final String EXPLORE = "Explore...";
035: private static final String ADD = "Add";
036: private final Shell m_parentShell;
037: private final IProject m_project;
038: private final EventMulticaster m_valueListener;
039: private Shell m_shell;
040: private boolean m_isAddButton;
041: private SelectionListener m_exploreListener;
042: private SelectionListener m_addListener;
043: private Layout m_layout;
044: private NavigatorBehavior m_behavior;
045:
046: public ExpressionChooser(Shell shell, String title, String message,
047: IProject project, NavigatorBehavior behavior) {
048: super (shell, title, null, message, MessageDialog.NONE,
049: new String[] { IDialogConstants.OK_LABEL,
050: IDialogConstants.CANCEL_LABEL }, 0);
051: this .m_parentShell = shell;
052: this .m_project = project;
053: this .m_valueListener = new EventMulticaster();
054: this .m_behavior = behavior;
055: }
056:
057: protected void configureShell(Shell shell) {
058: super .configureShell(shell);
059: m_shell = shell;
060: m_shell.setSize(400, 250);
061: SWTUtil.placeDialogInCenter(m_parentShell, m_shell);
062: }
063:
064: protected Control createCustomArea(Composite parent) {
065: initLayout(m_layout = new Layout(parent));
066: return parent;
067: }
068:
069: private void initLayout(final Layout layout) {
070: layout.m_exploreButton
071: .addSelectionListener(m_exploreListener = new SelectionAdapter() {
072: public void widgetSelected(SelectionEvent e) {
073: PackageNavigator dialog = new PackageNavigator(
074: getShell(), m_behavior.getTitle(),
075: m_project, m_behavior);
076: dialog
077: .addValueListener(new UpdateEventListener() {
078: public void handleUpdate(
079: UpdateEvent arg) {
080: String[] values = (String[]) arg.data;
081: for (int i = 0; i < values.length; i++) {
082: layout.m_list
083: .add(values[i]);
084: layout.m_list.forceFocus();
085: }
086: }
087: });
088: dialog.open();
089: }
090: });
091: m_addListener = new SelectionAdapter() {
092: public void widgetSelected(SelectionEvent e) {
093: layout.m_list.add(layout.m_selectField.getText());
094: layout.m_selectField.setText("");
095: }
096: };
097: layout.m_selectField.addModifyListener(new ModifyListener() {
098: public void modifyText(ModifyEvent e) {
099: if (((Text) e.widget).getText().trim().equals("")) {
100: layout.m_exploreButton
101: .removeSelectionListener(m_addListener);
102: layout.m_exploreButton
103: .addSelectionListener(m_exploreListener);
104: layout.m_exploreButton.setText(EXPLORE);
105: SWTUtil
106: .applyDefaultButtonSize(layout.m_exploreButton);
107: m_shell
108: .setDefaultButton(getButton(IDialogConstants.OK_ID));
109: m_isAddButton = false;
110: } else if (!m_isAddButton) {
111: layout.m_exploreButton
112: .removeSelectionListener(m_exploreListener);
113: layout.m_exploreButton
114: .addSelectionListener(m_addListener);
115: layout.m_exploreButton.setText(ADD);
116: SWTUtil
117: .applyDefaultButtonSize(layout.m_exploreButton);
118: m_shell.setDefaultButton(layout.m_exploreButton);
119: m_isAddButton = true;
120: }
121: }
122: });
123: layout.m_list.addKeyListener(new KeyAdapter() {
124: public void keyPressed(KeyEvent e) {
125: if (e.keyCode == SWT.DEL || e.keyCode == SWT.BS) {
126: int[] selected = ((List) e.getSource())
127: .getSelectionIndices();
128: layout.m_list.remove(selected);
129: layout.m_selectField.forceFocus();
130: }
131: }
132: });
133: }
134:
135: protected void buttonPressed(int buttonId) {
136: if (buttonId == IDialogConstants.OK_ID) {
137: m_valueListener.fireUpdateEvent(new UpdateEvent(
138: m_layout.m_list.getItems()));
139: }
140: super .buttonPressed(buttonId);
141: }
142:
143: public void addValueListener(UpdateEventListener listener) {
144: m_valueListener.addListener(listener);
145: }
146:
147: // --------------------------------------------------------------------------------
148:
149: private class Layout {
150: final Text m_selectField;
151: final Button m_exploreButton;
152: final List m_list;
153: GridData m_gridData;
154:
155: private Layout(Composite parent) {
156: Composite comp = new Composite(parent, SWT.NONE);
157:
158: GridLayout gridLayout = new GridLayout(2, false);
159: gridLayout.marginWidth = gridLayout.marginHeight = 0;
160: comp.setLayout(gridLayout);
161: comp.setLayoutData(new GridData(GridData.FILL_BOTH));
162:
163: this .m_selectField = new Text(comp, SWT.SINGLE | SWT.BORDER);
164: m_selectField.setLayoutData(new GridData(
165: GridData.FILL_HORIZONTAL));
166:
167: this .m_exploreButton = new Button(comp, SWT.PUSH);
168: m_exploreButton.setText(EXPLORE);
169: SWTUtil.applyDefaultButtonSize(m_exploreButton);
170:
171: this .m_list = new List(comp, SWT.BORDER | SWT.MULTI
172: | SWT.V_SCROLL);
173: m_gridData = new GridData(GridData.FILL_BOTH);
174: m_gridData.horizontalSpan = 2;
175: m_list.setLayoutData(m_gridData);
176: }
177: }
178: }
|