Package com.mxgraph.swing

Examples of com.mxgraph.swing.mxGraphComponent


    {
      graph.getModel().endUpdate();
    }

    // Overrides method to create the editing value
    mxGraphComponent graphComponent = new mxGraphComponent(graph)
    {
      /**
       *
       */
      private static final long serialVersionUID = 6824440535661529806L;

      public String getEditingValue(Object cell, EventObject trigger)
      {
        if (cell instanceof mxCell)
        {
          Object value = ((mxCell) cell).getValue();

          if (value instanceof Element)
          {
            Element elt = (Element) value;

            if (elt.getTagName().equalsIgnoreCase("person"))
            {
              String firstName = elt.getAttribute("firstName");
              String lastName = elt.getAttribute("lastName");

              return firstName + " " + lastName;
            }
          }
        }

        return super.getEditingValue(cell, trigger);
      };

    };
   
    getContentPane().add(graphComponent);
   
    // Stops editing after enter has been pressed instead
    // of adding a newline to the current editing value
    graphComponent.setEnterStopsCellEditing(true);
  }
View Full Code Here


    finally
    {
      graph.getModel().endUpdate();
    }

    mxGraphComponent graphComponent = new mxGraphComponent(graph);
    getContentPane().add(graphComponent);
  }
View Full Code Here

    finally
    {
       graph.getModel().endUpdate();
    }
   
    final mxGraphComponent graphComponent = new mxGraphComponent(graph);
    getContentPane().add(graphComponent);
   
    graphComponent.getGraphControl().addMouseListener(new MouseAdapter()
    {
   
      public void mouseReleased(MouseEvent e)
      {
        Object cell = graphComponent.getCellAt(e.getX(), e.getY());
       
        if (cell != null)
        {
          System.out.println("cell="+graph.getLabel(cell));
        }
View Full Code Here

        "Target Must Have 1 Source", "Target Must Connect From Source",
        true);

    graph.setMultiplicities(multiplicities);

    final mxGraphComponent graphComponent = new mxGraphComponent(graph);
    graph.setMultigraph(false);
    graph.setAllowDanglingEdges(false);
    graphComponent.setConnectable(true);
    graphComponent.setToolTips(true);

    // Enables rubberband selection
    new mxRubberband(graphComponent);
    new mxKeyboardHandler(graphComponent);

    // Installs automatic validation (use editor.validation = true
    // if you are using an mxEditor instance)
    graph.getModel().addListener(mxEvent.CHANGE, new mxIEventListener()
    {
      public void invoke(Object sender, mxEventObject evt)
      {
        graphComponent.validateGraph();
      }
    });

    // Initial validation
    graphComponent.validateGraph();

    getContentPane().add(graphComponent);
  }
View Full Code Here

  private static final long serialVersionUID = 6776304509649205465L;

  @SuppressWarnings("serial")
  public SchemaEditorMenuBar(final BasicGraphEditor editor)
  {
    final mxGraphComponent graphComponent = editor.getGraphComponent();
    final mxGraph graph = graphComponent.getGraph();
    JMenu menu = null;
    JMenu submenu = null;

    // Creates the file menu
    menu = add(new JMenu(mxResources.get("file")));

    menu.add(editor.bind(mxResources.get("new"), new NewAction(),
        "/com/mxgraph/examples/swing/images/new.gif"));
    menu.add(editor.bind(mxResources.get("openFile"), new OpenAction(),
        "/com/mxgraph/examples/swing/images/open.gif"));

    menu.addSeparator();

    menu.add(editor.bind(mxResources.get("save"), new SaveAction(false),
        "/com/mxgraph/examples/swing/images/save.gif"));
    menu.add(editor.bind(mxResources.get("saveAs"), new SaveAction(true),
        "/com/mxgraph/examples/swing/images/saveas.gif"));

    menu.addSeparator();

    menu.add(editor.bind(mxResources.get("pageSetup"),
        new PageSetupAction(),
        "/com/mxgraph/examples/swing/images/pagesetup.gif"));
    menu.add(editor.bind(mxResources.get("print"), new PrintAction(),
        "/com/mxgraph/examples/swing/images/print.gif"));

    menu.addSeparator();

    menu.add(editor.bind(mxResources.get("exit"), new ExitAction()));

    // Creates the edit menu
    menu = add(new JMenu(mxResources.get("edit")));

    menu.add(editor.bind(mxResources.get("undo"), new HistoryAction(true),
        "/com/mxgraph/examples/swing/images/undo.gif"));
    menu.add(editor.bind(mxResources.get("redo"), new HistoryAction(false),
        "/com/mxgraph/examples/swing/images/redo.gif"));

    menu.addSeparator();

    menu.add(editor.bind(mxResources.get("cut"), TransferHandler
        .getCutAction(), "/com/mxgraph/examples/swing/images/cut.gif"));
    menu.add(editor
        .bind(mxResources.get("copy"), TransferHandler.getCopyAction(),
            "/com/mxgraph/examples/swing/images/copy.gif"));
    menu.add(editor.bind(mxResources.get("paste"), TransferHandler
        .getPasteAction(),
        "/com/mxgraph/examples/swing/images/paste.gif"));

    menu.addSeparator();

    menu.add(editor.bind(mxResources.get("delete"), mxGraphActions
        .getDeleteAction(),
        "/com/mxgraph/examples/swing/images/delete.gif"));

    menu.addSeparator();

    menu.add(editor.bind(mxResources.get("selectAll"), mxGraphActions
        .getSelectAllAction()));
    menu.add(editor.bind(mxResources.get("selectNone"), mxGraphActions
        .getSelectNoneAction()));

    menu.addSeparator();

    menu.add(editor.bind(mxResources.get("warning"), new WarningAction()));
    menu.add(editor.bind(mxResources.get("edit"), mxGraphActions
        .getEditAction()));

    // Creates the view menu
    menu = add(new JMenu(mxResources.get("view")));

    JMenuItem item = menu.add(new TogglePropertyItem(graphComponent,
        mxResources.get("pageLayout"), "PageVisible", true,
        new ActionListener()
        {
          /**
           *
           */
          public void actionPerformed(ActionEvent e)
          {
            if (graphComponent.isPageVisible()
                && graphComponent.isCenterPage())
            {
              graphComponent.zoomAndCenter();
            }
          }
        }));

    item.addActionListener(new ActionListener()
    {
      /*
       * (non-Javadoc)
       * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
       */
      public void actionPerformed(ActionEvent e)
      {
        if (e.getSource() instanceof TogglePropertyItem)
        {
          final mxGraphComponent graphComponent = editor
              .getGraphComponent();
          TogglePropertyItem toggleItem = (TogglePropertyItem) e
              .getSource();

          if (toggleItem.isSelected())
          {
            // Scrolls the view to the center
            SwingUtilities.invokeLater(new Runnable()
            {
              /*
               * (non-Javadoc)
               * @see java.lang.Runnable#run()
               */
              public void run()
              {
                graphComponent.scrollToCenter(true);
                graphComponent.scrollToCenter(false);
              }
            });
          }
          else
          {
            // Resets the translation of the view
            mxPoint tr = graphComponent.getGraph().getView()
                .getTranslate();

            if (tr.getX() != 0 || tr.getY() != 0)
            {
              graphComponent.getGraph().getView().setTranslate(
                  new mxPoint());
            }
          }
        }
      }
    });

    menu.add(new TogglePropertyItem(graphComponent, mxResources
        .get("antialias"), "AntiAlias", true));

    menu.addSeparator();

    menu.add(new ToggleGridItem(editor, mxResources.get("grid")));
    menu.add(new ToggleRulersItem(editor, mxResources.get("rulers")));

    menu.addSeparator();

    submenu = (JMenu) menu.add(new JMenu(mxResources.get("zoom")));

    submenu.add(editor.bind("400%", new ScaleAction(4)));
    submenu.add(editor.bind("200%", new ScaleAction(2)));
    submenu.add(editor.bind("150%", new ScaleAction(1.5)));
    submenu.add(editor.bind("100%", new ScaleAction(1)));
    submenu.add(editor.bind("75%", new ScaleAction(0.75)));
    submenu.add(editor.bind("50%", new ScaleAction(0.5)));

    submenu.addSeparator();

    submenu.add(editor.bind(mxResources.get("custom"), new ScaleAction(0)));

    menu.addSeparator();

    menu.add(editor.bind(mxResources.get("zoomIn"), mxGraphActions
        .getZoomInAction()));
    menu.add(editor.bind(mxResources.get("zoomOut"), mxGraphActions
        .getZoomOutAction()));

    menu.addSeparator();

    menu.add(editor.bind(mxResources.get("page"), new ZoomPolicyAction(
        mxGraphComponent.ZOOM_POLICY_PAGE)));
    menu.add(editor.bind(mxResources.get("width"), new ZoomPolicyAction(
        mxGraphComponent.ZOOM_POLICY_WIDTH)));

    menu.addSeparator();

    menu.add(editor.bind(mxResources.get("actualSize"), mxGraphActions
        .getZoomActualAction()));

    // Creates the diagram menu
    menu = add(new JMenu(mxResources.get("diagram")));

    menu.add(new ToggleOutlineItem(editor, mxResources.get("outline")));

    menu.addSeparator();

    submenu = (JMenu) menu.add(new JMenu(mxResources.get("background")));

    submenu.add(editor.bind(mxResources.get("backgroundColor"),
        new BackgroundAction()));
    submenu.add(editor.bind(mxResources.get("backgroundImage"),
        new BackgroundImageAction()));

    submenu.addSeparator();

    submenu.add(editor.bind(mxResources.get("pageBackground"),
        new PageBackgroundAction()));

    submenu = (JMenu) menu.add(new JMenu(mxResources.get("grid")));

    submenu.add(editor.bind(mxResources.get("gridSize"),
        new PromptPropertyAction(graph, "Grid Size", "GridSize")));
    submenu.add(editor.bind(mxResources.get("gridColor"),
        new GridColorAction()));

    submenu.addSeparator();

    submenu.add(editor.bind(mxResources.get("dashed"), new GridStyleAction(
        mxGraphComponent.GRID_STYLE_DASHED)));
    submenu.add(editor.bind(mxResources.get("dot"), new GridStyleAction(
        mxGraphComponent.GRID_STYLE_DOT)));
    submenu.add(editor.bind(mxResources.get("line"), new GridStyleAction(
        mxGraphComponent.GRID_STYLE_LINE)));
    submenu.add(editor.bind(mxResources.get("cross"), new GridStyleAction(
        mxGraphComponent.GRID_STYLE_CROSS)));

    menu.addSeparator();

    submenu = (JMenu) menu.add(new JMenu(mxResources.get("layout")));

    submenu.add(editor.graphLayout("verticalHierarchical", true));
    submenu.add(editor.graphLayout("horizontalHierarchical", true));

    submenu.addSeparator();

    submenu.add(editor.graphLayout("verticalPartition", false));
    submenu.add(editor.graphLayout("horizontalPartition", false));

    submenu.addSeparator();

    submenu.add(editor.graphLayout("verticalStack", false));
    submenu.add(editor.graphLayout("horizontalStack", false));

    submenu.addSeparator();

    submenu.add(editor.graphLayout("verticalTree", true));
    submenu.add(editor.graphLayout("horizontalTree", true));

    submenu.addSeparator();

    submenu.add(editor.graphLayout("parallelEdges", false));

    submenu.addSeparator();

    submenu.add(editor.graphLayout("organicLayout", true));

    submenu = (JMenu) menu.add(new JMenu(mxResources.get("selection")));

    submenu.add(editor.bind(mxResources.get("selectPath"),
        new SelectShortestPathAction(false)));
    submenu.add(editor.bind(mxResources.get("selectDirectedPath"),
        new SelectShortestPathAction(true)));

    submenu.addSeparator();

    submenu.add(editor.bind(mxResources.get("selectTree"),
        new SelectSpanningTreeAction(false)));
    submenu.add(editor.bind(mxResources.get("selectDirectedTree"),
        new SelectSpanningTreeAction(true)));

    menu.addSeparator();

    submenu = (JMenu) menu.add(new JMenu(mxResources.get("stylesheet")));

    submenu
        .add(editor
            .bind(
                mxResources.get("basicStyle"),
                new StylesheetAction(
                    "/com/mxgraph/examples/swing/resources/basic-style.xml")));
    submenu
        .add(editor
            .bind(
                mxResources.get("defaultStyle"),
                new StylesheetAction(
                    "/com/mxgraph/examples/swing/resources/default-style.xml")));

    // Creates the options menu
    menu = add(new JMenu(mxResources.get("options")));

    submenu = (JMenu) menu.add(new JMenu(mxResources.get("display")));
    submenu.add(new TogglePropertyItem(graphComponent, mxResources
        .get("buffering"), "TripleBuffered", true));
    submenu.add(editor.bind(mxResources.get("dirty"),
        new ToggleDirtyAction()));

    submenu.addSeparator();

    item = submenu.add(new TogglePropertyItem(graphComponent, mxResources
        .get("centerPage"), "CenterPage", true, new ActionListener()
    {
      /**
       *
       */
      public void actionPerformed(ActionEvent e)
      {
        if (graphComponent.isPageVisible()
            && graphComponent.isCenterPage())
        {
          graphComponent.zoomAndCenter();
        }
      }
    }));

    submenu.add(new TogglePropertyItem(graphComponent, mxResources
View Full Code Here

     * Saves XML+PNG format.
     */
    protected void saveXmlPng(BasicGraphEditor editor, String filename,
        Color bg) throws IOException
    {
      mxGraphComponent graphComponent = editor.getGraphComponent();
      mxGraph graph = graphComponent.getGraph();

      // Creates the image for the PNG file
      BufferedImage image = mxCellRenderer.createBufferedImage(graph,
          null, 1, bg, graphComponent.isAntiAlias(), null,
          graphComponent.getCanvas());

      // Creates the URL-encoded XML data
      mxCodec codec = new mxCodec();
      String xml = URLEncoder.encode(
          mxXmlUtils.getXml(codec.encode(graph.getModel())), "UTF-8");
View Full Code Here

    {
      BasicGraphEditor editor = getEditor(e);

      if (editor != null)
      {
        mxGraphComponent graphComponent = editor.getGraphComponent();
        mxGraph graph = graphComponent.getGraph();
        FileFilter selectedFilter = null;
        DefaultFileFilter xmlPngFilter = new DefaultFileFilter(".png",
            "PNG+XML " + mxResources.get("file") + " (.png)");
        FileFilter vmlFileFilter = new DefaultFileFilter(".html",
            "VML " + mxResources.get("file") + " (.html)");
        String filename = null;
        boolean dialogShown = false;

        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 = xmlPngFilter;
          fc.addChoosableFileFilter(defaultFilter);

          // Adds special vector graphics formats and HTML
          fc.addChoosableFileFilter(new DefaultFileFilter(".mxe",
              "mxGraph Editor " + mxResources.get("file")
                  + " (.mxe)"));
          fc.addChoosableFileFilter(new DefaultFileFilter(".txt",
              "Graph Drawing " + mxResources.get("file")
                  + " (.txt)"));
          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<String> formats = new HashSet<String>();

          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"));
          dialogShown = true;

          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)
          {
            return;
          }
        }
        else
        {
          filename = editor.getCurrentFile().getAbsolutePath();
        }

        try
        {
          String ext = filename
              .substring(filename.lastIndexOf('.') + 1);

          if (ext.equalsIgnoreCase("svg"))
          {
            mxSvgCanvas canvas = (mxSvgCanvas) mxCellRenderer
                .drawCells(graph, null, 1, null,
                    new CanvasFactory()
                    {
                      public mxICanvas createCanvas(
                          int width, int height)
                      {
                        mxSvgCanvas canvas = new mxSvgCanvas(
                            mxDomUtils.createSvgDocument(
                                width, height));
                        canvas.setEmbedded(true);

                        return canvas;
                      }

                    });

            mxUtils.writeFile(mxXmlUtils.getXml(canvas.getDocument()),
                filename);
          }
          else if (selectedFilter == vmlFileFilter)
          {
            mxUtils.writeFile(mxXmlUtils.getXml(mxCellRenderer
                .createVmlDocument(graph, null, 1, null, null)
                .getDocumentElement()), filename);
          }
          else if (ext.equalsIgnoreCase("html"))
          {
            mxUtils.writeFile(mxXmlUtils.getXml(mxCellRenderer
                .createHtmlDocument(graph, null, 1, null, null)
                .getDocumentElement()), filename);
          }
          else if (ext.equalsIgnoreCase("mxe")
              || ext.equalsIgnoreCase("xml"))
          {
            mxCodec codec = new mxCodec();
            String xml = mxXmlUtils.getXml(codec.encode(graph
                .getModel()));

            mxUtils.writeFile(xml, filename);

            editor.setModified(false);
            editor.setCurrentFile(new File(filename));
          }
          else if (ext.equalsIgnoreCase("txt"))
          {
            String content = mxGdCodec.encode(graph);

            mxUtils.writeFile(content, filename);
          }
          else
          {
            Color bg = null;

            if ((!ext.equalsIgnoreCase("gif") && !ext
                .equalsIgnoreCase("png"))
                || JOptionPane.showConfirmDialog(
                    graphComponent, mxResources
                        .get("transparentBackground")) != JOptionPane.YES_OPTION)
            {
              bg = graphComponent.getBackground();
            }

            if (selectedFilter == xmlPngFilter
                || (editor.getCurrentFile() != null
                    && ext.equalsIgnoreCase("png") && !dialogShown))
            {
              saveXmlPng(editor, filename, bg);
            }
            else
            {
              BufferedImage image = mxCellRenderer
                  .createBufferedImage(graph, null, 1, bg,
                      graphComponent.isAntiAlias(), null,
                      graphComponent.getCanvas());

              if (image != null)
              {
                ImageIO.write(image, ext, new File(filename));
              }
View Full Code Here

     */
    public void actionPerformed(ActionEvent e)
    {
      if (e.getSource() instanceof mxGraphComponent)
      {
        mxGraphComponent graphComponent = (mxGraphComponent) e
            .getSource();
        mxGraph graph = graphComponent.getGraph();
        mxIGraphModel model = graph.getModel();

        Object source = null;
        Object target = null;

View Full Code Here

     */
    public void actionPerformed(ActionEvent e)
    {
      if (e.getSource() instanceof mxGraphComponent)
      {
        mxGraphComponent graphComponent = (mxGraphComponent) e
            .getSource();
        mxGraph graph = graphComponent.getGraph();
        mxIGraphModel model = graph.getModel();

        Object parent = graph.getDefaultParent();
        Object[] cells = graph.getSelectionCells();

View Full Code Here

     */
    public void actionPerformed(ActionEvent e)
    {
      if (e.getSource() instanceof mxGraphComponent)
      {
        mxGraphComponent graphComponent = (mxGraphComponent) e
            .getSource();
        graphComponent.showDirtyRectangle = !graphComponent.showDirtyRectangle;
      }
    }
View Full Code Here

TOP

Related Classes of com.mxgraph.swing.mxGraphComponent

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.