Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 44295 | Differences between
and this patch

Collapse All | Expand All

(-)Eclipse UI/org/eclipse/ui/internal/IWorkbenchConstants.java (+2 lines)
Lines 90-95 Link Here
90
90
91
    public static final String TAG_FACTORY_ID = "factoryID"; //$NON-NLS-1$
91
    public static final String TAG_FACTORY_ID = "factoryID"; //$NON-NLS-1$
92
92
93
    public static final String TAG_EDITOR_STATE = "editorState"; //$NON-NLS-1$
94
93
    public static final String TAG_TITLE = "title"; //$NON-NLS-1$
95
    public static final String TAG_TITLE = "title"; //$NON-NLS-1$
94
96
95
    public static final String TAG_X = "x"; //$NON-NLS-1$
97
    public static final String TAG_X = "x"; //$NON-NLS-1$
(-)Eclipse UI/org/eclipse/ui/internal/EditorReference.java (+11 lines)
Lines 23-28 Link Here
23
import org.eclipse.swt.widgets.Composite;
23
import org.eclipse.swt.widgets.Composite;
24
import org.eclipse.ui.IEditorInput;
24
import org.eclipse.ui.IEditorInput;
25
import org.eclipse.ui.IEditorPart;
25
import org.eclipse.ui.IEditorPart;
26
import org.eclipse.ui.IEditorPersistable;
26
import org.eclipse.ui.IEditorReference;
27
import org.eclipse.ui.IEditorReference;
27
import org.eclipse.ui.IEditorRegistry;
28
import org.eclipse.ui.IEditorRegistry;
28
import org.eclipse.ui.IElementFactory;
29
import org.eclipse.ui.IElementFactory;
Lines 55-60 Link Here
55
56
56
    private IMemento editorMemento;
57
    private IMemento editorMemento;
57
58
59
	private IMemento editorState;
60
58
    /**
61
    /**
59
     * Flag that lets us detect malfunctioning editors that don't fire PROP_INPUT events.
62
     * Flag that lets us detect malfunctioning editors that don't fire PROP_INPUT events.
60
     * It is never needed for a correctly-functioning 
63
     * It is never needed for a correctly-functioning 
Lines 100-105 Link Here
100
        this.manager = manager;
103
        this.manager = manager;
101
        initListenersAndHandlers();
104
        initListenersAndHandlers();
102
        this.editorMemento = memento;
105
        this.editorMemento = memento;
106
        editorState = editorMemento.getChild(IWorkbenchConstants.TAG_EDITOR_STATE);
103
        String id = memento.getString(IWorkbenchConstants.TAG_ID);
107
        String id = memento.getString(IWorkbenchConstants.TAG_ID);
104
        String title = memento.getString(IWorkbenchConstants.TAG_TITLE);
108
        String title = memento.getString(IWorkbenchConstants.TAG_TITLE);
105
        String tooltip = Util.safeString(memento
109
        String tooltip = Util.safeString(memento
Lines 217-222 Link Here
217
    protected void releaseReferences() {
221
    protected void releaseReferences() {
218
        super.releaseReferences();
222
        super.releaseReferences();
219
        editorMemento = null;
223
        editorMemento = null;
224
        editorState = null;
220
        name = null;
225
        name = null;
221
        factoryId = null;
226
        factoryId = null;
222
        restoredInput = null;
227
        restoredInput = null;
Lines 255-260 Link Here
255
260
256
        super.doDisposePart();
261
        super.doDisposePart();
257
        editorMemento = null;
262
        editorMemento = null;
263
        editorState = null;
258
        restoredInput = new NullEditorInput();
264
        restoredInput = new NullEditorInput();
259
    }
265
    }
260
266
Lines 582-587 Link Here
582
            // been modified until this point)
588
            // been modified until this point)
583
            site = manager.createSite(this, part, desc, editorInput);
589
            site = manager.createSite(this, part, desc, editorInput);
584
            
590
            
591
            // if there is saved state that's appropriate, pass it on
592
            if (part instanceof IEditorPersistable && editorState != null) {
593
				((IEditorPersistable) part).restoreState(editorState);
594
			}
595
            
585
            // Remember the site and the action bars (now that we've created them, we'll need to dispose
596
            // Remember the site and the action bars (now that we've created them, we'll need to dispose
586
            // them if an exception occurs)
597
            // them if an exception occurs)
587
            actionBars = (EditorActionBars) site.getActionBars();
598
            actionBars = (EditorActionBars) site.getActionBars();
(-)Eclipse UI/org/eclipse/ui/internal/EditorManager.java (+8 lines)
Lines 63-68 Link Here
63
import org.eclipse.ui.IEditorLauncher;
63
import org.eclipse.ui.IEditorLauncher;
64
import org.eclipse.ui.IEditorMatchingStrategy;
64
import org.eclipse.ui.IEditorMatchingStrategy;
65
import org.eclipse.ui.IEditorPart;
65
import org.eclipse.ui.IEditorPart;
66
import org.eclipse.ui.IEditorPersistable;
66
import org.eclipse.ui.IEditorReference;
67
import org.eclipse.ui.IEditorReference;
67
import org.eclipse.ui.IEditorRegistry;
68
import org.eclipse.ui.IEditorRegistry;
68
import org.eclipse.ui.IEditorSite;
69
import org.eclipse.ui.IEditorSite;
Lines 1598-1603 Link Here
1598
				inputMem.putString(IWorkbenchConstants.TAG_FACTORY_ID,
1599
				inputMem.putString(IWorkbenchConstants.TAG_FACTORY_ID,
1599
						persistable.getFactoryId());
1600
						persistable.getFactoryId());
1600
				persistable.saveState(inputMem);
1601
				persistable.saveState(inputMem);
1602
				
1603
				// any editors that want to persist state
1604
				if (editor instanceof IEditorPersistable) {
1605
					IMemento editorState = editorMem
1606
							.createChild(IWorkbenchConstants.TAG_EDITOR_STATE);
1607
					((IEditorPersistable) editor).saveState(editorState);
1608
				}
1601
			}
1609
			}
1602
1610
1603
			public void handleException(Throwable e) {
1611
			public void handleException(Throwable e) {
(-)Eclipse (+46 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2006 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 ******************************************************************************/
11
package org.eclipse.ui;
12
13
/**
14
 * An editor can implement this interface and participate in the workbench
15
 * session save/restore cycle using <code>IMemento</code>, similar to how
16
 * <code>IViewPart</code> currently works.
17
 * <p>
18
 * Refer to IWorkbenchPart for the part lifecycle.
19
 * </p>
20
 * <p>
21
 * If a memento is available, restoreState(*) will be inserted into the editor
22
 * startup.
23
 * <ol>
24
 * <li><code>editor.init(site, input)</code></li>
25
 * <li><code>editor.restoreState(memento)</code></li>
26
 * <li><code>editor.createPartControl(parent)</code></li>
27
 * <li>...</li>
28
 * </ol>
29
 * </p>
30
 * <p>
31
 * On workbench shutdown, the editor state will be persisted when the editor
32
 * references are saved.
33
 * </p>
34
 * 
35
 * @since 3.3
36
 */
37
public interface IEditorPersistable extends IPersistable {
38
	/**
39
	 * Called with a memento for this editor. The editor can parse the data or
40
	 * save the memento. This method may not be called.
41
	 * 
42
	 * @param memento
43
	 *            the saved state for this editor. May be <code>null</code>.
44
	 */
45
	public void restoreState(IMemento memento);
46
}
(-)plugin.xml (+7 lines)
Lines 443-448 Link Here
443
            id="org.eclipse.ui.tests.api.MockEditorPart2">
443
            id="org.eclipse.ui.tests.api.MockEditorPart2">
444
      </editor>
444
      </editor>
445
      <editor
445
      <editor
446
            name="Mock Editor Save"
447
            extensions="mocksave"
448
            icon="icons\mockeditorpart1.gif"
449
            class="org.eclipse.ui.tests.api.MockEditorWithState"
450
            id="org.eclipse.ui.tests.api.MockEditorStatePart">
451
      </editor>
452
      <editor
446
            name="IEditorActionBarContributorTest"
453
            name="IEditorActionBarContributorTest"
447
            extensions="mock3"
454
            extensions="mock3"
448
            icon="icons\mockeditorpart2.gif"
455
            icon="icons\mockeditorpart2.gif"
(-)Eclipse UI Tests/org/eclipse/ui/tests/api/IEditorPartTest.java (-47 / +58 lines)
Lines 19-72 Link Here
19
import org.eclipse.ui.tests.harness.util.FileUtil;
19
import org.eclipse.ui.tests.harness.util.FileUtil;
20
20
21
/**
21
/**
22
 * This is a test for IEditorPart.  Since IEditorPart is an
22
 * This is a test for IEditorPart. Since IEditorPart is an interface this test
23
 * interface this test verifies the IEditorPart lifecycle rather
23
 * verifies the IEditorPart lifecycle rather than the implementation.
24
 * than the implementation.
25
 */
24
 */
26
public class IEditorPartTest extends IWorkbenchPartTest {
25
public class IEditorPartTest extends IWorkbenchPartTest {
27
26
28
    /**
27
	/**
29
     * Constructor for IEditorPartTest
28
	 * Constructor for IEditorPartTest
30
     */
29
	 */
31
    public IEditorPartTest(String testName) {
30
	public IEditorPartTest(String testName) {
32
        super(testName);
31
		super(testName);
33
    }
32
	}
34
33
35
    /**
34
	/**
36
     * @see IWorkbenchPartTest#openPart(IWorkbenchPage)
35
	 * @see IWorkbenchPartTest#openPart(IWorkbenchPage)
37
     */
36
	 */
38
    protected MockPart openPart(IWorkbenchPage page) throws Throwable {
37
	protected MockPart openPart(IWorkbenchPage page) throws Throwable {
39
        IProject proj = FileUtil.createProject("IEditorPartTest");
38
		IProject proj = FileUtil.createProject("IEditorPartTest");
40
        IFile file = FileUtil.createFile("IEditorPartTest.txt", proj);
39
		IFile file = FileUtil.createFile("IEditorPartTest.txt", proj);
41
        return (MockWorkbenchPart) page.openEditor(new FileEditorInput(file),
40
		return (MockWorkbenchPart) page.openEditor(new FileEditorInput(file),
42
                MockEditorPart.ID1);
41
				MockEditorPart.ID1);
43
    }
42
	}
44
43
45
    /**
44
	/**
46
     * @see IWorkbenchPartTest#closePart(IWorkbenchPage, MockWorkbenchPart)
45
	 * @see IWorkbenchPartTest#closePart(IWorkbenchPage, MockWorkbenchPart)
47
     */
46
	 */
48
    protected void closePart(IWorkbenchPage page, MockPart part)
47
	protected void closePart(IWorkbenchPage page, MockPart part)
49
            throws Throwable {
48
			throws Throwable {
50
        page.closeEditor((IEditorPart) part, true);
49
		page.closeEditor((IEditorPart) part, true);
51
    }
50
	}
52
    
53
    /**
54
     * Tests that the editor is closed without saving if isSaveOnCloseNeeded()
55
     * returns false.
56
     * @see ISaveablePart#isSaveOnCloseNeeded()
57
     */
58
    public void testOpenAndCloseSaveNotNeeded() throws Throwable {
59
        // Open a part.
60
        MockEditorPart part = (MockEditorPart) openPart(fPage);
61
        part.setDirty(true);
62
        part.setSaveNeeded(false);
63
        closePart(fPage, part);
64
        
65
        CallHistory history = part.getCallHistory();
66
        assertTrue(history.verifyOrder(new String[] { "setInitializationData", "init",
67
                "createPartControl", "setFocus", "isSaveOnCloseNeeded", "widgetDisposed", "dispose" }));
68
        assertFalse(history.contains("doSave"));
69
    }
70
    
71
}
72
51
52
	/**
53
	 * Tests that the editor is closed without saving if isSaveOnCloseNeeded()
54
	 * returns false.
55
	 * 
56
	 * @see ISaveablePart#isSaveOnCloseNeeded()
57
	 */
58
	public void testOpenAndCloseSaveNotNeeded() throws Throwable {
59
		// Open a part.
60
		MockEditorPart part = (MockEditorPart) openPart(fPage);
61
		part.setDirty(true);
62
		part.setSaveNeeded(false);
63
		closePart(fPage, part);
64
65
		CallHistory history = part.getCallHistory();
66
		assertTrue(history.verifyOrder(new String[] { "setInitializationData",
67
				"init", "createPartControl", "setFocus", "isSaveOnCloseNeeded",
68
				"widgetDisposed", "dispose" }));
69
		assertFalse(history.contains("doSave"));
70
	}
71
72
	public void testOpenAndCloseWithNoMemento() throws Throwable {
73
		IProject proj = FileUtil.createProject("IEditorPartTest");
74
		IFile file = FileUtil.createFile("IEditorPartTest.txt", proj);
75
		MockEditorWithState editor = (MockEditorWithState) fPage.openEditor(
76
				new FileEditorInput(file), MockEditorWithState.ID);
77
		closePart(fPage, editor);
78
		
79
		CallHistory history = editor.getCallHistory();
80
		assertFalse(history.contains("saveState"));
81
		assertFalse(history.contains("restoreState"));
82
	}
83
}
(-)Eclipse UI Tests/org/eclipse/ui/tests/session/SessionTests.java (+2 lines)
Lines 41-46 Link Here
41
	private void addEditorTests() {
41
	private void addEditorTests() {
42
		addTest(new WorkbenchSessionTest("editorSessionTests",
42
		addTest(new WorkbenchSessionTest("editorSessionTests",
43
				Bug95357Test.class));
43
				Bug95357Test.class));
44
		addTest(new WorkbenchSessionTest("editorSessionTests",
45
				EditorWithStateTest.class));
44
	}
46
	}
45
47
46
	/**
48
	/**
(-)Eclipse (+179 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2006 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.ui.tests.api;
12
13
import org.eclipse.core.runtime.IProgressMonitor;
14
import org.eclipse.swt.SWT;
15
import org.eclipse.swt.events.SelectionAdapter;
16
import org.eclipse.swt.events.SelectionEvent;
17
import org.eclipse.swt.widgets.Button;
18
import org.eclipse.swt.widgets.Composite;
19
import org.eclipse.ui.IActionBars;
20
import org.eclipse.ui.IEditorInput;
21
import org.eclipse.ui.IEditorPart;
22
import org.eclipse.ui.IEditorPersistable;
23
import org.eclipse.ui.IEditorSite;
24
import org.eclipse.ui.IMemento;
25
import org.eclipse.ui.PartInitException;
26
27
public class MockEditorWithState extends MockWorkbenchPart implements
28
		IEditorPart, IEditorPersistable {
29
30
	public static final String ID = "org.eclipse.ui.tests.api.MockEditorStatePart";
31
32
	public static final String NAME = "Mock Editor State 1";
33
34
	private IEditorInput input;
35
36
	private boolean dirty = false;
37
38
	private boolean saveNeeded = true;
39
40
	private boolean saveAsAllowed = false;
41
42
	public MockEditorWithState() {
43
		super();
44
	}
45
46
	public void createPartControl(Composite parent) {
47
		super.createPartControl(parent);
48
49
		final Button dirtyToggle = new Button(parent, SWT.CHECK);
50
		dirtyToggle.setText("Dirty");
51
		dirtyToggle.addSelectionListener(new SelectionAdapter() {
52
			public void widgetSelected(SelectionEvent e) {
53
				setDirty(dirtyToggle.getSelection());
54
			}
55
		});
56
		dirtyToggle.setSelection(isDirty());
57
58
		final Button saveNeededToggle = new Button(parent, SWT.CHECK);
59
		saveNeededToggle.setText("Save on close");
60
		saveNeededToggle.addSelectionListener(new SelectionAdapter() {
61
			public void widgetSelected(SelectionEvent e) {
62
				setSaveNeeded(saveNeededToggle.getSelection());
63
			}
64
		});
65
		saveNeededToggle.setSelection(saveNeeded);
66
67
		final Button saveAsToggle = new Button(parent, SWT.CHECK);
68
		saveAsToggle.setText("Save as allowed");
69
		saveAsToggle.addSelectionListener(new SelectionAdapter() {
70
			public void widgetSelected(SelectionEvent e) {
71
				setSaveAsAllowed(saveAsToggle.getSelection());
72
			}
73
		});
74
		saveAsToggle.setSelection(saveAsAllowed);
75
	}
76
77
	/**
78
	 * @see IEditorPart#doSave(IProgressMonitor)
79
	 */
80
	public void doSave(IProgressMonitor monitor) {
81
		setDirty(false);
82
		callTrace.add("doSave");
83
	}
84
85
	/**
86
	 * @see IEditorPart#doSaveAs()
87
	 */
88
	public void doSaveAs() {
89
	}
90
91
	/**
92
	 * @see IEditorPart#getEditorInput()
93
	 */
94
	public IEditorInput getEditorInput() {
95
		return input;
96
	}
97
98
	/**
99
	 * @see IEditorPart#getEditorSite()
100
	 */
101
	public IEditorSite getEditorSite() {
102
		return (IEditorSite) getSite();
103
	}
104
105
	/**
106
	 * @see IEditorPart#init(IEditorSite, IEditorInput)
107
	 */
108
	public void init(IEditorSite site, IEditorInput input)
109
			throws PartInitException {
110
		this.input = input;
111
		setSite(site);
112
		callTrace.add("init");
113
		setSiteInitialized();
114
	}
115
116
	/**
117
	 * @see IEditorPart#isDirty()
118
	 */
119
	public boolean isDirty() {
120
		callTrace.add("isDirty");
121
		return dirty;
122
	}
123
124
	public void setDirty(boolean value) {
125
		dirty = value;
126
		firePropertyChange(PROP_DIRTY);
127
	}
128
129
	/**
130
	 * @see IEditorPart#isSaveAsAllowed()
131
	 */
132
	public boolean isSaveAsAllowed() {
133
		callTrace.add("isSaveAsAllowed");
134
		return saveAsAllowed;
135
	}
136
137
	/**
138
	 * @see IEditorPart#isSaveOnCloseNeeded()
139
	 */
140
	public boolean isSaveOnCloseNeeded() {
141
		callTrace.add("isSaveOnCloseNeeded");
142
		return saveNeeded;
143
	}
144
145
	public void setSaveAsAllowed(boolean isSaveAsAllowed) {
146
		this.saveAsAllowed = isSaveAsAllowed;
147
	}
148
149
	public void setSaveNeeded(boolean value) {
150
		saveNeeded = value;
151
	}
152
153
	/*
154
	 * (non-Javadoc)
155
	 * 
156
	 * @see org.eclipse.ui.tests.api.MockWorkbenchPart#getActionBars()
157
	 */
158
	protected IActionBars getActionBars() {
159
		return getEditorSite().getActionBars();
160
	}
161
162
	/*
163
	 * (non-Javadoc)
164
	 * 
165
	 * @see org.eclipse.ui.IEditorPersistable#restoreState(org.eclipse.ui.IMemento)
166
	 */
167
	public void restoreState(IMemento memento) {
168
		callTrace.add("restoreState");
169
	}
170
171
	/*
172
	 * (non-Javadoc)
173
	 * 
174
	 * @see org.eclipse.ui.IPersistable#saveState(org.eclipse.ui.IMemento)
175
	 */
176
	public void saveState(IMemento memento) {
177
		callTrace.add("saveState");
178
	}
179
}
(-)Eclipse (+67 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2004, 2006 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.ui.tests.session;
12
13
import junit.framework.TestCase;
14
15
import org.eclipse.core.resources.IFile;
16
import org.eclipse.core.resources.IProject;
17
import org.eclipse.core.resources.ResourcesPlugin;
18
import org.eclipse.ui.IWorkbench;
19
import org.eclipse.ui.IWorkbenchPage;
20
import org.eclipse.ui.PlatformUI;
21
import org.eclipse.ui.part.FileEditorInput;
22
import org.eclipse.ui.tests.api.MockEditorWithState;
23
import org.eclipse.ui.tests.harness.util.FileUtil;
24
25
/**
26
 * A test to verify the persistence of handler state between sessions.
27
 * 
28
 * @since 3.2
29
 */
30
public class EditorWithStateTest extends TestCase {
31
32
	/**
33
	 * Constructs a new instance of <code>EditorWithStateTest</code>.
34
	 * 
35
	 * @param testName
36
	 *            The name of the test; may be <code>null</code>.
37
	 */
38
	public EditorWithStateTest(final String testName) {
39
		super(testName);
40
	}
41
42
	public void testInitialEditorOpen() throws Exception {
43
		final IWorkbench workbench = PlatformUI.getWorkbench();
44
		final IWorkbenchPage page = workbench.getActiveWorkbenchWindow()
45
				.getActivePage();
46
		IProject proj = FileUtil.createProject("EditorSessionTest");
47
		IFile file = FileUtil.createFile("state.txt", proj);
48
		MockEditorWithState editor = (MockEditorWithState) page.openEditor(
49
				new FileEditorInput(file), MockEditorWithState.ID);
50
		assertFalse(editor.getCallHistory().contains("saveState"));
51
		assertFalse(editor.getCallHistory().contains("restoreState"));
52
	}
53
54
	public void testSecondEditorOpen() throws Exception {
55
		final IWorkbench workbench = PlatformUI.getWorkbench();
56
		final IWorkbenchPage page = workbench.getActiveWorkbenchWindow()
57
				.getActivePage();
58
		IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(
59
				"EditorSessionTest");
60
		IFile file = project.getFile("state.txt");
61
		MockEditorWithState editor = (MockEditorWithState) page
62
				.findEditor(new FileEditorInput(file));
63
		assertNotNull(editor);
64
		assertFalse(editor.getCallHistory().contains("saveState"));
65
		assertTrue(editor.getCallHistory().contains("restoreState"));
66
	}
67
}

Return to bug 44295