欢迎来到虫虫下载站 | 资源下载 资源专辑 关于我们
虫虫下载站

propertysupport.java

PIY(Program It Yourself)是一个基于Java的应用程序开发环境
JAVA
字号:
package piy.support;

import piy.*;
import java.awt.event.*;
import java.awt.*;
import java.beans.*;
import javax.swing.*;
import java.awt.Dimension;
import java.util.*;

/**
* Support for properties of Property type.  That is, properties in which the property being handled
* itself is a property.
* @author David Vivash
* @version 1.0, 22/03/01
*/
public class PropertySupport extends ClassSupport implements ActionListener
{
	private JComboBox holderChoice		= null;
	private JComboBox propertyChoice	= null;
	private Property value 				= null; //the property this is supporting
	private boolean editting			= false; //if true,indicates that a combobox is being editted, and so we should ignore action events on it
	private Property[] properties		= null;
	private ArrayList listeners = new ArrayList(1);

	private static final String HOLDER_CHANGED		= "holder";
	private static final String PROPERTY_CHANGED	= "property";
	

	public PropertySupport(Property property) {
		super(property);

		value = (Property)property.getValue();

		PIYNamer holderNamer = ProjectHandler.getInstance().getGuiNamer();

		holderChoice = new JComboBox(holderNamer.getNames());
		holderChoice.insertItemAt("", 0); //put a blank at the top of the list

		//select the correct holder
		int index = 0;
		if (value != null) {
			java.util.List holders = holderNamer.getValues();
			for (int i=0; i<holders.size(); i++)
				if (holders.get(i) == value.getHolder())
					index = i+1;
		}
		holderChoice.setSelectedIndex(index);

		propertyChoice = new JComboBox();
		setupPropertyChoice();


		setLayout(new GridLayout(2,1));
		add(holderChoice);
		add(propertyChoice);

		holderChoice.setActionCommand(HOLDER_CHANGED);
		propertyChoice.setActionCommand(PROPERTY_CHANGED);		

		propertyChoice.addActionListener(this);
		holderChoice.addActionListener(this);
	}

	/**
	* Sets up the property selection combo box.  We need to change the contents of this every 
	* time the property holder changes, since the valid properties will have changed.
	*/
	private void setupPropertyChoice() {
		editting = true;
		propertyChoice.removeAllItems();

		Object holder = getHolder();

		if (holder != null) {
			if (UserComponent.class.isAssignableFrom(holder.getClass())) {
				properties = new PropertyManager(PIY.getInstance().getClassSupport()).getProperties(holder, UserComponent.class, false, null);
			} else if (UserWindow.class.isAssignableFrom(holder.getClass())) {
				properties = new PropertyManager(PIY.getInstance().getClassSupport()).getProperties(holder, UserWindow.class, false, null);
			} else if (Variable.class.isAssignableFrom(holder.getClass())) {
				properties = new Property[1];
				properties[0] = new Property(holder, "Value", ((Variable)holder).getType());
			}
			
			int index = 0;
			propertyChoice.addItem("");
			for (int i=0; i<properties.length; i++) {
				if (properties[i].getName().equals(value.getName())) index = i+1;
				propertyChoice.addItem(properties[i].getName());
			}

			propertyChoice.setSelectedIndex(index);
		}
 
		editting = false;
	}

	public static Class getSupportedClass() { return Property.class; }

	public Dimension getMinimumSize() { return new Dimension(10, 40); }
	public Dimension getPreferredSize() { return new Dimension(100, 40); }
	public Dimension getMaximumSize() { return new Dimension(10000, 40); }

	//---- ActionListener method -----
	public void actionPerformed(ActionEvent e) {
		if (!editting) {

			String command = e.getActionCommand();

			if (command.equals(HOLDER_CHANGED)) {
				value = new Property(getHolder(), "", null);
				setupPropertyChoice();
			} else if (command.equals(PROPERTY_CHANGED)) {
				int index = propertyChoice.getSelectedIndex();
				Property bound = new Property(null, "", null);
				if (index > 0) bound = properties[index-1];
				value = new Property(getHolder(), bound.getName(), bound.getType());
			}

			property.setValue(value);
			ProjectHandler.getInstance().projectChanged();
			
			//trigger propertychange event for class support objects bound to this one
			for (int i=0; i<listeners.size(); i++)
				((PropertyChangeListener)listeners.get(i)).propertyChange(new PropertyChangeEvent(this, value.getName(), null, value));
		}
	}

	/**
	* Retrieve the currently selected holder.
	*/
	private Object getHolder() {
		return ProjectHandler.getInstance().getGuiNamer().getValue((String)holderChoice.getSelectedItem());
	}
	
	/**
	* Any object wishing to know when the property has been changed should register themseleves
	* for notification via this method.
	* @param listener the object listening for a change
	*/
	public void addPropertyChangeListener(PropertyChangeListener listener) {
		listeners.add(listener);
	}
}

⌨️ 快捷键说明

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