/* * 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.TextField; import java.awt.Label; import java.awt.Container; import java.awt.Button; import java.awt.Choice; import java.awt.Dimension; 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.ItemListener; import java.awt.event.ActionEvent; import java.awt.event.ItemEvent; 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.JButton; import javax.swing.BorderFactory; import javax.swing.JColorChooser; import javax.swing.JFileChooser; /** * * @author maryam */ public class WindowTiles extends JFrame implements ActionListener, ItemListener { private JGrowApp _parent; Container contentPane; Choice [] tf; TextField tn,tc; JButton bcc = new JButton("TILE"); Button toEditColor; Choice []toEditSides; JFrame toEditTilePopUp = new JFrame("Edit tile"); TextField toEditName, toEditConc; int toEditTileNum = -1; public void init(JGrowApp parent) { JMenuBar menuBar; JMenu menu; JMenuItem menuItem; setTitle("Tiles"); _parent = parent; addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { _parent.jv.hideTilesWindow(); } }); 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");//,KeyEvent.VK_O); 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); showCurrentTiles(); validate(); pack(); } public Dimension getMinimumSize() { return new Dimension(300,300); } public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); //System.out.println(command); if(command.equals("Add Tile")) { if(requestAddTile()) { showCurrentTiles(); _parent.nip.updateVars(); } } else if(command.equals("Close")) { _parent.jv.hideTilesWindow(); } else if(command.equals("TILE")) { Color newColor = JColorChooser.showDialog(WindowTiles.this,"Tile Color",bcc.getBackground()); if (newColor != null) { bcc.setBackground(newColor); } } else if(command.equals("CHANGE")) { Color newColor = JColorChooser.showDialog(WindowTiles.this,"Tile Color",toEditColor.getBackground()); if (newColor != null) { toEditColor.setBackground(newColor); } } else if(command.equals("Load")) { JFileChooser chooser = new JFileChooser(); 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("tiles")) return; int toLoadN = Integer.parseInt(toLoad.readLine()); for(int i=0; i<toLoadN; i++) { int [] toLoadBonds = new int[4]; for(int j=0; j<4; j++) { toLoadBonds[j] = Integer.parseInt(toLoad.readLine()); } int toLoadColR = Integer.parseInt(toLoad.readLine()); int toLoadColG = Integer.parseInt(toLoad.readLine()); int toLoadColB = Integer.parseInt(toLoad.readLine()); String toLoadConc = toLoad.readLine(); String toLoadName = toLoad.readLine(); if(!_parent.tiles.addTile(toLoadBonds,toLoadColR,toLoadColG,toLoadColB,toLoadConc,toLoadName)) return; } // showCurrentTiles(); _parent.nip.updateVars(); if(_parent.jv.windowTiles != null) { _parent.jv.windowTiles.showCurrentTiles(); } } catch(Exception e1) { e1.printStackTrace(); } } } else if(command.equals("Save")) { 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.showSaveDialog(this); //chooser.setCurrentDirectory(".....something...."); if(returnVal == JFileChooser.APPROVE_OPTION) { try { PrintWriter toSave = new PrintWriter(new BufferedWriter(new FileWriter(chooser.getSelectedFile()))); toSave.println("tiles"); toSave.println(_parent.tiles.size()); for(int i=0; i<_parent.tiles.size(); i++) { toSave.println(_parent.tiles.north[i]); toSave.println(_parent.tiles.east[i]); toSave.println(_parent.tiles.south[i]); toSave.println(_parent.tiles.west[i]); toSave.println(_parent.tiles.colorColor[i].getRed()); toSave.println(_parent.tiles.colorColor[i].getGreen()); toSave.println(_parent.tiles.colorColor[i].getBlue()); toSave.println(_parent.tiles.concs[i]); toSave.println(_parent.tiles.names[i]); } toSave.flush(); } catch(Exception e1) { e1.printStackTrace(); } } } else if(command.indexOf("editTile") > -1) { try { showEditTile(Integer.parseInt(command.substring(8))); } catch(Exception e1) { e1.printStackTrace(); } } else if(command.equals("saveTile")) { if(requestEditTile()) { showCurrentTiles(); _parent.nip.updateVars(); toEditTilePopUp.setVisible(false); } } else if(command.indexOf("deleteTile") > -1) { try { if(requestDeleteTile(Integer.parseInt(command.substring(10)))) { showCurrentTiles(); _parent.nip.updateVars(); } } catch(Exception e1) { e1.printStackTrace(); } } } public void itemStateChanged(ItemEvent e) { } public boolean requestAddTile() { int []stf = new int[4]; stf[0] = tf[0].getSelectedIndex(); stf[1] = tf[1].getSelectedIndex(); stf[2] = tf[2].getSelectedIndex(); stf[3] = tf[3].getSelectedIndex(); return _parent.tiles.addTile(stf, bcc.getBackground(),tc.getText(),tn.getText()); } public void clean() { bcc.removeActionListener(this); contentPane.removeAll(); } public void showCurrentTiles() { clean(); contentPane.setLayout(new BorderLayout()); setForeground(Color.black); int tile_number = 0; if(_parent.tiles.isEmpty()) { contentPane.add(new Label("No Tiles."), BorderLayout.NORTH); } else { InfoTiles tt = _parent.tiles; tile_number = tt.size(); if(tile_number == 1) contentPane.add(new Label("1 Tile."), BorderLayout.NORTH); else contentPane.add(new Label(tile_number+" Tiles."), BorderLayout.NORTH); JPanel matrix = new JPanel(); matrix.setBorder(BorderFactory.createTitledBorder("Current Tiles")); //matrix.setLayout(new BorderLayout()); // matrix.setLayout(new GridLayout(0,4)); contentPane.add(matrix,BorderLayout.CENTER); for(int i=0; i<tile_number; i++) { JPanel matrixGen = new JPanel(); JPanel matrixGen0 = new JPanel(new BorderLayout()); JPanel matrixGen1 = new JPanel(new BorderLayout()); matrixGen.setBorder(BorderFactory.createTitledBorder(tt.names[i])); Label tcolor = new Label(); tcolor.setBackground(tt.colorColor[i]); matrixGen0.add(tcolor,BorderLayout.CENTER); matrixGen0.add(new Label(_parent.bonds.getName(tt.north[i]),Label.CENTER),BorderLayout.NORTH); matrixGen0.add(new Label(_parent.bonds.getName(tt.east[i]),Label.CENTER),BorderLayout.EAST); matrixGen0.add(new Label(_parent.bonds.getName(tt.south[i]),Label.CENTER),BorderLayout.SOUTH); matrixGen0.add(new Label(_parent.bonds.getName(tt.west[i]),Label.CENTER),BorderLayout.WEST); Button editNdelete = new Button("delete"); editNdelete.setActionCommand("deleteTile"+i); editNdelete.addActionListener(this); matrixGen1.add(editNdelete,BorderLayout.NORTH); matrixGen1.add(new Label("["+tt.names[i]+"] = "+tt.concs[i]),BorderLayout.CENTER); editNdelete = new Button("edit"); editNdelete.setActionCommand("editTile"+i); editNdelete.addActionListener(this); matrixGen1.add(editNdelete,BorderLayout.SOUTH); /* JFrame toEditTilePopUp = new JFrame("Edit tile"); toEditColor = new Button("CHANGE"); toEditColor.setBackground(tt.colorColor[i]); toEditColor.addActionListener(this); matrixGen0.add(toEditColor,BorderLayout.CENTER); toEditSides = new Choice[4]; toEditSides[0] = new Choice(); toEditSides[0].addItemListener(this); toEditSides[1] = new Choice(); toEditSides[1].addItemListener(this); toEditSides[2] = new Choice(); toEditSides[2].addItemListener(this); toEditSides[3] = new Choice(); toEditSides[3].addItemListener(this); for(int ii=0; ii<_parent.bonds.size(); ii++) { toEditSides[0].add(_parent.bonds.getName(ii)); toEditSides[1].add(_parent.bonds.getName(ii)); toEditSides[2].add(_parent.bonds.getName(ii)); toEditSides[3].add(_parent.bonds.getName(ii)); } toEditSides[0].select(tt.north[i]); toEditSides[1].select(tt.east[i]); toEditSides[2].select(tt.south[i]); toEditSides[3].select(tt.west[i]); matrixGen0.add(toEditSides[0],BorderLayout.NORTH); matrixGen0.add(toEditSides[1],BorderLayout.EAST); matrixGen0.add(toEditSides[2],BorderLayout.SOUTH); matrixGen0.add(toEditSides[3],BorderLayout.WEST); Button editNdelete = new Button("cancel"); editNdelete.setActionCommand("cancelTile"+i); editNdelete.addActionListener(this); matrixGen1.add(editNdelete,BorderLayout.NORTH); matrixGen1.add(new Label("["+tt.names[i]+"] = "+tt.concs[i]),BorderLayout.CENTER); editNdelete = new Button("save"); editNdelete.setActionCommand("saveTile"+i); editNdelete.addActionListener(this); matrixGen1.add(editNdelete,BorderLayout.SOUTH); } */ matrixGen.add(matrixGen0); matrixGen.add(matrixGen1); matrix.add(matrixGen); } } JPanel newTile = new JPanel(); newTile.setLayout(new FlowLayout()); tf = new Choice[4]; JPanel matrixEntry; matrixEntry = new JPanel(); matrixEntry.setLayout(new BorderLayout()); tf[0] = new Choice(); tf[0].addItemListener(this); tf[1] = new Choice(); tf[1].addItemListener(this); tf[2] = new Choice(); tf[2].addItemListener(this); tf[3] = new Choice(); tf[3].addItemListener(this); for(int i=0; i<_parent.bonds.size(); i++) { tf[0].add(_parent.bonds.getName(i)); tf[1].add(_parent.bonds.getName(i)); tf[2].add(_parent.bonds.getName(i)); tf[3].add(_parent.bonds.getName(i)); } matrixEntry.add(tf[0],BorderLayout.NORTH); matrixEntry.add(tf[1],BorderLayout.EAST); matrixEntry.add(tf[2],BorderLayout.SOUTH); matrixEntry.add(tf[3],BorderLayout.WEST); bcc.addActionListener(this); matrixEntry.add(bcc,BorderLayout.CENTER); newTile.add(matrixEntry); matrixEntry = new JPanel(); matrixEntry.setLayout(new BorderLayout()); tc = new TextField(); matrixEntry.add(new Label("Concentration"),BorderLayout.NORTH); matrixEntry.add(tc,BorderLayout.SOUTH); newTile.add(matrixEntry); matrixEntry = new JPanel(); matrixEntry.setLayout(new BorderLayout()); matrixEntry = new JPanel(); matrixEntry.setLayout(new BorderLayout()); tn = new TextField("Tile"+(tile_number+1)); matrixEntry.add(tn,BorderLayout.NORTH); Button b1 = new Button("Add Tile"); b1.addActionListener(this); matrixEntry.add(b1,BorderLayout.SOUTH); newTile.add(matrixEntry); newTile.setBorder(BorderFactory.createTitledBorder("Add New Tile")); contentPane.add(newTile,BorderLayout.SOUTH); validate(); pack(); repaint(); } public void showEditTile(int toEditTile) { toEditTileNum = toEditTile; InfoTiles tt = _parent.tiles; JPanel matrixGen = new JPanel(); JPanel matrixGen0 = new JPanel(new BorderLayout()); JPanel matrixGen1 = new JPanel(new BorderLayout()); matrixGen.setBorder(BorderFactory.createTitledBorder("Edit "+tt.names[toEditTile])); toEditColor = new Button("CHANGE"); toEditColor.setBackground(tt.colorColor[toEditTile]); toEditColor.addActionListener(this); matrixGen0.add(toEditColor,BorderLayout.CENTER); toEditSides = new Choice[4]; toEditSides[0] = new Choice(); toEditSides[0].addItemListener(this); toEditSides[1] = new Choice(); toEditSides[1].addItemListener(this); toEditSides[2] = new Choice(); toEditSides[2].addItemListener(this); toEditSides[3] = new Choice(); toEditSides[3].addItemListener(this); for(int ii=0; ii<_parent.bonds.size(); ii++) { toEditSides[0].add(_parent.bonds.getName(ii)); toEditSides[1].add(_parent.bonds.getName(ii)); toEditSides[2].add(_parent.bonds.getName(ii)); toEditSides[3].add(_parent.bonds.getName(ii)); } toEditSides[0].select(tt.north[toEditTile]); toEditSides[1].select(tt.east[toEditTile]); toEditSides[2].select(tt.south[toEditTile]); toEditSides[3].select(tt.west[toEditTile]); matrixGen0.add(toEditSides[0],BorderLayout.NORTH); matrixGen0.add(toEditSides[1],BorderLayout.EAST); matrixGen0.add(toEditSides[2],BorderLayout.SOUTH); matrixGen0.add(toEditSides[3],BorderLayout.WEST); matrixGen1.add(new Label("["+tt.names[toEditTile]+"] = "+tt.concs[toEditTile]),BorderLayout.CENTER); toEditName = new TextField(tt.names[toEditTile]); matrixGen1.add(toEditName,BorderLayout.NORTH); toEditConc = new TextField(""+tt.concs[toEditTile]); matrixGen1.add(toEditConc,BorderLayout.CENTER); Button editNdelete = new Button("save"); editNdelete.setActionCommand("saveTile"); editNdelete.addActionListener(this); matrixGen1.add(editNdelete,BorderLayout.SOUTH); matrixGen.add(matrixGen0); matrixGen.add(matrixGen1); toEditTilePopUp.getContentPane().removeAll(); toEditTilePopUp.getContentPane().add(matrixGen); toEditTilePopUp.validate(); toEditTilePopUp.pack(); toEditTilePopUp.setLocation(256,256); toEditTilePopUp.setVisible(true); } public boolean requestDeleteTile(int tileToRemove) { return _parent.tiles.deleteTile(tileToRemove); } public boolean requestEditTile() { int []stf = new int[4]; stf[0] = toEditSides[0].getSelectedIndex(); stf[1] = toEditSides[1].getSelectedIndex(); stf[2] = toEditSides[2].getSelectedIndex(); stf[3] = toEditSides[3].getSelectedIndex(); return _parent.tiles.editTile(stf, toEditColor.getBackground(),toEditConc.getText(),toEditName.getText(),toEditTileNum); } }