001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 *
019 */
020 package org.apache.directory.studio.apacheds.actions;
021
022
023 import org.apache.directory.studio.apacheds.ApacheDsPluginConstants;
024 import org.apache.directory.studio.apacheds.model.Server;
025 import org.apache.directory.studio.apacheds.views.ServersView;
026 import org.eclipse.jface.action.Action;
027 import org.eclipse.jface.action.IAction;
028 import org.eclipse.jface.preference.PreferenceDialog;
029 import org.eclipse.jface.viewers.ISelection;
030 import org.eclipse.jface.viewers.StructuredSelection;
031 import org.eclipse.osgi.util.NLS;
032 import org.eclipse.ui.IWorkbenchWindow;
033 import org.eclipse.ui.IWorkbenchWindowActionDelegate;
034 import org.eclipse.ui.dialogs.PreferencesUtil;
035
036
037 /**
038 * This class implements the properties action for a server.
039 *
040 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
041 * @version $Rev$, $Date$
042 */
043 public class PropertiesAction extends Action implements IWorkbenchWindowActionDelegate
044 {
045 /** The associated view */
046 private ServersView view;
047
048
049 /**
050 * Creates a new instance of PropertiesAction.
051 */
052 public PropertiesAction()
053 {
054 super( Messages.getString( "PropertiesAction.Properties" ) ); //$NON-NLS-1$
055 init();
056 }
057
058
059 /**
060 * Creates a new instance of PropertiesAction.
061 *
062 * @param view
063 * the associated view
064 */
065 public PropertiesAction( ServersView view )
066 {
067 super( Messages.getString( "PropertiesAction.Properties" ) ); //$NON-NLS-1$
068 this.view = view;
069 init();
070 }
071
072
073 /**
074 * Initializes the action.
075 */
076 private void init()
077 {
078 setId( ApacheDsPluginConstants.CMD_PROPERTIES );
079 setActionDefinitionId( ApacheDsPluginConstants.CMD_PROPERTIES );
080 setToolTipText( Messages.getString( "PropertiesAction.PropertiesToolTip" ) ); //$NON-NLS-1$
081 }
082
083
084 /* (non-Javadoc)
085 * @see org.eclipse.jface.action.Action#run()
086 */
087 public void run()
088 {
089 if ( view != null )
090 {
091 StructuredSelection selection = ( StructuredSelection ) view.getViewer().getSelection();
092 if ( !selection.isEmpty() )
093 {
094 Server server = ( Server ) selection.getFirstElement();
095 PreferenceDialog dialog = PreferencesUtil.createPropertyDialogOn( view.getViewSite().getShell(),
096 server, ApacheDsPluginConstants.PROP_SERVER_PROPERTY_PAGE, null, null );
097 dialog.getShell().setText( NLS.bind( Messages.getString( "PropertiesAction.PropertiesFor" ), //$NON-NLS-1$
098 shorten( server.getName(), 30 ) ) );
099 dialog.open();
100 }
101 }
102 }
103
104
105 /**
106 * Shortens the given label to the given maximum length
107 * and filters non-printable characters.
108 *
109 * @param label the label
110 * @param maxLength the max length
111 *
112 * @return the shortened label
113 */
114 public static String shorten( String label, int maxLength )
115 {
116 if ( label == null )
117 {
118 return null;
119 }
120
121 // shorten label
122 if ( maxLength < 3 )
123 {
124 return "..."; //$NON-NLS-1$
125 }
126 if ( label.length() > maxLength )
127 {
128 label = label.substring( 0, maxLength / 2 ) + "..." //$NON-NLS-1$
129 + label.substring( label.length() - maxLength / 2, label.length() );
130
131 }
132
133 // filter non-printable characters
134 StringBuffer sb = new StringBuffer( maxLength + 3 );
135 for ( int i = 0; i < label.length(); i++ )
136 {
137 char c = label.charAt( i );
138 if ( Character.isISOControl( c ) )
139 {
140 sb.append( '.' );
141 }
142 else
143 {
144 sb.append( c );
145 }
146 }
147
148 return sb.toString();
149 }
150
151
152 /* (non-Javadoc)
153 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
154 */
155 public void run( IAction action )
156 {
157 run();
158 }
159
160
161 /* (non-Javadoc)
162 * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
163 */
164 public void dispose()
165 {
166 // Nothing to do
167 }
168
169
170 /* (non-Javadoc)
171 * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
172 */
173 public void init( IWorkbenchWindow window )
174 {
175 // Nothing to do
176 }
177
178
179 /* (non-Javadoc)
180 * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
181 */
182 public void selectionChanged( IAction action, ISelection selection )
183 {
184 // Nothing to do
185 }
186 }