1 | package de.uka.ipd.sdq.pcm.gmf.allocation.part; |
2 | |
3 | import org.eclipse.emf.common.util.URI; |
4 | import org.eclipse.emf.common.util.WrappedException; |
5 | import org.eclipse.emf.ecore.EObject; |
6 | import org.eclipse.emf.ecore.resource.Resource; |
7 | import org.eclipse.emf.ecore.resource.ResourceSet; |
8 | import org.eclipse.emf.edit.ui.action.LoadResourceAction.LoadResourceDialog; |
9 | import org.eclipse.emf.transaction.TransactionalEditingDomain; |
10 | import org.eclipse.gmf.runtime.emf.core.GMFEditingDomainFactory; |
11 | import org.eclipse.jface.wizard.WizardPage; |
12 | import org.eclipse.swt.SWT; |
13 | import org.eclipse.swt.events.SelectionAdapter; |
14 | import org.eclipse.swt.events.SelectionEvent; |
15 | import org.eclipse.swt.layout.GridData; |
16 | import org.eclipse.swt.layout.GridLayout; |
17 | import org.eclipse.swt.widgets.Button; |
18 | import org.eclipse.swt.widgets.Composite; |
19 | import org.eclipse.swt.widgets.Label; |
20 | import org.eclipse.swt.widgets.Shell; |
21 | import org.eclipse.swt.widgets.Text; |
22 | |
23 | /** |
24 | * This wizard allows the user to select a resource. It can be passed an |
25 | * already selected resource which then can be changed. A value of null |
26 | * will leave it up to the user to locate the resource. |
27 | * |
28 | * The dialog will only succeed if a valid resource file has been selected. |
29 | */ |
30 | class ResourceSelectorPage extends WizardPage { |
31 | private Text mySelectionText; |
32 | private EObject myResolvedObject; |
33 | |
34 | /** |
35 | * @return a valid resource, or null if no valid one has been selected |
36 | */ |
37 | protected EObject getResolvedObject() { |
38 | return myResolvedObject; |
39 | } |
40 | |
41 | protected ResourceSelectorPage(String name, EObject selectedResource) { |
42 | super(name); |
43 | setTitle("Diagram resource"); |
44 | setDescription("Select the resource to be used for the diagram."); |
45 | myResolvedObject = selectedResource; |
46 | } |
47 | |
48 | public void createControl(Composite parent) { |
49 | initializeDialogUnits(parent); |
50 | Composite topLevel = new Composite(parent, SWT.NONE); |
51 | topLevel.setLayout(new GridLayout()); |
52 | topLevel.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL |
53 | | GridData.HORIZONTAL_ALIGN_FILL)); |
54 | topLevel.setFont(parent.getFont()); |
55 | setControl(topLevel); |
56 | createPageContent(topLevel); |
57 | setPageComplete(doValidatePage()); |
58 | } |
59 | |
60 | /** |
61 | * creates the SWT widgets used to select and display the resource and initializes them |
62 | */ |
63 | private void createPageContent(Composite parent) { |
64 | Composite panel = new Composite(parent, SWT.NONE); |
65 | panel.setLayoutData(new GridData(GridData.FILL_BOTH)); |
66 | GridLayout layout = new GridLayout(); |
67 | panel.setLayout(layout); |
68 | |
69 | Label label = new Label(panel, SWT.NONE); |
70 | label.setText("Selected resource:"); |
71 | label.setLayoutData(new GridData( |
72 | GridData.HORIZONTAL_ALIGN_BEGINNING)); |
73 | |
74 | mySelectionText = new Text(panel, SWT.READ_ONLY); |
75 | mySelectionText.setText("none"); |
76 | if (myResolvedObject != null) { |
77 | mySelectionText.setText(myResolvedObject.eResource().getURI().path()); |
78 | } |
79 | mySelectionText.setLayoutData(new GridData( |
80 | GridData.CENTER)); |
81 | mySelectionText.pack(); |
82 | |
83 | Button button = new Button(panel, SWT.NONE); |
84 | button.setText("Change"); |
85 | button.setLayoutData(new GridData( |
86 | GridData.END)); |
87 | |
88 | button.addSelectionListener(new SelectionAdapter(){ |
89 | |
90 | /* (non-Javadoc) |
91 | * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent) |
92 | */ |
93 | @Override |
94 | public void widgetSelected(SelectionEvent e) { |
95 | Shell shell = e.display.getActiveShell(); |
96 | LoadResourceDialog dialog = new LoadResourceDialog(shell); |
97 | dialog.open(); |
98 | String uri = dialog.getURIText(); |
99 | if (uri != null){ |
100 | mySelectionText.setText(dialog.getURIText()); |
101 | } |
102 | mySelectionText.pack(); |
103 | resolveSelection(); |
104 | setPageComplete(validatePage()); |
105 | } |
106 | |
107 | }); |
108 | } |
109 | |
110 | /** |
111 | * @return true, if a valid resource file has been selected and resolved |
112 | */ |
113 | protected boolean validatePage() { |
114 | if (myResolvedObject == null) { |
115 | setErrorMessage("No or invalid resource selected"); |
116 | return false; |
117 | } |
118 | |
119 | setErrorMessage(null); |
120 | return true; |
121 | } |
122 | |
123 | protected boolean doValidatePage() { |
124 | return (myResolvedObject != null); |
125 | } |
126 | |
127 | /** |
128 | * checks if the selected file is a valid resource |
129 | */ |
130 | private void resolveSelection() { |
131 | myResolvedObject = null; |
132 | TransactionalEditingDomain editingDomain = GMFEditingDomainFactory.INSTANCE |
133 | .createEditingDomain(); |
134 | ResourceSet resourceSet = editingDomain.getResourceSet(); |
135 | |
136 | try { |
137 | Resource resource = resourceSet.getResource(URI.createURI(mySelectionText.getText(),true), true); |
138 | myResolvedObject = (EObject) resource.getContents().get(0); |
139 | |
140 | } catch (WrappedException ex) { |
141 | //do nothing |
142 | } |
143 | } |
144 | } |