?? mainpanel.java
字號(hào):
package fi.javasom.gui;
/**
* Main panel for the Clusoe GUI.
*
* Copyright (C) 2001 Tomi Suuronen
*
* @version 1.0
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.util.Vector;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.filechooser.*;
import fi.javasom.jsom.*;
//import java.io.System;
public class MainPanel extends JPanel implements ChangeListener
{
private ExecutePanel execute;
private SettingsPanel settings;
private JFrame parent;
private PdfSettingsDialog pdfSettingsDialog;
private Dimension screenSize;
private JTabbedPane tabs;
private boolean instructionSettings; //for checking if instructions values are accepted.
private String row; //for instruction information gathering
private String area; //for notifying information gathering steps
/* Tab elements */
private JTextArea textArea;
private JTextField inputTF;
private JTextField outputTF;
private JTextField outputNameTF;
private JCheckBox xmlCB;
private JCheckBox svgCB;
private JCheckBox pdfCB;
private JFileChooser openFile;
private JFileChooser saveFolder;
private ButtonGroup latticeBG;
private JRadioButton hexaRB;
private JRadioButton rectRB;
private ButtonGroup neighborBG;
private JRadioButton stepRB;
private JRadioButton gaussianRB;
private JCheckBox normCB;
private JTextField xDimTF;
private JTextField yDimTF;
private JTextField stepsTF;
private JTextField lrateTF;
private JTextField radiusTF;
private JLabel totalL;
private JLabel currentL;
private ButtonGroup lrateTypeBG;
private JRadioButton exponentialRB;
private JRadioButton linearRB;
private JRadioButton inverseRB;
/* ordering elements*/
private Vector steps;
private Vector lrate;
private Vector radius;
private Vector lRateType;
private int currentCounter;
private int totalCounter;
/* pdf paper settings */
private JComboBox paperCB;
private ButtonGroup pdfBG;
private JRadioButton portraitRB;
private JRadioButton landscapeRB;
private JLabel textL;
/*
* Main constructor.
*/
public MainPanel(JFrame parent,Dimension screenSize)
{
this.parent = parent;
this.screenSize = screenSize;
setLayout(new BorderLayout());
tabs = new JTabbedPane();
execute = new ExecutePanel();
settings = new SettingsPanel();
tabs.addTab("Settings",settings);
tabs.addTab("Execute",execute);
add(tabs,"Center");
/* pdf paper settings */
paperCB = new JComboBox();
paperCB.setEditable(false);
paperCB.addItem("A4");
paperCB.addItem("Letter");
portraitRB = new JRadioButton("Portrait",true);
landscapeRB = new JRadioButton("Landscape",false);
/* Focus listeners */
tabs.addChangeListener(this);
}
/*
* activates the PDF settings dialog.
*/
public void activatePdfSettings()
{
if(pdfSettingsDialog == null)
{
pdfSettingsDialog = new PdfSettingsDialog(parent,screenSize);
}
pdfSettingsDialog.show();
}
/*
* Resets all the fields in the GUI.
*/
public void clearAllFields()
{
inputTF.setText("");
xDimTF.setText("");
yDimTF.setText("");
stepsTF.setText("");
lrateTF.setText("");
radiusTF.setText("");
outputTF.setText("");
normCB.setSelected(true);
hexaRB.setSelected(true);
stepRB.setSelected(true);
exponentialRB.setSelected(true);
xmlCB.setSelected(false);
svgCB.setSelected(false);
pdfCB.setSelected(false);
steps.removeAllElements();
lrate.removeAllElements();
radius.removeAllElements();
lRateType.removeAllElements();
steps.addElement("");
lrate.addElement("");
radius.addElement("");
lRateType.addElement("exponential");
totalL.setText("1");
currentL.setText("1");
currentCounter = 1;
totalCounter = 1;
if(openFile !=null)
{
openFile.setSelectedFile(null);
}
if(saveFolder !=null)
{
saveFolder.setSelectedFile(null);
}
if(pdfSettingsDialog !=null)
{
textL.setText("(297mm x 210mm)");
portraitRB.setSelected(true);
paperCB.setSelectedIndex(0);
}
outputNameTF.setText("");
textArea.setText("");
}
/**
* Selected Tab has changed.
*/
public void stateChanged(ChangeEvent ce)
{
if(execute==tabs.getSelectedComponent())
{
/* Clears the field */
textArea.setText("");
area = "";
row = "";
String cache = "";
instructionSettings = false;
/* saves the current training view */
String button = "";
steps.setElementAt(stepsTF.getText(),currentCounter-1);
lrate.setElementAt(lrateTF.getText(),currentCounter-1);
radius.setElementAt(radiusTF.getText(),currentCounter-1);
if(exponentialRB.isSelected())
{
button = "exponential";
}
else if(linearRB.isSelected())
{
button = "linear";
}
else //inverse
{
button = "inverse";
}
lRateType.setElementAt(button,currentCounter-1);
/* some variables*/
int number;
float number2;
/* Starts checking and construction of the instructions string and runs the JSOM*/
run_it:
do
{
row += "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
row += "<!DOCTYPE instructions SYSTEM \"instructions.dtd\">";
row += "<instructions>";
if(openFile == null || openFile.getSelectedFile()==null)
{
textArea.setText("No input file selected. Construction not possible!");
break;
}
else
{
area += "Input data file: "+openFile.getSelectedFile().getAbsolutePath()+" \n";
textArea.setText(area);
}
row += "<input><file>"+openFile.getSelectedFile().getAbsolutePath()+"</file></input>";
row += "<initialization>";
row += "<normalization used=\""+normCB.isSelected()+"\" />";
if(normCB.isSelected())
{
area += "Normalization: yes \n";
textArea.setText(area);
}
else
{
area += "Normalization: no \n";
textArea.setText(area);
}
try
{
number = Integer.valueOf(xDimTF.getText().trim()).intValue();
row += "<x.dimension>"+number+"</x.dimension>";
area += "X-dimension: "+number+" nodes \n";
textArea.setText(area);
}
catch(Exception e1)
{
area += "X-dimension value is not a number. Construction not possible!";
textArea.setText(area);
break;
}
try
{
number = Integer.valueOf(yDimTF.getText().trim()).intValue();
row += "<y.dimension>"+number+"</y.dimension>";
area += "Y-dimension: "+number+" nodes \n";
textArea.setText(area);
}
catch(Exception e2)
{
area += "Y-dimension value is not a number. Construction not possible!";
textArea.setText(area);
break;
}
if(hexaRB.isSelected())
{
row += "<lattice type=\"hexagonal\" />";
area += "Lattice: Hexagonal \n";
textArea.setText(area);
}
else //rectangular
{
row += "<lattice type=\"rectangular\" />";
area += "Lattice: Rectangular \n";
textArea.setText(area);
}
if(stepRB.isSelected())
{
row += "<neighbourhood type=\"step\" />";
area += "Neighbourhood: Step (bubble) \n";
textArea.setText(area);
}
else //gaussian
{
row += "<neighbourhood type=\"gaussian\" />";
area += "Neighbourhood: Gaussian \n";
textArea.setText(area);
}
row += "</initialization>";
//training
for(int j=0;j<totalCounter;j++)
{
row += "<training>";
area += "Training set: "+(j+1)+" \n";
textArea.setText(area);
//steps
try
{
number = Integer.valueOf(steps.elementAt(j).toString().trim()).intValue();
row += "<steps>"+number+"</steps>";
area += " Steps: "+number+" \n";
textArea.setText(area);
}
catch(Exception e3)
{
area += " Steps value is not a number. Construction not possible!";
textArea.setText(area);
break run_it;
}
//learning-rate
try
{
number2 = Float.valueOf(lrate.elementAt(j).toString().trim()).floatValue();
row += "<lrate type=\""+lRateType.elementAt(j).toString().trim()+"\">"+number2+"</lrate>";
area += " Learning-rate: "+number2+" \n";
if(lRateType.elementAt(j).toString().trim().equals("exponential"))
{
area += " Learning-rate type: Exponential \n";
}
else if(lRateType.elementAt(j).toString().trim().equals("linear"))
{
area += " Learning-rate type: Linear \n";
}
else
{
area += " Learning-rate type: Inverse-time \n";
}
textArea.setText(area);
}
catch(Exception e4)
{
area += " Learning-rate value is not a number. Construction not possible!";
textArea.setText(area);
break run_it;
}
//radius
try
{
number = Integer.valueOf(radius.elementAt(j).toString().trim()).intValue();
row += "<radius>"+number+"</radius>";
area += " Radius: "+number+" \n";
textArea.setText(area);
}
catch(Exception e5)
{
area += " Radius value is not a number. Construction not possible!";
textArea.setText(area);
break run_it;
}
row += "</training>";
}
//paper format
row += "<output ";
if(((String)paperCB.getSelectedItem()).equals("A4"))
{
if(portraitRB.isSelected())
{
row += "paper=\"a4\" orientation=\"portrait\">";
cache = "(A4 and Portrait)";
}
else //landscape
{
row += "paper=\"a4\" orientation=\"landscape\">";
cache = "(A4 and Landscape)";
}
}
else //Letter
{
if(portraitRB.isSelected())
{
row += "paper=\"letter\" orientation=\"portrait\">";
cache = "(Letter and Portrait)";
}
else //landscape
{
row += "paper=\"letter\" orientation=\"landscape\">";
cache = "(Letter and Landscape)";
}
}
//folder
if(saveFolder == null || saveFolder.getSelectedFile()==null)
{
area += "No output folder selected. Construction not possible!";
textArea.setText(area);
break;
}
else
{
area += "Output folder: "+saveFolder.getSelectedFile().getAbsolutePath() +" \n";
textArea.setText(area);
}
row += "<folder>"+saveFolder.getSelectedFile().getAbsolutePath()+"</folder>";
//identifier
if(outputNameTF.getText().trim().equals("") || outputNameTF.getText()==null)
{
area += "No identifier set. Construction not possible!";
textArea.setText(area);
break;
}
else
{
row += "<identifier>"+outputNameTF.getText().trim()+"</identifier>";
area += "Identifier: "+outputNameTF.getText().trim()+" \n";
textArea.setText(area);
}
//type
if(!xmlCB.isSelected() && !svgCB.isSelected() && !pdfCB.isSelected())
{
area += "No output format selected. Construction not possible!";
textArea.setText(area);
break;
}
else
{
if(xmlCB.isSelected())
{
row += "<type format=\"xml\" />";
area += "Output: "+outputNameTF.getText().trim()+".xml \n";
textArea.setText(area);
}
if(svgCB.isSelected())
{
row += "<type format=\"svg\" />";
area += "Output: "+outputNameTF.getText().trim()+".svg \n";
textArea.setText(area);
}
if(pdfCB.isSelected())
{
row += "<type format=\"pdf\" />";
area += "Output: "+outputNameTF.getText().trim()+".pdf "+cache+" \n";
textArea.setText(area);
}
}
row += "</output>";
row += "</instructions>";
area += "\n";
area += "Instruction values were accepted. Ready to construct a map \n";
textArea.setText(area);
instructionSettings = true;
break;
}
while(true);
}
}
/*********************************************************************************/
/*
* Settings Panel.
*/
private class SettingsPanel extends JPanel implements ActionListener, ItemListener
{
private JButton inputBrowse;
private JButton outputBrowse;
private JButton prevB;
private JButton nextB;
private JButton addB;
private JButton delB;
private InputFileFilter filter;
public SettingsPanel()
{
setLayout(new BorderLayout());
JPanel center = new JPanel();
center.setLayout(new BorderLayout());
add(center,"Center");
Border etched = BorderFactory.createEtchedBorder();
filter = new InputFileFilter();
currentCounter = 1;
totalCounter = 1;
steps = new Vector(0,1);
lrate = new Vector(0,1);
radius = new Vector(0,1);
lRateType = new Vector(0,1);
steps.addElement("");
lrate.addElement("");
radius.addElement("");
//input file
JPanel iPanel = new JPanel();
iPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
Border inputTitled = BorderFactory.createTitledBorder(etched,"Input");
iPanel.setBorder(inputTitled);
iPanel.add(new JLabel("File: "));
inputTF = new JTextField(32);
inputTF.setEditable(false);
iPanel.add(inputTF);
inputBrowse = new JButton("Browse");
iPanel.add(inputBrowse);
add(iPanel,"North");
//initialization
JPanel iniPanel = new JPanel();
iniPanel.setLayout(new BorderLayout());
center.add(iniPanel,"North");
Border iniTitled = BorderFactory.createTitledBorder(etched,"Initialisation");
iniPanel.setBorder(iniTitled);
JPanel ini2Panel = new JPanel();
ini2Panel.setLayout(new BorderLayout());
iniPanel.add(ini2Panel,"Center");
JPanel ini3Panel = new JPanel();
ini3Panel.setLayout(new BorderLayout());
iniPanel.add(ini3Panel,"South");
//dimension + normalization
JPanel dimPanel = new JPanel();
dimPanel.setLayout(new BorderLayout());
JPanel normPanel = new JPanel();
normPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
normCB = new JCheckBox("Use normalization",true);
normPanel.add(normCB);
JPanel xdimPanel = new JPanel();
xdimPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
xDimTF = new JTextField(5);
xdimPanel.add(new JLabel("X-dimension: "));
xdimPanel.add(xDimTF);
xdimPanel.add(new JLabel(" (nodes)"));
JPanel ydimPanel = new JPanel();
ydimPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
yDimTF = new JTextField(5);
ydimPanel.add(new JLabel("Y-dimension: "));
ydimPanel.add(yDimTF);
ydimPanel.add(new JLabel(" (nodes)"));
dimPanel.add(normPanel,"North");
dimPanel.add(xdimPanel,"Center");
dimPanel.add(ydimPanel,"South");
ini3Panel.add(dimPanel,"North");
//lattice
JPanel laPanel = new JPanel();
laPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
latticeBG = new ButtonGroup();
laPanel.add(new JLabel("Lattice type: "));
hexaRB = new JRadioButton("Hexagonal",true);
rectRB = new JRadioButton("Rectangular",false);
latticeBG.add(hexaRB);
latticeBG.add(rectRB);
laPanel.add(hexaRB);
laPanel.add(rectRB);
ini3Panel.add(laPanel,"Center");
//neighbourhood
JPanel nePanel = new JPanel();
nePanel.setLayout(new FlowLayout(FlowLayout.LEFT));
neighborBG = new ButtonGroup();
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -