001: /*******************************************************************************
002: * Copyright (c) 2007 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.contributions.editor;
011:
012: import org.eclipse.core.commands.AbstractHandler;
013: import org.eclipse.core.commands.ExecutionEvent;
014: import org.eclipse.core.commands.ExecutionException;
015: import org.eclipse.core.commands.IHandler;
016: import org.eclipse.core.runtime.IProgressMonitor;
017: import org.eclipse.swt.SWT;
018: import org.eclipse.swt.events.KeyEvent;
019: import org.eclipse.swt.events.KeyListener;
020: import org.eclipse.swt.layout.GridData;
021: import org.eclipse.swt.layout.GridLayout;
022: import org.eclipse.swt.widgets.Composite;
023: import org.eclipse.swt.widgets.Label;
024: import org.eclipse.swt.widgets.Text;
025: import org.eclipse.ui.IEditorInput;
026: import org.eclipse.ui.IEditorSite;
027: import org.eclipse.ui.ISaveablePart;
028: import org.eclipse.ui.PartInitException;
029: import org.eclipse.ui.examples.contributions.Activator;
030: import org.eclipse.ui.examples.contributions.ContributionMessages;
031: import org.eclipse.ui.examples.contributions.model.Person;
032: import org.eclipse.ui.examples.contributions.model.PersonInput;
033: import org.eclipse.ui.handlers.IHandlerService;
034: import org.eclipse.ui.part.EditorPart;
035:
036: /**
037: * Edit a person.
038: *
039: * @since 3.3
040: */
041: public class InfoEditor extends EditorPart {
042: private static final String EDITOR_RESET_ID = "org.eclipse.ui.examples.contributions.editor.reset"; //$NON-NLS-1$
043:
044: private Person person;
045: private Text surnameText;
046: private Text givennameText;
047: private boolean dirty = false;
048: private IHandler resetHandler;
049:
050: /*
051: * (non-Javadoc)
052: *
053: * @see org.eclipse.ui.part.EditorPart#doSave(org.eclipse.core.runtime.IProgressMonitor)
054: */
055: public void doSave(IProgressMonitor monitor) {
056: monitor.beginTask(getPartName(), 2);
057: person.setSurname(surnameText.getText());
058: monitor.worked(1);
059: person.setGivenname(givennameText.getText());
060: monitor.worked(1);
061: monitor.done();
062: setDirty(false);
063: }
064:
065: /*
066: * (non-Javadoc)
067: *
068: * @see org.eclipse.ui.part.EditorPart#doSaveAs()
069: */
070: public void doSaveAs() {
071: // nothing
072: }
073:
074: /*
075: * (non-Javadoc)
076: *
077: * @see org.eclipse.ui.part.EditorPart#init(org.eclipse.ui.IEditorSite,
078: * org.eclipse.ui.IEditorInput)
079: */
080: public void init(IEditorSite site, IEditorInput input)
081: throws PartInitException {
082: setSite(site);
083: setInput(input);
084: if (!(input instanceof PersonInput)) {
085: throw new PartInitException("Not a person"); //$NON-NLS-1$
086: }
087: PersonInput pinput = (PersonInput) input;
088: person = (Person) Activator.getModel().get(pinput.getIndex());
089: setPartName("Person - " + pinput.getIndex()); //$NON-NLS-1$
090: }
091:
092: /*
093: * (non-Javadoc)
094: *
095: * @see org.eclipse.ui.part.EditorPart#isDirty()
096: */
097: public boolean isDirty() {
098: return dirty;
099: }
100:
101: private void setDirty(boolean d) {
102: dirty = d;
103: firePropertyChange(ISaveablePart.PROP_DIRTY);
104: }
105:
106: /*
107: * (non-Javadoc)
108: *
109: * @see org.eclipse.ui.part.EditorPart#isSaveAsAllowed()
110: */
111: public boolean isSaveAsAllowed() {
112: return false;
113: }
114:
115: /*
116: * (non-Javadoc)
117: *
118: * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
119: */
120: public void createPartControl(Composite parent) {
121: KeyListener keyListener = new KeyListener() {
122: public void keyPressed(KeyEvent e) {
123: if ((e.keyCode & SWT.MODIFIER_MASK) == 0) {
124: setDirty(true);
125: }
126: }
127:
128: public void keyReleased(KeyEvent e) {
129: // nothing
130: }
131: };
132:
133: Composite composite = new Composite(parent, SWT.NONE);
134: GridLayout gridLayout = new GridLayout(2, false);
135: composite.setLayout(gridLayout);
136:
137: Label l = new Label(composite, SWT.RIGHT);
138: l.setText(ContributionMessages.InfoEditor_surname);
139: l.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
140: false));
141: surnameText = new Text(composite, SWT.SINGLE);
142: GridData gridData = new GridData(SWT.FILL, SWT.CENTER, true,
143: false);
144: surnameText.setLayoutData(gridData);
145: surnameText.addKeyListener(keyListener);
146:
147: l = new Label(composite, SWT.RIGHT);
148: l.setText(ContributionMessages.InfoEditor_givenname);
149: l.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
150: false));
151: givennameText = new Text(composite, SWT.SINGLE);
152: gridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
153: givennameText.setLayoutData(gridData);
154: givennameText.addKeyListener(keyListener);
155:
156: updateText();
157:
158: createHandlers();
159: }
160:
161: /**
162: * Set our text field to the person text.
163: */
164: private void updateText() {
165: surnameText.setText(person.getSurname());
166: givennameText.setText(person.getGivenname());
167: }
168:
169: /**
170: * Instantiate any handlers specific to this view and activate them.
171: */
172: private void createHandlers() {
173: IHandlerService handlerService = (IHandlerService) getSite()
174: .getService(IHandlerService.class);
175: resetHandler = new AbstractHandler() {
176: public Object execute(ExecutionEvent event)
177: throws ExecutionException {
178: updateText();
179: setDirty(false);
180: return null;
181: }
182: };
183: handlerService.activateHandler(EDITOR_RESET_ID, resetHandler);
184: }
185:
186: /*
187: * (non-Javadoc)
188: *
189: * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
190: */
191: public void setFocus() {
192: surnameText.setFocus();
193: }
194:
195: /**
196: * A copy of the model object.
197: *
198: * @return a person
199: */
200: public Person getModelPerson() {
201: return new Person(person.getSurname(), person.getGivenname());
202: }
203:
204: /**
205: * A copy of the local data.
206: *
207: * @return a person
208: */
209: public Person getLocalPerson() {
210: return new Person(surnameText.getText(), givennameText
211: .getText());
212: }
213: }
|