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

📄 propertydialog.java

📁 用Java开发的、实现类似Visio功能的应用程序源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    //Handle clicks on the Set and Cancel buttons.
    public void actionPerformed(ActionEvent e) {
    	String cmd	=e.getActionCommand();
    	  

        if ("Confirm".equals(cmd)) {  
		stopEditing();

                //one object in select was disabled modifying properties
                List l	=new ArrayList();
                l.add(m_shape);
    		if (ShapeSettingDialog.getDisableModifyingProperties(l,true)){
 			JOptionPane.showMessageDialog(null, CADResource.getString("dialog.shapesetting.modificationdisabled"), CADResource.getString("sys.warn"), JOptionPane.ERROR_MESSAGE); 
			m_propertiesChanged	=false;
    		}else{
        		ObjectList objList	=m_tableModel.getProperties();
        		m_shape.getPropertyList().setList(objList.getList());
			m_propertiesChanged	=true;        	
        	}
        	m_dialog.setVisible(false);

        }else if ("Cancel".equals(cmd)){
		m_propertiesChanged	=false;
        	m_dialog.setVisible(false);

        }else if ("New".equals(cmd)){
        	try{
			stopEditing();

        		m_currentProperties	=m_tableModel.getProperties();
        		m_currentProperties.add(new Property());
        		m_tableModel.setProperties(m_currentProperties);
        		m_table.repaint();
        		int rowId=m_currentProperties.size()-1;
        		m_table.setRowSelectionInterval(rowId,rowId);
        		m_table.editCellAt(rowId,0);
        	}catch(Exception ee){
        	}
	
	}else if ("Remove".equals(cmd)){
		int row	=m_table.getSelectedRow();
		if (row<0) return;
		
		try{
			stopEditing();
                          
			m_currentProperties.removeByIndex(row);
        		m_tableModel.setProperties(m_currentProperties);
        		m_table.repaint();

        		int rowId=row-1;
        		if (rowId<0) rowId=0;
        		m_table.setRowSelectionInterval(rowId,rowId);
        		m_table.editCellAt(rowId,0);

		}catch(Exception ee){
		}
	}
    }   



    /*
     * This method picks good column sizes.
     * If all column heads are wider than the column's cells'
     * contents, then you can just use column.sizeWidthToFit().
     */
    private void initColumnSizes(JTable table) {
        MyTableModel model = (MyTableModel)table.getModel();
        TableColumn column = null;
        Component comp = null;
        int headerWidth = 0;
        int cellWidth = 0;
        Object[] longValues = model.longValues;
        TableCellRenderer headerRenderer =
            table.getTableHeader().getDefaultRenderer();

        for (int i = 0; i < 4; i++) {

            column = table.getColumnModel().getColumn(i);

            comp = headerRenderer.getTableCellRendererComponent(
                                 null, column.getHeaderValue(),
                                 false, false, 0, 0);

            headerWidth = comp.getPreferredSize().width;

            comp = table.getDefaultRenderer(model.getColumnClass(i)).
                             getTableCellRendererComponent(
                                 table, longValues[i],
                                 false, false, 0, i);

            cellWidth = comp.getPreferredSize().width;

            column.setPreferredWidth(Math.max(headerWidth, cellWidth));

        }
    }

    public void setTypeComboColumn(JTable table,
                                 TableColumn typeColumn) {
        //Set up the editor for the sport cells.
        JComboBox comboBox = new JComboBox();    
	comboBox.setFont(GUIConst.FONT_BUTTON);
        List item	=Property.getTypeDescList();
        Iterator it	=item.iterator();
        while (it!=null && it.hasNext()){
        	comboBox.addItem((String)it.next());
	}        	
        typeColumn.setCellEditor(new DefaultCellEditor(comboBox));

        //Set up tool tips for the sport cells.
        DefaultTableCellRenderer renderer =
                new DefaultTableCellRenderer();
        renderer.setToolTipText(CADResource.getString("label.property.clickforcombo"));
        typeColumn.setCellRenderer(renderer);
    }
   
    
    
    class MyTableModel extends AbstractTableModel {
        private String[] columnNames = {
        	CADResource.getString("label.property.name"),
        	CADResource.getString("label.property.type"),
        	CADResource.getString("label.property.value"),
        	CADResource.getString("label.property.comments")};
        
        public final Object[] longValues = {"012345", "0123456",
                                            "01234567","01234567"};
                                                    
        private Object[][] data = {};      
        
        public MyTableModel(ObjectList objList){
        	setProperties(objList);
        	fireTableStructureChanged();
	}
	
	public void setProperties(ObjectList objList){
		int size	=objList.size();  
		if (size==0){
			try{
				//click an empty property row if no properties definied.
				objList.add(new Property());
				size=1;
			}catch(Exception e){
			}
		}
			
		data		=new Object[size][4];
		Iterator it	=objList.getList().iterator();
		int cnt=0;
		while (it!=null && it.hasNext()){
			Property p	=(Property)it.next();
			data[cnt][0]	=p.getName();
			data[cnt][1]	=p.getDescByType(p.getType());
			data[cnt][2]	=p.getValue();
			data[cnt][3]	=p.getComment();
			cnt++;
		}
	}        
	
	public ObjectList getProperties(){
		try{
			ObjectList ret	=new ObjectList();
			for (int i=0; i<data.length; i++){
				Property p	=new Property();
				p.setName((String)data[i][0]);
				p.setType(p.getTypeByDesc((String)data[i][1]));
				p.setValue((String)data[i][2]);
				p.setComment((String)data[i][3]);
				if (!p.getName().equals("") || !p.getValue().equals("") || !p.getComment().equals(""))
 				    ret.add(p);
			}
				
			return ret;
		}catch(Exception e){
			return null;
		}
	}        

        public int getColumnCount() {
            return columnNames.length;
        }

        public int getRowCount() {
            return data.length;
        }

        public String getColumnName(int col) {
            return columnNames[col];
        }

        public Object getValueAt(int row, int col) {
            return data[row][col];
        }

        /*
         * JTable uses this method to determine the default renderer/
         * editor for each cell.  If we didn't implement this method,
         * then the last column would contain text ("true"/"false"),
         * rather than a check box.
         */
        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }

        /*
         * Don't need to implement this method unless your table's
         * editable.
         */
        public boolean isCellEditable(int row, int col) {
            //Note that the data/cell address is constant,
            //no matter where the cell appears onscreen.
            //if (col < 2) {
            //     return false;
            // } else {
            //     return true;
            // }
            return true;
        }


        /*
         * Don't need to implement this method unless your table's
         * data can change.
         */
        public void setValueAt(Object value, int row, int col) {
            data[row][col] = value;
            fireTableCellUpdated(row, col);
        }

    }
    
        
}

⌨️ 快捷键说明

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