package uk.ac.leeds.ccg.cluster; import uk.ac.leeds.ccg.geotools.*; import uk.ac.leeds.ccg.widgets.*; import java.io.*; import java.awt.*; import java.awt.event.*; /* * DatabaseViewer.java * * Created on 01 February 2001, 00:55 */ /** The database to be viewed * * * @author iant */ public class DatabaseViewer extends java.awt.Panel implements ActionListener{ private static final String Header = "$Header: /export/cvsroot/spinsrc/src/uk/ac/leeds/ccg/cluster/DatabaseViewer.java,v 1.2 2002/11/04 15:59:38 ian Exp $"; Database dbase = new Database(); FileDialog fd; FilenameFilter ff; Frame f; SearchTool search; public DatabaseViewer(Frame f) { this.f=f; initComponents(); ToolBar tb = new ToolBar(viewer1); search = new SearchTool(); tb.addTool(search); tb.setSize(200,50); // tb.add(new ScaleBar(viewer1)); // readd scalebar when next geotools released. //tb.add(new ScaleBar(viewer1)); add(tb,"South"); fd =new FileDialog(new Frame(),"File to load "); ff = new FileFil(fd,".shp"); } public DatabaseViewer(Frame f,Database db){ this(f); dbase = db; displayData(); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ private void initComponents() {//GEN-BEGIN:initComponents menuBar1 = new java.awt.MenuBar(); menu1 = new java.awt.Menu(); menuItem1 = new java.awt.MenuItem(); menuItem1.addActionListener(this); menuItem3 = new java.awt.MenuItem(); menuItem3.addActionListener(this); menuItem2 = new java.awt.MenuItem(); menuItem2.addActionListener(this); menu2 = new java.awt.Menu(); menuItem4 = new java.awt.MenuItem(); menuItem4.addActionListener(this); viewer1 = new uk.ac.leeds.ccg.geotools.Viewer(); viewer1.setSize(250,250); viewer1.setBackground(Color.white); menu1.setLabel("File"); menuItem1.setActionCommand("loaddb"); menuItem1.setLabel("Load Database"); menu1.add(menuItem1); menuItem3.setActionCommand("cases"); menuItem3.setLabel("load text file"); menu1.add(menuItem3); menuItem2.setName("Close"); menuItem2.setLabel("Quit"); menu1.add(menuItem2); menuBar1.add(menu1); menu2.setLabel("Geography"); menuItem4.setActionCommand("sload"); menuItem4.setLabel("Load Shapefile"); menu2.add(menuItem4); menuBar1.add(menu2); setLayout(new java.awt.BorderLayout()); add(viewer1, java.awt.BorderLayout.CENTER); }//GEN-END:initComponents /** handles the actions of the menubar - feel free to use a different one if you prefer * @param p1 the event */ public void actionPerformed(java.awt.event.ActionEvent p1) { String cmd = p1.getActionCommand(); // if its quit then quit! if(cmd.equalsIgnoreCase("quit")){ f.dispose(); } if(cmd.equalsIgnoreCase("exit")){ System.exit(0); } if(cmd.equalsIgnoreCase("save")){ ff = new FileFil(fd,".dbc"); fd.setTitle("File to save Database to"); fd.setFilenameFilter(ff); fd.setVisible(true); // work out which file they chose. if(fd.getFile()==null)return; File fs = new File(fd.getDirectory(),fd.getFile()); System.out.println("File = "+fs); dbase.save(fs); } if(cmd.equalsIgnoreCase("cases")){ ff = new FileFil(fd,".dat"); fd.setTitle("Case file to load"); fd.setFilenameFilter(ff); fd.setVisible(true); // work out which file they chose. if(fd.getFile()==null)return; File fs = new File(fd.getDirectory(),fd.getFile()); System.out.println("File = "+fs); dbase = new Database(fs.toString()); dbase.convertToRates(); displayData(); } if(cmd.equalsIgnoreCase("loaddb")){ dbase = new Database(); ff = new FileFil(fd,".dbc"); fd.setTitle("Database file to load"); fd.setFilenameFilter(ff); fd.setVisible(true); // work out which file they chose. if(fd.getFile()==null)return; File fs = new File(fd.getDirectory(),fd.getFile()); System.out.println("File = "+fs); dbase.load(fs); displayData(); } if(cmd.equalsIgnoreCase("sload")){ fd.setTitle("Shapefile to load"); ff = new FileFil(fd,".shp"); fd.setFilenameFilter(ff); fd.setVisible(true); // work out which file they chose. if(fd.getFile()==null)return; File fs = new File(fd.getDirectory(),fd.getFile()); System.out.println("File = "+fs); try{ // call the addShapes method in the MyCanvas3 String name = fs.getCanonicalPath(); int i = name.lastIndexOf(".shp"); //try{ ShapefileReader sr = new ShapefileReader(name.substring(0,i)); //ShapefileReader sr = new ShapefileReader(new URL("file://"+name.substring(0,i))); Theme t = sr.getTheme(); t.setStyle(new ShadeStyle(false,true,null,Color.black,true)); System.out.println(""+t); viewer1.addTheme(t); //}catch(MalformedURLException me){} }catch(FileNotFoundException e1){ }catch(IOException e2){ System.err.println("File read error"); return; } } } /** allows the frame using the viewer to have the menu bar * @return the menubar */ public MenuBar getMenuBar(){ return menuBar1; } // Variables declaration - do not modify//GEN-BEGIN:variables public java.awt.MenuBar menuBar1; private java.awt.Menu menu1; private java.awt.MenuItem menuItem1; private java.awt.MenuItem menuItem3; private java.awt.MenuItem menuItem2; private java.awt.Menu menu2; private java.awt.MenuItem menuItem4; private uk.ac.leeds.ccg.geotools.Viewer viewer1; // End of variables declaration//GEN-END:variables /** A simple test program for development work * @param args could be extended to read from command line */ public static void main(String args[]){ Frame f = new Frame(); DatabaseViewer dv = new DatabaseViewer(f); f.setSize(200,200); f.add(dv); f.setMenuBar(dv.getMenuBar()); f.setVisible(true); } public void displayData(){ GeoData gd = dbase.getCaseVals(); //Shader sp = new MyMonoShader(Color.red); Shader sp = new ClassificationShader(gd,4,ClassificationShader.EQUAL_INTERVAL); sp.setMissingValueCode(0.0); sp.setMissingValueColor(Color.yellow); System.out.println("Cases range "+gd.getMin()+" "+gd.getMax()); //sp.setRange(1.0,gd.getMax()); Theme points = new Theme(dbase); points.setShader(sp); points.setGeoData(gd); //points.setFilter(new CaseFilter(gd)); points.setStyle(new ShadeStyle(true,false)); viewer1.addTheme(points); add(sp.getKey(),"West"); } public void setDatabase(Database db){ dbase = db; displayData(); } public Database getDatabase(){ return dbase; } public GeoRectangle getSearchBox(){ return search.getBox(); } public void addGeography(Theme t){ viewer1.addTheme(t); } }