fixedpropertysupport.java
字号:
package piy.support;
import piy.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.BevelBorder;
import java.awt.Dimension;
import java.util.ArrayList;
/**
* Support for properties of FixedProperty type.
* @author David Vivash
* @version 1.0, 25/03/01
*/
public class FixedPropertySupport extends ClassSupport implements ActionListener, ChangeListener
{
private FixedPropertySupport instance = null;
private JComboBox valueChoice = null, holderChoice = null;
private boolean editting = false;
private FixedProperty value = null;
private ClassSupport support = null;
private static final String STATIC = "Static", DYNAMIC = "Dynamic",
HOLDER_CHOICE="Holder", VALUE_CHOICE="Value";
public FixedPropertySupport(Property property) {
super(property);
AlignmentLayout layout = new AlignmentLayout();
setLayout(layout);
value = (FixedProperty)property.getValue();
boolean dynamicOnly = false;
if (value != null) dynamicOnly = value.getDynamicOnly();
instance = this;
JTabbedPane tabPane = null;
if (!dynamicOnly) {
tabPane = new JTabbedPane();
add(tabPane);
layout.setAlignment(tabPane, Align.CLIENT);
}
//Set up static panel
if ((value != null) && !dynamicOnly)
if (value.getType() != null) {
support = new PropertyManager(PIY.getInstance().getClassSupport()).getSupport(new Property(value, "Value", value.getType()));
JPanel panel = new JPanel();
CentralLayout cLayout = new CentralLayout();
panel.setLayout(cLayout);
panel.add(support);
cLayout.addLayoutComponent("", support);
tabPane.addTab(STATIC, panel);
}
//Set up dynamic panel
holderChoice = new JComboBox(ProjectHandler.getInstance().getGuiNamer().getNames());
holderChoice.insertItemAt("", 0);
if (value.getProperty() != null) {
String holderName = ProjectHandler.getInstance().getGuiNamer().getName(value.getProperty().getHolder());
if (holderName == null) holderChoice.setSelectedIndex(0);
else
for (int i=0; i<holderChoice.getItemCount(); i++) {
if (holderName.equals((String)holderChoice.getItemAt(i)))
holderChoice.setSelectedIndex(i);
}
}
valueChoice = new JComboBox();
setup();
JPanel holder = new JPanel();
AlignmentLayout holderLayout = new AlignmentLayout();
holder.setLayout(holderLayout);
holder.add(holderChoice);
holder.add(valueChoice);
holderChoice.setBounds(0,0,100,24);
valueChoice.setBounds(0,24,100,24);
holderLayout.setAlignment(holderChoice, Align.TOP);
holderLayout.setAlignment(valueChoice, Align.BOTTOM);
holder.setPreferredSize(new Dimension(100,48));
JPanel dynamicPanel = new JPanel();
CentralLayout dynamicLayout = new CentralLayout();
dynamicPanel.setLayout(dynamicLayout);
dynamicPanel.add(holder);
dynamicLayout.addLayoutComponent("", holder);
if (!dynamicOnly) {
tabPane.addTab(DYNAMIC, dynamicPanel);
tabPane.setSelectedIndex(value.getInternal() ? 0 : 1);
} else {
add(dynamicPanel);
layout.setAlignment(dynamicPanel, Align.CLIENT);
}
valueChoice.setActionCommand(VALUE_CHOICE);
holderChoice.setActionCommand(HOLDER_CHOICE);
valueChoice.addActionListener(this);
holderChoice.addActionListener(this);
if (!dynamicOnly) tabPane.addChangeListener(this);
}
/**
* Get the properties of the specified holder which are of the correct type for our fixed
* property.
* @return the property names
*/
private String[] getProperties(Object holder) {
String[] toReturn = null;
if (holder instanceof Variable) {
if ( ((Variable)holder).getType() == value.getType() ) //the variable is of the correct type
toReturn = new String[] { "Value" };
else toReturn = new String[0];
} else {
Property[] names = new PropertyManager(PIY.getInstance().getClassSupport()).getProperties(holder, UserWindow.class.isAssignableFrom(holder.getClass()) ? UserWindow.class : UserComponent.class, false, null);
ArrayList properties = new ArrayList(names.length);
for (int i=0; i<names.length; i++)
if (names[i].getType() == value.getType())
properties.add(names[i].getName());
toReturn = new String[properties.size()];
for (int i=0; i<toReturn.length; i++)
toReturn[i] = (String)properties.get(i);
}
return toReturn;
}
private void setup() {
editting = true;
valueChoice.removeAllItems();
valueChoice.addItem("");
if ((value.getProperty() != null) && (value.getProperty().getHolder() != null)) {
String[] values = getProperties(value.getProperty().getHolder());
for (int i=0; i<values.length; i++)
valueChoice.addItem(values[i]);
String propertyName = value.getProperty().getName();
if (propertyName != null)
for (int i=0; i<values.length; i++)
if (propertyName.equals(values[i]))
valueChoice.setSelectedIndex(i+1);
value.setProperty(new Property(value.getProperty().getHolder(), (String)valueChoice.getSelectedItem(), value.getType()));
}
editting = false;
}
public static Class getSupportedClass() { return FixedProperty.class; }
public Dimension getMinimumSize() { return support == null ? new Dimension(10, 50) : addDimension(support.getMinimumSize(), new Dimension(10, 50)); }
public Dimension getPreferredSize() { return support == null ? new Dimension(100, 50) : addDimension(support.getPreferredSize(), new Dimension(10, 50)); }
public Dimension getMaximumSize() { return support == null ? new Dimension(10000, 50) : addDimension(support.getMaximumSize(), new Dimension(10, 50)); }
private Dimension addDimension(Dimension a, Dimension b) {
return new Dimension((int)(a.getWidth() + b.getWidth()), (int)(a.getHeight()+b.getHeight()));
}
public void stateChanged(ChangeEvent e) {
value.setInternal(((JTabbedPane)e.getSource()).getSelectedIndex() == 0);
ProjectHandler.getInstance().projectChanged();
}
//---- ActionListener method -----
public void actionPerformed(ActionEvent e) {
if (!editting) {
String action = e.getActionCommand();
if (action == HOLDER_CHOICE) { //set the holder
Object holder = ProjectHandler.getInstance().getGuiNamer().getValue((String)holderChoice.getSelectedItem());
value.setProperty(new Property(holder, "", value.getType()));
setup();
} else { //set the property
value.setProperty(new Property(value.getProperty().getHolder(), (String)valueChoice.getSelectedItem(), value.getType()));
property.setValue(value);
}
ProjectHandler.getInstance().projectChanged();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -