1 /******************************************************************************* 2 * Copyright (c) 2000, 2016 IBM Corporation and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * IBM Corporation - initial API and implementation 13 * Patrik Suzzi <psuzzi@gmail.com> - Bug 498814 14 *******************************************************************************/ 15 package org.eclipse.ui.forms; 16 /** 17 * AbstractFormPart implements IFormPart interface and can be used as a 18 * convenient base class for concrete form parts. If a method contains 19 * code that must be called, look for instructions to call 'super' 20 * when overriding. 21 * 22 * @see org.eclipse.ui.forms.widgets.Section 23 * @since 3.0 24 */ 25 public abstract class AbstractFormPart implements IFormPart { 26 private IManagedForm managedForm; 27 private boolean dirty = false; 28 private boolean stale = true; 29 /** 30 * @see org.eclipse.ui.forms.IFormPart#initialize(org.eclipse.ui.forms.IManagedForm) 31 */ 32 @Override initialize(IManagedForm form)33 public void initialize(IManagedForm form) { 34 this.managedForm = form; 35 } 36 /** 37 * Returns the form that manages this part. 38 * 39 * @return the managed form 40 */ getManagedForm()41 public IManagedForm getManagedForm() { 42 return managedForm; 43 } 44 /** 45 * Disposes the part. Subclasses should override to release any system 46 * resources. 47 */ 48 @Override dispose()49 public void dispose() { 50 } 51 /** 52 * Commits the part. Subclasses should call 'super' when overriding. 53 * 54 * @param onSave 55 * <code>true</code> if the request to commit has arrived as a 56 * result of the 'save' action. 57 */ 58 @Override commit(boolean onSave)59 public void commit(boolean onSave) { 60 dirty = false; 61 } 62 /** 63 * Sets the overall form input. Subclases may elect to override the method 64 * and adjust according to the form input. 65 * 66 * @param input 67 * the form input object 68 * @return <code>false</code> 69 */ 70 @Override setFormInput(Object input)71 public boolean setFormInput(Object input) { 72 return false; 73 } 74 /** 75 * Instructs the part to grab keyboard focus. 76 */ 77 @Override setFocus()78 public void setFocus() { 79 } 80 /** 81 * Refreshes the section after becoming stale (falling behind data in the 82 * model). Subclasses must call 'super' when overriding this method. 83 */ 84 @Override refresh()85 public void refresh() { 86 stale = false; 87 // since we have refreshed, any changes we had in the 88 // part are gone and we are not dirty 89 dirty = false; 90 } 91 /** 92 * Marks the part dirty. Subclasses should call this method as a result of 93 * user interaction with the widgets in the section. 94 */ markDirty()95 public void markDirty() { 96 if (dirty) { 97 return; 98 } 99 dirty = true; 100 managedForm.dirtyStateChanged(); 101 } 102 /** 103 * Tests whether the part is dirty i.e. its widgets have state that is 104 * newer than the data in the model. 105 * 106 * @return <code>true</code> if the part is dirty, <code>false</code> 107 * otherwise. 108 */ 109 @Override isDirty()110 public boolean isDirty() { 111 return dirty; 112 } 113 /** 114 * Tests whether the part is stale i.e. its widgets have state that is 115 * older than the data in the model. 116 * 117 * @return <code>true</code> if the part is stale, <code>false</code> 118 * otherwise. 119 */ 120 @Override isStale()121 public boolean isStale() { 122 return stale; 123 } 124 /** 125 * Marks the part stale. Subclasses should call this method as a result of 126 * model notification that indicates that the content of the section is no 127 * longer in sync with the model. 128 */ markStale()129 public void markStale() { 130 stale = true; 131 managedForm.staleStateChanged(); 132 } 133 } 134