KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnet > Browser > CreateUserBundleDialog


1 /*
2  * InstallBundleDialog.java
3  *
4  * Created on 10. květen 2004, 22:42
5  */

6
7 package SOFA.SOFAnet.Browser;
8
9 import SOFA.SOFAnet.Admin.RMI.*;
10 import SOFA.SOFAnet.Repository.BundleInfo;
11 import SOFA.SOFAnode.TR.ComponentInfo;
12 import SOFA.SOFAnode.TR.Impl.ComponentInfoImpl;
13 import javax.swing.*;
14 import java.rmi.RemoteException JavaDoc;
15 import java.util.*;
16 import java.awt.Component JavaDoc;
17 import java.awt.Frame JavaDoc;
18
19 /**
20  *
21  * @author Ladislav Sobr
22  */

23 public class CreateUserBundleDialog extends javax.swing.JDialog JavaDoc
24 {
25   private RMIAdminInterface admin;
26   private DefaultListModel content;
27   private SortedSet components;
28   private boolean isOK;
29
30   public static boolean showDialog(Component JavaDoc frameComp, RMIAdminInterface admin, String JavaDoc defaultBundleName, ComponentInfo[] defaultComponents)
31   {
32     Frame JavaDoc frame = JOptionPane.getFrameForComponent(frameComp);
33     CreateUserBundleDialog dialog = new CreateUserBundleDialog(frame, true, admin, defaultBundleName, defaultComponents);
34     dialog.setLocationRelativeTo(frameComp);
35     dialog.setVisible(true);
36     return dialog.isOK();
37   }
38   
39   /** Creates new form CreateUserBundleDialog */
40   public CreateUserBundleDialog(java.awt.Frame JavaDoc parent, boolean modal, RMIAdminInterface admin, String JavaDoc defaultBundleName, ComponentInfo[] defaultComponents)
41   {
42     super(parent, modal);
43     this.admin = admin;
44     isOK = false;
45     content = new DefaultListModel();
46     components = new TreeSet();
47     initComponents();
48     
49     bundleNameTextField.setText(defaultBundleName);
50     if (defaultComponents != null)
51     {
52       for (int i = 0; i < defaultComponents.length; i++) components.add(defaultComponents[i].getName() + "[" + defaultComponents[i].getImplementationVersion() + "]");
53       reloadContent();
54     }
55   }
56
57   public boolean isOK()
58   {
59     return isOK;
60   }
61   
62   private void reloadContent()
63   {
64     content.clear();
65     content.ensureCapacity(components.size());
66     Iterator it = components.iterator();
67     while (it.hasNext()) content.addElement(it.next());
68   }
69   
70   /** This method is called from within the constructor to
71    * initialize the form.
72    * WARNING: Do NOT modify this code. The content of this method is
73    * always regenerated by the Form Editor.
74    */

75   private void initComponents()//GEN-BEGIN:initComponents
76
{
77     jPanel4 = new javax.swing.JPanel JavaDoc();
78     jPanel1 = new javax.swing.JPanel JavaDoc();
79     jPanel5 = new javax.swing.JPanel JavaDoc();
80     bundleNameLabel = new javax.swing.JLabel JavaDoc();
81     bundleNameTextField = new javax.swing.JTextField JavaDoc();
82     componentsLabel = new javax.swing.JLabel JavaDoc();
83     jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
84     componentsList = new javax.swing.JList JavaDoc();
85     jPanel6 = new javax.swing.JPanel JavaDoc();
86     subcomponentsCheckBox = new javax.swing.JCheckBox JavaDoc();
87     jPanel2 = new javax.swing.JPanel JavaDoc();
88     addComponentsButton = new javax.swing.JButton JavaDoc();
89     removeComponentsButton = new javax.swing.JButton JavaDoc();
90     jPanel3 = new javax.swing.JPanel JavaDoc();
91     createUserBundleButton = new javax.swing.JButton JavaDoc();
92     cancelButton = new javax.swing.JButton JavaDoc();
93
94     setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
95     setTitle("Create User Bundle");
96     jPanel4.setLayout(new java.awt.BorderLayout JavaDoc());
97
98     jPanel4.setMinimumSize(new java.awt.Dimension JavaDoc(400, 93));
99     jPanel4.setPreferredSize(new java.awt.Dimension JavaDoc(400, 300));
100     jPanel1.setLayout(new java.awt.BorderLayout JavaDoc());
101
102     jPanel1.setBorder(new javax.swing.border.EmptyBorder JavaDoc(new java.awt.Insets JavaDoc(1, 10, 1, 10)));
103     jPanel1.setMinimumSize(new java.awt.Dimension JavaDoc(26, 23));
104     jPanel1.setPreferredSize(new java.awt.Dimension JavaDoc(26, 23));
105     jPanel5.setLayout(new java.awt.GridLayout JavaDoc(3, 0));
106
107     bundleNameLabel.setText("Bundle Name:");
108     jPanel5.add(bundleNameLabel);
109
110     bundleNameTextField.setMinimumSize(new java.awt.Dimension JavaDoc(200, 21));
111     bundleNameTextField.setPreferredSize(new java.awt.Dimension JavaDoc(200, 21));
112     jPanel5.add(bundleNameTextField);
113
114     componentsLabel.setText("Components:");
115     jPanel5.add(componentsLabel);
116
117     jPanel1.add(jPanel5, java.awt.BorderLayout.NORTH);
118
119     componentsList.setModel(content);
120     jScrollPane1.setViewportView(componentsList);
121
122     jPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER);
123
124     jPanel6.setLayout(new java.awt.GridLayout JavaDoc());
125
126     subcomponentsCheckBox.setText("Include Subcomponents");
127     jPanel6.add(subcomponentsCheckBox);
128
129     jPanel1.add(jPanel6, java.awt.BorderLayout.SOUTH);
130
131     jPanel4.add(jPanel1, java.awt.BorderLayout.CENTER);
132
133     addComponentsButton.setText("Add Components from TR");
134     addComponentsButton.addActionListener(new java.awt.event.ActionListener JavaDoc()
135     {
136       public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt)
137       {
138         addComponentsButtonActionPerformed(evt);
139       }
140     });
141
142     jPanel2.add(addComponentsButton);
143
144     removeComponentsButton.setText("Remove Selected Components");
145     removeComponentsButton.addActionListener(new java.awt.event.ActionListener JavaDoc()
146     {
147       public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt)
148       {
149         removeComponentsButtonActionPerformed(evt);
150       }
151     });
152
153     jPanel2.add(removeComponentsButton);
154
155     jPanel4.add(jPanel2, java.awt.BorderLayout.NORTH);
156
157     createUserBundleButton.setText("Create User Bundle");
158     createUserBundleButton.addActionListener(new java.awt.event.ActionListener JavaDoc()
159     {
160       public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt)
161       {
162         createUserBundleButtonActionPerformed(evt);
163       }
164     });
165
166     jPanel3.add(createUserBundleButton);
167
168     cancelButton.setText("Cancel");
169     cancelButton.addActionListener(new java.awt.event.ActionListener JavaDoc()
170     {
171       public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt)
172       {
173         cancelButtonActionPerformed(evt);
174       }
175     });
176
177     jPanel3.add(cancelButton);
178
179     jPanel4.add(jPanel3, java.awt.BorderLayout.SOUTH);
180
181     getContentPane().add(jPanel4, java.awt.BorderLayout.CENTER);
182
183     pack();
184   }//GEN-END:initComponents
185

186   private void removeComponentsButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt)//GEN-FIRST:event_removeComponentsButtonActionPerformed
187
{//GEN-HEADEREND:event_removeComponentsButtonActionPerformed
188
Object JavaDoc[] selected = componentsList.getSelectedValues();
189     for (int i = 0; i < selected.length; i++)
190     {
191       if (selected[i] instanceof String JavaDoc) components.remove((String JavaDoc)selected[i]);
192     }
193     reloadContent();
194   }//GEN-LAST:event_removeComponentsButtonActionPerformed
195

196   private void addComponentsButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt)//GEN-FIRST:event_addComponentsButtonActionPerformed
197
{//GEN-HEADEREND:event_addComponentsButtonActionPerformed
198
ComponentInfo[] componentInfos = null;
199     try
200     {
201       componentInfos = admin.componentsInTRList();
202     }
203     catch (RemoteException JavaDoc e)
204     {
205       JOptionPane.showMessageDialog(this, "RMI Error occured while accessing SOFA net node:\n" + e, "Error", JOptionPane.ERROR_MESSAGE);
206       return;
207     }
208     
209     String JavaDoc[] compNames = new String JavaDoc[componentInfos.length];
210     int i;
211     for (i = 0; i < componentInfos.length; i++) compNames[i] = componentInfos[i].getName() + "[" + componentInfos[i].getImplementationVersion() + "]";
212     
213     Arrays.sort(compNames);
214     
215     Object JavaDoc[] result = (Object JavaDoc [])ListDialog.showDialog(this, this, "Components in TR:", "Choose Components from TR to create User Bundle", (Object JavaDoc[])compNames, null, null, true, true);
216     if (result != null) for (i = 0; i < result.length; i++) components.add((String JavaDoc)result[i]);
217
218     reloadContent();
219   }//GEN-LAST:event_addComponentsButtonActionPerformed
220

221   private void createUserBundleButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt)//GEN-FIRST:event_createUserBundleButtonActionPerformed
222
{//GEN-HEADEREND:event_createUserBundleButtonActionPerformed
223
String JavaDoc bundleName = bundleNameTextField.getText();
224     boolean subcomponents = subcomponentsCheckBox.isSelected();
225
226     BundleInfo bundleInfo = new BundleInfo();
227     try
228     {
229       bundleInfo.fromBundleName(bundleName);
230     }
231     catch (BundleInfo.InvalidBundleNameException e)
232     {
233       JOptionPane.showMessageDialog(this, "Invalid name of bundle:\n" + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
234       return;
235     }
236     
237     if (bundleInfo.isComponent())
238     {
239       JOptionPane.showMessageDialog(this, "Invalid name of bundle:\nName of User Bundle expected", "Error", JOptionPane.ERROR_MESSAGE);
240       return;
241     }
242
243     ComponentInfo[] componentInfos = new ComponentInfo[components.size()];
244     int i = 0;
245     Iterator it = components.iterator();
246     while (it.hasNext())
247     {
248       String JavaDoc compName = (String JavaDoc)it.next();
249       int leftBracket = compName.lastIndexOf('[');
250       int rightBracket = compName.lastIndexOf(']');
251       componentInfos[i++] = new ComponentInfoImpl(compName.substring(0, leftBracket), compName.substring(leftBracket + 1, rightBracket));
252     }
253     
254     try
255     {
256       admin.createUserBundle(bundleName, componentInfos, subcomponents);
257     }
258     catch (RemoteException JavaDoc e)
259     {
260       JOptionPane.showMessageDialog(this, e, "Error", JOptionPane.ERROR_MESSAGE);
261       dispose();
262       return;
263     }
264     catch (RMIAdminException e)
265     {
266       JOptionPane.showMessageDialog(this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
267       dispose();
268       return;
269     }
270     
271     JOptionPane.showMessageDialog(this, "User Bundle '" + bundleName + "' successfully created");
272     
273     isOK = true;
274     dispose();
275   }//GEN-LAST:event_createUserBundleButtonActionPerformed
276

277   private void cancelButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt)//GEN-FIRST:event_cancelButtonActionPerformed
278
{//GEN-HEADEREND:event_cancelButtonActionPerformed
279
dispose();
280   }//GEN-LAST:event_cancelButtonActionPerformed
281

282   
283   // Variables declaration - do not modify//GEN-BEGIN:variables
284
private javax.swing.JButton JavaDoc addComponentsButton;
285   private javax.swing.JLabel JavaDoc bundleNameLabel;
286   private javax.swing.JTextField JavaDoc bundleNameTextField;
287   private javax.swing.JButton JavaDoc cancelButton;
288   private javax.swing.JLabel JavaDoc componentsLabel;
289   private javax.swing.JList JavaDoc componentsList;
290   private javax.swing.JButton JavaDoc createUserBundleButton;
291   private javax.swing.JPanel JavaDoc jPanel1;
292   private javax.swing.JPanel JavaDoc jPanel2;
293   private javax.swing.JPanel JavaDoc jPanel3;
294   private javax.swing.JPanel JavaDoc jPanel4;
295   private javax.swing.JPanel JavaDoc jPanel5;
296   private javax.swing.JPanel JavaDoc jPanel6;
297   private javax.swing.JScrollPane JavaDoc jScrollPane1;
298   private javax.swing.JButton JavaDoc removeComponentsButton;
299   private javax.swing.JCheckBox JavaDoc subcomponentsCheckBox;
300   // End of variables declaration//GEN-END:variables
301

302 }
303
Popular Tags