⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jgpdpropertiespanel.java

📁 用JGraph编的软件
💻 JAVA
字号:
/*
 *
 * Copyright (C) 2004 David Benson
 *
 * JGpd is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * JGpd is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with JGpd; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */

package org.jgpd.UI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Map;
import javax.swing.*;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTabbedPane;
import javax.swing.border.MatteBorder;
import javax.swing.table.DefaultTableModel;

import org.jgpd.UI.tableModels.JGpdTableModel;
import org.jgpd.io.JGpdModelNode;
import org.jgpd.io.ModelExportInterface;
import org.jgraph.GPGraphpad;
import org.jgraph.event.GraphSelectionEvent;
import org.jgraph.event.GraphSelectionListener;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.pad.GPDocument;
import org.jgraph.pad.GPGraph;

public class JGpdPropertiesPanel
    extends JPanel
    implements GraphSelectionListener {
  protected GPGraph graph;
  protected GPDocument document;
  protected GPGraphpad graphpad;

  private final int NUM_TABLES = 6;

  protected JTable[] table;
  protected JGpdTableModel[] tableModel;
  protected JTabbedPane tabbedPane;
  protected boolean panelEnabled;
  // Store last cell selected as if the same cell is selected twice
  // the new selection event still fires
  protected Object lastCellSelected = null;
  // Also store userObject associated with this cell
  protected Object currentUserObject = null;
  // Map for storing applying values and sending them back
  // to export node
  protected Map properties;

  /** JGpdPropertiesPanel provides the means to alter model specific values
   * for various secondary models.
   * Must use the {@link #createPropPanel} method to create a new instance.
   */
  protected JGpdPropertiesPanel(GPGraph g, GPDocument d, GPGraphpad gp) {
    graph = g;
    document = d;
    graphpad = gp;

    ModelExportInterface exportModel = document.getExportModel();
    if (exportModel == null) {
      // FIXME log this error
      return;
    }

    tableModel = exportModel.getPropPanelModelTypes(NUM_TABLES);
    // Register as selection listener
    graph.getSelectionModel().addGraphSelectionListener(this);

    table = new JTable[NUM_TABLES];
    panelEnabled = false;

    for (int i = 0; i < NUM_TABLES; i++) {
      table[i] = new JTable(tableModel[i]);
    }

//      Properties pane added at bottom of main display
    JPanel tabbedPanel = createTabbedPanel();
    tabbedPanel.setPreferredSize(new Dimension(200, 100));

    setLayout(new BorderLayout());
    add(BorderLayout.CENTER, tabbedPanel);

    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS)); // V1.4 java calls it PAGE_AXIS
    buttonPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
// FIXME implement close button that drop the panel down		JButton closeButton = new JButton("Close"); //Translator.getString("Close") FIXME
    JButton applyButton =
        new JButton("应用"); //Translator.getString("Apply") FIXME
    JButton addButton = new JButton("添加"); //Translator.getString("Add") FIXME
    JButton deleteButton = new JButton("删除"); //Translator.getString("Delete") FIXME
    buttonPanel.add(addButton);
    buttonPanel.add(deleteButton);
    buttonPanel.add(applyButton);
// FIXME as above		buttonPanel.add(closeButton);
    add(BorderLayout.EAST, buttonPanel);
    applyButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        apply();
      }
    });
//		closeButton.addActionListener(new ActionListener() {
//			public void actionPerformed(ActionEvent e) {
    // FIXME todo need to get the current cell - apply(graph, cell, dataModel);
    // FIXME todo need to get the current cell - minimise the properties pane
//			}
//		});
    deleteButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        JOptionPane.showMessageDialog(null,"同一泳道的活动不能用消息流联接","建模错误",JOptionPane.ERROR_MESSAGE);
        int index = tabbedPane.getSelectedIndex();
        // index is -1 if no tab selected
        if (index < 0 || index > NUM_TABLES) {
          // FIXME todo, how can there be none selected?
        }
        else {
          int[] selectedRows = table[index].getSelectedRows();
          int numSelectedRows = table[index].getSelectedRowCount();
          // Remove the rows in order from highest to lowest
          // As the removal of higher numbered rows doesn't
          // effect the position of lower numbers
          int lowestRowRemoved = 20000000; // stupidly high number to start with
          int highestRowSoFar;
          for (int i = 0; i < numSelectedRows; i++) {
            highestRowSoFar = -1; // needs resetting for each i loop
            for (int j = 0; j < numSelectedRows; j++) {
              if ( (selectedRows[j] > highestRowSoFar) &&
                  (selectedRows[j] < lowestRowRemoved)) {
                // So this is the highest numbered row we've
                // found in the j loop but it isn't one of
                // the rows we've already removed
                highestRowSoFar = selectedRows[j];
              }
            }
            // Remove the last row
            ( (DefaultTableModel) (table[index].getModel()))
                .removeRow(highestRowSoFar);
            // Store this of the lowest row removed so far
            lowestRowRemoved = highestRowSoFar;
          }
        }
      }
    });
    addButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
          JOptionPane.showMessageDialog(null,"顺序流不能联接不同泳池的对象","建模错误",JOptionPane.ERROR_MESSAGE);
        int index = tabbedPane.getSelectedIndex();
        // index is -1 if no tab selected
        if (index < 0 || index > NUM_TABLES) {
          // FIXME todo, how can there be none selected?
        }
        else {
          // FIXME, any way to do this without creating
          // the unused Object[]
          ( (DefaultTableModel) (table[index].getModel()))
              .addRow(new Object[] {""});
        }
      }
    });
  }

  protected JComponent makeTablePanel(int tabIndex) {
    JPanel panel = new JPanel(false);
    JScrollPane scrollPane = new JScrollPane(table[tabIndex]);
    panel.setLayout(new GridLayout(1, 1));
    panel.add(scrollPane);
    return panel;
  }

  protected JPanel createTabbedPanel() {
    JPanel panel = new JPanel(false);
    panel.setLayout(new GridLayout(1, 1));

    tabbedPane = new JTabbedPane();

    JComponent panel1 = makeTablePanel(0);
    tabbedPane.addTab("属性", null, panel1);
    tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);

    JComponent panel2 = makeTablePanel(1);
       tabbedPane.addTab("", null, panel2,
       "Does twice as much nothing");
       tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);
       JComponent panel3 = makeTablePanel(2);
       tabbedPane.addTab("", null, panel3,
       "Still does nothing");
       tabbedPane.setMnemonicAt(2, KeyEvent.VK_3);
       JComponent panel4 = makeTablePanel(3);
       tabbedPane.addTab("", null, panel4,
       "Does nothing at all");
       tabbedPane.setMnemonicAt(3, KeyEvent.VK_4);
       JComponent panel5 = makeTablePanel(4);
       tabbedPane.addTab("", null, panel5,
       "Does nothing at all");
       tabbedPane.setMnemonicAt(4, KeyEvent.VK_5);
       JComponent panel6 = makeTablePanel(5);
       tabbedPane.addTab("", null, panel6,
       "Does nothing at all");
       tabbedPane.setMnemonicAt(5, KeyEvent.VK_6);

    // Enable this for a tab sroller
    tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);

    panel.add(tabbedPane);

    return panel;
  }

  /** JGpdPropertiesPanel factory that returns instance with small inset as a buffer.*/
  public static JPanel createPropPanel(GPGraph g,
                                       GPDocument d,
                                       GPGraphpad gp) {
    JPanel panelWithInternalOffset = new JPanel();
    panelWithInternalOffset.setLayout(new BorderLayout());
    panelWithInternalOffset.setBorder(new MatteBorder(2, 2, 2, 2, Color.WHITE));
    panelWithInternalOffset.add(new JGpdPropertiesPanel(g, d, gp),
                                BorderLayout.CENTER);
    return panelWithInternalOffset;
  }

  public void paintChildren(Graphics g) {
    super.paintChildren(g);
  }

  //
  // GraphModelListener
  //

  /**
   * Update the panel when the selection changes
   */
  public void valueChanged(GraphSelectionEvent e) {
    if (graph != null) {
      // Check only one object is selected
      if (graph.getSelectionCount() == 1) {
        // Obtain reference to the selected object
        Object cell = graph.getSelectionCell();

        // Send object hashCode to model interface to ask
        // for panel details if this panel is visible

        // FIXME todo if panel !visibile return
        if ( (document != null) && (cell != null)) {
          if (cell.equals(lastCellSelected)) {
            // if you select the same cell twice
            // this function still fires twice
            // ignore subsequent fires
          }
          else {
            lastCellSelected = cell;
            ModelExportInterface exportModel = document.getExportModel();
            if (exportModel != null) {
              currentUserObject =
                  ( (DefaultGraphCell) cell).getUserObject();
              PropPanelConfig panelDetails =
                  exportModel.getPropPanelModel
                  (currentUserObject,
                   tableModel,
                   table,
                   graphpad);
              if (panelDetails == null) {
                // This type of object does populate the properties panel
                disablePropPanel();
              }
              else {
                populatePanel(panelDetails);
              }
            }
            else {
              //						FIXME todo log error of no export model available
            }
          }
        }
        else {
          // FIXME todo log error of no document available
          // or the cell can't be obtained
        }
      }
      else {
        // For now I'm disabling the panel for more than one selection
        disablePropPanel();
      }
    }
  }

  protected void disablePropPanel() {
    if (panelEnabled == true) {
      // Only disable it if currently enabled
      // This is for efficiency
      tabbedPane.getSelectedIndex();
      for (int i = 0; i < NUM_TABLES; i++) {
        // Blank out all the tab titles and disable the tabs
        tabbedPane.setTitleAt(i, "");
        tabbedPane.setEnabledAt(i, false);
      }

      tableModel[0].setRowCount(0);

      panelEnabled = false;
      // null the lastCellSelected attribute. Otherwise you
      // can select a cell, click on empty space then select
      // the cell again and the valueChanged function will
      // ignore it thinking it is a repeat click
      lastCellSelected = null;
      currentUserObject = null;
    }
  }

  /**
   * @param details The configuration of the properties panel received
   * from the external model
   */
  protected void populatePanel(PropPanelConfig details) {
    // Select first tab
    tabbedPane.getModel().setSelectedIndex(0);
    // Fill in table details using those supplied

    for (int i = 0; i < details.numTabsUsed; i++) {
      tabbedPane.setTitleAt(i, (String) ( (details.tabStrings).get(i)));
      tabbedPane.setEnabledAt(i, true);
    }

    // Removed the names from the other tabs
    for (int i = details.numTabsUsed; i < NUM_TABLES; i++) {
      tabbedPane.setTitleAt(i, "");
      tabbedPane.setEnabledAt(i, false);
    }

    panelEnabled = true;
  }

  /**
   * Sends the applied table model to the secondary model node held
   * by the current cell
   */
  protected void apply() {
    // Ensure map is empty first
    ( (JGpdModelNode) currentUserObject).applyProperties(tableModel);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -