KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > aciitemeditor > dialogs > MultiValuedDialog


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  */

20 package org.apache.directory.ldapstudio.aciitemeditor.dialogs;
21
22
23 import java.util.List JavaDoc;
24
25 import org.apache.directory.ldapstudio.aciitemeditor.ACIItemValueWithContext;
26 import org.apache.directory.ldapstudio.aciitemeditor.Activator;
27 import org.apache.directory.ldapstudio.browser.core.internal.model.Attribute;
28 import org.apache.directory.ldapstudio.browser.core.internal.model.Value;
29 import org.apache.directory.ldapstudio.browser.core.model.IAttribute;
30 import org.apache.directory.ldapstudio.browser.core.model.IValue;
31 import org.apache.directory.ldapstudio.browser.core.model.ModelModificationException;
32 import org.apache.directory.ldapstudio.valueeditors.AbstractDialogStringValueEditor;
33 import org.eclipse.jface.dialogs.Dialog;
34 import org.eclipse.jface.dialogs.IDialogConstants;
35 import org.eclipse.jface.viewers.ArrayContentProvider;
36 import org.eclipse.jface.viewers.CellEditor;
37 import org.eclipse.jface.viewers.DoubleClickEvent;
38 import org.eclipse.jface.viewers.IDoubleClickListener;
39 import org.eclipse.jface.viewers.ISelectionChangedListener;
40 import org.eclipse.jface.viewers.IStructuredSelection;
41 import org.eclipse.jface.viewers.LabelProvider;
42 import org.eclipse.jface.viewers.SelectionChangedEvent;
43 import org.eclipse.jface.viewers.TableViewer;
44 import org.eclipse.swt.SWT;
45 import org.eclipse.swt.events.SelectionAdapter;
46 import org.eclipse.swt.events.SelectionEvent;
47 import org.eclipse.swt.layout.GridData;
48 import org.eclipse.swt.layout.GridLayout;
49 import org.eclipse.swt.widgets.Button;
50 import org.eclipse.swt.widgets.Composite;
51 import org.eclipse.swt.widgets.Control;
52 import org.eclipse.swt.widgets.Shell;
53 import org.eclipse.swt.widgets.Table;
54
55
56 /**
57  * Dialog to edit user classes or protected items with multiple values.
58  *
59  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
60  * @version $Rev$, $Date$
61  */

62 public class MultiValuedDialog extends Dialog
63 {
64     /** The dialog title */
65     private String JavaDoc displayName;
66
67     /** The value editor */
68     private AbstractDialogStringValueEditor valueEditor;
69
70     /** The values, may be empty. */
71     private List JavaDoc<String JavaDoc> values;
72
73     /** The context */
74     private ACIItemValueWithContext context;
75
76     /** The inner composite for all the content */
77     private Composite composite = null;
78
79     /** The table control for the table viewer */
80     private Table table = null;
81
82     /** The table viewer containing all user classes */
83     private TableViewer tableViewer = null;
84
85     /** The composite containing the buttons */
86     private Composite buttonComposite = null;
87
88     /** The add button */
89     private Button addButton = null;
90
91     /** The edit button */
92     private Button editButton = null;
93
94     /** The delete button */
95     private Button deleteButton = null;
96
97
98     /**
99      * Creates a new instance of MultiValuedDialog.
100      *
101      * @param parentShell the shell
102      * @param displayName the display name of the edited element
103      * @param values a modifyable list of values
104      * @param context the context
105      * @param valueEditor the detail value editor
106      */

107     public MultiValuedDialog( Shell parentShell, String JavaDoc displayName, List JavaDoc<String JavaDoc> values,
108         ACIItemValueWithContext context, AbstractDialogStringValueEditor valueEditor )
109     {
110         super( parentShell );
111         super.setShellStyle( super.getShellStyle() | SWT.RESIZE );
112
113         this.displayName = displayName;
114         this.values = values;
115         this.context = context;
116         this.valueEditor = valueEditor;
117     }
118
119
120     /**
121      * {@inheritDoc}
122      *
123      * Sets the dialog title.
124      */

125     protected void configureShell( Shell shell )
126     {
127         super.configureShell( shell );
128         shell.setText( Messages.getString("MultiValuedDialog.dialog.titlePrefix") + displayName ); //$NON-NLS-1$
129
shell.setImage( Activator.getDefault().getImage( Messages.getString("MultiValuedDialog.dialog.icon") ) ); //$NON-NLS-1$
130
}
131
132
133     /**
134      * {@inheritDoc}
135      *
136      * Creates only a OK button.
137      */

138     protected void createButtonsForButtonBar( Composite parent )
139     {
140         createButton( parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, false );
141     }
142
143
144     /**
145      * {@inheritDoc}
146      */

147     protected Control createDialogArea( Composite parent )
148     {
149         // create composite
150
composite = ( Composite ) super.createDialogArea( parent );
151         GridData gd = new GridData( GridData.FILL_BOTH );
152         gd.widthHint = convertHorizontalDLUsToPixels( IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH );
153         gd.heightHint = convertHorizontalDLUsToPixels( IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH / 2 );
154         composite.setLayoutData( gd );
155         GridLayout layout = ( GridLayout ) composite.getLayout();
156         layout.makeColumnsEqualWidth = false;
157         layout.numColumns = 2;
158
159         createTable();
160
161         createButtonComposite();
162
163         applyDialogFont( composite );
164         return composite;
165     }
166
167
168     /**
169      * This method initializes table and table viewer
170      */

171     private void createTable()
172     {
173         GridData tableGridData = new GridData( GridData.FILL_BOTH );
174         tableGridData.grabExcessHorizontalSpace = true;
175         tableGridData.verticalAlignment = GridData.FILL;
176         tableGridData.horizontalAlignment = GridData.FILL;
177         //tableGridData.heightHint = 100;
178

179         table = new Table( composite, SWT.BORDER );
180         table.setHeaderVisible( false );
181         table.setLayoutData( tableGridData );
182         table.setLinesVisible( false );
183         tableViewer = new TableViewer( table );
184         tableViewer.setContentProvider( new ArrayContentProvider() );
185         tableViewer.setLabelProvider( new LabelProvider() );
186         tableViewer.setInput( values );
187
188         tableViewer.addSelectionChangedListener( new ISelectionChangedListener()
189         {
190             public void selectionChanged( SelectionChangedEvent event )
191             {
192                 valueSelected();
193             }
194         } );
195
196         tableViewer.addDoubleClickListener( new IDoubleClickListener()
197         {
198             public void doubleClick( DoubleClickEvent event )
199             {
200                 editValue();
201             }
202         } );
203     }
204
205
206     /**
207      * This method initializes buttons
208      */

209     private void createButtonComposite()
210     {
211         GridData deleteButtonGridData = new GridData();
212         deleteButtonGridData.horizontalAlignment = GridData.FILL;
213         deleteButtonGridData.grabExcessHorizontalSpace = false;
214         deleteButtonGridData.verticalAlignment = GridData.BEGINNING;
215         deleteButtonGridData.widthHint = Activator.getButtonWidth( composite );
216
217         GridData editButtonGridData = new GridData();
218         editButtonGridData.horizontalAlignment = GridData.FILL;
219         editButtonGridData.grabExcessHorizontalSpace = false;
220         editButtonGridData.verticalAlignment = GridData.BEGINNING;
221         editButtonGridData.widthHint = Activator.getButtonWidth( composite );
222
223         GridData addButtonGridData = new GridData();
224         addButtonGridData.horizontalAlignment = GridData.FILL;
225         addButtonGridData.grabExcessHorizontalSpace = false;
226         addButtonGridData.verticalAlignment = GridData.BEGINNING;
227         addButtonGridData.widthHint = Activator.getButtonWidth( composite );
228
229         GridLayout gridLayout = new GridLayout();
230         gridLayout.marginWidth = 0;
231         gridLayout.marginHeight = 0;
232         GridData gridData = new GridData();
233         gridData.horizontalAlignment = GridData.CENTER;
234         gridData.grabExcessHorizontalSpace = false;
235         gridData.grabExcessVerticalSpace = false;
236         gridData.verticalAlignment = GridData.FILL;
237
238         buttonComposite = new Composite( composite, SWT.NONE );
239         buttonComposite.setLayoutData( gridData );
240         buttonComposite.setLayout( gridLayout );
241
242         addButton = new Button( buttonComposite, SWT.NONE );
243         addButton.setText( Messages.getString( "MultiValuedDialog.button.add" ) ); //$NON-NLS-1$
244
addButton.setLayoutData( addButtonGridData );
245         addButton.addSelectionListener( new SelectionAdapter()
246         {
247             public void widgetSelected( SelectionEvent e )
248             {
249                 addValue();
250             }
251         } );
252
253         editButton = new Button( buttonComposite, SWT.NONE );
254         editButton.setText( Messages.getString( "MultiValuedDialog.button.edit" ) ); //$NON-NLS-1$
255
editButton.setLayoutData( editButtonGridData );
256         editButton.addSelectionListener( new SelectionAdapter()
257         {
258             public void widgetSelected( SelectionEvent e )
259             {
260                 editValue();
261             }
262         } );
263         editButton.setEnabled( false );
264
265         deleteButton = new Button( buttonComposite, SWT.NONE );
266         deleteButton.setText( Messages.getString( "MultiValuedDialog.button.delete" ) ); //$NON-NLS-1$
267
deleteButton.setLayoutData( deleteButtonGridData );
268         deleteButton.addSelectionListener( new SelectionAdapter()
269         {
270             public void widgetSelected( SelectionEvent e )
271             {
272                 deleteValue();
273             }
274         } );
275         deleteButton.setEnabled( false );
276
277     }
278
279
280     /**
281      * Opens the editor and adds the new value to the list.
282      */

283     private void addValue()
284     {
285         
286         Object JavaDoc oldRawValue;
287         try
288         {
289             IAttribute attribute = new Attribute( context.getEntry(), "" ); //$NON-NLS-1$
290
IValue value = new Value( attribute, "" ); //$NON-NLS-1$
291
oldRawValue = valueEditor.getRawValue( value ); //$NON-NLS-1$
292
}
293         catch ( ModelModificationException e )
294         {
295             oldRawValue = valueEditor.getRawValue( context.getConnection(), "" ); //$NON-NLS-1$
296
}
297         
298
299         CellEditor cellEditor = valueEditor.getCellEditor();
300         cellEditor.setValue( oldRawValue );
301         cellEditor.activate();
302         Object JavaDoc newRawValue = cellEditor.getValue();
303
304         if ( newRawValue != null )
305         {
306             String JavaDoc newValue = ( String JavaDoc ) valueEditor.getStringOrBinaryValue( newRawValue );
307
308             values.add( newValue );
309             tableViewer.refresh();
310         }
311     }
312
313
314     /**
315      * Opens the editor with the currently selected
316      * value and puts the modified value into the list.
317      */

318     private void editValue()
319     {
320         String JavaDoc oldValue = getSelectedValue();
321         if ( oldValue != null )
322         {
323             Object JavaDoc oldRawValue;
324             try
325             {
326                 IAttribute attribute = new Attribute( context.getEntry(), "" ); //$NON-NLS-1$
327
IValue value = new Value( attribute, oldValue ); //$NON-NLS-1$
328
oldRawValue = valueEditor.getRawValue( value ); //$NON-NLS-1$
329
}
330             catch ( ModelModificationException e )
331             {
332                 oldRawValue = valueEditor.getRawValue( context.getConnection(), oldValue );
333             }
334
335             CellEditor cellEditor = valueEditor.getCellEditor();
336             cellEditor.setValue( oldRawValue );
337             cellEditor.activate();
338             Object JavaDoc newRawValue = cellEditor.getValue();
339
340             if ( newRawValue != null )
341             {
342                 String JavaDoc newValue = ( String JavaDoc ) valueEditor.getStringOrBinaryValue( newRawValue );
343
344                 values.remove( oldValue );
345                 values.add( newValue );
346                 tableViewer.refresh();
347             }
348         }
349     }
350
351
352     /**
353      * Deletes the currently selected value from list.
354      */

355     private void deleteValue()
356     {
357         String JavaDoc value = getSelectedValue();
358         if ( value != null )
359         {
360             values.remove( value );
361             tableViewer.refresh();
362         }
363     }
364
365
366     /**
367      * Called when value is selected in table viewer.
368      * Updates the enabled/disabled state of the buttons.
369      */

370     private void valueSelected()
371     {
372         String JavaDoc value = getSelectedValue();
373
374         if ( value == null )
375         {
376             editButton.setEnabled( false );
377             deleteButton.setEnabled( false );
378         }
379         else
380         {
381             editButton.setEnabled( true );
382             deleteButton.setEnabled( true );
383         }
384     }
385
386
387     /**
388      * @return the value that is selected in the table viewer, or null.
389      */

390     private String JavaDoc getSelectedValue()
391     {
392         String JavaDoc value = null;
393
394         IStructuredSelection selection = ( IStructuredSelection ) tableViewer.getSelection();
395         if ( !selection.isEmpty() )
396         {
397             Object JavaDoc element = selection.getFirstElement();
398             if ( element instanceof String JavaDoc )
399             {
400                 value = ( String JavaDoc ) element;
401             }
402         }
403
404         return value;
405     }
406
407 }
408
Popular Tags