This project has retired. For details please refer to its Attic page.
ObjectPanel 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 groovy.ui.Console;
22  
23  import java.awt.BorderLayout;
24  import java.awt.Cursor;
25  import java.awt.Font;
26  import java.awt.event.ActionEvent;
27  import java.awt.event.ActionListener;
28  import java.io.File;
29  import java.io.IOException;
30  import java.io.Writer;
31  import java.util.ArrayList;
32  import java.util.Collections;
33  import java.util.HashMap;
34  import java.util.HashSet;
35  import java.util.List;
36  import java.util.Map;
37  import java.util.Set;
38  
39  import javax.script.ScriptEngineFactory;
40  import javax.script.ScriptEngineManager;
41  import javax.swing.BorderFactory;
42  import javax.swing.BoxLayout;
43  import javax.swing.JButton;
44  import javax.swing.JPanel;
45  import javax.swing.JTextArea;
46  import javax.swing.JTextField;
47  import javax.swing.SwingUtilities;
48  
49  import org.apache.chemistry.opencmis.client.api.CmisObject;
50  import org.apache.chemistry.opencmis.client.api.Document;
51  import org.apache.chemistry.opencmis.client.api.FileableCmisObject;
52  import org.apache.chemistry.opencmis.client.api.Folder;
53  import org.apache.chemistry.opencmis.client.api.Session;
54  import org.apache.chemistry.opencmis.client.bindings.spi.LinkAccess;
55  import org.apache.chemistry.opencmis.tck.CmisTestGroup;
56  import org.apache.chemistry.opencmis.workbench.ClientHelper;
57  import org.apache.chemistry.opencmis.workbench.checks.ObjectComplianceTestGroup;
58  import org.apache.chemistry.opencmis.workbench.checks.SwingReport;
59  import org.apache.chemistry.opencmis.workbench.model.ClientModel;
60  import org.apache.chemistry.opencmis.workbench.model.ClientModelEvent;
61  import org.apache.chemistry.opencmis.workbench.model.ObjectListener;
62  import org.apache.chemistry.opencmis.workbench.swing.InfoPanel;
63  
64  public class ObjectPanel extends InfoPanel implements ObjectListener {
65  
66      private static final long serialVersionUID = 1L;
67  
68      private final Set<String> scriptExtensions;
69  
70      private JTextField nameField;
71      private JTextField idField;
72      private JTextField typeField;
73      private JTextField basetypeField;
74      private JTextField versionLabelField;
75      private JTextField pwcField;
76      private JTextField contentUrlField;
77      private InfoList paths;
78      private InfoList allowableActionsList;
79      private JPanel buttonPanel;
80      private JButton refreshButton;
81      private JButton checkButton;
82      private JPanel scriptPanel;
83      private JButton scriptOpenButton;
84      private JButton scriptRunButton;
85      private JTextArea scriptOutput;
86      private JTextAreaWriter scriptOutputWriter;
87  
88      public ObjectPanel(ClientModel model) {
89          super(model);
90  
91          model.addObjectListener(this);
92  
93          // get all installed script engines
94          scriptExtensions = new HashSet<String>();
95          ScriptEngineManager mgr = new ScriptEngineManager();
96          for (ScriptEngineFactory sef : mgr.getEngineFactories()) {
97              scriptExtensions.addAll(sef.getExtensions());
98          }
99  
100         createGUI();
101     }
102 
103     public void objectLoaded(ClientModelEvent event) {
104         CmisObject object = getClientModel().getCurrentObject();
105 
106         if (object == null) {
107             nameField.setText("");
108             idField.setText("");
109             typeField.setText("");
110             basetypeField.setText("");
111             versionLabelField.setText("");
112             pwcField.setText("");
113             paths.removeAll();
114             contentUrlField.setText("");
115             allowableActionsList.removeAll();
116             refreshButton.setEnabled(false);
117             checkButton.setEnabled(false);
118             scriptPanel.setVisible(false);
119         } else {
120             try {
121                 nameField.setText(object.getName());
122                 idField.setText(object.getId());
123                 typeField.setText(object.getType().getId());
124                 basetypeField.setText(object.getBaseTypeId().toString());
125                 if (object instanceof Document) {
126                     Document doc = (Document) object;
127 
128                     try {
129                         versionLabelField.setText(doc.getVersionLabel());
130                     } catch (Exception e) {
131                         versionLabelField.setText("???");
132                     }
133 
134                     if (doc.isVersionSeriesCheckedOut() == null) {
135                         pwcField.setText("");
136                     } else if (doc.isVersionSeriesCheckedOut().booleanValue()) {
137                         pwcField.setText(doc.getVersionSeriesCheckedOutId());
138                     } else {
139                         pwcField.setText("(not checked out)");
140                     }
141                 } else {
142                     pwcField.setText("");
143                     versionLabelField.setText("");
144                 }
145 
146                 if (object instanceof FileableCmisObject) {
147                     if (object instanceof Folder) {
148                         paths.setList(Collections.singletonList(((Folder) object).getPath()));
149                     } else {
150                         paths.setList(Collections.singletonList(""));
151                         final FileableCmisObject pathObject = (FileableCmisObject) object;
152                         SwingUtilities.invokeLater(new Runnable() {
153                             @Override
154                             public void run() {
155                                 try {
156                                     List<String> pathsList = pathObject.getPaths();
157                                     if ((pathsList == null) || (pathsList.size() == 0)) {
158                                         paths.setList(Collections.singletonList("(unfiled)"));
159                                     } else {
160                                         paths.setList(pathsList);
161                                     }
162                                 } catch (Exception e) {
163                                     paths.setList(Collections.singletonList("(???)"));
164                                     // ClientHelper.showError(null, e);
165                                 }
166                                 ObjectPanel.this.revalidate();
167                             }
168                         });
169                     }
170                 } else {
171                     paths.setList(Collections.singletonList("(not filable)"));
172                 }
173 
174                 String docUrl = getDocumentURL(object, getClientModel().getClientSession().getSession());
175                 if (docUrl != null) {
176                     contentUrlField.setText(docUrl);
177                 } else {
178                     contentUrlField.setText("(not available)");
179                 }
180 
181                 if (object.getAllowableActions() != null) {
182                     allowableActionsList.setList(object.getAllowableActions().getAllowableActions());
183                 } else {
184                     allowableActionsList.setList(Collections.singletonList("(missing)"));
185                 }
186 
187                 refreshButton.setEnabled(true);
188                 checkButton.setEnabled(true);
189 
190                 if (object instanceof Document) {
191                     String name = object.getName().toLowerCase();
192                     int x = name.lastIndexOf('.');
193                     if ((x > -1) && (scriptExtensions.contains(name.substring(x + 1)))) {
194                         scriptPanel.setVisible(true);
195                         scriptOutput.setVisible(false);
196                     } else {
197                         scriptPanel.setVisible(false);
198                     }
199                 }
200             } catch (Exception e) {
201                 ClientHelper.showError(this, e);
202             }
203         }
204 
205         revalidate();
206     }
207 
208     private void createGUI() {
209         setupGUI();
210 
211         nameField = addLine("Name:", true);
212         idField = addId("Id:");
213         typeField = addLine("Type:");
214         basetypeField = addLine("Base Type:");
215         paths = addComponent("Paths:", new InfoList());
216         versionLabelField = addLine("Version Label:");
217         pwcField = addId("PWC:");
218         contentUrlField = addLink("Content URL:");
219         allowableActionsList = addComponent("Allowable Actions:", new InfoList());
220 
221         buttonPanel = addComponent("", new JPanel(new BorderLayout()));
222         buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
223         buttonPanel.setOpaque(false);
224 
225         refreshButton = new JButton("Refresh");
226         refreshButton.setEnabled(false);
227         buttonPanel.add(refreshButton);
228         checkButton = new JButton("Check specification compliance");
229         checkButton.setEnabled(false);
230         buttonPanel.add(checkButton);
231 
232         scriptPanel = addComponent("", new JPanel(new BorderLayout()));
233         scriptPanel.setOpaque(false);
234         scriptPanel.setVisible(false);
235 
236         JPanel scriptButtonPanel = new JPanel();
237         scriptButtonPanel.setLayout(new BoxLayout(scriptButtonPanel, BoxLayout.LINE_AXIS));
238         scriptButtonPanel.setOpaque(false);
239         scriptPanel.add(scriptButtonPanel, BorderLayout.PAGE_START);
240         scriptOpenButton = new JButton("Open Script");
241         scriptButtonPanel.add(scriptOpenButton);
242         scriptRunButton = new JButton("Run Script");
243         scriptButtonPanel.add(scriptRunButton);
244 
245         scriptOutput = new JTextArea(null, 1, 80);
246         scriptOutput.setEditable(false);
247         scriptOutput.setFont(Font.decode("Monospaced"));
248         scriptOutput.setBorder(BorderFactory.createTitledBorder(""));
249         scriptOutputWriter = new JTextAreaWriter(scriptOutput);
250         scriptPanel.add(scriptOutput, BorderLayout.CENTER);
251 
252         refreshButton.addActionListener(new ActionListener() {
253             public void actionPerformed(ActionEvent e) {
254                 try {
255                     setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
256                     getClientModel().reloadObject();
257                 } catch (Exception ex) {
258                     ClientHelper.showError(null, ex);
259                 } finally {
260                     setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
261                 }
262             }
263         });
264 
265         checkButton.addActionListener(new ActionListener() {
266             public void actionPerformed(ActionEvent e) {
267                 try {
268                     setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
269 
270                     Map<String, String> parameters = new HashMap<String, String>(getClientModel().getClientSession()
271                             .getSessionParameters());
272                     String objectId = getClientModel().getCurrentObject().getId();
273 
274                     ObjectComplianceTestGroup octg = new ObjectComplianceTestGroup(parameters, objectId);
275                     octg.run();
276 
277                     List<CmisTestGroup> groups = new ArrayList<CmisTestGroup>();
278                     groups.add(octg);
279                     SwingReport report = new SwingReport(null, 700, 500);
280                     report.createReport(parameters, groups, (Writer) null);
281                 } catch (Exception ex) {
282                     ClientHelper.showError(null, ex);
283                 } finally {
284                     setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
285                 }
286             }
287         });
288 
289         scriptOpenButton.addActionListener(new ActionListener() {
290             public void actionPerformed(ActionEvent e) {
291                 try {
292                     setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
293                     Document doc = (Document) getClientModel().getCurrentObject();
294 
295                     String name = doc.getName().toLowerCase();
296                     if (name.endsWith(".groovy")) {
297                         File file = ClientHelper.createTempFileFromDocument(doc, null);
298                         Console console = ClientHelper.openConsole(ObjectPanel.this, getClientModel(), null);
299                         if (console != null) {
300                             console.loadScriptFile(file);
301                         }
302                     } else {
303                         ClientHelper.open(ObjectPanel.this, doc, null);
304                     }
305                 } catch (Exception ex) {
306                     ClientHelper.showError(null, ex);
307                 } finally {
308                     setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
309                 }
310             }
311         });
312 
313         scriptRunButton.addActionListener(new ActionListener() {
314             public void actionPerformed(ActionEvent e) {
315                 try {
316                     setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
317                     Document doc = (Document) getClientModel().getCurrentObject();
318                     File file = ClientHelper.createTempFileFromDocument(doc, null);
319                     String name = doc.getName().toLowerCase();
320                     String ext = name.substring(name.lastIndexOf('.') + 1);
321 
322                     scriptOutput.setText("");
323                     scriptOutput.setVisible(true);
324                     scriptOutput.invalidate();
325 
326                     ClientHelper.runJSR223Script(ObjectPanel.this, getClientModel(), file, ext, scriptOutputWriter);
327                 } catch (Exception ex) {
328                     ClientHelper.showError(null, ex);
329                 } finally {
330                     setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
331                 }
332             }
333         });
334 
335     }
336 
337     public String getDocumentURL(final CmisObject document, final Session session) {
338         if (!(document instanceof Document)) {
339             return null;
340         }
341 
342         if (session.getBinding().getObjectService() instanceof LinkAccess) {
343             return ((LinkAccess) session.getBinding().getObjectService()).loadContentLink(session.getRepositoryInfo()
344                     .getId(), document.getId());
345         }
346 
347         return null;
348     }
349 
350     private static class JTextAreaWriter extends Writer {
351         private final JTextArea textArea;
352 
353         public JTextAreaWriter(JTextArea textArea) {
354             this.textArea = textArea;
355         }
356 
357         @Override
358         public void write(final char[] cbuf, final int off, final int len) throws IOException {
359             final String s = new String(cbuf, off, len);
360             SwingUtilities.invokeLater(new Runnable() {
361                 public void run() {
362                     textArea.append(s);
363                 }
364             });
365         }
366 
367         @Override
368         public void flush() throws IOException {
369         }
370 
371         @Override
372         public void close() throws IOException {
373         }
374     }
375 }