attributestable.java

来自「The ElectricTM VLSI Design System is an 」· Java 代码 · 共 893 行 · 第 1/2 页

JAVA
893
字号
				Object newValue = ve.getObject();				boolean newCreate = false;				boolean newDisplay = ve.isDisplay();				CodeExpression.Code newCode = ve.getCode();				int newDispPos = ve.getDispPos().getIndex();				int newUnits = ve.getUnits().getIndex();				if (var == null)				{					// this is a new var					newCreate = true;					String name = ve.getName();					if (!name.startsWith("ATTR_") && !name.startsWith("ATTRP_"))						name = "ATTR_"+name;					newKey = Variable.newKey(name);				} else				{					if (!ve.isChanged()) continue;					newKey = ve.getKey();				}				createKey.add(newKey);				createValue.add(newValue);				createNew.add(Boolean.valueOf(newCreate));				createDisplay.add(Boolean.valueOf(newDisplay));				createCode.add(newCode);				createDispPos.add(new Integer(newDispPos));				createUnits.add(new Integer(newUnits));			}			new ApplyChanges(owner, createKey, createValue, createNew, createDisplay, createCode, createDispPos, createUnits, deleteTheseVars);		}		/**		 * Cancel all changes		 */		public void cancelChanges() {}		private String getUniqueName(String name) {			boolean nameConflict = true;			String newName = name;			int i = 0;			while (nameConflict) {				nameConflict = false;				i++;				for (VarEntry ve : vars) {					if (newName.equals(ve.getName())) {						nameConflict = true;						newName = name + "_" + i;						break;					}				}			}			return newName;		}		private static class ApplyChanges extends Job		{			private ElectricObject owner;			private List<Variable.Key> createKey;			private List<Object> createValue;			private List<Boolean> createNew;			private List<Boolean> createDisplay;			private List<CodeExpression.Code> createCode;			private List<Integer> createDispPos;			private List<Integer> createUnits;			private List<Variable.Key> varsToDelete;			private ApplyChanges(ElectricObject owner,				List<Variable.Key> createKey,				List<Object> createValue,				List<Boolean> createNew,				List<Boolean> createDisplay,				List<CodeExpression.Code> createCode,				List<Integer> createDispPos,				List<Integer> createUnits,				List<Variable.Key> varsToDelete)			{				super("Apply Attribute Changes", User.getUserTool(), Job.Type.CHANGE, null, null, Job.Priority.USER);				this.owner = owner;				this.createKey = createKey;				this.createValue = createValue;				this.createNew = createNew;				this.createDisplay = createDisplay;				this.createCode = createCode;				this.createDispPos = createDispPos;				this.createUnits = createUnits;				this.varsToDelete = varsToDelete;				startJob();			}			public boolean doIt() throws JobException			{				// delete variables first				for (Variable.Key key : varsToDelete)				{					if (owner.isParam(key)) {                        if (owner instanceof Cell)    						((Cell)owner).getCellGroup().delParam((Variable.AttrKey)key);                        else if (owner instanceof NodeInst)                            ((NodeInst)owner).delParameter(key);                    } else {						owner.delVar(key);                    }				}				// now create new and update existing variables				for(int i=0; i<createKey.size(); i++)				{					Variable.Key key = createKey.get(i);					Object obj = createValue.get(i);					boolean makeNew = createNew.get(i).booleanValue();					boolean display = createDisplay.get(i).booleanValue();					CodeExpression.Code code = createCode.get(i);					TextDescriptor.DispPos dispPos = TextDescriptor.DispPos.getShowStylesAt(createDispPos.get(i).intValue());					TextDescriptor.Unit units = TextDescriptor.Unit.getUnitAt(createUnits.get(i).intValue());					obj = Variable.withCode(obj, code);					Variable newVar = null;					if (makeNew)					{						// this is a new variable						newVar = owner.newVar(key, obj);					} else					{						// update variable						newVar = owner.updateVar(key, obj);					}					if (newVar != null)					{						// set/update properties						assert newVar.getCode() == code;						TextDescriptor td = null;						// see if this is a parameter						if (owner instanceof NodeInst)						{							NodeInst ni = (NodeInst)owner;							if (ni.isCellInstance())							{								Cell cell = (Cell)ni.getProto();								for(Iterator<Variable> it = cell.getParameters(); it.hasNext(); )								{									Variable var = it.next();									if (var.getKey() == key)									{										// get text descriptor, adjust for cell center										td = var.getTextDescriptor();										double xOff = td.getXOff();										double yOff = td.getYOff();										xOff -= cell.getBounds().getCenterX();										yOff -= cell.getBounds().getCenterY();										td = td.withOff(xOff, yOff);									}								}							}						}						if (td == null) td = newVar.getTextDescriptor();						td = td.withDisplay(display).withDispPart(dispPos).withUnit(units);						owner.setTextDescriptor(newVar.getKey(), td);					}				}				return true;			}		} // end class ApplyChanges	} // end class VariableTableModel	// ----------------------------------------------------------------	private static JComboBox codeComboBox = null;	private static JComboBox dispComboBox = null;	private static JComboBox unitComboBox = null;	private static final String displaynone = "None";	private JPopupMenu popup;	private Point popupLocation;	private ElectricObject owner;	/**	 * Create a new Attributes Table	 */	public AttributesTable(ElectricObject owner, boolean showCode, boolean showDispPos, boolean showUnits) {		setElectricObject(owner);		setGridColor(getBackground());          // hides cell grids		setSelectionMode(ListSelectionModel.SINGLE_SELECTION);		VariableTableModel model = new VariableTableModel(showCode, showDispPos, showUnits);		setModel(model);		initComboBoxes();		// set up combo box editors		if (showCode) {			TableColumn codeColumn = getColumnModel().getColumn((model.getCodeColumn()));			if (codeColumn != null)				codeColumn.setCellEditor(new DefaultCellEditor(codeComboBox));		}		if (showDispPos) {			TableColumn codeColumn = getColumnModel().getColumn((model.getDispColumn()));			if (codeColumn != null)				codeColumn.setCellEditor(new DefaultCellEditor(dispComboBox));		}		if (showUnits){			TableColumn codeColumn = getColumnModel().getColumn((model.getUnitsColumn()));			if (codeColumn != null)				codeColumn.setCellEditor(new DefaultCellEditor(unitComboBox));		}		MouseListener mouseListener = new MouseAdapter() {			public void mousePressed(MouseEvent e) {				if (e.isShiftDown() || e.isControlDown() || e.isAltDown()) return;				if (e.isMetaDown()) {					initPopupMenu();					popupLocation = new Point(e.getX(), e.getY());					popup.show(e.getComponent(), e.getX(), e.getY());				}			}		};		addMouseListener(mouseListener);		UserInterfaceMain.addDatabaseChangeListener(this);	}	private void initComboBoxes() {		if (codeComboBox == null) {			codeComboBox = new JComboBox();			for (Iterator<CodeExpression.Code> it = CodeExpression.Code.getCodes(); it.hasNext(); ) {				codeComboBox.addItem(it.next());			}			codeComboBox.setFont(new Font("Dialog", 0, 11));		}		if (dispComboBox == null) {			dispComboBox = new JComboBox();			dispComboBox.addItem(displaynone);			for (Iterator<TextDescriptor.DispPos> it = TextDescriptor.DispPos.getShowStyles(); it.hasNext(); ) {				dispComboBox.addItem(it.next());			}			dispComboBox.setFont(new Font("Dialog", 0, 11));		}		if (unitComboBox == null) {			unitComboBox = new JComboBox();			for (Iterator<TextDescriptor.Unit> it = TextDescriptor.Unit.getUnits(); it.hasNext(); ) {				unitComboBox.addItem(it.next());			}			unitComboBox.setFont(new Font("Dialog", 0, 11));		}	}	private void initPopupMenu() {		popup = new JPopupMenu();		JMenuItem m;		m = new JMenuItem("New Attr");		m.addActionListener(new ActionListener() {			public void actionPerformed(ActionEvent e) { newVar(); }		});		popup.add(m);//		m = new JMenuItem("Edit Cell");//		m.addActionListener(new ActionListener() {//			public void actionPerformed(ActionEvent e) { editCell(popup.getLocation()); }//		});//		popup.add(m);		m = new JMenuItem("Duplicate Attr");		m.addActionListener(new ActionListener() {			public void actionPerformed(ActionEvent e) { duplicateVar(popupLocation); }		});		popup.add(m);		m = new JMenuItem("Delete Attr");		m.addActionListener(new ActionListener() {			public void actionPerformed(ActionEvent e) { deleteVar(popupLocation); }		});		popup.add(m);		JMenu showMenu = new JMenu("Show...");		JCheckBoxMenuItem cb;		VariableTableModel model = (VariableTableModel)getModel();		cb = new JCheckBoxMenuItem("Code", model.showCode);		cb.addActionListener(new ActionListener() {		   public void actionPerformed(ActionEvent e) { toggleShowCode(); }		});		showMenu.add(cb);		cb = new JCheckBoxMenuItem("Display", model.showDispPos);		cb.addActionListener(new ActionListener() {			public void actionPerformed(ActionEvent e) { toggleShowDisp(); }		});		showMenu.add(cb);		cb = new JCheckBoxMenuItem("Units", model.showUnits);		cb.addActionListener(new ActionListener() {			public void actionPerformed(ActionEvent e) { toggleShowUnits(); }		});		showMenu.add(cb);		popup.add(showMenu);	}	/**	 * Create a new Variable	 */	private void newVar() {		VariableTableModel model = (VariableTableModel)getModel();		model.newVar(owner);	}	/**	 * Duplicate the Variable in the row pointed to by location	 * @param location	 */	private void duplicateVar(Point location) {		int row = rowAtPoint(location);		VariableTableModel model = (VariableTableModel)getModel();		model.duplicateVar(row);	}	/**	 * Delete the Variable in the row pointed to by location	 * @param location	 */	private void deleteVar(Point location) {		int row = rowAtPoint(location);		VariableTableModel model = (VariableTableModel)getModel();		model.deleteVar(row);	}	/**	 * Applies all changes made to attributes to database	 */	public void applyChanges() {		VariableTableModel model = (VariableTableModel)getModel();		// clean up if a cell is currently being edited		if (isEditing()) {			int row = getEditingRow();			int col = getEditingColumn();			TableCellEditor editor = getCellEditor(row, col);			editor.stopCellEditing();		}		model.applyChanges();	}	/**	 * Method to handle the "Cancel" button in attributes.	 */	public void cancelChanges() {		// clean up if a cell is currently being edited		if (isEditing()) {			int row = getEditingRow();			int col = getEditingColumn();			TableCellEditor editor = getCellEditor(row, col);			editor.cancelCellEditing();		}		// revert back to database values		setElectricObject(owner);	}	private void toggleShowCode() {		VariableTableModel model = (VariableTableModel)getModel();		model.setShowCode(!model.showCode);		updateEditors();	}	private void toggleShowDisp() {		VariableTableModel model = (VariableTableModel)getModel();		model.setShowDisp(!model.showDispPos);		updateEditors();	}	private void toggleShowUnits() {		VariableTableModel model = (VariableTableModel)getModel();		model.setShowUnits(!model.showUnits);		updateEditors();	}	private void updateEditors() {		VariableTableModel model = (VariableTableModel)getModel();		int codeCol = model.getCodeColumn();		if (codeCol != -1) {			TableColumn codeColumn = getColumnModel().getColumn(codeCol);			if (codeColumn != null)				codeColumn.setCellEditor(new DefaultCellEditor(codeComboBox));		}		int dispCol = model.getDispColumn();		if (dispCol != -1) {			TableColumn codeColumn = getColumnModel().getColumn(dispCol);			if (codeColumn != null)				codeColumn.setCellEditor(new DefaultCellEditor(dispComboBox));		}		int unitsCol = model.getUnitsColumn();		if (unitsCol != -1) {			TableColumn codeColumn = getColumnModel().getColumn(unitsCol);			if (codeColumn != null)				codeColumn.setCellEditor(new DefaultCellEditor(unitComboBox));		}	}	/**	 * Set the ElectricObject whose Variables will be shown	 * @param eobj	 */	public void setElectricObject(ElectricObject eobj) {		if (owner == eobj) return;		// clear old vars		clearVariables();		// add new vars		if (eobj != null) {			List<Variable> vars = new ArrayList<Variable>();			if (eobj instanceof NodeInst) {				for (Iterator<Variable> it = ((NodeInst)eobj).getParameters(); it.hasNext(); ) {					// only add attributes					Variable param = it.next();					vars.add(param);				}				for (Iterator<Variable> it = eobj.getVariables(); it.hasNext(); ) {					// only add attributes					Variable var = it.next();					if (var.isAttribute() && !eobj.isParam(var.getKey()))						vars.add(var);				}			} else {				for (Iterator<Variable> it = eobj.getVariables(); it.hasNext(); ) {					// only add attributes					Variable var = it.next();					if (var.isAttribute())						vars.add(var);				}			}			// sort vars by name			//Collections.sort(vars, new Attributes.VariableNameSort());			((VariableTableModel)getModel()).setVars(eobj, vars);		}		owner = eobj;	}	/**	 * Clear all variables from the table	 */	private void clearVariables() {		((VariableTableModel)getModel()).clearVariables();	}	public void databaseChanged(DatabaseChangeEvent e) {		// reload vars		ElectricObject eobj = owner;		setElectricObject(null);		setElectricObject(eobj);	}}

⌨️ 快捷键说明

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