actionlisteditor.java
字号:
package piy;
import piy.action.IfAction;
import piy.support.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.lang.reflect.*;
import java.beans.PropertyChangeListener;
import java.util.*;
import java.io.*;
/**
* The window in which action lists can be edited/created.
* @author David Vivash
* @version 1.0.4, 28/04/01
*/
public class ActionListEditor extends JFrame implements ChangeListener, ActionListener, Observer
{
private static String NEW_ACTIONLIST = "newActionList";
private static String DELETE_ACTIONLIST = "deleteActionList";
private static String INSERT_BEFORE = "insertBefore";
private static String INSERT_AFTER = "insertAfter";
private static String DELETE_ACTION = "deleteAction";
private ActionList list = null; //the action list being editted
private ActionDescriptor[] actionDescriptors;
private ScrollingButtonPanel[] actionPanel = null;
private JTabbedPane tabPane = null;
private ActionListNamePanel actionListNamePanel = null;
private PropertiesPanel propertiesPanel = new PropertiesPanel();
private ActionListViewer viewer = null;
private JScrollPane scroller = null;
//The action descriptor that has currently been selected on the toolbar
private ActionDescriptor toolbarSelection = null;
/**
* Construct a new ActionListEditor window viewing the specified action list. If the specified
* list has not yet been added to the project currently being editted, this constructor
* automatically adds it.
* @param actionDescriptors the descriptor classes for all of the available actions
* @param list the list to be editted in the window
*/
public ActionListEditor(ActionDescriptor[] actionDescriptors, ActionList list) {
super("Action List Editor");
PIYNamer actionListNamer = ProjectHandler.getInstance().getProject().getActionListNamer();
//if the list doesn't have a name...
// if ((list != null) && (actionListNamer.getName(list) == null))
// actionListNamer.add(list, "ActionList");
this.list = list;
this.actionDescriptors = actionDescriptors;
PIY.getInstance().addObserver(this);
startup();
}
/**
* Called by the constructor to set up the initial frame structure.
*/
private void startup() {
getContentPane().setLayout(new BorderLayout());
JPanel topPanel = new JPanel();
topPanel.setLayout(new BorderLayout());
//---- Set up toolbar buttons -------
JPanel toolbarButtons = new JPanel();
toolbarButtons.setLayout(null);
toolbarButtons.setPreferredSize(new Dimension(64,60));
String sep = File.separator;
String images = "piy" + sep + "images" + sep;
FloatingButton newActionList = new FloatingButton(new ImageIcon(images + "new.gif").getImage(), NEW_ACTIONLIST, false);
newActionList.addActionListener(this);
newActionList.setBounds(4, 6, 22, 22);
newActionList.setToolTipText("New Action List");
FloatingButton removeAction = new FloatingButton(new ImageIcon(images + "bin.gif").getImage(), DELETE_ACTION, false);
removeAction.addActionListener(this);
removeAction.setBounds(30, 6, 22, 22);
removeAction.setToolTipText("Remove Action");
FloatingButton insertBefore = new FloatingButton(new ImageIcon(images + "insertbefore.gif").getImage(), INSERT_BEFORE, false);
insertBefore.addActionListener(this);
insertBefore.setBounds(4, 34, 22, 22);
insertBefore.setToolTipText("Insert Before");
FloatingButton insertAfter = new FloatingButton(new ImageIcon(images + "insertafter.gif").getImage(), INSERT_AFTER, false);
insertAfter.addActionListener(this);
insertAfter.setBounds(30, 34, 22, 22);
insertAfter.setToolTipText("Insert After");
toolbarButtons.add(newActionList);
toolbarButtons.add(removeAction);
toolbarButtons.add(insertBefore);
toolbarButtons.add(insertAfter);
//------ Set up action buttons panel -----
tabPane = new JTabbedPane();
ArrayList collections = sortCategories();
actionPanel = new ScrollingButtonPanel[collections.size()];
FloatingButton toolButton = null;
ActionDescriptor[] descriptors = null;
for (int i=0; i<collections.size(); i++) {
descriptors = (ActionDescriptor[])collections.get(i);
actionPanel[i] = new ScrollingButtonPanel(descriptors.length, 26, 26, 4);
tabPane.addTab(descriptors[0].getCategory(), actionPanel[i]);
actionPanel[i].addChangeListener(this);
for (int j=0; j<descriptors.length; j++) {
actionPanel[i].addButton(toolButton = new FloatingButton(descriptors[j].getIcon(), ""+j, true));
//where the action descriptor is located in the actionDescriptors array
int num = findDescriptor(descriptors[j]);
toolButton.setIdentifier(num);
toolButton.setToolTipText(descriptors[j].getName());
}
}
//Move the "General" tab to be the first tab in the pane
int posOfGeneral = tabPane.indexOfTab("General");
if (posOfGeneral != -1) {
if (posOfGeneral != 0) {
Component generalPanel = tabPane.getComponentAt(posOfGeneral);
tabPane.removeTabAt(posOfGeneral);
tabPane.insertTab("General", null, generalPanel, null, 0);
}
}
topPanel.add(toolbarButtons, BorderLayout.WEST);
topPanel.add(tabPane, BorderLayout.CENTER);
JPanel padding = new JPanel();
padding.setPreferredSize(new Dimension(0,8));
topPanel.add(padding, BorderLayout.SOUTH);
JPanel leftPanel = new JPanel();
leftPanel.setPreferredSize(new Dimension(180,100));
leftPanel.setMaximumSize(new Dimension(400,100));
leftPanel.setMinimumSize(new Dimension(50,100));
actionListNamePanel = new ActionListNamePanel(list);
leftPanel.setLayout(new BorderLayout());
leftPanel.add(actionListNamePanel, BorderLayout.NORTH);
leftPanel.add(propertiesPanel, BorderLayout.CENTER);
viewer = new ActionListViewer(list);
scroller = new JScrollPane(viewer);
JSplitPane bottomPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, scroller);
leftPanel.setPreferredSize(new Dimension(180,100));
getContentPane().add(topPanel, BorderLayout.NORTH);
getContentPane().add(bottomPanel, BorderLayout.CENTER);
}
/**
* Takes the action descriptors, and sorts them into a collection of arrays - each array contains
* descriptors of actions in the same category. So sorts out all of the string actions from the
* files actions, for example. Each entry in the array list returned is an ActionDescriptor[].
* @return a collection of ActionDescriptor arrays, with each array containing descriptors in the
* same category.
*/
private ArrayList sortCategories() {
//generate a map from each category name to the number of actions in that category
//ie categoryName(String) -> Count(Inteeger)
HashMap nameToCount = new HashMap(actionDescriptors.length);
Integer num = null;
for (int i=0; i< actionDescriptors.length; i++) {
num = (Integer)nameToCount.get(actionDescriptors[i].getCategory());
if (num == null) //category name doesn't yet exist
num = new Integer(1);
else
num = new Integer(num.intValue() + 1);
nameToCount.put(actionDescriptors[i].getCategory(), num);
}
//Now construct each array and store it in the arraylist to return.
//We could return an array of ActionDescriptor arrays, but I think that's a bit
//too confusing
ArrayList toReturn = new ArrayList(nameToCount.size());
ActionDescriptor[] array = null;
String currentName = null;
int arrayCounter = 0;
//Iterate over each named category
Iterator nameIterator = nameToCount.keySet().iterator();
while (nameIterator.hasNext()) {
currentName = (String)nameIterator.next();
array = new ActionDescriptor[((Integer)nameToCount.get(currentName)).intValue()];
arrayCounter = 0;
for (int j=0; j<actionDescriptors.length; j++) {
if (actionDescriptors[j].getCategory().equals(currentName)) {
array[arrayCounter] = actionDescriptors[j];
arrayCounter++;
}
}
toReturn.add(array);
}
return toReturn;
}
/**
* Find the specified Descriptor's location in the actionDescriptors array
* @param descriptor the descriptor to search for
* @return the descriptor's location in the actionDescriptors array, or -1 if it was not found
*/
private int findDescriptor(ActionDescriptor descriptor) {
for (int i=0; i<actionDescriptors.length; i++)
if (descriptor == actionDescriptors[i])
return i;
return -1;
}
//----- ChangeListener method ------------
private boolean editting = false;
public void stateChanged(ChangeEvent e) {
if (!editting) {
ScrollingButtonPanel inside = (ScrollingButtonPanel)tabPane.getSelectedComponent();
int pos = inside.getSelectedIndex();
editting = true;
//Clear selections on other tabs now
for (int i=0; i<actionPanel.length; i++)
if (actionPanel[i] != inside) actionPanel[i].clearSelection();
//TO DO!!!! Get the action descriptor that was selected
if (pos == -1) {
toolbarSelection = null;
} else {
FloatingButton button = (FloatingButton)inside.getSelectedButton();
toolbarSelection = actionDescriptors[button.getIdentifier()];
}
editting = false;
if (pos == -1) viewer.setEditType(ActionListViewer.SELECT);
else viewer.setEditType(ActionListViewer.ADD);
}
}
//------ ActionListener method -----------
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals(INSERT_BEFORE)) {
viewer.insertBefore();
scroller.validate();
} else if (command.equals(INSERT_AFTER)) {
viewer.insertAfter();
scroller.validate();
} else if (command.equals(NEW_ACTIONLIST)) {
viewer.setList(actionListNamePanel.newList());
} else if (command.equals(DELETE_ACTION)) {
viewer.removeSelectedAction();
}
ProjectHandler.getInstance().projectChanged();
}
//--------- Observer method ----------------
public void update(Observable source, Object arg) {
ActionEvent e = (ActionEvent)arg;
String command = e.getActionCommand();
if ((command == PIY.NEW_PROJECT) || (command == PIY.OPEN_PROJECT)) {
viewer.setList(null);
actionListNamePanel.setup(list);
}
}
/**
* The actual view of an action list, as a series of boxes (icons). Each box can be
* clicked on - the boxes with icons in them can be dragged.
* @author David Vivash
* @version 1.0, 26/02/01
*/
private class ActionListViewer extends JPanel implements ActionListener {
private int arrowWidth = 16; //space between two actions
private int ifBeforeWidth = 32; //space between an "if" and the first action in its lists
private int ifAfterWidth = 24; //space between the last action in an "if"-list and the action after the "if"
private int spaceHeight = 4; //extra space to pad between parallel lists
private ActionIcon selected = null; //the currently selected action icon
private Color selectColor = Color.red; //the border color for the selected icon
private int selectThickness = 2; //the thickness of the border to draw
public static final int SELECT = 0;
public static final int ADD = 1;
private int editType = SELECT;
public ActionListViewer(ActionList list) {
super();
setBackground(Color.white);
setLayout(null);
if (list != null) {
restructureList(list);
//sets up the list and selects the first action in it
selectAction(setupList(list, new Point(0,0), new Dimension(getRequiredWidth(list), getRequiredHeight(list)), list.get(0)));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -