📄 configframe.java
字号:
/*
* This file is part of JGAP.
*
* JGAP offers a dual license model containing the LGPL as well as the MPL.
*
* For licencing information please see the file license.txt included with JGAP
* or have a look at the top of class org.jgap.Chromosome which representatively
* includes the JGAP license policy applicable for any file delivered with JGAP.
*/
package org.jgap.gui;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import org.jgap.*;
import org.jgap.data.config.*;
import info.clearthought.layout.*;
/**
* GUI for the JGAP Configurator.
*
* @author Siddhartha Azad
* @since 2.3
*/
public class ConfigFrame
extends JFrame
implements IConfigInfo {
/** String containing the CVS revision. Read out via reflection!*/
private final static String CVS_REVISION = "$Revision: 1.10 $";
// data members of class ConfigFrame
private ConfigurationHandler conHandler;
private boolean isRoot;
// list of JPanel objects added in this frame
private ArrayList panels;
// ListBox properties
ArrayList listProps;
// TextBox properties
ArrayList textProps;
// list of ListGroups
ArrayList listGroups;
// list of TextGroups
ArrayList textGroups;
private JPanel listPanel;
private JPanel textPanel;
private JPanel configPanel;
private JButton configButton;
private ConfigButtonListener cbl;
private JTextField fileName;
private JButton configureButton;
private JTextField configItem;
private Configurable conObj;
// the parent frame of this frame
private ConfigFrame parent;
// default name for the config file
private static final String defaultConfigFile = "jgap.con";
/**
* Constructor
* @param title The title of the frame.
* @author Siddhartha Azad
* @since 2.3
* */
ConfigFrame(ConfigFrame _parent, String title, boolean _isRoot) {
super(title);
panels = new ArrayList();
textProps = new ArrayList();
listProps = new ArrayList();
listGroups = new ArrayList();
textGroups = new ArrayList();
cbl = new ConfigButtonListener(this);
isRoot = _isRoot;
parent = _parent;
}
/**
* Does the initial setup of the JFrame and shows it.
* @param _conHandler The configuration handler from which this
* ConfigFrame
* would get information.
* @author Siddhartha Azad.
* @since 2.3
*/
public void createAndShowGUI(ConfigurationHandler _conHandler) {
JFrame.setDefaultLookAndFeelDecorated(true);
conHandler = _conHandler;
// display
this.pack();
this.setVisible(true);
this.setBounds(100, 100, 300, 300);
this.setSize(500, 300);
try {
MetaConfig mt = MetaConfig.instance();
}
catch(MetaConfigException mcEx) {
JOptionPane.showMessageDialog( null ,
"Exception while parsing JGAP Meta Config file "+
mcEx.getMessage(),
"Meta Config Exception",
JOptionPane.ERROR_MESSAGE);
}
catch(Exception ex) {
JOptionPane.showMessageDialog( null ,
"Exception while parsing JGAP Meta Config file "+
ex.getMessage(),
"Meta Config Exception",
JOptionPane.ERROR_MESSAGE);
}
this.setup();
this.show();
if(isRoot)
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
else
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
/**
* Getter for the Configuration Information on this frame.
* @return The ConfigData object containing the configuration
* information on this frame.
* @author Siddhartha Azad
* @since 2.3
* */
public ConfigData getConfigData() {
ConfigData cd = new ConfigData();
cd.setNS(conHandler.getNS());
// add lists
ArrayList values;
try {
for(Iterator lIter = listGroups.iterator(); lIter.hasNext();) {
ListGroup lg = (ListGroup)lIter.next();
values = new ArrayList();
for(Enumeration e = lg.getOutListModel().elements() ;
e.hasMoreElements() ;) {
String val = (String)e.nextElement();
values.add(val);
}
cd.addListData(lg.getProp().getName(), values);
}
// add textFields
TextGroup tg;
for(Iterator tIter = textGroups.iterator(); tIter.hasNext();) {
tg = (TextGroup)tIter.next();
cd.addTextData(tg.getProp().getName(), tg.getTextField().getText());
}
}
catch(ClassCastException cex) {
JOptionPane.showMessageDialog( null ,
cex.getMessage(),
"ConfigFrame.getConfigData():Configuration Error",
JOptionPane.INFORMATION_MESSAGE);
}
return cd;
}
/**
* Get the config file to write to.
* @return The config file name to write to.
* @author Siddhartha Azad.
* @since 2.3
*/
public String getFileName() {
// only the root frame has the text box for the filename
if(isRoot) {
String fName = fileName.getText();
// use a default file name
if(fName.equals(""))
fName = ConfigFrame.defaultConfigFile;
return fName;
}
else
return parent.getFileName();
}
/**
* Setup the GUI.
* There are 3 maximum panels at this time. The first one contains JLists if
* there are configurable values that can be choosen from a list of items.
* The second panel contains all values configurable via a JTextField. The
* third panel contains the filename and configure button.
* @author Siddhartha Azad.
* @since 2.3
*/
private void setup() {
int numLists = 0, numTexts = 0;
ArrayList props = null;
try {
/** @todo find a better way to get the classname than getNS() */
props = MetaConfig.instance().getConfigProperty(conHandler.getNS());
}
catch(Exception ex) {
JOptionPane.showMessageDialog( null ,
ex.getMessage(),
"Configuration Error: Could not get properties for class "
+conHandler.getNS(),
JOptionPane.INFORMATION_MESSAGE);
}
if(props == null) {
JOptionPane.showMessageDialog( null ,
"setup():No Configurable Properties in this Configuration",
"Configuration Message",
JOptionPane.INFORMATION_MESSAGE);
return;
}
for(Iterator iter = props.iterator(); iter.hasNext();) {
try {
ConfigProperty prop = (ConfigProperty)iter.next();
if(prop.getWidget().equals("JList")) {
numLists++;
listProps.add(prop);
}
else if(prop.getWidget().equals("JTextField")) {
numTexts++;
textProps.add(prop);
}
else {
// Only JLists and JTextFields allowed at this point
JOptionPane.showMessageDialog( null ,
"Unknown Widget "+prop.getWidget(),
"Configuration Error",
JOptionPane.INFORMATION_MESSAGE);
}
}
catch(ClassCastException cex) {
JOptionPane.showMessageDialog( null ,
cex.getMessage(),
"ConfigError.setup():Configuration Error: Invalid cast",
JOptionPane.INFORMATION_MESSAGE);
}
}
// If no known widgets are present, a GUI cannot be rendered
if(numLists == 0 && numTexts == 0) {
JOptionPane.showMessageDialog( null ,
"No Configurable Properties in this Configuration",
"Configuration Information",
JOptionPane.INFORMATION_MESSAGE);
return;
}
// 2 panels at least, 1 for the widgets and 1 in the end for the
// Frame specific buttons
int numPanels = 2;
if(numLists > 0 && numTexts > 0)
numPanels = 3;
// add the appropriate number of panels
addWidgets(numPanels, numLists, numTexts);
}
/**
* Add the widgets to the frame.
* @param numPanels Number of panels to add.
* @param numLists Number of lists to add.
* @param numTexts Number of text boxes to add.
* @since 2.3
* */
private void addWidgets(int numPanels, int numLists, int numTexts) {
try {
int numRows = numPanels;
numPanels = 3;
// TableLayout setup for the panels on the frame
double [][] tableArray = new double[2][numPanels];
double perPanel = (double)(1.0/(double)numPanels);
int i = 0;
for(i = 0; i < numPanels-1; i++)
tableArray[1][i] = perPanel;
// give the remaining space to the last row
tableArray[1][i] = TableLayout.FILL;
// single column can take all the space available
tableArray[0][0] = TableLayout.FILL;
this.getContentPane().setLayout(new TableLayout(tableArray));
// add the panels to the frame now
int panelsAdded = 0;
JPanel panel;
// if we have lists to add
if(numLists > 0) {
double panelSize[][];
// for every input list there's an output list and the buttons
// hence 3 columns for every list
int numCols = 3 * numLists;
// TableLayout setup for the list panel
panelSize = new double [2][numCols];
double space = (double)(1.0/(double)numLists);
// 40% space to the lists, 20% to the buttons
double listSpace = space * 0.4;
double buttonSpace = space * 0.2;
for(int itr = 0; itr < numLists; itr++) {
panelSize[0][3*itr] = listSpace;
panelSize[0][3*itr+1] = buttonSpace;
panelSize[0][3*itr+2] = listSpace;
}
// single row can take all the space
panelSize[1][0] = TableLayout.FILL;
listPanel = new JPanel();
panels.add(listPanel);
listPanel.setLayout(new TableLayout(panelSize));
this.getContentPane().add(listPanel, new TableLayoutConstraints(
0, panelsAdded, 0, panelsAdded,
TableLayout.FULL, TableLayout.FULL));
// increment number of panels added
panelsAdded++;
// add the lists to the panel
Iterator iter = listProps.iterator(), valIter;
ConfigProperty prop;
ListGroup lg;
for(int itr1 = 0; itr1 < numLists && iter.hasNext(); itr1++) {
lg = new ListGroup(this);
listGroups.add(lg);
prop = (ConfigProperty)iter.next();
lg.setProp(prop);
listPanel.add(lg.getListScroller(),
new TableLayoutConstraints(3*itr1, 0, 3*itr1, 0,
TableLayout.CENTER, TableLayout.CENTER));
// add the button to move data from outlist back to list
listPanel.add(lg.getLButton(),
new TableLayoutConstraints(3*itr1+1, 0, 3*itr1+1, 0,
TableLayout.CENTER, TableLayout.TOP));
// add the button to move data from list to outlist
listPanel.add(lg.getRButton(),
new TableLayoutConstraints(3*itr1+1, 0, 3*itr1+1, 0,
TableLayout.CENTER, TableLayout.BOTTOM));
// added the item values to the list
valIter = prop.getValuesIter();
while(valIter.hasNext())
lg.getListModel().addElement(valIter.next());
listPanel.add(lg.getOutListScroller(),
new TableLayoutConstraints(3*itr1 + 2 , 0,
3*itr1 + 2, 0,
TableLayout.CENTER, TableLayout.CENTER));
}
}
// add the textFields
if(numTexts > 0) {
double panelSize[][];
int numCols = numTexts*2;
panelSize = new double [2][numCols];
// TableLayout setup for the JTextFields panel
double perText = (double)(1.0/(double)numCols);
int itr = 0;
// add the panel for the texts fields
for(itr = 0; itr < numCols-1; itr++)
panelSize[0][itr] = perText;
panelSize[0][itr] = TableLayout.FILL;
// single row
panelSize[1][0] = TableLayout.FILL;
textPanel = new JPanel();
panels.add(textPanel);
textPanel.setLayout(new TableLayout(panelSize));
this.getContentPane().add(textPanel, new TableLayoutConstraints(
0, panelsAdded, 0, panelsAdded,
TableLayout.FULL, TableLayout.FULL));
panelsAdded++;
// add the text fields to the panel
TextGroup tg;
Iterator iter = textProps.iterator(), valIter;
ConfigProperty prop;
for(int itr1 = 0; itr1 < numTexts && iter.hasNext(); itr1++) {
tg = new TextGroup();
textGroups.add(tg);
prop = (ConfigProperty)iter.next();
tg.setProp(prop);
JLabel label = tg.getLabel();
label.setText(prop.getName());
textPanel.add(label,
new TableLayoutConstraints(itr1, 0, itr1, 0,
TableLayout.RIGHT, TableLayout.CENTER));
textPanel.add(tg.getTextField(),
new TableLayoutConstraints(itr1+1, 0, itr1+1, 0,
TableLayout.LEFT, TableLayout.CENTER));
}
}
// add the configure button
double panelSize[][];
panelSize = new double [2][4];
// percentage per column for the tablelayout
panelSize[0][0] = .25;
panelSize[0][1] = .25;
panelSize[0][2] = .25;
panelSize[0][3] = .25;
// single row
panelSize[1][0] = TableLayout.FILL;
configPanel = new JPanel();
panels.add(configPanel);
configPanel.setLayout(new TableLayout(panelSize));
this.getContentPane().add(configPanel, new TableLayoutConstraints(
0, panelsAdded, 0, panelsAdded,
TableLayout.FULL, TableLayout.FULL));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -