This project has retired. For details please refer to its Attic page.
FolderTable 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.datatransfer.DataFlavor;
23  import java.awt.datatransfer.Transferable;
24  import java.awt.datatransfer.UnsupportedFlavorException;
25  import java.awt.event.ActionEvent;
26  import java.awt.event.ActionListener;
27  import java.awt.event.KeyEvent;
28  import java.awt.event.KeyListener;
29  import java.awt.event.MouseAdapter;
30  import java.awt.event.MouseEvent;
31  import java.io.File;
32  import java.io.IOException;
33  import java.util.Collections;
34  import java.util.GregorianCalendar;
35  import java.util.HashMap;
36  import java.util.List;
37  import java.util.Map;
38  
39  import javax.swing.DropMode;
40  import javax.swing.ImageIcon;
41  import javax.swing.JComponent;
42  import javax.swing.JMenuItem;
43  import javax.swing.JPopupMenu;
44  import javax.swing.JTable;
45  import javax.swing.ListSelectionModel;
46  import javax.swing.TransferHandler;
47  import javax.swing.event.ListSelectionEvent;
48  import javax.swing.event.ListSelectionListener;
49  import javax.swing.table.AbstractTableModel;
50  import javax.swing.table.TableColumn;
51  
52  import org.apache.chemistry.opencmis.client.api.CmisObject;
53  import org.apache.chemistry.opencmis.client.api.Document;
54  import org.apache.chemistry.opencmis.client.api.Folder;
55  import org.apache.chemistry.opencmis.commons.enums.BaseTypeId;
56  import org.apache.chemistry.opencmis.workbench.model.ClientModel;
57  import org.apache.chemistry.opencmis.workbench.model.ClientModelEvent;
58  import org.apache.chemistry.opencmis.workbench.model.FolderListener;
59  import org.apache.chemistry.opencmis.workbench.swing.GregorianCalendarRenderer;
60  
61  public class FolderTable extends JTable implements FolderListener {
62  
63      private static final long serialVersionUID = 1L;
64  
65      private static final String[] COLUMN_NAMES = { "", "Name", "Type", "Content Type", "Size", "Creation Date",
66              "Created by", "Modification Date", "Modified by", "Id" };
67      private static final int[] COLUMN_WIDTHS = { 24, 200, 150, 150, 80, 180, 100, 180, 100, 300 };
68      public static final int ID_COLUMN = 9;
69  
70      private final ClientModel model;
71  
72      private Map<BaseTypeId, ImageIcon> icons;
73      private ImageIcon checkedOutIcon;
74      private ImageIcon pwcIcon;
75  
76      public FolderTable(final ClientModel model) {
77          super();
78  
79          this.model = model;
80  
81          setModel(new FolderTableModel());
82  
83          setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
84          setAutoResizeMode(AUTO_RESIZE_OFF);
85          setAutoCreateRowSorter(true);
86  
87          setDefaultRenderer(GregorianCalendar.class, new GregorianCalendarRenderer());
88          setTransferHandler(new FolderTransferHandler());
89          setDragEnabled(true);
90          setDropMode(DropMode.INSERT);
91  
92          for (int i = 0; i < COLUMN_WIDTHS.length; i++) {
93              TableColumn column = getColumnModel().getColumn(i);
94              column.setPreferredWidth(COLUMN_WIDTHS[i]);
95          }
96  
97          final JPopupMenu popup = new JPopupMenu();
98          JMenuItem menuItem = new JMenuItem("Copy to clipboard");
99          popup.add(menuItem);
100 
101         menuItem.addActionListener(new ActionListener() {
102             public void actionPerformed(ActionEvent e) {
103                 ClientHelper.copyTableToClipboard(FolderTable.this);
104             }
105         });
106 
107         getSelectionModel().addListSelectionListener(new ListSelectionListener() {
108             public void valueChanged(ListSelectionEvent e) {
109                 if (e.getValueIsAdjusting()) {
110                     return;
111                 }
112 
113                 int row = getSelectedRow();
114                 if (row > -1) {
115                     String id = getModel().getValueAt(getRowSorter().convertRowIndexToModel(row), ID_COLUMN).toString();
116 
117                     try {
118                         setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
119                         model.loadObject(id);
120                     } catch (Exception ex) {
121                         ClientHelper.showError(null, ex);
122                         return;
123                     } finally {
124                         setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
125                     }
126                 }
127             }
128         });
129 
130         addMouseListener(new MouseAdapter() {
131             public void mouseClicked(MouseEvent e) {
132                 if (e.getClickCount() == 2) {
133                     doAction(e.isShiftDown());
134                 }
135             }
136 
137             public void mousePressed(MouseEvent e) {
138                 maybeShowPopup(e);
139             }
140 
141             public void mouseReleased(MouseEvent e) {
142                 maybeShowPopup(e);
143             }
144 
145             private void maybeShowPopup(MouseEvent e) {
146                 if (e.isPopupTrigger()) {
147                     popup.show(e.getComponent(), e.getX(), e.getY());
148                 }
149             }
150         });
151 
152         addKeyListener(new KeyListener() {
153             public void keyTyped(KeyEvent e) {
154             }
155 
156             public void keyReleased(KeyEvent e) {
157                 if (e.getKeyCode() == KeyEvent.VK_SPACE) {
158                     doAction(e.isShiftDown());
159                 }
160             }
161 
162             public void keyPressed(KeyEvent e) {
163             }
164         });
165 
166         loadIcons();
167     }
168 
169     private void loadIcons() {
170         icons = new HashMap<BaseTypeId, ImageIcon>();
171         icons.put(BaseTypeId.CMIS_DOCUMENT, ClientHelper.getIcon("document.png"));
172         icons.put(BaseTypeId.CMIS_FOLDER, ClientHelper.getIcon("folder.png"));
173         icons.put(BaseTypeId.CMIS_RELATIONSHIP, ClientHelper.getIcon("relationship.png"));
174         icons.put(BaseTypeId.CMIS_POLICY, ClientHelper.getIcon("policy.png"));
175 
176         checkedOutIcon = ClientHelper.getIcon("checkedout.png");
177         pwcIcon = ClientHelper.getIcon("pwc.png");
178     }
179 
180     public void folderLoaded(ClientModelEvent event) {
181         event.getClientModel().getCurrentChildren();
182 
183         ((FolderTableModel) getModel()).fireTableDataChanged();
184     }
185 
186     private void doAction(boolean alternate) {
187         int row = getSelectedRow();
188         if ((row > -1) && (row < model.getCurrentChildren().size())) {
189             String id = getModel().getValueAt(getRowSorter().convertRowIndexToModel(row), ID_COLUMN).toString();
190             CmisObject object = model.getFromCurrentChildren(id);
191 
192             if (object instanceof Document) {
193                 if (alternate) {
194                     ClientHelper.download(this.getParent(), (Document) object, null);
195                 } else {
196                     ClientHelper.open(this.getParent(), (Document) object, null);
197                 }
198             } else if (object instanceof Folder) {
199                 try {
200                     setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
201                     model.loadFolder(object.getId(), false);
202                 } catch (Exception ex) {
203                     ClientHelper.showError(null, ex);
204                     return;
205                 } finally {
206                     setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
207                 }
208             }
209         }
210     }
211 
212     class FolderTableModel extends AbstractTableModel {
213 
214         private static final long serialVersionUID = 1L;
215 
216         public String getColumnName(int columnIndex) {
217             return COLUMN_NAMES[columnIndex];
218         }
219 
220         public int getColumnCount() {
221             return COLUMN_NAMES.length;
222         }
223 
224         public int getRowCount() {
225             return model.getCurrentChildren().size();
226         }
227 
228         public Object getValueAt(int rowIndex, int columnIndex) {
229             CmisObject obj = model.getCurrentChildren().get(rowIndex);
230 
231             switch (columnIndex) {
232             case 0:
233                 if (obj instanceof Document) {
234                     Document doc = (Document) obj;
235                     if (Boolean.TRUE.equals(doc.isVersionSeriesCheckedOut())) {
236                         if (doc.getId().equals(doc.getVersionSeriesCheckedOutId())) {
237                             return pwcIcon;
238                         } else {
239                             return checkedOutIcon;
240                         }
241                     } else {
242                         return icons.get(BaseTypeId.CMIS_DOCUMENT);
243                     }
244                 }
245                 return icons.get(obj.getBaseTypeId());
246             case 1:
247                 return obj.getName();
248             case 2:
249                 return obj.getType().getId();
250             case 3:
251                 if (obj instanceof Document) {
252                     return ((Document) obj).getContentStreamMimeType();
253                 } else {
254                     return null;
255                 }
256             case 4:
257                 if (obj instanceof Document) {
258                     return ((Document) obj).getContentStreamLength();
259                 } else {
260                     return null;
261                 }
262             case 5:
263                 return obj.getCreationDate();
264             case 6:
265                 return obj.getCreatedBy();
266             case 7:
267                 return obj.getLastModificationDate();
268             case 8:
269                 return obj.getLastModifiedBy();
270             case ID_COLUMN:
271                 return obj.getId();
272             }
273 
274             return "";
275         }
276 
277         @Override
278         public Class<?> getColumnClass(int columnIndex) {
279             switch (columnIndex) {
280             case 0:
281                 return ImageIcon.class;
282             case 4:
283                 return Long.class;
284             case 5:
285             case 7:
286                 return GregorianCalendar.class;
287             }
288 
289             return String.class;
290         }
291     }
292 
293     class FolderTransferHandler extends TransferHandler {
294 
295         private static final long serialVersionUID = 1L;
296 
297         public FolderTransferHandler() {
298             super();
299         }
300 
301         @Override
302         public boolean canImport(TransferSupport support) {
303             if (!support.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
304                 return false;
305             }
306 
307             if (!support.isDrop()) {
308                 return false;
309             }
310 
311             return true;
312         }
313 
314         @SuppressWarnings("unchecked")
315         @Override
316         public boolean importData(TransferSupport support) {
317             if (!canImport(support)) {
318                 return false;
319             }
320 
321             File file = null;
322             try {
323                 List<File> fileList = (List<File>) support.getTransferable().getTransferData(
324                         DataFlavor.javaFileListFlavor);
325 
326                 if ((fileList == null) || (fileList.size() != 1) || (fileList.get(0) == null)
327                         || !fileList.get(0).isFile()) {
328                     return false;
329                 }
330 
331                 file = fileList.get(0);
332             } catch (Exception ex) {
333                 ClientHelper.showError(null, ex);
334                 return false;
335             }
336 
337             new CreateDocumentDialog(null, model, file);
338 
339             return true;
340         }
341 
342         @Override
343         public int getSourceActions(JComponent c) {
344             return COPY;
345         }
346 
347         @Override
348         protected Transferable createTransferable(JComponent c) {
349             int row = getSelectedRow();
350             if ((row > -1) && (row < model.getCurrentChildren().size())) {
351                 String id = getValueAt(row, ID_COLUMN).toString();
352                 CmisObject object = model.getFromCurrentChildren(id);
353 
354                 if (object instanceof Document) {
355                     Document doc = (Document) object;
356 
357                     File tempFile = null;
358                     try {
359                         tempFile = ClientHelper.createTempFileFromDocument(doc, null);
360                     } catch (Exception e) {
361                         ClientHelper.showError(null, e);
362                     }
363 
364                     final File tempTransFile = tempFile;
365 
366                     return new Transferable() {
367                         public boolean isDataFlavorSupported(DataFlavor flavor) {
368                             return flavor == DataFlavor.javaFileListFlavor;
369                         }
370 
371                         public DataFlavor[] getTransferDataFlavors() {
372                             return new DataFlavor[] { DataFlavor.javaFileListFlavor };
373                         }
374 
375                         public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
376                             return (List<File>) Collections.singletonList(tempTransFile);
377                         }
378                     };
379                 }
380             }
381 
382             return null;
383         }
384     }
385 }