001: package nz.ac.massey.take.takeep.actionsSets.panels;
002:
003: import java.util.LinkedList;
004:
005: import nz.ac.massey.take.takeep.actionsSets.wizards.TakeCompilerWizard;
006: import nz.ac.massey.take.takeep.editor.TakeEditor;
007:
008: import org.eclipse.core.resources.IFolder;
009: import org.eclipse.core.resources.IProject;
010: import org.eclipse.core.resources.IResource;
011: import org.eclipse.core.runtime.IAdaptable;
012: import org.eclipse.core.runtime.IPath;
013: import org.eclipse.jdt.core.IMember;
014: import org.eclipse.jdt.core.IPackageFragment;
015: import org.eclipse.jdt.core.JavaCore;
016: import org.eclipse.jdt.internal.ui.wizards.TypedElementSelectionValidator;
017: import org.eclipse.jdt.internal.ui.wizards.TypedViewerFilter;
018: import org.eclipse.jdt.ui.IJavaElementSearchConstants;
019: import org.eclipse.jdt.ui.JavaUI;
020: import org.eclipse.jface.dialogs.IDialogConstants;
021: import org.eclipse.jface.viewers.ILabelProvider;
022: import org.eclipse.jface.viewers.ISelection;
023: import org.eclipse.jface.viewers.IStructuredSelection;
024: import org.eclipse.jface.viewers.ITreeContentProvider;
025: import org.eclipse.jface.viewers.ViewerFilter;
026: import org.eclipse.jface.window.Window;
027: import org.eclipse.jface.wizard.WizardPage;
028: import org.eclipse.swt.SWT;
029: import org.eclipse.swt.events.SelectionEvent;
030: import org.eclipse.swt.events.SelectionListener;
031: import org.eclipse.swt.layout.GridData;
032: import org.eclipse.swt.layout.GridLayout;
033: import org.eclipse.swt.widgets.Button;
034: import org.eclipse.swt.widgets.Composite;
035: import org.eclipse.swt.widgets.Event;
036: import org.eclipse.swt.widgets.Label;
037: import org.eclipse.swt.widgets.List;
038: import org.eclipse.swt.widgets.Listener;
039: import org.eclipse.swt.widgets.Text;
040: import org.eclipse.ui.IEditorInput;
041: import org.eclipse.ui.IEditorPart;
042: import org.eclipse.ui.IFileEditorInput;
043: import org.eclipse.ui.IWorkbench;
044: import org.eclipse.ui.IWorkbenchPage;
045: import org.eclipse.ui.IWorkbenchWindow;
046: import org.eclipse.ui.PlatformUI;
047: import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
048: import org.eclipse.ui.dialogs.ISelectionStatusValidator;
049: import org.eclipse.ui.dialogs.SelectionDialog;
050: import org.eclipse.ui.model.WorkbenchContentProvider;
051: import org.eclipse.ui.model.WorkbenchLabelProvider;
052: import org.eclipse.ui.views.navigator.ResourceComparator;
053:
054: public class TakeCompileWizardPanel extends WizardPage {
055:
056: public TakeCompileWizardPanel(String name) {
057: super (name);
058: // TODO Auto-generated constructor stub
059: }
060:
061: private String packageName = "generated.nz.ac";
062: private String className = "KB";
063:
064: private String sourceOutputLocation = "src";
065:
066: private boolean includeTakeLibrary = false;
067: private String includeTakeLibraryLocation = "lib";
068:
069: private LinkedList<String> importStatements = new LinkedList<String>();
070:
071: private LinkedList<String> additionalInterfaces = new LinkedList<String>();
072:
073: private boolean autoAnotate = true;
074: private boolean sourceTransform = true;
075:
076: @Override
077: public void createControl(Composite parent) {
078:
079: initializeDialogUnits(parent);
080:
081: Composite composite = new Composite(parent, SWT.NONE);
082: composite.setFont(parent.getFont());
083:
084: int nColumns = 4;
085:
086: GridLayout layout = new GridLayout();
087: layout.numColumns = nColumns;
088: composite.setLayout(layout);
089:
090: addNamingPanel(composite, nColumns);
091: addLocationPanel(composite, nColumns);
092:
093: addImportPanel(composite, nColumns);
094:
095: addInterfacePanel(composite, nColumns);
096:
097: addCheckPanel(composite, nColumns);
098:
099: this .setControl(composite);
100: }
101:
102: private void addNamingPanel(Composite parent, int numcols) {
103: Composite topLevel = new Composite(parent, SWT.NONE);
104: topLevel.setLayout(new GridLayout(3, false));
105: topLevel.setLayoutData(new GridData(GridData.FILL,
106: GridData.CENTER, true, false, numcols, 1));
107:
108: Label pnLbl = new Label(topLevel, SWT.CENTER);
109: pnLbl.setText("Package Name");
110:
111: final Text pnTB = new Text(topLevel, SWT.SINGLE);
112: pnTB.setText(this .packageName);
113: pnTB.setLayoutData(new GridData(GridData.FILL, GridData.CENTER,
114: true, false));
115: pnTB.addListener(SWT.Modify, new Listener() {
116:
117: @Override
118: public void handleEvent(Event event) {
119: TakeCompileWizardPanel.this .packageName = pnTB
120: .getText();
121:
122: }
123: });
124:
125: final IWorkbenchPage iworkbenchpage = getWorkbench();
126: if (iworkbenchpage == null)
127: return;
128:
129: Button pnbtn = new Button(topLevel, SWT.PUSH);
130: pnbtn.setText("Browse");
131:
132: pnbtn.addSelectionListener(new SelectionListener() {
133:
134: @Override
135: public void widgetDefaultSelected(SelectionEvent e) {
136: }
137:
138: @Override
139: public void widgetSelected(SelectionEvent e) {
140: try {
141: SelectionDialog dialog = JavaUI
142: .createPackageDialog(
143: getShell(),
144: JavaCore
145: .create(getProjectFromWorkbench(iworkbenchpage)),
146: SWT.NONE);
147:
148: if (dialog.open() == IDialogConstants.CANCEL_ID)
149: return;
150:
151: Object[] types = dialog.getResult();
152: if (types == null || types.length == 0)
153: return;
154:
155: for (Object o : types) {
156: // org.eclipse.jdt.internal.core.PackageFragment
157: String elementName = ((IPackageFragment) o)
158: .getElementName();
159: pnTB.setText(elementName);
160: TakeCompileWizardPanel.this .packageName = elementName;
161: }
162:
163: } catch (Exception e1) {
164: // TODO Auto-generated catch block
165: e1.printStackTrace();
166: }
167:
168: }
169: });
170:
171: Label cnLbl = new Label(topLevel, SWT.CENTER);
172: cnLbl.setText("Class Name");
173:
174: final Text cnTB = new Text(topLevel, SWT.SINGLE);
175: cnTB.setText(this .className);
176: cnTB.setLayoutData(new GridData(GridData.FILL, GridData.CENTER,
177: true, false));
178: cnTB.addListener(SWT.Modify, new Listener() {
179:
180: @Override
181: public void handleEvent(Event event) {
182: TakeCompileWizardPanel.this .className = cnTB.getText();
183:
184: }
185: });
186: }
187:
188: private void addLocationPanel(Composite parent, int numcols) {
189: Composite topLevel = new Composite(parent, SWT.NONE);
190: topLevel.setLayout(new GridLayout(3, false));
191: topLevel.setLayoutData(new GridData(GridData.FILL,
192: GridData.CENTER, true, false, numcols, 1));
193:
194: Label lLbl = new Label(topLevel, SWT.CENTER);
195: lLbl.setText("Source Output Location Folder");
196:
197: final Text lTB = new Text(topLevel, SWT.SINGLE);
198: lTB.setText(this .sourceOutputLocation);
199: lTB.setLayoutData(new GridData(GridData.FILL, GridData.CENTER,
200: true, false));
201:
202: lTB.addListener(SWT.Modify, new Listener() {
203:
204: @Override
205: public void handleEvent(Event event) {
206: TakeCompileWizardPanel.this .sourceOutputLocation = lTB
207: .getText();
208:
209: }
210: });
211:
212: IWorkbenchPage iworkbenchpage = getWorkbench();
213: if (iworkbenchpage == null)
214: return;
215:
216: final IProject project = getProjectFromWorkbench(iworkbenchpage);
217:
218: Button lcbtn = new Button(topLevel, SWT.PUSH);
219: lcbtn.setText("Browse");
220:
221: lcbtn.addSelectionListener(new SelectionListener() {
222:
223: @Override
224: public void widgetDefaultSelected(SelectionEvent e) {
225: }
226:
227: @Override
228: public void widgetSelected(SelectionEvent e) {
229: try {
230:
231: IFolder chooseFolder = chooseFolder(project,
232: "Title", "message", project.getLocation());
233: if (chooseFolder == null)
234: return;
235: TakeCompileWizardPanel.this .sourceOutputLocation = chooseFolder
236: .getProjectRelativePath().toString();
237: lTB
238: .setText(TakeCompileWizardPanel.this .sourceOutputLocation);
239:
240: } catch (Exception e1) {
241: e1.printStackTrace();
242: }
243:
244: }
245: });
246:
247: try {
248: TakeEditor.getProjectClassLoader(JavaCore.create(project))
249: .loadClass("nz.org.take.rt.AbstractIterator");
250: } catch (ClassNotFoundException e1) {
251:
252: includeTakeLibrary = true;
253: final Button ilcbtn = new Button(topLevel, SWT.CHECK);
254: ilcbtn.setText("Install Take Runtime Library");
255: ilcbtn.setSelection(includeTakeLibrary);
256:
257: final Text ilTB = new Text(topLevel, SWT.SINGLE);
258: ilTB.setText(this .includeTakeLibraryLocation);
259: ilTB.setLayoutData(new GridData(GridData.FILL,
260: GridData.CENTER, true, false));
261:
262: ilTB.addListener(SWT.Modify, new Listener() {
263:
264: @Override
265: public void handleEvent(Event event) {
266: TakeCompileWizardPanel.this .includeTakeLibraryLocation = ilTB
267: .getText();
268:
269: }
270: });
271:
272: final Button ilbtn = new Button(topLevel, SWT.PUSH);
273: ilbtn.setText("Browse");
274:
275: Label note = new Label(topLevel, SWT.LEFT);
276: note.setText("Note: manual addition to classpath needed");
277:
278: ilbtn.addSelectionListener(new SelectionListener() {
279:
280: @Override
281: public void widgetDefaultSelected(SelectionEvent e) {
282: }
283:
284: @Override
285: public void widgetSelected(SelectionEvent e) {
286: try {
287:
288: IFolder chooseFolder = chooseFolder(project,
289: "Title", "message", project
290: .getLocation());
291: if (chooseFolder == null)
292: return;
293: TakeCompileWizardPanel.this .includeTakeLibraryLocation = chooseFolder
294: .getProjectRelativePath().toString();
295: ilTB
296: .setText(TakeCompileWizardPanel.this .includeTakeLibraryLocation);
297:
298: } catch (Exception e1) {
299: e1.printStackTrace();
300: }
301:
302: }
303: });
304:
305: ilcbtn.addSelectionListener(new SelectionListener() {
306:
307: @Override
308: public void widgetDefaultSelected(SelectionEvent e) {
309: }
310:
311: @Override
312: public void widgetSelected(SelectionEvent e) {
313: includeTakeLibrary = ilcbtn.getSelection();
314: ilbtn.setEnabled(includeTakeLibrary);
315: ilTB.setEnabled(includeTakeLibrary);
316: }
317: });
318: }
319:
320: }
321:
322: private IFolder chooseFolder(IProject currProject, String title,
323: String message, IPath initialPath) {
324: Class[] acceptedClasses = new Class[] { IFolder.class };
325: ISelectionStatusValidator validator = new TypedElementSelectionValidator(
326: acceptedClasses, false);
327: ViewerFilter filter = new TypedViewerFilter(acceptedClasses,
328: null);
329:
330: ILabelProvider lp = new WorkbenchLabelProvider();
331: ITreeContentProvider cp = new WorkbenchContentProvider();
332:
333: ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(
334: getShell(), lp, cp);
335: dialog.setValidator(validator);
336: dialog.setTitle(title);
337: dialog.setMessage(message);
338: dialog.addFilter(filter);
339: dialog.setInput(currProject);
340: dialog.setComparator(new ResourceComparator(
341: ResourceComparator.NAME));
342: IResource res = currProject.findMember(initialPath);
343: if (res != null) {
344: dialog.setInitialSelection(res);
345: }
346:
347: if (dialog.open() == Window.OK) {
348: return (IFolder) dialog.getFirstResult();
349: }
350: return null;
351: }
352:
353: private void addImportPanel(Composite parent, int numcols) {
354: // Composite topLevel = new Composite(parent, SWT.NONE);
355: // topLevel.setLayout(new GridLayout(1,false));
356: // topLevel.setLayoutData(new
357: // GridData(GridData.FILL,GridData.FILL,true,true,numcols,1));
358: //
359: // Label iLbl = new Label(topLevel,SWT.CENTER);
360: // iLbl.setText("Additional Import Statements");
361: //
362: // final Text iTB = new Text(topLevel,SWT.MULTI | SWT.V_SCROLL);
363: // iTB.setText(sourceOutputLocation);
364: // iTB.setLayoutData(new GridData(GridData.FILL,
365: // GridData.FILL,true,true));
366: // iTB.addListener(SWT.Modify, new Listener(){
367: // @Override
368: // public void handleEvent(Event event) {
369: // importStatements.clear();
370: // StringTokenizer st = new
371: // StringTokenizer(iTB.getText(),iTB.getLineDelimiter());
372: //
373: // while(st.hasMoreTokens())
374: // {
375: // importStatements.add(st.nextToken());
376: // }
377: //
378: // }});
379:
380: Composite topLevel = new Composite(parent, SWT.NONE);
381: topLevel.setLayout(new GridLayout(2, false));
382: topLevel.setLayoutData(new GridData(GridData.FILL,
383: GridData.FILL, true, true, numcols, 1));
384:
385: Label iLbl = new Label(topLevel, SWT.CENTER);
386: iLbl.setText("Additional Import Statements");
387: iLbl.setLayoutData(new GridData(GridData.FILL, GridData.FILL,
388: true, true, 2, 1));
389:
390: final List interfaceList = new List(topLevel, SWT.SINGLE);
391: interfaceList.setLayoutData(new GridData(GridData.FILL,
392: GridData.FILL, true, true));
393: Composite right = new Composite(topLevel, SWT.NONE);
394: right.setLayout(new GridLayout(1, false));
395:
396: Button addBtn = new Button(right, SWT.PUSH);
397: addBtn.setText("Add");
398:
399: IWorkbenchPage iworkbenchpage = getWorkbench();
400: if (iworkbenchpage == null)
401: return;
402:
403: final IProject project = getProjectFromWorkbench(iworkbenchpage);
404:
405: addBtn.addSelectionListener(new SelectionListener() {
406:
407: @Override
408: public void widgetDefaultSelected(SelectionEvent e) {
409:
410: }
411:
412: @Override
413: public void widgetSelected(SelectionEvent e) {
414: try {
415: SelectionDialog dialog = JavaUI
416: .createTypeDialog(
417: getShell(),
418: getWizard().getContainer(),
419: project,
420: IJavaElementSearchConstants.CONSIDER_ALL_TYPES,
421: true);
422:
423: if (dialog.open() == IDialogConstants.CANCEL_ID)
424: return;
425:
426: Object[] types = dialog.getResult();
427: if (types == null || types.length == 0)
428: return;
429:
430: for (Object o : types) {
431: // org.eclipse.jdt.internal.core.BinaryType
432: String elementName = ((IMember) o).getParent()
433: .getParent().getElementName()
434: + "." + ((IMember) o).getElementName();
435: interfaceList.add(elementName);
436: TakeCompileWizardPanel.this .importStatements
437: .add(elementName);
438: }
439:
440: } catch (Exception e1) {
441: // TODO Auto-generated catch block
442: e1.printStackTrace();
443: }
444:
445: }
446: });
447:
448: Button removeBtn = new Button(right, SWT.PUSH);
449: removeBtn.setText("Remove");
450:
451: removeBtn.addSelectionListener(new SelectionListener() {
452:
453: @Override
454: public void widgetDefaultSelected(SelectionEvent e) {
455: // TODO Auto-generated method stub
456:
457: }
458:
459: @Override
460: public void widgetSelected(SelectionEvent e) {
461: for (int i : interfaceList.getSelectionIndices()) {
462:
463: TakeCompileWizardPanel.this .importStatements
464: .remove(interfaceList.getItem(i));
465: interfaceList.remove(i);
466: }
467:
468: }
469: });
470:
471: }
472:
473: private void addInterfacePanel(Composite parent, int ncols) {
474: Composite topLevel = new Composite(parent, SWT.NONE);
475: topLevel.setLayout(new GridLayout(2, false));
476: topLevel.setLayoutData(new GridData(GridData.FILL,
477: GridData.FILL, true, true, ncols, 1));
478:
479: Label iLbl = new Label(topLevel, SWT.CENTER);
480: iLbl.setText("Additional Implemented Interfaces");
481: iLbl.setLayoutData(new GridData(GridData.FILL, GridData.FILL,
482: true, true, 2, 1));
483:
484: final List interfaceList = new List(topLevel, SWT.SINGLE);
485: interfaceList.setLayoutData(new GridData(GridData.FILL,
486: GridData.FILL, true, true));
487: Composite right = new Composite(topLevel, SWT.NONE);
488: right.setLayout(new GridLayout(1, false));
489:
490: Button addBtn = new Button(right, SWT.PUSH);
491: addBtn.setText("Add");
492:
493: IWorkbenchPage iworkbenchpage = getWorkbench();
494: if (iworkbenchpage == null)
495: return;
496:
497: final IProject project = getProjectFromWorkbench(iworkbenchpage);
498:
499: addBtn.addSelectionListener(new SelectionListener() {
500:
501: @Override
502: public void widgetDefaultSelected(SelectionEvent e) {
503:
504: }
505:
506: @Override
507: public void widgetSelected(SelectionEvent e) {
508: try {
509: SelectionDialog dialog = JavaUI
510: .createTypeDialog(
511: getShell(),
512: getWizard().getContainer(),
513: project,
514: IJavaElementSearchConstants.CONSIDER_INTERFACES,
515: true);
516:
517: if (dialog.open() == IDialogConstants.CANCEL_ID)
518: return;
519:
520: Object[] types = dialog.getResult();
521: if (types == null || types.length == 0)
522: return;
523:
524: for (Object o : types) {
525: String elementName = ((IMember) o).getParent()
526: .getParent().getElementName()
527: + "." + ((IMember) o).getElementName();
528: interfaceList.add(elementName);
529: TakeCompileWizardPanel.this .additionalInterfaces
530: .add(elementName);
531:
532: }
533:
534: } catch (Exception e1) {
535: // TODO Auto-generated catch block
536: e1.printStackTrace();
537: }
538:
539: }
540: });
541:
542: Button removeBtn = new Button(right, SWT.PUSH);
543: removeBtn.setText("Remove");
544:
545: removeBtn.addSelectionListener(new SelectionListener() {
546:
547: @Override
548: public void widgetDefaultSelected(SelectionEvent e) {
549: // TODO Auto-generated method stub
550:
551: }
552:
553: @Override
554: public void widgetSelected(SelectionEvent e) {
555: for (int i : interfaceList.getSelectionIndices()) {
556: TakeCompileWizardPanel.this .additionalInterfaces
557: .remove(interfaceList.getItem(i));
558: interfaceList.remove(i);
559:
560: }
561:
562: }
563: });
564: }
565:
566: public static IWorkbenchPage getWorkbench() {
567: IWorkbench iworkbench = PlatformUI.getWorkbench();
568: if (iworkbench == null) {
569: System.out.println("no i workbench");
570: return null;
571: }
572: IWorkbenchWindow iworkbenchwindow = iworkbench
573: .getActiveWorkbenchWindow();
574: if (iworkbenchwindow == null) {
575: System.out.println("no i workbenchwindows");
576: return null;
577: }
578: IWorkbenchPage iworkbenchpage = iworkbenchwindow
579: .getActivePage();
580: if (iworkbenchpage == null) {
581: System.out.println("no i workbenchpage");
582: return null;
583: }
584: return iworkbenchpage;
585: }
586:
587: private void addCheckPanel(Composite parent, int numcols) {
588: Composite topLevel = new Composite(parent, SWT.NONE);
589: topLevel.setLayout(new GridLayout(2, false));
590: topLevel.setLayoutData(new GridData(GridData.FILL,
591: GridData.CENTER, true, false, numcols, 1));
592:
593: final Button aabtn = new Button(topLevel, SWT.CHECK);
594: aabtn.setText("Auto Annotate");
595: aabtn.setSelection(this .isAutoAnotate());
596:
597: aabtn.addSelectionListener(new SelectionListener() {
598:
599: @Override
600: public void widgetDefaultSelected(SelectionEvent e) {
601: }
602:
603: @Override
604: public void widgetSelected(SelectionEvent e) {
605: autoAnotate = aabtn.getSelection();
606: }
607: });
608:
609: final Button ppbtn = new Button(topLevel, SWT.CHECK);
610: ppbtn.setText("Pretty Print Source");
611: ppbtn.setSelection(this .isSourceTransform());
612:
613: ppbtn.addSelectionListener(new SelectionListener() {
614:
615: @Override
616: public void widgetDefaultSelected(SelectionEvent e) {
617: }
618:
619: @Override
620: public void widgetSelected(SelectionEvent e) {
621: sourceTransform = ppbtn.getSelection();
622: }
623: });
624:
625: }
626:
627: public String getPackageName() {
628: return this .packageName;
629: }
630:
631: public String getClassName() {
632: return this .className;
633: }
634:
635: public String getSourceOutputLocation() {
636: return this .sourceOutputLocation;
637: }
638:
639: public LinkedList<String> getImportStatements() {
640: return this .importStatements;
641: }
642:
643: public LinkedList<String> getAdditionalInterfaces() {
644: return this .additionalInterfaces;
645: }
646:
647: public boolean isAutoAnotate() {
648: return this .autoAnotate;
649: }
650:
651: public boolean isSourceTransform() {
652: return this .sourceTransform;
653: }
654:
655: public static IProject getProjectFromWorkbench(
656: IWorkbenchPage iworkbenchpage) {
657: IResource res = extractSelection(iworkbenchpage.getSelection());
658:
659: if (res == null) {
660: res = extractResource(iworkbenchpage.getActiveEditor());
661: }
662:
663: if (res == null) {
664: return null;
665: }
666: return res.getProject();
667: }
668:
669: public static IResource extractSelection(ISelection sel) {
670: if (!(sel instanceof IStructuredSelection))
671: return null;
672: IStructuredSelection ss = (IStructuredSelection) sel;
673: Object element = ss.getFirstElement();
674: if (element instanceof IResource)
675: return (IResource) element;
676: if (!(element instanceof IAdaptable))
677: return null;
678: IAdaptable adaptable = (IAdaptable) element;
679: Object adapter = adaptable.getAdapter(IResource.class);
680: return (IResource) adapter;
681: }
682:
683: public static IResource extractResource(IEditorPart editor) {
684: IEditorInput input = editor.getEditorInput();
685: if (!(input instanceof IFileEditorInput))
686: return null;
687: return ((IFileEditorInput) input).getFile();
688: }
689:
690: public boolean isIncludeTakeLibrary() {
691: return includeTakeLibrary;
692: }
693:
694: public String getIncludeTakeLibraryLocation() {
695: return includeTakeLibraryLocation;
696: }
697: }
|