This project has retired. For details please refer to its Attic page.
CreateRelationshipDialog 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.Frame;
23  import java.awt.event.ActionEvent;
24  import java.awt.event.ActionListener;
25  
26  import javax.swing.JButton;
27  import javax.swing.JComboBox;
28  import javax.swing.JOptionPane;
29  import javax.swing.JTextField;
30  
31  import org.apache.chemistry.opencmis.client.api.ObjectId;
32  import org.apache.chemistry.opencmis.commons.enums.BaseTypeId;
33  import org.apache.chemistry.opencmis.workbench.model.ClientModel;
34  import org.apache.chemistry.opencmis.workbench.swing.CreateDialog;
35  
36  public class CreateRelationshipDialog extends CreateDialog {
37  
38      private static final long serialVersionUID = 1L;
39  
40      private JTextField nameField;
41      private JComboBox typeBox;
42      private JTextField sourceIdField;
43      private JTextField targetIdField;
44  
45      public CreateRelationshipDialog(Frame owner, ClientModel model) {
46          super(owner, "Create Relationship", model);
47          createGUI();
48      }
49  
50      private void createGUI() {
51          final CreateRelationshipDialog thisDialog = this;
52  
53          nameField = new JTextField(60);
54          createRow("Name:", nameField, 0);
55  
56          Object[] types = getTypes(BaseTypeId.CMIS_RELATIONSHIP.value());
57          if (types.length == 0) {
58              JOptionPane.showMessageDialog(this, "No creatable type!", "Creatable Types", JOptionPane.ERROR_MESSAGE);
59              thisDialog.dispose();
60              return;
61          }
62  
63          typeBox = new JComboBox(types);
64          typeBox.setSelectedIndex(0);
65          createRow("Type:", typeBox, 1);
66  
67          sourceIdField = new JTextField(60);
68          if (getClientModel().getCurrentObject() != null) {
69              sourceIdField.setText(getClientModel().getCurrentObject().getId());
70          }
71          createRow("Source Id:", sourceIdField, 2);
72  
73          targetIdField = new JTextField(60);
74          createRow("Target Id:", targetIdField, 3);
75  
76          JButton createButton = new JButton("Create Relationship");
77          createButton.addActionListener(new ActionListener() {
78              public void actionPerformed(ActionEvent event) {
79                  String name = nameField.getText();
80                  String type = ((ObjectTypeItem) typeBox.getSelectedItem()).getObjectType().getId();
81                  String sourceId = sourceIdField.getText();
82                  String targetId = targetIdField.getText();
83  
84                  try {
85                      setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
86  
87                      ObjectId objectId = getClientModel().createRelationship(name, type, sourceId, targetId);
88  
89                      if (objectId != null) {
90                          getClientModel().loadObject(objectId.getId());
91                      }
92  
93                      thisDialog.setVisible(false);
94                      thisDialog.dispose();
95                  } catch (Exception e) {
96                      ClientHelper.showError(null, e);
97                  } finally {
98                      setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
99  
100                     try {
101                         getClientModel().reloadFolder();
102                     } catch (Exception e) {
103                         ClientHelper.showError(null, e);
104                     }
105                 }
106             }
107         });
108         createRow("", createButton, 4);
109 
110         getRootPane().setDefaultButton(createButton);
111 
112         showDialog();
113     }
114 }