View Javadoc

1   /*
2    WSMO Studio - a Semantic Web Service Editor
3    Copyright (c) 2004-2006, OntoText Lab. / SIRMA Group
4    
5    This library is free software; you can redistribute it and/or modify it under
6    the terms of the GNU Lesser General Public License as published by the Free
7    Software Foundation; either version 2.1 of the License, or (at your option)
8    any later version.
9    This library is distributed in the hope that it will be useful, but WITHOUT
10   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   details.
13   You should have received a copy of the GNU Lesser General Public License along
14   with this library; if not, write to the Free Software Foundation, Inc.,
15   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16   */
17  
18  /***
19   * <p>Title: WSMO Studio</p>
20   * <p>Description: Semantic Web Service Editor</p>
21   * <p>Copyright:  Copyright (c) 2004-2006</p>
22   * <p>Company: OntoText Lab. / SIRMA </p>
23   */
24  
25  package org.wsmostudio.repository.ui;
26  
27  
28  import java.util.HashSet;
29  import java.util.Set;
30  
31  import org.eclipse.jface.dialogs.MessageDialog;
32  import org.eclipse.jface.resource.JFaceResources;
33  import org.eclipse.swt.SWT;
34  import org.eclipse.swt.events.SelectionAdapter;
35  import org.eclipse.swt.events.SelectionEvent;
36  import org.eclipse.swt.graphics.Image;
37  import org.eclipse.swt.graphics.Point;
38  import org.eclipse.swt.layout.FillLayout;
39  import org.eclipse.swt.layout.GridData;
40  import org.eclipse.swt.layout.GridLayout;
41  import org.eclipse.swt.widgets.Button;
42  import org.eclipse.swt.widgets.Combo;
43  import org.eclipse.swt.widgets.Composite;
44  import org.eclipse.swt.widgets.Display;
45  import org.eclipse.swt.widgets.Group;
46  import org.eclipse.swt.widgets.Label;
47  import org.eclipse.swt.widgets.Shell;
48  import org.eclipse.swt.widgets.Text;
49  import org.wsmostudio.repository.ExtensionManager;
50  import org.wsmostudio.runtime.WsmoImageRegistry;
51  
52  /***
53   * This dialog appears when the user creates a new repository instance from
54   * the <i>Repositories Explorer</i> view. The implementation retrieves all
55   * registered repository extensions (of point <code>org.wsmostudio.repository.Repository</code>),
56   * and gives a choice to the user.
57   *
58   * @author not attributable
59   * @version $Revision: 888 $ $Date: 2006-07-20 18:15:52 +0300 $
60   */
61  
62  public class NewRepositoryDialog {
63  
64      private Shell shell;
65      
66      private Text nameField;
67      private Combo typeSelector;
68      
69      private String resultName;
70      private String resultType;
71      
72      public NewRepositoryDialog(Shell parentShell) {
73          shell = new Shell(parentShell, SWT.DIALOG_TRIM | SWT.RESIZE | SWT.SYSTEM_MODAL);
74          Image icon = JFaceResources.getImageRegistry().get(WsmoImageRegistry.DEFAULT_WINDOW_ICON);
75          shell.setImage(icon);
76          shell.setText("New Repository");
77          shell.setLayout(new GridLayout(1, true));
78          Point pLocation = parentShell.getLocation();
79          Point pSize = parentShell.getSize();
80  
81          createDialogArea(shell);
82          createControlArea(shell);
83          
84          shell.pack();
85          shell.setSize(400, shell.getSize().y);
86  
87          shell.setLocation(pLocation.x + pSize.x / 2 - shell.getSize().x / 2,
88                  pLocation.y + pSize.y / 2 - shell.getSize().y / 2);
89      }
90      
91      private void createDialogArea(Composite comp) {
92          
93          Group mainComp = new Group(comp, SWT.NONE);
94          mainComp.setLayout(new GridLayout(2, false));
95          mainComp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
96          
97          new Label(mainComp, SWT.NONE).setText("Name : ");
98          
99          nameField = new Text(mainComp, SWT.BORDER | SWT.SINGLE);
100         nameField.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
101         
102         new Label(mainComp, SWT.NONE).setText("Type : ");
103         
104         typeSelector = new Combo(mainComp, SWT.READ_ONLY);
105         typeSelector.setItems(
106                 filterForRelevantTypes(ExtensionManager.listConfigTypes()));
107     	typeSelector.setLayoutData(new GridData(GridData.FILL_BOTH));
108     	if (typeSelector.getItemCount() > 0) {
109     	    typeSelector.select(0);
110     	}
111     	else {
112     	    typeSelector.add("< No repository extensions found >");
113     	    typeSelector.select(0);
114     	    typeSelector.setEnabled(false);
115     	}
116     } 
117     
118     private String[] filterForRelevantTypes(String[] allTypes) {
119         Set<String> instanciatedTypes = ExtensionManager.collectExistingRepositoriesTypes();
120         Set<String> result = new HashSet<String>();
121         for (int i = 0; i < allTypes.length; i++) {
122             try {
123                 if (false == instanciatedTypes.contains(allTypes[i])
124                         || ExtensionManager.allowsMultiInstances(allTypes[i])) {
125                     result.add(allTypes[i]);
126                 }
127             }
128             catch(Exception ex) {
129                 continue;
130             }
131         }
132         return result.toArray(new String[result.size()]);
133     }
134     
135     public boolean open() {
136         shell.open();
137         Display display = shell.getDisplay();
138         while (!shell.isDisposed()) {
139             if (!display.readAndDispatch()) {
140                 display.sleep();
141             }
142         }
143         return resultName != null;
144     }
145     
146     public String getRepositoryName() {
147         return resultName;
148     }
149     
150     public String getRepositoryType() {
151         return resultType;
152     }
153     
154     private void createControlArea(Composite mainContainer) {
155         Composite buttons = new Composite(mainContainer, SWT.NONE);
156         buttons.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
157         FillLayout layout = new FillLayout(SWT.HORIZONTAL);
158         layout.spacing = 3;
159         buttons.setLayout(layout);
160 
161         Button okButton = new Button(buttons, SWT.PUSH);
162         okButton.setText("OK");
163         okButton.addSelectionListener(new SelectionAdapter() {
164             public void widgetSelected(SelectionEvent e) {
165                 if (doUpdate()) {
166                     shell.dispose();
167                 }
168             }
169         });
170         okButton.setEnabled(typeSelector.isEnabled());
171         shell.setDefaultButton(okButton);
172         
173         Button noButton = new Button(buttons, SWT.PUSH);
174         noButton.setText("Cancel");
175         noButton.addSelectionListener(new SelectionAdapter() {
176             public void widgetSelected(SelectionEvent e) {
177                 resultName = null;
178                 shell.dispose();
179             }
180         });
181     }
182     
183     private boolean doUpdate() {
184         String newRepName = nameField.getText();
185         if (newRepName == null
186                 || newRepName.trim().length() == 0) {
187             MessageDialog.openError(shell, 
188                                     "No Name", 
189                                     "No repository name specified!");
190             return false;
191         }
192         int typeIndex = typeSelector.getSelectionIndex();
193         if (typeIndex == -1) {
194             MessageDialog.openError(shell, 
195                                     "No Type", 
196                                     "No repository type selected!");
197             return false;
198         }
199         resultName = newRepName.trim();
200         resultType = typeSelector.getItems()[typeIndex];
201         return true;
202     }
203 }
204 
205 /*
206  * $Log$
207  * Revision 1.9  2006/07/20 15:13:18  alex_simov
208  * ui fixes
209  *
210  * Revision 1.8  2006/07/18 13:29:19  alex_simov
211  * bugfix/workaround [1484820] : usage of FillLayout replaced by GridLayout,
212  * previously causing empty dialog windows under Linux - gtk
213  *
214  * Revision 1.7  2006/01/09 12:51:11  alex_simov
215  * Copyright message in header updated
216  *
217  * Revision 1.6  2005/12/01 14:10:27  alex_simov
218  * no message
219  *
220  * Revision 1.5  2005/07/21 11:46:32  alex_simov
221  * added javadoc: class description, footer
222  *
223  */
224