001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package org.terracotta.dso.views;
006:
007: import org.eclipse.jdt.internal.ui.util.SelectionUtil;
008: import org.eclipse.jface.action.Action;
009: import org.eclipse.jface.action.ActionContributionItem;
010: import org.eclipse.jface.action.IMenuCreator;
011: import org.eclipse.jface.action.Separator;
012: import org.eclipse.jface.dialogs.IDialogConstants;
013: import org.eclipse.jface.dialogs.MessageDialog;
014: import org.eclipse.jface.viewers.ISelection;
015: import org.eclipse.jface.viewers.ISelectionProvider;
016: import org.eclipse.swt.SWT;
017: import org.eclipse.swt.layout.GridData;
018: import org.eclipse.swt.widgets.Composite;
019: import org.eclipse.swt.widgets.Control;
020: import org.eclipse.swt.widgets.Menu;
021: import org.eclipse.swt.widgets.Shell;
022: import org.eclipse.swt.widgets.Text;
023: import org.eclipse.ui.actions.ActionContext;
024: import org.terracotta.dso.actions.ActionUtil;
025:
026: import com.terracottatech.config.OnLoad;
027:
028: public class OnLoadAction extends Action implements IMenuCreator {
029: ConfigViewPart fPart;
030: Action fNoop;
031: Action fExecute;
032: Action fMethod;
033: Action fEdit;
034:
035: OnLoadAction(ConfigViewPart part) {
036: super ("On load", AS_DROP_DOWN_MENU);
037: setMenuCreator(this );
038: fPart = part;
039:
040: fNoop = new Action("Do nothing", AS_RADIO_BUTTON) {
041: public void run() {
042: if (isChecked()) {
043: fPart.setOnLoad(OnLoadAction.this , "");
044: }
045: }
046: };
047: fExecute = new Action("Execute code", AS_RADIO_BUTTON) {
048: public void run() {
049: if (isChecked()) {
050: OnLoadAction.this .run();
051: fEdit.run();
052: }
053: }
054: };
055: fMethod = new Action("Call method", AS_RADIO_BUTTON) {
056: public void run() {
057: if (isChecked()) {
058: OnLoadAction.this .run();
059: fEdit.run();
060: }
061: }
062: };
063:
064: fEdit = new Action("Edit...") {
065: public void run() {
066: IncludeWrapper wrapper = (IncludeWrapper) SelectionUtil
067: .getSingleElement(getSelection());
068: boolean isExecute = isExecute();
069: Shell shell = ActionUtil.findSelectedEditorPart()
070: .getSite().getShell();
071: OnLoadDialog dialog = new OnLoadDialog(shell, wrapper,
072: isExecute);
073: try {
074: if (dialog.open() == IDialogConstants.OK_ID) {
075: fPart.setOnLoad(OnLoadAction.this , dialog
076: .getResult());
077: }
078: } catch (Throwable t) {
079: t.printStackTrace();
080: }
081: }
082: };
083: }
084:
085: public void run() {
086: //fPart.setOnLoad(this, "");
087: }
088:
089: boolean isNoop() {
090: return fNoop.isChecked();
091: }
092:
093: boolean isExecute() {
094: return fExecute.isChecked();
095: }
096:
097: boolean isMethod() {
098: return fMethod.isChecked();
099: }
100:
101: public void setContext(ActionContext context) {
102: Object element = SelectionUtil.getSingleElement(getSelection());
103:
104: if (element instanceof IncludeWrapper) {
105: IncludeWrapper wrapper = (IncludeWrapper) element;
106:
107: fNoop.setChecked(!wrapper.isSetOnLoad());
108: fExecute.setChecked(wrapper.isSetOnLoadExecute());
109: fMethod.setChecked(wrapper.isSetOnLoadMethod());
110:
111: fEdit.setEnabled(wrapper.isSetOnLoad());
112: }
113: }
114:
115: public boolean canActionBeAdded() {
116: Object element = SelectionUtil.getSingleElement(getSelection());
117: return element instanceof IncludeWrapper;
118: }
119:
120: private ISelection getSelection() {
121: ISelectionProvider provider = fPart.getSite()
122: .getSelectionProvider();
123:
124: if (provider != null) {
125: return provider.getSelection();
126: }
127:
128: return null;
129: }
130:
131: public void dispose() {
132: /**/
133: }
134:
135: private void fillMenu(Menu menu) {
136: ActionContributionItem item;
137:
138: item = new ActionContributionItem(fNoop);
139: item.fill(menu, -1);
140:
141: item = new ActionContributionItem(fExecute);
142: item.fill(menu, -1);
143:
144: item = new ActionContributionItem(fMethod);
145: item.fill(menu, -1);
146:
147: new Separator().fill(menu, -1);
148:
149: item = new ActionContributionItem(fEdit);
150: item.fill(menu, -1);
151: }
152:
153: public Menu getMenu(Control parent) {
154: Menu menu = new Menu(parent);
155: fillMenu(menu);
156: return menu;
157: }
158:
159: public Menu getMenu(Menu parent) {
160: Menu menu = new Menu(parent);
161: fillMenu(menu);
162: return menu;
163: }
164:
165: class OnLoadDialog extends MessageDialog {
166: IncludeWrapper fWrapper;
167: boolean fIsExecute;
168: Text fText;
169: String fResult;
170:
171: OnLoadDialog(Shell shell, IncludeWrapper wrapper,
172: boolean isExecute) {
173: super (shell, "OnLoad: " + wrapper, null,
174: isExecute() ? "Execute script" : "Call method",
175: MessageDialog.NONE, new String[] {
176: IDialogConstants.OK_LABEL,
177: IDialogConstants.CANCEL_LABEL }, 0);
178: fWrapper = wrapper;
179: fIsExecute = isExecute;
180: }
181:
182: protected Control createCustomArea(Composite parent) {
183: int flags = fIsExecute ? SWT.MULTI | SWT.V_SCROLL : 0;
184: fText = new Text(parent, flags | SWT.BORDER);
185: GridData gd = new GridData(GridData.FILL_BOTH);
186: if (fIsExecute)
187: gd.heightHint = 4 * fText.getLineHeight();
188: fText.setLayoutData(gd);
189: OnLoad onload = fWrapper.ensureOnLoad();
190: String text = fIsExecute ? onload.getExecute() : onload
191: .getMethod();
192: fText.setText(text == null ? "" : text);
193: fText.selectAll();
194: return fText;
195: }
196:
197: public boolean close() {
198: fResult = fText.getText().trim();
199: return super .close();
200: }
201:
202: String getResult() {
203: return fResult;
204: }
205: }
206: }
|