예제 #1
0
 public void deleteNode(int node_index) {
   graphCanvas_.selectNode(node_index);
   graphCanvas_.deleteSelected(false);
 }
예제 #2
0
  public boolean action(Event event, Object what) {
    if (event.target instanceof Checkbox) {
      CheckboxGroup cbgrp = ((Checkbox) (event.target)).getCheckboxGroup();
      Checkbox selected = cbgrp.getCurrent();
      String label = selected.getLabel();

      int mode = GraphCanvas.CREATE_NODES;
      if (label.equals("Create Edges")) mode = GraphCanvas.CREATE_EDGES;
      else if (label.equals("Select Nodes")) mode = GraphCanvas.SELECT_NODES;
      else if (label.equals("Select Edges")) mode = GraphCanvas.SELECT_EDGES;
      else if (label.equals("Select Nodes or Edges")) mode = GraphCanvas.SELECT_BOTH;

      graphCanvas_.setMouseMode(mode);
    } else if (event.target instanceof CheckboxMenuItem) {
      String label = ((CheckboxMenuItem) (event.target)).getLabel();
      boolean state = ((CheckboxMenuItem) (event.target)).getState();

      if (label.equals("Scale Node Size")) graphCanvas_.scaleBounds(state);
      else if (label.equals("Show Controls  [s]")) {
        if (state) controls_.show();
        else controls_.hide();
        validate();
      } else if (label.equals("Directed")) graphCanvas_.setDirected(state);
      else if (label.equals("Use Node ID As Default Label")) {
        Node.setDefaultLabel(state);
        graphCanvas_.update(false);
      } else if (label.equals("High Quality Display"))
        graphCanvas_.setQuality(state == false ? 1 : 2);
    } else if (event.target instanceof MenuItem) {
      GraphAlgorithm alg;

      MenuItem menuitem = (MenuItem) event.target;

      alg = (GraphAlgorithm) algHashTable_.get(menuitem);
      if (alg != null) {
        applyAlgorithm_(alg);
      } else {
        String label = menuitem.getLabel();

        if (label.equals("Open Example Graph")) {
          URL src = null;
          try {
            src = new URL(context_, "example.gml");
            InputStream strm = src.openStream();
            GMLlexer lexer = new GMLlexer(strm);
            GMLfile_ = new GMLobject(lexer, null);
            GMLobject gmlgraph = GMLfile_.getGMLSubObject("graph", GMLobject.GMLlist, false);

            Graph newgraph = null;
            newgraph = new Graph(gmlgraph);
            graph_.copy(newgraph);
            graphCanvas_.update(true);

            filename_ = null;
            setTitle_();
          } catch (Exception e) {
            new MessageDialog(this, "Error", "Error loading example graph.", true);
          }
          ;

        } else if (label.equals("Open (GML)")) {
          String filename = null;

          FileDialog fd;
          try {
            fd = new FileDialog(this, "Open VGJ File (GML)", FileDialog.LOAD);
            fd.show();
          } catch (Throwable e) {
            MessageDialog dg =
                new MessageDialog(
                    this, "Error", "It appears your VM does not allow file loading.", true);
            return true;
          }
          filename = fd.getFile();
          if (filename == null) return true;
          filename = fd.getDirectory() + filename;

          loadFile(filename);
          return true;
        } else if (label.equals("Save (GML)") || label.equals("Save As (GML)")) {
          try {
            String filename;
            if (label.equals("Save As (GML)") || filename_ == null) {
              FileDialog fd;
              try {
                fd = new FileDialog(this, "Save VGJ File (GML)", FileDialog.SAVE);
                fd.show();
              } catch (Throwable e) {
                MessageDialog dg =
                    new MessageDialog(
                        this, "Error", "It appears your VM does not allow file saving.", true);
                return true;
              }

              filename = fd.getFile();
              if (filename == null) return true;

              // Work around JDK Windows bug.
              if (filename.endsWith(".*.*")) {
                String tmpstr = filename.substring(0, filename.length() - 4);
                filename = tmpstr;
              }
              filename = fd.getDirectory() + filename;
            } else filename = filename_;

            PrintStream ps = new PrintStream(new FileOutputStream(filename));
            if (GMLfile_ == null) {
              GMLfile_ = new GMLobject(null, GMLobject.GMLfile);
              GMLfile_.addObjectToEnd(new GMLobject("graph", GMLobject.GMLlist));
            }
            GMLobject gmlgraph = GMLfile_.getGMLSubObject("graph", GMLobject.GMLlist, false);
            graph_.setGMLvalues(gmlgraph);
            gmlgraph.prune();

            ps.println(GMLfile_.toString(0));
            ps.close();

            filename_ = filename;
            setTitle_();
          } catch (IOException e) {
            MessageDialog dg = new MessageDialog(this, "Error", e.getMessage(), true);
          }

        } else if (label.equals("Exit This Window")) Destroy();
        else if (label.startsWith("Exit Application")) System.exit(0);
        else if (label.equals("Delete Selected Items")) graphCanvas_.deleteSelected(true);
        else if (label.equals("Select All")) graphCanvas_.selectAll();
        else if (label.equals("Remove All Edge Bends")) graphCanvas_.removeEdgeBends();
        else if (label.equals("Remove All Groups")) graphCanvas_.removeGroups();
        else if (label.equals("Group Control")) {
          if (groupControl_ == null) groupControl_ = new GroupControl(this, graphCanvas_);
          else groupControl_.showMe();
        } else if (label.equals("Set New Node Properties")) graphCanvas_.setNodeProperties(true);
        else if (label.equals("Set Node Spacing")) {
          if (algPropDialog_ == null) algPropDialog_ = new AlgPropDialog(this, graphCanvas_);
          else algPropDialog_.showMe();
        } else if (label.equals("Set Font")) {
          if (fontPropDialog_ == null) fontPropDialog_ = new FontPropDialog(this, graphCanvas_);
          else fontPropDialog_.showMe();
        } else if (label.equals("Edit Text Representation (GML)")) {
          graphCanvas_.unselectItems();
          GraphEdit ge = new GraphEdit(graph_, graphCanvas_);
          ge.pack();
          ge.show();
        } else if (label.equals("PostScript Output")) {
          if (psDialog_ == null) psDialog_ = new PSdialog(this, graphCanvas_);
          else {
            psDialog_.pack();
            psDialog_.show();
          }
        }
      }
    } else if (event.target instanceof Button) {
      if (((String) what).equals("Scale / 2") // mod
          || ((String) what).equals("Zoom Out") // mod
      ) // mod
      {
        // scale_ /= 2.0;        // mod
        scale_ /= 1.2;
        graphCanvas_.setScale(scale_);

        scaleLabel_.setText("Scale: " + scale_);
      } else if (((String) what).equals("Scale * 2") // mod
          || ((String) what).equals("Zoom In") // mod
      ) // mod
      {
        // scale_ *= 2.0;       // mod
        scale_ *= 1.2;
        graphCanvas_.setScale(scale_);

        scaleLabel_.setText("Scale: " + scale_);
      } else if (((String) what).equals("Scale = 1")) {
        scale_ = 1.0;
        graphCanvas_.setScale(scale_);

        scaleLabel_.setText("Scale: " + scale_);
      } else if (((String) what).equals("Center")) {
        DDimension port_dim = viewingPanel_.getPortSize();
        DDimension cont_dim = viewingPanel_.getContentSize();

        double x = (cont_dim.width - port_dim.width) / 2.0;
        double y = (cont_dim.height - port_dim.height) / 2.0;

        viewingPanel_.scrollTo((int) x, (int) y);
        portScroller_.setOffset(x, y);
      } else if (((String) what).equals("Update")) {
        System.out.println("Update button pressed. " + myAction);
        if (myAction != null) {
          System.out.println("Performing action.");
          myAction.execute();
        }
      }
    }
    return true;
  }