memoryeditor.java
字号:
package piy;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.File;
import java.lang.reflect.*;
import piy.support.StringSupport;
/**
* Editor which allows the user to create new variables of particular datatypes such as
* Strings, integers and booleans.
* @author David Vivash
* @version 1.0, 22/04/01
*/
public class MemoryEditor extends JFrame implements Observer, ActionListener {
//supported class types
private Class[] support;
//The namer from which the variables and their names can be obtained
private PIYNamer namer = null;
//The main (bottom) panel in which the properties for the Variable var can be editted
private Container propertiesPane;
//Panel which contains the variable name selector combo box
private Container comboPanel = null;
private JComboBox combo = null; //name combo box
//Variable currently being editted, or null if none is
private Variable var = null;
//Variable type selector
private JPanel typePanel = null;
private JComboBox typeCombo = null;
//true if a combo box is being editted, and any events it fires should be ignored
private boolean editting = false;
//possible action events that can occur
private static final String NEW_VARIABLE = "newVariable";
private static final String DELETE_VARIABLE = "delVariable";
private static final String COMBO_CHANGE = "combo";
private static final String COMBO_TYPE_CHANGE = "comboType";
/**
* This frame allows the user to modify the current project's global variables.
* @param support what types of variable are supported
*/
public MemoryEditor(Class[] support) {
super("Memory Editor");
this.support = support;
namer = ProjectHandler.getInstance().getGuiNamer();
PIY.getInstance().addObserver(this);
AlignmentLayout layout = null;
getContentPane().setLayout(layout = new AlignmentLayout());
JPanel toolbar = new JPanel();
AlignmentLayout toolbarLayout = null;
toolbar.setLayout(toolbarLayout = new AlignmentLayout());
//----- Setup buttons panel ---------
JPanel buttons = new JPanel() {
//paint a bevelled line on the right hand side of the panel
public void paint(Graphics g) {
super.paint(g);
g.setColor(getBackground().brighter());
g.drawLine(getWidth()-1,0,getWidth()-1, getHeight());
g.setColor(getBackground().darker());
g.drawLine(getWidth()-2,0,getWidth()-2, getHeight());
}
};
buttons.setLayout(null);
String sep = File.separator;
String images = "piy" + sep + "images" + sep;
FloatingButton newVariable = new FloatingButton(new ImageIcon(images + "newvar.gif").getImage(), NEW_VARIABLE, false);
newVariable.addActionListener(this);
newVariable.setBounds(4, 4, 22, 22);
newVariable.setToolTipText("New Variable");
FloatingButton deleteVariable = new FloatingButton(new ImageIcon(images + "bin.gif").getImage(), DELETE_VARIABLE, false);
deleteVariable.addActionListener(this);
deleteVariable.setBounds(30, 4, 22, 22);
deleteVariable.setToolTipText("Delete Variable");
buttons.add(newVariable);
buttons.add(deleteVariable);
toolbar.add(buttons);
buttons.setBounds(0,0,64,32); //set width of buttons container
toolbarLayout.setAlignment(buttons, Align.LEFT);
//--------------------------------------------------
//----------- Set up combo panel --------------
comboPanel = new JPanel() {
public Insets getInsets() { return new Insets(4,10,4,10); }
};
comboPanel.setLayout(new BorderLayout());
toolbar.add(comboPanel);
toolbarLayout.setAlignment(comboPanel, Align.CLIENT);
//---------------------------------------------
getContentPane().add(toolbar);
toolbar.setBounds(0,0,100,32); //set height of toolbar
layout.setAlignment(toolbar, Align.TOP);
propertiesPane = new JPanel();
JScrollPane scroller = new JScrollPane(propertiesPane);
getContentPane().add(scroller);
layout.setAlignment(scroller, Align.CLIENT);
//we can set up the variable type combo box straight away
setupTypeCombo();
typePanel = new JPanel();
CentralLayout cl = new CentralLayout();
typePanel.setLayout(cl);
typePanel.add(typeCombo);
typeCombo.setPreferredSize(new Dimension(100,24));
cl.addLayoutComponent("", typeCombo);
typeCombo.setActionCommand(COMBO_TYPE_CHANGE);
typeCombo.addActionListener(this);
updateView();
}
/**
* Set the variable to view and edit.
* @param var the variable reference
*/
public void setVariable(Variable var) {
this.var = var;
// propertiesPane.setVisible(var != null);
updateView();
}
/**
* Creates the view for the current project's memory.
*/
private void updateView() {
propertiesPane.setVisible(var != null);
//get all the Variable object names
setupCombo();
setupTypeCombo();
propertiesPane.removeAll();
//Layout the class support panels
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);
//--- Name of Variable ----
c.gridy = 0; c.gridx = 0; c.weightx = 0;
JLabel nameLabel = new JLabel("Name");
gridbag.setConstraints(nameLabel, c);
propertiesPane.add(nameLabel);
ClassSupport nameSupport = new StringSupport(new Property(this, "Name", String.class));
c.gridx = 1; c.weightx = 1.0;
gridbag.setConstraints(nameSupport, c);
propertiesPane.add(nameSupport);
//--- Type of Variable ---
c.gridy = 1; c.gridx = 0; c.weightx = 0;
JLabel typeLabel = new JLabel("Type");
gridbag.setConstraints(typeLabel, c);
propertiesPane.add(typeLabel);
c.gridx = 1; c.weightx = 1.0;
gridbag.setConstraints(typePanel, c);
propertiesPane.add(typePanel);
int gridpos = 2;
//--- Value of Variable ---
if ((var != null) && (var.getType() != null)) {
c.gridy = 2; c.gridx = 0; c.weightx = 0;
JLabel valueLabel = new JLabel("Value");
gridbag.setConstraints(valueLabel, c);
propertiesPane.add(valueLabel);
PropertyManager propMan = new PropertyManager(support);
ClassSupport valueSupport = propMan.getSupport(new Property(var, "Value", var.getType()));
c.gridx = 1; c.weightx = 1.0;
if (valueSupport != null) {
gridbag.setConstraints(valueSupport, c);
propertiesPane.add(valueSupport);
}
gridpos ++;
}
//add a blank panel at the bottom to make sure the (real) properties are
//aligned to the top of the frame.
c.gridy = gridpos; c.weighty = 1.0;
JPanel blank = new JPanel();
gridbag.setConstraints(blank, c);
propertiesPane.add(blank);
//--------------
validate();
repaint();
}
/**
* Sets up the name combo box.
*/
private void setupCombo() {
editting = true;
String[] names = namer.getNames(namer.getNamesList(Variable.class));
comboPanel.removeAll();
combo = new JComboBox(names);
combo.insertItemAt("", 0);
comboPanel.add(combo, BorderLayout.CENTER);
if (var != null) combo.setSelectedItem(namer.getName(var));
else combo.setSelectedIndex(0);
combo.setActionCommand(COMBO_CHANGE);
combo.addActionListener(this);
editting = false;
}
/**
* Sets up the type combo box.
*/
private void setupTypeCombo() {
//initial setup of the combo box
editting = true;
if (typeCombo == null) {
String[] names = new String[support.length + 1];
names[0] = "";
Method getSupportedClass = null;
try{
for (int i=0; i<support.length; i++) {
getSupportedClass = support[i].getMethod("getSupportedClass", null);
Class supported = (Class)getSupportedClass.invoke(null, new Object[0]);
names[i+1] = getShortName(supported);
}
} catch (Exception e) {
System.out.println("Unable to get support method from support class");
e.printStackTrace();
}
typeCombo = new JComboBox(names);
}
//set the selected item to the type of the variable
if (var != null) {
if (var.getType() == null) {
typeCombo.setSelectedIndex(0);
} else {
typeCombo.setSelectedItem(getShortName(var.getType()));
}
}
editting = false;
}
/**
* Gets the partially qualified name of the class. That is, if the class c has the name
* "java.lang.String", this method returns simply "String".
* @param c the class type to retreive the short name from
* @return the shortened version of the class' name
*/
private String getShortName(Class c) {
String name = c.getName();
return name.substring(name.lastIndexOf('.')+1, name.length());
}
/**
* Force the name setting on the name panel to come through this method so we can
* update the combo box
* @param name the new name for the variable being editted
*/
public void setName(String name) {
editting = true;
String oldName = namer.getName(var);
namer.renameValue(oldName, name);
String newName = namer.getName(var);
combo.removeItem(oldName);
combo.addItem(newName);
combo.setSelectedItem(newName);
editting = false;
}
/**
* Gets the name of the action list currently being editted.
* @returns the unique name for the action list.
*/
public String getName() {
return namer.getName(var);
}
//------ ActionListener method ---------
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command == NEW_VARIABLE) {
var = new Variable(Void.class, null);
namer.add(var, "Var");
setVariable(var);
ProjectHandler.getInstance().projectChanged();
} else if (command == DELETE_VARIABLE) {
if (combo.getSelectedIndex() > 0) {
namer.removeValue(namer.getName(var));
ProjectHandler.getInstance().projectChanged();
}
setVariable(null);
} else if (command == COMBO_CHANGE) {
if (!editting) {
if (combo.getSelectedIndex() > 0) {
String item = (String)combo.getSelectedItem();
setVariable((Variable)namer.getValue(item));
} if (combo.getSelectedIndex() == 0) {
setVariable(null);
}
}
} else if (command == COMBO_TYPE_CHANGE) {
if (!editting) {
int index = typeCombo.getSelectedIndex();
if (index == 0) {
var.setType(Void.class);
updateView();
} else {
Class supportedBy = support[index-1];
try{
Method getSupportedClass = supportedBy.getMethod("getSupportedClass", null);
var.setType((Class)getSupportedClass.invoke(null, new Object[0]));
ProjectHandler.getInstance().projectChanged();
updateView();
} catch (Exception ex) {
System.out.println("Unable to set Variable to specified type");
}
}
}
}
}
//------ Observer method -----------
public void update(Observable o, Object arg) {
ActionEvent e = (ActionEvent)arg;
if (e.getActionCommand().equals(PIY.NEW_PROJECT) || e.getActionCommand().equals(PIY.OPEN_PROJECT)) {
namer = ProjectHandler.getInstance().getGuiNamer();
setVariable(null);
updateView();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -