⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form.java

📁 利用 java SWT 实现MVC功能
💻 JAVA
字号:
package ExampleViewer.form;

import java.util.Enumeration;
import java.util.Iterator;
import java.util.Vector;

import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.swt.widgets.Text;

import ExampleViewer.data.ExampleViewContentProvider;
import ExampleViewer.data.IExampleContentProvider;

public abstract class Form extends Composite implements IForm {
	//	A named pairing of controls on this form
	//
	private Vector	_formFrags = new Vector();
	private	Vector	_controls  = new Vector();

	private	ExampleViewContentProvider	_content;
	private	int			_mode;
	private int					_style;
	private Vector	_fields = new Vector(10, 1);
	
	public Form (Composite parent, int style, int mode, ExampleViewContentProvider content) {
		super(parent, style);
		_content = content;
		_style = style;
		setMode(mode);
		init(style);
	}
	
	
	private void init(int style) {
		
		createControls(style);
		controlsCreated();		
	}
	
	protected void controlsCreated() {	
		readFromContent();
	}
	/* (non-Javadoc)
	 * @see com.ibm.eworkplaces.pim.ui.docedit.form.IForm#getFormFrags()
	 */
	public Vector getFormFrags() {
		return _formFrags;
	}

	/**
	/* (non-Javadoc)
	 * @see com.ibm.eworkplaces.pim.ui.docedit.form.IFormFragment#getFields()
	 */
	public void addField(Field field) {
		_fields.add(field);
	}
	
	public void addControl(Control control) {
		_controls.add(control);
	}
	

	/* (non-Javadoc)
	 * @see com.ibm.eworkplaces.pim.ui.docedit.form.IFormFragment#getFields()
	 */
	public Vector getFields() {
		// TODO Auto-generated method stub
		return _fields;
	}

	

	/*	
	 *  (non-Javadoc)
	 * @see com.ibm.eworkplaces.pim.ui.docedit.form.IForm#refreshFromData()
	 * 
	 * TODO	This is a little crufty for now.  -- DWC
	 */
	public void readFromContent() {
		Vector		textControls = new Vector();
		Enumeration	eFormFrag = getFormFrags().elements();

		
		
	
				Vector		fields = getFields();
				
				if (fields != null) {
					Iterator		iFields = fields.iterator();
				
					if (iFields != null) {
						//	Loop over the fields on the form frag
						while (iFields.hasNext()) {	
							Field	field = (Field )iFields.next();

							// spr JTHN5YVS7S
							
							// Not sure this is the correct fix for JTHN5YVS7S.  Should we return or just skip this field.
							// In any case.. we can't just blindly check field.getControl().  Some fields (GButtonsField for example)
							// have an array of controls instead of a single control.  - Frank Pavelski
							if (field.getControl() != null) {
							    if (field.getControl().isDisposed()) {
							        return;
							    }
							}
							else if (field.getControls() != null) {
							    Control ctrls[] = field.getControls();
							    int numCtrls = ctrls.length;
							    for (int i = 0; i < numCtrls; i++) {
							        if (ctrls[i].isDisposed()) {
							            return;
							        }
							    }
							}
							
							field.readFromContent(_content);
							
							//	If it is a text field, add the modify listener now that we have read the data
							Control control = field.getControl();
							Text	text = null;							
							if (control instanceof Text) 
								text = (Text )field.getControl();
							if (text != null) {
								textControls.add(text);
							}								
						}
					}	
				}
			
		
		
		calcHideWhen(false);
		layout(true);
	}
	
	/*	
	 *  (non-Javadoc)
	 * @see com.ibm.eworkplaces.pim.ui.docedit.form.IForm#refreshFromData()
	 * 
	 * TODO	This is a little crufty for now.  -- DWC
	 */
	public void writeToContent() {
		Enumeration	eFormFrag = getFormFrags().elements();
		
		//	Loop over the form frags on the form
		//
		while (eFormFrag.hasMoreElements()) {
			IForm	formFrag = (IForm )eFormFrag.nextElement();
			
			if (formFrag != null) {
				Vector		fields = formFrag.getFields();
				
				if (fields != null) {
					Iterator		iFields = fields.iterator();
				
					if (iFields != null) {
						//	Loop over the fields on the form frag
						while (iFields.hasNext()) {	
							Field	field = (Field )iFields.next();
							field.writeToContent(_content);
						}
					}	
				}
			}
		}
	}

	public boolean calcHideWhen() {
		return calcHideWhen(true);
	}
	
	/*	
	 *  (non-Javadoc)
	 * @see com.ibm.eworkplaces.pim.ui.docedit.form.IForm#refreshFromData()
	 * 
	 * TODO	This is a little crufty for now.  -- DWC
	 */
	public boolean calcHideWhen(boolean doLayout) {
		boolean		needsLayout = false;
		Enumeration	eFormFrag = getFormFrags().elements();
		
		//	Loop over the form frags on the form
		//
		while (eFormFrag.hasMoreElements()) {
			IForm	formFrag = (IForm )eFormFrag.nextElement();
			
			if (formFrag != null) {
			
			}
		}				
		if (doLayout && needsLayout) {
			layout(true);			
			return false;
		} else {
			return needsLayout;
		}
	}	

	/* (non-Javadoc)
	 * @see com.ibm.eworkplaces.pim.ui.docedit.form.IForm#getContent()
	 */
	public IExampleContentProvider getContentProvider() {
		return _content;
	}
	
	/* (non-Javadoc)
		 * @see com.ibm.eworkplaces.pim.ui.docedit.form.IForm#getContent()
		 */
	public Object getContentData() {
		return getContentProvider().getData();
	}
	
	/* (non-Javadoc)
		 * @see com.ibm.eworkplaces.pim.ui.docedit.form.IForm#getContent()
		 */
	public IForm getForm() {
		return this;
	}	
	
	public FieldMode getMode() {
		return null;
	}
	
	private void setMode(int mode) {
		_mode = mode;
	}
	
	public void switchMode(int newMode) {
		Enumeration	eFormFrag = getFormFrags().elements();
		
		//	Loop over the form frags on the form and get rid of them
		//
		while (eFormFrag.hasMoreElements()) {
			IForm	formFrag = (IForm )eFormFrag.nextElement();

			if (formFrag != null)
				((Composite )formFrag).dispose();
		}
		
		//	Remove all form frags from list now that they are disposed
		getFormFrags().clear();
		
		//	Loop over all controls and eliminate them too
		Enumeration	eControls = _controls.elements();
		while (eControls.hasMoreElements()) {
			Control	control = (Control )eControls.nextElement();
			
			if (control != null)
				control.dispose();
		}
		_controls.clear();
		
		//	Re-execute creation and population of controls.
		//
		setMode(newMode);
		init(_style);
	}
	
	public void setFormFragmentExpanded(String name, boolean expanded, boolean force) {
		Enumeration	eFormFrag = getFormFrags().elements();
		
		//	Loop over the form frags on the form for the specific one
		//
		while (eFormFrag.hasMoreElements()) {
			Object object = eFormFrag.nextElement();

			if (object != null) {
				Control			control = (Control )object;
				
				if (control.getClass().getName().endsWith(name))  {
				 	//formFrag.setExpanded(expanded, force);
				 	return;
				}
			}
		}
	}
	
	/*
	 *  (non-Javadoc)
	 * @see org.eclipse.swt.widgets.Control#setFocus()
	 */
	public boolean setFocus() {
		return super.setFocus();
	}
	
	public void setExpanded(boolean expanded, boolean force) {
		Layout	layout = getLayout();		
		if (layout instanceof GridLayout) {
		} else {
			super.setVisible(expanded);
		}			
	}	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -