📄 modeldisplay.java
字号:
package jwo.jpss.spatial.gui; // Part of the spatial GUI package.
import jwo.jpss.spatial.*; // For spatial classes.
import javax.swing.*; // For Swing components
import java.awt.*; // For AWT components.
import java.awt.event.*; // For event handling.
import java.io.*; // For file handling.
// **************************************************************
/** Creates a simple window for displaying spatial models.
* @author Jo Wood.
* @version 1.3, 9th October, 2001
*/
// **************************************************************
public class ModelDisplay extends JFrame implements ActionListener
{
// --------------------- Object Variables ---------=----------
private JTextField statusBar;
private JTextArea taInfo;
private JMenuItem mOpen,mSave,mExit;
private JFileChooser fileChooser;
private JPanel p0;
private SpatialObject spObject;
private boolean isApplet;
// ----------------------- Constructor -----------------------
/** Creates a top-level application window which displays the
* given spatial object.
* @param spObject Spatial object to display.
*/
public ModelDisplay(SpatialObject spObject)
{
this(spObject,false);
}
/** Creates a top-level window which displays the given spatial object.
* @param spObject Spatial object to display.
*/
public ModelDisplay(SpatialObject spObject, boolean isApplet)
{
// Create closable window with a title and menu.
super("Spatial Model Display");
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
addWindowListener(new WinMonitor());
Container contentPane = getContentPane();
this.isApplet = isApplet;
// Perform applet-specific initialisation.
if (!isApplet)
{
JMenuBar menuBar = new JMenuBar();
JMenu menFile = new JMenu("File");
mOpen = new JMenuItem("Open...");
mOpen.addActionListener(this);
menFile.add(mOpen);
mSave = new JMenuItem("Save...");
mSave.addActionListener(this);
menFile.add(mSave);
menFile.addSeparator();
mExit = new JMenuItem("Exit");
mExit.addActionListener(this);
menFile.add(mExit);
menuBar.add(menFile);
setJMenuBar(menuBar);
// Initialse the file chooser.
fileChooser = new JFileChooser();
fileChooser.setFileFilter(new SpatialFileFilter());
}
// Place everything inside a panel with spacing border.
p0 = new JPanel(new GridLayout(1,2));
p0.setBorder(BorderFactory.createEmptyBorder(4,4,4,4));
p0.add(new JPanel());
// Set up a text area to display model information.
taInfo = new JTextArea();
taInfo.setBackground(getBackground());
taInfo.setFont(new Font("SansSerif", Font.BOLD, 12));
taInfo.setEditable(false);
taInfo.setLineWrap(true);
taInfo.setWrapStyleWord(true);
taInfo.setMargin(new Insets(4,8,4,4));
JScrollPane scrollPane = new JScrollPane(taInfo);
p0.add(scrollPane,1);
statusBar = new JTextField("Status:");
statusBar.setBackground(getBackground());
statusBar.setEditable(false);
contentPane.add(statusBar,BorderLayout.SOUTH);
// Add the model panel and a status bar.
contentPane.add(p0, BorderLayout.CENTER);
// Update panel with current spatial object.
setSpatialObject(spObject);
// Make the window visible.
pack();
setVisible(true);
}
// ------------------------ Methods --------------------------
/** Updates the display with the given spatial object.
* @param spObject New spatial object to display.
*/
public void setSpatialObject(SpatialObject spObject)
{
this.spObject = spObject;
SpatialPanel modelPanel = spObject.createPanel();
modelPanel.setOutput(statusBar);
Header header = spObject.getHeader();
taInfo.setText(header.getTitle()+"\n\n");
taInfo.append(spObject.getBounds()+"\n\n");
taInfo.append("Author: "+header.getAuthor()+"\n\n");
taInfo.append("Rights: "+header.getRights()+"\n\n");
taInfo.append("Notes: "+header.getNotes());
p0.remove(0);
p0.add(modelPanel,0);
}
/** Attempts to open the given file and update the spatial object
* being displayed.
* @param inFile Spatial file to open.
* @return True if opened successfully.
*/
public boolean openFile(File inFile)
{
// Don't allow applets to open files.
if (isApplet)
return false;
SpatialObject spObject = FileHandler.readFile(inFile);
if (spObject==null)
{
statusBar.setText("Problem opening "+inFile.getAbsolutePath());
return false;
}
setSpatialObject(spObject);
statusBar.setText("New spatial object: "+inFile.getAbsolutePath());
return true;
}
/** Attempts to save the current spatial object.
* @param outFile Spatial file to save.
* @return True if saved successfully.
*/
public boolean saveFile(File outFile)
{
// Don't allow applets to save files.
if (isApplet)
return false;
if (FileHandler.saveFile(spObject, outFile) == false)
{
statusBar.setText("Problem saving "+outFile.getAbsolutePath());
return false;
}
statusBar.setText("Saved: "+outFile.getAbsolutePath());
return true;
}
// ------------------ Implemented Methods --------------------
/** Responds to a selection of menu item.
* @param event Menu selection event.
*/
public void actionPerformed(ActionEvent event)
{
// Don't allow applets access to menus.
if (isApplet)
return;
if (event.getSource() == mExit)
closeDown(); // Close application.
if (event.getSource() == mOpen)
{
int choice = fileChooser.showOpenDialog(this);
if (choice == JFileChooser.APPROVE_OPTION)
openFile(fileChooser.getSelectedFile());
}
if (event.getSource() == mSave)
{
int choice = fileChooser.showSaveDialog(this);
if (choice == JFileChooser.APPROVE_OPTION)
saveFile(fileChooser.getSelectedFile());
}
}
// -------------------- Private Methods ----------------------
/** Asks the user if they really want to quit, then closes.
*/
private void closeDown()
{
int response = JOptionPane.showConfirmDialog(this,
"Are you sure you want to quit?");
if (response == JOptionPane.YES_OPTION)
{
if (isApplet)
dispose(); // Close window.
else
System.exit(0); // Exit program.
}
}
// -------------------- Nested Classes -----------------------
/** Monitors window closing events and performs a 'clean exit'
* when requested.
*/
private class WinMonitor extends WindowAdapter
{
/** Responds to attempt to close window via the GUI. Checks
* the user really wants to quite before closing down.
* @param event Window closing event.
*/
public void windowClosing(WindowEvent event)
{
closeDown();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -