categoryeditui.java

来自「Rapla是一个灵活的多用户资源管理系统。它提供的一些功能有:日历GUI」· Java 代码 · 共 560 行 · 第 1/2 页

JAVA
560
字号
        Category selectedCategory = categoryNode.getCategory();
        Category parent = selectedCategory.getParent();
        if ( parent == null || selectedCategory.equals( rootCategory))
            return;

        Category[] childs = parent.getCategories();
        for ( int i=0;i<childs.length;i++)
        {
            parent.removeCategory( childs[i]);
        }
        if ( direction == -1)
        {
            Category last = null;
            for ( int i=0;i<childs.length;i++)
            {
                Category current = childs[i];
                if ( current.equals( selectedCategory)) {
                    parent.addCategory( current);
                }
                if ( last != null && !last.equals( selectedCategory))
                {
                    parent.addCategory(last);
                }
                last = current;
            }
            if  (last != null) {
                parent.addCategory(last);
            }
        }
        else
        {
            boolean insertNow = false;
            for ( int i=0;i<childs.length;i++)
            {
                Category current = childs[i];
                if ( !current.equals( selectedCategory)) {
                    parent.addCategory( current);
                } else {
                    insertNow = true;
                    continue;
                }
                if ( insertNow)
                {
                    insertNow = false;
                    parent.addCategory( selectedCategory);
                }
            }
            if  ( insertNow) {
                parent.addCategory( selectedCategory);
            }
        }
        updateModel();

    }

    public void removeNodes(CategoryNode[] nodes) {
        ArrayList childList = new ArrayList();
        TreeNode[] path = null;
        CategoryNode parentNode = null;
        for (int i=0;i<nodes.length;i++) {
            if (parentNode == null) {
                path= nodes[i].getPath();
                parentNode = (CategoryNode)nodes[i].getParent();
            }
            // dont't delete the root-node
            if (parentNode == null)
                continue;
            int index = parentNode.getIndexOfUserObject(nodes[i].getUserObject());
            if (index >= 0) {
                childList.add(nodes[i]);
                if (getLogger().isDebugEnabled())
                    getLogger().debug("Removing CategoryNode " + nodes[i].getCategory());
            }
        }
        if (path != null) {
            int size = childList.size();
            Object[] childs = new Object[size];
            for (int i=0;i<size;i++) {
                childs[i] = childList.get(i);
            }
            for (int i=0;i<size;i++) {
                Category subCategory = ((CategoryNode)childs[i]).getCategory();
                subCategory.getParent().removeCategory(subCategory);
                getLogger().debug("category removed " + subCategory);
            }
        }
    }

    public void mapTo(Object object) throws RaplaException,UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

    public void mapToObject() throws RaplaException {
        validate( this.rootCategory );
        confirmEdits();
    }

    public Object getObject() {
        return this.rootCategory;
    }

    private void updateModel() throws RaplaException {
        this.rootNode = new CategoryNode(null,rootCategory);
        model = new DefaultTreeModel(this.rootNode);
        RaplaTree.exchangeTreeModel( model , treeEdit.getTree() );
    }

    public void confirmEdits() throws RaplaException {
        if ( getSelectedIndex() < 0 )
            return;
        Object category = treeEdit.getSelectedValue();
        detailPanel.mapTo ( category );
        TreePath path = treeEdit.getTree().getSelectionPath();
        if (path != null)
            model.nodeChanged((TreeNode)path.getLastPathComponent() );
    }


    private void validate(Category category) throws RaplaException {
        checkKey( category.getKey() );
        Category[] categories = category.getCategories();
        for ( int i=0; i< categories.length;i++) {
            validate( categories[i] );
        }
    }

    private void checkKey(String key) throws RaplaException {
        if (key.length() ==0)
            throw new RaplaException(getString("error.no_key"));
        if (!Tools.isKey(key))
            throw new RaplaException(getI18n().format("error.invalid_key"
                    ,key
                    ,"'-', '_'"));
    }

    public void setEditKeys(boolean editKeys) {
        detailPanel.setEditKeys(editKeys);
        this.editKeys = editKeys;
    }

    class CategoryNode extends RecursiveNode {
        public CategoryNode(TreeNode parent,Category category) {
            super(parent,category);
        }

        protected Category getCategory() {
            return (Category) getUserObject();
        }

        protected Object[] getChildObjects() {
            return getCategory().getCategories();
        }

        protected RecursiveNode createChildNode(Object userObject) {
            return new CategoryNode(this,(Category)userObject);
        }

        public String toString() {
            return getName(getCategory());
        }
    }
}

class CategoryDetail extends AbstractEditField
    implements
    ChangeListener
{
    JPanel panel = new JPanel();
    JLabel nameLabel = new JLabel();
    JLabel keyLabel = new JLabel();
    JLabel colorLabel = new JLabel();
    JButton colorChooserBtn = new JButton();
    Color currentColor;

    MultiLanguageField name;
    TextField key;
    TextField colorTextField;
    JPanel colorPanel = new JPanel();

    public CategoryDetail(RaplaContext sm) throws RaplaException {
        super( sm);
        name = new MultiLanguageField(sm,"name");
        key = new TextField(sm,"key");
        colorTextField = new TextField(sm,"color");
        double fill = TableLayout.FILL;
        double pre = TableLayout.PREFERRED;
        panel.setLayout( new TableLayout( new double[][]
            {{5, pre, 5, fill },  // Columns
             {5, pre ,5, pre, 5, pre, 5}} // Rows
                                          ));
        panel.add("1,1,l,f", nameLabel);
        panel.add("3,1,f,f", name.getComponent() );
        panel.add("1,3,l,f", keyLabel);
        panel.add("3,3,f,f", key.getComponent() );
        panel.add("1,5,l,f", colorLabel);
        panel.add("3,5,f,f", colorPanel);
        colorPanel.setLayout( new BorderLayout());
        colorPanel.add( colorTextField.getComponent(), BorderLayout.CENTER );
        colorPanel.add( colorChooserBtn, BorderLayout.EAST );

        nameLabel.setText(getString("name") + ":");
        keyLabel.setText(getString("key") + ":");
        colorLabel.setText( getString("color") + ":");
        name.addChangeListener ( this );
        key.addChangeListener ( this );
        colorTextField.addChangeListener( this );

        colorChooserBtn.setText( getString("change") );
        colorChooserBtn.addActionListener( new ActionListener() {

    		public void actionPerformed(ActionEvent e) {
    			currentColor = JColorChooser.showDialog(
                         panel,
                         "Choose Background Color",
                          currentColor);
                colorChooserBtn.setBackground( currentColor );
                if ( currentColor != null) {
                	colorTextField.setValue( RaplaColorList.getHexForColor( currentColor ));
                }
                fireContentChanged();
    		}

        });

    }

    public void requestFocus() {
        name.requestFocus();
    }

    public void setEditKeys(boolean editKeys) {
        keyLabel.setVisible( editKeys );
        key.getComponent().setVisible( editKeys );
        colorLabel.setVisible( editKeys );
        colorTextField.getComponent().setVisible( editKeys );
        colorChooserBtn.setVisible( editKeys );
    }

    public JComponent getComponent() {
        return panel;
    }

    public Object getValue() {return null;}

    public void setValue(Object object) {}

    public void mapFrom(Object object) throws RaplaException {
        Category category = (Category) object;
        name.mapFrom(category);
        key.mapFrom(category);
        String colorValue = category.getAnnotation(CategoryAnnotations.KEY_NAME_COLOR, null) ;
        if ( colorValue != null) {
        	currentColor =  RaplaColorList.getColorForHex( colorValue );
            colorTextField.setValue( colorValue );
        }  else {
            colorTextField.setValue( "" );
        	currentColor = null;
        }
        colorChooserBtn.setBackground( currentColor );
    }

    public void mapTo(Object object) throws RaplaException {
        Category category = (Category) object;
        name.mapTo(category);
        key.mapTo(category);
        String colorValue = colorTextField.getValue().toString().trim();
        if ( colorValue.length() > 0) {
            category.setAnnotation(CategoryAnnotations.KEY_NAME_COLOR, colorValue );
        } else {
            category.setAnnotation(CategoryAnnotations.KEY_NAME_COLOR, null );
        }
    }

    public void stateChanged(ChangeEvent e) {
        fireContentChanged();
    }
}


⌨️ 快捷键说明

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