KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > common > actions > NewAttributeAction


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
21 package org.apache.directory.ldapstudio.browser.common.actions;
22
23
24 import org.apache.directory.ldapstudio.browser.common.BrowserCommonActivator;
25 import org.apache.directory.ldapstudio.browser.common.BrowserCommonConstants;
26 import org.apache.directory.ldapstudio.browser.common.wizards.AttributeWizard;
27 import org.apache.directory.ldapstudio.browser.core.internal.model.Attribute;
28 import org.apache.directory.ldapstudio.browser.core.model.IAttribute;
29 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
30 import org.apache.directory.ldapstudio.browser.core.model.ModelModificationException;
31 import org.eclipse.jface.dialogs.MessageDialog;
32 import org.eclipse.jface.resource.ImageDescriptor;
33 import org.eclipse.jface.wizard.WizardDialog;
34 import org.eclipse.swt.widgets.Display;
35
36
37 /**
38  * This Action creates a new Attribute
39  *
40  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
41  * @version $Rev$, $Date$
42  */

43 public class NewAttributeAction extends BrowserAction
44 {
45     /**
46      * Creates a new instance of NewAttributeAction.
47      */

48     public NewAttributeAction()
49     {
50         super();
51     }
52
53
54     /**
55      * {@inheritDoc}
56      */

57     public void dispose()
58     {
59         super.dispose();
60     }
61
62
63     /**
64      * {@inheritDoc}
65      */

66     public void run()
67     {
68
69         IEntry entry = null;
70         if ( getInput() != null && getInput() instanceof IEntry )
71         {
72             entry = ( IEntry ) getInput();
73         }
74         else if ( getSelectedEntries().length > 0 )
75         {
76             entry = getSelectedEntries()[0];
77         }
78         else if ( getSelectedAttributes().length > 0 )
79         {
80             entry = getSelectedAttributes()[0].getEntry();
81         }
82         else if ( getSelectedValues().length > 0 )
83         {
84             entry = getSelectedValues()[0].getAttribute().getEntry();
85         }
86
87         if ( entry != null )
88         {
89             AttributeWizard wizard = new AttributeWizard( "New Attribute", true, true, null, entry );
90             WizardDialog dialog = new WizardDialog( getShell(), wizard );
91             dialog.setBlockOnOpen( true );
92             dialog.create();
93             if ( dialog.open() == WizardDialog.OK )
94             {
95                 String JavaDoc newAttributeDescription = wizard.getAttributeDescription();
96                 if ( newAttributeDescription != null && !"".equals( newAttributeDescription ) )
97                 {
98                     try
99                     {
100                         IAttribute att = entry.getAttribute( newAttributeDescription );
101                         if ( att == null )
102                         {
103                             att = new Attribute( entry, newAttributeDescription );
104                             entry.addAttribute( att ) ;
105                         }
106
107                         att.addEmptyValue();
108                     }
109                     catch ( ModelModificationException mme )
110                     {
111                         MessageDialog.openError( Display.getDefault().getActiveShell(), "Error While Adding Attribute",
112                             mme.getMessage() );
113                     }
114                 }
115             }
116         }
117     }
118
119
120     /**
121      * {@inheritDoc}
122      */

123     public String JavaDoc getText()
124     {
125         return "New Attribute...";
126     }
127
128
129     /**
130      * {@inheritDoc}
131      */

132     public ImageDescriptor getImageDescriptor()
133     {
134         return BrowserCommonActivator.getDefault().getImageDescriptor( BrowserCommonConstants.IMG_ATTRIBUTE_ADD );
135     }
136
137
138     /**
139      * {@inheritDoc}
140      */

141     public String JavaDoc getCommandId()
142     {
143         return "org.apache.directory.ldapstudio.browser.action.addAttribute";
144     }
145
146
147     /**
148      * {@inheritDoc}
149      */

150     public boolean isEnabled()
151     {
152
153         if ( ( getSelectedSearchResults().length == 1 && getSelectedAttributes().length > 0 ) )
154         {
155             return false;
156         }
157
158         return ( ( getInput() != null && getInput() instanceof IEntry ) || getSelectedEntries().length == 1
159             || getSelectedAttributes().length > 0 || getSelectedValues().length > 0 );
160     }
161 }
162
Popular Tags