propertyeditorframe.java
字号:
pos++;
}
}
//add a blank panel at the bottom to make sure the (real) properties are
//aligned to the top of the frame.
c.gridx = 0; c.gridy = pos; c.weighty = 1.0;
JPanel blank2 = new JPanel();
gridbag.setConstraints(blank2, c);
listenersPane.add(blank2);
}
editting = false;
//make sure all components are laid out properly
propertiesPane.validate();
propertiesPane.repaint();
listenersPane.validate();
listenersPane.repaint();
}
/**
* Set the name of the currently selected component. This uses the current project's
* Namer object to ensure naming clashes do not occur.
* @param name the new name of the object
*/
public void setName(String name) {
editting = true;
PIYNamer namer = ProjectHandler.getInstance().getGuiNamer();
String oldName = namer.getName(selected);
namer.renameValue(oldName, name);
String newName = namer.getName(selected);
names.removeItem(oldName);
names.addItem(newName);
names.setSelectedItem(newName);
editting = false;
}
/**
* Get's the name of the currently selected component.
* @return the component's name
*/
public String getName() {
return ProjectHandler.getInstance().getProject().getName(selected);
}
// ------------------------------------------------------------------
// We redirect the size and position properties through here to ensure the bounding
// box gets updated. We can also ensure that the layout manager gets invoked
// to keep the component in the correct place after is is moved or resized
public Dimension getSizes() { return ((Component)selected).getSize(); }
public Point getPosition() { return ((Component)selected).getLocation(); }
public void setPosition(Point pos) {
if (selected != null) {
((Component)selected).setLocation(pos);
UserWindow win = UserWindowListener.getInstance().getCurrentWindow();
if (win != null) win.validate();
UserWindowListener.getInstance().validateBoundingBox();
}
}
public void setSizes(Dimension size) {
if (selected != null) {
((Component)selected).setSize(size);
UserWindow win = UserWindowListener.getInstance().getCurrentWindow();
if (win != null) win.validate();
UserWindowListener.getInstance().validateBoundingBox();
}
}
//------------------------------------------------
/**
* Set the alignment of the currently selected component.
* @param align the new alignment of the object
*/
public void setAlignment(Align align) {
AlignmentLayout al = (AlignmentLayout)((Component)selected).getParent().getLayout();
al.setAlignment((Component)selected, align.getAlign());
al.layoutContainer(((Component)selected).getParent());
UserWindowListener.getInstance().validateBoundingBox();
}
/**
* Get's the alignment of the currently selected component.
* @return the component's alignment
*/
public Align getAlignment() {
AlignmentLayout al = (AlignmentLayout)(((Component)selected).getParent()).getLayout();
return new Align(al.getAlignment((Component)selected));
}
//----- Observer Method -------
public void update(Observable o, Object arg) {
ActionEvent e = (ActionEvent)arg;
if (e.getActionCommand().equals(PIY.COMPONENT_SELECTED) ||
e.getActionCommand().equals(PIY.COMPONENT_CHANGED) ||
e.getActionCommand().equals(PIY.COMPONENT_DESELECTED)) {
selected = UserWindowListener.getInstance().getSelectedComponent();
if (selected == null)
selected = UserWindowListener.getInstance().getCurrentWindow();
updateView(selected);
}
}
//------- ActionListener Method ----------
public void actionPerformed(ActionEvent e) {
if (!editting) {
if (e.getActionCommand() == NAMES) { //A names combo box item was selected
//"select" the relevant component on the relevant window
String chosen = (String)names.getSelectedItem();
if (chosen.equals("")) {
UserWindowListener.getInstance().setCurrentWindow(null);
UserWindowListener.getInstance().selectComponent(null);
} else {
Object toSelect = ProjectHandler.getInstance().getGuiNamer().getValue(chosen);
Component win = (Component)toSelect;
while (!(win instanceof UserWindow)) win = win.getParent();
((JFrame)win).setVisible(true);
((JFrame)win).requestFocus();
if (win == toSelect) {
toSelect = null; //toSelect is a window
UserWindowListener.getInstance().setCurrentWindow((JFrame)win);
}
UserWindowListener.getInstance().selectComponent((JComponent)toSelect);
}
} else if (e.getActionCommand() == DELETE_SELECTED) {
String chosen = (String)names.getSelectedItem();
if (chosen.equals("")) {
} else {
PIYNamer namer = ProjectHandler.getInstance().getGuiNamer();
Component toDelete = (Component)namer.getValue(chosen);
boolean clearView = true;
if (toDelete instanceof UserWindow) {
killChildren(((JFrame)toDelete).getContentPane()); //remove references to all things stored in the user window
((JFrame)toDelete).getContentPane().removeAll(); //physically remove them
boolean dispose = ProjectHandler.getInstance().getProject().removeWindow(chosen);//remove user window from project
if (dispose) { //the window should be deleted, since it has been removed from the project
((JFrame)toDelete).dispose();
UserWindowListener.getInstance().setCurrentWindow(null);
}
clearView = dispose;
} else if (toDelete instanceof UserContainer) {
Container parent = (toDelete).getParent();
parent.remove(toDelete); //remove this component from its parent
killChildren((Container)toDelete); //remove container and children from project
} else {
Container parent = (toDelete).getParent();
parent.remove(toDelete);
namer.removeValue(chosen); //remove component from project
parent.repaint();
}
UserWindowListener.getInstance().selectComponent(null);
if (clearView) updateView(null);
JFrame curWindow = UserWindowListener.getInstance().getCurrentWindow();
if (curWindow != null) curWindow.validate();
UserWindowListener.getInstance().validateBoundingBox();
}
} else { //an actionlist has been selected
String actionListName = (String)((JComboBox)e.getSource()).getSelectedItem();
//get the event listener that listens on the selected event
Class eventListener = getEventListener(e.getActionCommand());
HashMap componentsToListeners = ProjectHandler.getInstance().getProject().getEventListeners(selected);
HashMap namesToLists = (HashMap)componentsToListeners.get(eventListener);
if (namesToLists == null) componentsToListeners.put(eventListener, namesToLists = new HashMap(2));
ActionList actionList = (ActionList)ProjectHandler.getInstance().getProject().getActionListNamer().getValue(actionListName);
namesToLists.put(e.getActionCommand(), actionList);
}
}
}
/**
* Removes all of the children and the specified container from the piy project mappings.
* @param container the container who should be removed
*/
private void killChildren(Container holder) {
PIYNamer namer = ProjectHandler.getInstance().getGuiNamer();
namer.removeValue(namer.getName(holder));
Component[] kids = holder.getComponents();
for (int i=0; i<kids.length; i++)
if (kids[i] instanceof Container) killChildren((Container)kids[i]);
else namer.removeValue(namer.getName(kids[i]));
}
/**
* Gets the PIYEventListener class object for the specified event name.
* @param eventName the name of the event, or null if the event is not
* found in any registered listeners
*/
private Class getEventListener(String eventName) {
PiyEventListener[] eventListeners = listeners.getListeners(selected);
for (int i=0; i<eventListeners.length; i++) {
String[] events = eventListeners[i].getEvents();
for (int j=0; j<events.length; j++)
if (events[j] == eventName) //same reference
return eventListeners[i].getClass();
}
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -