📄 editorframe.java
字号:
}
}
catch (IOException exception) {
JOptionPane.showInternalMessageDialog(desktop,
exception);
}
}
public void save()
{
GraphFrame frame
= (GraphFrame) desktop.getSelectedFrame();
if (frame == null) return;
String fileName = frame.getFileName();
if (fileName == null) { saveAs(); return; }
try
{
saveFile(frame.getGraph(), new FileOutputStream(fileName));
frame.getGraphPanel().setModified(false);
}
catch (Exception exception)
{
JOptionPane.showInternalMessageDialog(desktop,
exception);
}
}
/**
Saves the current graph as a new file.
*/
public void saveAs()
{
GraphFrame frame
= (GraphFrame)desktop.getSelectedFrame();
if (frame == null) return;
Graph graph = frame.getGraph();
try
{
FileService.Save save = fileService.save(null, frame.getFileName(), violetFilter, null, defaultExtension);
OutputStream out = save.getOutputStream();
if (out != null)
{
try
{
saveFile(graph, out);
}
finally
{
out.close();
}
frame.setFileName(save.getName());
setTitle();
frame.getGraphPanel().setModified(false);
}
}
catch (IOException exception)
{
JOptionPane.showInternalMessageDialog(desktop,
exception);
}
}
/**
Exports the current graph to an image file.
*/
public void exportImage()
{
GraphFrame frame
= (GraphFrame)desktop.getSelectedFrame();
if (frame == null) return;
try
{
String imageExtensions = editorResources.getString("files.image.extension");
FileService.Save save = fileService.save(null, frame.getFileName(), exportFilter,
defaultExtension, imageExtensions);
OutputStream out = save.getOutputStream();
if (out != null)
{
String format;
String fileName = save.getName();
if (fileName == null)
{
int n = imageExtensions.indexOf("|");
if (n < 0) n = imageExtensions.length();
format = imageExtensions.substring(1, n);
}
else
format = fileName.substring(fileName.lastIndexOf(".") + 1);
if (!ImageIO.getImageWritersByFormatName(format)
.hasNext())
{
MessageFormat formatter = new MessageFormat(
editorResources.getString("error.unsupported_image"));
JOptionPane.showInternalMessageDialog(desktop,
formatter.format(new Object[] { format }));
return;
}
Graph graph = frame.getGraph();
try
{
saveImage(graph, out, format);
}
finally
{
out.close();
}
}
}
catch (Exception exception)
{
JOptionPane.showInternalMessageDialog(desktop,
exception);
}
}
/**
Prints the current graph.
*/
public void print()
{
GraphFrame frame
= (GraphFrame)desktop.getSelectedFrame();
if (frame == null) return;
PrintDialog dialog = new PrintDialog(frame.getGraph());
dialog.setVisible(true);
}
/**
Reads a graph file
@param in the input stream to read
@return the graph that is read in
*/
public static Graph read(InputStream in)
throws IOException
{
XMLDecoder reader
= new XMLDecoder(in);
Graph graph = (Graph) reader.readObject();
in.close();
return graph;
}
/**
Saves the current graph in a file. We use long-term
bean persistence to save the program data. See
http://java.sun.com/products/jfc/tsc/articles/persistence4/index.html
for an overview.
@param out the stream for saving
*/
private static void saveFile(Graph graph, OutputStream out)
{
XMLEncoder encoder = new XMLEncoder(out);
encoder.setExceptionListener(new
ExceptionListener()
{
public void exceptionThrown(Exception ex)
{
ex.printStackTrace();
}
});
/*
The following does not work due to bug #4741757
encoder.setPersistenceDelegate(
Point2D.Double.class,
new DefaultPersistenceDelegate(
new String[]{ "x", "y" }) );
*/
encoder.setPersistenceDelegate(Point2D.Double.class, new
DefaultPersistenceDelegate()
{
protected void initialize(Class type,
Object oldInstance, Object newInstance,
Encoder out)
{
super.initialize(type, oldInstance,
newInstance, out);
Point2D p = (Point2D)oldInstance;
out.writeStatement(
new Statement(oldInstance,
"setLocation", new Object[]{ new Double(p.getX()), new Double(p.getY()) }) );
}
});
encoder.setPersistenceDelegate(BentStyle.class,
staticFieldDelegate);
encoder.setPersistenceDelegate(LineStyle.class,
staticFieldDelegate);
encoder.setPersistenceDelegate(ArrowHead.class,
staticFieldDelegate);
Graph.setPersistenceDelegate(encoder);
AbstractNode.setPersistenceDelegate(encoder);
encoder.writeObject(graph);
encoder.close();
}
/**
Exports a current graph to an image file.
@param graph the graph
@param out the output stream
@param format the image file format
*/
public static void saveImage(Graph graph, OutputStream out, String format)
throws IOException
{
BufferedImage dummy = new BufferedImage(1, 1,
BufferedImage.TYPE_INT_RGB);
// need a dummy image to get a Graphics to
// measure the size
Rectangle2D bounds = graph.getBounds(
(Graphics2D) dummy.getGraphics());
BufferedImage image
= new BufferedImage((int)bounds.getWidth() + 1,
(int)bounds.getHeight() + 1,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D)image.getGraphics();
g2.translate(-bounds.getX(), -bounds.getY());
g2.setColor(Color.WHITE);
g2.fill(new Rectangle2D.Double(
bounds.getX(),
bounds.getY(),
bounds.getWidth() + 1,
bounds.getHeight() + 1));
g2.setColor(Color.BLACK);
g2.setBackground(Color.WHITE);
graph.draw(g2, null);
ImageIO.write(image, format, out);
}
/**
Displays the About dialog box.
*/
public void showAboutDialog()
{
MessageFormat formatter = new MessageFormat(
editorResources.getString("dialog.about.version"));
JOptionPane.showInternalMessageDialog(desktop,
formatter.format(new Object[] {
appResources.getString("app.name"),
versionResources.getString("version.number"),
versionResources.getString("version.date"),
appResources.getString("app.copyright"),
editorResources.getString("dialog.about.license")}),
null,
JOptionPane.INFORMATION_MESSAGE,
new ImageIcon(
getClass().getResource(appResources.getString("app.icon"))));
}
/**
Exits the program if no graphs have been modified
or if the user agrees to abandon modified graphs.
*/
public void exit()
{
int modcount = 0;
JInternalFrame[] frames = desktop.getAllFrames();
for (int i = 0; i < frames.length; i++)
{
if (frames[i] instanceof GraphFrame)
{
GraphFrame frame = (GraphFrame)frames[i];
if (frame.getGraphPanel().isModified()) modcount++;
}
}
if (modcount > 0)
{
// ask user if it is ok to close
int result
= JOptionPane.showInternalConfirmDialog(
desktop,
MessageFormat.format(editorResources.getString("dialog.exit.ok"),
new Object[] { new Integer(modcount) }),
null,
JOptionPane.YES_NO_OPTION);
// if the user doesn't agree, veto the close
if (result != JOptionPane.YES_OPTION)
return;
}
savePreferences();
System.exit(0);
}
/**
* Saves the user preferences before exiting.
*/
public void savePreferences()
{
String recent = "";
for (int i = 0; i < Math.min(recentFiles.size(), maxRecentFiles); i++)
{
if (recent.length() > 0) recent += "|";
recent += recentFiles.get(i);
}
preferences.put("recent", recent);
}
private ResourceFactory appFactory;
private ResourceBundle appResources;
private ResourceBundle versionResources;
private ResourceBundle editorResources;
private JDesktopPane desktop;
private FileService fileService;
private PreferencesService preferences;
private JMenu newMenu;
private String defaultExtension;
private ArrayList recentFiles;
private JMenu recentFilesMenu;
private int maxRecentFiles = DEFAULT_MAX_RECENT_FILES;
private ExtensionFilter violetFilter;
private ExtensionFilter exportFilter;
private static final int FRAME_GAP = 20;
private static final int ESTIMATED_FRAMES = 5;
private static final int DEFAULT_MAX_RECENT_FILES = 5;
private static final double GROW_SCALE_FACTOR = Math.sqrt(2);
private static PersistenceDelegate staticFieldDelegate
= new
DefaultPersistenceDelegate()
{
protected Expression instantiate(Object
oldInstance, Encoder out)
{
try
{
Class cl = oldInstance.getClass();
Field[] fields = cl.getFields();
for (int i = 0; i < fields.length; i++)
{
if (Modifier.isStatic(
fields[i].getModifiers()) &&
fields[i].get(null) == oldInstance)
{
return new Expression(fields[i],
"get",
new Object[] { null });
}
}
}
catch (IllegalAccessException ex)
{
ex.printStackTrace();
}
return null;
}
protected boolean mutatesTo(
Object oldInstance, Object newInstance)
{
return oldInstance == newInstance;
}
};
// workaround for bug #4646747 in J2SE SDK 1.4.0
private static java.util.HashMap beanInfos;
static
{
beanInfos = new java.util.HashMap();
Class[] cls = new Class[]
{
Point2D.Double.class,
BentStyle.class,
ArrowHead.class,
LineStyle.class,
Graph.class,
AbstractNode.class,
};
for (int i = 0; i < cls.length; i++)
{
try
{
beanInfos.put(cls[i],
java.beans.Introspector.getBeanInfo(cls[i]));
}
catch (java.beans.IntrospectionException ex)
{
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -