This project has retired. For details please refer to its Attic page.
AbstractDetailsTable 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.details;
20  
21  import java.awt.Cursor;
22  import java.awt.event.ActionEvent;
23  import java.awt.event.ActionListener;
24  import java.awt.event.MouseAdapter;
25  import java.awt.event.MouseEvent;
26  import java.awt.event.MouseMotionListener;
27  import java.util.Collection;
28  
29  import javax.swing.JMenuItem;
30  import javax.swing.JPopupMenu;
31  import javax.swing.JTabbedPane;
32  import javax.swing.JTable;
33  import javax.swing.SwingUtilities;
34  import javax.swing.table.AbstractTableModel;
35  import javax.swing.table.TableColumn;
36  
37  import org.apache.chemistry.opencmis.client.api.CmisObject;
38  import org.apache.chemistry.opencmis.client.api.ObjectId;
39  import org.apache.chemistry.opencmis.workbench.ClientHelper;
40  import org.apache.chemistry.opencmis.workbench.model.ClientModel;
41  import org.apache.chemistry.opencmis.workbench.model.ClientModelEvent;
42  import org.apache.chemistry.opencmis.workbench.model.ObjectListener;
43  import org.apache.chemistry.opencmis.workbench.swing.CollectionRenderer;
44  import org.apache.chemistry.opencmis.workbench.swing.IdRenderer;
45  
46  public abstract class AbstractDetailsTable extends JTable implements ObjectListener {
47  
48      private static final long serialVersionUID = 1L;
49  
50      private static final Cursor HAND_CURSOR = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
51      private static final Cursor DEFAULT_CURSOR = Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);
52  
53      private ClientModel model;
54      private String[] columnNames;
55  
56      public void init(ClientModel model, String[] columnNames, int[] colummnWidths) {
57          this.model = model;
58          model.addObjectListener(this);
59  
60          this.columnNames = columnNames;
61  
62          setModel(new DetailsTableModel(this));
63  
64          setDefaultRenderer(Collection.class, new CollectionRenderer());
65          setDefaultRenderer(ObjectId.class, new IdRenderer());
66          setAutoResizeMode(AUTO_RESIZE_OFF);
67          setAutoCreateRowSorter(true);
68  
69          for (int i = 0; i < colummnWidths.length; i++) {
70              TableColumn column = getColumnModel().getColumn(i);
71              column.setPreferredWidth(colummnWidths[i]);
72          }
73  
74          setFillsViewportHeight(true);
75  
76          final JPopupMenu popup = new JPopupMenu();
77          final JMenuItem menuItem = new JMenuItem("Copy to clipboard");
78          popup.add(menuItem);
79  
80          menuItem.addActionListener(new ActionListener() {
81              public void actionPerformed(ActionEvent e) {
82                  ClientHelper.copyTableToClipboard(AbstractDetailsTable.this);
83              }
84          });
85  
86          addMouseListener(new MouseAdapter() {
87              public void mouseClicked(MouseEvent e) {
88                  if (SwingUtilities.isLeftMouseButton(e)) {
89                      int row = rowAtPoint(e.getPoint());
90                      int column = columnAtPoint(e.getPoint());
91                      if (row > -1 && column > -1) {
92                          if (e.getClickCount() == 1) {
93                              singleClickAction(e, row, column);
94                          } else if (e.getClickCount() == 2) {
95                              doubleClickAction(e, row);
96                          }
97                      }
98                  }
99              }
100 
101             public void mousePressed(MouseEvent e) {
102                 maybeShowPopup(e);
103             }
104 
105             public void mouseReleased(MouseEvent e) {
106                 maybeShowPopup(e);
107             }
108 
109             private void maybeShowPopup(MouseEvent e) {
110                 if (e.isPopupTrigger()) {
111                     popup.show(e.getComponent(), e.getX(), e.getY());
112                 }
113             }
114         });
115 
116         addMouseMotionListener(new MouseMotionListener() {
117             public void mouseMoved(MouseEvent e) {
118                 int row = rowAtPoint(e.getPoint());
119                 int column = columnAtPoint(e.getPoint());
120                 if (row > -1 && getColumnClass(column) == ObjectId.class) {
121                     setCursor(HAND_CURSOR);
122                 } else {
123                     setCursor(DEFAULT_CURSOR);
124                 }
125 
126             }
127 
128             public void mouseDragged(MouseEvent e) {
129             }
130         });
131     }
132 
133     public void objectLoaded(ClientModelEvent event) {
134         ((DetailsTableModel) getModel()).fireTableDataChanged();
135     }
136 
137     public CmisObject getObject() {
138         return model.getCurrentObject();
139     }
140 
141     public ClientModel getClientModel() {
142         return model;
143     }
144 
145     public String[] getColumnNames() {
146         return columnNames;
147     }
148 
149     public abstract int getDetailRowCount();
150 
151     public abstract Object getDetailValueAt(int rowIndex, int columnIndex);
152 
153     public Class<?> getDetailColumClass(int columnIndex) {
154         return String.class;
155     }
156 
157     public void singleClickAction(MouseEvent e, int rowIndex, int colIndex) {
158     }
159 
160     public void doubleClickAction(MouseEvent e, int rowIndex) {
161     }
162 
163     public void setTab(int tab) {
164         ((JTabbedPane) getParent().getParent().getParent()).setSelectedIndex(tab);
165     }
166 
167     static class DetailsTableModel extends AbstractTableModel {
168 
169         private final AbstractDetailsTable table;
170 
171         public DetailsTableModel(AbstractDetailsTable table) {
172             this.table = table;
173         }
174 
175         private static final long serialVersionUID = 1L;
176 
177         public String getColumnName(int columnIndex) {
178             return table.getColumnNames()[columnIndex];
179         }
180 
181         public int getColumnCount() {
182             return table.getColumnNames().length;
183         }
184 
185         public int getRowCount() {
186             if (table.getObject() == null) {
187                 return 0;
188             }
189 
190             return table.getDetailRowCount();
191         }
192 
193         public Object getValueAt(int rowIndex, int columnIndex) {
194             if (table.getObject() == null) {
195                 return null;
196             }
197 
198             return table.getDetailValueAt(rowIndex, columnIndex);
199         }
200 
201         public Class<?> getColumnClass(int columnIndex) {
202             return table.getDetailColumClass(columnIndex);
203         }
204     }
205 }