This project has retired. For details please refer to its Attic page.
TypesFrame xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.workbench;
20  
21  import java.awt.Cursor;
22  import java.awt.Dimension;
23  import java.util.List;
24  
25  import javax.swing.JFrame;
26  import javax.swing.JScrollPane;
27  import javax.swing.JSplitPane;
28  import javax.swing.JTree;
29  import javax.swing.event.TreeSelectionEvent;
30  import javax.swing.event.TreeSelectionListener;
31  import javax.swing.tree.DefaultMutableTreeNode;
32  import javax.swing.tree.DefaultTreeModel;
33  import javax.swing.tree.TreeSelectionModel;
34  
35  import org.apache.chemistry.opencmis.client.api.ObjectType;
36  import org.apache.chemistry.opencmis.client.api.Tree;
37  import org.apache.chemistry.opencmis.workbench.model.ClientModel;
38  
39  public class TypesFrame extends JFrame {
40  
41      private static final long serialVersionUID = 1L;
42  
43      private static final String WINDOW_TITLE = "CMIS Types";
44  
45      private final ClientModel model;
46  
47      private JTree typesTree;
48      private TypeSplitPane typePanel;
49  
50      public TypesFrame(ClientModel model) {
51          super();
52  
53          this.model = model;
54          createGUI();
55          loadData();
56      }
57  
58      private void createGUI() {
59          setTitle(WINDOW_TITLE + " - " + model.getRepositoryName());
60          setPreferredSize(new Dimension(1000, 700));
61          setMinimumSize(new Dimension(200, 60));
62  
63          typesTree = new JTree();
64          typesTree.setRootVisible(false);
65          typesTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
66  
67          typesTree.addTreeSelectionListener(new TreeSelectionListener() {
68              public void valueChanged(TreeSelectionEvent e) {
69                  DefaultMutableTreeNode node = (DefaultMutableTreeNode) ((JTree) e.getSource())
70                          .getLastSelectedPathComponent();
71  
72                  if (node == null) {
73                      return;
74                  }
75  
76                  ObjectType type = ((TypeNode) node.getUserObject()).getType();
77                  typePanel.setType(type);
78              }
79          });
80  
81          typePanel = new TypeSplitPane(model);
82  
83          JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new JScrollPane(typesTree), typePanel);
84          splitPane.setDividerLocation(300);
85  
86          add(splitPane);
87  
88          ClientHelper.installEscapeBinding(this, getRootPane(), true);
89  
90          setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
91          pack();
92  
93          setLocationRelativeTo(null);
94          setVisible(true);
95      }
96  
97      private void loadData() {
98          try {
99              setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
100 
101             DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode();
102 
103             List<Tree<ObjectType>> types = model.getTypeDescendants();
104             for (Tree<ObjectType> tt : types) {
105                 addLevel(rootNode, tt);
106             }
107 
108             DefaultTreeModel treeModel = new DefaultTreeModel(rootNode);
109             typesTree.setModel(treeModel);
110         } catch (Exception ex) {
111             ClientHelper.showError(null, ex);
112             return;
113         } finally {
114             setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
115         }
116     }
117 
118     private void addLevel(DefaultMutableTreeNode parent, Tree<ObjectType> tree) {
119         DefaultMutableTreeNode node = new DefaultMutableTreeNode(new TypeNode(tree.getItem()));
120         parent.add(node);
121 
122         if (tree.getChildren() != null) {
123             for (Tree<ObjectType> tt : tree.getChildren()) {
124                 addLevel(node, tt);
125             }
126         }
127     }
128 
129     static class TypeNode {
130         private final ObjectType type;
131 
132         public TypeNode(ObjectType type) {
133             this.type = type;
134         }
135 
136         public ObjectType getType() {
137             return type;
138         }
139 
140         @Override
141         public String toString() {
142             return type.getDisplayName() + " (" + type.getId() + ")";
143         }
144     }
145 }