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

propertyeditorframe.java

PIY(Program It Yourself)是一个基于Java的应用程序开发环境
JAVA
第 1 页 / 共 2 页
字号:
package piy;

import piy.support.*;
import java.util.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.io.File;

/**
* This frame contains ClassSupport panels which are used to edit the properties of
* particular components.
* @author David Vivash
* @version 1.0, 08/02/01
*/
public class PropertyEditorFrame extends JFrame implements Observer, ActionListener {
	
	private Object selected				= null; //the currently selected component
	private PropertyManager manager		= null; //the property manager
	private ListenerManager listeners	= null; //the listener manager

	private JComboBox names = null;
	private static final String NAMES			= "Names"; //action command of the names comboBox
	private static final String DELETE_SELECTED	= "Delete"; //action command of the delete button


	private Container propertiesPane	= null; //the content pane of the properties
	private Container listenersPane		= null; //the content pane of the listeners

	private boolean editting = false; //set to true when the entries in the names combo box are being editted

	/**
	* Construct a new property editor which uses the specified property manager to
	* check components for properties.
	* @param manager the PropertyManager that can be used to introspect a component's 
	* properties
	* @param listeners the ListenerManager which habdles which events can be listened 
	* for on particular objects
	*/
	public PropertyEditorFrame(PropertyManager manager, ListenerManager listeners) {
		super("Properties");
		this.manager	= manager;
		this.listeners	= listeners;

		propertiesPane	= new JPanel();
		listenersPane	= new JPanel();

		JScrollPane pScrollPane = new JScrollPane(propertiesPane);
		JScrollPane lScrollPane = new JScrollPane(listenersPane);

		pScrollPane.setBorder(new EmptyBorder(0,0,0,0));
		lScrollPane.setBorder(new EmptyBorder(0,0,0,0));


		JTabbedPane tabbedPane = new JTabbedPane();
		tabbedPane.addTab("Properties", pScrollPane);
		tabbedPane.addTab("Events", lScrollPane);

		//names combo box with a delete button to the left
		names = new JComboBox();
		names.addActionListener(this);
		
		JPanel comboPanel = new JPanel();
		comboPanel.setLayout(new BorderLayout());
		comboPanel.add(names, BorderLayout.CENTER);
		
		String sep = File.separator;
		String images = "piy" + sep + "images" + sep;

		FloatingButton delSelected = new FloatingButton(new ImageIcon(images + "delete.gif").getImage(), DELETE_SELECTED, false);
		delSelected.addActionListener(this);
		delSelected.setToolTipText("Delete Selected");
		delSelected.setPreferredSize(new Dimension(24,24));

		comboPanel.add(delSelected, BorderLayout.WEST);
		
		Container container = getContentPane();
		container.setLayout(new BorderLayout());

		container.add(comboPanel, BorderLayout.NORTH);
		container.add(tabbedPane, BorderLayout.CENTER);

		//update the view by showing the properties of the first user edit window
		PIYNamer windows = ProjectHandler.getInstance().getProject().getGuiNamer();
		updateView(selected = windows.getValue(windows.getNames()[0]));

		PIY.getInstance().addObserver(this);
	}

	/**
	* Updates the properties on display in the window.
	* @param selected the currently selected object whose properties will be displayed for editting
	*/
	private void updateView(Object selected) {

		ClassSupport[] support = null;

		propertiesPane.removeAll(); //get rid of old property editors
		listenersPane.removeAll(); //get rid of old listeners

		editting = true;
		//Update the choice list names (since a name could have been changed, or a 
		//component deleted
		names.removeAllItems();
		PIYNamer namer = ProjectHandler.getInstance().getProject().getGuiNamer();
		java.util.List windowNames = namer.getNamesList(UserWindow.class);
		java.util.List componentNames = namer.getNamesList(UserComponent.class);
		//merge the two lists and convert to an array of strings
		windowNames.addAll(componentNames);
		String[] allNames = namer.getNames(windowNames); //misleading method name -> relocate method to a utils class or something

		for (int i=0; i<allNames.length; i++)
			names.addItem(allNames[i]);

		names.insertItemAt("", 0); //insert a blank selection at the top of the list		

		if (selected != null) names.setSelectedItem(getName());
		else names.setSelectedIndex(0);

		names.setActionCommand(NAMES);


		//Sort out the properties of the selected component/container
		if (selected != null) {
	
			//use the property manager to get the edittable properties
			if (selected instanceof UserComponent) 
				support = manager.getClassSupport(selected, UserComponent.class, true, this);
			else
				support = manager.getClassSupport(selected, UserWindow.class, true, this);
	
			GridBagLayout gridbag = new GridBagLayout();
			GridBagConstraints c = new GridBagConstraints();
	
			propertiesPane.setLayout(gridbag);
			c.fill = GridBagConstraints.HORIZONTAL;
			c.ipadx = 4; c.ipady = 8;
			c.insets = new Insets(4,4,4,4);
	
			
			//Set up the "name" property first - force it to come through this class
			Property nameProperty = new Property(this, "Name", String.class);
			JLabel nameLabel = new JLabel("Name");
			c.weightx = 0;
			gridbag.setConstraints(nameLabel, c);
			propertiesPane.add(nameLabel);
			
			ClassSupport nameSupport = new StringSupport(nameProperty);
			c.gridx = 1; c.weightx = 1.0;
			gridbag.setConstraints(nameSupport, c);
			propertiesPane.add(nameSupport);
	
			//Set up the "Alignment" property (if the selected component is not a window) -
			//force it to come through this class
			if (!(selected instanceof UserWindow)) {
	
				Property alignProperty = new Property(this, "Alignment", Align.class);
				JLabel alignLabel = new JLabel("Alignment");
				c.weightx = 0; c.gridx = 0; c.gridy = 1;
				gridbag.setConstraints(alignLabel, c);
				propertiesPane.add(alignLabel);
	
				ClassSupport alignSupport = new AlignSupport(alignProperty);
				c.gridx = 1; c.weightx = 1.0;
				gridbag.setConstraints(alignSupport, c);
				propertiesPane.add(alignSupport);
			}
	
	
			//Add the rest of the properties
			for (int i=0; i<support.length; i++) {
	
				c.gridy = i+2; c.gridx = 0; c.weightx = 0;
				JLabel label = new JLabel(support[i].getProperty().getName());
				gridbag.setConstraints(label, c);
				propertiesPane.add(label);
				
				c.gridx = 1; c.weightx = 1.0;
				gridbag.setConstraints(support[i], c);
				propertiesPane.add(support[i]);
			}
			
			//add a blank panel at the bottom to make sure the (real) properties are 
			//aligned to the top of the frame.
			c.gridy = support.length + 2; c.weighty = 1.0;
			JPanel blank = new JPanel();
			gridbag.setConstraints(blank, c);
			propertiesPane.add(blank);
	
			//------- Now set up the listeners ---------
			PiyEventListener[] lSupport = listeners.getListeners(selected);
	
			listenersPane.setLayout(gridbag);
			c.fill = GridBagConstraints.HORIZONTAL;
			c.ipadx = 0; c.ipady = 0; c.weighty = 0;
			c.insets = new Insets(0,0,0,0);
	
			String[] actionLists = ProjectHandler.getInstance().getProject().getActionListNamer().getNames();
	
			HashMap eventListeners = ProjectHandler.getInstance().getProject().getEventListeners(selected);
	
			//Add the listeners
			int pos = 0;
//			editting = true;
			for (int i=0; i<lSupport.length; i++) {
				String[] eventNames = lSupport[i].getEvents();
	
				HashMap namesToActionLists = (HashMap)eventListeners.get(lSupport[i].getClass());
	
	
				for (int j=0; j<eventNames.length; j++) {
		
					c.gridy = pos; c.gridx = 0; c.weightx = 0;
					JLabel label = new JLabel(eventNames[j]);
					gridbag.setConstraints(label, c);
					listenersPane.add(label);
					
					c.gridx = 1; c.weightx = 1.0;
					JComboBox temp = new JComboBox(actionLists);
					temp.insertItemAt("", 0);
	
					//Get the index
					if (namesToActionLists != null) {
						ActionList mappedTo = (ActionList)namesToActionLists.get(eventNames[j]);
						String name = ProjectHandler.getInstance().getProject().getActionListNamer().getName(mappedTo);
						temp.setSelectedItem(name);
					} else temp.setSelectedIndex(0);
					
					temp.setActionCommand(eventNames[j]);
					temp.addActionListener(this);
					temp.setPreferredSize(new Dimension(100, 24));
					gridbag.setConstraints(temp, c);
					listenersPane.add(temp);

⌨️ 快捷键说明

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