Mercurial > hg > Members > shoshi > TreeCMSv1
view src/treecms/proto/gui/GraphicalMonotonicTreeEditor.java @ 50:a72718a0bccf
added demo tree builder
author | shoshi |
---|---|
date | Tue, 01 Feb 2011 16:28:49 +0900 |
parents | e647bb37b7ad |
children | d4709911e0ed |
line wrap: on
line source
package treecms.proto.gui; import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JSplitPane; import javax.swing.JTable; import javax.swing.JTree; import javax.swing.border.TitledBorder; import javax.swing.event.TreeSelectionEvent; import javax.swing.event.TreeSelectionListener; import javax.swing.table.DefaultTableModel; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeModel; import treecms.proto.api.Editor; import treecms.proto.api.Node; import treecms.proto.cassandra.CassBrowser; import treecms.proto.cassandra.CassEditor; public class GraphicalMonotonicTreeEditor extends JFrame { private static final long serialVersionUID = 1095393471116557554L; public static void main(String _args[]) throws Exception { CassBrowser browser = CassBrowser.createInstance("localhost","9160","TreeCMSKS","TreeCMSCF03"); new GraphicalMonotonicTreeEditor(new CassEditor(browser)); //ContentsTreeBuilder.blogSite(); //new GraphicalMonotonicTreeEditor(new SimpleEditor()); } private static final String WINDOW_TITLE = "Monotonic-Tree Editor"; //menu bar private JButton m_commit,m_update,m_check,m_merge; //buttons private JButton m_saveButton,m_clearButton,m_addButton; //contents view private JTree m_tree; //properties view private JTable m_table; //editor private Editor m_editor; public GraphicalMonotonicTreeEditor(Editor _editor) { super(WINDOW_TITLE); m_editor = _editor; getContentPane().setLayout(new BorderLayout()); JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT,buildTreeViewer(),buildNodeEditor()); split.setDividerLocation(400); getContentPane().add(buildMenuBar(),BorderLayout.NORTH); getContentPane().add(split,BorderLayout.CENTER); setSize(500,600); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); for(int i = 0;i < m_tree.getRowCount();i ++){ m_tree.expandRow(i); } } public JComponent buildMenuBar() { JPanel panel = new JPanel(new GridLayout(1,4)); m_commit = new JButton("commit"); m_update = new JButton("update"); m_check = new JButton("check"); m_merge = new JButton("merge"); EditorMenuActionListener listener = new EditorMenuActionListener(); m_commit.addActionListener(listener); m_update.addActionListener(listener); m_check.addActionListener(listener); m_merge.addActionListener(listener); panel.add(m_commit); panel.add(m_update); panel.add(m_check); panel.add(m_merge); return panel; } public JComponent buildNodeEditor() { m_table = new JTable(new NodeEditorTableModel()); m_saveButton = new JButton("save"); m_clearButton = new JButton("clear"); m_addButton = new JButton("add child"); EditorButtonActionListener listener = new EditorButtonActionListener(); m_saveButton.addActionListener(listener); m_clearButton.addActionListener(listener); m_addButton.addActionListener(listener); JPanel panel = new JPanel(new BorderLayout()); panel.setBorder(new TitledBorder("Node Editor")); JPanel btnPanel = new JPanel(new GridLayout(1,2)); btnPanel.add(m_saveButton); btnPanel.add(m_clearButton); btnPanel.add(m_addButton); panel.add(new JScrollPane(m_table),BorderLayout.CENTER); panel.add(btnPanel,BorderLayout.SOUTH); return panel; } private class NodeEditorTableModel extends DefaultTableModel { private static final long serialVersionUID = -7158401828787373107L; private Node m_node; public NodeEditorTableModel() { m_node = null; setColumnIdentifiers(new String[]{"Key","Value"}); } @SuppressWarnings("unused") public NodeEditorTableModel(Node _node) { this(); m_node = _node; printNode(); } @Override public boolean isCellEditable(int _row,int _col) { if(_col == 0 || _row == 0){ return false; //disable editing id and parameter names } return true; } public void setNode(Node _node) { m_node = _node; printNode(); } public void clear() { printNode(); } private void printNode() { setRowCount(0); addRow(new String[]{"id",m_node.getID().toString()}); addRow(new String[]{"title",m_node.getTitle()}); addRow(new String[]{"class",m_node.getClassName()}); } public String getTitle() { return (String)getValueAt(1,1); } public String getClassName() { return (String)getValueAt(2,1); } public Node getNode() { return m_node; } } public JComponent buildTreeViewer() { m_tree = new JTree(new ContentsViewerTreeModel(m_editor.useContents())); m_tree.addTreeSelectionListener(new TreeSelectionListener(){ @Override public void valueChanged(TreeSelectionEvent _e) { DefaultMutableTreeNode selected = (DefaultMutableTreeNode)m_tree.getLastSelectedPathComponent(); if(selected == null){ return; } Node node = (Node)selected.getUserObject(); //get node // NodeEditorTableModel model = (NodeEditorTableModel)m_table.getModel(); model.setNode(node); } }); return new JScrollPane(m_tree); } private class ContentsViewerTreeModel extends DefaultTreeModel { private static final long serialVersionUID = -4710796110922619395L; public ContentsViewerTreeModel(Node _contents) { super(new DefaultMutableTreeNode()); setRootNode(_contents,false); } public void setRootNode(Node _contents,boolean _reload) { DefaultMutableTreeNode root = (DefaultMutableTreeNode)getRoot(); root.removeAllChildren(); root.setUserObject(_contents); for(Node child : _contents.getChildren()){ treewalk(child,root); } if(_reload){ reload(); } } private void treewalk(Node _node,DefaultMutableTreeNode _treeNode) { DefaultMutableTreeNode treeChild = new DefaultMutableTreeNode(); treeChild.setUserObject(_node); _treeNode.add(treeChild); for(Node child : _node.getChildren()){ treewalk(child,treeChild); } } } private class EditorMenuActionListener implements ActionListener { @Override public void actionPerformed(ActionEvent _e) { JButton source = (JButton)_e.getSource(); if(source.equals(m_commit)){ //commit m_editor.commit(true); } if(source.equals(m_update)){ //update m_editor.update(); ContentsViewerTreeModel model = (ContentsViewerTreeModel)m_tree.getModel(); model.setRootNode(m_editor.useContents(),true); } if(source.equals(m_check)){ //check if(m_editor.check()){ JOptionPane.showMessageDialog(GraphicalMonotonicTreeEditor.this,"updates aviable!","update check",JOptionPane.OK_OPTION); }else{ JOptionPane.showMessageDialog(GraphicalMonotonicTreeEditor.this,"updates not aviable!","update check",JOptionPane.OK_OPTION); } } if(source.equals(m_merge)){ //merge m_editor.merge(); ContentsViewerTreeModel model = (ContentsViewerTreeModel)m_tree.getModel(); model.setRootNode(m_editor.useContents(),true); } for(int i = 0;i < m_tree.getRowCount();i ++){ m_tree.expandRow(i); } } } private class EditorButtonActionListener implements ActionListener { @Override public void actionPerformed(ActionEvent _e) { JButton source = (JButton)_e.getSource(); NodeEditorTableModel tableModel = (NodeEditorTableModel)m_table.getModel(); if(source.equals(m_saveButton)){ Node target = tableModel.getNode(); Node newNode = m_editor.edit(target); newNode.setTitle(tableModel.getTitle()); newNode.setClassName(tableModel.getClassName()); ContentsViewerTreeModel treeModel = (ContentsViewerTreeModel)m_tree.getModel(); treeModel.setRootNode(m_editor.getUncommited(),true); } if(source.equals(m_clearButton)){ tableModel.clear(); } if(source.equals(m_addButton)){ Node newNode = m_editor.edit(tableModel.getNode()); newNode.addChild(newNode.createNode()); ContentsViewerTreeModel treeModel = (ContentsViewerTreeModel)m_tree.getModel(); treeModel.setRootNode(m_editor.getUncommited(),true); } for(int i = 0;i < m_tree.getRowCount();i ++){ m_tree.expandRow(i); } } } }