This project has retired. For details please refer to its Attic page.
FolderPanel 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.BorderLayout;
22  import java.awt.Cursor;
23  import java.awt.event.ActionEvent;
24  import java.awt.event.ActionListener;
25  import java.awt.event.KeyEvent;
26  import java.awt.event.KeyListener;
27  
28  import javax.swing.BorderFactory;
29  import javax.swing.BoxLayout;
30  import javax.swing.JButton;
31  import javax.swing.JPanel;
32  import javax.swing.JScrollPane;
33  import javax.swing.JTextField;
34  
35  import org.apache.chemistry.opencmis.client.api.Folder;
36  import org.apache.chemistry.opencmis.client.api.ObjectId;
37  import org.apache.chemistry.opencmis.workbench.model.ClientModel;
38  import org.apache.chemistry.opencmis.workbench.model.ClientModelEvent;
39  import org.apache.chemistry.opencmis.workbench.model.FolderListener;
40  import org.apache.chemistry.opencmis.workbench.model.ObjectListener;
41  
42  public class FolderPanel extends JPanel implements FolderListener, ObjectListener {
43  
44      private static final long serialVersionUID = 1L;
45  
46      private final ClientModel model;
47  
48      private String parentId;
49  
50      private JButton upButton;
51      private JTextField pathField;
52      private JButton goButton;
53      private FolderTable folderTable;
54  
55      public FolderPanel(ClientModel model) {
56          super();
57  
58          this.model = model;
59          model.addFolderListener(this);
60          model.addObjectListener(this);
61          createGUI();
62      }
63  
64      public void folderLoaded(ClientModelEvent event) {
65          Folder currentFolder = event.getClientModel().getCurrentFolder();
66  
67          if (currentFolder != null) {
68              String path = currentFolder.getPath();
69              pathField.setText(path);
70  
71              Folder parent = currentFolder.getFolderParent();
72              if (parent == null) {
73                  parentId = null;
74                  upButton.setEnabled(false);
75              } else {
76                  parentId = parent.getId();
77                  upButton.setEnabled(true);
78              }
79          } else {
80              pathField.setText("???");
81              parentId = null;
82              upButton.setEnabled(false);
83          }
84      }
85  
86      public void objectLoaded(ClientModelEvent event) {
87          if ((folderTable.getSelectedRow() > -1) && (event.getClientModel().getCurrentObject() != null)) {
88              String selId = folderTable.getValueAt(folderTable.getSelectedRow(), FolderTable.ID_COLUMN).toString();
89              String curId = event.getClientModel().getCurrentObject().getId();
90  
91              if (!curId.equals(selId)) {
92                  folderTable.clearSelection();
93              }
94          }
95      }
96  
97      private void createGUI() {
98          setLayout(new BorderLayout());
99  
100         JPanel panel = new JPanel();
101         panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
102         panel.setBorder(BorderFactory.createEmptyBorder(1, 0, 1, 0));
103 
104         upButton = new JButton("up");
105         upButton.setEnabled(false);
106         upButton.addActionListener(new ActionListener() {
107             public void actionPerformed(ActionEvent e) {
108                 try {
109                     setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
110                     ObjectId objectId = model.loadFolder(parentId, false);
111                     model.loadObject(objectId.getId());
112                 } catch (Exception ex) {
113                     ClientHelper.showError(null, ex);
114                     return;
115                 } finally {
116                     setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
117                 }
118             }
119         });
120         panel.add(upButton);
121 
122         pathField = new JTextField("");
123         pathField.addKeyListener(new KeyListener() {
124             public void keyTyped(KeyEvent e) {
125             }
126 
127             public void keyReleased(KeyEvent e) {
128                 if (e.getKeyCode() == KeyEvent.VK_ENTER) {
129                     loadFolder();
130                 }
131             }
132 
133             public void keyPressed(KeyEvent e) {
134             }
135         });
136         panel.add(pathField);
137 
138         goButton = new JButton("go");
139         goButton.addActionListener(new ActionListener() {
140             public void actionPerformed(ActionEvent e) {
141                 loadFolder();
142             }
143         });
144         panel.add(goButton);
145 
146         add(panel, BorderLayout.PAGE_START);
147 
148         folderTable = new FolderTable(model);
149         folderTable.setFillsViewportHeight(true);
150         model.addFolderListener(folderTable);
151 
152         add(new JScrollPane(folderTable), BorderLayout.CENTER);
153     }
154 
155     private void loadFolder() {
156         try {
157             setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
158             String id = pathField.getText().trim();
159             if (id.length() == 0) {
160                 id = "/";
161             }
162             ObjectId objectId = model.loadFolder(id, id.startsWith("/"));
163             model.loadObject(objectId.getId());
164         } catch (Exception ex) {
165             ClientHelper.showError(null, ex);
166             return;
167         } finally {
168             setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
169         }
170     }
171 }