001: /*******************************************************************************
002: * Copyright (c) 2004, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.examples.components.views.comparisons;
011:
012: import org.eclipse.swt.SWT;
013: import org.eclipse.swt.events.ModifyEvent;
014: import org.eclipse.swt.events.ModifyListener;
015: import org.eclipse.swt.layout.GridData;
016: import org.eclipse.swt.layout.GridLayout;
017: import org.eclipse.swt.widgets.Composite;
018: import org.eclipse.swt.widgets.Text;
019: import org.eclipse.ui.IMemento;
020: import org.eclipse.ui.IViewSite;
021: import org.eclipse.ui.PartInitException;
022: import org.eclipse.ui.part.ViewPart;
023:
024: /**
025: * View containing a text box. If the text box contains any text, it will be saved
026: * and restored between sessions. Uses the old view API.
027: *
028: * @since 3.1
029: */
030: public class PersistenceViewOld extends ViewPart {
031: // Constants
032: private static final String ATT_SOMETEXT = "sometext";
033:
034: // Saved state
035: private IMemento savedState = null;
036:
037: // Widgets
038: private Text textField;
039:
040: // Current contents of text box
041: private String textValue;
042:
043: /* (non-Javadoc)
044: * @see org.eclipse.ui.part.ViewPart#init(org.eclipse.ui.IViewSite, org.eclipse.ui.IMemento)
045: */
046: public void init(IViewSite site, IMemento memento)
047: throws PartInitException {
048: super .init(site, memento);
049:
050: this .savedState = memento;
051: }
052:
053: /* (non-Javadoc)
054: * @see org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
055: */
056: public void createPartControl(Composite parent) {
057: // Create widgets & layout
058: GridLayout layout = new GridLayout(1, true);
059: parent.setLayout(layout);
060:
061: textField = new Text(parent, SWT.NONE);
062: textField.setLayoutData(new GridData(GridData.FILL_BOTH));
063: textField.addModifyListener(new ModifyListener() {
064: /* (non-Javadoc)
065: * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
066: */
067: public void modifyText(ModifyEvent e) {
068: textValue = textField.getText();
069: }
070: });
071:
072: if (savedState != null) {
073: restoreState(savedState);
074: }
075: }
076:
077: /* (non-Javadoc)
078: * @see org.eclipse.ui.IWorkbenchPart#setFocus()
079: */
080: public void setFocus() {
081: textField.setFocus();
082: }
083:
084: /**
085: * Restores the previously saved state. Called from createPartControl if a previously
086: * saved state exists.
087: *
088: * @param state previously saved state
089: */
090: private void restoreState(IMemento state) {
091: String currentName = state.getString(ATT_SOMETEXT);
092: if (currentName != null) {
093: textField.setText(currentName);
094: }
095: }
096:
097: /* (non-Javadoc)
098: * @see org.eclipse.ui.part.ViewPart#saveState(org.eclipse.ui.IMemento)
099: */
100: public void saveState(IMemento memento) {
101: super.saveState(memento);
102:
103: memento.putString(ATT_SOMETEXT, textValue);
104: }
105:
106: }
|