/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package jgrow;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.PrintWriter;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.TextField;
import java.awt.Label;
import java.awt.Button;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.BorderFactory;
import javax.swing.SwingConstants;
import javax.swing.JFileChooser;
/**
*
* @author maryam
*/
public class WindowBonds extends JFrame implements ActionListener {
private JGrowApp _parent;
Container contentPane;
TextField [] tf;
TextField tn;
JFrame toEditBondPopUp = new JFrame("Edit Binding Domain");
TextField [] toEditStrengths;
TextField toEditName;
int toEditBondNum = -1;
public void init(JGrowApp parent)
{
JMenuBar menuBar;
JMenu menu;
JMenuItem menuItem;
setTitle("Binding Domains");
_parent = parent;
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
_parent.jv.hideBondsWindow();
}
});
contentPane = getContentPane();
menuBar = new JMenuBar();
setJMenuBar(menuBar);
menu = new JMenu("File");
menu.setMnemonic(KeyEvent.VK_F);
menu.getAccessibleContext().setAccessibleDescription("File");
menuBar.add(menu);
menuItem = new JMenuItem("Load");
menuItem.addActionListener(this);
menu.add(menuItem);
menuItem = new JMenuItem("Save");
menuItem.addActionListener(this);
menu.add(menuItem);
menuItem = new JMenuItem("Close");
menuItem.addActionListener(this);
menu.add(menuItem);
showCurrentMatrix();
contentPane.validate();
pack();
setResizable(false);
}
@Override
public Dimension getMinimumSize() {
return new Dimension(200,200);
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("Add Domain")) {
if(requestAddBindingDomain()) {
showCurrentMatrix();
if(_parent.jv.windowTiles != null) {
_parent.jv.windowTiles.showCurrentTiles();
}
}
} else if(e.getActionCommand().equals("Load")) {
JFileChooser chooser = new JFileChooser();
/*
// Note: source for ExampleFileFilter can be found in FileChooserDemo,
// under the demo/jfc directory in the Java 2 SDK, Standard Edition.
ExampleFileFilter filter = new ExampleFileFilter();
filter.addExtension("jpg");
filter.addExtension("gif");
filter.setDescription("JPG & GIF Images");
chooser.setFileFilter(filter);
*/
int returnVal = chooser.showDialog(this,"Load");
//chooser.setCurrentDirectory(".....something....");
if(returnVal == JFileChooser.APPROVE_OPTION) {
try {
BufferedReader toLoad = new BufferedReader(new FileReader(chooser.getSelectedFile()));
if(!toLoad.readLine().equals("bonds")) return;
int toLoadN = Integer.parseInt(toLoad.readLine());
for(int i=1; i<toLoadN+1; i++) {
double [] toLoadStrengths = new double[i];
for(int j=0; j<i; j++) {
toLoadStrengths[j] = Double.parseDouble(toLoad.readLine());
}
String toLoadName = toLoad.readLine();
if(!_parent.bonds.addBond(toLoadStrengths,toLoadName)) return;
}
showCurrentMatrix();
if(_parent.jv.windowTiles != null) {
_parent.jv.windowTiles.showCurrentTiles();
}
_parent.nip.updateVars();
} catch(Exception e1) {
e1.printStackTrace();
}
}
} else if(e.getActionCommand().equals("Save")) {
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showSaveDialog(this);
//chooser.setCurrentDirectory(".....something....");
if(returnVal == JFileChooser.APPROVE_OPTION) {
try {
PrintWriter toSave = new PrintWriter(new BufferedWriter(new FileWriter(chooser.getSelectedFile())));
toSave.println("bonds");
toSave.println(_parent.bonds.size());
for(int i=0; i<_parent.bonds.size(); i++) {
for(int j=0; j<i+1; j++) {
toSave.println(_parent.bonds.getElementAtIndex(j+i*_parent.bonds.size()));
}
toSave.println(_parent.bonds.names[i]);
}
toSave.flush();
} catch(Exception e1) {
e1.printStackTrace();
}
}
} else if(e.getActionCommand().indexOf("editBond") > -1) {
try {
showEditBond(Integer.parseInt(e.getActionCommand().substring(8)));
} catch(Exception e1) {
e1.printStackTrace();
}
} else if(e.getActionCommand().equals("saveBond")) {
if(requestEditBond()) {
showCurrentMatrix();
if(_parent.jv.windowTiles != null) {
_parent.jv.windowTiles.showCurrentTiles();
}
toEditBondPopUp.setVisible(false);
}
} else if(e.getActionCommand().equals("Close")) {
_parent.jv.hideBondsWindow();
}
}
public boolean requestEditBond() {
int bond_number = _parent.bonds.size();
String [] strengths = new String[bond_number];
for(int i=0; i<bond_number; i++) {
strengths[i] = toEditStrengths[i].getText();
}
return _parent.bonds.editBond(strengths,toEditName.getText(),toEditBondNum);
}
public boolean requestAddBindingDomain() {
int bond_number = _parent.bonds.size();
String [] strengths = new String[bond_number+1];
for(int i=0; i<bond_number+1; i++) {
strengths[i] = tf[i].getText();
}
return _parent.bonds.addBond(strengths,tn.getText());
}
public void showCurrentMatrix() {
contentPane.removeAll();
contentPane.setLayout(new BorderLayout());
setForeground(Color.black);
JPanel newBinding = new JPanel();
int bond_number = 0;
newBinding.setLayout(new FlowLayout());
if(_parent.bonds.isEmpty()) {
contentPane.add(new Label("No binding domains."), BorderLayout.NORTH);
tf = new TextField[1];
} else {
bond_number = _parent.bonds.size();
JPanel matrix = new JPanel();
if(bond_number > 1) contentPane.add(new Label(bond_number+" binding domains."), BorderLayout.NORTH);
else contentPane.add(new Label("1 binding domain."), BorderLayout.NORTH);
matrix.setBorder(BorderFactory.createTitledBorder("Binding Domain Interactions"));
matrix.setLayout(new GridLayout(bond_number+1, bond_number+2));
contentPane.add(matrix,BorderLayout.CENTER);
matrix.add(new Label("->"));
for(int i=0; i<bond_number; i++) {
matrix.add(new Label(_parent.bonds.getName(i)));
}
matrix.add(new Label("<-"));
for(int i=0; i < bond_number; i++) {
matrix.add(new Label(_parent.bonds.getName(i)));
for(int j = 0; j<bond_number; j++) {
matrix.add(new Label(""+_parent.bonds.getElementAtIndex(i*bond_number+j)));
}
Button editButton = new Button("edit");
editButton.setActionCommand("editBond"+i);
editButton.addActionListener(this);
matrix.add(editButton);
}
tf = new TextField[bond_number+1];
for(int i=0; i< bond_number; i++) {
JPanel matrixEntry = new JPanel();
matrixEntry.setLayout(new BorderLayout());
tf[i] = new TextField();
matrixEntry.add(new Label(_parent.bonds.getName(i),SwingConstants.CENTER),BorderLayout.NORTH);
matrixEntry.add(tf[i],BorderLayout.SOUTH);
newBinding.add(matrixEntry);
}
}
JPanel matrixEntry = new JPanel();
matrixEntry.setLayout(new BorderLayout());
tf[bond_number] = new TextField();
matrixEntry.add(new Label("Domain"+(bond_number+1),SwingConstants.CENTER),BorderLayout.NORTH);
matrixEntry.add(tf[bond_number],BorderLayout.SOUTH);
newBinding.add(matrixEntry);
matrixEntry = new JPanel();
matrixEntry.setLayout(new BorderLayout());
tn = new TextField("Domain"+(bond_number+1));
Button b1 = new Button("Add Domain");
b1.addActionListener(this);
matrixEntry.add(tn,BorderLayout.NORTH);
matrixEntry.add(b1,BorderLayout.SOUTH);
newBinding.add(matrixEntry);
newBinding.setBorder(BorderFactory.createTitledBorder("Add New Binding Domain"));
contentPane.add(newBinding,BorderLayout.SOUTH);
validate();
pack();
repaint();
}
public void showEditBond(int toEditBond) {
toEditBondNum = toEditBond;
InfoBonds tt = _parent.bonds;
JPanel matrixGen = new JPanel(new FlowLayout());
matrixGen.setBorder(BorderFactory.createTitledBorder("Edit "+tt.names[toEditBond]));
int bond_number = _parent.bonds.size();
toEditStrengths = new TextField[bond_number];
for(int j = 0; j<bond_number; j++) {
JPanel matrixGen0 = new JPanel(new BorderLayout());
toEditStrengths[j] = new TextField(""+_parent.bonds.getElementAtIndex(toEditBond*bond_number+j));
matrixGen0.add(toEditStrengths[j],BorderLayout.CENTER);
matrixGen0.add(new Label(_parent.bonds.getName(j)),BorderLayout.NORTH);
matrixGen.add(matrixGen0);
}
JPanel matrixGen0 = new JPanel(new BorderLayout());
toEditName = new TextField(_parent.bonds.getName(toEditBond));
matrixGen0.add(toEditName,BorderLayout.NORTH);
Button saveTheBond = new Button("save");
saveTheBond.setActionCommand("saveBond");
saveTheBond.addActionListener(this);
matrixGen0.add(saveTheBond);
matrixGen.add(matrixGen0);
toEditBondPopUp.getContentPane().removeAll();
toEditBondPopUp.getContentPane().add(matrixGen);
toEditBondPopUp.validate();
toEditBondPopUp.pack();
toEditBondPopUp.setVisible(true);
}
}
# |
Change |
User |
Description |
Committed |
|
#2
|
7857 |
renat_bekbolatov |
sync |
|
|
#1
|
7855 |
renat_bekbolatov |
adding windowTiles and windowBonds |
|
|