attributestable.java
来自「The ElectricTM VLSI Design System is an 」· Java 代码 · 共 893 行 · 第 1/2 页
JAVA
893 行
/* -*- tab-width: 4 -*- * * Electric(tm) VLSI Design System * * File: AttributesTable.java * * Copyright (c) 2004 Sun Microsystems and Static Free Software * * Electric(tm) 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 3 of the License, or * (at your option) any later version. * * Electric(tm) 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 Electric(tm); see the file COPYING. If not, write to * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, Mass 02111-1307, USA. */package com.sun.electric.tool.user.dialogs;import com.sun.electric.database.change.DatabaseChangeEvent;import com.sun.electric.database.change.DatabaseChangeListener;import com.sun.electric.database.hierarchy.Cell;import com.sun.electric.database.topology.NodeInst;import com.sun.electric.database.variable.CodeExpression;import com.sun.electric.database.variable.ElectricObject;import com.sun.electric.database.variable.TextDescriptor;import com.sun.electric.database.variable.Variable;import com.sun.electric.tool.Job;import com.sun.electric.tool.JobException;import com.sun.electric.tool.user.User;import com.sun.electric.tool.user.UserInterfaceMain;import java.awt.Font;import java.awt.Point;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.Iterator;import java.util.List;import javax.swing.DefaultCellEditor;import javax.swing.JCheckBoxMenuItem;import javax.swing.JComboBox;import javax.swing.JMenu;import javax.swing.JMenuItem;import javax.swing.JOptionPane;import javax.swing.JPopupMenu;import javax.swing.JTable;import javax.swing.ListSelectionModel;import javax.swing.table.AbstractTableModel;import javax.swing.table.TableCellEditor;import javax.swing.table.TableColumn;/** * Class to define the Attributes panel in other dialogs. */public class AttributesTable extends JTable implements DatabaseChangeListener{ /** * Class to define attributes on nodes or arcs. */ public static class AttValPair { Variable.Key key; String trueName; String value; String eval; boolean code; } /** * Model for storing Table data */ private static class VariableTableModel extends AbstractTableModel { private List<VarEntry> vars; // list of variables to display private static final String [] columnNames = { "Name", "Value", "Code", "Display", "Units" }; private boolean showCode = true; private boolean showDispPos = false; private boolean showUnits = false; private List<VarEntry> varsToDelete; private static class VarEntry { // current state of var entry private String varTrueName; private Variable.Key varKey; private Object value; private CodeExpression.Code code; private TextDescriptor.DispPos dispPos; private TextDescriptor.Unit units; private boolean display; private ElectricObject owner; // if var is null, this means create a new var // if any of the above values are different from the original (below) var's value, modify // if this is in the varsToDelete list, delete it private Variable var; // initial state of var entry private String initialVarTrueName; private Object initialValue; private CodeExpression.Code initialCode; private TextDescriptor.DispPos initialDispPos; private TextDescriptor.Unit initialUnits; private boolean initialDisplay; private VarEntry(ElectricObject owner, Variable var) { this.owner = owner; this.var = var; if (var == null) return; varKey = var.getKey(); varTrueName = initialVarTrueName = var.getTrueName(); value = initialValue = var.getObject(); code = initialCode = var.getCode(); dispPos = initialDispPos = var.getDispPart(); units = initialUnits = var.getUnit(); display = initialDisplay = var.isDisplay(); } private String getName() { return varTrueName; } private Object getObject() { return value; } private CodeExpression.Code getCode() { return code; } private TextDescriptor.DispPos getDispPos() { return dispPos; } private TextDescriptor.Unit getUnits() { return units; } private boolean isDisplay() { return display; } private ElectricObject getOwner() { return owner; } private Variable.Key getKey() { return varKey; } private boolean isChanged() { if (!varTrueName.equals(initialVarTrueName)) return true; if (value != initialValue) return true; if (code != initialCode) return true; if (display != initialDisplay) return true; if (dispPos != initialDispPos) return true; if (units != initialUnits) return true; return false; } } /** * Class to sort Variables by name. */ public static class VarEntrySort implements Comparator<VarEntry> { public int compare(VarEntry v1, VarEntry v2) { String s1 = v1.getName(); String s2 = v2.getName(); return s1.compareToIgnoreCase(s2); } } // constructor private VariableTableModel(boolean showCode, boolean showDispPos, boolean showUnits) { vars = new ArrayList<VarEntry>(); varsToDelete = new ArrayList<VarEntry>(); this.showCode = showCode; this.showDispPos = showDispPos; this.showUnits = showUnits; } /** Get number of columns in table */ public int getColumnCount() { int c = 2; if (showCode) c++; if (showDispPos) c++; if (showUnits) c++; return c; } /** Get number of rows in table */ public int getRowCount() { return vars.size(); } private int getCodeColumn() { if (!showCode) return -1; return 2; } private int getDispColumn() { if (!showDispPos) return -1; int c = 2; if (showCode) c++; return c; } private int getUnitsColumn() { if (!showUnits) return -1; int c = 2; if (showCode) c++; if (showDispPos) c++; return c; } /** Get object at location in table */ public Object getValueAt(int rowIndex, int columnIndex) { // order: name, value VarEntry ve = vars.get(rowIndex); if (ve == null) return null; if (columnIndex == 0) { // name return ve.getName(); } if (columnIndex == 1) { // value return ve.getObject().toString(); } if (columnIndex == getCodeColumn()) { return ve.getCode(); } if (columnIndex == getDispColumn()) { if (!ve.isDisplay()) return displaynone; return ve.getDispPos(); } if (columnIndex == getUnitsColumn()) { return ve.getUnits(); } return null; } /** Get column header names */ public String getColumnName(int col) { if (col < 2) return columnNames[col]; if (col == getCodeColumn()) return columnNames[2]; if (col == getDispColumn()) return columnNames[3]; if (col == getUnitsColumn()) return columnNames[4]; return null; } /** See if cell is editable. */ public boolean isCellEditable(int row, int col) { if (col > 0) return true; return false; } /** Set a value */ public void setValueAt(Object aValue, int row, int col) { VarEntry ve = vars.get(row); ElectricObject owner = ve.getOwner(); if (ve == null) return; if (owner == null) return; if (col == 0) return; // can't change var name if (col == 1) { if (!aValue.toString().equals(ve.getObject().toString())) { ve.value = aValue; fireTableCellUpdated(row, col); } return; } if (col == getCodeColumn()) { CodeExpression.Code newCode = (CodeExpression.Code)aValue; if (newCode != ve.getCode()) { ve.code = newCode; fireTableCellUpdated(row, col); } return; } if (col == getDispColumn()) { if (aValue == displaynone) { if (ve.isDisplay()) { ve.display = false; fireTableCellUpdated(row, col); } } else { TextDescriptor.DispPos newDispPos = (TextDescriptor.DispPos)aValue; if ((newDispPos != ve.getDispPos()) || !ve.isDisplay()) { ve.dispPos = newDispPos; ve.display = true; fireTableCellUpdated(row, col); } } return; } if (col == getUnitsColumn()) { TextDescriptor.Unit newUnit = (TextDescriptor.Unit)aValue; if (newUnit != ve.getUnits()) { ve.units = newUnit; fireTableCellUpdated(row, col); } return; } } /** * Set this to be the list of variables shown * @param owner ElectricObject which owns Variables * @param variables the list of Variables to show */ private void setVars(ElectricObject owner, List<Variable> variables) { vars.clear(); varsToDelete.clear(); // sort by name for (Variable var : variables) { vars.add(new VarEntry(owner, var)); } Collections.sort(vars, new VarEntrySort()); fireTableDataChanged(); } /** Add a variable to be displayed in the Table */// private void addVariable(Variable var) {// vars.add(new VarEntry(var));// Collections.sort(vars, new VarEntrySort());// fireTableDataChanged();// } private void clearVariables() { vars.clear(); varsToDelete.clear(); fireTableDataChanged(); } public Class<?> getColumnClass(int col) { if (col == getCodeColumn()) return CodeExpression.Code.class; if (col == getDispColumn()) return Object.class; if (col == getUnitsColumn()) return TextDescriptor.Unit.class; return String.class; } public void setShowCode(boolean showCode) { if (this.showCode == showCode) return; this.showCode = showCode; fireTableStructureChanged(); } public void setShowDisp(boolean showDisp) { if (this.showDispPos == showDisp) return; this.showDispPos = showDisp; fireTableStructureChanged(); } public void setShowUnits(boolean showUnits) { if (this.showUnits == showUnits) return; this.showUnits = showUnits; fireTableStructureChanged(); } /** * Create a new var with default properties */ public void newVar(ElectricObject owner) { VarEntry ve = new VarEntry(owner, null); ve.var = null; ve.varKey = null; ve.varTrueName = getUniqueName("newVar"); ve.value = "?"; ve.code = CodeExpression.Code.NONE; ve.dispPos = TextDescriptor.DispPos.NAMEVALUE; ve.units = TextDescriptor.Unit.NONE; ve.display = true; vars.add(ve); Collections.sort(vars, new VarEntrySort()); fireTableDataChanged(); } /** * Duplicate the variable in the specified row * @param row the row containing the var to duplicate */ public void duplicateVar(int row) { if (row >= vars.size()) { JOptionPane.showMessageDialog(null, "Please select an attribute to duplicate", "Invalid Action", JOptionPane.WARNING_MESSAGE); return; } VarEntry srcVe = vars.get(row); VarEntry ve = new VarEntry(srcVe.getOwner(), null); ve.var = null; ve.varKey = null; ve.varTrueName = getUniqueName(srcVe.getName()); ve.value = srcVe.getObject(); ve.code = srcVe.getCode(); ve.dispPos = srcVe.getDispPos(); ve.units = srcVe.getUnits(); ve.display = srcVe.isDisplay(); vars.add(ve); Collections.sort(vars, new VarEntrySort()); fireTableDataChanged(); } /** * Delete the var in the specified row * @param row the row containing the var to delete */ public void deleteVar(int row) { if (row >= vars.size()) { JOptionPane.showMessageDialog(null, "Please select an attribute to delete", "Invalid Action", JOptionPane.WARNING_MESSAGE); return; } VarEntry ve = vars.remove(row); varsToDelete.add(ve); fireTableDataChanged(); } /** * Apply all new/delete/duplicate/modify changes */ public void applyChanges() { // prepare information about deleted attributes List<Variable.Key> deleteTheseVars = new ArrayList<Variable.Key>(); ElectricObject owner = null; for (VarEntry ve : varsToDelete) { Variable var = ve.var; if (var == null) continue; owner = ve.getOwner(); deleteTheseVars.add(var.getKey()); } // prepare information about new and modified attributes List<Variable.Key> createKey = new ArrayList<Variable.Key>(); List<Object> createValue = new ArrayList<Object>(); List<Boolean> createNew = new ArrayList<Boolean>(); List<Boolean> createDisplay = new ArrayList<Boolean>(); List<CodeExpression.Code> createCode = new ArrayList<CodeExpression.Code>(); List<Integer> createDispPos = new ArrayList<Integer>(); List<Integer> createUnits = new ArrayList<Integer>(); for(VarEntry ve : vars) { Variable var = ve.var; owner = ve.getOwner(); Variable.Key newKey = null;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?