001: package newprocess.diagram.cust.annotations.dialogs;
002:
003: import newprocess.diagram.part.New_processDiagramEditorPlugin;
004:
005: import org.eclipse.core.resources.IContainer;
006: import org.eclipse.core.resources.IFile;
007: import org.eclipse.core.resources.ResourcesPlugin;
008: import org.eclipse.core.runtime.IPath;
009: import org.eclipse.core.runtime.Path;
010: import org.eclipse.emf.ecore.EObject;
011: import org.eclipse.emf.edit.ui.provider.AdapterFactoryContentProvider;
012: import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider;
013: import org.eclipse.jface.dialogs.Dialog;
014: import org.eclipse.jface.dialogs.IDialogConstants;
015: import org.eclipse.jface.viewers.ILabelProvider;
016: import org.eclipse.jface.viewers.ILabelProviderListener;
017: import org.eclipse.jface.viewers.ISelectionChangedListener;
018: import org.eclipse.jface.viewers.IStructuredSelection;
019: import org.eclipse.jface.viewers.ITreeContentProvider;
020: import org.eclipse.jface.viewers.SelectionChangedEvent;
021: import org.eclipse.jface.viewers.TreeViewer;
022: import org.eclipse.jface.viewers.Viewer;
023: import org.eclipse.jface.viewers.ViewerFilter;
024: import org.eclipse.swt.SWT;
025: import org.eclipse.swt.graphics.Image;
026: import org.eclipse.swt.layout.GridData;
027: import org.eclipse.swt.widgets.Composite;
028: import org.eclipse.swt.widgets.Control;
029: import org.eclipse.swt.widgets.Shell;
030: import org.eclipse.ui.model.WorkbenchContentProvider;
031: import org.eclipse.ui.model.WorkbenchLabelProvider;
032:
033: /**
034: * This dialog is used to link to an existing
035: * file.
036: */
037: public class ElementChooserDialog extends Dialog {
038: private TreeViewer myTreeViewer;
039: private IFile mySelectedModelElement = null;
040:
041: /**
042: * Constructor
043: */
044: public ElementChooserDialog(Shell parentShell) {
045: super (parentShell);
046: setShellStyle(getShellStyle() | SWT.RESIZE);
047: }
048:
049: /**
050: * Creates the Dialog area
051: */
052: protected Control createDialogArea(Composite parent) {
053: Composite composite = (Composite) super
054: .createDialogArea(parent);
055: getShell().setText("Select an Annotation");
056: createModelBrowser(composite);
057: return composite;
058: }
059:
060: /**
061: * create the button bar
062: */
063: protected Control createButtonBar(Composite parent) {
064: Control buttonBar = super .createButtonBar(parent);
065: setOkButtonEnabled(false);
066: return buttonBar;
067: }
068:
069: /**
070: * create the model browser
071: */
072: private void createModelBrowser(Composite composite) {
073: myTreeViewer = new TreeViewer(composite, SWT.SINGLE
074: | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
075: GridData layoutData = new GridData(GridData.FILL_BOTH);
076: layoutData.heightHint = 300;
077: layoutData.widthHint = 300;
078: myTreeViewer.getTree().setLayoutData(layoutData);
079: myTreeViewer
080: .setContentProvider(new ModelElementsTreeContentProvider());
081: myTreeViewer
082: .setLabelProvider(new ModelElementsTreeLabelProvider());
083: myTreeViewer.setInput(ResourcesPlugin.getWorkspace().getRoot());
084: myTreeViewer.addFilter(new ModelFilesFilter());
085: myTreeViewer.addSelectionChangedListener(new OkButtonEnabler());
086: }
087:
088: /**
089: * sets the Ok button to ok
090: */
091: private void setOkButtonEnabled(boolean enabled) {
092: getButton(IDialogConstants.OK_ID).setEnabled(enabled);
093: }
094:
095: /**
096: * check if it is a valid file
097: */
098: private boolean isValidModelFile(IFile file) {
099: /*
100: String fileExtension = file.getFullPath().getFileExtension();
101: if((AnnotationTypes.MSEXCEL_FILEEXTENSION.equals(fileExtension))
102: || AnnotationTypes.MSWORD_FILEEXTENSION.equals(fileExtension))
103: return Boolean.TRUE;
104: else return Boolean.FALSE;
105: */
106: return true;
107: }
108:
109: /**
110: * fetch the file uri
111: */
112: public String getSelectedModelElementURI() {
113: if (mySelectedModelElement == null)
114: return null;
115: return mySelectedModelElement.getProjectRelativePath()
116: .toString();
117:
118: //**********************************************************************************************************
119: //return URI.createPlatformResourceURI(mySelectedModelElement.getName());
120: //String[] names = mySelectedModelElement.getWorkspace().getPathVariableManager().getPathVariableNames();
121: //String projectName = mySelectedModelElement.getFullPath().segment(0);
122:
123: //URIUtil uriutil = new URIUtil();
124: //URI uri = uriutil.relativePathToProjectPath(path);
125: //**********************************************************************************************************
126: /*
127: System.out.println("==================> File Pathes ");
128: System.out.println("Full Path " + mySelectedModelElement.getFullPath()); //**** du kommst hier rein
129: System.out.println("location " + mySelectedModelElement.getLocation());
130: System.out.println("locationURI " + mySelectedModelElement.getLocationURI());
131: System.out.println("Name " + mySelectedModelElement.getName());
132: System.out.println("Project relativ Path " + mySelectedModelElement.getProjectRelativePath());
133: System.out.println("Raw Location " + mySelectedModelElement.getRawLocation());
134: System.out.println("Raw Location URI " + mySelectedModelElement.getRawLocationURI());
135: System.out.println("location path " + mySelectedModelElement.getLocation());
136: System.out.println("Project Relative Path " + mySelectedModelElement.getProjectRelativePath());
137:
138: System.out.println("==================> Project Pathes ");
139: System.out.println("Project Relative Path " + mySelectedModelElement.getProjectRelativePath());
140: System.out.println("Project Raw Location URI " + mySelectedModelElement.getProject().getRawLocationURI());
141: System.out.println("Project Raw Location " + mySelectedModelElement.getProject().getRawLocation());
142: System.out.println("Project Full Path " + mySelectedModelElement.getProject().getFullPath());
143: System.out.println("Project Location " + mySelectedModelElement.getProject().getLocation());
144: System.out.println("Project Location URI " + mySelectedModelElement.getProject().getLocationURI()); //**** du kommst hier rein
145: System.out.println("Project Relative Path " + mySelectedModelElement.getProject().getProjectRelativePath());
146: return mySelectedModelElement.getProjectRelativePath();
147: */
148: //**********************************************************************************************************
149: }
150:
151: /**
152: * Gets the selected project
153: */
154: public String getSelectedProject() {
155: if (mySelectedModelElement == null)
156: return null;
157: return mySelectedModelElement.getProject().toString();
158: }
159:
160: /**
161: * Gets the selected File
162: */
163: public IFile getSelectedFile() {
164: return mySelectedModelElement;
165: }
166:
167: /**
168: * The Tree Content Provider
169: */
170: private class ModelElementsTreeContentProvider implements
171: ITreeContentProvider {
172: private ITreeContentProvider myWorkbenchContentProvider = new WorkbenchContentProvider();
173: private AdapterFactoryContentProvider myAdapterFctoryContentProvier = new AdapterFactoryContentProvider(
174: New_processDiagramEditorPlugin.getInstance()
175: .getItemProvidersAdapterFactory());
176:
177: public Object[] getChildren(Object parentElement) {
178: Object[] result = myWorkbenchContentProvider
179: .getChildren(parentElement);
180: if (result != null && result.length > 0) {
181: return result;
182: }
183: if (parentElement instanceof IFile) {
184: IFile modelFile = (IFile) parentElement;
185: IPath resourcePath = modelFile.getFullPath();
186: }
187: return myAdapterFctoryContentProvier
188: .getChildren(parentElement);
189: }
190:
191: public Object getParent(Object element) {
192: Object parent = myWorkbenchContentProvider
193: .getParent(element);
194: if (parent != null) {
195: return parent;
196: }
197: if (element instanceof EObject) {
198: EObject eObject = (EObject) element;
199: if (eObject.eContainer() == null
200: && eObject.eResource().getURI().isFile()) {
201: String path = eObject.eResource().getURI().path();
202: return ResourcesPlugin.getWorkspace().getRoot()
203: .getFileForLocation(new Path(path));
204: }
205: return myAdapterFctoryContentProvier.getParent(eObject);
206: }
207: return null;
208: }
209:
210: public boolean hasChildren(Object element) {
211: if (element instanceof IFile) {
212: return isValidModelFile((IFile) element);
213: }
214: return myWorkbenchContentProvider.hasChildren(element)
215: || myAdapterFctoryContentProvier
216: .hasChildren(element);
217: }
218:
219: public Object[] getElements(Object inputElement) {
220: Object[] elements = myWorkbenchContentProvider
221: .getElements(inputElement);
222: return elements;
223: }
224:
225: public void dispose() {
226: myWorkbenchContentProvider.dispose();
227: myAdapterFctoryContentProvier.dispose();
228: }
229:
230: public void inputChanged(Viewer viewer, Object oldInput,
231: Object newInput) {
232: myWorkbenchContentProvider.inputChanged(viewer, oldInput,
233: newInput);
234: myAdapterFctoryContentProvier.inputChanged(viewer,
235: oldInput, newInput);
236: }
237: }
238:
239: /**
240: * The Label provider for the element chooser dialog
241: */
242: private class ModelElementsTreeLabelProvider implements
243: ILabelProvider {
244: private WorkbenchLabelProvider myWorkbenchLabelProvider = new WorkbenchLabelProvider();
245: private AdapterFactoryLabelProvider myAdapterFactoryLabelProvider = new AdapterFactoryLabelProvider(
246: New_processDiagramEditorPlugin.getInstance()
247: .getItemProvidersAdapterFactory());
248:
249: public Image getImage(Object element) {
250: Image result = myWorkbenchLabelProvider.getImage(element);
251: return result != null ? result
252: : myAdapterFactoryLabelProvider.getImage(element);
253: }
254:
255: public String getText(Object element) {
256: String result = myWorkbenchLabelProvider.getText(element);
257: return result != null && result.length() > 0 ? result
258: : myAdapterFactoryLabelProvider.getText(element);
259: }
260:
261: public void addListener(ILabelProviderListener listener) {
262: myWorkbenchLabelProvider.addListener(listener);
263: myAdapterFactoryLabelProvider.addListener(listener);
264: }
265:
266: public void dispose() {
267: myWorkbenchLabelProvider.dispose();
268: myAdapterFactoryLabelProvider.dispose();
269: }
270:
271: public boolean isLabelProperty(Object element, String property) {
272: return myWorkbenchLabelProvider.isLabelProperty(element,
273: property)
274: || myAdapterFactoryLabelProvider.isLabelProperty(
275: element, property);
276: }
277:
278: public void removeListener(ILabelProviderListener listener) {
279: myWorkbenchLabelProvider.removeListener(listener);
280: myAdapterFactoryLabelProvider.removeListener(listener);
281: }
282:
283: }
284:
285: /**
286: * This class is used to check if the
287: * given objects should be shown
288: */
289: private class ModelFilesFilter extends ViewerFilter {
290: public boolean select(Viewer viewer, Object parentElement,
291: Object element) {
292: if (element instanceof IContainer) {
293: return true;
294: }
295: if (element instanceof IFile) {
296: IFile file = (IFile) element;
297: return isValidModelFile(file);
298: }
299: return true;
300: }
301: }
302:
303: /**
304: * This class handles the buttons enable/disable
305: * mode
306: */
307: private class OkButtonEnabler implements ISelectionChangedListener {
308: public void selectionChanged(SelectionChangedEvent event) {
309: if (event.getSelection() instanceof IStructuredSelection) {
310: IStructuredSelection selection = (IStructuredSelection) event
311: .getSelection();
312: if (selection.size() == 1) {
313: Object selectedElement = selection
314: .getFirstElement();
315: if (selectedElement instanceof IFile) {
316: mySelectedModelElement = (IFile) selectedElement;
317: setOkButtonEnabled(true);
318: return;
319: }
320: }
321: }
322: mySelectedModelElement = null;
323: setOkButtonEnabled(false);
324: }
325: }
326: }
|