01: /*******************************************************************************
02: * Copyright (c) 2004, 2005 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.ui.examples.components.views.comparisons;
11:
12: import org.eclipse.swt.SWT;
13: import org.eclipse.swt.events.ModifyEvent;
14: import org.eclipse.swt.events.ModifyListener;
15: import org.eclipse.swt.layout.GridData;
16: import org.eclipse.swt.layout.GridLayout;
17: import org.eclipse.swt.widgets.Composite;
18: import org.eclipse.swt.widgets.Text;
19: import org.eclipse.ui.IMemento;
20: import org.eclipse.ui.IPersistable;
21: import org.eclipse.ui.internal.part.components.services.ISavedState;
22:
23: /**
24: * View containing a text box. If the text box contains any text, it will be saved
25: * and restored between sessions. Uses the new view API.
26: *
27: * @since 3.1
28: */
29: public class PersistenceViewNew implements IPersistable {
30: // Constants
31: private static final String ATT_SOMETEXT = "sometext";
32:
33: // Widgets
34: private Text textField;
35:
36: // Current contents of text box
37: private String textValue;
38:
39: /**
40: * Component constructor. Do not invoke directly.
41: */
42: public PersistenceViewNew(Composite parent, ISavedState savedState) {
43: // Create widgets & layout
44: GridLayout layout = new GridLayout(1, true);
45: parent.setLayout(layout);
46:
47: textField = new Text(parent, SWT.NONE);
48: textField.setLayoutData(new GridData(GridData.FILL_BOTH));
49: textField.addModifyListener(new ModifyListener() {
50: /* (non-Javadoc)
51: * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
52: */
53: public void modifyText(ModifyEvent e) {
54: textValue = textField.getText();
55: }
56: });
57:
58: // Restore saved state (if any)
59: IMemento state = savedState.getState();
60: if (state != null) {
61: restoreState(state);
62: }
63: }
64:
65: /**
66: * Restores the previously saved state. Called from the constructor if a previously
67: * saved state exists.
68: *
69: * @param state previously saved state
70: */
71: private void restoreState(IMemento state) {
72: String currentName = state.getString(ATT_SOMETEXT);
73: if (currentName != null) {
74: textField.setText(currentName);
75: }
76: }
77:
78: /* (non-Javadoc)
79: * @see org.eclipse.ui.internal.part.components.interfaces.IPersistable#saveState(org.eclipse.ui.IMemento)
80: */
81: public void saveState(IMemento memento) {
82: memento.putString(ATT_SOMETEXT, textValue);
83: }
84: }
|