001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package org.terracotta.dso.views;
005:
006: import org.eclipse.core.resources.IProject;
007: import org.eclipse.jdt.core.IClassFile;
008: import org.eclipse.jdt.core.ICompilationUnit;
009: import org.eclipse.jdt.core.IField;
010: import org.eclipse.jdt.core.IJavaElement;
011: import org.eclipse.jdt.core.IMethod;
012: import org.eclipse.jdt.core.IType;
013: import org.eclipse.jdt.internal.ui.packageview.SelectionTransferDropAdapter;
014: import org.eclipse.jdt.internal.ui.util.SelectionUtil;
015: import org.eclipse.jface.dialogs.MessageDialog;
016: import org.eclipse.jface.viewers.ISelection;
017: import org.eclipse.jface.viewers.StructuredViewer;
018: import org.eclipse.swt.dnd.DND;
019: import org.eclipse.swt.dnd.DropTargetEvent;
020: import org.eclipse.swt.widgets.Display;
021: import org.eclipse.swt.widgets.Shell;
022: import org.terracotta.dso.TcPlugin;
023:
024: import java.util.Iterator;
025: import java.util.List;
026:
027: class ConfigTransferDropAdapter extends SelectionTransferDropAdapter {
028: private static final int OPERATION = DND.DROP_LINK;
029: private ConfigViewPart fPart;
030:
031: public ConfigTransferDropAdapter(ConfigViewPart viewPart,
032: StructuredViewer viewer) {
033: super (viewer);
034: setFullWidthMatchesItem(false);
035: fPart = viewPart;
036: }
037:
038: public void validateDrop(Object target, DropTargetEvent event,
039: int operation) {
040: event.detail = DND.DROP_NONE;
041: initializeSelection();
042:
043: IJavaElement[] inputElements = getInputElements(getSelection());
044: if (inputElements != null && inputElements.length > 0) {
045: switch (inputElements[0].getElementType()) {
046: case IJavaElement.FIELD:
047: if (target instanceof RootsWrapper
048: || target instanceof RootWrapper
049: || target instanceof TransientFieldsWrapper
050: || target instanceof TransientFieldWrapper) {
051: event.detail = OPERATION;
052: }
053: break;
054: case IJavaElement.PACKAGE_DECLARATION:
055: case IJavaElement.PACKAGE_FRAGMENT:
056: if (target instanceof AutolocksWrapper
057: || target instanceof AutolockWrapper
058: || target instanceof NamedLocksWrapper
059: || target instanceof NamedLockWrapper
060: || target instanceof IncludesWrapper
061: || target instanceof IncludeWrapper
062: || target instanceof ExcludesWrapper
063: || target instanceof ExcludeWrapper) {
064: event.detail = OPERATION;
065: }
066: break;
067: case IJavaElement.METHOD:
068: if (target instanceof DistributedMethodsWrapper
069: || target instanceof DistributedMethodWrapper
070: || target instanceof AutolocksWrapper
071: || target instanceof AutolockWrapper
072: || target instanceof NamedLocksWrapper
073: || target instanceof NamedLockWrapper) {
074: event.detail = OPERATION;
075: }
076: break;
077: case IJavaElement.CLASS_FILE:
078: case IJavaElement.COMPILATION_UNIT:
079: case IJavaElement.TYPE:
080: if (target instanceof AdditionalBootJarClassesWrapper
081: || target instanceof AutolocksWrapper
082: || target instanceof AutolockWrapper
083: || target instanceof NamedLocksWrapper
084: || target instanceof NamedLockWrapper
085: || target instanceof IncludesWrapper
086: || target instanceof IncludeWrapper
087: || target instanceof ExcludesWrapper
088: || target instanceof ExcludeWrapper) {
089: event.detail = OPERATION;
090: }
091: break;
092: }
093: }
094: }
095:
096: public boolean isEnabled(DropTargetEvent event) {
097: return true;
098: }
099:
100: public void drop(Object target, DropTargetEvent event) {
101: List list = SelectionUtil.toList(getSelection());
102: IProject project = fPart.m_javaProject.getProject();
103: TcPlugin plugin = TcPlugin.getDefault();
104:
105: if (plugin.getConfiguration(project) == TcPlugin.BAD_CONFIG) {
106: Shell shell = Display.getDefault().getActiveShell();
107: String title = "Terracotta Plugin";
108: String msg = "The configuration source is not parsable and cannot be\n used until these errors are resolved.";
109:
110: MessageDialog.openWarning(shell, title, msg);
111: try {
112: plugin.openConfigurationEditor(project);
113: } catch (Exception e) {
114: // TODO:
115: }
116: return;
117: }
118:
119: IJavaElement element = (IJavaElement) list.get(0);
120: switch (element.getElementType()) {
121: case IJavaElement.FIELD:
122: if (target instanceof RootsWrapper
123: || target instanceof RootWrapper) {
124: fPart.addRoots((IField[]) list.toArray(new IField[0]));
125: } else if (target instanceof TransientFieldsWrapper
126: || target instanceof TransientFieldWrapper) {
127: fPart.addTransientFields((IField[]) list
128: .toArray(new IField[0]));
129: }
130: break;
131: case IJavaElement.PACKAGE_DECLARATION:
132: case IJavaElement.PACKAGE_FRAGMENT:
133: if (target instanceof AutolocksWrapper
134: || target instanceof AutolockWrapper) {
135: fPart.addAutolocks((IJavaElement[]) list
136: .toArray(new IJavaElement[0]));
137: } else if (target instanceof NamedLocksWrapper
138: || target instanceof NamedLockWrapper) {
139: fPart.addNamedLocks((IJavaElement[]) list
140: .toArray(new IJavaElement[0]));
141: } else if (target instanceof IncludesWrapper
142: || target instanceof IncludeWrapper) {
143: fPart.addIncludes((IJavaElement[]) list
144: .toArray(new IJavaElement[0]));
145: } else if (target instanceof ExcludesWrapper
146: || target instanceof ExcludeWrapper) {
147: fPart.addExcludes((IJavaElement[]) list
148: .toArray(new IJavaElement[0]));
149: }
150: break;
151: case IJavaElement.METHOD:
152: if (target instanceof DistributedMethodsWrapper
153: || target instanceof DistributedMethodWrapper) {
154: fPart.addDistributedMethods((IMethod[]) list
155: .toArray(new IMethod[0]));
156: } else if (target instanceof AutolocksWrapper
157: || target instanceof AutolockWrapper) {
158: fPart.addAutolocks((IMethod[]) list
159: .toArray(new IMethod[0]));
160: } else if (target instanceof NamedLocksWrapper
161: || target instanceof NamedLockWrapper) {
162: fPart.addNamedLocks((IMethod[]) list
163: .toArray(new IMethod[0]));
164: }
165: break;
166: case IJavaElement.CLASS_FILE:
167: try {
168: if (target instanceof AdditionalBootJarClassesWrapper) {
169: IType[] types = new IType[list.size()];
170: for (int i = 0; i < list.size(); i++) {
171: types[i] = ((IClassFile) list.get(i)).getType();
172: }
173: fPart.addAdditionalBootJarClasses(types);
174: } else if (target instanceof AutolocksWrapper
175: || target instanceof AutolockWrapper) {
176: fPart.addAutolocks((IJavaElement[]) list
177: .toArray(new IJavaElement[0]));
178: } else if (target instanceof NamedLocksWrapper
179: || target instanceof NamedLockWrapper) {
180: fPart.addNamedLocks((IJavaElement[]) list
181: .toArray(new IJavaElement[0]));
182: } else if (target instanceof IncludesWrapper
183: || target instanceof IncludeWrapper) {
184: fPart.addIncludes((IJavaElement[]) list
185: .toArray(new IJavaElement[0]));
186: } else if (target instanceof ExcludesWrapper
187: || target instanceof ExcludeWrapper) {
188: fPart.addExcludes((IJavaElement[]) list
189: .toArray(new IJavaElement[0]));
190: }
191: } catch (Exception e) {/**/
192: }
193: break;
194: case IJavaElement.COMPILATION_UNIT:
195: if (target instanceof AdditionalBootJarClassesWrapper) {
196: IType[] types = new IType[list.size()];
197: for (int i = 0; i < list.size(); i++) {
198: types[i] = ((ICompilationUnit) list.get(i))
199: .findPrimaryType();
200: }
201: fPart.addAdditionalBootJarClasses(types);
202: } else if (target instanceof AutolocksWrapper
203: || target instanceof AutolockWrapper) {
204: fPart.addAutolocks((IJavaElement[]) list
205: .toArray(new IJavaElement[0]));
206: } else if (target instanceof NamedLocksWrapper
207: || target instanceof NamedLockWrapper) {
208: fPart.addNamedLocks((IJavaElement[]) list
209: .toArray(new IJavaElement[0]));
210: } else if (target instanceof IncludesWrapper
211: || target instanceof IncludeWrapper) {
212: fPart.addIncludes((IJavaElement[]) list
213: .toArray(new IJavaElement[0]));
214: } else if (target instanceof ExcludesWrapper
215: || target instanceof ExcludeWrapper) {
216: fPart.addExcludes((IJavaElement[]) list
217: .toArray(new IJavaElement[0]));
218: }
219: break;
220: case IJavaElement.TYPE:
221: if (target instanceof AdditionalBootJarClassesWrapper) {
222: fPart.addAdditionalBootJarClasses((IType[]) list
223: .toArray(new IType[0]));
224: } else if (target instanceof AutolocksWrapper
225: || target instanceof AutolockWrapper) {
226: fPart
227: .addAutolocks((IType[]) list
228: .toArray(new IType[0]));
229: } else if (target instanceof NamedLocksWrapper
230: || target instanceof NamedLockWrapper) {
231: fPart.addNamedLocks((IType[]) list
232: .toArray(new IType[0]));
233: } else if (target instanceof IncludesWrapper
234: || target instanceof IncludeWrapper) {
235: fPart.addIncludes((IType[]) list.toArray(new IType[0]));
236: } else if (target instanceof ExcludesWrapper
237: || target instanceof ExcludeWrapper) {
238: fPart.addExcludes((IType[]) list.toArray(new IType[0]));
239: }
240: break;
241: }
242: }
243:
244: private static IJavaElement[] getInputElements(ISelection selection) {
245: List list = SelectionUtil.toList(selection);
246: if (list == null)
247: return null;
248: return getCandidates(list);
249: }
250:
251: public static IJavaElement[] getCandidates(List input) {
252: Iterator iter = input.iterator();
253: int type = -1;
254:
255: while (iter.hasNext()) {
256: Object element = iter.next();
257: if (!(element instanceof IJavaElement)) {
258: return null;
259: }
260: IJavaElement javaElement = (IJavaElement) element;
261: int elementType = javaElement.getElementType();
262: if (type == -1) {
263: type = elementType;
264: } else if (type != elementType) {
265: return null;
266: }
267: }
268:
269: return (IJavaElement[]) input.toArray(new IJavaElement[0]);
270: }
271: }
|