console.java

来自「gps garmingpstool-src-0[1].9.tar.gz」· Java 代码 · 共 532 行

JAVA
532
字号
package net.aetherial.gis.garmin;import java.io.*;import javax.swing.*;import java.awt.event.*;import java.awt.*;import java.net.*;import org.apache.batik.swing.*;import org.apache.batik.swing.gvt.*;import org.apache.batik.swing.svg.*;import net.aetherial.gis.garmin.*;class ConsoleFrame extends JFrame implements ActionListener{	public static Image icon;	public static int waypointType = 103;	private JMenuItem dlWaypts;	private JMenuItem dlTracks;	JMenuItem ssave;	private JSVGComponent svgCanvas;	private String svgURI = "jar:file:net.aetherial.gis.garmin.jar!/net/aetherial/gis/garmin/defaultMap.svg";	public ConsoleFrame ()	{		try		{			icon = Toolkit.getDefaultToolkit().createImage (new URL ("jar:file:net.aetherial.gis.garmin.jar!/net/aetherial/gis/garmin/icon.gif"));		}		catch (MalformedURLException e)		{			System.out.println (e);		}
		setTitle ("Garmin GPS: Uninitialized");
		setSize (548, 320);
		setIconImage (icon);

		addWindowListener (	new WindowAdapter ()
					{
						public void windowClosing (WindowEvent e)
						{
							File f = new File (GarminConfiguration.getTmpFile ());							if (f.exists () && !(svgURI.startsWith ("jar")))        							f.delete ();							System.exit (0);
						}
					} );
					

		JMenuBar menu = new JMenuBar ();
		setJMenuBar (menu);

		svgCanvas = new JSVGCanvas ();		svgCanvas.loadSVGDocument (svgURI);		svgCanvas.setSize (540, 270);		// svgCanvas.setEnableImageZoomInteractor (true);		svgCanvas.addGVTTreeRendererListener (new GVTTreeRendererListener()			{     				public void gvtRenderingCompleted(GVTTreeRendererEvent e)				{					GarminConfiguration.setSVGDoc (svgCanvas.getSVGDocument ());     				}				public void gvtRenderingPrepare (GVTTreeRendererEvent e)				{				}								public void gvtRenderingCancelled(GVTTreeRendererEvent e) 				{				}				public void gvtRenderingFailed(GVTTreeRendererEvent e) 				{				}				public void gvtRenderingStarted(GVTTreeRendererEvent e) 				{				} 			});		getContentPane ().add (svgCanvas);		JMenu fileMenu = new JMenu ("File");
		JMenuItem nnew = new JMenuItem ("New");
		nnew.addActionListener (this);
		JMenuItem save = new JMenuItem ("Save as GML");
		save.addActionListener (this);
		ssave = new JMenuItem ("Save Map as SVG");
		ssave.setEnabled (false);		ssave.addActionListener (this);
		JMenuItem exit = new JMenuItem ("Exit");
		exit.addActionListener (this);
		// fileMenu.add (nnew);
		fileMenu.add (save);
		fileMenu.add (ssave);
		fileMenu.addSeparator ();
		fileMenu.add (exit);

		JMenu gpsMenu = new JMenu ("GPS");
		dlTracks = new JMenuItem ("Download Tracks");
		dlTracks.addActionListener (this);
		dlTracks.setEnabled (false);		dlWaypts = new JMenuItem ("Download Waypoints");
		dlWaypts.addActionListener (this);
		dlWaypts.setEnabled (false);		JMenuItem initGPS = new JMenuItem ("Initialize GPS");
		initGPS.addActionListener (this);
		JMenuItem gpsInfo = new JMenuItem ("GPS Info");
		gpsInfo.addActionListener (this);
		gpsMenu.add (dlTracks);
		gpsMenu.add (dlWaypts);
		gpsMenu.add (initGPS);
		gpsMenu.addSeparator ();
		gpsMenu.add (gpsInfo);

		JMenu toolsMenu = new JMenu ("Tools");
		JMenuItem options = new JMenuItem ("Options");
		options.addActionListener (this);
		JMenuItem reset = new JMenuItem ("Reset View");
		reset.addActionListener (this);
		JMenuItem update = new JMenuItem ("Update View");		update.addActionListener (this);		toolsMenu.add (update);
		toolsMenu.add (reset);
		toolsMenu.addSeparator ();
		toolsMenu.add (options);

		JMenu helpMenu = new JMenu ("Help");
		JMenuItem about = new JMenuItem ("About");
		about.addActionListener (this);
		helpMenu.add (about);

		menu.add (fileMenu);
		menu.add (gpsMenu);
		menu.add (toolsMenu);
		menu.add (helpMenu);
	}

	public void actionPerformed (ActionEvent evt)
	{
		String arg = evt.getActionCommand ();

		if (arg.equals ("Exit"))
		{
			File f = new File (GarminConfiguration.getTmpFile ());			if (f.exists () && !(svgURI.startsWith ("jar")))        			f.delete ();			System.exit (0);
		}
		else if (arg.equals ("Open"))
		{
			JFileChooser fileDialog = new JFileChooser ();
			fileDialog.setCurrentDirectory (new File ("."));

			int result = fileDialog.showOpenDialog  (this);

			if (result == JFileChooser.APPROVE_OPTION)
			{
				String filename = fileDialog.getSelectedFile ().getName ();

				JOptionPane.showMessageDialog (this, new String (filename), new String (filename), JOptionPane.INFORMATION_MESSAGE);
			}
		}
		else if (arg.equals ("Save as GML"))
		{
			JFileChooser fileDialog = new JFileChooser ();
			fileDialog.setCurrentDirectory (new File ("."));
			 
			int result = fileDialog.showSaveDialog  (this);
						  
			if (result == JFileChooser.APPROVE_OPTION)
			{
				try 				{					File f = fileDialog.getSelectedFile ();
					String filename = f.getName ();
					if (!(filename.endsWith (".xml")))
					filename = filename + ".xml";

					f = new File (filename);

					if (f.exists ())
						JOptionPane.showMessageDialog (this, new String (filename), new String ("This file already exists. Choose another."), JOptionPane.INFORMATION_MESSAGE);
						else
					{
						f.createNewFile ();

						PrintWriter fout = new PrintWriter (new FileOutputStream (f));
						fout.print (GarminGMLDoc.printDoc ());						fout.close ();
						JOptionPane.showMessageDialog (this, new String ("Save Successful"), new String ("GPS data saved to " +filename+"."), JOptionPane.INFORMATION_MESSAGE);
					}				}				catch (FileNotFoundException ex)				{					System.out.println (ex);				}					catch (IOException ex)				{					System.out.println (ex);				}

			}

		}
		else if (arg.equals ("Save Map as SVG"))		{			JFileChooser fileDialog = new JFileChooser ();			fileDialog.setCurrentDirectory (new File ("."));			int result = fileDialog.showSaveDialog  (this);							if (result == JFileChooser.APPROVE_OPTION)			{				try				{					File svgFile = fileDialog.getSelectedFile ();					String filename = svgFile.getName ();					if (!(filename.endsWith (".svg")))        					filename = filename + ".svg";					if  (filename.endsWith ("defaultMap.svg"))						filename = filename + ".REAL.svg";									svgFile = new File (filename);					BufferedReader bin = new BufferedReader(new FileReader (GarminConfiguration.getTmpFile ()));					PrintWriter fout = new PrintWriter (new FileOutputStream (svgFile));					String line = null;					for (line = bin.readLine (); line != null; line = bin.readLine ())					{        					fout.println (line);					}					fout.close ();				}				catch (FileNotFoundException ex)        				{					System.out.println (ex);				}				catch (IOException ex)				{        				System.out.println (ex);				}			}		}		else if (arg.equals ("Options"))
		{
			JFrame optionsFrame = new ConsoleOptionsFrame ();
			optionsFrame.show ();
		}
		else if (arg.equals ("GPS Info"))
		{
			JFrame gps = new ConsoleGPSInfoFrame ();
			gps.show ();
		}
		else if (arg.equals ("About"))
		{
			JFrame about = new ConsoleAboutFrame ();
			about.show ();
		}
		else if (arg.equals ("Download Waypoints"))		{			WaypointCommand.getWaypoints ();		}		else if (arg.equals ("Download Tracks"))		{			TrackLogCommand.getTrackLogGML ();		}		else if (arg.equals ("Update View"))		{			svgURI = GarminGMLDoc.updateSVGFile (svgURI);			svgCanvas.loadSVGDocument (svgURI);			ssave.setEnabled (true);		}		else if (arg.equals ("Initialize GPS"))		{			GarminConfiguration.initReceiver ();			dlTracks.setEnabled (true);			dlWaypts.setEnabled (true);			setTitle (GarminConfiguration.getDesc ());		}		else if (arg.equals ("Reset View"))		{			setSize (548, 320);			svgCanvas.loadSVGDocument (svgURI);		}		else
		{
			JOptionPane.showMessageDialog (this, new String (arg+" not implemented."), new String (arg+" not implemented"), JOptionPane.INFORMATION_MESSAGE);
		}
	}
}

class ConsoleAboutFrame extends JFrame implements ActionListener
{
	public ConsoleAboutFrame ()
	{
		setTitle ("About");
                setSize (290, 270);
                setIconImage (ConsoleFrame.icon);
	

		getContentPane ().setLayout (new BorderLayout ());
		JButton okButton = new JButton ("OK");
		okButton.addActionListener (this);		

		Box bc = Box.createHorizontalBox ();
		bc.add (Box.createHorizontalGlue ());
		bc.add (new JLabel ("GarminGPSApp "), BorderLayout.CENTER);
		bc.add (Box.createHorizontalGlue ());

		Box bb = Box.createVerticalBox ();
		bb.add (new JSeparator ());
		bb.add (addTextLine ("This program and supporting libraries was"));
		bb.add (addTextLine ("developed as part of the thesis research"));
		bb.add (addTextLine ("of Chris Karr at Princeton University."));
		bb.add (addTextLine (" "));
		bb.add (addTextLine ("Current versions of this software and libraries"));
		bb.add (addTextLine ("is availiable online at "));
		bb.add (addTextLine (" "));
		bb.add (addTextLine ("http://garmingpsapp.sourceforge.net"));
		bb.add (addTextLine (" "));
		bb.add (addTextLine ("This program is BSD-licensed."));
		bc.add (Box.createVerticalGlue ());
		bb.add (new JSeparator ());
		

		Box bok = Box.createHorizontalBox ();
		bok.add (Box.createHorizontalGlue ());
		bok.add (okButton);
		bok.add (Box.createHorizontalGlue ());

		
		getContentPane ().add (Box.createHorizontalStrut (5), BorderLayout.EAST);
		getContentPane ().add (bc, BorderLayout.NORTH);
		getContentPane ().add (bb, BorderLayout.CENTER);
		getContentPane ().add (bok, BorderLayout.SOUTH);
		getContentPane ().add (Box.createHorizontalStrut (5), BorderLayout.WEST);
	}

	public Box addTextLine (String text)
	{
		Box textBox = Box.createHorizontalBox ();
		textBox.add (new JLabel (text));
		textBox.add (Box.createHorizontalGlue ());

		return textBox;
	}

	public void actionPerformed (ActionEvent evt)
	{
		String arg = evt.getActionCommand ();

		if (arg.equals ("OK"))
			dispose ();
	}
}

class ConsoleGPSInfoFrame extends JFrame implements ActionListener
{
	public ConsoleGPSInfoFrame ()
	{
		ProductData pd = new ProductData ();
		DateTime dt = new DateTime ();

		Box b0 = Box.createHorizontalBox ();
		Box b1 = Box.createHorizontalBox ();
		Box b8 = Box.createHorizontalBox ();
		Box b3 = Box.createHorizontalBox ();
		Box b4 = Box.createHorizontalBox ();
		Box b5 = Box.createHorizontalBox ();
		Box b6 = Box.createHorizontalBox ();
		Box b7 = Box.createHorizontalBox ();

		setTitle ("GPS Receiver Information");
		setSize (320,305);
		setIconImage (ConsoleFrame.icon);

		Box pb = Box.createVerticalBox ();
		String[] pros = pd.getPhysicalProtocols ();
		String phypro = new String ("");
		for (int i = 0; i < pros.length; i++)
		{
			phypro = new String (phypro+pros[i]+" ");
			if (i % 4 == 3)
			{
				pb.add (new JLabel (phypro));
				phypro = new String ("");
			}
		}
		pb.add (new JLabel (phypro));
		

		Box lb = Box.createVerticalBox ();
		pros = pd.getLinkProtocols ();
		String linkpro = new String ("");
		for (int i = 0; i < pros.length; i++)
		{
			linkpro = new String (linkpro+pros[i]+" ");
			if (i % 4 == 3)
			{
				lb.add (new JLabel (linkpro));
				linkpro = new String ("");
			}
		}
		lb.add (new JLabel (linkpro));
		
		Box ab = Box.createVerticalBox ();
		pros = pd.getApplicationProtocols ();
                String apppro = new String ("");
                for (int i = 0; i < pros.length; i++)
		{
       			apppro = new String (apppro+pros[i]+" ");
			if (i % 4 == 3)
			{
				ab.add (new JLabel (apppro));
				apppro = new String ("");
			}
		}
		ab.add (new JLabel (apppro));

		Box db = Box.createVerticalBox ();
		pros = pd.getDataFormats ();
                String datapro = new String ("");
                for (int i = 0; i < pros.length; i++)
		{
                        datapro = datapro + pros[i] + " ";		
			if (i % 4 == 3)
			{
				db.add (new JLabel (datapro));
				datapro = new String ("");
			}
		}
		db.add (new JLabel (datapro));
			
		b0.add (new JLabel ("Product ID:"));
		b0.add (Box.createGlue ());
		b0.add (new JLabel (""+pd.getProductID ()));
		b1.add (new JLabel ("Software Version:"));
		b1.add (Box.createGlue ());
		b1.add (new JLabel (""+pd.getSoftwareVersion ()));
		b3.add (new JLabel ("Product Description:"));
		b3.add (Box.createGlue ());
		b3.add (new JLabel (pd.getProductDescription ()));
		b4.add (new JLabel ("Physical Protocols:"));
		b4.add (Box.createGlue ());
		b4.add (pb);
		b5.add (new JLabel ("Link Protocols:"));
		b5.add (Box.createGlue ());
		b5.add (lb);
		b6.add (new JLabel ("Application Protocols:"));
		b6.add (Box.createGlue ());
		b6.add (ab);
		b7.add (new JLabel ("Data Formats:"));
		b7.add (Box.createGlue ());
		b7.add (db);
		b8.add (new JLabel ("GPS Clock:"));
		b8.add (Box.createGlue ());
		b8.add (new JLabel (dt.printDate ()));

		Box p = Box.createVerticalBox ();
		p.add (b0);
		p.add (Box.createVerticalStrut (5));
		p.add (b1);
		p.add (Box.createVerticalStrut (5));
		p.add (b3);
		p.add (Box.createVerticalStrut (5));
		p.add (b4);
		p.add (Box.createVerticalStrut (5));
		p.add (b5);
		p.add (Box.createVerticalStrut (5));
		p.add (b6);
		p.add (Box.createVerticalStrut (5));
		p.add (b7);
		p.add (Box.createVerticalStrut (5));
		p.add (b8);
		p.add (Box.createVerticalStrut (5));

		JButton okButton = new JButton ("OK");
		okButton.addActionListener (this);

		Box okBox = Box.createHorizontalBox ();
		okBox.add (Box.createHorizontalGlue ());
		okBox.add (okButton);
		okBox.add (Box.createHorizontalGlue ());

		getContentPane ().setLayout (new BorderLayout());

		getContentPane ().add (Box.createRigidArea (new Dimension (5, 5)), BorderLayout.NORTH);
		getContentPane ().add (Box.createRigidArea (new Dimension (5, 5)), BorderLayout.EAST);
		getContentPane ().add (p, BorderLayout.CENTER);
		getContentPane ().add (Box.createRigidArea (new Dimension (5, 5)), BorderLayout.WEST);
		getContentPane ().add (okBox, BorderLayout.SOUTH);
	}
 	
	public void actionPerformed (ActionEvent evt)
        {
                String arg = evt.getActionCommand ();
		if (arg.equals ("OK"))
			dispose ();
	}
}

public class Console
{	public static void main (String[] args)
	{		JFrame frame = new ConsoleFrame ();
		frame.show ();
	}}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?