Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2b4b7a1911ae08243764d856a0deadb6fce7fce7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*******************************************************************************
 * Copyright (c) 2006, 2007 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 * IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.examples.model.ui;

import java.io.ByteArrayInputStream;
import java.util.Iterator;

import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.Adapters;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.team.examples.model.*;
import org.eclipse.ui.navigator.*;

/**
 * Model action provider for use with the Common Navigator framework. The
 * purpose of this example is to illustrate logical model integration support in
 * Eclipse and, more specifically, Team. It should not be taken as an
 * illustration of other features (e.g. UI responsiveness, etc).
 */
public class ModelNavigatorActionProvider extends CommonActionProvider {

	private Action newModAction;
	private Action newFolderAction;
	private Action newMoeAction;
	private Action deleteAction;
	private Action makeDirty;

	public ModelNavigatorActionProvider() {
		super();
	}
	
	public void init(ICommonActionExtensionSite aSite) {
		super.init(aSite);
		createActions();
	}

	private void createActions() {
		deleteAction = new Action("Delete") {
			public void run() {
				IStructuredSelection selection = (IStructuredSelection)getContext().getSelection();
				try {
					for (Iterator iter = selection.iterator(); iter.hasNext();) {
						Object element = iter.next();
						if (element instanceof ModelObject) {
							ModelObject mo = (ModelObject) element;
							mo.delete();
						}
					}
				} catch (CoreException e) {
					ErrorDialog.openError(getShell(), null, null, e.getStatus());
				}
			}
		};
		newFolderAction = new Action("Create Folder") {
			public void run() {
				IContainer container = getSelectedContainer();
				if (container != null) {
					String name = promptForName();
					if (name == null)
						return;
					IFolder folder = container.getFolder(new Path(name));
					try {
						folder.create(false, true, null);
					} catch (CoreException e) {
						ErrorDialog.openError(getShell(), null, null, e.getStatus());
					}
				}
			}

			private String promptForName() {
				InputDialog dialog = new InputDialog(getShell(), "Enter Name", "Enter the name of the new folder", "New Folder", null);
				int result = dialog.open();
				if (result == Window.OK) {
					return dialog.getValue();
				}
				return null;
			}
		};
		newModAction = new Action("Create MOD File") {
			public void run() {
				IContainer container = getSelectedContainer();
				if (container != null) {
					String name = promptForName();
					if (name == null)
						return;
					if (!name.endsWith(".mod"))
						name += ".mod";
					IFile file = container.getFile(new Path(name));
					try {
						file.create(new ByteArrayInputStream("".getBytes()), false, null);
					} catch (CoreException e) {
						ErrorDialog.openError(getShell(), null, null, e.getStatus());
					}
				}
			}

			private String promptForName() {
				InputDialog dialog = new InputDialog(getShell(), "Enter Name", "Enter the name of the new model object", "New Object", null);
				int result = dialog.open();
				if (result == Window.OK) {
					return dialog.getValue();
				}
				return null;
			}
		};
		newMoeAction = new Action("Create MOE File") {
			public void run() {
				ModelObjectDefinitionFile modFile = getSelectedModFile();
				if (modFile != null) {
					String path = promptForPath((ModelContainer)modFile.getParent());
					if (path == null)
						return;
					if (!path.endsWith(".moe"))
						path += ".moe";
					ModelContainer parent = (ModelContainer)modFile.getParent();
					IFile file = ((IContainer)parent.getResource()).getFile(new Path(path));
					try {
						file.create(new ByteArrayInputStream("".getBytes()), false, null);
						modFile.addMoe(file);
					} catch (CoreException e) {
						ErrorDialog.openError(getShell(), null, null, e.getStatus());
					}
				}
			}

			private String promptForPath(ModelContainer parent) {
				InputDialog dialog = new InputDialog(getShell(), "Enter Path", "Enter the path of the new model element relative to " + parent.getPath(), "New Element", null);
				int result = dialog.open();
				if (result == Window.OK) {
					return dialog.getValue();
				}
				return null;
			}
		};
		makeDirty = new Action("Make Dirty") {
			public void run() {
				IStructuredSelection selection = (IStructuredSelection)getContext().getSelection();
				for (Iterator iter = selection.iterator(); iter.hasNext();) {
					Object element = iter.next();
					if (element instanceof ModelObjectDefinitionFile) {
						ModelObjectDefinitionFile mo = (ModelObjectDefinitionFile) element;
						ModelSaveablesProvider provider = getSaveablesProvider();
						provider.makeDirty(mo);
					}
				}
			}

			private ModelSaveablesProvider getSaveablesProvider() {
				ITreeContentProvider provider = getActionSite().getContentService().getContentExtensionById("org.eclipse.team.examples.model.navigator").getContentProvider();
				return (ModelSaveablesProvider)Adapters.adapt(provider, SaveablesProvider.class);
			}
		};
	}
	
	protected Shell getShell() {
		return getActionSite().getViewSite().getShell();
	}

	public void fillContextMenu(IMenuManager menu) {
		super.fillContextMenu(menu);
		menu.add(deleteAction);
		IContainer container = getSelectedContainer();
		if (container != null) {
			menu.add(newFolderAction);
			menu.add(newModAction);
		}
		ModelObjectDefinitionFile modFile = getSelectedModFile();
		if (modFile != null) {
			menu.add(newMoeAction);
			menu.add(makeDirty);
		}
	}

	IContainer getSelectedContainer() {
		IStructuredSelection selection = (IStructuredSelection)getContext().getSelection();
		if (selection.size() == 1) {
			Object o = selection.getFirstElement();
			if (o instanceof ModelContainer) {
				ModelContainer mc = (ModelContainer) o;
				return (IContainer)mc.getResource();
			}
		}
		return null;
	}
	
	ModelObjectDefinitionFile getSelectedModFile() {
		IStructuredSelection selection = (IStructuredSelection)getContext().getSelection();
		if (selection.size() == 1) {
			Object o = selection.getFirstElement();
			if (o instanceof ModelObjectDefinitionFile) {
				return (ModelObjectDefinitionFile) o;
			}
		}
		return null;
	}

}

Back to the top