Skip to main content
summaryrefslogtreecommitdiffstats
blob: f1f6838d4358268f91e6d5732fdc66073c8152ea (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
/*******************************************************************************
 * Copyright (c) 2010 BestSolution.at and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation
 ******************************************************************************/
package org.eclipse.e4.tools.compat.parts;


import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.e4.core.contexts.ContextInjectionFactory;
import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.tools.compat.internal.CopyAction;
import org.eclipse.e4.tools.compat.internal.CutAction;
import org.eclipse.e4.tools.compat.internal.PartHelper;
import org.eclipse.e4.tools.compat.internal.PasteAction;
import org.eclipse.e4.tools.services.IClipboardService;
import org.eclipse.e4.tools.services.IDirtyProviderService;
import org.eclipse.e4.ui.di.Focus;
import org.eclipse.e4.ui.di.Persist;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.part.EditorPart;

public abstract class DIEditorPart<C> extends EditorPart implements IDirtyProviderService {
	private IEclipseContext context;
	private C component;
	private Class<C> clazz;
	private boolean dirtyState;
	
	private int features;

	protected static final int COPY = 1;
	protected static final int PASTE = 1 << 1;
	protected static final int CUT = 1 << 2;
	
	
	public DIEditorPart(Class<C> clazz) {
		this(clazz, SWT.NONE);
	}
	
	public DIEditorPart(Class<C> clazz, int features) {
		this.clazz = clazz;
		this.features = features;
	}
	
	@Override
	public void doSave(IProgressMonitor monitor) {
		IEclipseContext saveContext = context.createChild();
		ContextInjectionFactory.invoke(component, Persist.class, saveContext);
		saveContext.dispose();
	}
	
	@Override
	public void doSaveAs() {
		
	}

	@Override
	public boolean isSaveAsAllowed() {
		return false;
	}

	@Override
	public void init(IEditorSite site, IEditorInput input)
			throws PartInitException {
		setSite(site);
		setInput(input);
		
		context = PartHelper.createPartContext(this);
		context.declareModifiable(IEditorInput.class);
		context.declareModifiable(IEditorPart.class);
		context.declareModifiable(IDirtyProviderService.class);
		
		context.set(IEditorPart.class,this);
		context.set(IDirtyProviderService.class,this);
		context.set(IEditorInput.class, input);
	}

	
	@Override
	public void createPartControl(Composite parent) {
		component = PartHelper.creatComponent(parent, context, clazz, this);
		makeActions();
	}
	
	public C getComponent() {
		return component;
	}
	
	protected void makeActions() {
		if( (features & COPY) == COPY ) {
			IClipboardService clipboard = context.get(IClipboardService.class);
			getEditorSite().getActionBars().setGlobalActionHandler(ActionFactory.COPY.getId(), new CopyAction(clipboard));
		}
		
		if( (features & PASTE) == PASTE ) {
			IClipboardService clipboard = context.get(IClipboardService.class);
			getEditorSite().getActionBars().setGlobalActionHandler(ActionFactory.PASTE.getId(), new PasteAction(clipboard));
		}
		
		if( (features & CUT) == CUT ) {
			IClipboardService clipboard = context.get(IClipboardService.class);
			getEditorSite().getActionBars().setGlobalActionHandler(ActionFactory.CUT.getId(), new CutAction(clipboard));
		}
	}
	
	public void setDirtyState(boolean dirtyState) {
		if( dirtyState != this.dirtyState ) {
			this.dirtyState = dirtyState;
			firePropertyChange(PROP_DIRTY);
		}
	}
	
	@Override
	public boolean isDirty() {
		return dirtyState;
	}
	
	@Override
	public void setFocus() {
		ContextInjectionFactory.invoke(component, Focus.class, context);
	}
	
	@Override
	public void dispose() {
		context.dispose();
		context = null;
		super.dispose();
	}
}

Back to the top