📄 editoractions.java
字号:
/* * $Id: EditorActions.java,v 1.15 2009/04/11 10:01:25 gaudenz Exp $ * Copyright (c) 2001-2005, Gaudenz Alder * * All rights reserved. * * See LICENSE file for license details. If you are unable to locate * this file please contact info (at) jgraph (dot) com. */package com.mxgraph.swing.examples.editor;import java.awt.Color;import java.awt.Component;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.image.BufferedImage;import java.awt.print.PageFormat;import java.awt.print.Paper;import java.awt.print.PrinterException;import java.awt.print.PrinterJob;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeListener;import java.io.File;import java.io.IOException;import java.lang.reflect.Method;import java.util.HashSet;import javax.imageio.ImageIO;import javax.swing.AbstractAction;import javax.swing.ImageIcon;import javax.swing.JCheckBoxMenuItem;import javax.swing.JColorChooser;import javax.swing.JEditorPane;import javax.swing.JFileChooser;import javax.swing.JOptionPane;import javax.swing.JSplitPane;import javax.swing.SwingUtilities;import javax.swing.filechooser.FileFilter;import javax.swing.text.html.HTML;import javax.swing.text.html.HTMLDocument;import javax.swing.text.html.HTMLEditorKit;import org.w3c.dom.Document;import com.mxgraph.algebra.mxDistanceCostFunction;import com.mxgraph.algebra.mxGraphAlgebra;import com.mxgraph.io.mxCodec;import com.mxgraph.model.mxCell;import com.mxgraph.model.mxIGraphModel;import com.mxgraph.swing.mxGraphComponent;import com.mxgraph.swing.mxGraphOutline;import com.mxgraph.swing.handler.mxConnectionHandler;import com.mxgraph.swing.util.mxGraphActions;import com.mxgraph.swing.view.mxCellEditor;import com.mxgraph.util.mxCellRenderer;import com.mxgraph.util.mxConstants;import com.mxgraph.util.mxResources;import com.mxgraph.util.mxUtils;import com.mxgraph.view.mxGraph;/** * @author Administrator * */public class EditorActions{ /** * * @param e * @return Returns the graph for the given action event. */ public static final BasicGraphEditor getEditor(ActionEvent e) { if (e.getSource() instanceof Component) { Component component = (Component) e.getSource(); while (component != null && !(component instanceof BasicGraphEditor)) { component = component.getParent(); } return (BasicGraphEditor) component; } return null; } /** * */ public static class ToggleRulersItem extends JCheckBoxMenuItem { /** * */ public ToggleRulersItem(final BasicGraphEditor editor, String name) { super(name); setSelected(editor.getGraphComponent().getColumnHeader() != null); addActionListener(new ActionListener() { /** * */ public void actionPerformed(ActionEvent e) { mxGraphComponent graphComponent = editor .getGraphComponent(); if (graphComponent.getColumnHeader() != null) { graphComponent.setColumnHeader(null); graphComponent.setRowHeader(null); } else { graphComponent.setColumnHeaderView(new EditorRuler( graphComponent, EditorRuler.ORIENTATION_HORIZONTAL)); graphComponent.setRowHeaderView(new EditorRuler( graphComponent, EditorRuler.ORIENTATION_VERTICAL)); } } }); } } /** * */ public static class ToggleGridItem extends JCheckBoxMenuItem { /** * */ public ToggleGridItem(final BasicGraphEditor editor, String name) { super(name); setSelected(true); addActionListener(new ActionListener() { /** * */ public void actionPerformed(ActionEvent e) { mxGraphComponent graphComponent = editor .getGraphComponent(); mxGraph graph = graphComponent.getGraph(); boolean enabled = !graph.isGridEnabled(); graph.setGridEnabled(enabled); graphComponent.setGridVisible(enabled); graphComponent.repaint(); setSelected(enabled); } }); } } /** * */ public static class ToggleOutlineItem extends JCheckBoxMenuItem { /** * */ public ToggleOutlineItem(final BasicGraphEditor editor, String name) { super(name); setSelected(true); addActionListener(new ActionListener() { /** * */ public void actionPerformed(ActionEvent e) { final mxGraphOutline outline = editor.getGraphOutline(); outline.setVisible(!outline.isVisible()); outline.revalidate(); SwingUtilities.invokeLater(new Runnable() { /* * (non-Javadoc) * @see java.lang.Runnable#run() */ public void run() { if (outline.getParent() instanceof JSplitPane) { if (outline.isVisible()) { ((JSplitPane) outline.getParent()) .setDividerLocation(editor .getHeight() - 300); ((JSplitPane) outline.getParent()) .setDividerSize(6); } else { ((JSplitPane) outline.getParent()) .setDividerSize(0); } } } }); } }); } } /** * */ public static class ExitAction extends AbstractAction { /** * */ public void actionPerformed(ActionEvent e) { BasicGraphEditor editor = getEditor(e); if (editor != null) { editor.exit(); } } } /** * */ public static class StylesheetAction extends AbstractAction { /** * */ protected String stylesheet; /** * */ public StylesheetAction(String stylesheet) { this.stylesheet = stylesheet; } /** * */ public void actionPerformed(ActionEvent e) { if (e.getSource() instanceof mxGraphComponent) { mxGraphComponent graphComponent = (mxGraphComponent) e .getSource(); mxGraph graph = graphComponent.getGraph(); mxCodec codec = new mxCodec(); Document doc = mxUtils.loadDocument(EditorActions.class .getResource(stylesheet).toString()); if (doc != null) { codec.decode(doc.getDocumentElement(), graph .getStylesheet()); graph.refresh(); } } } } /** * */ public static class ZoomPolicyAction extends AbstractAction { /** * */ protected int zoomPolicy; /** * */ public ZoomPolicyAction(int zoomPolicy) { this.zoomPolicy = zoomPolicy; } /** * */ public void actionPerformed(ActionEvent e) { if (e.getSource() instanceof mxGraphComponent) { mxGraphComponent graphComponent = (mxGraphComponent) e .getSource(); graphComponent.setPageVisible(true); graphComponent.setZoomPolicy(zoomPolicy); } } } /** * */ public static class GridStyleAction extends AbstractAction { /** * */ protected int style; /** * */ public GridStyleAction(int style) { this.style = style; } /** * */ public void actionPerformed(ActionEvent e) { if (e.getSource() instanceof mxGraphComponent) { mxGraphComponent graphComponent = (mxGraphComponent) e .getSource(); graphComponent.setGridStyle(style); graphComponent.repaint(); } } } /** * */ public static class GridColorAction extends AbstractAction { /** * */ public void actionPerformed(ActionEvent e) { if (e.getSource() instanceof mxGraphComponent) { mxGraphComponent graphComponent = (mxGraphComponent) e .getSource(); mxGraph graph = graphComponent.getGraph(); Color newColor = JColorChooser.showDialog(graphComponent, mxResources.get("gridColor"), graphComponent .getGridColor()); if (newColor != null) { graphComponent.setGridColor(newColor); graphComponent.repaint(); } } } } /** * */ public static class ScaleAction extends AbstractAction { /** * */ protected double scale; /** * */ public ScaleAction(double scale) { this.scale = scale; } /** * */ public void actionPerformed(ActionEvent e) { if (e.getSource() instanceof mxGraphComponent) { mxGraphComponent graphComponent = (mxGraphComponent) e .getSource(); mxGraph graph = graphComponent.getGraph(); double scale = this.scale; if (scale == 0) { String value = (String) JOptionPane.showInputDialog( graphComponent, mxResources.get("value"), mxResources.get("scale") + " (%)", JOptionPane.PLAIN_MESSAGE, null, null, ""); if (value != null) { scale = Double.parseDouble(value.replace("%", "")) / 100; } } if (scale > 0) { graphComponent.zoomTo(scale, graphComponent.isCenterZoom()); } } } } /** * */ public static class PageSetupAction extends AbstractAction { /** * */ public void actionPerformed(ActionEvent e) { if (e.getSource() instanceof mxGraphComponent) { mxGraphComponent graphComponent = (mxGraphComponent) e .getSource(); PrinterJob pj = PrinterJob.getPrinterJob(); PageFormat format = pj.pageDialog(graphComponent .getPageFormat()); if (format != null) { graphComponent.setPageFormat(format); graphComponent.zoomAndCenter(); } } } } /** * */ public static class PrintAction extends AbstractAction { /** * */ public void actionPerformed(ActionEvent e) { if (e.getSource() instanceof mxGraphComponent) { mxGraphComponent graphComponent = (mxGraphComponent) e .getSource(); PrinterJob pj = PrinterJob.getPrinterJob(); if (pj.printDialog()) { PageFormat pf = graphComponent.getPageFormat(); Paper paper = new Paper(); double margin = 36; paper.setImageableArea(margin, margin, paper.getWidth() - margin * 2, paper.getHeight() - margin * 2); pf.setPaper(paper); pj.setPrintable(graphComponent, pf); try { pj.print(); } catch (PrinterException e2) { System.out.println(e2); } } } } } /** * */ public static class SaveAction extends AbstractAction { /** * */ protected boolean showDialog; /** * */ protected String lastDir = null; /** * */ public SaveAction(boolean showDialog) { this.showDialog = showDialog; } /** * */ public void actionPerformed(ActionEvent e) { BasicGraphEditor editor = getEditor(e); if (editor != null) { mxGraphComponent graphComponent = editor.getGraphComponent(); mxGraph graph = graphComponent.getGraph(); FileFilter selectedFilter = null; FileFilter vmlFileFilter = new DefaultFileFilter(".html", "VML " + mxResources.get("file") + " (.html)"); String filename = null; if (showDialog || editor.getCurrentFile() == null) { String wd; if (lastDir != null) { wd = lastDir; } else if (editor.getCurrentFile() != null) { wd = editor.getCurrentFile().getParent(); } else { wd = System.getProperty("user.dir"); } JFileChooser fc = new JFileChooser(wd); // Adds the default file format FileFilter defaultFilter = new DefaultFileFilter(".mxe", "mxGraph Editor " + mxResources.get("file") + " (.mxe)"); fc.addChoosableFileFilter(defaultFilter); // Adds special vector graphics formats and HTML fc.addChoosableFileFilter(new DefaultFileFilter(".svg", "SVG " + mxResources.get("file") + " (.svg)")); fc.addChoosableFileFilter(vmlFileFilter); fc.addChoosableFileFilter(new DefaultFileFilter(".html", "HTML " + mxResources.get("file") + " (.html)")); // Adds a filter for each supported image format Object[] imageFormats = ImageIO.getReaderFormatNames(); // Finds all distinct extensions HashSet formats = new HashSet(); for (int i = 0; i < imageFormats.length; i++) { String ext = imageFormats[i].toString().toLowerCase(); formats.add(ext); } imageFormats = formats.toArray(); for (int i = 0; i < imageFormats.length; i++) { String ext = imageFormats[i].toString(); fc.addChoosableFileFilter(new DefaultFileFilter("." + ext, ext.toUpperCase() + " " + mxResources.get("file") + " (." + ext + ")")); } // Adds filter that accepts all supported image formats fc .addChoosableFileFilter(new DefaultFileFilter.ImageFileFilter( mxResources.get("allImages"))); fc.setFileFilter(defaultFilter); int rc = fc.showDialog(null, mxResources.get("save")); if (rc != JFileChooser.APPROVE_OPTION) { return; } else { lastDir = fc.getSelectedFile().getParent(); } filename = fc.getSelectedFile().getAbsolutePath(); selectedFilter = fc.getFileFilter(); if (selectedFilter instanceof DefaultFileFilter) { String ext = ((DefaultFileFilter) selectedFilter) .getExtension(); if (!filename.toLowerCase().endsWith(ext)) { filename += ext; } } if (new File(filename).exists() && JOptionPane.showConfirmDialog(graphComponent, mxResources.get("overwriteExistingFile")) != JOptionPane.YES_OPTION)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -