This project has retired. For details please refer to its Attic page.
ClientFrame 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.Container;
23  import java.awt.Cursor;
24  import java.awt.Dimension;
25  import java.awt.Point;
26  import java.awt.event.ActionEvent;
27  import java.awt.event.ActionListener;
28  import java.awt.event.WindowEvent;
29  import java.awt.event.WindowListener;
30  import java.util.Collections;
31  import java.util.List;
32  import java.util.prefs.Preferences;
33  
34  import javax.swing.ImageIcon;
35  import javax.swing.JButton;
36  import javax.swing.JFrame;
37  import javax.swing.JMenuItem;
38  import javax.swing.JPopupMenu;
39  import javax.swing.JSplitPane;
40  import javax.swing.JToolBar;
41  
42  import org.apache.chemistry.opencmis.workbench.ClientHelper.FileEntry;
43  import org.apache.chemistry.opencmis.workbench.details.DetailsTabs;
44  import org.apache.chemistry.opencmis.workbench.model.ClientModel;
45  import org.apache.chemistry.opencmis.workbench.model.ClientSession;
46  
47  public class ClientFrame extends JFrame implements WindowListener {
48  
49      private static final long serialVersionUID = 1L;
50  
51      private static final String WINDOW_TITLE = "CMIS Workbench";
52  
53      private static final int BUTTON_CONNECT = 0;
54      private static final int BUTTON_REPOSITORY_INFO = 1;
55      private static final int BUTTON_TYPES = 2;
56      private static final int BUTTON_QUERY = 3;
57      private static final int BUTTON_CHANGELOG = 4;
58      private static final int BUTTON_CONSOLE = 5;
59      private static final int BUTTON_TCK = 6;
60      private static final int BUTTON_CREATE_DOCUMENT = 7;
61      private static final int BUTTON_CREATE_FOLDER = 8;
62      private static final int BUTTON_CREATE_RELATIONSHIP = 9;
63      private static final int BUTTON_LOG = 10;
64      private static final int BUTTON_INFO = 11;
65  
66      private static final String PREFS_X = "x";
67      private static final String PREFS_Y = "y";
68      private static final String PREFS_WIDTH = "width";
69      private static final String PREFS_HEIGHT = "height";
70      private static final String PREFS_DIV = "div";
71  
72      private static final String GROOVY_SCRIPT_FOLDER = "/scripts/";
73      private static final String GROOVY_SCRIPT_LIBRARY = "script-library.properties";
74  
75      private final Preferences prefs = Preferences.userNodeForPackage(this.getClass());
76  
77      private LoginDialog loginDialog;
78      private LogFrame logFrame;
79      private InfoDialog infoDialog;
80  
81      private JToolBar toolBar;
82      private JButton[] toolbarButton;
83      private JPopupMenu toolbarConsolePopup;
84  
85      private JSplitPane split;
86      private FolderPanel folderPanel;
87      private DetailsTabs detailsTabs;
88  
89      private final ClientModel model;
90  
91      public ClientFrame() {
92          super();
93          ClientHelper.installKeyBindings();
94  
95          model = new ClientModel();
96          createGUI();
97          showLoginForm();
98      }
99  
100     private void createGUI() {
101         setTitle(WINDOW_TITLE);
102 
103         ImageIcon icon = ClientHelper.getIcon("icon.png");
104         if (icon != null) {
105             setIconImage(icon.getImage());
106         }
107 
108         setLayout(new BorderLayout());
109 
110         final ClientFrame thisFrame = this;
111         loginDialog = new LoginDialog(this);
112         logFrame = new LogFrame();
113         infoDialog = new InfoDialog(this);
114 
115         Container pane = getContentPane();
116 
117         toolBar = new JToolBar("CMIS Toolbar", JToolBar.HORIZONTAL);
118 
119         toolbarButton = new JButton[12];
120 
121         toolbarButton[BUTTON_CONNECT] = new JButton("Connection", ClientHelper.getIcon("connect.png"));
122         toolbarButton[BUTTON_CONNECT].addActionListener(new ActionListener() {
123             public void actionPerformed(ActionEvent e) {
124                 showLoginForm();
125             }
126         });
127 
128         toolBar.add(toolbarButton[BUTTON_CONNECT]);
129 
130         toolBar.addSeparator();
131 
132         toolbarButton[BUTTON_REPOSITORY_INFO] = new JButton("Repository Info",
133                 ClientHelper.getIcon("repository-info.png"));
134         toolbarButton[BUTTON_REPOSITORY_INFO].setEnabled(false);
135         toolbarButton[BUTTON_REPOSITORY_INFO].addActionListener(new ActionListener() {
136             public void actionPerformed(ActionEvent e) {
137                 new RepositoryInfoFrame(model);
138             }
139         });
140 
141         toolBar.add(toolbarButton[BUTTON_REPOSITORY_INFO]);
142 
143         toolbarButton[BUTTON_TYPES] = new JButton("Types", ClientHelper.getIcon("types.png"));
144         toolbarButton[BUTTON_TYPES].setEnabled(false);
145         toolbarButton[BUTTON_TYPES].addActionListener(new ActionListener() {
146             public void actionPerformed(ActionEvent e) {
147                 new TypesFrame(model);
148             }
149         });
150 
151         toolBar.add(toolbarButton[BUTTON_TYPES]);
152 
153         toolbarButton[BUTTON_QUERY] = new JButton("Query", ClientHelper.getIcon("query.png"));
154         toolbarButton[BUTTON_QUERY].setEnabled(false);
155         toolbarButton[BUTTON_QUERY].addActionListener(new ActionListener() {
156             public void actionPerformed(ActionEvent e) {
157                 new QueryFrame(model);
158             }
159         });
160 
161         toolBar.add(toolbarButton[BUTTON_QUERY]);
162 
163         toolbarButton[BUTTON_CHANGELOG] = new JButton("Change Log", ClientHelper.getIcon("changelog.png"));
164         toolbarButton[BUTTON_CHANGELOG].setEnabled(false);
165         toolbarButton[BUTTON_CHANGELOG].addActionListener(new ActionListener() {
166             public void actionPerformed(ActionEvent e) {
167                 new ChangeLogFrame(model);
168             }
169         });
170 
171         toolBar.add(toolbarButton[BUTTON_CHANGELOG]);
172 
173         toolbarButton[BUTTON_CONSOLE] = new JButton("Console", ClientHelper.getIcon("console.png"));
174         toolbarButton[BUTTON_CONSOLE].setEnabled(false);
175         toolbarButton[BUTTON_CONSOLE].addActionListener(new ActionListener() {
176             public void actionPerformed(ActionEvent e) {
177                 toolbarConsolePopup.show(toolbarButton[BUTTON_CONSOLE], 0, toolbarButton[BUTTON_CONSOLE].getHeight());
178             }
179         });
180 
181         toolBar.add(toolbarButton[BUTTON_CONSOLE]);
182 
183         toolbarConsolePopup = new JPopupMenu();
184         for (FileEntry fe : readScriptLibrary()) {
185             JMenuItem menuItem = new JMenuItem(fe.getName());
186             final String file = fe.getFile();
187             menuItem.addActionListener(new ActionListener() {
188                 @Override
189                 public void actionPerformed(ActionEvent e) {
190                     ClientHelper.openConsole(ClientFrame.this, model, file);
191                 }
192             });
193             toolbarConsolePopup.add(menuItem);
194         }
195 
196         toolbarButton[BUTTON_TCK] = new JButton("TCK", ClientHelper.getIcon("tck.png"));
197         toolbarButton[BUTTON_TCK].setEnabled(false);
198         toolbarButton[BUTTON_TCK].addActionListener(new ActionListener() {
199             public void actionPerformed(ActionEvent e) {
200                 new TckDialog(thisFrame, model);
201             }
202         });
203 
204         toolBar.add(toolbarButton[BUTTON_TCK]);
205 
206         toolBar.addSeparator();
207 
208         toolbarButton[BUTTON_CREATE_DOCUMENT] = new JButton("Create Document", ClientHelper.getIcon("newdocument.png"));
209         toolbarButton[BUTTON_CREATE_DOCUMENT].setEnabled(false);
210         toolbarButton[BUTTON_CREATE_DOCUMENT].addActionListener(new ActionListener() {
211             public void actionPerformed(ActionEvent e) {
212                 new CreateDocumentDialog(thisFrame, model);
213             }
214         });
215 
216         toolBar.add(toolbarButton[BUTTON_CREATE_DOCUMENT]);
217 
218         toolbarButton[BUTTON_CREATE_FOLDER] = new JButton("Create Folder", ClientHelper.getIcon("newfolder.png"));
219         toolbarButton[BUTTON_CREATE_FOLDER].setEnabled(false);
220         toolbarButton[BUTTON_CREATE_FOLDER].addActionListener(new ActionListener() {
221             public void actionPerformed(ActionEvent e) {
222                 new CreateFolderDialog(thisFrame, model);
223             }
224         });
225 
226         toolBar.add(toolbarButton[BUTTON_CREATE_FOLDER]);
227 
228         toolbarButton[BUTTON_CREATE_RELATIONSHIP] = new JButton("Create Relationship",
229                 ClientHelper.getIcon("newrelationship.png"));
230         toolbarButton[BUTTON_CREATE_RELATIONSHIP].setEnabled(false);
231         toolbarButton[BUTTON_CREATE_RELATIONSHIP].addActionListener(new ActionListener() {
232             public void actionPerformed(ActionEvent e) {
233                 new CreateRelationshipDialog(thisFrame, model);
234             }
235         });
236 
237         toolBar.add(toolbarButton[BUTTON_CREATE_RELATIONSHIP]);
238 
239         toolBar.addSeparator();
240 
241         toolbarButton[BUTTON_LOG] = new JButton("Log", ClientHelper.getIcon("log.png"));
242         toolbarButton[BUTTON_LOG].addActionListener(new ActionListener() {
243             public void actionPerformed(ActionEvent e) {
244                 logFrame.showFrame();
245             }
246         });
247 
248         toolBar.add(toolbarButton[BUTTON_LOG]);
249 
250         toolbarButton[BUTTON_INFO] = new JButton("Info", ClientHelper.getIcon("info.png"));
251         toolbarButton[BUTTON_INFO].addActionListener(new ActionListener() {
252             public void actionPerformed(ActionEvent e) {
253                 infoDialog.showDialog();
254             }
255         });
256 
257         toolBar.add(toolbarButton[BUTTON_INFO]);
258 
259         pane.add(toolBar, BorderLayout.PAGE_START);
260 
261         folderPanel = new FolderPanel(model);
262         detailsTabs = new DetailsTabs(model);
263 
264         split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, folderPanel, detailsTabs);
265 
266         pane.add(split, BorderLayout.CENTER);
267 
268         addWindowListener(this);
269 
270         setPreferredSize(new Dimension(prefs.getInt(PREFS_WIDTH, 1000), prefs.getInt(PREFS_HEIGHT, 600)));
271         setMinimumSize(new Dimension(200, 60));
272 
273         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
274         pack();
275 
276         split.setDividerLocation(prefs.getInt(PREFS_DIV, 500));
277 
278         if (prefs.getInt(PREFS_X, Integer.MAX_VALUE) == Integer.MAX_VALUE) {
279             setLocationRelativeTo(null);
280         } else {
281             setLocation(prefs.getInt(PREFS_X, 0), prefs.getInt(PREFS_Y, 0));
282         }
283 
284         setVisible(true);
285     }
286 
287     private void showLoginForm() {
288         loginDialog.showDialog();
289         if (!loginDialog.isCanceled()) {
290             ClientSession clientSession = loginDialog.getClientSession();
291 
292             model.setClientSession(clientSession);
293 
294             try {
295                 setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
296 
297                 model.loadFolder(clientSession.getSession().getRepositoryInfo().getRootFolderId(), false);
298                 model.loadObject(clientSession.getSession().getRepositoryInfo().getRootFolderId());
299 
300                 toolbarButton[BUTTON_REPOSITORY_INFO].setEnabled(true);
301                 toolbarButton[BUTTON_TYPES].setEnabled(true);
302                 toolbarButton[BUTTON_QUERY].setEnabled(model.supportsQuery());
303                 toolbarButton[BUTTON_CHANGELOG].setEnabled(model.supportsChangeLog());
304                 toolbarButton[BUTTON_CONSOLE].setEnabled(true);
305                 toolbarButton[BUTTON_TCK].setEnabled(true);
306                 toolbarButton[BUTTON_CREATE_DOCUMENT].setEnabled(true);
307                 toolbarButton[BUTTON_CREATE_FOLDER].setEnabled(true);
308                 toolbarButton[BUTTON_CREATE_RELATIONSHIP].setEnabled(model.supportsRelationships());
309 
310                 setTitle(WINDOW_TITLE + " - " + clientSession.getSession().getRepositoryInfo().getName());
311             } catch (Exception ex) {
312                 toolbarButton[BUTTON_REPOSITORY_INFO].setEnabled(false);
313                 toolbarButton[BUTTON_TYPES].setEnabled(false);
314                 toolbarButton[BUTTON_QUERY].setEnabled(false);
315                 toolbarButton[BUTTON_CHANGELOG].setEnabled(false);
316                 toolbarButton[BUTTON_CONSOLE].setEnabled(false);
317                 toolbarButton[BUTTON_TCK].setEnabled(false);
318                 toolbarButton[BUTTON_CREATE_DOCUMENT].setEnabled(false);
319                 toolbarButton[BUTTON_CREATE_FOLDER].setEnabled(false);
320                 toolbarButton[BUTTON_CREATE_RELATIONSHIP].setEnabled(false);
321 
322                 ClientHelper.showError(null, ex);
323 
324                 setTitle(WINDOW_TITLE);
325             } finally {
326                 setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
327             }
328         }
329     }
330 
331     private List<FileEntry> readScriptLibrary() {
332         List<FileEntry> result = ClientHelper.readFileProperties(GROOVY_SCRIPT_FOLDER + GROOVY_SCRIPT_LIBRARY,
333                 GROOVY_SCRIPT_FOLDER);
334         if (result == null) {
335             result = Collections.singletonList(new FileEntry("Groovy Console", null));
336         }
337 
338         return result;
339     }
340 
341     public void windowOpened(WindowEvent e) {
342     }
343 
344     public void windowClosing(WindowEvent e) {
345         Point p = getLocation();
346         prefs.putInt(PREFS_X, p.x);
347         prefs.putInt(PREFS_Y, p.y);
348         prefs.putInt(PREFS_WIDTH, getWidth());
349         prefs.putInt(PREFS_HEIGHT, getHeight());
350         prefs.putInt(PREFS_DIV, split.getDividerLocation());
351     }
352 
353     public void windowClosed(WindowEvent e) {
354     }
355 
356     public void windowIconified(WindowEvent e) {
357     }
358 
359     public void windowDeiconified(WindowEvent e) {
360     }
361 
362     public void windowActivated(WindowEvent e) {
363     }
364 
365     public void windowDeactivated(WindowEvent e) {
366     }
367 }