1 5 package org.terracotta.dso.views; 6 7 import org.eclipse.jdt.internal.ui.util.SelectionUtil; 8 import org.eclipse.jface.action.Action; 9 import org.eclipse.jface.action.ActionContributionItem; 10 import org.eclipse.jface.action.IMenuCreator; 11 import org.eclipse.jface.action.Separator; 12 import org.eclipse.jface.dialogs.IDialogConstants; 13 import org.eclipse.jface.dialogs.MessageDialog; 14 import org.eclipse.jface.viewers.ISelection; 15 import org.eclipse.jface.viewers.ISelectionProvider; 16 import org.eclipse.swt.SWT; 17 import org.eclipse.swt.layout.GridData; 18 import org.eclipse.swt.widgets.Composite; 19 import org.eclipse.swt.widgets.Control; 20 import org.eclipse.swt.widgets.Menu; 21 import org.eclipse.swt.widgets.Shell; 22 import org.eclipse.swt.widgets.Text; 23 import org.eclipse.ui.actions.ActionContext; 24 import org.terracotta.dso.actions.ActionUtil; 25 26 import com.terracottatech.config.OnLoad; 27 28 public class OnLoadAction extends Action implements IMenuCreator { 29 ConfigViewPart fPart; 30 Action fNoop; 31 Action fExecute; 32 Action fMethod; 33 Action fEdit; 34 35 OnLoadAction(ConfigViewPart part) { 36 super("On load", AS_DROP_DOWN_MENU); 37 setMenuCreator(this); 38 fPart = part; 39 40 fNoop = new Action("Do nothing", AS_RADIO_BUTTON) { 41 public void run() { 42 if(isChecked()) { 43 fPart.setOnLoad(OnLoadAction.this, ""); 44 } 45 } 46 }; 47 fExecute = new Action("Execute code", AS_RADIO_BUTTON) { 48 public void run() { 49 if(isChecked()) { 50 OnLoadAction.this.run(); 51 fEdit.run(); 52 } 53 } 54 }; 55 fMethod = new Action("Call method", AS_RADIO_BUTTON) { 56 public void run() { 57 if(isChecked()) { 58 OnLoadAction.this.run(); 59 fEdit.run(); 60 } 61 } 62 }; 63 64 fEdit = new Action("Edit...") { 65 public void run() { 66 IncludeWrapper wrapper = (IncludeWrapper)SelectionUtil.getSingleElement(getSelection()); 67 boolean isExecute = isExecute(); 68 Shell shell = ActionUtil.findSelectedEditorPart().getSite().getShell(); 69 OnLoadDialog dialog = new OnLoadDialog(shell, wrapper, isExecute); 70 try { 71 if(dialog.open() == IDialogConstants.OK_ID) { 72 fPart.setOnLoad(OnLoadAction.this, dialog.getResult()); 73 } 74 } catch(Throwable t) { 75 t.printStackTrace(); 76 } 77 } 78 }; 79 } 80 81 public void run() { 82 } 84 85 boolean isNoop() {return fNoop.isChecked();} 86 boolean isExecute() {return fExecute.isChecked();} 87 boolean isMethod() {return fMethod.isChecked();} 88 89 public void setContext(ActionContext context) { 90 Object element = SelectionUtil.getSingleElement(getSelection()); 91 92 if(element instanceof IncludeWrapper) { 93 IncludeWrapper wrapper = (IncludeWrapper)element; 94 95 fNoop.setChecked(!wrapper.isSetOnLoad()); 96 fExecute.setChecked(wrapper.isSetOnLoadExecute()); 97 fMethod.setChecked(wrapper.isSetOnLoadMethod()); 98 99 fEdit.setEnabled(wrapper.isSetOnLoad()); 100 } 101 } 102 103 public boolean canActionBeAdded() { 104 Object element = SelectionUtil.getSingleElement(getSelection()); 105 return element instanceof IncludeWrapper; 106 } 107 108 private ISelection getSelection() { 109 ISelectionProvider provider = fPart.getSite().getSelectionProvider(); 110 111 if(provider != null) { 112 return provider.getSelection(); 113 } 114 115 return null; 116 } 117 118 public void dispose() { 119 120 } 121 122 private void fillMenu(Menu menu) { 123 ActionContributionItem item; 124 125 item = new ActionContributionItem(fNoop); 126 item.fill(menu, -1); 127 128 item = new ActionContributionItem(fExecute); 129 item.fill(menu, -1); 130 131 item = new ActionContributionItem(fMethod); 132 item.fill(menu, -1); 133 134 new Separator().fill(menu, -1); 135 136 item = new ActionContributionItem(fEdit); 137 item.fill(menu, -1); 138 } 139 140 public Menu getMenu(Control parent) { 141 Menu menu = new Menu(parent); 142 fillMenu(menu); 143 return menu; 144 } 145 146 public Menu getMenu(Menu parent) { 147 Menu menu = new Menu(parent); 148 fillMenu(menu); 149 return menu; 150 } 151 152 class OnLoadDialog extends MessageDialog { 153 IncludeWrapper fWrapper; 154 boolean fIsExecute; 155 Text fText; 156 String fResult; 157 158 OnLoadDialog(Shell shell, IncludeWrapper wrapper, boolean isExecute) { 159 super(shell, "OnLoad: "+wrapper, null, 160 isExecute() ? "Execute script" : "Call method", 161 MessageDialog.NONE, 162 new String [] {IDialogConstants.OK_LABEL, 163 IDialogConstants.CANCEL_LABEL}, 0); 164 fWrapper = wrapper; 165 fIsExecute = isExecute; 166 } 167 168 protected Control createCustomArea(Composite parent) { 169 int flags = fIsExecute ? SWT.MULTI | SWT.V_SCROLL : 0; 170 fText = new Text(parent, flags | SWT.BORDER); 171 GridData gd = new GridData(GridData.FILL_BOTH); 172 if(fIsExecute) gd.heightHint = 4 * fText.getLineHeight(); 173 fText.setLayoutData(gd); 174 OnLoad onload = fWrapper.ensureOnLoad(); 175 String text = fIsExecute ? onload.getExecute() : onload.getMethod(); 176 fText.setText(text == null ? "" : text); 177 fText.selectAll(); 178 return fText; 179 } 180 181 public boolean close() { 182 fResult = fText.getText().trim(); 183 return super.close(); 184 } 185 186 String getResult() { 187 return fResult; 188 } 189 } 190 } 191
| Popular Tags
|